Massive patch for the first months work adding System FC to GHC #18
[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 tv) ty) | (tv,ty) <- eq_spec] 
292     con_tau = foldr1 IfaceFunTy (arg_tys ++ [tc_app])
293     tc_app  = IfaceTyConApp (IfaceTc (LocalTop tc)) 
294                             [IfaceTyVar tv | (tv,_) <- univ_tvs]
295         -- Gruesome, but jsut for debug print
296
297 instance Outputable IfaceRule where
298   ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
299                    ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs }) 
300     = sep [hsep [doubleQuotes (ftext name), ppr act,
301                  ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
302            nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
303                         ptext SLIT("=") <+> ppr rhs])
304       ]
305
306 instance Outputable IfaceInst where
307   ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag, 
308                   ifInstCls = cls, ifInstTys = mb_tcs})
309     = hang (ptext SLIT("instance") <+> ppr flag 
310                 <+> ppr cls <+> brackets (pprWithCommas ppr_mb mb_tcs))
311          2 (equals <+> ppr dfun_id)
312     where
313       ppr_mb Nothing   = dot
314       ppr_mb (Just tc) = ppr tc
315 \end{code}
316
317
318 ----------------------------- Printing IfaceExpr ------------------------------------
319
320 \begin{code}
321 instance Outputable IfaceExpr where
322     ppr e = pprIfaceExpr noParens e
323
324 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
325         -- The function adds parens in context that need
326         -- an atomic value (e.g. function args)
327
328 pprIfaceExpr add_par (IfaceLcl v)       = ppr v
329 pprIfaceExpr add_par (IfaceExt v)       = ppr v
330 pprIfaceExpr add_par (IfaceLit l)       = ppr l
331 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
332 pprIfaceExpr add_par (IfaceType ty)     = char '@' <+> pprParendIfaceType ty
333
334 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
335 pprIfaceExpr add_par (IfaceTuple c as)  = tupleParens c (interpp'SP as)
336
337 pprIfaceExpr add_par e@(IfaceLam _ _)   
338   = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
339                   pprIfaceExpr noParens body])
340   where 
341     (bndrs,body) = collect [] e
342     collect bs (IfaceLam b e) = collect (b:bs) e
343     collect bs e              = (reverse bs, e)
344
345 -- gaw 2004 
346 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
347 -- gaw 2004
348   = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
349                         <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
350                   pprIfaceExpr noParens rhs <+> char '}'])
351
352 -- gaw 2004
353 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
354 -- gaw 2004
355   = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
356                         <+> ppr bndr <+> char '{',
357                   nest 2 (sep (map ppr_alt alts)) <+> char '}'])
358
359 pprIfaceExpr add_par (IfaceCast expr co) = add_par (ptext SLIT("cast") <+> ppr expr <+> ppr co)
360
361 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
362   = add_par (sep [ptext SLIT("let {"), 
363                   nest 2 (ppr_bind (b, rhs)),
364                   ptext SLIT("} in"), 
365                   pprIfaceExpr noParens body])
366
367 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
368   = add_par (sep [ptext SLIT("letrec {"),
369                   nest 2 (sep (map ppr_bind pairs)), 
370                   ptext SLIT("} in"),
371                   pprIfaceExpr noParens body])
372
373 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
374
375 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs, 
376                               arrow <+> pprIfaceExpr noParens rhs]
377
378 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
379 ppr_con_bs con bs                     = ppr con <+> hsep (map ppr bs)
380   
381 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty, 
382                              equals <+> pprIfaceExpr noParens rhs]
383
384 ------------------
385 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
386 pprIfaceApp fun                args = sep (pprIfaceExpr parens fun : args)
387
388 ------------------
389 instance Outputable IfaceNote where
390     ppr (IfaceSCC cc)     = pprCostCentreCore cc
391     ppr IfaceInlineMe     = ptext SLIT("__inline_me")
392     ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
393
394 instance Outputable IfaceConAlt where
395     ppr IfaceDefault      = text "DEFAULT"
396     ppr (IfaceLitAlt l)   = ppr l
397     ppr (IfaceDataAlt d)  = ppr d
398     ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt" 
399         -- IfaceTupleAlt is handled by the case-alternative printer
400
401 ------------------
402 instance Outputable IfaceIdInfo where
403    ppr NoInfo       = empty
404    ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
405
406 ppr_hs_info (HsUnfold unf)      = ptext SLIT("Unfolding:") <+>
407                                         parens (pprIfaceExpr noParens unf)
408 ppr_hs_info (HsInline act)      = ptext SLIT("Inline:") <+> ppr act
409 ppr_hs_info (HsArity arity)     = ptext SLIT("Arity:") <+> int arity
410 ppr_hs_info (HsStrictness str)  = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
411 ppr_hs_info HsNoCafRefs         = ptext SLIT("HasNoCafRefs")
412 ppr_hs_info (HsWorker w a)      = ptext SLIT("Worker:") <+> ppr w <+> int a
413 \end{code}
414
415
416 %************************************************************************
417 %*                                                                      *
418         Equality, for interface file version generaion only
419 %*                                                                      *
420 %************************************************************************
421
422 Equality over IfaceSyn returns an IfaceEq, not a Bool.  The new constructor is
423 EqBut, which gives the set of *locally-defined* things whose version must be equal
424 for the whole thing to be equal.  So the key function is eqIfExt, which compares
425 IfaceExtNames.
426
427 Of course, equality is also done modulo alpha conversion.
428
429 \begin{code}
430 data IfaceEq 
431   = Equal               -- Definitely exactly the same
432   | NotEqual            -- Definitely different
433   | EqBut OccSet        -- The same provided these local things have not changed
434
435 bool :: Bool -> IfaceEq
436 bool True  = Equal
437 bool False = NotEqual
438
439 toBool :: IfaceEq -> Bool
440 toBool Equal     = True
441 toBool (EqBut _) = True
442 toBool NotEqual  = False
443
444 zapEq :: IfaceEq -> IfaceEq     -- Used to forget EqBut information
445 zapEq (EqBut _) = Equal
446 zapEq other     = other
447
448 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
449 Equal       &&& x           = x
450 NotEqual    &&& x           = NotEqual
451 EqBut occs  &&& Equal       = EqBut occs
452 EqBut occs  &&& NotEqual    = NotEqual
453 EqBut occs1 &&& EqBut occs2 = EqBut (occs1 `unionOccSets` occs2)
454
455 ---------------------
456 eqIfExt :: IfaceExtName -> IfaceExtName -> IfaceEq
457 -- This function is the core of the EqBut stuff
458 eqIfExt (ExtPkg mod1 occ1)     (ExtPkg mod2 occ2)     = bool (mod1==mod2 && occ1==occ2)
459 eqIfExt (HomePkg mod1 occ1 v1) (HomePkg mod2 occ2 v2) = bool (mod1==mod2 && occ1==occ2 && v1==v2)
460 eqIfExt (LocalTop occ1)       (LocalTop occ2)      | occ1 == occ2 = EqBut (unitOccSet occ1)
461 eqIfExt (LocalTopSub occ1 p1) (LocalTop occ2)      | occ1 == occ2 = EqBut (unitOccSet p1)
462 eqIfExt (LocalTopSub occ1 p1) (LocalTopSub occ2 _) | occ1 == occ2 = EqBut (unitOccSet p1)
463 eqIfExt n1 n2 = NotEqual
464 \end{code}
465
466
467 \begin{code}
468 ---------------------
469 checkBootDecl :: IfaceDecl      -- The boot decl
470               -> IfaceDecl      -- The real decl
471               -> Bool           -- True <=> compatible
472 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
473   = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
474
475 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
476   = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
477
478 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
479   = ASSERT( ifName d1 == ifName d2 )
480     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
481           eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
482
483 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
484 -- We don't check the recursion flags because the boot-one is
485 -- recursive, to be conservative, but the real one may not be.
486 -- I'm not happy with the way recursive flags are dealt with.
487   = ASSERT( ifName d1    == ifName d2 ) 
488     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
489         eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&& 
490         case ifCons d1 of
491             IfAbstractTyCon -> Equal
492             cons1           -> eq_hsCD env cons1 (ifCons d2)
493
494 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
495   = ASSERT( ifName d1 == ifName d2 )
496     toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env -> 
497           eqListBy (eq_hsFD env)    (ifFDs d1)  (ifFDs d2) &&&
498           case (ifCtxt d1, ifSigs d1) of
499              ([], [])      -> Equal
500              (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2)  &&&
501                               eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
502
503 checkBootDecl _ _ = False       -- default case
504
505 ---------------------
506 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
507 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
508   = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
509
510 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
511   = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
512
513 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
514   = bool (ifName d1    == ifName d2 && 
515           ifRec d1     == ifRec   d2 && 
516           ifVrcs d1    == ifVrcs   d2 && 
517           ifGadtSyntax d1 == ifGadtSyntax   d2 && 
518           ifGeneric d1 == ifGeneric d2) &&&
519     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
520             eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&& 
521             eq_hsCD env (ifCons d1) (ifCons d2) 
522         )
523         -- The type variables of the data type do not scope
524         -- over the constructors (any more), but they do scope
525         -- over the stupid context in the IfaceConDecls
526
527 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
528   = bool (ifName d1 == ifName d2) &&&
529     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
530           eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
531         )
532
533 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
534   = bool (ifName d1 == ifName d2 && 
535           ifRec d1  == ifRec  d2 && 
536           ifVrcs d1 == ifVrcs d2) &&&
537     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
538           eq_ifContext env (ifCtxt d1) (ifCtxt d2)  &&&
539           eqListBy (eq_hsFD env)    (ifFDs d1)  (ifFDs d2) &&&
540           eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
541        )
542
543 eqIfDecl _ _ = NotEqual -- default case
544
545 -- Helper
546 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
547 eqWith = eq_ifTvBndrs emptyEqEnv
548
549 -----------------------
550 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
551 -- All other changes are handled via the version info on the dfun
552
553 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
554          (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
555        = bool (n1==n2 && a1==a2 && o1 == o2) &&&
556          f1 `eqIfExt` f2 &&&
557          eq_ifBndrs emptyEqEnv bs1 bs2 (\env -> 
558          zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
559                 -- zapEq: for the LHSs, ignore the EqBut part
560          eq_ifaceExpr env rhs1 rhs2)
561
562 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2) 
563   = eqListBy (eq_ConDecl env) c1 c2
564
565 eq_hsCD env (IfNewTyCon c1)  (IfNewTyCon c2)  = eq_ConDecl env c1 c2
566 eq_hsCD env IfAbstractTyCon  IfAbstractTyCon  = Equal
567 eq_hsCD env d1               d2               = NotEqual
568
569 eq_ConDecl env c1 c2
570   = bool (ifConOcc c1     == ifConOcc c2 && 
571           ifConInfix c1   == ifConInfix c2 && 
572           ifConStricts c1 == ifConStricts c2 && 
573           ifConFields c1  == ifConFields c2) &&&
574     eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
575     eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
576         eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
577         eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
578
579 eq_hsFD env (ns1,ms1) (ns2,ms2)
580   = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
581
582 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
583   = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
584 \end{code}
585
586
587 \begin{code}
588 -----------------
589 eqIfIdInfo NoInfo        NoInfo        = Equal
590 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
591 eqIfIdInfo i1            i2 = NotEqual
592
593 eq_item (HsInline a1)      (HsInline a2)      = bool (a1 == a2)
594 eq_item (HsArity a1)       (HsArity a2)       = bool (a1 == a2)
595 eq_item (HsStrictness s1)  (HsStrictness s2)  = bool (s1 == s2)
596 eq_item (HsUnfold u1)   (HsUnfold u2)         = eq_ifaceExpr emptyEqEnv u1 u2
597 eq_item HsNoCafRefs        HsNoCafRefs        = Equal
598 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
599 eq_item _ _ = NotEqual
600
601 -----------------
602 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
603 eq_ifaceExpr env (IfaceLcl v1)        (IfaceLcl v2)        = eqIfOcc env v1 v2
604 eq_ifaceExpr env (IfaceExt v1)        (IfaceExt v2)        = eqIfExt v1 v2
605 eq_ifaceExpr env (IfaceLit l1)        (IfaceLit l2)        = bool (l1 == l2)
606 eq_ifaceExpr env (IfaceFCall c1 ty1)  (IfaceFCall c2 ty2)  = bool (c1==c2) &&& eq_ifType env ty1 ty2
607 eq_ifaceExpr env (IfaceType ty1)      (IfaceType ty2)      = eq_ifType env ty1 ty2
608 eq_ifaceExpr env (IfaceTuple n1 as1)  (IfaceTuple n2 as2)  = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
609 eq_ifaceExpr env (IfaceLam b1 body1)  (IfaceLam b2 body2)  = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
610 eq_ifaceExpr env (IfaceApp f1 a1)     (IfaceApp f2 a2)     = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
611 eq_ifaceExpr env (IfaceCast e1 co1)   (IfaceCast e2 co2)   = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
612 eq_ifaceExpr env (IfaceNote n1 r1)    (IfaceNote n2 r2)    = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
613
614 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
615   = eq_ifaceExpr env s1 s2 &&&
616     eq_ifType env ty1 ty2 &&&
617     eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
618   where
619     eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
620         = bool (eq_ifaceConAlt c1 c2) &&& 
621           eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
622
623 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
624   = eq_ifaceExpr env r1 r2 &&& eq_ifIdBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
625
626 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
627   = eq_ifIdBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
628   where
629     (bs1,rs1) = unzip as1
630     (bs2,rs2) = unzip as2
631
632
633 eq_ifaceExpr env _ _ = NotEqual
634
635 -----------------
636 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
637 eq_ifaceConAlt IfaceDefault       IfaceDefault          = True
638 eq_ifaceConAlt (IfaceDataAlt n1)  (IfaceDataAlt n2)     = n1==n2
639 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2)    = c1==c2
640 eq_ifaceConAlt (IfaceLitAlt l1)   (IfaceLitAlt l2)      = l1==l2
641 eq_ifaceConAlt _ _ = False
642
643 -----------------
644 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
645 eq_ifaceNote env (IfaceSCC c1)    (IfaceSCC c2)        = bool (c1==c2)
646 eq_ifaceNote env IfaceInlineMe    IfaceInlineMe        = Equal
647 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
648 eq_ifaceNote env _ _ = NotEqual
649 \end{code}
650
651 \begin{code}
652 ---------------------
653 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
654
655 -------------------
656 eq_ifType env (IfaceTyVar n1)         (IfaceTyVar n2)         = eqIfOcc env n1 n2
657 eq_ifType env (IfaceAppTy s1 t1)      (IfaceAppTy s2 t2)      = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
658 eq_ifType env (IfacePredTy st1)       (IfacePredTy st2)       = eq_ifPredType env st1 st2
659 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
660 eq_ifType env (IfaceForAllTy tv1 t1)  (IfaceForAllTy tv2 t2)  = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
661 eq_ifType env (IfaceFunTy s1 t1)      (IfaceFunTy s2 t2)      = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
662 eq_ifType env _ _ = NotEqual
663
664 -------------------
665 eq_ifTypes env = eqListBy (eq_ifType env)
666
667 -------------------
668 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
669
670 -------------------
671 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&&  eq_ifTypes env tys1 tys2
672 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2)   = bool (n1 == n2) &&& eq_ifType env ty1 ty2
673 eq_ifPredType env _ _ = NotEqual
674
675 -------------------
676 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
677 eqIfTc IfaceIntTc    IfaceIntTc    = Equal
678 eqIfTc IfaceCharTc   IfaceCharTc   = Equal
679 eqIfTc IfaceBoolTc   IfaceBoolTc   = Equal
680 eqIfTc IfaceListTc   IfaceListTc   = Equal
681 eqIfTc IfacePArrTc   IfacePArrTc   = Equal
682 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
683 eqIfTc _ _ = NotEqual
684 \end{code}
685
686 -----------------------------------------------------------
687         Support code for equality checking
688 -----------------------------------------------------------
689
690 \begin{code}
691 ------------------------------------
692 type EqEnv = UniqFM FastString  -- Tracks the mapping from L-variables to R-variables
693
694 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
695 eqIfOcc env n1 n2 = case lookupUFM env n1 of
696                         Just n1 -> bool (n1 == n2)
697                         Nothing -> bool (n1 == n2)
698
699 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
700 extendEqEnv env n1 n2 | n1 == n2  = env
701                       | otherwise = addToUFM env n1 n2
702
703 emptyEqEnv :: EqEnv
704 emptyEqEnv = emptyUFM
705
706 ------------------------------------
707 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
708
709 eq_ifNakedBndr :: ExtEnv FastString
710 eq_ifBndr      :: ExtEnv IfaceBndr
711 eq_ifTvBndr    :: ExtEnv IfaceTvBndr
712 eq_ifIdBndr    :: ExtEnv IfaceIdBndr
713
714 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
715
716 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
717 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
718 eq_ifBndr _ _ _ _ = NotEqual
719
720 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
721 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
722
723 eq_ifBndrs      :: ExtEnv [IfaceBndr]
724 eq_ifIdBndrs    :: ExtEnv [IfaceIdBndr]
725 eq_ifTvBndrs    :: ExtEnv [IfaceTvBndr]
726 eq_ifNakedBndrs :: ExtEnv [FastString]
727 eq_ifBndrs      = eq_bndrs_with eq_ifBndr
728 eq_ifIdBndrs    = eq_bndrs_with eq_ifIdBndr
729 eq_ifTvBndrs    = eq_bndrs_with eq_ifTvBndr
730 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
731
732 eq_bndrs_with eq env []       []       k = k env
733 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
734 eq_bndrs_with eq env _        _        _ = NotEqual
735 \end{code}
736
737 \begin{code}
738 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
739 eqListBy eq []     []     = Equal
740 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
741 eqListBy eq xs     ys     = NotEqual
742
743 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
744 eqMaybeBy eq Nothing Nothing   = Equal
745 eqMaybeBy eq (Just x) (Just y) = eq x y
746 eqMaybeBy eq x        y        = NotEqual
747 \end{code}