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