Straightened out implicit coercions for indexed types
[ghc-hetmet.git] / compiler / iface / IfaceSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
7 %*                                                                      *
8 %************************************************************************
9
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well.  Currently trying the former... MEGA SIGH.
12
13 \begin{code}
14 module IfaceSyn (
15         module IfaceType,               -- Re-export all this
16
17         IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..), IfaceConDecls(..),
18         IfaceExpr(..), IfaceAlt, IfaceNote(..),
19         IfaceBinding(..), IfaceConAlt(..), IfaceIdInfo(..),
20         IfaceInfoItem(..), IfaceRule(..), IfaceInst(..), 
21
22         -- Misc
23         visibleIfConDecls,
24
25         -- Equality
26         IfaceEq(..), (&&&), bool, eqListBy, eqMaybeBy,
27         eqIfDecl, eqIfInst, eqIfRule, checkBootDecl,
28         
29         -- Pretty printing
30         pprIfaceExpr, pprIfaceDecl, pprIfaceDeclHead 
31     ) where
32
33 #include "HsVersions.h"
34
35 import CoreSyn
36 import IfaceType
37
38 import NewDemand        ( StrictSig, pprIfaceStrictSig )
39 import TcType           ( deNoteType )
40 import Class            ( FunDep, DefMeth, pprFundeps )
41 import OccName          ( OccName, parenSymOcc, occNameFS,
42                           OccSet, unionOccSets, unitOccSet )
43 import UniqFM           ( UniqFM, emptyUFM, addToUFM, lookupUFM )
44 import Name             ( Name, NamedThing(..), nameOccName, isExternalName )
45 import CostCentre       ( CostCentre, pprCostCentreCore )
46 import Literal          ( Literal )
47 import ForeignCall      ( ForeignCall )
48 import BasicTypes       ( Arity, Activation(..), StrictnessMark, OverlapFlag,
49                           RecFlag(..), Boxity(..), 
50                           isAlwaysActive, tupleParens )
51 import Outputable
52 import FastString
53 import Maybes           ( catMaybes )
54 import Util             ( lengthIs )
55
56 infixl 3 &&&
57 infix  4 `eqIfExt`, `eqIfIdInfo`, `eqIfType`
58 \end{code}
59
60
61 %************************************************************************
62 %*                                                                      *
63                 Data type declarations
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 data IfaceDecl 
69   = IfaceId { ifName   :: OccName,
70               ifType   :: IfaceType, 
71               ifIdInfo :: IfaceIdInfo }
72
73   | IfaceData { ifName       :: OccName,        -- Type constructor
74                 ifTyVars     :: [IfaceTvBndr],  -- Type variables
75                 ifCtxt       :: IfaceContext,   -- The "stupid theta"
76                 ifCons       :: IfaceConDecls,  -- Includes new/data info
77                 ifRec        :: RecFlag,        -- Recursive or not?
78                 ifGadtSyntax :: Bool,           -- True <=> declared using
79                                                 -- GADT syntax 
80                 ifGeneric    :: Bool,           -- True <=> generic converter
81                                                 --          functions available
82                                                 -- We need this for imported
83                                                 -- data decls, since the
84                                                 -- imported modules may have
85                                                 -- been compiled with
86                                                 -- different flags to the
87                                                 -- current compilation unit 
88                 ifFamInst    :: Maybe           -- Just _ <=> instance of fam
89                                   (IfaceTyCon,  --   Family tycon
90                                    [IfaceType], --   Instance types
91                                    Int    )     --   Unique index for naming
92     }
93
94   | IfaceSyn  { ifName    :: OccName,           -- Type constructor
95                 ifTyVars  :: [IfaceTvBndr],     -- Type variables
96                 ifOpenSyn :: Bool,              -- Is an open family?
97                 ifSynRhs  :: IfaceType          -- Type for an ordinary
98                                                 -- synonym and kind for an
99                                                 -- open family
100     }
101
102   | IfaceClass { ifCtxt    :: IfaceContext,     -- Context...
103                  ifName    :: OccName,          -- Name of the class
104                  ifTyVars  :: [IfaceTvBndr],    -- Type variables
105                  ifFDs     :: [FunDep FastString], -- Functional dependencies
106                  ifATs     :: [IfaceDecl],      -- Associated type families
107                  ifSigs    :: [IfaceClassOp],   -- Method signatures
108                  ifRec     :: RecFlag           -- Is newtype/datatype associated with the class recursive?
109     }
110
111   | IfaceForeign { ifName :: OccName,           -- Needs expanding when we move beyond .NET
112                    ifExtName :: Maybe FastString }
113
114 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
115         -- Nothing    => no default method
116         -- Just False => ordinary polymorphic default method
117         -- Just True  => generic default method
118
119 data IfaceConDecls
120   = IfAbstractTyCon             -- No info
121   | IfOpenDataTyCon             -- Open data family
122   | IfOpenNewTyCon              -- Open newtype family
123   | IfDataTyCon [IfaceConDecl]  -- data type decls
124   | IfNewTyCon  IfaceConDecl    -- newtype decls
125
126 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
127 visibleIfConDecls IfAbstractTyCon  = []
128 visibleIfConDecls IfOpenDataTyCon  = []
129 visibleIfConDecls IfOpenNewTyCon   = []
130 visibleIfConDecls (IfDataTyCon cs) = cs
131 visibleIfConDecls (IfNewTyCon c)   = [c]
132
133 data IfaceConDecl 
134   = IfCon {
135         ifConOcc     :: OccName,                -- Constructor name
136         ifConInfix   :: Bool,                   -- True <=> declared infix
137         ifConUnivTvs :: [IfaceTvBndr],          -- Universal tyvars
138         ifConExTvs   :: [IfaceTvBndr],          -- Existential tyvars
139         ifConEqSpec  :: [(OccName,IfaceType)],  -- Equality contraints
140         ifConCtxt    :: IfaceContext,           -- Non-stupid context
141         ifConArgTys  :: [IfaceType],            -- Arg types
142         ifConFields  :: [OccName],              -- ...ditto... (field labels)
143         ifConStricts :: [StrictnessMark]}       -- Empty (meaning all lazy),
144                                                 -- or 1-1 corresp with arg tys
145
146 data IfaceInst 
147   = IfaceInst { ifInstCls  :: IfaceExtName,             -- See comments with
148                 ifInstTys  :: [Maybe IfaceTyCon],       -- the defn of Instance
149                 ifDFun     :: OccName,                  -- The dfun
150                 ifOFlag    :: OverlapFlag,              -- Overlap flag
151                 ifInstOrph :: Maybe OccName }           -- See is_orph in defn of Instance
152         -- There's always a separate IfaceDecl for the DFun, which gives 
153         -- its IdInfo with its full type and version number.
154         -- The instance declarations taken together have a version number,
155         -- and we don't want that to wobble gratuitously
156         -- If this instance decl is *used*, we'll record a usage on the dfun;
157         -- and if the head does not change it won't be used if it wasn't before
158
159 data IfaceRule
160   = IfaceRule { 
161         ifRuleName   :: RuleName,
162         ifActivation :: Activation,
163         ifRuleBndrs  :: [IfaceBndr],    -- Tyvars and term vars
164         ifRuleHead   :: IfaceExtName,   -- Head of lhs
165         ifRuleArgs   :: [IfaceExpr],    -- Args of LHS
166         ifRuleRhs    :: IfaceExpr,
167         ifRuleOrph   :: Maybe OccName   -- Just like IfaceInst
168     }
169
170 data IfaceIdInfo
171   = NoInfo                      -- When writing interface file without -O
172   | HasInfo [IfaceInfoItem]     -- Has info, and here it is
173
174 -- Here's a tricky case:
175 --   * Compile with -O module A, and B which imports A.f
176 --   * Change function f in A, and recompile without -O
177 --   * When we read in old A.hi we read in its IdInfo (as a thunk)
178 --      (In earlier GHCs we used to drop IdInfo immediately on reading,
179 --       but we do not do that now.  Instead it's discarded when the
180 --       ModIface is read into the various decl pools.)
181 --   * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
182 --      and so gives a new version.
183
184 data IfaceInfoItem
185   = HsArity      Arity
186   | HsStrictness StrictSig
187   | HsInline     Activation
188   | HsUnfold     IfaceExpr
189   | HsNoCafRefs
190   | HsWorker     IfaceExtName Arity     -- Worker, if any see IdInfo.WorkerInfo
191                                         -- for why we want arity here.
192         -- NB: we need IfaceExtName (not just OccName) because the worker
193         --     can simplify to a function in another module.
194 -- NB: Specialisations and rules come in separately and are
195 -- only later attached to the Id.  Partial reason: some are orphans.
196
197 --------------------------------
198 data IfaceExpr
199   = IfaceLcl    FastString
200   | IfaceExt    IfaceExtName
201   | IfaceType   IfaceType
202   | IfaceTuple  Boxity [IfaceExpr]              -- Saturated; type arguments omitted
203   | IfaceLam    IfaceBndr IfaceExpr
204   | IfaceApp    IfaceExpr IfaceExpr
205   | IfaceCase   IfaceExpr FastString IfaceType [IfaceAlt]
206   | IfaceLet    IfaceBinding  IfaceExpr
207   | IfaceNote   IfaceNote IfaceExpr
208   | IfaceCast   IfaceExpr IfaceCoercion
209   | IfaceLit    Literal
210   | IfaceFCall  ForeignCall IfaceType
211
212 data IfaceNote = IfaceSCC CostCentre
213                | IfaceInlineMe
214                | IfaceCoreNote String
215
216 type IfaceAlt = (IfaceConAlt, [FastString], IfaceExpr)
217         -- Note: FastString, not IfaceBndr (and same with the case binder)
218         -- We reconstruct the kind/type of the thing from the context
219         -- thus saving bulk in interface files
220
221 data IfaceConAlt = IfaceDefault
222                  | IfaceDataAlt OccName
223                  | IfaceTupleAlt Boxity
224                  | IfaceLitAlt Literal
225
226 data IfaceBinding
227   = IfaceNonRec IfaceIdBndr IfaceExpr
228   | IfaceRec    [(IfaceIdBndr, IfaceExpr)]
229 \end{code}
230
231
232 %************************************************************************
233 %*                                                                      *
234 \subsection[HsCore-print]{Printing Core unfoldings}
235 %*                                                                      *
236 %************************************************************************
237
238 ----------------------------- Printing IfaceDecl ------------------------------------
239
240 \begin{code}
241 instance Outputable IfaceDecl where
242   ppr = pprIfaceDecl
243
244 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
245   = sep [ ppr var <+> dcolon <+> ppr ty, 
246           nest 2 (ppr info) ]
247
248 pprIfaceDecl (IfaceForeign {ifName = tycon})
249   = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
250
251 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars, 
252                         ifOpenSyn = False, ifSynRhs = mono_ty})
253   = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
254        4 (equals <+> ppr mono_ty)
255
256 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars, 
257                         ifOpenSyn = True, ifSynRhs = mono_ty})
258   = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
259        4 (dcolon <+> ppr mono_ty)
260
261 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
262                          ifTyVars = tyvars, ifCons = condecls, 
263                          ifRec = isrec, ifFamInst = mbFamInst})
264   = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
265        4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
266                 pprFamily mbFamInst])
267   where
268     pp_nd = case condecls of
269                 IfAbstractTyCon -> ptext SLIT("data")
270                 IfOpenDataTyCon -> ptext SLIT("data family")
271                 IfDataTyCon _   -> ptext SLIT("data")
272                 IfNewTyCon _    -> ptext SLIT("newtype")
273                 IfOpenNewTyCon  -> ptext SLIT("newtype family")
274
275 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars, 
276                           ifFDs = fds, ifATs = ats, ifSigs = sigs, 
277                           ifRec = isrec})
278   = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
279        4 (vcat [pprRec isrec,
280                 sep (map ppr ats),
281                 sep (map ppr sigs)])
282
283 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
284 pprGen True  = ptext SLIT("Generics: yes")
285 pprGen False = ptext SLIT("Generics: no")
286
287 pprFamily Nothing                  = ptext SLIT("FamilyInstance: none")
288 pprFamily (Just (fam, tys, index)) = ptext SLIT("FamilyInstance:") <+> 
289                                      ppr fam <+> hsep (map ppr tys) <+>
290                                      brackets (ppr index)
291
292 instance Outputable IfaceClassOp where
293    ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
294
295 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
296 pprIfaceDeclHead context thing tyvars
297   = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing), 
298           pprIfaceTvBndrs tyvars]
299
300 pp_condecls tc IfAbstractTyCon  = ptext SLIT("{- abstract -}")
301 pp_condecls tc IfOpenNewTyCon   = empty
302 pp_condecls tc (IfNewTyCon c)   = equals <+> pprIfaceConDecl tc c
303 pp_condecls tc IfOpenDataTyCon  = empty
304 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
305                                                              (map (pprIfaceConDecl tc) cs))
306
307 pprIfaceConDecl tc
308         (IfCon { ifConOcc = name, ifConInfix = is_infix, 
309                  ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs, 
310                  ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys, 
311                  ifConStricts = strs, ifConFields = fields })
312   = sep [main_payload,
313          if is_infix then ptext SLIT("Infix") else empty,
314          if null strs then empty 
315               else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
316          if null fields then empty
317               else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
318   where
319     main_payload = ppr name <+> dcolon <+> 
320                    pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) (ppr con_tau)
321
322     eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty) 
323               | (tv,ty) <- eq_spec] 
324     con_tau = foldr1 IfaceFunTy (arg_tys ++ [tc_app])
325     tc_app  = IfaceTyConApp (IfaceTc (LocalTop tc)) 
326                             [IfaceTyVar tv | (tv,_) <- univ_tvs]
327         -- Gruesome, but jsut for debug print
328
329 instance Outputable IfaceRule where
330   ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
331                    ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs }) 
332     = sep [hsep [doubleQuotes (ftext name), ppr act,
333                  ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
334            nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
335                         ptext SLIT("=") <+> ppr rhs])
336       ]
337
338 instance Outputable IfaceInst where
339   ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag, 
340                   ifInstCls = cls, ifInstTys = mb_tcs})
341     = hang (ptext SLIT("instance") <+> ppr flag 
342                 <+> ppr cls <+> brackets (pprWithCommas ppr_mb mb_tcs))
343          2 (equals <+> ppr dfun_id)
344     where
345       ppr_mb Nothing   = dot
346       ppr_mb (Just tc) = ppr tc
347 \end{code}
348
349
350 ----------------------------- Printing IfaceExpr ------------------------------------
351
352 \begin{code}
353 instance Outputable IfaceExpr where
354     ppr e = pprIfaceExpr noParens e
355
356 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
357         -- The function adds parens in context that need
358         -- an atomic value (e.g. function args)
359
360 pprIfaceExpr add_par (IfaceLcl v)       = ppr v
361 pprIfaceExpr add_par (IfaceExt v)       = ppr v
362 pprIfaceExpr add_par (IfaceLit l)       = ppr l
363 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
364 pprIfaceExpr add_par (IfaceType ty)     = char '@' <+> pprParendIfaceType ty
365
366 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
367 pprIfaceExpr add_par (IfaceTuple c as)  = tupleParens c (interpp'SP as)
368
369 pprIfaceExpr add_par e@(IfaceLam _ _)   
370   = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
371                   pprIfaceExpr noParens body])
372   where 
373     (bndrs,body) = collect [] e
374     collect bs (IfaceLam b e) = collect (b:bs) e
375     collect bs e              = (reverse bs, e)
376
377 -- gaw 2004 
378 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
379 -- gaw 2004
380   = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
381                         <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
382                   pprIfaceExpr noParens rhs <+> char '}'])
383
384 -- gaw 2004
385 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
386 -- gaw 2004
387   = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
388                         <+> ppr bndr <+> char '{',
389                   nest 2 (sep (map ppr_alt alts)) <+> char '}'])
390
391 pprIfaceExpr add_par (IfaceCast expr co) = add_par (ptext SLIT("cast") <+> ppr expr <+> ppr co)
392
393 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
394   = add_par (sep [ptext SLIT("let {"), 
395                   nest 2 (ppr_bind (b, rhs)),
396                   ptext SLIT("} in"), 
397                   pprIfaceExpr noParens body])
398
399 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
400   = add_par (sep [ptext SLIT("letrec {"),
401                   nest 2 (sep (map ppr_bind pairs)), 
402                   ptext SLIT("} in"),
403                   pprIfaceExpr noParens body])
404
405 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
406
407 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs, 
408                               arrow <+> pprIfaceExpr noParens rhs]
409
410 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
411 ppr_con_bs con bs                     = ppr con <+> hsep (map ppr bs)
412   
413 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty, 
414                              equals <+> pprIfaceExpr noParens rhs]
415
416 ------------------
417 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
418 pprIfaceApp fun                args = sep (pprIfaceExpr parens fun : args)
419
420 ------------------
421 instance Outputable IfaceNote where
422     ppr (IfaceSCC cc)     = pprCostCentreCore cc
423     ppr IfaceInlineMe     = ptext SLIT("__inline_me")
424     ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
425
426 instance Outputable IfaceConAlt where
427     ppr IfaceDefault      = text "DEFAULT"
428     ppr (IfaceLitAlt l)   = ppr l
429     ppr (IfaceDataAlt d)  = ppr d
430     ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt" 
431         -- IfaceTupleAlt is handled by the case-alternative printer
432
433 ------------------
434 instance Outputable IfaceIdInfo where
435    ppr NoInfo       = empty
436    ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
437
438 ppr_hs_info (HsUnfold unf)      = ptext SLIT("Unfolding:") <+>
439                                         parens (pprIfaceExpr noParens unf)
440 ppr_hs_info (HsInline act)      = ptext SLIT("Inline:") <+> ppr act
441 ppr_hs_info (HsArity arity)     = ptext SLIT("Arity:") <+> int arity
442 ppr_hs_info (HsStrictness str)  = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
443 ppr_hs_info HsNoCafRefs         = ptext SLIT("HasNoCafRefs")
444 ppr_hs_info (HsWorker w a)      = ptext SLIT("Worker:") <+> ppr w <+> int a
445 \end{code}
446
447
448 %************************************************************************
449 %*                                                                      *
450         Equality, for interface file version generaion only
451 %*                                                                      *
452 %************************************************************************
453
454 Equality over IfaceSyn returns an IfaceEq, not a Bool.  The new constructor is
455 EqBut, which gives the set of *locally-defined* things whose version must be equal
456 for the whole thing to be equal.  So the key function is eqIfExt, which compares
457 IfaceExtNames.
458
459 Of course, equality is also done modulo alpha conversion.
460
461 \begin{code}
462 data IfaceEq 
463   = Equal               -- Definitely exactly the same
464   | NotEqual            -- Definitely different
465   | EqBut OccSet        -- The same provided these local things have not changed
466
467 bool :: Bool -> IfaceEq
468 bool True  = Equal
469 bool False = NotEqual
470
471 toBool :: IfaceEq -> Bool
472 toBool Equal     = True
473 toBool (EqBut _) = True
474 toBool NotEqual  = False
475
476 zapEq :: IfaceEq -> IfaceEq     -- Used to forget EqBut information
477 zapEq (EqBut _) = Equal
478 zapEq other     = other
479
480 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
481 Equal       &&& x           = x
482 NotEqual    &&& x           = NotEqual
483 EqBut occs  &&& Equal       = EqBut occs
484 EqBut occs  &&& NotEqual    = NotEqual
485 EqBut occs1 &&& EqBut occs2 = EqBut (occs1 `unionOccSets` occs2)
486
487 ---------------------
488 eqIfExt :: IfaceExtName -> IfaceExtName -> IfaceEq
489 -- This function is the core of the EqBut stuff
490 eqIfExt (ExtPkg mod1 occ1)     (ExtPkg mod2 occ2)     = bool (mod1==mod2 && occ1==occ2)
491 eqIfExt (HomePkg mod1 occ1 v1) (HomePkg mod2 occ2 v2) = bool (mod1==mod2 && occ1==occ2 && v1==v2)
492 eqIfExt (LocalTop occ1)       (LocalTop occ2)      | occ1 == occ2 = EqBut (unitOccSet occ1)
493 eqIfExt (LocalTopSub occ1 p1) (LocalTop occ2)      | occ1 == occ2 = EqBut (unitOccSet p1)
494 eqIfExt (LocalTopSub occ1 p1) (LocalTopSub occ2 _) | occ1 == occ2 = EqBut (unitOccSet p1)
495 eqIfExt n1 n2 = NotEqual
496 \end{code}
497
498
499 \begin{code}
500 ---------------------
501 checkBootDecl :: IfaceDecl      -- The boot decl
502               -> IfaceDecl      -- The real decl
503               -> Bool           -- True <=> compatible
504 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
505   = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
506
507 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
508   = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
509
510 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
511   = ASSERT( ifName d1 == ifName d2 )
512     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
513           eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
514
515 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
516 -- We don't check the recursion flags because the boot-one is
517 -- recursive, to be conservative, but the real one may not be.
518 -- I'm not happy with the way recursive flags are dealt with.
519   = ASSERT( ifName d1    == ifName d2 ) 
520     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
521         eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&& 
522         case ifCons d1 of
523             IfAbstractTyCon -> Equal
524             cons1           -> eq_hsCD env cons1 (ifCons d2)
525
526 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
527   = ASSERT( ifName d1 == ifName d2 )
528     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
529           eqListBy (eq_hsFD env)    (ifFDs d1)  (ifFDs d2) &&&
530           case (ifCtxt d1, ifSigs d1) of
531              ([], [])      -> Equal
532              (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2)  &&&
533                               eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
534
535 checkBootDecl _ _ = False       -- default case
536
537 ---------------------
538 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
539 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
540   = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
541
542 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
543   = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
544
545 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
546   = bool (ifName d1    == ifName d2 && 
547           ifRec d1     == ifRec   d2 && 
548           ifGadtSyntax d1 == ifGadtSyntax   d2 && 
549           ifGeneric d1 == ifGeneric d2) &&&
550     ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
551     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
552             eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&& 
553             eq_hsCD env (ifCons d1) (ifCons d2) 
554         )
555         -- The type variables of the data type do not scope
556         -- over the constructors (any more), but they do scope
557         -- over the stupid context in the IfaceConDecls
558   where
559     Nothing                  `eqIfTc_fam` Nothing                  = Equal
560     (Just (fam1, tys1, co1)) `eqIfTc_fam` (Just (fam2, tys2, co2)) = 
561       fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2 &&& bool (co1 == co2)
562     _                        `eqIfTc_fam` _                        = NotEqual
563
564 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
565   = bool (ifName d1 == ifName d2) &&&
566     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
567           eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
568         )
569
570 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
571   = bool (ifName d1 == ifName d2 && 
572           ifRec d1  == ifRec  d2) &&&
573     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
574           eq_ifContext env (ifCtxt d1) (ifCtxt d2)  &&&
575           eqListBy (eq_hsFD env)    (ifFDs d1)  (ifFDs d2) &&&
576           eqListBy eqIfDecl         (ifATs d1)  (ifATs d2) &&&
577           eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
578        )
579
580 eqIfDecl _ _ = NotEqual -- default case
581
582 -- Helper
583 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
584 eqWith = eq_ifTvBndrs emptyEqEnv
585
586 -----------------------
587 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
588 -- All other changes are handled via the version info on the dfun
589
590 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
591          (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
592        = bool (n1==n2 && a1==a2 && o1 == o2) &&&
593          f1 `eqIfExt` f2 &&&
594          eq_ifBndrs emptyEqEnv bs1 bs2 (\env -> 
595          zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
596                 -- zapEq: for the LHSs, ignore the EqBut part
597          eq_ifaceExpr env rhs1 rhs2)
598
599 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2) 
600   = eqListBy (eq_ConDecl env) c1 c2
601
602 eq_hsCD env (IfNewTyCon c1)  (IfNewTyCon c2)  = eq_ConDecl env c1 c2
603 eq_hsCD env IfAbstractTyCon  IfAbstractTyCon  = Equal
604 eq_hsCD env IfOpenDataTyCon  IfOpenDataTyCon  = Equal
605 eq_hsCD env IfOpenNewTyCon   IfOpenNewTyCon   = Equal
606 eq_hsCD env d1               d2               = NotEqual
607
608 eq_ConDecl env c1 c2
609   = bool (ifConOcc c1     == ifConOcc c2 && 
610           ifConInfix c1   == ifConInfix c2 && 
611           ifConStricts c1 == ifConStricts c2 && 
612           ifConFields c1  == ifConFields c2) &&&
613     eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
614     eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
615         eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
616         eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
617
618 eq_hsFD env (ns1,ms1) (ns2,ms2)
619   = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
620
621 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
622   = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
623 \end{code}
624
625
626 \begin{code}
627 -----------------
628 eqIfIdInfo NoInfo        NoInfo        = Equal
629 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
630 eqIfIdInfo i1            i2 = NotEqual
631
632 eq_item (HsInline a1)      (HsInline a2)      = bool (a1 == a2)
633 eq_item (HsArity a1)       (HsArity a2)       = bool (a1 == a2)
634 eq_item (HsStrictness s1)  (HsStrictness s2)  = bool (s1 == s2)
635 eq_item (HsUnfold u1)   (HsUnfold u2)         = eq_ifaceExpr emptyEqEnv u1 u2
636 eq_item HsNoCafRefs        HsNoCafRefs        = Equal
637 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
638 eq_item _ _ = NotEqual
639
640 -----------------
641 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
642 eq_ifaceExpr env (IfaceLcl v1)        (IfaceLcl v2)        = eqIfOcc env v1 v2
643 eq_ifaceExpr env (IfaceExt v1)        (IfaceExt v2)        = eqIfExt v1 v2
644 eq_ifaceExpr env (IfaceLit l1)        (IfaceLit l2)        = bool (l1 == l2)
645 eq_ifaceExpr env (IfaceFCall c1 ty1)  (IfaceFCall c2 ty2)  = bool (c1==c2) &&& eq_ifType env ty1 ty2
646 eq_ifaceExpr env (IfaceType ty1)      (IfaceType ty2)      = eq_ifType env ty1 ty2
647 eq_ifaceExpr env (IfaceTuple n1 as1)  (IfaceTuple n2 as2)  = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
648 eq_ifaceExpr env (IfaceLam b1 body1)  (IfaceLam b2 body2)  = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
649 eq_ifaceExpr env (IfaceApp f1 a1)     (IfaceApp f2 a2)     = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
650 eq_ifaceExpr env (IfaceCast e1 co1)   (IfaceCast e2 co2)   = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
651 eq_ifaceExpr env (IfaceNote n1 r1)    (IfaceNote n2 r2)    = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
652
653 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
654   = eq_ifaceExpr env s1 s2 &&&
655     eq_ifType env ty1 ty2 &&&
656     eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
657   where
658     eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
659         = bool (eq_ifaceConAlt c1 c2) &&& 
660           eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
661
662 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
663   = eq_ifaceExpr env r1 r2 &&& eq_ifIdBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
664
665 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
666   = eq_ifIdBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
667   where
668     (bs1,rs1) = unzip as1
669     (bs2,rs2) = unzip as2
670
671
672 eq_ifaceExpr env _ _ = NotEqual
673
674 -----------------
675 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
676 eq_ifaceConAlt IfaceDefault       IfaceDefault          = True
677 eq_ifaceConAlt (IfaceDataAlt n1)  (IfaceDataAlt n2)     = n1==n2
678 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2)    = c1==c2
679 eq_ifaceConAlt (IfaceLitAlt l1)   (IfaceLitAlt l2)      = l1==l2
680 eq_ifaceConAlt _ _ = False
681
682 -----------------
683 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
684 eq_ifaceNote env (IfaceSCC c1)    (IfaceSCC c2)        = bool (c1==c2)
685 eq_ifaceNote env IfaceInlineMe    IfaceInlineMe        = Equal
686 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
687 eq_ifaceNote env _ _ = NotEqual
688 \end{code}
689
690 \begin{code}
691 ---------------------
692 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
693
694 -------------------
695 eq_ifType env (IfaceTyVar n1)         (IfaceTyVar n2)         = eqIfOcc env n1 n2
696 eq_ifType env (IfaceAppTy s1 t1)      (IfaceAppTy s2 t2)      = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
697 eq_ifType env (IfacePredTy st1)       (IfacePredTy st2)       = eq_ifPredType env st1 st2
698 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
699 eq_ifType env (IfaceForAllTy tv1 t1)  (IfaceForAllTy tv2 t2)  = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
700 eq_ifType env (IfaceFunTy s1 t1)      (IfaceFunTy s2 t2)      = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
701 eq_ifType env _ _ = NotEqual
702
703 -------------------
704 eq_ifTypes env = eqListBy (eq_ifType env)
705
706 -------------------
707 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
708
709 -------------------
710 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&&  eq_ifTypes env tys1 tys2
711 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2)   = bool (n1 == n2) &&& eq_ifType env ty1 ty2
712 eq_ifPredType env _ _ = NotEqual
713
714 -------------------
715 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
716 eqIfTc IfaceIntTc    IfaceIntTc    = Equal
717 eqIfTc IfaceCharTc   IfaceCharTc   = Equal
718 eqIfTc IfaceBoolTc   IfaceBoolTc   = Equal
719 eqIfTc IfaceListTc   IfaceListTc   = Equal
720 eqIfTc IfacePArrTc   IfacePArrTc   = Equal
721 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
722 eqIfTc _ _ = NotEqual
723 \end{code}
724
725 -----------------------------------------------------------
726         Support code for equality checking
727 -----------------------------------------------------------
728
729 \begin{code}
730 ------------------------------------
731 type EqEnv = UniqFM FastString  -- Tracks the mapping from L-variables to R-variables
732
733 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
734 eqIfOcc env n1 n2 = case lookupUFM env n1 of
735                         Just n1 -> bool (n1 == n2)
736                         Nothing -> bool (n1 == n2)
737
738 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
739 extendEqEnv env n1 n2 | n1 == n2  = env
740                       | otherwise = addToUFM env n1 n2
741
742 emptyEqEnv :: EqEnv
743 emptyEqEnv = emptyUFM
744
745 ------------------------------------
746 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
747
748 eq_ifNakedBndr :: ExtEnv FastString
749 eq_ifBndr      :: ExtEnv IfaceBndr
750 eq_ifTvBndr    :: ExtEnv IfaceTvBndr
751 eq_ifIdBndr    :: ExtEnv IfaceIdBndr
752
753 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
754
755 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
756 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
757 eq_ifBndr _ _ _ _ = NotEqual
758
759 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
760 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
761
762 eq_ifBndrs      :: ExtEnv [IfaceBndr]
763 eq_ifIdBndrs    :: ExtEnv [IfaceIdBndr]
764 eq_ifTvBndrs    :: ExtEnv [IfaceTvBndr]
765 eq_ifNakedBndrs :: ExtEnv [FastString]
766 eq_ifBndrs      = eq_bndrs_with eq_ifBndr
767 eq_ifIdBndrs    = eq_bndrs_with eq_ifIdBndr
768 eq_ifTvBndrs    = eq_bndrs_with eq_ifTvBndr
769 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
770
771 eq_bndrs_with eq env []       []       k = k env
772 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
773 eq_bndrs_with eq env _        _        _ = NotEqual
774 \end{code}
775
776 \begin{code}
777 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
778 eqListBy eq []     []     = Equal
779 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
780 eqListBy eq xs     ys     = NotEqual
781
782 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
783 eqMaybeBy eq Nothing Nothing   = Equal
784 eqMaybeBy eq (Just x) (Just y) = eq x y
785 eqMaybeBy eq x        y        = NotEqual
786 \end{code}