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"
47 infix 4 `eqIfExt`, `eqIfIdInfo`, `eqIfType`
51 %************************************************************************
53 Data type declarations
55 %************************************************************************
59 = IfaceId { ifName :: OccName,
61 ifIdInfo :: IfaceIdInfo }
63 | IfaceData { ifName :: OccName, -- Type constructor
64 ifTyVars :: [IfaceTvBndr], -- Type variables
65 ifCtxt :: IfaceContext, -- The "stupid theta"
66 ifCons :: IfaceConDecls, -- Includes new/data info
67 ifRec :: RecFlag, -- Recursive or not?
68 ifGadtSyntax :: Bool, -- True <=> declared using
70 ifGeneric :: Bool, -- True <=> generic converter
71 -- functions available
72 -- We need this for imported
73 -- data decls, since the
74 -- imported modules may have
76 -- different flags to the
77 -- current compilation unit
78 ifFamInst :: Maybe (IfaceTyCon, [IfaceType])
79 -- Just <=> instance of family
82 | IfaceSyn { ifName :: OccName, -- Type constructor
83 ifTyVars :: [IfaceTvBndr], -- Type variables
84 ifOpenSyn :: Bool, -- Is an open family?
85 ifSynRhs :: IfaceType -- Type for an ordinary
86 -- synonym and kind for an
90 | IfaceClass { ifCtxt :: IfaceContext, -- Context...
91 ifName :: OccName, -- Name of the class
92 ifTyVars :: [IfaceTvBndr], -- Type variables
93 ifFDs :: [FunDep FastString], -- Functional dependencies
94 ifATs :: [IfaceDecl], -- Associated type families
95 ifSigs :: [IfaceClassOp], -- Method signatures
96 ifRec :: RecFlag -- Is newtype/datatype associated with the class recursive?
99 | IfaceForeign { ifName :: OccName, -- Needs expanding when we move
101 ifExtName :: Maybe FastString }
103 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
104 -- Nothing => no default method
105 -- Just False => ordinary polymorphic default method
106 -- Just True => generic default method
109 = IfAbstractTyCon -- No info
110 | IfOpenDataTyCon -- Open data family
111 | IfOpenNewTyCon -- Open newtype family
112 | IfDataTyCon [IfaceConDecl] -- data type decls
113 | IfNewTyCon IfaceConDecl -- newtype decls
115 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
116 visibleIfConDecls IfAbstractTyCon = []
117 visibleIfConDecls IfOpenDataTyCon = []
118 visibleIfConDecls IfOpenNewTyCon = []
119 visibleIfConDecls (IfDataTyCon cs) = cs
120 visibleIfConDecls (IfNewTyCon c) = [c]
124 ifConOcc :: OccName, -- Constructor name
125 ifConInfix :: Bool, -- True <=> declared infix
126 ifConUnivTvs :: [IfaceTvBndr], -- Universal tyvars
127 ifConExTvs :: [IfaceTvBndr], -- Existential tyvars
128 ifConEqSpec :: [(OccName,IfaceType)], -- Equality contraints
129 ifConCtxt :: IfaceContext, -- Non-stupid context
130 ifConArgTys :: [IfaceType], -- Arg types
131 ifConFields :: [OccName], -- ...ditto... (field labels)
132 ifConStricts :: [StrictnessMark]} -- Empty (meaning all lazy),
133 -- or 1-1 corresp with arg tys
136 = IfaceInst { ifInstCls :: Name, -- See comments with
137 ifInstTys :: [Maybe IfaceTyCon], -- the defn of Instance
138 ifDFun :: Name, -- The dfun
139 ifOFlag :: OverlapFlag, -- Overlap flag
140 ifInstOrph :: Maybe OccName } -- See Note [Orphans]
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
149 = IfaceFamInst { ifFamInstFam :: Name -- Family tycon
150 , ifFamInstTys :: [Maybe IfaceTyCon] -- Rough match types
151 , ifFamInstTyCon :: IfaceTyCon -- Instance decl
156 ifRuleName :: RuleName,
157 ifActivation :: Activation,
158 ifRuleBndrs :: [IfaceBndr], -- Tyvars and term vars
159 ifRuleHead :: Name, -- Head of lhs
160 ifRuleArgs :: [IfaceExpr], -- Args of LHS
161 ifRuleRhs :: IfaceExpr,
162 ifRuleOrph :: Maybe OccName -- Just like IfaceInst
166 = NoInfo -- When writing interface file without -O
167 | HasInfo [IfaceInfoItem] -- Has info, and here it is
169 -- Here's a tricky case:
170 -- * Compile with -O module A, and B which imports A.f
171 -- * Change function f in A, and recompile without -O
172 -- * When we read in old A.hi we read in its IdInfo (as a thunk)
173 -- (In earlier GHCs we used to drop IdInfo immediately on reading,
174 -- but we do not do that now. Instead it's discarded when the
175 -- ModIface is read into the various decl pools.)
176 -- * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
177 -- and so gives a new version.
181 | HsStrictness StrictSig
182 | HsInline Activation
185 | HsWorker Name Arity -- Worker, if any see IdInfo.WorkerInfo
186 -- for why we want arity here.
187 -- NB: we need IfaceExtName (not just OccName) because the worker
188 -- can simplify to a function in another module.
189 -- NB: Specialisations and rules come in separately and are
190 -- only later attached to the Id. Partial reason: some are orphans.
192 --------------------------------
194 = IfaceLcl FastString
196 | IfaceType IfaceType
197 | IfaceTuple Boxity [IfaceExpr] -- Saturated; type arguments omitted
198 | IfaceLam IfaceBndr IfaceExpr
199 | IfaceApp IfaceExpr IfaceExpr
200 | IfaceCase IfaceExpr FastString IfaceType [IfaceAlt]
201 | IfaceLet IfaceBinding IfaceExpr
202 | IfaceNote IfaceNote IfaceExpr
203 | IfaceCast IfaceExpr IfaceCoercion
205 | IfaceFCall ForeignCall IfaceType
207 data IfaceNote = IfaceSCC CostCentre
209 | IfaceCoreNote String
211 type IfaceAlt = (IfaceConAlt, [FastString], IfaceExpr)
212 -- Note: FastString, not IfaceBndr (and same with the case binder)
213 -- We reconstruct the kind/type of the thing from the context
214 -- thus saving bulk in interface files
216 data IfaceConAlt = IfaceDefault
218 | IfaceTupleAlt Boxity
219 | IfaceLitAlt Literal
222 = IfaceNonRec IfaceIdBndr IfaceExpr
223 | IfaceRec [(IfaceIdBndr, IfaceExpr)]
226 Note [Orphans]: the ifInstOrph and ifRuleOrph fields
227 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228 If a module contains any "orphans", then its interface file is read
229 regardless, so that its instances are not missed.
231 Roughly speaking, an instance is an orphan if its head (after the =>)
232 mentions nothing defined in this module. Functional dependencies
233 complicate the situation though. Consider
235 module M where { class C a b | a -> b }
237 and suppose we are compiling module X:
242 instance C Int T where ...
244 This instance is an orphan, because when compiling a third module Y we
245 might get a constraint (C Int v), and we'd want to improve v to T. So
246 we must make sure X's instances are loaded, even if we do not directly
249 More precisely, an instance is an orphan iff
251 If there are no fundeps, then at least of the names in
252 the instance head is locally defined.
254 If there are fundeps, then for every fundep, at least one of the
255 names free in a *non-determined* part of the instance head is
256 defined in this module.
258 (Note that these conditions hold trivially if the class is locally
261 Note [Versioning of instances]
262 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263 Now consider versioning. If we *use* an instance decl in one compilation,
264 we'll depend on the dfun id for that instance, so we'll recompile if it changes.
265 But suppose we *don't* (currently) use an instance! We must recompile if
266 the instance is changed in such a way that it becomes important. (This would
267 only matter with overlapping instances, else the importing module wouldn't have
268 compiled before and the recompilation check is irrelevant.)
270 The is_orph field is set to (Just n) if the instance is not an orphan.
271 The 'n' is *any* of the locally-defined names mentioned anywhere in the
272 instance head. This name is used for versioning; the instance decl is
273 considered part of the defn of this 'n'.
275 I'm worried about whether this works right if we pick a name from
276 a functionally-dependent part of the instance decl. E.g.
278 module M where { class C a b | a -> b }
280 and suppose we are compiling module X:
286 instance C S T where ...
288 If we base the instance verion on T, I'm worried that changing S to S'
289 would change T's version, but not S or S'. But an importing module might
290 not depend on T, and so might not be recompiled even though the new instance
291 (C S' T) might be relevant. I have not been able to make a concrete example,
292 and it seems deeply obscure, so I'm going to leave it for now.
295 Note [Versioning of rules]
296 ~~~~~~~~~~~~~~~~~~~~~~~~~~
297 A rule that is not an orphan has an ifRuleOrph field of (Just n), where
298 n appears on the LHS of the rule; any change in the rule changes the version of n.
302 -- -----------------------------------------------------------------------------
305 ifaceDeclSubBndrs :: IfaceDecl -> [OccName]
306 -- *Excludes* the 'main' name, but *includes* the implicitly-bound names
307 -- Deeply revolting, because it has to predict what gets bound,
308 -- especially the question of whether there's a wrapper for a datacon
310 ifaceDeclSubBndrs (IfaceClass {ifCtxt = sc_ctxt, ifName = cls_occ,
311 ifSigs = sigs, ifATs = ats })
313 [tc_occ, dc_occ, dcww_occ] ++
314 [op | IfaceClassOp op _ _ <- sigs] ++
315 [ifName at | at <- ats ] ++
316 [mkSuperDictSelOcc n cls_occ | n <- [1..n_ctxt]]
318 n_ctxt = length sc_ctxt
320 tc_occ = mkClassTyConOcc cls_occ
321 dc_occ = mkClassDataConOcc cls_occ
322 co_occs | is_newtype = [mkNewTyCoOcc tc_occ]
324 dcww_occ -- | is_newtype = mkDataConWrapperOcc dc_occ -- Newtypes have wrapper but no worker
325 | otherwise = mkDataConWorkerOcc dc_occ -- Otherwise worker but no wrapper
326 is_newtype = n_sigs + n_ctxt == 1 -- Sigh
328 ifaceDeclSubBndrs IfaceData {ifCons = IfAbstractTyCon}
331 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
332 ifCons = IfNewTyCon (
333 IfCon { ifConOcc = con_occ,
336 ifFamInst = famInst})
337 = fields ++ [con_occ, mkDataConWorkerOcc con_occ, mkNewTyCoOcc tc_occ]
338 ++ famInstCo famInst tc_occ
340 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
341 ifCons = IfDataTyCon cons,
342 ifFamInst = famInst})
343 = nub (concatMap ifConFields cons) -- Eliminate duplicate fields
344 ++ concatMap dc_occs cons
345 ++ famInstCo famInst tc_occ
348 | has_wrapper = [con_occ, work_occ, wrap_occ]
349 | otherwise = [con_occ, work_occ]
351 con_occ = ifConOcc con_decl
352 strs = ifConStricts con_decl
353 wrap_occ = mkDataConWrapperOcc con_occ
354 work_occ = mkDataConWorkerOcc con_occ
355 has_wrapper = any isMarkedStrict strs -- See MkId.mkDataConIds (sigh)
356 || not (null . ifConEqSpec $ con_decl)
358 -- ToDo: may miss strictness in existential dicts
360 ifaceDeclSubBndrs _other = []
362 -- coercion for data/newtype family instances
363 famInstCo Nothing baseOcc = []
364 famInstCo (Just _) baseOcc = [mkInstTyCoOcc baseOcc]
366 ----------------------------- Printing IfaceDecl ------------------------------
368 instance Outputable IfaceDecl where
371 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
372 = sep [ ppr var <+> dcolon <+> ppr ty,
375 pprIfaceDecl (IfaceForeign {ifName = tycon})
376 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
378 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
379 ifOpenSyn = False, ifSynRhs = mono_ty})
380 = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
381 4 (equals <+> ppr mono_ty)
383 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
384 ifOpenSyn = True, ifSynRhs = mono_ty})
385 = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
386 4 (dcolon <+> ppr mono_ty)
388 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
389 ifTyVars = tyvars, ifCons = condecls,
390 ifRec = isrec, ifFamInst = mbFamInst})
391 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
392 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
393 pprFamily mbFamInst])
395 pp_nd = case condecls of
396 IfAbstractTyCon -> ptext SLIT("data")
397 IfOpenDataTyCon -> ptext SLIT("data family")
398 IfDataTyCon _ -> ptext SLIT("data")
399 IfNewTyCon _ -> ptext SLIT("newtype")
400 IfOpenNewTyCon -> ptext SLIT("newtype family")
402 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
403 ifFDs = fds, ifATs = ats, ifSigs = sigs,
405 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
406 4 (vcat [pprRec isrec,
410 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
411 pprGen True = ptext SLIT("Generics: yes")
412 pprGen False = ptext SLIT("Generics: no")
414 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
415 pprFamily (Just famInst) = ptext SLIT("FamilyInstance:") <+> ppr famInst
417 instance Outputable IfaceClassOp where
418 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
420 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
421 pprIfaceDeclHead context thing tyvars
422 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
423 pprIfaceTvBndrs tyvars]
425 pp_condecls tc IfAbstractTyCon = ptext SLIT("{- abstract -}")
426 pp_condecls tc IfOpenNewTyCon = empty
427 pp_condecls tc (IfNewTyCon c) = equals <+> pprIfaceConDecl tc c
428 pp_condecls tc IfOpenDataTyCon = empty
429 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
430 (map (pprIfaceConDecl tc) cs))
432 pprIfaceConDecl :: OccName -> IfaceConDecl -> SDoc
434 (IfCon { ifConOcc = name, ifConInfix = is_infix,
435 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
436 ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys,
437 ifConStricts = strs, ifConFields = fields })
439 if is_infix then ptext SLIT("Infix") else empty,
440 if null strs then empty
441 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
442 if null fields then empty
443 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
445 main_payload = ppr name <+> dcolon <+>
446 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) pp_tau
448 eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty)
449 | (tv,ty) <- eq_spec]
451 -- A bit gruesome this, but we can't form the full con_tau, and ppr it,
452 -- because we don't have a Name for the tycon, only an OccName
453 pp_tau = case map pprParendIfaceType arg_tys ++ [pp_res_ty] of
454 (t:ts) -> fsep (t : map (arrow <+>) ts)
455 [] -> panic "pp_con_taus"
457 pp_res_ty = ppr tc <+> fsep [ppr tv | (tv,_) <- univ_tvs]
459 instance Outputable IfaceRule where
460 ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
461 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
462 = sep [hsep [doubleQuotes (ftext name), ppr act,
463 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
464 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
465 ptext SLIT("=") <+> ppr rhs])
468 instance Outputable IfaceInst where
469 ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag,
470 ifInstCls = cls, ifInstTys = mb_tcs})
471 = hang (ptext SLIT("instance") <+> ppr flag
472 <+> ppr cls <+> brackets (pprWithCommas ppr_rough mb_tcs))
473 2 (equals <+> ppr dfun_id)
475 instance Outputable IfaceFamInst where
476 ppr (IfaceFamInst {ifFamInstFam = fam, ifFamInstTys = mb_tcs,
477 ifFamInstTyCon = tycon_id})
478 = hang (ptext SLIT("family instance") <+>
479 ppr fam <+> brackets (pprWithCommas ppr_rough mb_tcs))
480 2 (equals <+> ppr tycon_id)
482 ppr_rough :: Maybe IfaceTyCon -> SDoc
483 ppr_rough Nothing = dot
484 ppr_rough (Just tc) = ppr tc
488 ----------------------------- Printing IfaceExpr ------------------------------------
491 instance Outputable IfaceExpr where
492 ppr e = pprIfaceExpr noParens e
494 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
495 -- The function adds parens in context that need
496 -- an atomic value (e.g. function args)
498 pprIfaceExpr add_par (IfaceLcl v) = ppr v
499 pprIfaceExpr add_par (IfaceExt v) = ppr v
500 pprIfaceExpr add_par (IfaceLit l) = ppr l
501 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
502 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
504 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
505 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
507 pprIfaceExpr add_par e@(IfaceLam _ _)
508 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
509 pprIfaceExpr noParens body])
511 (bndrs,body) = collect [] e
512 collect bs (IfaceLam b e) = collect (b:bs) e
513 collect bs e = (reverse bs, e)
515 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
516 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
517 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
518 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
519 pprIfaceExpr noParens rhs <+> char '}'])
521 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
522 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
523 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
524 <+> ppr bndr <+> char '{',
525 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
527 pprIfaceExpr add_par (IfaceCast expr co)
528 = sep [pprIfaceExpr parens expr,
529 nest 2 (ptext SLIT("`cast`")),
530 pprParendIfaceType co]
532 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
533 = add_par (sep [ptext SLIT("let {"),
534 nest 2 (ppr_bind (b, rhs)),
536 pprIfaceExpr noParens body])
538 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
539 = add_par (sep [ptext SLIT("letrec {"),
540 nest 2 (sep (map ppr_bind pairs)),
542 pprIfaceExpr noParens body])
544 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
546 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
547 arrow <+> pprIfaceExpr noParens rhs]
549 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
550 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
552 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty,
553 equals <+> pprIfaceExpr noParens rhs]
556 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
557 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
560 instance Outputable IfaceNote where
561 ppr (IfaceSCC cc) = pprCostCentreCore cc
562 ppr IfaceInlineMe = ptext SLIT("__inline_me")
563 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
566 instance Outputable IfaceConAlt where
567 ppr IfaceDefault = text "DEFAULT"
568 ppr (IfaceLitAlt l) = ppr l
569 ppr (IfaceDataAlt d) = ppr d
570 ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt"
571 -- IfaceTupleAlt is handled by the case-alternative printer
574 instance Outputable IfaceIdInfo where
576 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
578 ppr_hs_info (HsUnfold unf) = ptext SLIT("Unfolding:") <+>
579 parens (pprIfaceExpr noParens unf)
580 ppr_hs_info (HsInline act) = ptext SLIT("Inline:") <+> ppr act
581 ppr_hs_info (HsArity arity) = ptext SLIT("Arity:") <+> int arity
582 ppr_hs_info (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
583 ppr_hs_info HsNoCafRefs = ptext SLIT("HasNoCafRefs")
584 ppr_hs_info (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
588 %************************************************************************
590 Equality, for interface file version generaion only
592 %************************************************************************
594 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new
595 constructor is EqBut, which gives the set of things whose version must
596 be equal for the whole thing to be equal. So the key function is
597 eqIfExt, which compares Names.
599 Of course, equality is also done modulo alpha conversion.
603 = Equal -- Definitely exactly the same
604 | NotEqual -- Definitely different
605 | EqBut a -- The same provided these Names have not changed
607 type IfaceEq = GenIfaceEq NameSet
609 instance Outputable IfaceEq where
610 ppr Equal = ptext SLIT("Equal")
611 ppr NotEqual = ptext SLIT("NotEqual")
612 ppr (EqBut occset) = ptext SLIT("EqBut") <+> ppr (nameSetToList occset)
614 bool :: Bool -> IfaceEq
616 bool False = NotEqual
618 toBool :: IfaceEq -> Bool
620 toBool (EqBut _) = True
621 toBool NotEqual = False
623 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
624 zapEq (EqBut _) = Equal
627 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
629 NotEqual &&& x = NotEqual
630 EqBut nms &&& Equal = EqBut nms
631 EqBut nms &&& NotEqual = NotEqual
632 EqBut nms1 &&& EqBut nms2 = EqBut (nms1 `unionNameSets` nms2)
634 -- This function is the core of the EqBut stuff
635 -- ASSUMPTION: The left-hand argument is the NEW CODE, and hence
636 -- any Names in the left-hand arg have the correct parent in them.
637 eqIfExt :: Name -> Name -> IfaceEq
639 | name1 == name2 = EqBut (unitNameSet name1)
640 | otherwise = NotEqual
642 ---------------------
643 checkBootDecl :: IfaceDecl -- The boot decl
644 -> IfaceDecl -- The real decl
645 -> Bool -- True <=> compatible
646 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
647 = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
649 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
650 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
652 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
653 = ASSERT( ifName d1 == ifName d2 )
654 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
655 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
657 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
658 -- We don't check the recursion flags because the boot-one is
659 -- recursive, to be conservative, but the real one may not be.
660 -- I'm not happy with the way recursive flags are dealt with.
661 = ASSERT( ifName d1 == ifName d2 )
662 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
663 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
665 IfAbstractTyCon -> Equal
666 cons1 -> eq_hsCD env cons1 (ifCons d2)
668 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
669 = ASSERT( ifName d1 == ifName d2 )
670 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
671 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
672 case (ifCtxt d1, ifSigs d1) of
674 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
675 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
677 checkBootDecl _ _ = False -- default case
679 ---------------------
680 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
681 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
682 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
684 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
685 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
687 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
688 = bool (ifName d1 == ifName d2 &&
689 ifRec d1 == ifRec d2 &&
690 ifGadtSyntax d1 == ifGadtSyntax d2 &&
691 ifGeneric d1 == ifGeneric d2) &&&
692 ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
693 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
694 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
695 eq_hsCD env (ifCons d1) (ifCons d2)
697 -- The type variables of the data type do not scope
698 -- over the constructors (any more), but they do scope
699 -- over the stupid context in the IfaceConDecls
701 Nothing `eqIfTc_fam` Nothing = Equal
702 (Just (fam1, tys1)) `eqIfTc_fam` (Just (fam2, tys2)) =
703 fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2
704 _ `eqIfTc_fam` _ = NotEqual
706 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
707 = bool (ifName d1 == ifName d2) &&&
708 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
709 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
712 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
713 = bool (ifName d1 == ifName d2 &&
714 ifRec d1 == ifRec d2) &&&
715 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
716 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
717 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
718 eqListBy eqIfDecl (ifATs d1) (ifATs d2) &&&
719 eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
722 eqIfDecl _ _ = NotEqual -- default case
725 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
726 eqWith = eq_ifTvBndrs emptyEqEnv
728 -----------------------
729 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
730 -- All other changes are handled via the version info on the dfun
732 eqIfFamInst d1 d2 = bool (ifFamInstTyCon d1 == ifFamInstTyCon d2)
733 -- All other changes are handled via the version info on the tycon
735 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
736 (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
737 = bool (n1==n2 && a1==a2 && o1 == o2) &&&
739 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
740 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
741 -- zapEq: for the LHSs, ignore the EqBut part
742 eq_ifaceExpr env rhs1 rhs2)
744 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
745 = eqListBy (eq_ConDecl env) c1 c2
747 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
748 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
749 eq_hsCD env IfOpenDataTyCon IfOpenDataTyCon = Equal
750 eq_hsCD env IfOpenNewTyCon IfOpenNewTyCon = Equal
751 eq_hsCD env d1 d2 = NotEqual
754 = bool (ifConOcc c1 == ifConOcc c2 &&
755 ifConInfix c1 == ifConInfix c2 &&
756 ifConStricts c1 == ifConStricts c2 &&
757 ifConFields c1 == ifConFields c2) &&&
758 eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
759 eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
760 eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
761 eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
763 eq_hsFD env (ns1,ms1) (ns2,ms2)
764 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
766 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
767 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
773 eqIfIdInfo NoInfo NoInfo = Equal
774 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
775 eqIfIdInfo i1 i2 = NotEqual
777 eq_item (HsInline a1) (HsInline a2) = bool (a1 == a2)
778 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
779 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
780 eq_item (HsUnfold u1) (HsUnfold u2) = eq_ifaceExpr emptyEqEnv u1 u2
781 eq_item HsNoCafRefs HsNoCafRefs = Equal
782 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
783 eq_item _ _ = NotEqual
786 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
787 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
788 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
789 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
790 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
791 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
792 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
793 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
794 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
795 eq_ifaceExpr env (IfaceCast e1 co1) (IfaceCast e2 co2) = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
796 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
798 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
799 = eq_ifaceExpr env s1 s2 &&&
800 eq_ifType env ty1 ty2 &&&
801 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
803 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
804 = bool (eq_ifaceConAlt c1 c2) &&&
805 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
807 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
808 = eq_ifaceExpr env r1 r2 &&& eq_ifIdBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
810 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
811 = eq_ifIdBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
813 (bs1,rs1) = unzip as1
814 (bs2,rs2) = unzip as2
817 eq_ifaceExpr env _ _ = NotEqual
820 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
821 eq_ifaceConAlt IfaceDefault IfaceDefault = True
822 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
823 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
824 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
825 eq_ifaceConAlt _ _ = False
828 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
829 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
830 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
831 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
832 eq_ifaceNote env _ _ = NotEqual
836 ---------------------
837 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
840 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
841 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
842 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
843 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
844 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
845 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
846 eq_ifType env _ _ = NotEqual
849 eq_ifTypes env = eqListBy (eq_ifType env)
852 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
855 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
856 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
857 eq_ifPredType env _ _ = NotEqual
860 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
861 eqIfTc IfaceIntTc IfaceIntTc = Equal
862 eqIfTc IfaceCharTc IfaceCharTc = Equal
863 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
864 eqIfTc IfaceListTc IfaceListTc = Equal
865 eqIfTc IfacePArrTc IfacePArrTc = Equal
866 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
867 eqIfTc IfaceLiftedTypeKindTc IfaceLiftedTypeKindTc = Equal
868 eqIfTc IfaceOpenTypeKindTc IfaceOpenTypeKindTc = Equal
869 eqIfTc IfaceUnliftedTypeKindTc IfaceUnliftedTypeKindTc = Equal
870 eqIfTc IfaceUbxTupleKindTc IfaceUbxTupleKindTc = Equal
871 eqIfTc IfaceArgTypeKindTc IfaceArgTypeKindTc = Equal
872 eqIfTc _ _ = NotEqual
875 -----------------------------------------------------------
876 Support code for equality checking
877 -----------------------------------------------------------
880 ------------------------------------
881 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
883 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
884 eqIfOcc env n1 n2 = case lookupUFM env n1 of
885 Just n1 -> bool (n1 == n2)
886 Nothing -> bool (n1 == n2)
888 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
889 extendEqEnv env n1 n2 | n1 == n2 = env
890 | otherwise = addToUFM env n1 n2
893 emptyEqEnv = emptyUFM
895 ------------------------------------
896 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
898 eq_ifNakedBndr :: ExtEnv FastString
899 eq_ifBndr :: ExtEnv IfaceBndr
900 eq_ifTvBndr :: ExtEnv IfaceTvBndr
901 eq_ifIdBndr :: ExtEnv IfaceIdBndr
903 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
905 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
906 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
907 eq_ifBndr _ _ _ _ = NotEqual
909 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
910 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
912 eq_ifBndrs :: ExtEnv [IfaceBndr]
913 eq_ifIdBndrs :: ExtEnv [IfaceIdBndr]
914 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
915 eq_ifNakedBndrs :: ExtEnv [FastString]
916 eq_ifBndrs = eq_bndrs_with eq_ifBndr
917 eq_ifIdBndrs = eq_bndrs_with eq_ifIdBndr
918 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
919 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
921 eq_bndrs_with eq env [] [] k = k env
922 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
923 eq_bndrs_with eq env _ _ _ = NotEqual
927 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
928 eqListBy eq [] [] = Equal
929 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
930 eqListBy eq xs ys = NotEqual
932 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
933 eqMaybeBy eq Nothing Nothing = Equal
934 eqMaybeBy eq (Just x) (Just y) = eq x y
935 eqMaybeBy eq x y = NotEqual