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