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(..), IfaceLetBndr(..),
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 | IfDataTyCon [IfaceConDecl] -- data type decls
112 | IfNewTyCon IfaceConDecl -- newtype decls
114 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
115 visibleIfConDecls IfAbstractTyCon = []
116 visibleIfConDecls IfOpenDataTyCon = []
117 visibleIfConDecls (IfDataTyCon cs) = cs
118 visibleIfConDecls (IfNewTyCon c) = [c]
122 ifConOcc :: OccName, -- Constructor name
123 ifConInfix :: Bool, -- True <=> declared infix
124 ifConUnivTvs :: [IfaceTvBndr], -- Universal tyvars
125 ifConExTvs :: [IfaceTvBndr], -- Existential tyvars
126 ifConEqSpec :: [(OccName,IfaceType)], -- Equality contraints
127 ifConCtxt :: IfaceContext, -- Non-stupid context
128 ifConArgTys :: [IfaceType], -- Arg types
129 ifConFields :: [OccName], -- ...ditto... (field labels)
130 ifConStricts :: [StrictnessMark]} -- Empty (meaning all lazy),
131 -- or 1-1 corresp with arg tys
134 = IfaceInst { ifInstCls :: Name, -- See comments with
135 ifInstTys :: [Maybe IfaceTyCon], -- the defn of Instance
136 ifDFun :: Name, -- The dfun
137 ifOFlag :: OverlapFlag, -- Overlap flag
138 ifInstOrph :: Maybe OccName } -- See Note [Orphans]
139 -- There's always a separate IfaceDecl for the DFun, which gives
140 -- its IdInfo with its full type and version number.
141 -- The instance declarations taken together have a version number,
142 -- and we don't want that to wobble gratuitously
143 -- If this instance decl is *used*, we'll record a usage on the dfun;
144 -- and if the head does not change it won't be used if it wasn't before
147 = IfaceFamInst { ifFamInstFam :: Name -- Family tycon
148 , ifFamInstTys :: [Maybe IfaceTyCon] -- Rough match types
149 , ifFamInstTyCon :: IfaceTyCon -- Instance decl
154 ifRuleName :: RuleName,
155 ifActivation :: Activation,
156 ifRuleBndrs :: [IfaceBndr], -- Tyvars and term vars
157 ifRuleHead :: Name, -- Head of lhs
158 ifRuleArgs :: [IfaceExpr], -- Args of LHS
159 ifRuleRhs :: IfaceExpr,
160 ifRuleOrph :: Maybe OccName -- Just like IfaceInst
164 = NoInfo -- When writing interface file without -O
165 | HasInfo [IfaceInfoItem] -- Has info, and here it is
167 -- Here's a tricky case:
168 -- * Compile with -O module A, and B which imports A.f
169 -- * Change function f in A, and recompile without -O
170 -- * When we read in old A.hi we read in its IdInfo (as a thunk)
171 -- (In earlier GHCs we used to drop IdInfo immediately on reading,
172 -- but we do not do that now. Instead it's discarded when the
173 -- ModIface is read into the various decl pools.)
174 -- * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
175 -- and so gives a new version.
179 | HsStrictness StrictSig
180 | HsInline Activation
183 | HsWorker Name Arity -- Worker, if any see IdInfo.WorkerInfo
184 -- for why we want arity here.
185 -- NB: we need IfaceExtName (not just OccName) because the worker
186 -- can simplify to a function in another module.
187 -- NB: Specialisations and rules come in separately and are
188 -- only later attached to the Id. Partial reason: some are orphans.
190 --------------------------------
192 = IfaceLcl FastString
194 | IfaceType IfaceType
195 | IfaceTuple Boxity [IfaceExpr] -- Saturated; type arguments omitted
196 | IfaceLam IfaceBndr IfaceExpr
197 | IfaceApp IfaceExpr IfaceExpr
198 | IfaceCase IfaceExpr FastString IfaceType [IfaceAlt]
199 | IfaceLet IfaceBinding IfaceExpr
200 | IfaceNote IfaceNote IfaceExpr
201 | IfaceCast IfaceExpr IfaceCoercion
203 | IfaceFCall ForeignCall IfaceType
205 data IfaceNote = IfaceSCC CostCentre
207 | IfaceCoreNote String
209 type IfaceAlt = (IfaceConAlt, [FastString], IfaceExpr)
210 -- Note: FastString, not IfaceBndr (and same with the case binder)
211 -- We reconstruct the kind/type of the thing from the context
212 -- thus saving bulk in interface files
214 data IfaceConAlt = IfaceDefault
216 | IfaceTupleAlt Boxity
217 | IfaceLitAlt Literal
220 = IfaceNonRec IfaceLetBndr IfaceExpr
221 | IfaceRec [(IfaceLetBndr, IfaceExpr)]
223 -- IfaceLetBndr is like IfaceIdBndr, but has IdInfo too
224 -- It's used for *non-top-level* let/rec binders
225 -- See Note [IdInfo on nested let-bindings]
226 data IfaceLetBndr = IfLetBndr FastString IfaceType IfaceIdInfo
229 Note [IdInfo on nested let-bindings]
230 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231 Occasionally we want to preserve IdInfo on nested let bindings The one
232 that came up was a NOINLINE pragma on a let-binding inside an INLINE
233 function. The user (Duncan Coutts) really wanted the NOINLINE control
234 to cross the separate compilation boundary.
236 So a IfaceLetBndr keeps a trimmed-down list of IfaceIdInfo stuff.
237 Currently we only actually retain InlinePragInfo, but in principle we could
241 Note [Orphans]: the ifInstOrph and ifRuleOrph fields
242 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 If a module contains any "orphans", then its interface file is read
244 regardless, so that its instances are not missed.
246 Roughly speaking, an instance is an orphan if its head (after the =>)
247 mentions nothing defined in this module. Functional dependencies
248 complicate the situation though. Consider
250 module M where { class C a b | a -> b }
252 and suppose we are compiling module X:
257 instance C Int T where ...
259 This instance is an orphan, because when compiling a third module Y we
260 might get a constraint (C Int v), and we'd want to improve v to T. So
261 we must make sure X's instances are loaded, even if we do not directly
264 More precisely, an instance is an orphan iff
266 If there are no fundeps, then at least of the names in
267 the instance head is locally defined.
269 If there are fundeps, then for every fundep, at least one of the
270 names free in a *non-determined* part of the instance head is
271 defined in this module.
273 (Note that these conditions hold trivially if the class is locally
276 Note [Versioning of instances]
277 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278 Now consider versioning. If we *use* an instance decl in one compilation,
279 we'll depend on the dfun id for that instance, so we'll recompile if it changes.
280 But suppose we *don't* (currently) use an instance! We must recompile if
281 the instance is changed in such a way that it becomes important. (This would
282 only matter with overlapping instances, else the importing module wouldn't have
283 compiled before and the recompilation check is irrelevant.)
285 The is_orph field is set to (Just n) if the instance is not an orphan.
286 The 'n' is *any* of the locally-defined names mentioned anywhere in the
287 instance head. This name is used for versioning; the instance decl is
288 considered part of the defn of this 'n'.
290 I'm worried about whether this works right if we pick a name from
291 a functionally-dependent part of the instance decl. E.g.
293 module M where { class C a b | a -> b }
295 and suppose we are compiling module X:
301 instance C S T where ...
303 If we base the instance verion on T, I'm worried that changing S to S'
304 would change T's version, but not S or S'. But an importing module might
305 not depend on T, and so might not be recompiled even though the new instance
306 (C S' T) might be relevant. I have not been able to make a concrete example,
307 and it seems deeply obscure, so I'm going to leave it for now.
310 Note [Versioning of rules]
311 ~~~~~~~~~~~~~~~~~~~~~~~~~~
312 A rule that is not an orphan has an ifRuleOrph field of (Just n), where
313 n appears on the LHS of the rule; any change in the rule changes the version of n.
317 -- -----------------------------------------------------------------------------
320 ifaceDeclSubBndrs :: IfaceDecl -> [OccName]
321 -- *Excludes* the 'main' name, but *includes* the implicitly-bound names
322 -- Deeply revolting, because it has to predict what gets bound,
323 -- especially the question of whether there's a wrapper for a datacon
325 ifaceDeclSubBndrs (IfaceClass {ifCtxt = sc_ctxt, ifName = cls_occ,
326 ifSigs = sigs, ifATs = ats })
328 [tc_occ, dc_occ, dcww_occ] ++
329 [op | IfaceClassOp op _ _ <- sigs] ++
330 [ifName at | at <- ats ] ++
331 [mkSuperDictSelOcc n cls_occ | n <- [1..n_ctxt]]
333 n_ctxt = length sc_ctxt
335 tc_occ = mkClassTyConOcc cls_occ
336 dc_occ = mkClassDataConOcc cls_occ
337 co_occs | is_newtype = [mkNewTyCoOcc tc_occ]
339 dcww_occ -- | is_newtype = mkDataConWrapperOcc dc_occ -- Newtypes have wrapper but no worker
340 | otherwise = mkDataConWorkerOcc dc_occ -- Otherwise worker but no wrapper
341 is_newtype = n_sigs + n_ctxt == 1 -- Sigh
343 ifaceDeclSubBndrs IfaceData {ifCons = IfAbstractTyCon}
346 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
347 ifCons = IfNewTyCon (
348 IfCon { ifConOcc = con_occ,
351 ifFamInst = famInst})
352 = fields ++ [con_occ, mkDataConWorkerOcc con_occ, mkNewTyCoOcc tc_occ]
353 ++ famInstCo famInst tc_occ
355 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
356 ifCons = IfDataTyCon cons,
357 ifFamInst = famInst})
358 = nub (concatMap ifConFields cons) -- Eliminate duplicate fields
359 ++ concatMap dc_occs cons
360 ++ famInstCo famInst tc_occ
363 | has_wrapper = [con_occ, work_occ, wrap_occ]
364 | otherwise = [con_occ, work_occ]
366 con_occ = ifConOcc con_decl
367 strs = ifConStricts con_decl
368 wrap_occ = mkDataConWrapperOcc con_occ
369 work_occ = mkDataConWorkerOcc con_occ
370 has_wrapper = any isMarkedStrict strs -- See MkId.mkDataConIds (sigh)
371 || not (null . ifConEqSpec $ con_decl)
373 -- ToDo: may miss strictness in existential dicts
375 ifaceDeclSubBndrs _other = []
377 -- coercion for data/newtype family instances
378 famInstCo Nothing baseOcc = []
379 famInstCo (Just _) baseOcc = [mkInstTyCoOcc baseOcc]
381 ----------------------------- Printing IfaceDecl ------------------------------
383 instance Outputable IfaceDecl where
386 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
387 = sep [ ppr var <+> dcolon <+> ppr ty,
390 pprIfaceDecl (IfaceForeign {ifName = tycon})
391 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
393 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
394 ifOpenSyn = False, ifSynRhs = mono_ty})
395 = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
396 4 (equals <+> ppr mono_ty)
398 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
399 ifOpenSyn = True, ifSynRhs = mono_ty})
400 = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
401 4 (dcolon <+> ppr mono_ty)
403 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
404 ifTyVars = tyvars, ifCons = condecls,
405 ifRec = isrec, ifFamInst = mbFamInst})
406 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
407 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
408 pprFamily mbFamInst])
410 pp_nd = case condecls of
411 IfAbstractTyCon -> ptext SLIT("data")
412 IfOpenDataTyCon -> ptext SLIT("data family")
413 IfDataTyCon _ -> ptext SLIT("data")
414 IfNewTyCon _ -> ptext SLIT("newtype")
416 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
417 ifFDs = fds, ifATs = ats, ifSigs = sigs,
419 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
420 4 (vcat [pprRec isrec,
424 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
425 pprGen True = ptext SLIT("Generics: yes")
426 pprGen False = ptext SLIT("Generics: no")
428 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
429 pprFamily (Just famInst) = ptext SLIT("FamilyInstance:") <+> ppr famInst
431 instance Outputable IfaceClassOp where
432 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
434 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
435 pprIfaceDeclHead context thing tyvars
436 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
437 pprIfaceTvBndrs tyvars]
439 pp_condecls tc IfAbstractTyCon = ptext SLIT("{- abstract -}")
440 pp_condecls tc (IfNewTyCon c) = equals <+> pprIfaceConDecl tc c
441 pp_condecls tc IfOpenDataTyCon = empty
442 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
443 (map (pprIfaceConDecl tc) cs))
445 pprIfaceConDecl :: OccName -> IfaceConDecl -> SDoc
447 (IfCon { ifConOcc = name, ifConInfix = is_infix,
448 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
449 ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys,
450 ifConStricts = strs, ifConFields = fields })
452 if is_infix then ptext SLIT("Infix") else empty,
453 if null strs then empty
454 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
455 if null fields then empty
456 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
458 main_payload = ppr name <+> dcolon <+>
459 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) pp_tau
461 eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty)
462 | (tv,ty) <- eq_spec]
464 -- A bit gruesome this, but we can't form the full con_tau, and ppr it,
465 -- because we don't have a Name for the tycon, only an OccName
466 pp_tau = case map pprParendIfaceType arg_tys ++ [pp_res_ty] of
467 (t:ts) -> fsep (t : map (arrow <+>) ts)
468 [] -> panic "pp_con_taus"
470 pp_res_ty = ppr tc <+> fsep [ppr tv | (tv,_) <- univ_tvs]
472 instance Outputable IfaceRule where
473 ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
474 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
475 = sep [hsep [doubleQuotes (ftext name), ppr act,
476 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
477 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
478 ptext SLIT("=") <+> ppr rhs])
481 instance Outputable IfaceInst where
482 ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag,
483 ifInstCls = cls, ifInstTys = mb_tcs})
484 = hang (ptext SLIT("instance") <+> ppr flag
485 <+> ppr cls <+> brackets (pprWithCommas ppr_rough mb_tcs))
486 2 (equals <+> ppr dfun_id)
488 instance Outputable IfaceFamInst where
489 ppr (IfaceFamInst {ifFamInstFam = fam, ifFamInstTys = mb_tcs,
490 ifFamInstTyCon = tycon_id})
491 = hang (ptext SLIT("family instance") <+>
492 ppr fam <+> brackets (pprWithCommas ppr_rough mb_tcs))
493 2 (equals <+> ppr tycon_id)
495 ppr_rough :: Maybe IfaceTyCon -> SDoc
496 ppr_rough Nothing = dot
497 ppr_rough (Just tc) = ppr tc
501 ----------------------------- Printing IfaceExpr ------------------------------------
504 instance Outputable IfaceExpr where
505 ppr e = pprIfaceExpr noParens e
507 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
508 -- The function adds parens in context that need
509 -- an atomic value (e.g. function args)
511 pprIfaceExpr add_par (IfaceLcl v) = ppr v
512 pprIfaceExpr add_par (IfaceExt v) = ppr v
513 pprIfaceExpr add_par (IfaceLit l) = ppr l
514 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
515 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
517 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
518 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
520 pprIfaceExpr add_par e@(IfaceLam _ _)
521 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
522 pprIfaceExpr noParens body])
524 (bndrs,body) = collect [] e
525 collect bs (IfaceLam b e) = collect (b:bs) e
526 collect bs e = (reverse bs, e)
528 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
529 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
530 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
531 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
532 pprIfaceExpr noParens rhs <+> char '}'])
534 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
535 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
536 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
537 <+> ppr bndr <+> char '{',
538 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
540 pprIfaceExpr add_par (IfaceCast expr co)
541 = sep [pprIfaceExpr parens expr,
542 nest 2 (ptext SLIT("`cast`")),
543 pprParendIfaceType co]
545 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
546 = add_par (sep [ptext SLIT("let {"),
547 nest 2 (ppr_bind (b, rhs)),
549 pprIfaceExpr noParens body])
551 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
552 = add_par (sep [ptext SLIT("letrec {"),
553 nest 2 (sep (map ppr_bind pairs)),
555 pprIfaceExpr noParens body])
557 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
559 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
560 arrow <+> pprIfaceExpr noParens rhs]
562 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
563 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
565 ppr_bind (IfLetBndr b ty info, rhs)
566 = sep [hang (ppr b <+> dcolon <+> ppr ty) 2 (ppr info),
567 equals <+> pprIfaceExpr noParens rhs]
570 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
571 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
574 instance Outputable IfaceNote where
575 ppr (IfaceSCC cc) = pprCostCentreCore cc
576 ppr IfaceInlineMe = ptext SLIT("__inline_me")
577 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
580 instance Outputable IfaceConAlt where
581 ppr IfaceDefault = text "DEFAULT"
582 ppr (IfaceLitAlt l) = ppr l
583 ppr (IfaceDataAlt d) = ppr d
584 ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt"
585 -- IfaceTupleAlt is handled by the case-alternative printer
588 instance Outputable IfaceIdInfo where
590 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr is) <+> ptext SLIT("-}")
592 instance Outputable IfaceInfoItem where
593 ppr (HsUnfold unf) = ptext SLIT("Unfolding:") <+>
594 parens (pprIfaceExpr noParens unf)
595 ppr (HsInline act) = ptext SLIT("Inline:") <+> ppr act
596 ppr (HsArity arity) = ptext SLIT("Arity:") <+> int arity
597 ppr (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
598 ppr HsNoCafRefs = ptext SLIT("HasNoCafRefs")
599 ppr (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
603 %************************************************************************
605 Equality, for interface file version generaion only
607 %************************************************************************
609 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new
610 constructor is EqBut, which gives the set of things whose version must
611 be equal for the whole thing to be equal. So the key function is
612 eqIfExt, which compares Names.
614 Of course, equality is also done modulo alpha conversion.
618 = Equal -- Definitely exactly the same
619 | NotEqual -- Definitely different
620 | EqBut a -- The same provided these Names have not changed
622 type IfaceEq = GenIfaceEq NameSet
624 instance Outputable IfaceEq where
625 ppr Equal = ptext SLIT("Equal")
626 ppr NotEqual = ptext SLIT("NotEqual")
627 ppr (EqBut occset) = ptext SLIT("EqBut") <+> ppr (nameSetToList occset)
629 bool :: Bool -> IfaceEq
631 bool False = NotEqual
633 toBool :: IfaceEq -> Bool
635 toBool (EqBut _) = True
636 toBool NotEqual = False
638 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
639 zapEq (EqBut _) = Equal
642 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
644 NotEqual &&& x = NotEqual
645 EqBut nms &&& Equal = EqBut nms
646 EqBut nms &&& NotEqual = NotEqual
647 EqBut nms1 &&& EqBut nms2 = EqBut (nms1 `unionNameSets` nms2)
649 -- This function is the core of the EqBut stuff
650 -- ASSUMPTION: The left-hand argument is the NEW CODE, and hence
651 -- any Names in the left-hand arg have the correct parent in them.
652 eqIfExt :: Name -> Name -> IfaceEq
654 | name1 == name2 = EqBut (unitNameSet name1)
655 | otherwise = NotEqual
657 ---------------------
658 checkBootDecl :: IfaceDecl -- The boot decl
659 -> IfaceDecl -- The real decl
660 -> Bool -- True <=> compatible
661 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
662 = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
664 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
665 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
667 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
668 = ASSERT( ifName d1 == ifName d2 )
669 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
670 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
672 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
673 -- We don't check the recursion flags because the boot-one is
674 -- recursive, to be conservative, but the real one may not be.
675 -- I'm not happy with the way recursive flags are dealt with.
676 = ASSERT( ifName d1 == ifName d2 )
677 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
678 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
680 IfAbstractTyCon -> Equal
681 cons1 -> eq_hsCD env cons1 (ifCons d2)
683 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
684 = ASSERT( ifName d1 == ifName d2 )
685 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
686 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
687 case (ifCtxt d1, ifSigs d1) of
689 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
690 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
692 checkBootDecl _ _ = False -- default case
694 ---------------------
695 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
696 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
697 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
699 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
700 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
702 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
703 = bool (ifName d1 == ifName d2 &&
704 ifRec d1 == ifRec d2 &&
705 ifGadtSyntax d1 == ifGadtSyntax d2 &&
706 ifGeneric d1 == ifGeneric d2) &&&
707 ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
708 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
709 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
710 eq_hsCD env (ifCons d1) (ifCons d2)
712 -- The type variables of the data type do not scope
713 -- over the constructors (any more), but they do scope
714 -- over the stupid context in the IfaceConDecls
716 Nothing `eqIfTc_fam` Nothing = Equal
717 (Just (fam1, tys1)) `eqIfTc_fam` (Just (fam2, tys2)) =
718 fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2
719 _ `eqIfTc_fam` _ = NotEqual
721 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
722 = bool (ifName d1 == ifName d2) &&&
723 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
724 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
727 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
728 = bool (ifName d1 == ifName d2 &&
729 ifRec d1 == ifRec d2) &&&
730 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
731 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
732 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
733 eqListBy eqIfDecl (ifATs d1) (ifATs d2) &&&
734 eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
737 eqIfDecl _ _ = NotEqual -- default case
740 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
741 eqWith = eq_ifTvBndrs emptyEqEnv
743 -----------------------
744 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
745 -- All other changes are handled via the version info on the dfun
747 eqIfFamInst d1 d2 = bool (ifFamInstTyCon d1 == ifFamInstTyCon d2)
748 -- All other changes are handled via the version info on the tycon
750 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
751 (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
752 = bool (n1==n2 && a1==a2 && o1 == o2) &&&
754 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
755 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
756 -- zapEq: for the LHSs, ignore the EqBut part
757 eq_ifaceExpr env rhs1 rhs2)
759 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
760 = eqListBy (eq_ConDecl env) c1 c2
762 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
763 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
764 eq_hsCD env IfOpenDataTyCon IfOpenDataTyCon = Equal
765 eq_hsCD env d1 d2 = NotEqual
768 = bool (ifConOcc c1 == ifConOcc c2 &&
769 ifConInfix c1 == ifConInfix c2 &&
770 ifConStricts c1 == ifConStricts c2 &&
771 ifConFields c1 == ifConFields c2) &&&
772 eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
773 eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
774 eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
775 eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
777 eq_hsFD env (ns1,ms1) (ns2,ms2)
778 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
780 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
781 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
787 eqIfIdInfo NoInfo NoInfo = Equal
788 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
789 eqIfIdInfo i1 i2 = NotEqual
791 eq_item (HsInline a1) (HsInline a2) = bool (a1 == a2)
792 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
793 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
794 eq_item (HsUnfold u1) (HsUnfold u2) = eq_ifaceExpr emptyEqEnv u1 u2
795 eq_item HsNoCafRefs HsNoCafRefs = Equal
796 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
797 eq_item _ _ = NotEqual
800 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
801 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
802 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
803 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
804 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
805 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
806 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
807 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
808 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
809 eq_ifaceExpr env (IfaceCast e1 co1) (IfaceCast e2 co2) = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
810 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
812 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
813 = eq_ifaceExpr env s1 s2 &&&
814 eq_ifType env ty1 ty2 &&&
815 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
817 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
818 = bool (eq_ifaceConAlt c1 c2) &&&
819 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
821 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
822 = eq_ifaceExpr env r1 r2 &&& eq_ifLetBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
824 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
825 = eq_ifLetBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
827 (bs1,rs1) = unzip as1
828 (bs2,rs2) = unzip as2
831 eq_ifaceExpr env _ _ = NotEqual
834 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
835 eq_ifaceConAlt IfaceDefault IfaceDefault = True
836 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
837 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
838 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
839 eq_ifaceConAlt _ _ = False
842 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
843 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
844 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
845 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
846 eq_ifaceNote env _ _ = NotEqual
850 ---------------------
851 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
854 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
855 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
856 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
857 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
858 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
859 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
860 eq_ifType env _ _ = NotEqual
863 eq_ifTypes env = eqListBy (eq_ifType env)
866 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
869 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
870 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
871 eq_ifPredType env _ _ = NotEqual
874 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
875 eqIfTc IfaceIntTc IfaceIntTc = Equal
876 eqIfTc IfaceCharTc IfaceCharTc = Equal
877 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
878 eqIfTc IfaceListTc IfaceListTc = Equal
879 eqIfTc IfacePArrTc IfacePArrTc = Equal
880 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
881 eqIfTc IfaceLiftedTypeKindTc IfaceLiftedTypeKindTc = Equal
882 eqIfTc IfaceOpenTypeKindTc IfaceOpenTypeKindTc = Equal
883 eqIfTc IfaceUnliftedTypeKindTc IfaceUnliftedTypeKindTc = Equal
884 eqIfTc IfaceUbxTupleKindTc IfaceUbxTupleKindTc = Equal
885 eqIfTc IfaceArgTypeKindTc IfaceArgTypeKindTc = Equal
886 eqIfTc _ _ = NotEqual
889 -----------------------------------------------------------
890 Support code for equality checking
891 -----------------------------------------------------------
894 ------------------------------------
895 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
897 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
898 eqIfOcc env n1 n2 = case lookupUFM env n1 of
899 Just n1 -> bool (n1 == n2)
900 Nothing -> bool (n1 == n2)
902 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
903 extendEqEnv env n1 n2 | n1 == n2 = env
904 | otherwise = addToUFM env n1 n2
907 emptyEqEnv = emptyUFM
909 ------------------------------------
910 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
912 eq_ifNakedBndr :: ExtEnv FastString
913 eq_ifBndr :: ExtEnv IfaceBndr
914 eq_ifTvBndr :: ExtEnv IfaceTvBndr
915 eq_ifIdBndr :: ExtEnv IfaceIdBndr
917 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
919 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
920 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
921 eq_ifBndr _ _ _ _ = NotEqual
923 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
924 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
926 eq_ifLetBndr env (IfLetBndr v1 t1 i1) (IfLetBndr v2 t2 i2) k
927 = eq_ifType env t1 t2 &&& eqIfIdInfo i1 i2 &&& k (extendEqEnv env v1 v2)
929 eq_ifBndrs :: ExtEnv [IfaceBndr]
930 eq_ifLetBndrs :: ExtEnv [IfaceLetBndr]
931 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
932 eq_ifNakedBndrs :: ExtEnv [FastString]
933 eq_ifBndrs = eq_bndrs_with eq_ifBndr
934 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
935 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
936 eq_ifLetBndrs = eq_bndrs_with eq_ifLetBndr
938 eq_bndrs_with eq env [] [] k = k env
939 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
940 eq_bndrs_with eq env _ _ _ = NotEqual
944 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
945 eqListBy eq [] [] = Equal
946 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
947 eqListBy eq xs ys = NotEqual
949 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
950 eqMaybeBy eq Nothing Nothing = Equal
951 eqMaybeBy eq (Just x) (Just y) = eq x y
952 eqMaybeBy eq x y = NotEqual