2 % (c) The GRASP Project, Glasgow University, 1992-2002
6 TcRnIf, TcRn, TcM, RnM, IfM, IfL, IfG, -- The monad is opaque outside this module
9 -- The environment types
11 TcGblEnv(..), TcLclEnv(..),
12 IfGblEnv(..), IfLclEnv(..),
15 EntityUsage, emptyUsages, ErrCtxt,
16 ImportAvails(..), emptyImportAvails, plusImportAvails,
17 plusAvail, pruneAvails,
18 AvailEnv, emptyAvailEnv, unitAvailEnv, plusAvailEnv,
19 mkAvailEnv, lookupAvailEnv, lookupAvailEnv_maybe, availEnvElts, addAvail,
20 WhereFrom(..), mkModDeps,
23 TcTyThing(..), GadtRefinement,
26 ThStage(..), topStage, topSpliceStage,
27 ThLevel, impLevel, topLevel,
30 ArrowCtxt(..), topArrowCtxt, ProcLevel, topProcLevel,
33 Inst(..), InstOrigin(..), InstLoc(..), pprInstLoc,
34 instLocSrcLoc, instLocSrcSpan,
35 LIE, emptyLIE, unitLIE, plusLIE, consLIE,
36 plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
39 TcId, TcIdSet, TcDictBinds
42 #include "HsVersions.h"
44 import HsSyn ( PendingSplice, HsOverLit, LRuleDecl, LForeignDecl,
45 ArithSeqInfo, DictBinds, LHsBinds )
46 import HscTypes ( FixityEnv,
47 HscEnv, TypeEnv, TyThing,
48 GenAvailInfo(..), AvailInfo,
49 availName, IsBootInterface, Deprecations )
50 import Packages ( PackageName )
51 import Type ( Type, TvSubstEnv )
52 import TcType ( TcTyVarSet, TcType, TcTauType, TcThetaType, SkolemInfo,
53 TcPredType, TcKind, tcCmpPred, tcCmpType, tcCmpTypes )
54 import InstEnv ( DFunId, InstEnv )
56 import RdrName ( GlobalRdrEnv, LocalRdrEnv )
59 import NameSet ( NameSet, emptyNameSet, DefUses )
60 import OccName ( OccEnv )
61 import Var ( Id, TyVar )
62 import VarEnv ( TidyEnv )
64 import SrcLoc ( SrcSpan, SrcLoc, srcSpanStart )
65 import VarSet ( IdSet )
66 import ErrUtils ( Messages, Message )
67 import UniqSupply ( UniqSupply )
68 import BasicTypes ( IPName )
69 import Util ( thenCmp )
72 import Maybe ( mapMaybe )
73 import ListSetOps ( unionLists )
77 %************************************************************************
79 Standard monad definition for TcRn
80 All the combinators for the monad can be found in TcRnMonad
82 %************************************************************************
84 The monad itself has to be defined here, because it is mentioned by ErrCtxt
87 type TcRef a = IORef a
88 type TcId = Id -- Type may be a TcType
90 type TcDictBinds = DictBinds TcId -- Bag of dictionary bindings
94 type TcRnIf a b c = IOEnv (Env a b) c
95 type IfM lcl a = TcRnIf IfGblEnv lcl a -- Iface stuff
96 type IfG a = IfM () a -- Top level
97 type IfL a = IfM IfLclEnv a -- Nested
98 type TcRn a = TcRnIf TcGblEnv TcLclEnv a
99 type RnM a = TcRn a -- Historical
100 type TcM a = TcRn a -- Historical
104 %************************************************************************
106 The main environment types
108 %************************************************************************
111 data Env gbl lcl -- Changes as we move into an expression
113 env_top :: HscEnv, -- Top-level stuff that never changes
114 -- Includes all info about imported things
116 env_us :: TcRef UniqSupply, -- Unique supply for local varibles
118 env_gbl :: gbl, -- Info about things defined at the top level
119 -- of the module being compiled
121 env_lcl :: lcl -- Nested stuff; changes as we go into
125 -- TcGblEnv describes the top-level of the module at the
126 -- point at which the typechecker is finished work.
127 -- It is this structure that is handed on to the desugarer
131 tcg_mod :: Module, -- Module being compiled
132 tcg_rdr_env :: GlobalRdrEnv, -- Top level envt; used during renaming
133 tcg_default :: Maybe [Type], -- Types used for defaulting
134 -- Nothing => no 'default' decl
136 tcg_fix_env :: FixityEnv, -- Just for things in this module
138 tcg_type_env :: TypeEnv, -- Global type env for the module we are compiling now
139 -- All TyCons and Classes (for this module) end up in here right away,
140 -- along with their derived constructors, selectors.
142 -- (Ids defined in this module start in the local envt,
143 -- though they move to the global envt during zonking)
145 tcg_type_env_var :: TcRef TypeEnv,
146 -- Used only to initialise the interface-file
147 -- typechecker in initIfaceTcRn, so that it can see stuff
148 -- bound in this module when dealing with hi-boot recursions
149 -- Updated at intervals (e.g. after dealing with types and classes)
151 tcg_inst_env :: InstEnv, -- Instance envt for *home-package* modules
152 -- Includes the dfuns in tcg_insts
153 tcg_inst_uses :: TcRef NameSet, -- Home-package Dfuns actually used
154 -- Used to generate version dependencies
155 -- This records usages, rather like tcg_dus, but it has to
156 -- be a mutable variable so it can be augmented
157 -- when we look up an instance. These uses of dfuns are
158 -- rather like the free variables of the program, but
159 -- are implicit instead of explicit.
161 tcg_th_used :: TcRef Bool, -- True <=> Template Haskell syntax used
162 -- We need this so that we can generate a dependency on the
163 -- Template Haskell package, becuase the desugarer is going to
164 -- emit loads of references to TH symbols. It's rather like
165 -- tcg_inst_uses; the reference is implicit rather than explicit,
166 -- so we have to zap a mutable variable.
168 -- Now a bunch of things about this module that are simply
169 -- accumulated, but never consulted until the end.
170 -- Nevertheless, it's convenient to accumulate them along
171 -- with the rest of the info from this module.
172 tcg_exports :: NameSet, -- What is exported
173 tcg_imports :: ImportAvails, -- Information about what was imported
174 -- from where, including things bound
177 tcg_dus :: DefUses, -- What is defined in this module and what is used.
178 -- The latter is used to generate
179 -- (a) version tracking; no need to recompile if these
180 -- things have not changed version stamp
181 -- (b) unused-import info
183 tcg_keep :: NameSet, -- Set of names to keep alive, and to expose in the
184 -- interface file (but not to export to the user).
185 -- These are typically extra definitions generated from
186 -- data type declarations which would otherwise be
187 -- dropped as dead code.
189 -- The next fields accumulate the payload of the module
190 -- The binds, rules and foreign-decl fiels are collected
191 -- initially in un-zonked form and are finally zonked in tcRnSrcDecls
192 tcg_binds :: LHsBinds Id, -- Value bindings in this module
193 tcg_deprecs :: Deprecations, -- ...Deprecations
194 tcg_insts :: [DFunId], -- ...Instances
195 tcg_rules :: [LRuleDecl Id], -- ...Rules
196 tcg_fords :: [LForeignDecl Id] -- ...Foreign import & exports
200 %************************************************************************
202 The interface environments
203 Used when dealing with IfaceDecls
205 %************************************************************************
210 -- The type environment for the module being compiled,
211 -- in case the interface refers back to it via a reference that
212 -- was originally a hi-boot file.
213 -- We need the module name so we can test when it's appropriate
214 -- to look in this env.
215 if_rec_types :: Maybe (Module, IfG TypeEnv)
216 -- Allows a read effect, so it can be in a mutable
217 -- variable; c.f. handling the external package type env
218 -- Nothing => interactive stuff, no loops possible
223 -- The module for the current IfaceDecl
224 -- So if we see f = \x -> x
225 -- it means M.f = \x -> x, where M is the if_mod
226 if_mod :: ModuleName,
228 if_tv_env :: OccEnv TyVar, -- Nested tyvar bindings
229 if_id_env :: OccEnv Id -- Nested id binding
234 %************************************************************************
236 The local typechecker environment
238 %************************************************************************
240 The Global-Env/Local-Env story
241 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242 During type checking, we keep in the tcg_type_env
243 * All types and classes
244 * All Ids derived from types and classes (constructors, selectors)
246 At the end of type checking, we zonk the local bindings,
247 and as we do so we add to the tcg_type_env
248 * Locally defined top-level Ids
250 Why? Because they are now Ids not TcIds. This final GlobalEnv is
251 a) fed back (via the knot) to typechecking the
252 unfoldings of interface signatures
253 b) used in the ModDetails of this module
256 data TcLclEnv -- Changes as we move inside an expression
257 -- Discarded after typecheck/rename; not passed on to desugarer
259 tcl_loc :: SrcSpan, -- Source span
260 tcl_ctxt :: ErrCtxt, -- Error context
261 tcl_errs :: TcRef Messages, -- Place to accumulate errors
263 tcl_th_ctxt :: ThStage, -- Template Haskell context
264 tcl_arrow_ctxt :: ArrowCtxt, -- Arrow-notation context
266 tcl_rdr :: LocalRdrEnv, -- Local name envt
267 -- Maintained during renaming, of course, but also during
268 -- type checking, solely so that when renaming a Template-Haskell
269 -- splice we have the right environment for the renamer.
271 -- Does *not* include global name envt; may shadow it
272 -- Includes both ordinary variables and type variables;
273 -- they are kept distinct because tyvar have a different
274 -- occurrence contructor (Name.TvOcc)
275 -- We still need the unsullied global name env so that
276 -- we can look up record field names
278 tcl_env :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars
279 -- defined in this module
281 tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars"
282 -- Namely, the in-scope TyVars bound in tcl_env,
283 -- plus the tyvars mentioned in the types of Ids bound in tcl_lenv
284 -- Why mutable? see notes with tcGetGlobalTyVars
286 tcl_lie :: TcRef LIE, -- Place to accumulate type constraints
287 tcl_gadt :: GadtRefinement -- The current type refinement for GADTs
289 -----------------------------------------------------------
290 -- Not yet; it's a new complication and I want to see whether it bites
291 -- tcl_given :: [Inst] -- Insts available in the current context (see Note [Given Insts])
292 -----------------------------------------------------------
295 type GadtRefinement = TvSubstEnv -- Binds rigid type variables to their refinements
297 {- Note [Given Insts]
299 Because of GADTs, we have to pass inwards the Insts provided by type signatures
300 and existential contexts. Consider
301 data T a where { T1 :: b -> b -> T [b] }
302 f :: Eq a => T a -> Bool
303 f (T1 x y) = [x]==[y]
305 The constructor T1 binds an existential variable 'b', and we need Eq [b].
306 Well, we have it, because Eq a refines to Eq [b], but we can only spot that if we
311 ---------------------------
312 -- Template Haskell levels
313 ---------------------------
315 type ThLevel = Int -- Always >= 0
318 = Comp -- Ordinary compiling, at level topLevel
319 | Splice ThLevel -- Inside a splice
320 | Brack ThLevel -- Inside brackets;
321 (TcRef [PendingSplice]) -- accumulate pending splices here
322 (TcRef LIE) -- and type constraints here
323 topStage, topSpliceStage :: ThStage
325 topSpliceStage = Splice (topLevel - 1) -- Stage for the body of a top-level splice
328 impLevel, topLevel :: ThLevel
329 topLevel = 1 -- Things defined at top level of this module
330 impLevel = 0 -- Imported things; they can be used inside a top level splice
334 -- g1 = $(map ...) is OK
335 -- g2 = $(f ...) is not OK; because we havn't compiled f yet
338 ---------------------------
339 -- Arrow-notation stages
340 ---------------------------
342 -- In arrow notation, a variable bound by a proc (or enclosed let/kappa)
343 -- is not in scope to the left of an arrow tail (-<). For example
345 -- proc x -> (e1 -< e2)
347 -- Here, x is not in scope in e1, but it is in scope in e2. This can get
348 -- a bit complicated:
351 -- prox y -> (proc z -> e1) -< e2
353 -- Here, x and z are in scope in e1, but y is not. Here's how we track this:
354 -- a) Assign an "proc level" to each proc, being the number of
355 -- lexically-enclosing procs + 1.
356 -- b) Assign to each local variable the proc-level of its lexically
358 -- c) Keep a list of out-of-scope procs. When moving to the left of
359 -- an arrow-tail, add the proc-level of the immediately enclosing
361 -- d) When looking up a variable, complain if its proc-level is in
364 type ProcLevel = Int -- Always >= 0
365 topProcLevel = 0 -- Not inside any proc
367 data ArrowCtxt = ArrCtxt { proc_level :: ProcLevel, -- Current level
368 proc_banned :: [ProcLevel] } -- Out of scope proc-levels
370 topArrowCtxt = ArrCtxt { proc_level = topProcLevel, proc_banned = [] }
372 ---------------------------
374 ---------------------------
377 = AGlobal TyThing -- Used only in the return type of a lookup
378 | ATcId TcId ThLevel ProcLevel -- Ids defined in this module; may not be fully zonked
379 | ATyVar TyVar -- Type variables
380 | AThing TcKind -- Used temporarily, during kind checking, for the
381 -- tycons and clases in this recursive group
383 instance Outputable TcTyThing where -- Debugging only
384 ppr (AGlobal g) = text "AGlobal" <+> ppr g
385 ppr (ATcId g tl pl) = text "ATcId" <+> ppr g <+> ppr tl <+> ppr pl
386 ppr (ATyVar t) = text "ATyVar" <+> ppr t
387 ppr (AThing k) = text "AThing" <+> ppr k
391 type ErrCtxt = [TidyEnv -> TcM (TidyEnv, Message)]
392 -- Innermost first. Monadic so that we have a chance
393 -- to deal with bound type variables just before error
394 -- message construction
398 %************************************************************************
402 %************************************************************************
404 EntityUsage tells what things are actually need in order to compile this
405 module. It is used for generating the usage-version field of the ModIface.
407 Note that we do not record version info for entities from
408 other (non-home) packages. If the package changes, GHC doesn't help.
411 type EntityUsage = NameSet
412 -- The Names are all the (a) home-package
413 -- (b) "big" (i.e. no data cons, class ops)
414 -- (c) non-locally-defined
416 -- names that have been slurped in so far.
417 -- This is used to generate the "usage" information for this module.
419 emptyUsages :: EntityUsage
420 emptyUsages = emptyNameSet
424 %************************************************************************
426 Operations over ImportAvails
428 %************************************************************************
430 ImportAvails summarises what was imported from where, irrespective
431 of whether the imported things are actually used or not
432 It is used * when processing the export list
433 * when constructing usage info for the inteface file
434 * to identify the list of directly imported modules
435 for initialisation purposes
436 * when figuring out what things are really unused
442 -- All the things that are available from the import
443 -- Its domain is all the "main" things;
444 -- i.e. *excluding* class ops and constructors
445 -- (which appear inside their parent AvailTC)
447 imp_qual :: ModuleEnv AvailEnv,
448 -- Used to figure out "module M" export specifiers
449 -- (see 1.4 Report Section 5.1.1). Ultimately, we want to find
450 -- everything that is unambiguously in scope as 'M.x'
451 -- and where plain 'x' is (perhaps ambiguously) in scope.
452 -- So the starting point is all things that are in scope as 'M.x',
453 -- which is what this field tells us.
455 -- Domain is the *module qualifier* for imports.
456 -- e.g. import List as Foo
457 -- would add a binding Foo |-> ...stuff from List...
459 -- We keep the stuff as an AvailEnv so that it's easy to
460 -- combine stuff coming from different (unqualified)
461 -- imports of the same module
463 imp_mods :: ModuleEnv (Module, Maybe Bool, SrcSpan),
464 -- Domain is all directly-imported modules
465 -- Maybe value answers the question "is the import restricted?"
466 -- Nothing => unrestricted import (e.g., "import Foo")
467 -- Just True => restricted import, at least one entity (e.g., "import Foo(x)")
468 -- Just False => fully restricted import (e.g., "import Foo ()")
470 -- A distinction is made between the first and the third in order
471 -- to more precisely emit warnings about unused imports.
473 -- We need the Module in the range because we can't get
474 -- the keys of a ModuleEnv
476 -- (a) to help construct the usage information in
477 -- the interface file; if we import everything we
478 -- need to recompile if the module version changes
479 -- (b) to specify what child modules to initialise
481 imp_dep_mods :: ModuleEnv (ModuleName, IsBootInterface),
482 -- Home-package modules needed by the module being compiled
484 -- It doesn't matter whether any of these dependencies are actually
485 -- *used* when compiling the module; they are listed if they are below
486 -- it at all. For example, suppose M imports A which imports X. Then
487 -- compiling M might not need to consult X.hi, but X is still listed
488 -- in M's dependencies.
490 imp_dep_pkgs :: [PackageName],
491 -- Packages needed by the module being compiled, whether
492 -- directly, or via other modules in this package, or via
493 -- modules imported from other packages.
495 imp_orphs :: [ModuleName]
496 -- Orphan modules below us in the import tree
499 mkModDeps :: [(ModuleName, IsBootInterface)]
500 -> ModuleEnv (ModuleName, IsBootInterface)
501 mkModDeps deps = foldl add emptyModuleEnv deps
503 add env elt@(m,_) = extendModuleEnvByName env m elt
505 emptyImportAvails :: ImportAvails
506 emptyImportAvails = ImportAvails { imp_env = emptyAvailEnv,
507 imp_qual = emptyModuleEnv,
508 imp_mods = emptyModuleEnv,
509 imp_dep_mods = emptyModuleEnv,
513 plusImportAvails :: ImportAvails -> ImportAvails -> ImportAvails
515 (ImportAvails { imp_env = env1, imp_qual = unqual1, imp_mods = mods1,
516 imp_dep_mods = dmods1, imp_dep_pkgs = dpkgs1, imp_orphs = orphs1 })
517 (ImportAvails { imp_env = env2, imp_qual = unqual2, imp_mods = mods2,
518 imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2, imp_orphs = orphs2 })
519 = ImportAvails { imp_env = env1 `plusAvailEnv` env2,
520 imp_qual = plusModuleEnv_C plusAvailEnv unqual1 unqual2,
521 imp_mods = mods1 `plusModuleEnv` mods2,
522 imp_dep_mods = plusModuleEnv_C plus_mod_dep dmods1 dmods2,
523 imp_dep_pkgs = dpkgs1 `unionLists` dpkgs2,
524 imp_orphs = orphs1 `unionLists` orphs2 }
526 plus_mod_dep (m1, boot1) (m2, boot2)
527 = WARN( not (m1 == m2), (ppr m1 <+> ppr m2) $$ (ppr boot1 <+> ppr boot2) )
528 -- Check mod-names match
529 (m1, boot1 && boot2) -- If either side can "see" a non-hi-boot interface, use that
532 %************************************************************************
534 Avails, AvailEnv, etc
536 v%************************************************************************
539 plusAvail (Avail n1) (Avail n2) = Avail n1
540 plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n2 (ns1 `unionLists` ns2)
543 plusAvail a1 a2 = pprPanic "RnEnv.plusAvail" (hsep [ppr a1,ppr a2])
546 -------------------------
547 pruneAvails :: (Name -> Bool) -- Keep if this is True
550 pruneAvails keep avails
551 = mapMaybe del avails
553 del :: AvailInfo -> Maybe AvailInfo -- Nothing => nothing left!
554 del (Avail n) | keep n = Just (Avail n)
555 | otherwise = Nothing
556 del (AvailTC n ns) | null ns' = Nothing
557 | otherwise = Just (AvailTC n ns')
562 ---------------------------------------
564 ---------------------------------------
567 type AvailEnv = NameEnv AvailInfo -- Maps a Name to the AvailInfo that contains it
569 emptyAvailEnv :: AvailEnv
570 emptyAvailEnv = emptyNameEnv
572 unitAvailEnv :: AvailInfo -> AvailEnv
573 unitAvailEnv a = unitNameEnv (availName a) a
575 plusAvailEnv :: AvailEnv -> AvailEnv -> AvailEnv
576 plusAvailEnv = plusNameEnv_C plusAvail
578 lookupAvailEnv_maybe :: AvailEnv -> Name -> Maybe AvailInfo
579 lookupAvailEnv_maybe = lookupNameEnv
581 lookupAvailEnv :: AvailEnv -> Name -> AvailInfo
582 lookupAvailEnv env n = case lookupNameEnv env n of
584 Nothing -> pprPanic "lookupAvailEnv" (ppr n)
586 availEnvElts = nameEnvElts
588 addAvail :: AvailEnv -> AvailInfo -> AvailEnv
589 addAvail avails avail = extendNameEnv_C plusAvail avails (availName avail) avail
591 mkAvailEnv :: [AvailInfo] -> AvailEnv
592 -- 'avails' may have several items with the same availName
593 -- E.g import Ix( Ix(..), index )
594 -- will give Ix(Ix,index,range) and Ix(index)
595 -- We want to combine these; addAvail does that
596 mkAvailEnv avails = foldl addAvail emptyAvailEnv avails
599 %************************************************************************
601 \subsection{Where from}
603 %************************************************************************
605 The @WhereFrom@ type controls where the renamer looks for an interface file
609 = ImportByUser IsBootInterface -- Ordinary user import (perhaps {-# SOURCE #-})
610 | ImportBySystem -- Non user import.
612 instance Outputable WhereFrom where
613 ppr (ImportByUser is_boot) | is_boot = ptext SLIT("{- SOURCE -}")
615 ppr ImportBySystem = ptext SLIT("{- SYSTEM -}")
619 %************************************************************************
621 \subsection[Inst-types]{@Inst@ types}
623 v%************************************************************************
625 An @Inst@ is either a dictionary, an instance of an overloaded
626 literal, or an instance of an overloaded value. We call the latter a
627 ``method'' even though it may not correspond to a class operation.
628 For example, we might have an instance of the @double@ function at
629 type Int, represented by
631 Method 34 doubleId [Int] origin
643 TcId -- The overloaded function
644 -- This function will be a global, local, or ClassOpId;
645 -- inside instance decls (only) it can also be an InstId!
646 -- The id needn't be completely polymorphic.
647 -- You'll probably find its name (for documentation purposes)
648 -- inside the InstOrigin
650 [TcType] -- The types to which its polymorphic tyvars
651 -- should be instantiated.
652 -- These types must saturate the Id's foralls.
654 TcThetaType -- The (types of the) dictionaries to which the function
655 -- must be applied to get the method
657 TcTauType -- The tau-type of the method
661 -- INVARIANT 1: in (Method u f tys theta tau loc)
662 -- type of (f tys dicts(from theta)) = tau
664 -- INVARIANT 2: tau must not be of form (Pred -> Tau)
665 -- Reason: two methods are considered equal if the
666 -- base Id matches, and the instantiating types
667 -- match. The TcThetaType should then match too.
668 -- This only bites in the call to tcInstClassOp in TcClassDcl.mkMethodBind
672 HsOverLit -- The literal from the occurrence site
673 -- INVARIANT: never a rebindable-syntax literal
674 -- Reason: tcSyntaxName does unification, and we
675 -- don't want to deal with that during tcSimplify
676 TcType -- The type at which the literal is used
680 @Insts@ are ordered by their class/type info, rather than by their
681 unique. This allows the context-reduction mechanism to use standard finite
682 maps to do their stuff.
685 instance Ord Inst where
688 instance Eq Inst where
689 (==) i1 i2 = case i1 `cmpInst` i2 of
693 cmpInst (Dict _ pred1 _) (Dict _ pred2 _) = pred1 `tcCmpPred` pred2
694 cmpInst (Dict _ _ _) other = LT
696 cmpInst (Method _ _ _ _ _ _) (Dict _ _ _) = GT
697 cmpInst (Method _ id1 tys1 _ _ _) (Method _ id2 tys2 _ _ _) = (id1 `compare` id2) `thenCmp` (tys1 `tcCmpTypes` tys2)
698 cmpInst (Method _ _ _ _ _ _) other = LT
700 cmpInst (LitInst _ _ _ _) (Dict _ _ _) = GT
701 cmpInst (LitInst _ _ _ _) (Method _ _ _ _ _ _) = GT
702 cmpInst (LitInst _ lit1 ty1 _) (LitInst _ lit2 ty2 _) = (lit1 `compare` lit2) `thenCmp` (ty1 `tcCmpType` ty2)
706 %************************************************************************
708 \subsection[Inst-collections]{LIE: a collection of Insts}
710 %************************************************************************
715 isEmptyLIE = isEmptyBag
717 unitLIE inst = unitBag inst
718 mkLIE insts = listToBag insts
719 plusLIE lie1 lie2 = lie1 `unionBags` lie2
720 consLIE inst lie = inst `consBag` lie
721 plusLIEs lies = unionManyBags lies
722 lieToList = bagToList
723 listToLIE = listToBag
727 %************************************************************************
729 \subsection[Inst-origin]{The @InstOrigin@ type}
731 %************************************************************************
733 The @InstOrigin@ type gives information about where a dictionary came from.
734 This is important for decent error message reporting because dictionaries
735 don't appear in the original source code. Doubtless this type will evolve...
737 It appears in TcMonad because there are a couple of error-message-generation
738 functions that deal with it.
741 data InstLoc = InstLoc InstOrigin SrcSpan ErrCtxt
743 instLocSrcLoc :: InstLoc -> SrcLoc
744 instLocSrcLoc (InstLoc _ src_span _) = srcSpanStart src_span
746 instLocSrcSpan :: InstLoc -> SrcSpan
747 instLocSrcSpan (InstLoc _ src_span _) = src_span
750 = SigOrigin SkolemInfo -- Pattern, class decl, inst decl etc;
751 -- Places that bind type variables and introduce
752 -- available constraints
754 | IPBindOrigin (IPName Name) -- Binding site of an implicit parameter
756 -------------------------------------------------------
757 -- The rest are all occurrences: Insts that are 'wanted'
758 -------------------------------------------------------
759 | OccurrenceOf Name -- Occurrence of an overloaded identifier
761 | IPOccOrigin (IPName Name) -- Occurrence of an implicit parameter
763 | LiteralOrigin HsOverLit -- Occurrence of a literal
765 | ArithSeqOrigin (ArithSeqInfo Name) -- [x..], [x..y] etc
766 | PArrSeqOrigin (ArithSeqInfo Name) -- [:x..y:] and [:x,y..z:]
768 | InstSigOrigin -- A dict occurrence arising from instantiating
769 -- a polymorphic type during a subsumption check
772 | InstScOrigin -- Typechecking superclasses of an instance declaration
773 | DerivOrigin -- Typechecking deriving
774 | DefaultOrigin -- Typechecking a default decl
775 | DoOrigin -- Arising from a do expression
776 | ProcOrigin -- Arising from a proc expression
780 pprInstLoc :: InstLoc -> SDoc
781 pprInstLoc (InstLoc (SigOrigin info) locn _)
782 = text "arising from" <+> ppr info -- I don't think this happens much, if at all
783 pprInstLoc (InstLoc orig locn _)
784 = hsep [text "arising from", pp_orig orig, text "at", ppr locn]
786 pp_orig (OccurrenceOf name) = hsep [ptext SLIT("use of"), quotes (ppr name)]
787 pp_orig (IPOccOrigin name) = hsep [ptext SLIT("use of implicit parameter"), quotes (ppr name)]
788 pp_orig (IPBindOrigin name) = hsep [ptext SLIT("binding for implicit parameter"), quotes (ppr name)]
789 pp_orig RecordUpdOrigin = ptext SLIT("a record update")
790 pp_orig (LiteralOrigin lit) = hsep [ptext SLIT("the literal"), quotes (ppr lit)]
791 pp_orig (ArithSeqOrigin seq) = hsep [ptext SLIT("the arithmetic sequence"), quotes (ppr seq)]
792 pp_orig (PArrSeqOrigin seq) = hsep [ptext SLIT("the parallel array sequence"), quotes (ppr seq)]
793 pp_orig InstSigOrigin = ptext SLIT("instantiating a type signature")
794 pp_orig InstScOrigin = ptext SLIT("the superclasses of an instance declaration")
795 pp_orig DerivOrigin = ptext SLIT("the 'deriving' clause of a data type declaration")
796 pp_orig DefaultOrigin = ptext SLIT("a 'default' declaration")
797 pp_orig DoOrigin = ptext SLIT("a do statement")
798 pp_orig ProcOrigin = ptext SLIT("a proc expression")
799 pp_orig (SigOrigin info) = ppr info