2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
8 module IfaceType, -- Re-export all this
10 IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..), IfaceConDecls(..),
11 IfaceExpr(..), IfaceAlt, IfaceNote(..),
12 IfaceBinding(..), IfaceConAlt(..), IfaceIdInfo(..),
13 IfaceInfoItem(..), IfaceRule(..), IfaceInst(..), IfaceFamInst(..),
16 ifaceDeclSubBndrs, visibleIfConDecls,
19 GenIfaceEq(..), IfaceEq, (&&&), bool, eqListBy, eqMaybeBy,
20 eqIfDecl, eqIfInst, eqIfFamInst, eqIfRule, checkBootDecl,
23 pprIfaceExpr, pprIfaceDeclHead
26 #include "HsVersions.h"
49 infix 4 `eqIfExt`, `eqIfIdInfo`, `eqIfType`
53 %************************************************************************
55 Data type declarations
57 %************************************************************************
61 = IfaceId { ifName :: OccName,
63 ifIdInfo :: IfaceIdInfo }
65 | IfaceData { ifName :: OccName, -- Type constructor
66 ifTyVars :: [IfaceTvBndr], -- Type variables
67 ifCtxt :: IfaceContext, -- The "stupid theta"
68 ifCons :: IfaceConDecls, -- Includes new/data info
69 ifRec :: RecFlag, -- Recursive or not?
70 ifGadtSyntax :: Bool, -- True <=> declared using
72 ifGeneric :: Bool, -- True <=> generic converter
73 -- functions available
74 -- We need this for imported
75 -- data decls, since the
76 -- imported modules may have
78 -- different flags to the
79 -- current compilation unit
80 ifFamInst :: Maybe (IfaceTyCon, [IfaceType])
81 -- Just <=> instance of family
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
103 ifExtName :: Maybe FastString }
105 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
106 -- Nothing => no default method
107 -- Just False => ordinary polymorphic default method
108 -- Just True => generic default method
111 = IfAbstractTyCon -- No info
112 | IfOpenDataTyCon -- Open data family
113 | IfOpenNewTyCon -- Open newtype family
114 | IfDataTyCon [IfaceConDecl] -- data type decls
115 | IfNewTyCon IfaceConDecl -- newtype decls
117 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
118 visibleIfConDecls IfAbstractTyCon = []
119 visibleIfConDecls IfOpenDataTyCon = []
120 visibleIfConDecls IfOpenNewTyCon = []
121 visibleIfConDecls (IfDataTyCon cs) = cs
122 visibleIfConDecls (IfNewTyCon c) = [c]
126 ifConOcc :: OccName, -- Constructor name
127 ifConInfix :: Bool, -- True <=> declared infix
128 ifConUnivTvs :: [IfaceTvBndr], -- Universal tyvars
129 ifConExTvs :: [IfaceTvBndr], -- Existential tyvars
130 ifConEqSpec :: [(OccName,IfaceType)], -- Equality contraints
131 ifConCtxt :: IfaceContext, -- Non-stupid context
132 ifConArgTys :: [IfaceType], -- Arg types
133 ifConFields :: [OccName], -- ...ditto... (field labels)
134 ifConStricts :: [StrictnessMark]} -- Empty (meaning all lazy),
135 -- or 1-1 corresp with arg tys
138 = IfaceInst { ifInstCls :: Name, -- See comments with
139 ifInstTys :: [Maybe IfaceTyCon], -- the defn of Instance
140 ifDFun :: Name, -- The dfun
141 ifOFlag :: OverlapFlag, -- Overlap flag
142 ifInstOrph :: Maybe OccName } -- See Note [Orphans]
143 -- There's always a separate IfaceDecl for the DFun, which gives
144 -- its IdInfo with its full type and version number.
145 -- The instance declarations taken together have a version number,
146 -- and we don't want that to wobble gratuitously
147 -- If this instance decl is *used*, we'll record a usage on the dfun;
148 -- and if the head does not change it won't be used if it wasn't before
151 = IfaceFamInst { ifFamInstFam :: Name -- Family tycon
152 , ifFamInstTys :: [Maybe IfaceTyCon] -- Rough match types
153 , ifFamInstTyCon :: IfaceTyCon -- Instance decl
158 ifRuleName :: RuleName,
159 ifActivation :: Activation,
160 ifRuleBndrs :: [IfaceBndr], -- Tyvars and term vars
161 ifRuleHead :: Name, -- Head of lhs
162 ifRuleArgs :: [IfaceExpr], -- Args of LHS
163 ifRuleRhs :: IfaceExpr,
164 ifRuleOrph :: Maybe OccName -- Just like IfaceInst
168 = NoInfo -- When writing interface file without -O
169 | HasInfo [IfaceInfoItem] -- Has info, and here it is
171 -- Here's a tricky case:
172 -- * Compile with -O module A, and B which imports A.f
173 -- * Change function f in A, and recompile without -O
174 -- * When we read in old A.hi we read in its IdInfo (as a thunk)
175 -- (In earlier GHCs we used to drop IdInfo immediately on reading,
176 -- but we do not do that now. Instead it's discarded when the
177 -- ModIface is read into the various decl pools.)
178 -- * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
179 -- and so gives a new version.
183 | HsStrictness StrictSig
184 | HsInline Activation
187 | HsWorker Name Arity -- Worker, if any see IdInfo.WorkerInfo
188 -- for why we want arity here.
189 -- NB: we need IfaceExtName (not just OccName) because the worker
190 -- can simplify to a function in another module.
191 -- NB: Specialisations and rules come in separately and are
192 -- only later attached to the Id. Partial reason: some are orphans.
194 --------------------------------
196 = IfaceLcl FastString
198 | IfaceType IfaceType
199 | IfaceTuple Boxity [IfaceExpr] -- Saturated; type arguments omitted
200 | IfaceLam IfaceBndr IfaceExpr
201 | IfaceApp IfaceExpr IfaceExpr
202 | IfaceCase IfaceExpr FastString IfaceType [IfaceAlt]
203 | IfaceLet IfaceBinding IfaceExpr
204 | IfaceNote IfaceNote IfaceExpr
205 | IfaceCast IfaceExpr IfaceCoercion
207 | IfaceFCall ForeignCall IfaceType
209 data IfaceNote = IfaceSCC CostCentre
211 | IfaceCoreNote String
213 type IfaceAlt = (IfaceConAlt, [FastString], IfaceExpr)
214 -- Note: FastString, not IfaceBndr (and same with the case binder)
215 -- We reconstruct the kind/type of the thing from the context
216 -- thus saving bulk in interface files
218 data IfaceConAlt = IfaceDefault
220 | IfaceTupleAlt Boxity
221 | IfaceLitAlt Literal
224 = IfaceNonRec IfaceIdBndr IfaceExpr
225 | IfaceRec [(IfaceIdBndr, IfaceExpr)]
228 Note [Orphans]: the ifInstOrph and ifRuleOrph fields
229 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230 If a module contains any "orphans", then its interface file is read
231 regardless, so that its instances are not missed.
233 Roughly speaking, an instance is an orphan if its head (after the =>)
234 mentions nothing defined in this module. Functional dependencies
235 complicate the situation though. Consider
237 module M where { class C a b | a -> b }
239 and suppose we are compiling module X:
244 instance C Int T where ...
246 This instance is an orphan, because when compiling a third module Y we
247 might get a constraint (C Int v), and we'd want to improve v to T. So
248 we must make sure X's instances are loaded, even if we do not directly
251 More precisely, an instance is an orphan iff
253 If there are no fundeps, then at least of the names in
254 the instance head is locally defined.
256 If there are fundeps, then for every fundep, at least one of the
257 names free in a *non-determined* part of the instance head is
258 defined in this module.
260 (Note that these conditions hold trivially if the class is locally
263 Note [Versioning of instances]
264 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265 Now consider versioning. If we *use* an instance decl in one compilation,
266 we'll depend on the dfun id for that instance, so we'll recompile if it changes.
267 But suppose we *don't* (currently) use an instance! We must recompile if
268 the instance is changed in such a way that it becomes important. (This would
269 only matter with overlapping instances, else the importing module wouldn't have
270 compiled before and the recompilation check is irrelevant.)
272 The is_orph field is set to (Just n) if the instance is not an orphan.
273 The 'n' is *any* of the locally-defined names mentioned anywhere in the
274 instance head. This name is used for versioning; the instance decl is
275 considered part of the defn of this 'n'.
277 I'm worried about whether this works right if we pick a name from
278 a functionally-dependent part of the instance decl. E.g.
280 module M where { class C a b | a -> b }
282 and suppose we are compiling module X:
288 instance C S T where ...
290 If we base the instance verion on T, I'm worried that changing S to S'
291 would change T's version, but not S or S'. But an importing module might
292 not depend on T, and so might not be recompiled even though the new instance
293 (C S' T) might be relevant. I have not been able to make a concrete example,
294 and it seems deeply obscure, so I'm going to leave it for now.
297 Note [Versioning of rules]
298 ~~~~~~~~~~~~~~~~~~~~~~~~~~
299 A rule that is not an orphan has an ifRuleOrph field of (Just n), where
300 n appears on the LHS of the rule; any change in the rule changes the version of n.
304 -- -----------------------------------------------------------------------------
307 ifaceDeclSubBndrs :: IfaceDecl -> [OccName]
308 -- *Excludes* the 'main' name, but *includes* the implicitly-bound names
309 -- Deeply revolting, because it has to predict what gets bound,
310 -- especially the question of whether there's a wrapper for a datacon
312 ifaceDeclSubBndrs (IfaceClass {ifCtxt = sc_ctxt, ifName = cls_occ,
313 ifSigs = sigs, ifATs = ats })
315 [tc_occ, dc_occ, dcww_occ] ++
316 [op | IfaceClassOp op _ _ <- sigs] ++
317 [ifName at | at <- ats ] ++
318 [mkSuperDictSelOcc n cls_occ | n <- [1..n_ctxt]]
320 n_ctxt = length sc_ctxt
322 tc_occ = mkClassTyConOcc cls_occ
323 dc_occ = mkClassDataConOcc cls_occ
324 co_occs | is_newtype = [mkNewTyCoOcc tc_occ]
326 dcww_occ -- | is_newtype = mkDataConWrapperOcc dc_occ -- Newtypes have wrapper but no worker
327 | otherwise = mkDataConWorkerOcc dc_occ -- Otherwise worker but no wrapper
328 is_newtype = n_sigs + n_ctxt == 1 -- Sigh
330 ifaceDeclSubBndrs IfaceData {ifCons = IfAbstractTyCon}
333 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
334 ifCons = IfNewTyCon (
335 IfCon { ifConOcc = con_occ,
338 ifFamInst = famInst})
339 = fields ++ [con_occ, mkDataConWorkerOcc con_occ, mkNewTyCoOcc tc_occ]
340 ++ famInstCo famInst tc_occ
342 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
343 ifCons = IfDataTyCon cons,
344 ifFamInst = famInst})
345 = nub (concatMap ifConFields cons) -- Eliminate duplicate fields
346 ++ concatMap dc_occs cons
347 ++ famInstCo famInst tc_occ
350 | has_wrapper = [con_occ, work_occ, wrap_occ]
351 | otherwise = [con_occ, work_occ]
353 con_occ = ifConOcc con_decl
354 strs = ifConStricts con_decl
355 wrap_occ = mkDataConWrapperOcc con_occ
356 work_occ = mkDataConWorkerOcc con_occ
357 has_wrapper = any isMarkedStrict strs -- See MkId.mkDataConIds (sigh)
358 || not (null . ifConEqSpec $ con_decl)
360 -- ToDo: may miss strictness in existential dicts
362 ifaceDeclSubBndrs _other = []
364 -- coercion for data/newtype family instances
365 famInstCo Nothing baseOcc = []
366 famInstCo (Just _) baseOcc = [mkInstTyCoOcc baseOcc]
368 ----------------------------- Printing IfaceDecl ------------------------------
370 instance Outputable IfaceDecl where
373 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
374 = sep [ ppr var <+> dcolon <+> ppr ty,
377 pprIfaceDecl (IfaceForeign {ifName = tycon})
378 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
380 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
381 ifOpenSyn = False, ifSynRhs = mono_ty})
382 = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
383 4 (equals <+> ppr mono_ty)
385 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
386 ifOpenSyn = True, ifSynRhs = mono_ty})
387 = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
388 4 (dcolon <+> ppr mono_ty)
390 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
391 ifTyVars = tyvars, ifCons = condecls,
392 ifRec = isrec, ifFamInst = mbFamInst})
393 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
394 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
395 pprFamily mbFamInst])
397 pp_nd = case condecls of
398 IfAbstractTyCon -> ptext SLIT("data")
399 IfOpenDataTyCon -> ptext SLIT("data family")
400 IfDataTyCon _ -> ptext SLIT("data")
401 IfNewTyCon _ -> ptext SLIT("newtype")
402 IfOpenNewTyCon -> ptext SLIT("newtype family")
404 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
405 ifFDs = fds, ifATs = ats, ifSigs = sigs,
407 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
408 4 (vcat [pprRec isrec,
412 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
413 pprGen True = ptext SLIT("Generics: yes")
414 pprGen False = ptext SLIT("Generics: no")
416 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
417 pprFamily (Just famInst) = ptext SLIT("FamilyInstance:") <+> ppr famInst
419 instance Outputable IfaceClassOp where
420 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
422 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
423 pprIfaceDeclHead context thing tyvars
424 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
425 pprIfaceTvBndrs tyvars]
427 pp_condecls tc IfAbstractTyCon = ptext SLIT("{- abstract -}")
428 pp_condecls tc IfOpenNewTyCon = empty
429 pp_condecls tc (IfNewTyCon c) = equals <+> pprIfaceConDecl tc c
430 pp_condecls tc IfOpenDataTyCon = empty
431 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
432 (map (pprIfaceConDecl tc) cs))
435 (IfCon { ifConOcc = name, ifConInfix = is_infix,
436 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
437 ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys,
438 ifConStricts = strs, ifConFields = fields })
440 if is_infix then ptext SLIT("Infix") else empty,
441 if null strs then empty
442 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
443 if null fields then empty
444 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
446 main_payload = ppr name <+> dcolon <+>
447 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) (ppr con_tau)
449 eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty)
450 | (tv,ty) <- eq_spec]
451 con_tau = foldr1 IfaceFunTy (arg_tys ++ [tc_app])
452 tc_app = IfaceTyConApp (IfaceTc tc_name)
453 [IfaceTyVar tv | (tv,_) <- univ_tvs]
454 tc_name = mkInternalName (mkBuiltinUnique 1) tc noSrcLoc
455 -- Really Gruesome, but just for debug print
457 instance Outputable IfaceRule where
458 ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
459 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
460 = sep [hsep [doubleQuotes (ftext name), ppr act,
461 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
462 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
463 ptext SLIT("=") <+> ppr rhs])
466 instance Outputable IfaceInst where
467 ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag,
468 ifInstCls = cls, ifInstTys = mb_tcs})
469 = hang (ptext SLIT("instance") <+> ppr flag
470 <+> ppr cls <+> brackets (pprWithCommas ppr_rough mb_tcs))
471 2 (equals <+> ppr dfun_id)
473 instance Outputable IfaceFamInst where
474 ppr (IfaceFamInst {ifFamInstFam = fam, ifFamInstTys = mb_tcs,
475 ifFamInstTyCon = tycon_id})
476 = hang (ptext SLIT("family instance") <+>
477 ppr fam <+> brackets (pprWithCommas ppr_rough mb_tcs))
478 2 (equals <+> ppr tycon_id)
480 ppr_rough :: Maybe IfaceTyCon -> SDoc
481 ppr_rough Nothing = dot
482 ppr_rough (Just tc) = ppr tc
486 ----------------------------- Printing IfaceExpr ------------------------------------
489 instance Outputable IfaceExpr where
490 ppr e = pprIfaceExpr noParens e
492 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
493 -- The function adds parens in context that need
494 -- an atomic value (e.g. function args)
496 pprIfaceExpr add_par (IfaceLcl v) = ppr v
497 pprIfaceExpr add_par (IfaceExt v) = ppr v
498 pprIfaceExpr add_par (IfaceLit l) = ppr l
499 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
500 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
502 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
503 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
505 pprIfaceExpr add_par e@(IfaceLam _ _)
506 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
507 pprIfaceExpr noParens body])
509 (bndrs,body) = collect [] e
510 collect bs (IfaceLam b e) = collect (b:bs) e
511 collect bs e = (reverse bs, e)
513 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
514 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
515 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
516 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
517 pprIfaceExpr noParens rhs <+> char '}'])
519 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
520 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
521 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
522 <+> ppr bndr <+> char '{',
523 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
525 pprIfaceExpr add_par (IfaceCast expr co)
526 = sep [pprIfaceExpr parens expr,
527 nest 2 (ptext SLIT("`cast`")),
528 pprParendIfaceType co]
530 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
531 = add_par (sep [ptext SLIT("let {"),
532 nest 2 (ppr_bind (b, rhs)),
534 pprIfaceExpr noParens body])
536 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
537 = add_par (sep [ptext SLIT("letrec {"),
538 nest 2 (sep (map ppr_bind pairs)),
540 pprIfaceExpr noParens body])
542 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
544 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
545 arrow <+> pprIfaceExpr noParens rhs]
547 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
548 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
550 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty,
551 equals <+> pprIfaceExpr noParens rhs]
554 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
555 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
558 instance Outputable IfaceNote where
559 ppr (IfaceSCC cc) = pprCostCentreCore cc
560 ppr IfaceInlineMe = ptext SLIT("__inline_me")
561 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
564 instance Outputable IfaceConAlt where
565 ppr IfaceDefault = text "DEFAULT"
566 ppr (IfaceLitAlt l) = ppr l
567 ppr (IfaceDataAlt d) = ppr d
568 ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt"
569 -- IfaceTupleAlt is handled by the case-alternative printer
572 instance Outputable IfaceIdInfo where
574 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
576 ppr_hs_info (HsUnfold unf) = ptext SLIT("Unfolding:") <+>
577 parens (pprIfaceExpr noParens unf)
578 ppr_hs_info (HsInline act) = ptext SLIT("Inline:") <+> ppr act
579 ppr_hs_info (HsArity arity) = ptext SLIT("Arity:") <+> int arity
580 ppr_hs_info (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
581 ppr_hs_info HsNoCafRefs = ptext SLIT("HasNoCafRefs")
582 ppr_hs_info (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
586 %************************************************************************
588 Equality, for interface file version generaion only
590 %************************************************************************
592 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new
593 constructor is EqBut, which gives the set of things whose version must
594 be equal for the whole thing to be equal. So the key function is
595 eqIfExt, which compares Names.
597 Of course, equality is also done modulo alpha conversion.
601 = Equal -- Definitely exactly the same
602 | NotEqual -- Definitely different
603 | EqBut a -- The same provided these Names have not changed
605 type IfaceEq = GenIfaceEq NameSet
607 instance Outputable IfaceEq where
608 ppr Equal = ptext SLIT("Equal")
609 ppr NotEqual = ptext SLIT("NotEqual")
610 ppr (EqBut occset) = ptext SLIT("EqBut") <+> ppr (nameSetToList occset)
612 bool :: Bool -> IfaceEq
614 bool False = NotEqual
616 toBool :: IfaceEq -> Bool
618 toBool (EqBut _) = True
619 toBool NotEqual = False
621 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
622 zapEq (EqBut _) = Equal
625 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
627 NotEqual &&& x = NotEqual
628 EqBut nms &&& Equal = EqBut nms
629 EqBut nms &&& NotEqual = NotEqual
630 EqBut nms1 &&& EqBut nms2 = EqBut (nms1 `unionNameSets` nms2)
632 -- This function is the core of the EqBut stuff
633 -- ASSUMPTION: The left-hand argument is the NEW CODE, and hence
634 -- any Names in the left-hand arg have the correct parent in them.
635 eqIfExt :: Name -> Name -> IfaceEq
637 | name1 == name2 = EqBut (unitNameSet name1)
638 | otherwise = NotEqual
640 ---------------------
641 checkBootDecl :: IfaceDecl -- The boot decl
642 -> IfaceDecl -- The real decl
643 -> Bool -- True <=> compatible
644 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
645 = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
647 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
648 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
650 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
651 = ASSERT( ifName d1 == ifName d2 )
652 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
653 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
655 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
656 -- We don't check the recursion flags because the boot-one is
657 -- recursive, to be conservative, but the real one may not be.
658 -- I'm not happy with the way recursive flags are dealt with.
659 = ASSERT( ifName d1 == ifName d2 )
660 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
661 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
663 IfAbstractTyCon -> Equal
664 cons1 -> eq_hsCD env cons1 (ifCons d2)
666 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
667 = ASSERT( ifName d1 == ifName d2 )
668 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
669 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
670 case (ifCtxt d1, ifSigs d1) of
672 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
673 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
675 checkBootDecl _ _ = False -- default case
677 ---------------------
678 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
679 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
680 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
682 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
683 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
685 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
686 = bool (ifName d1 == ifName d2 &&
687 ifRec d1 == ifRec d2 &&
688 ifGadtSyntax d1 == ifGadtSyntax d2 &&
689 ifGeneric d1 == ifGeneric d2) &&&
690 ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
691 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
692 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
693 eq_hsCD env (ifCons d1) (ifCons d2)
695 -- The type variables of the data type do not scope
696 -- over the constructors (any more), but they do scope
697 -- over the stupid context in the IfaceConDecls
699 Nothing `eqIfTc_fam` Nothing = Equal
700 (Just (fam1, tys1)) `eqIfTc_fam` (Just (fam2, tys2)) =
701 fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2
702 _ `eqIfTc_fam` _ = NotEqual
704 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
705 = bool (ifName d1 == ifName d2) &&&
706 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
707 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
710 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
711 = bool (ifName d1 == ifName d2 &&
712 ifRec d1 == ifRec d2) &&&
713 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
714 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
715 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
716 eqListBy eqIfDecl (ifATs d1) (ifATs d2) &&&
717 eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
720 eqIfDecl _ _ = NotEqual -- default case
723 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
724 eqWith = eq_ifTvBndrs emptyEqEnv
726 -----------------------
727 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
728 -- All other changes are handled via the version info on the dfun
730 eqIfFamInst d1 d2 = bool (ifFamInstTyCon d1 == ifFamInstTyCon d2)
731 -- All other changes are handled via the version info on the tycon
733 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
734 (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
735 = bool (n1==n2 && a1==a2 && o1 == o2) &&&
737 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
738 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
739 -- zapEq: for the LHSs, ignore the EqBut part
740 eq_ifaceExpr env rhs1 rhs2)
742 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
743 = eqListBy (eq_ConDecl env) c1 c2
745 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
746 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
747 eq_hsCD env IfOpenDataTyCon IfOpenDataTyCon = Equal
748 eq_hsCD env IfOpenNewTyCon IfOpenNewTyCon = Equal
749 eq_hsCD env d1 d2 = NotEqual
752 = bool (ifConOcc c1 == ifConOcc c2 &&
753 ifConInfix c1 == ifConInfix c2 &&
754 ifConStricts c1 == ifConStricts c2 &&
755 ifConFields c1 == ifConFields c2) &&&
756 eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
757 eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
758 eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
759 eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
761 eq_hsFD env (ns1,ms1) (ns2,ms2)
762 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
764 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
765 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
771 eqIfIdInfo NoInfo NoInfo = Equal
772 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
773 eqIfIdInfo i1 i2 = NotEqual
775 eq_item (HsInline a1) (HsInline a2) = bool (a1 == a2)
776 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
777 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
778 eq_item (HsUnfold u1) (HsUnfold u2) = eq_ifaceExpr emptyEqEnv u1 u2
779 eq_item HsNoCafRefs HsNoCafRefs = Equal
780 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
781 eq_item _ _ = NotEqual
784 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
785 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
786 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
787 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
788 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
789 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
790 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
791 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
792 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
793 eq_ifaceExpr env (IfaceCast e1 co1) (IfaceCast e2 co2) = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
794 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
796 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
797 = eq_ifaceExpr env s1 s2 &&&
798 eq_ifType env ty1 ty2 &&&
799 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
801 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
802 = bool (eq_ifaceConAlt c1 c2) &&&
803 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
805 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
806 = eq_ifaceExpr env r1 r2 &&& eq_ifIdBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
808 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
809 = eq_ifIdBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
811 (bs1,rs1) = unzip as1
812 (bs2,rs2) = unzip as2
815 eq_ifaceExpr env _ _ = NotEqual
818 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
819 eq_ifaceConAlt IfaceDefault IfaceDefault = True
820 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
821 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
822 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
823 eq_ifaceConAlt _ _ = False
826 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
827 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
828 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
829 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
830 eq_ifaceNote env _ _ = NotEqual
834 ---------------------
835 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
838 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
839 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
840 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
841 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
842 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
843 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
844 eq_ifType env _ _ = NotEqual
847 eq_ifTypes env = eqListBy (eq_ifType env)
850 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
853 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
854 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
855 eq_ifPredType env _ _ = NotEqual
858 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
859 eqIfTc IfaceIntTc IfaceIntTc = Equal
860 eqIfTc IfaceCharTc IfaceCharTc = Equal
861 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
862 eqIfTc IfaceListTc IfaceListTc = Equal
863 eqIfTc IfacePArrTc IfacePArrTc = Equal
864 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
865 eqIfTc IfaceLiftedTypeKindTc IfaceLiftedTypeKindTc = Equal
866 eqIfTc IfaceOpenTypeKindTc IfaceOpenTypeKindTc = Equal
867 eqIfTc IfaceUnliftedTypeKindTc IfaceUnliftedTypeKindTc = Equal
868 eqIfTc IfaceUbxTupleKindTc IfaceUbxTupleKindTc = Equal
869 eqIfTc IfaceArgTypeKindTc IfaceArgTypeKindTc = Equal
870 eqIfTc _ _ = NotEqual
873 -----------------------------------------------------------
874 Support code for equality checking
875 -----------------------------------------------------------
878 ------------------------------------
879 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
881 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
882 eqIfOcc env n1 n2 = case lookupUFM env n1 of
883 Just n1 -> bool (n1 == n2)
884 Nothing -> bool (n1 == n2)
886 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
887 extendEqEnv env n1 n2 | n1 == n2 = env
888 | otherwise = addToUFM env n1 n2
891 emptyEqEnv = emptyUFM
893 ------------------------------------
894 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
896 eq_ifNakedBndr :: ExtEnv FastString
897 eq_ifBndr :: ExtEnv IfaceBndr
898 eq_ifTvBndr :: ExtEnv IfaceTvBndr
899 eq_ifIdBndr :: ExtEnv IfaceIdBndr
901 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
903 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
904 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
905 eq_ifBndr _ _ _ _ = NotEqual
907 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
908 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
910 eq_ifBndrs :: ExtEnv [IfaceBndr]
911 eq_ifIdBndrs :: ExtEnv [IfaceIdBndr]
912 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
913 eq_ifNakedBndrs :: ExtEnv [FastString]
914 eq_ifBndrs = eq_bndrs_with eq_ifBndr
915 eq_ifIdBndrs = eq_bndrs_with eq_ifIdBndr
916 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
917 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
919 eq_bndrs_with eq env [] [] k = k env
920 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
921 eq_bndrs_with eq env _ _ _ = NotEqual
925 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
926 eqListBy eq [] [] = Equal
927 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
928 eqListBy eq xs ys = NotEqual
930 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
931 eqMaybeBy eq Nothing Nothing = Equal
932 eqMaybeBy eq (Just x) (Just y) = eq x y
933 eqMaybeBy eq x y = NotEqual