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