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
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
86 -- different flags to the
87 -- current compilation unit
88 ifFamInst :: Maybe -- Just _ <=> instance of fam
89 (IfaceTyCon, -- Family tycon
90 [IfaceType]) -- Instance types
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
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?
110 | IfaceForeign { ifName :: OccName, -- Needs expanding when we move beyond .NET
111 ifExtName :: Maybe FastString }
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
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
125 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
126 visibleIfConDecls IfAbstractTyCon = []
127 visibleIfConDecls IfOpenDataTyCon = []
128 visibleIfConDecls IfOpenNewTyCon = []
129 visibleIfConDecls (IfDataTyCon cs) = cs
130 visibleIfConDecls (IfNewTyCon c) = [c]
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
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
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
170 = NoInfo -- When writing interface file without -O
171 | HasInfo [IfaceInfoItem] -- Has info, and here it is
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.
185 | HsStrictness StrictSig
186 | HsInline Activation
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.
196 --------------------------------
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
209 | IfaceFCall ForeignCall IfaceType
211 data IfaceNote = IfaceSCC CostCentre
213 | IfaceCoreNote String
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
220 data IfaceConAlt = IfaceDefault
221 | IfaceDataAlt OccName
222 | IfaceTupleAlt Boxity
223 | IfaceLitAlt Literal
226 = IfaceNonRec IfaceIdBndr IfaceExpr
227 | IfaceRec [(IfaceIdBndr, IfaceExpr)]
231 %************************************************************************
233 \subsection[HsCore-print]{Printing Core unfoldings}
235 %************************************************************************
237 ----------------------------- Printing IfaceDecl ------------------------------------
240 instance Outputable IfaceDecl where
243 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
244 = sep [ ppr var <+> dcolon <+> ppr ty,
247 pprIfaceDecl (IfaceForeign {ifName = tycon})
248 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
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)
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)
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])
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")
274 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
275 ifFDs = fds, ifATs = ats, ifSigs = sigs,
277 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
278 4 (vcat [pprRec isrec,
282 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
283 pprGen True = ptext SLIT("Generics: yes")
284 pprGen False = ptext SLIT("Generics: no")
286 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
287 pprFamily (Just (fam, tys)) = ptext SLIT("FamilyInstance:") <+>
288 ppr fam <+> hsep (map ppr tys)
290 instance Outputable IfaceClassOp where
291 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
293 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
294 pprIfaceDeclHead context thing tyvars
295 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
296 pprIfaceTvBndrs tyvars]
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))
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 })
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))]
317 main_payload = ppr name <+> dcolon <+>
318 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) (ppr con_tau)
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
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])
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)
344 ppr_mb (Just tc) = ppr tc
348 ----------------------------- Printing IfaceExpr ------------------------------------
351 instance Outputable IfaceExpr where
352 ppr e = pprIfaceExpr noParens e
354 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
355 -- The function adds parens in context that need
356 -- an atomic value (e.g. function args)
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
364 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
365 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
367 pprIfaceExpr add_par e@(IfaceLam _ _)
368 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
369 pprIfaceExpr noParens body])
371 (bndrs,body) = collect [] e
372 collect bs (IfaceLam b e) = collect (b:bs) e
373 collect bs e = (reverse bs, e)
376 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
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 '}'])
383 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
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 '}'])
389 pprIfaceExpr add_par (IfaceCast expr co) = add_par (ptext SLIT("cast") <+> ppr expr <+> ppr co)
391 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
392 = add_par (sep [ptext SLIT("let {"),
393 nest 2 (ppr_bind (b, rhs)),
395 pprIfaceExpr noParens body])
397 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
398 = add_par (sep [ptext SLIT("letrec {"),
399 nest 2 (sep (map ppr_bind pairs)),
401 pprIfaceExpr noParens body])
403 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
405 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
406 arrow <+> pprIfaceExpr noParens rhs]
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)
411 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty,
412 equals <+> pprIfaceExpr noParens rhs]
415 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
416 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
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)
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
432 instance Outputable IfaceIdInfo where
434 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
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
446 %************************************************************************
448 Equality, for interface file version generaion only
450 %************************************************************************
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
457 Of course, equality is also done modulo alpha conversion.
461 = Equal -- Definitely exactly the same
462 | NotEqual -- Definitely different
463 | EqBut OccSet -- The same provided these local things have not changed
465 bool :: Bool -> IfaceEq
467 bool False = NotEqual
469 toBool :: IfaceEq -> Bool
471 toBool (EqBut _) = True
472 toBool NotEqual = False
474 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
475 zapEq (EqBut _) = Equal
478 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
480 NotEqual &&& x = NotEqual
481 EqBut occs &&& Equal = EqBut occs
482 EqBut occs &&& NotEqual = NotEqual
483 EqBut occs1 &&& EqBut occs2 = EqBut (occs1 `unionOccSets` occs2)
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
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)
505 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
506 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
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)
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) &&&
521 IfAbstractTyCon -> Equal
522 cons1 -> eq_hsCD env cons1 (ifCons d2)
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
530 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
531 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
533 checkBootDecl _ _ = False -- default case
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)
540 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
541 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
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)
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
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
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)
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)
578 eqIfDecl _ _ = NotEqual -- default case
581 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
582 eqWith = eq_ifTvBndrs emptyEqEnv
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
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) &&&
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)
597 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
598 = eqListBy (eq_ConDecl env) c1 c2
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
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)))
616 eq_hsFD env (ns1,ms1) (ns2,ms2)
617 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
619 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
620 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
626 eqIfIdInfo NoInfo NoInfo = Equal
627 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
628 eqIfIdInfo i1 i2 = NotEqual
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
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
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)
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)
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)
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)
666 (bs1,rs1) = unzip as1
667 (bs2,rs2) = unzip as2
670 eq_ifaceExpr env _ _ = NotEqual
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
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
689 ---------------------
690 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
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
702 eq_ifTypes env = eqListBy (eq_ifType env)
705 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
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
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
723 -----------------------------------------------------------
724 Support code for equality checking
725 -----------------------------------------------------------
728 ------------------------------------
729 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
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)
736 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
737 extendEqEnv env n1 n2 | n1 == n2 = env
738 | otherwise = addToUFM env n1 n2
741 emptyEqEnv = emptyUFM
743 ------------------------------------
744 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
746 eq_ifNakedBndr :: ExtEnv FastString
747 eq_ifBndr :: ExtEnv IfaceBndr
748 eq_ifTvBndr :: ExtEnv IfaceTvBndr
749 eq_ifIdBndr :: ExtEnv IfaceIdBndr
751 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
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
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)
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
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
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
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