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 | 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 IfaceLetBndr IfaceExpr
223 | IfaceRec [(IfaceLetBndr, IfaceExpr)]
225 -- IfaceLetBndr is like IfaceIdBndr, but has IdInfo too
226 -- It's used for *non-top-level* let/rec binders
227 -- See Note [IdInfo on nested let-bindings]
228 data IfaceLetBndr = IfLetBndr FastString IfaceType IfaceIdInfo
231 Note [IdInfo on nested let-bindings]
232 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233 Occasionally we want to preserve IdInfo on nested let bindings The one
234 that came up was a NOINLINE pragma on a let-binding inside an INLINE
235 function. The user (Duncan Coutts) really wanted the NOINLINE control
236 to cross the separate compilation boundary.
238 So a IfaceLetBndr keeps a trimmed-down list of IfaceIdInfo stuff.
239 Currently we only actually retain InlinePragInfo, but in principle we could
243 Note [Orphans]: the ifInstOrph and ifRuleOrph fields
244 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245 If a module contains any "orphans", then its interface file is read
246 regardless, so that its instances are not missed.
248 Roughly speaking, an instance is an orphan if its head (after the =>)
249 mentions nothing defined in this module. Functional dependencies
250 complicate the situation though. Consider
252 module M where { class C a b | a -> b }
254 and suppose we are compiling module X:
259 instance C Int T where ...
261 This instance is an orphan, because when compiling a third module Y we
262 might get a constraint (C Int v), and we'd want to improve v to T. So
263 we must make sure X's instances are loaded, even if we do not directly
266 More precisely, an instance is an orphan iff
268 If there are no fundeps, then at least of the names in
269 the instance head is locally defined.
271 If there are fundeps, then for every fundep, at least one of the
272 names free in a *non-determined* part of the instance head is
273 defined in this module.
275 (Note that these conditions hold trivially if the class is locally
278 Note [Versioning of instances]
279 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280 Now consider versioning. If we *use* an instance decl in one compilation,
281 we'll depend on the dfun id for that instance, so we'll recompile if it changes.
282 But suppose we *don't* (currently) use an instance! We must recompile if
283 the instance is changed in such a way that it becomes important. (This would
284 only matter with overlapping instances, else the importing module wouldn't have
285 compiled before and the recompilation check is irrelevant.)
287 The is_orph field is set to (Just n) if the instance is not an orphan.
288 The 'n' is *any* of the locally-defined names mentioned anywhere in the
289 instance head. This name is used for versioning; the instance decl is
290 considered part of the defn of this 'n'.
292 I'm worried about whether this works right if we pick a name from
293 a functionally-dependent part of the instance decl. E.g.
295 module M where { class C a b | a -> b }
297 and suppose we are compiling module X:
303 instance C S T where ...
305 If we base the instance verion on T, I'm worried that changing S to S'
306 would change T's version, but not S or S'. But an importing module might
307 not depend on T, and so might not be recompiled even though the new instance
308 (C S' T) might be relevant. I have not been able to make a concrete example,
309 and it seems deeply obscure, so I'm going to leave it for now.
312 Note [Versioning of rules]
313 ~~~~~~~~~~~~~~~~~~~~~~~~~~
314 A rule that is not an orphan has an ifRuleOrph field of (Just n), where
315 n appears on the LHS of the rule; any change in the rule changes the version of n.
319 -- -----------------------------------------------------------------------------
322 ifaceDeclSubBndrs :: IfaceDecl -> [OccName]
323 -- *Excludes* the 'main' name, but *includes* the implicitly-bound names
324 -- Deeply revolting, because it has to predict what gets bound,
325 -- especially the question of whether there's a wrapper for a datacon
327 ifaceDeclSubBndrs (IfaceClass {ifCtxt = sc_ctxt, ifName = cls_occ,
328 ifSigs = sigs, ifATs = ats })
330 [tc_occ, dc_occ, dcww_occ] ++
331 [op | IfaceClassOp op _ _ <- sigs] ++
332 [ifName at | at <- ats ] ++
333 [mkSuperDictSelOcc n cls_occ | n <- [1..n_ctxt]]
335 n_ctxt = length sc_ctxt
337 tc_occ = mkClassTyConOcc cls_occ
338 dc_occ = mkClassDataConOcc cls_occ
339 co_occs | is_newtype = [mkNewTyCoOcc tc_occ]
341 dcww_occ -- | is_newtype = mkDataConWrapperOcc dc_occ -- Newtypes have wrapper but no worker
342 | otherwise = mkDataConWorkerOcc dc_occ -- Otherwise worker but no wrapper
343 is_newtype = n_sigs + n_ctxt == 1 -- Sigh
345 ifaceDeclSubBndrs IfaceData {ifCons = IfAbstractTyCon}
348 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
349 ifCons = IfNewTyCon (
350 IfCon { ifConOcc = con_occ,
353 ifFamInst = famInst})
354 = fields ++ [con_occ, mkDataConWorkerOcc con_occ, mkNewTyCoOcc tc_occ]
355 ++ famInstCo famInst tc_occ
357 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
358 ifCons = IfDataTyCon cons,
359 ifFamInst = famInst})
360 = nub (concatMap ifConFields cons) -- Eliminate duplicate fields
361 ++ concatMap dc_occs cons
362 ++ famInstCo famInst tc_occ
365 | has_wrapper = [con_occ, work_occ, wrap_occ]
366 | otherwise = [con_occ, work_occ]
368 con_occ = ifConOcc con_decl
369 strs = ifConStricts con_decl
370 wrap_occ = mkDataConWrapperOcc con_occ
371 work_occ = mkDataConWorkerOcc con_occ
372 has_wrapper = any isMarkedStrict strs -- See MkId.mkDataConIds (sigh)
373 || not (null . ifConEqSpec $ con_decl)
375 -- ToDo: may miss strictness in existential dicts
377 ifaceDeclSubBndrs _other = []
379 -- coercion for data/newtype family instances
380 famInstCo Nothing baseOcc = []
381 famInstCo (Just _) baseOcc = [mkInstTyCoOcc baseOcc]
383 ----------------------------- Printing IfaceDecl ------------------------------
385 instance Outputable IfaceDecl where
388 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
389 = sep [ ppr var <+> dcolon <+> ppr ty,
392 pprIfaceDecl (IfaceForeign {ifName = tycon})
393 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
395 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
396 ifOpenSyn = False, ifSynRhs = mono_ty})
397 = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
398 4 (equals <+> ppr mono_ty)
400 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
401 ifOpenSyn = True, ifSynRhs = mono_ty})
402 = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
403 4 (dcolon <+> ppr mono_ty)
405 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
406 ifTyVars = tyvars, ifCons = condecls,
407 ifRec = isrec, ifFamInst = mbFamInst})
408 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
409 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
410 pprFamily mbFamInst])
412 pp_nd = case condecls of
413 IfAbstractTyCon -> ptext SLIT("data")
414 IfOpenDataTyCon -> ptext SLIT("data family")
415 IfDataTyCon _ -> ptext SLIT("data")
416 IfNewTyCon _ -> ptext SLIT("newtype")
417 IfOpenNewTyCon -> ptext SLIT("newtype family")
419 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
420 ifFDs = fds, ifATs = ats, ifSigs = sigs,
422 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
423 4 (vcat [pprRec isrec,
427 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
428 pprGen True = ptext SLIT("Generics: yes")
429 pprGen False = ptext SLIT("Generics: no")
431 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
432 pprFamily (Just famInst) = ptext SLIT("FamilyInstance:") <+> ppr famInst
434 instance Outputable IfaceClassOp where
435 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
437 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
438 pprIfaceDeclHead context thing tyvars
439 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
440 pprIfaceTvBndrs tyvars]
442 pp_condecls tc IfAbstractTyCon = ptext SLIT("{- abstract -}")
443 pp_condecls tc IfOpenNewTyCon = empty
444 pp_condecls tc (IfNewTyCon c) = equals <+> pprIfaceConDecl tc c
445 pp_condecls tc IfOpenDataTyCon = empty
446 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
447 (map (pprIfaceConDecl tc) cs))
449 pprIfaceConDecl :: OccName -> IfaceConDecl -> SDoc
451 (IfCon { ifConOcc = name, ifConInfix = is_infix,
452 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
453 ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys,
454 ifConStricts = strs, ifConFields = fields })
456 if is_infix then ptext SLIT("Infix") else empty,
457 if null strs then empty
458 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
459 if null fields then empty
460 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
462 main_payload = ppr name <+> dcolon <+>
463 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) pp_tau
465 eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty)
466 | (tv,ty) <- eq_spec]
468 -- A bit gruesome this, but we can't form the full con_tau, and ppr it,
469 -- because we don't have a Name for the tycon, only an OccName
470 pp_tau = case map pprParendIfaceType arg_tys ++ [pp_res_ty] of
471 (t:ts) -> fsep (t : map (arrow <+>) ts)
472 [] -> panic "pp_con_taus"
474 pp_res_ty = ppr tc <+> fsep [ppr tv | (tv,_) <- univ_tvs]
476 instance Outputable IfaceRule where
477 ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
478 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
479 = sep [hsep [doubleQuotes (ftext name), ppr act,
480 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
481 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
482 ptext SLIT("=") <+> ppr rhs])
485 instance Outputable IfaceInst where
486 ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag,
487 ifInstCls = cls, ifInstTys = mb_tcs})
488 = hang (ptext SLIT("instance") <+> ppr flag
489 <+> ppr cls <+> brackets (pprWithCommas ppr_rough mb_tcs))
490 2 (equals <+> ppr dfun_id)
492 instance Outputable IfaceFamInst where
493 ppr (IfaceFamInst {ifFamInstFam = fam, ifFamInstTys = mb_tcs,
494 ifFamInstTyCon = tycon_id})
495 = hang (ptext SLIT("family instance") <+>
496 ppr fam <+> brackets (pprWithCommas ppr_rough mb_tcs))
497 2 (equals <+> ppr tycon_id)
499 ppr_rough :: Maybe IfaceTyCon -> SDoc
500 ppr_rough Nothing = dot
501 ppr_rough (Just tc) = ppr tc
505 ----------------------------- Printing IfaceExpr ------------------------------------
508 instance Outputable IfaceExpr where
509 ppr e = pprIfaceExpr noParens e
511 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
512 -- The function adds parens in context that need
513 -- an atomic value (e.g. function args)
515 pprIfaceExpr add_par (IfaceLcl v) = ppr v
516 pprIfaceExpr add_par (IfaceExt v) = ppr v
517 pprIfaceExpr add_par (IfaceLit l) = ppr l
518 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
519 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
521 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
522 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
524 pprIfaceExpr add_par e@(IfaceLam _ _)
525 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
526 pprIfaceExpr noParens body])
528 (bndrs,body) = collect [] e
529 collect bs (IfaceLam b e) = collect (b:bs) e
530 collect bs e = (reverse bs, e)
532 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
533 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
534 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
535 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
536 pprIfaceExpr noParens rhs <+> char '}'])
538 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
539 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
540 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
541 <+> ppr bndr <+> char '{',
542 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
544 pprIfaceExpr add_par (IfaceCast expr co)
545 = sep [pprIfaceExpr parens expr,
546 nest 2 (ptext SLIT("`cast`")),
547 pprParendIfaceType co]
549 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
550 = add_par (sep [ptext SLIT("let {"),
551 nest 2 (ppr_bind (b, rhs)),
553 pprIfaceExpr noParens body])
555 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
556 = add_par (sep [ptext SLIT("letrec {"),
557 nest 2 (sep (map ppr_bind pairs)),
559 pprIfaceExpr noParens body])
561 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
563 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
564 arrow <+> pprIfaceExpr noParens rhs]
566 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
567 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
569 ppr_bind (IfLetBndr b ty info, rhs)
570 = sep [hang (ppr b <+> dcolon <+> ppr ty) 2 (ppr info),
571 equals <+> pprIfaceExpr noParens rhs]
574 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
575 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
578 instance Outputable IfaceNote where
579 ppr (IfaceSCC cc) = pprCostCentreCore cc
580 ppr IfaceInlineMe = ptext SLIT("__inline_me")
581 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
584 instance Outputable IfaceConAlt where
585 ppr IfaceDefault = text "DEFAULT"
586 ppr (IfaceLitAlt l) = ppr l
587 ppr (IfaceDataAlt d) = ppr d
588 ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt"
589 -- IfaceTupleAlt is handled by the case-alternative printer
592 instance Outputable IfaceIdInfo where
594 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr is) <+> ptext SLIT("-}")
596 instance Outputable IfaceInfoItem where
597 ppr (HsUnfold unf) = ptext SLIT("Unfolding:") <+>
598 parens (pprIfaceExpr noParens unf)
599 ppr (HsInline act) = ptext SLIT("Inline:") <+> ppr act
600 ppr (HsArity arity) = ptext SLIT("Arity:") <+> int arity
601 ppr (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
602 ppr HsNoCafRefs = ptext SLIT("HasNoCafRefs")
603 ppr (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
607 %************************************************************************
609 Equality, for interface file version generaion only
611 %************************************************************************
613 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new
614 constructor is EqBut, which gives the set of things whose version must
615 be equal for the whole thing to be equal. So the key function is
616 eqIfExt, which compares Names.
618 Of course, equality is also done modulo alpha conversion.
622 = Equal -- Definitely exactly the same
623 | NotEqual -- Definitely different
624 | EqBut a -- The same provided these Names have not changed
626 type IfaceEq = GenIfaceEq NameSet
628 instance Outputable IfaceEq where
629 ppr Equal = ptext SLIT("Equal")
630 ppr NotEqual = ptext SLIT("NotEqual")
631 ppr (EqBut occset) = ptext SLIT("EqBut") <+> ppr (nameSetToList occset)
633 bool :: Bool -> IfaceEq
635 bool False = NotEqual
637 toBool :: IfaceEq -> Bool
639 toBool (EqBut _) = True
640 toBool NotEqual = False
642 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
643 zapEq (EqBut _) = Equal
646 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
648 NotEqual &&& x = NotEqual
649 EqBut nms &&& Equal = EqBut nms
650 EqBut nms &&& NotEqual = NotEqual
651 EqBut nms1 &&& EqBut nms2 = EqBut (nms1 `unionNameSets` nms2)
653 -- This function is the core of the EqBut stuff
654 -- ASSUMPTION: The left-hand argument is the NEW CODE, and hence
655 -- any Names in the left-hand arg have the correct parent in them.
656 eqIfExt :: Name -> Name -> IfaceEq
658 | name1 == name2 = EqBut (unitNameSet name1)
659 | otherwise = NotEqual
661 ---------------------
662 checkBootDecl :: IfaceDecl -- The boot decl
663 -> IfaceDecl -- The real decl
664 -> Bool -- True <=> compatible
665 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
666 = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
668 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
669 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
671 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
672 = ASSERT( ifName d1 == ifName d2 )
673 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
674 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
676 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
677 -- We don't check the recursion flags because the boot-one is
678 -- recursive, to be conservative, but the real one may not be.
679 -- I'm not happy with the way recursive flags are dealt with.
680 = ASSERT( ifName d1 == ifName d2 )
681 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
682 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
684 IfAbstractTyCon -> Equal
685 cons1 -> eq_hsCD env cons1 (ifCons d2)
687 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
688 = ASSERT( ifName d1 == ifName d2 )
689 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
690 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
691 case (ifCtxt d1, ifSigs d1) of
693 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
694 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
696 checkBootDecl _ _ = False -- default case
698 ---------------------
699 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
700 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
701 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
703 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
704 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
706 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
707 = bool (ifName d1 == ifName d2 &&
708 ifRec d1 == ifRec d2 &&
709 ifGadtSyntax d1 == ifGadtSyntax d2 &&
710 ifGeneric d1 == ifGeneric d2) &&&
711 ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
712 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
713 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
714 eq_hsCD env (ifCons d1) (ifCons d2)
716 -- The type variables of the data type do not scope
717 -- over the constructors (any more), but they do scope
718 -- over the stupid context in the IfaceConDecls
720 Nothing `eqIfTc_fam` Nothing = Equal
721 (Just (fam1, tys1)) `eqIfTc_fam` (Just (fam2, tys2)) =
722 fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2
723 _ `eqIfTc_fam` _ = NotEqual
725 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
726 = bool (ifName d1 == ifName d2) &&&
727 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
728 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
731 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
732 = bool (ifName d1 == ifName d2 &&
733 ifRec d1 == ifRec d2) &&&
734 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
735 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
736 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
737 eqListBy eqIfDecl (ifATs d1) (ifATs d2) &&&
738 eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
741 eqIfDecl _ _ = NotEqual -- default case
744 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
745 eqWith = eq_ifTvBndrs emptyEqEnv
747 -----------------------
748 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
749 -- All other changes are handled via the version info on the dfun
751 eqIfFamInst d1 d2 = bool (ifFamInstTyCon d1 == ifFamInstTyCon d2)
752 -- All other changes are handled via the version info on the tycon
754 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
755 (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
756 = bool (n1==n2 && a1==a2 && o1 == o2) &&&
758 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
759 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
760 -- zapEq: for the LHSs, ignore the EqBut part
761 eq_ifaceExpr env rhs1 rhs2)
763 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
764 = eqListBy (eq_ConDecl env) c1 c2
766 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
767 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
768 eq_hsCD env IfOpenDataTyCon IfOpenDataTyCon = Equal
769 eq_hsCD env IfOpenNewTyCon IfOpenNewTyCon = Equal
770 eq_hsCD env d1 d2 = NotEqual
773 = bool (ifConOcc c1 == ifConOcc c2 &&
774 ifConInfix c1 == ifConInfix c2 &&
775 ifConStricts c1 == ifConStricts c2 &&
776 ifConFields c1 == ifConFields c2) &&&
777 eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
778 eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
779 eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
780 eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
782 eq_hsFD env (ns1,ms1) (ns2,ms2)
783 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
785 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
786 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
792 eqIfIdInfo NoInfo NoInfo = Equal
793 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
794 eqIfIdInfo i1 i2 = NotEqual
796 eq_item (HsInline a1) (HsInline a2) = bool (a1 == a2)
797 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
798 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
799 eq_item (HsUnfold u1) (HsUnfold u2) = eq_ifaceExpr emptyEqEnv u1 u2
800 eq_item HsNoCafRefs HsNoCafRefs = Equal
801 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
802 eq_item _ _ = NotEqual
805 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
806 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
807 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
808 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
809 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
810 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
811 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
812 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
813 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
814 eq_ifaceExpr env (IfaceCast e1 co1) (IfaceCast e2 co2) = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
815 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
817 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
818 = eq_ifaceExpr env s1 s2 &&&
819 eq_ifType env ty1 ty2 &&&
820 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
822 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
823 = bool (eq_ifaceConAlt c1 c2) &&&
824 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
826 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
827 = eq_ifaceExpr env r1 r2 &&& eq_ifLetBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
829 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
830 = eq_ifLetBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
832 (bs1,rs1) = unzip as1
833 (bs2,rs2) = unzip as2
836 eq_ifaceExpr env _ _ = NotEqual
839 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
840 eq_ifaceConAlt IfaceDefault IfaceDefault = True
841 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
842 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
843 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
844 eq_ifaceConAlt _ _ = False
847 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
848 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
849 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
850 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
851 eq_ifaceNote env _ _ = NotEqual
855 ---------------------
856 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
859 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
860 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
861 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
862 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
863 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
864 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
865 eq_ifType env _ _ = NotEqual
868 eq_ifTypes env = eqListBy (eq_ifType env)
871 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
874 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
875 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
876 eq_ifPredType env _ _ = NotEqual
879 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
880 eqIfTc IfaceIntTc IfaceIntTc = Equal
881 eqIfTc IfaceCharTc IfaceCharTc = Equal
882 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
883 eqIfTc IfaceListTc IfaceListTc = Equal
884 eqIfTc IfacePArrTc IfacePArrTc = Equal
885 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
886 eqIfTc IfaceLiftedTypeKindTc IfaceLiftedTypeKindTc = Equal
887 eqIfTc IfaceOpenTypeKindTc IfaceOpenTypeKindTc = Equal
888 eqIfTc IfaceUnliftedTypeKindTc IfaceUnliftedTypeKindTc = Equal
889 eqIfTc IfaceUbxTupleKindTc IfaceUbxTupleKindTc = Equal
890 eqIfTc IfaceArgTypeKindTc IfaceArgTypeKindTc = Equal
891 eqIfTc _ _ = NotEqual
894 -----------------------------------------------------------
895 Support code for equality checking
896 -----------------------------------------------------------
899 ------------------------------------
900 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
902 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
903 eqIfOcc env n1 n2 = case lookupUFM env n1 of
904 Just n1 -> bool (n1 == n2)
905 Nothing -> bool (n1 == n2)
907 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
908 extendEqEnv env n1 n2 | n1 == n2 = env
909 | otherwise = addToUFM env n1 n2
912 emptyEqEnv = emptyUFM
914 ------------------------------------
915 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
917 eq_ifNakedBndr :: ExtEnv FastString
918 eq_ifBndr :: ExtEnv IfaceBndr
919 eq_ifTvBndr :: ExtEnv IfaceTvBndr
920 eq_ifIdBndr :: ExtEnv IfaceIdBndr
922 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
924 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
925 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
926 eq_ifBndr _ _ _ _ = NotEqual
928 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
929 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
931 eq_ifLetBndr env (IfLetBndr v1 t1 i1) (IfLetBndr v2 t2 i2) k
932 = eq_ifType env t1 t2 &&& eqIfIdInfo i1 i2 &&& k (extendEqEnv env v1 v2)
934 eq_ifBndrs :: ExtEnv [IfaceBndr]
935 eq_ifLetBndrs :: ExtEnv [IfaceLetBndr]
936 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
937 eq_ifNakedBndrs :: ExtEnv [FastString]
938 eq_ifBndrs = eq_bndrs_with eq_ifBndr
939 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
940 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
941 eq_ifLetBndrs = eq_bndrs_with eq_ifLetBndr
943 eq_bndrs_with eq env [] [] k = k env
944 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
945 eq_bndrs_with eq env _ _ _ = NotEqual
949 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
950 eqListBy eq [] [] = Equal
951 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
952 eqListBy eq xs ys = NotEqual
954 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
955 eqMaybeBy eq Nothing Nothing = Equal
956 eqMaybeBy eq (Just x) (Just y) = eq x y
957 eqMaybeBy eq x y = NotEqual