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
81 -- ifCons /= IfOpenDataTyCon
82 -- for family instances
85 | IfaceSyn { ifName :: OccName, -- Type constructor
86 ifTyVars :: [IfaceTvBndr], -- Type variables
87 ifOpenSyn :: Bool, -- Is an open family?
88 ifSynRhs :: IfaceType, -- Type for an ordinary
89 -- synonym and kind for an
91 ifFamInst :: Maybe (IfaceTyCon, [IfaceType])
92 -- Just <=> instance of family
93 -- Invariant: ifOpenSyn == False
94 -- for family instances
97 | IfaceClass { ifCtxt :: IfaceContext, -- Context...
98 ifName :: OccName, -- Name of the class
99 ifTyVars :: [IfaceTvBndr], -- Type variables
100 ifFDs :: [FunDep FastString], -- Functional dependencies
101 ifATs :: [IfaceDecl], -- Associated type families
102 ifSigs :: [IfaceClassOp], -- Method signatures
103 ifRec :: RecFlag -- Is newtype/datatype associated with the class recursive?
106 | IfaceForeign { ifName :: OccName, -- Needs expanding when we move
108 ifExtName :: Maybe FastString }
110 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
111 -- Nothing => no default method
112 -- Just False => ordinary polymorphic default method
113 -- Just True => generic default method
116 = IfAbstractTyCon -- No info
117 | IfOpenDataTyCon -- Open data family
118 | IfDataTyCon [IfaceConDecl] -- data type decls
119 | IfNewTyCon IfaceConDecl -- newtype decls
121 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
122 visibleIfConDecls IfAbstractTyCon = []
123 visibleIfConDecls IfOpenDataTyCon = []
124 visibleIfConDecls (IfDataTyCon cs) = cs
125 visibleIfConDecls (IfNewTyCon c) = [c]
129 ifConOcc :: OccName, -- Constructor name
130 ifConInfix :: Bool, -- True <=> declared infix
131 ifConUnivTvs :: [IfaceTvBndr], -- Universal tyvars
132 ifConExTvs :: [IfaceTvBndr], -- Existential tyvars
133 ifConEqSpec :: [(OccName,IfaceType)], -- Equality contraints
134 ifConCtxt :: IfaceContext, -- Non-stupid context
135 ifConArgTys :: [IfaceType], -- Arg types
136 ifConFields :: [OccName], -- ...ditto... (field labels)
137 ifConStricts :: [StrictnessMark]} -- Empty (meaning all lazy),
138 -- or 1-1 corresp with arg tys
141 = IfaceInst { ifInstCls :: Name, -- See comments with
142 ifInstTys :: [Maybe IfaceTyCon], -- the defn of Instance
143 ifDFun :: Name, -- The dfun
144 ifOFlag :: OverlapFlag, -- Overlap flag
145 ifInstOrph :: Maybe OccName } -- See Note [Orphans]
146 -- There's always a separate IfaceDecl for the DFun, which gives
147 -- its IdInfo with its full type and version number.
148 -- The instance declarations taken together have a version number,
149 -- and we don't want that to wobble gratuitously
150 -- If this instance decl is *used*, we'll record a usage on the dfun;
151 -- and if the head does not change it won't be used if it wasn't before
154 = IfaceFamInst { ifFamInstFam :: Name -- Family tycon
155 , ifFamInstTys :: [Maybe IfaceTyCon] -- Rough match types
156 , ifFamInstTyCon :: IfaceTyCon -- Instance decl
161 ifRuleName :: RuleName,
162 ifActivation :: Activation,
163 ifRuleBndrs :: [IfaceBndr], -- Tyvars and term vars
164 ifRuleHead :: Name, -- Head of lhs
165 ifRuleArgs :: [IfaceExpr], -- Args of LHS
166 ifRuleRhs :: IfaceExpr,
167 ifRuleOrph :: Maybe OccName -- Just like IfaceInst
171 = NoInfo -- When writing interface file without -O
172 | HasInfo [IfaceInfoItem] -- Has info, and here it is
174 -- Here's a tricky case:
175 -- * Compile with -O module A, and B which imports A.f
176 -- * Change function f in A, and recompile without -O
177 -- * When we read in old A.hi we read in its IdInfo (as a thunk)
178 -- (In earlier GHCs we used to drop IdInfo immediately on reading,
179 -- but we do not do that now. Instead it's discarded when the
180 -- ModIface is read into the various decl pools.)
181 -- * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
182 -- and so gives a new version.
186 | HsStrictness StrictSig
187 | HsInline Activation
190 | HsWorker Name Arity -- Worker, if any see IdInfo.WorkerInfo
191 -- for why we want arity here.
192 -- NB: we need IfaceExtName (not just OccName) because the worker
193 -- can simplify to a function in another module.
194 -- NB: Specialisations and rules come in separately and are
195 -- only later attached to the Id. Partial reason: some are orphans.
197 --------------------------------
199 = IfaceLcl FastString
201 | IfaceType IfaceType
202 | IfaceTuple Boxity [IfaceExpr] -- Saturated; type arguments omitted
203 | IfaceLam IfaceBndr IfaceExpr
204 | IfaceApp IfaceExpr IfaceExpr
205 | IfaceCase IfaceExpr FastString IfaceType [IfaceAlt]
206 | IfaceLet IfaceBinding IfaceExpr
207 | IfaceNote IfaceNote IfaceExpr
208 | IfaceCast IfaceExpr IfaceCoercion
210 | IfaceFCall ForeignCall IfaceType
212 data IfaceNote = IfaceSCC CostCentre
214 | IfaceCoreNote String
216 type IfaceAlt = (IfaceConAlt, [FastString], IfaceExpr)
217 -- Note: FastString, not IfaceBndr (and same with the case binder)
218 -- We reconstruct the kind/type of the thing from the context
219 -- thus saving bulk in interface files
221 data IfaceConAlt = IfaceDefault
223 | IfaceTupleAlt Boxity
224 | IfaceLitAlt Literal
227 = IfaceNonRec IfaceLetBndr IfaceExpr
228 | IfaceRec [(IfaceLetBndr, IfaceExpr)]
230 -- IfaceLetBndr is like IfaceIdBndr, but has IdInfo too
231 -- It's used for *non-top-level* let/rec binders
232 -- See Note [IdInfo on nested let-bindings]
233 data IfaceLetBndr = IfLetBndr FastString IfaceType IfaceIdInfo
236 Note [IdInfo on nested let-bindings]
237 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
238 Occasionally we want to preserve IdInfo on nested let bindings The one
239 that came up was a NOINLINE pragma on a let-binding inside an INLINE
240 function. The user (Duncan Coutts) really wanted the NOINLINE control
241 to cross the separate compilation boundary.
243 So a IfaceLetBndr keeps a trimmed-down list of IfaceIdInfo stuff.
244 Currently we only actually retain InlinePragInfo, but in principle we could
248 Note [Orphans]: the ifInstOrph and ifRuleOrph fields
249 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250 If a module contains any "orphans", then its interface file is read
251 regardless, so that its instances are not missed.
253 Roughly speaking, an instance is an orphan if its head (after the =>)
254 mentions nothing defined in this module. Functional dependencies
255 complicate the situation though. Consider
257 module M where { class C a b | a -> b }
259 and suppose we are compiling module X:
264 instance C Int T where ...
266 This instance is an orphan, because when compiling a third module Y we
267 might get a constraint (C Int v), and we'd want to improve v to T. So
268 we must make sure X's instances are loaded, even if we do not directly
271 More precisely, an instance is an orphan iff
273 If there are no fundeps, then at least of the names in
274 the instance head is locally defined.
276 If there are fundeps, then for every fundep, at least one of the
277 names free in a *non-determined* part of the instance head is
278 defined in this module.
280 (Note that these conditions hold trivially if the class is locally
283 Note [Versioning of instances]
284 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285 Now consider versioning. If we *use* an instance decl in one compilation,
286 we'll depend on the dfun id for that instance, so we'll recompile if it changes.
287 But suppose we *don't* (currently) use an instance! We must recompile if
288 the instance is changed in such a way that it becomes important. (This would
289 only matter with overlapping instances, else the importing module wouldn't have
290 compiled before and the recompilation check is irrelevant.)
292 The is_orph field is set to (Just n) if the instance is not an orphan.
293 The 'n' is *any* of the locally-defined names mentioned anywhere in the
294 instance head. This name is used for versioning; the instance decl is
295 considered part of the defn of this 'n'.
297 I'm worried about whether this works right if we pick a name from
298 a functionally-dependent part of the instance decl. E.g.
300 module M where { class C a b | a -> b }
302 and suppose we are compiling module X:
308 instance C S T where ...
310 If we base the instance verion on T, I'm worried that changing S to S'
311 would change T's version, but not S or S'. But an importing module might
312 not depend on T, and so might not be recompiled even though the new instance
313 (C S' T) might be relevant. I have not been able to make a concrete example,
314 and it seems deeply obscure, so I'm going to leave it for now.
317 Note [Versioning of rules]
318 ~~~~~~~~~~~~~~~~~~~~~~~~~~
319 A rule that is not an orphan has an ifRuleOrph field of (Just n), where
320 n appears on the LHS of the rule; any change in the rule changes the version of n.
324 -- -----------------------------------------------------------------------------
327 ifaceDeclSubBndrs :: IfaceDecl -> [OccName]
328 -- *Excludes* the 'main' name, but *includes* the implicitly-bound names
329 -- Deeply revolting, because it has to predict what gets bound,
330 -- especially the question of whether there's a wrapper for a datacon
332 ifaceDeclSubBndrs (IfaceClass {ifCtxt = sc_ctxt, ifName = cls_occ,
333 ifSigs = sigs, ifATs = ats })
335 [tc_occ, dc_occ, dcww_occ] ++
336 [op | IfaceClassOp op _ _ <- sigs] ++
337 [ifName at | at <- ats ] ++
338 [mkSuperDictSelOcc n cls_occ | n <- [1..n_ctxt]]
340 n_ctxt = length sc_ctxt
342 tc_occ = mkClassTyConOcc cls_occ
343 dc_occ = mkClassDataConOcc cls_occ
344 co_occs | is_newtype = [mkNewTyCoOcc tc_occ]
346 dcww_occ -- | is_newtype = mkDataConWrapperOcc dc_occ -- Newtypes have wrapper but no worker
347 | otherwise = mkDataConWorkerOcc dc_occ -- Otherwise worker but no wrapper
348 is_newtype = n_sigs + n_ctxt == 1 -- Sigh
350 ifaceDeclSubBndrs IfaceData {ifCons = IfAbstractTyCon}
353 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
354 ifCons = IfNewTyCon (
355 IfCon { ifConOcc = con_occ,
358 ifFamInst = famInst})
359 = fields ++ [con_occ, mkDataConWorkerOcc con_occ, mkNewTyCoOcc tc_occ]
360 ++ famInstCo famInst tc_occ
362 ifaceDeclSubBndrs (IfaceData {ifName = tc_occ,
363 ifCons = IfDataTyCon cons,
364 ifFamInst = famInst})
365 = nub (concatMap ifConFields cons) -- Eliminate duplicate fields
366 ++ concatMap dc_occs cons
367 ++ famInstCo famInst tc_occ
370 | has_wrapper = [con_occ, work_occ, wrap_occ]
371 | otherwise = [con_occ, work_occ]
373 con_occ = ifConOcc con_decl
374 strs = ifConStricts con_decl
375 wrap_occ = mkDataConWrapperOcc con_occ
376 work_occ = mkDataConWorkerOcc con_occ
377 has_wrapper = any isMarkedStrict strs -- See MkId.mkDataConIds (sigh)
378 || not (null . ifConEqSpec $ con_decl)
380 -- ToDo: may miss strictness in existential dicts
382 ifaceDeclSubBndrs _other = []
384 -- coercion for data/newtype family instances
385 famInstCo Nothing baseOcc = []
386 famInstCo (Just _) baseOcc = [mkInstTyCoOcc baseOcc]
388 ----------------------------- Printing IfaceDecl ------------------------------
390 instance Outputable IfaceDecl where
393 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
394 = sep [ ppr var <+> dcolon <+> ppr ty,
397 pprIfaceDecl (IfaceForeign {ifName = tycon})
398 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
400 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
401 ifOpenSyn = False, ifSynRhs = mono_ty,
402 ifFamInst = mbFamInst})
403 = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
404 4 (vcat [equals <+> ppr mono_ty, pprFamily mbFamInst])
406 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars,
407 ifOpenSyn = True, ifSynRhs = mono_ty})
408 = hang (ptext SLIT("type family") <+> pprIfaceDeclHead [] tycon tyvars)
409 4 (dcolon <+> ppr mono_ty)
411 pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen, ifCtxt = context,
412 ifTyVars = tyvars, ifCons = condecls,
413 ifRec = isrec, ifFamInst = mbFamInst})
414 = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
415 4 (vcat [pprRec isrec, pprGen gen, pp_condecls tycon condecls,
416 pprFamily mbFamInst])
418 pp_nd = case condecls of
419 IfAbstractTyCon -> ptext SLIT("data")
420 IfOpenDataTyCon -> ptext SLIT("data family")
421 IfDataTyCon _ -> ptext SLIT("data")
422 IfNewTyCon _ -> ptext SLIT("newtype")
424 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
425 ifFDs = fds, ifATs = ats, ifSigs = sigs,
427 = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
428 4 (vcat [pprRec isrec,
432 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
433 pprGen True = ptext SLIT("Generics: yes")
434 pprGen False = ptext SLIT("Generics: no")
436 pprFamily Nothing = ptext SLIT("FamilyInstance: none")
437 pprFamily (Just famInst) = ptext SLIT("FamilyInstance:") <+> ppr famInst
439 instance Outputable IfaceClassOp where
440 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
442 pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
443 pprIfaceDeclHead context thing tyvars
444 = hsep [pprIfaceContext context, parenSymOcc thing (ppr thing),
445 pprIfaceTvBndrs tyvars]
447 pp_condecls tc IfAbstractTyCon = ptext SLIT("{- abstract -}")
448 pp_condecls tc (IfNewTyCon c) = equals <+> pprIfaceConDecl tc c
449 pp_condecls tc IfOpenDataTyCon = empty
450 pp_condecls tc (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
451 (map (pprIfaceConDecl tc) cs))
453 pprIfaceConDecl :: OccName -> IfaceConDecl -> SDoc
455 (IfCon { ifConOcc = name, ifConInfix = is_infix,
456 ifConUnivTvs = univ_tvs, ifConExTvs = ex_tvs,
457 ifConEqSpec = eq_spec, ifConCtxt = ctxt, ifConArgTys = arg_tys,
458 ifConStricts = strs, ifConFields = fields })
460 if is_infix then ptext SLIT("Infix") else empty,
461 if null strs then empty
462 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
463 if null fields then empty
464 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
466 main_payload = ppr name <+> dcolon <+>
467 pprIfaceForAllPart (univ_tvs ++ ex_tvs) (eq_ctxt ++ ctxt) pp_tau
469 eq_ctxt = [(IfaceEqPred (IfaceTyVar (occNameFS tv)) ty)
470 | (tv,ty) <- eq_spec]
472 -- A bit gruesome this, but we can't form the full con_tau, and ppr it,
473 -- because we don't have a Name for the tycon, only an OccName
474 pp_tau = case map pprParendIfaceType arg_tys ++ [pp_res_ty] of
475 (t:ts) -> fsep (t : map (arrow <+>) ts)
476 [] -> panic "pp_con_taus"
478 pp_res_ty = ppr tc <+> fsep [ppr tv | (tv,_) <- univ_tvs]
480 instance Outputable IfaceRule where
481 ppr (IfaceRule { ifRuleName = name, ifActivation = act, ifRuleBndrs = bndrs,
482 ifRuleHead = fn, ifRuleArgs = args, ifRuleRhs = rhs })
483 = sep [hsep [doubleQuotes (ftext name), ppr act,
484 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
485 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
486 ptext SLIT("=") <+> ppr rhs])
489 instance Outputable IfaceInst where
490 ppr (IfaceInst {ifDFun = dfun_id, ifOFlag = flag,
491 ifInstCls = cls, ifInstTys = mb_tcs})
492 = hang (ptext SLIT("instance") <+> ppr flag
493 <+> ppr cls <+> brackets (pprWithCommas ppr_rough mb_tcs))
494 2 (equals <+> ppr dfun_id)
496 instance Outputable IfaceFamInst where
497 ppr (IfaceFamInst {ifFamInstFam = fam, ifFamInstTys = mb_tcs,
498 ifFamInstTyCon = tycon_id})
499 = hang (ptext SLIT("family instance") <+>
500 ppr fam <+> brackets (pprWithCommas ppr_rough mb_tcs))
501 2 (equals <+> ppr tycon_id)
503 ppr_rough :: Maybe IfaceTyCon -> SDoc
504 ppr_rough Nothing = dot
505 ppr_rough (Just tc) = ppr tc
509 ----------------------------- Printing IfaceExpr ------------------------------------
512 instance Outputable IfaceExpr where
513 ppr e = pprIfaceExpr noParens e
515 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
516 -- The function adds parens in context that need
517 -- an atomic value (e.g. function args)
519 pprIfaceExpr add_par (IfaceLcl v) = ppr v
520 pprIfaceExpr add_par (IfaceExt v) = ppr v
521 pprIfaceExpr add_par (IfaceLit l) = ppr l
522 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
523 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
525 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
526 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
528 pprIfaceExpr add_par e@(IfaceLam _ _)
529 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
530 pprIfaceExpr noParens body])
532 (bndrs,body) = collect [] e
533 collect bs (IfaceLam b e) = collect (b:bs) e
534 collect bs e = (reverse bs, e)
536 pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
537 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
538 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
539 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
540 pprIfaceExpr noParens rhs <+> char '}'])
542 pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
543 = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty
544 <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
545 <+> ppr bndr <+> char '{',
546 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
548 pprIfaceExpr add_par (IfaceCast expr co)
549 = sep [pprIfaceExpr parens expr,
550 nest 2 (ptext SLIT("`cast`")),
551 pprParendIfaceType co]
553 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
554 = add_par (sep [ptext SLIT("let {"),
555 nest 2 (ppr_bind (b, rhs)),
557 pprIfaceExpr noParens body])
559 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
560 = add_par (sep [ptext SLIT("letrec {"),
561 nest 2 (sep (map ppr_bind pairs)),
563 pprIfaceExpr noParens body])
565 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
567 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
568 arrow <+> pprIfaceExpr noParens rhs]
570 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
571 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
573 ppr_bind (IfLetBndr b ty info, rhs)
574 = sep [hang (ppr b <+> dcolon <+> ppr ty) 2 (ppr info),
575 equals <+> pprIfaceExpr noParens rhs]
578 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
579 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
582 instance Outputable IfaceNote where
583 ppr (IfaceSCC cc) = pprCostCentreCore cc
584 ppr IfaceInlineMe = ptext SLIT("__inline_me")
585 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
588 instance Outputable IfaceConAlt where
589 ppr IfaceDefault = text "DEFAULT"
590 ppr (IfaceLitAlt l) = ppr l
591 ppr (IfaceDataAlt d) = ppr d
592 ppr (IfaceTupleAlt b) = panic "ppr IfaceConAlt"
593 -- IfaceTupleAlt is handled by the case-alternative printer
596 instance Outputable IfaceIdInfo where
598 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr is) <+> ptext SLIT("-}")
600 instance Outputable IfaceInfoItem where
601 ppr (HsUnfold unf) = ptext SLIT("Unfolding:") <+>
602 parens (pprIfaceExpr noParens unf)
603 ppr (HsInline act) = ptext SLIT("Inline:") <+> ppr act
604 ppr (HsArity arity) = ptext SLIT("Arity:") <+> int arity
605 ppr (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
606 ppr HsNoCafRefs = ptext SLIT("HasNoCafRefs")
607 ppr (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
611 %************************************************************************
613 Equality, for interface file version generaion only
615 %************************************************************************
617 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new
618 constructor is EqBut, which gives the set of things whose version must
619 be equal for the whole thing to be equal. So the key function is
620 eqIfExt, which compares Names.
622 Of course, equality is also done modulo alpha conversion.
626 = Equal -- Definitely exactly the same
627 | NotEqual -- Definitely different
628 | EqBut a -- The same provided these Names have not changed
630 type IfaceEq = GenIfaceEq NameSet
632 instance Outputable IfaceEq where
633 ppr Equal = ptext SLIT("Equal")
634 ppr NotEqual = ptext SLIT("NotEqual")
635 ppr (EqBut occset) = ptext SLIT("EqBut") <+> ppr (nameSetToList occset)
637 bool :: Bool -> IfaceEq
639 bool False = NotEqual
641 toBool :: IfaceEq -> Bool
643 toBool (EqBut _) = True
644 toBool NotEqual = False
646 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
647 zapEq (EqBut _) = Equal
650 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
652 NotEqual &&& x = NotEqual
653 EqBut nms &&& Equal = EqBut nms
654 EqBut nms &&& NotEqual = NotEqual
655 EqBut nms1 &&& EqBut nms2 = EqBut (nms1 `unionNameSets` nms2)
657 -- This function is the core of the EqBut stuff
658 -- ASSUMPTION: The left-hand argument is the NEW CODE, and hence
659 -- any Names in the left-hand arg have the correct parent in them.
660 eqIfExt :: Name -> Name -> IfaceEq
662 | name1 == name2 = EqBut (unitNameSet name1)
663 | otherwise = NotEqual
665 ---------------------
666 checkBootDecl :: IfaceDecl -- The boot decl
667 -> IfaceDecl -- The real decl
668 -> Bool -- True <=> compatible
669 checkBootDecl (IfaceId s1 t1 _) (IfaceId s2 t2 _)
670 = ASSERT( s1==s2 ) toBool (t1 `eqIfType` t2)
672 checkBootDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
673 = ASSERT (ifName d1 == ifName d2 ) ifExtName d1 == ifExtName d2
675 checkBootDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
676 = ASSERT( ifName d1 == ifName d2 )
677 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
678 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
680 checkBootDecl d1@(IfaceData {}) d2@(IfaceData {})
681 -- We don't check the recursion flags because the boot-one is
682 -- recursive, to be conservative, but the real one may not be.
683 -- I'm not happy with the way recursive flags are dealt with.
684 = ASSERT( ifName d1 == ifName d2 )
685 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
686 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
688 IfAbstractTyCon -> Equal
689 cons1 -> eq_hsCD env cons1 (ifCons d2)
691 checkBootDecl d1@(IfaceClass {}) d2@(IfaceClass {})
692 = ASSERT( ifName d1 == ifName d2 )
693 toBool $ eqWith (ifTyVars d1) (ifTyVars d2) $ \ env ->
694 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
695 case (ifCtxt d1, ifSigs d1) of
697 (cxt1, sigs1) -> eq_ifContext env cxt1 (ifCtxt d2) &&&
698 eqListBy (eq_cls_sig env) sigs1 (ifSigs d2)
700 checkBootDecl _ _ = False -- default case
702 ---------------------
703 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
704 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
705 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
707 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
708 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
710 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
711 = bool (ifName d1 == ifName d2 &&
712 ifRec d1 == ifRec d2 &&
713 ifGadtSyntax d1 == ifGadtSyntax d2 &&
714 ifGeneric d1 == ifGeneric d2) &&&
715 ifFamInst d1 `eqIfTc_fam` ifFamInst d2 &&&
716 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
717 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
718 eq_hsCD env (ifCons d1) (ifCons d2)
720 -- The type variables of the data type do not scope
721 -- over the constructors (any more), but they do scope
722 -- over the stupid context in the IfaceConDecls
724 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
725 = bool (ifName d1 == ifName d2) &&&
726 ifFamInst d1 `eqIfTc_fam` ifFamInst 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 eqIfTc_fam :: Maybe (IfaceTyCon, [IfaceType])
748 -> Maybe (IfaceTyCon, [IfaceType])
750 Nothing `eqIfTc_fam` Nothing = Equal
751 (Just (fam1, tys1)) `eqIfTc_fam` (Just (fam2, tys2)) =
752 fam1 `eqIfTc` fam2 &&& eqListBy eqIfType tys1 tys2
753 _ `eqIfTc_fam` _ = NotEqual
756 -----------------------
757 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2 && ifOFlag d1 == ifOFlag d2)
758 -- All other changes are handled via the version info on the dfun
760 eqIfFamInst d1 d2 = bool (ifFamInstTyCon d1 == ifFamInstTyCon d2)
761 -- All other changes are handled via the version info on the tycon
763 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1 o1)
764 (IfaceRule n2 a2 bs2 f2 es2 rhs2 o2)
765 = bool (n1==n2 && a1==a2 && o1 == o2) &&&
767 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
768 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
769 -- zapEq: for the LHSs, ignore the EqBut part
770 eq_ifaceExpr env rhs1 rhs2)
772 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2)
773 = eqListBy (eq_ConDecl env) c1 c2
775 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
776 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
777 eq_hsCD env IfOpenDataTyCon IfOpenDataTyCon = Equal
778 eq_hsCD env d1 d2 = NotEqual
781 = bool (ifConOcc c1 == ifConOcc c2 &&
782 ifConInfix c1 == ifConInfix c2 &&
783 ifConStricts c1 == ifConStricts c2 &&
784 ifConFields c1 == ifConFields c2) &&&
785 eq_ifTvBndrs env (ifConUnivTvs c1) (ifConUnivTvs c2) (\ env ->
786 eq_ifTvBndrs env (ifConExTvs c1) (ifConExTvs c2) (\ env ->
787 eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
788 eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)))
790 eq_hsFD env (ns1,ms1) (ns2,ms2)
791 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
793 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
794 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
800 eqIfIdInfo NoInfo NoInfo = Equal
801 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
802 eqIfIdInfo i1 i2 = NotEqual
804 eq_item (HsInline a1) (HsInline a2) = bool (a1 == a2)
805 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
806 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
807 eq_item (HsUnfold u1) (HsUnfold u2) = eq_ifaceExpr emptyEqEnv u1 u2
808 eq_item HsNoCafRefs HsNoCafRefs = Equal
809 eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
810 eq_item _ _ = NotEqual
813 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
814 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
815 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
816 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
817 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
818 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
819 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
820 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
821 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
822 eq_ifaceExpr env (IfaceCast e1 co1) (IfaceCast e2 co2) = eq_ifaceExpr env e1 e2 &&& eq_ifType env co1 co2
823 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
825 eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
826 = eq_ifaceExpr env s1 s2 &&&
827 eq_ifType env ty1 ty2 &&&
828 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
830 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
831 = bool (eq_ifaceConAlt c1 c2) &&&
832 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
834 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
835 = eq_ifaceExpr env r1 r2 &&& eq_ifLetBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
837 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
838 = eq_ifLetBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
840 (bs1,rs1) = unzip as1
841 (bs2,rs2) = unzip as2
844 eq_ifaceExpr env _ _ = NotEqual
847 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
848 eq_ifaceConAlt IfaceDefault IfaceDefault = True
849 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
850 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
851 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
852 eq_ifaceConAlt _ _ = False
855 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
856 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
857 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
858 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
859 eq_ifaceNote env _ _ = NotEqual
863 ---------------------
864 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
867 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
868 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
869 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
870 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
871 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
872 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
873 eq_ifType env _ _ = NotEqual
876 eq_ifTypes env = eqListBy (eq_ifType env)
879 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
882 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
883 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
884 eq_ifPredType env _ _ = NotEqual
887 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
888 eqIfTc IfaceIntTc IfaceIntTc = Equal
889 eqIfTc IfaceCharTc IfaceCharTc = Equal
890 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
891 eqIfTc IfaceListTc IfaceListTc = Equal
892 eqIfTc IfacePArrTc IfacePArrTc = Equal
893 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
894 eqIfTc IfaceLiftedTypeKindTc IfaceLiftedTypeKindTc = Equal
895 eqIfTc IfaceOpenTypeKindTc IfaceOpenTypeKindTc = Equal
896 eqIfTc IfaceUnliftedTypeKindTc IfaceUnliftedTypeKindTc = Equal
897 eqIfTc IfaceUbxTupleKindTc IfaceUbxTupleKindTc = Equal
898 eqIfTc IfaceArgTypeKindTc IfaceArgTypeKindTc = Equal
899 eqIfTc _ _ = NotEqual
902 -----------------------------------------------------------
903 Support code for equality checking
904 -----------------------------------------------------------
907 ------------------------------------
908 type EqEnv = UniqFM FastString -- Tracks the mapping from L-variables to R-variables
910 eqIfOcc :: EqEnv -> FastString -> FastString -> IfaceEq
911 eqIfOcc env n1 n2 = case lookupUFM env n1 of
912 Just n1 -> bool (n1 == n2)
913 Nothing -> bool (n1 == n2)
915 extendEqEnv :: EqEnv -> FastString -> FastString -> EqEnv
916 extendEqEnv env n1 n2 | n1 == n2 = env
917 | otherwise = addToUFM env n1 n2
920 emptyEqEnv = emptyUFM
922 ------------------------------------
923 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
925 eq_ifNakedBndr :: ExtEnv FastString
926 eq_ifBndr :: ExtEnv IfaceBndr
927 eq_ifTvBndr :: ExtEnv IfaceTvBndr
928 eq_ifIdBndr :: ExtEnv IfaceIdBndr
930 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
932 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
933 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
934 eq_ifBndr _ _ _ _ = NotEqual
936 eq_ifTvBndr env (v1, k1) (v2, k2) k = eq_ifType env k1 k2 &&& k (extendEqEnv env v1 v2)
937 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
939 eq_ifLetBndr env (IfLetBndr v1 t1 i1) (IfLetBndr v2 t2 i2) k
940 = eq_ifType env t1 t2 &&& eqIfIdInfo i1 i2 &&& k (extendEqEnv env v1 v2)
942 eq_ifBndrs :: ExtEnv [IfaceBndr]
943 eq_ifLetBndrs :: ExtEnv [IfaceLetBndr]
944 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
945 eq_ifNakedBndrs :: ExtEnv [FastString]
946 eq_ifBndrs = eq_bndrs_with eq_ifBndr
947 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
948 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
949 eq_ifLetBndrs = eq_bndrs_with eq_ifLetBndr
951 eq_bndrs_with eq env [] [] k = k env
952 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
953 eq_bndrs_with eq env _ _ _ = NotEqual
957 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
958 eqListBy eq [] [] = Equal
959 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
960 eqListBy eq xs ys = NotEqual
962 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
963 eqMaybeBy eq Nothing Nothing = Equal
964 eqMaybeBy eq (Just x) (Just y) = eq x y
965 eqMaybeBy eq x y = NotEqual