2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 %************************************************************************
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
8 %************************************************************************
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well. Currently trying the former... MEGA SIGH.
15 module IfaceType, -- Re-export all this
17 IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..), IfaceConDecls(..),
18 IfaceExpr(..), IfaceAlt, IfaceNote(..),
19 IfaceBinding(..), IfaceConAlt(..), IfaceIdInfo(..),
20 IfaceInfoItem(..), IfaceRule(..), IfaceInst(..),
26 IfaceEq(..), (&&&), bool, eqListBy, eqMaybeBy,
27 eqIfDecl, eqIfInst, eqIfRule, checkBootDecl,
30 pprIfaceExpr, pprIfaceDecl, pprIfaceDeclHead
33 #include "HsVersions.h"
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 )
53 import Maybes ( catMaybes )
54 import Util ( lengthIs )
57 infix 4 `eqIfExt`, `eqIfIdInfo`, `eqIfType`
61 %************************************************************************
63 Data type declarations
65 %************************************************************************
69 = IfaceId { ifName :: OccName,
71 ifIdInfo :: IfaceIdInfo }
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
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
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?
101 | IfaceForeign { ifName :: OccName, -- Needs expanding when we move beyond .NET
102 ifExtName :: Maybe FastString }
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
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
116 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
117 visibleIfConDecls IfAbstractTyCon = []
118 visibleIfConDecls IfOpenDataTyCon = []
119 visibleIfConDecls IfOpenNewTyCon = []
120 visibleIfConDecls (IfDataTyCon cs) = cs
121 visibleIfConDecls (IfNewTyCon c) = [c]
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
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
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
160 = NoInfo -- When writing interface file without -O
161 | HasInfo [IfaceInfoItem] -- Has info, and here it is
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.
175 | HsStrictness StrictSig
176 | HsInline Activation
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.
186 --------------------------------
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
199 | IfaceFCall ForeignCall IfaceType
201 data IfaceNote = IfaceSCC CostCentre
203 | IfaceCoreNote String
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
210 data IfaceConAlt = IfaceDefault
211 | IfaceDataAlt OccName
212 | IfaceTupleAlt Boxity
213 | IfaceLitAlt Literal
216 = IfaceNonRec IfaceIdBndr IfaceExpr
217 | IfaceRec [(IfaceIdBndr, IfaceExpr)]
221 %************************************************************************
223 \subsection[HsCore-print]{Printing Core unfoldings}
225 %************************************************************************
227 ----------------------------- Printing IfaceDecl ------------------------------------
230 instance Outputable IfaceDecl where
233 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
234 = sep [ ppr var <+> dcolon <+> ppr ty,
237 pprIfaceDecl (IfaceForeign {ifName = tycon})
238 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
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)
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)
250 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
251 ifTyVars = tyvars, ifCons = condecls,
253 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
254 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls])
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")
263 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
264 ifFDs = fds, ifATs = ats, ifSigs = sigs,
266 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
267 4 (vcat [pprRec isrec,
271 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
272 pprGen True = ptext SLIT("Generics: yes")
273 pprGen False = ptext SLIT("Generics: no")
275 instance Outputable IfaceClassOp where
276 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
278 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
279 pprIfaceDeclHead context thing tyvars
280 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing), pprIfaceTvBndrs tyvars]
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))
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 })
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))]
301 main_payload = ppr name <+> dcolon <+>
302 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) (ppr con_tau)
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
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])
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)
328 ppr_mb (Just tc) = ppr tc
332 ----------------------------- Printing IfaceExpr ------------------------------------
335 instance Outputable IfaceExpr where
336 ppr e = pprIfaceExpr noParens e
338 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
339 -- The function adds parens in context that need
340 -- an atomic value (e.g. function args)
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
348 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
349 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
351 pprIfaceExpr add_par e@(IfaceLam _ _)
352 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
353 pprIfaceExpr noParens body])
355 (bndrs,body) = collect [] e
356 collect bs (IfaceLam b e) = collect (b:bs) e
357 collect bs e = (reverse bs, e)
360 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
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 '}'])
367 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
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 '}'])
373 pprIfaceExpr add_par (IfaceCast expr co) = add_par (ptext SLIT("cast") <+> ppr expr <+> ppr co)
375 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
376 = add_par (sep [ptext SLIT("let {"),
377 nest 2 (ppr_bind (b, rhs)),
379 pprIfaceExpr noParens body])
381 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
382 = add_par (sep [ptext SLIT("letrec {"),
383 nest 2 (sep (map ppr_bind pairs)),
385 pprIfaceExpr noParens body])
387 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
389 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
390 arrow <+> pprIfaceExpr noParens rhs]
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)
395 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty,
396 equals <+> pprIfaceExpr noParens rhs]
399 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
400 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
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)
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
416 instance Outputable IfaceIdInfo where
418 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
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
430 %************************************************************************
432 Equality, for interface file version generaion only
434 %************************************************************************
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
441 Of course, equality is also done modulo alpha conversion.
445 = Equal -- Definitely exactly the same
446 | NotEqual -- Definitely different
447 | EqBut OccSet -- The same provided these local things have not changed
449 bool :: Bool -> IfaceEq
451 bool False = NotEqual
453 toBool :: IfaceEq -> Bool
455 toBool (EqBut _) = True
456 toBool NotEqual = False
458 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
459 zapEq (EqBut _) = Equal
462 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
464 NotEqual &&& x = NotEqual
465 EqBut occs &&& Equal = EqBut occs
466 EqBut occs &&& NotEqual = NotEqual
467 EqBut occs1 &&& EqBut occs2 = EqBut (occs1 `unionOccSets` occs2)
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
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)
489 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
490 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
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)
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) &&&
505 IfAbstractTyCon -> Equal
506 cons1 -> eq_hsCD env cons1 (ifCons d2)
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
514 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
515 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
517 checkBootDecl _ _ = False -- default case
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)
524 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
525 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
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)
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
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)
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)
556 eqIfDecl _ _ = NotEqual -- default case
559 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
560 eqWith = eq_ifTvBndrs emptyEqEnv
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
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) &&&
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)
575 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
576 = eqListBy (eq_ConDecl env) c1 c2
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
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)))
594 eq_hsFD env (ns1,ms1) (ns2,ms2)
595 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
597 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
598 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
604 eqIfIdInfo NoInfo NoInfo = Equal
605 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
606 eqIfIdInfo i1 i2 = NotEqual
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
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
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)
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)
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)
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)
644 (bs1,rs1) = unzip as1
645 (bs2,rs2) = unzip as2
648 eq_ifaceExpr env _ _ = NotEqual
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
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
667 ---------------------
668 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
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
680 eq_ifTypes env = eqListBy (eq_ifType env)
683 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
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
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
701 -----------------------------------------------------------
702 Support code for equality checking
703 -----------------------------------------------------------
706 ------------------------------------
707 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
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)
714 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
715 extendEqEnv env n1 n2 | n1 == n2 = env
716 | otherwise = addToUFM env n1 n2
719 emptyEqEnv = emptyUFM
721 ------------------------------------
722 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
724 eq_ifNakedBndr :: ExtEnv FastString
725 eq_ifBndr :: ExtEnv IfaceBndr
726 eq_ifTvBndr :: ExtEnv IfaceTvBndr
727 eq_ifIdBndr :: ExtEnv IfaceIdBndr
729 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
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
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)
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
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
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
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