2 % (c) The University of Glasgow 2006
3 % (c) The GRASP Project, Glasgow University, 1992-2002
7 TcRnIf, TcRn, TcM, RnM, IfM, IfL, IfG, -- The monad is opaque outside this module
10 -- The environment types
12 TcGblEnv(..), TcLclEnv(..),
13 IfGblEnv(..), IfLclEnv(..),
16 ErrCtxt, RecFieldEnv(..),
17 ImportAvails(..), emptyImportAvails, plusImportAvails,
18 WhereFrom(..), mkModDeps,
21 TcTypeEnv, TcTyThing(..), pprTcTyThingCategory,
24 ThStage(..), topStage, topAnnStage, topSpliceStage,
25 ThLevel, impLevel, outerLevel, thLevel,
28 ArrowCtxt(NoArrowCtxt), newArrowScope, escapeArrowScope,
31 Untouchables(..), inTouchableRange, isNoUntouchables,
32 WantedConstraints, emptyWanteds, andWanteds, extendWanteds,
33 WantedConstraint(..), WantedEvVar(..), wantedEvVarLoc,
34 wantedEvVarToVar, wantedEvVarPred, splitWanteds,
38 CtLoc(..), ctLocSpan, ctLocOrigin, setCtLocOrigin,
39 CtOrigin(..), EqOrigin(..),
40 WantedLoc, GivenLoc, pushErrCtxt,
45 pprEvVarTheta, pprWantedsWithLocs, pprWantedWithLoc,
46 pprEvVars, pprEvVarWithType,
47 pprArising, pprArisingAt,
50 TcId, TcIdSet, TcTyVarBind(..), TcTyVarBinds
54 #include "HsVersions.h"
82 import StaticFlags( opt_ErrorSpans )
88 %************************************************************************
90 Standard monad definition for TcRn
91 All the combinators for the monad can be found in TcRnMonad
93 %************************************************************************
95 The monad itself has to be defined here, because it is mentioned by ErrCtxt
98 type TcRef a = IORef a
99 type TcId = Id -- Type may be a TcType DV: WHAT??????????
103 type TcRnIf a b c = IOEnv (Env a b) c
104 type IfM lcl a = TcRnIf IfGblEnv lcl a -- Iface stuff
106 type IfG a = IfM () a -- Top level
107 type IfL a = IfM IfLclEnv a -- Nested
108 type TcRn a = TcRnIf TcGblEnv TcLclEnv a
109 type RnM a = TcRn a -- Historical
110 type TcM a = TcRn a -- Historical
113 Representation of type bindings to uninstantiated meta variables used during
117 data TcTyVarBind = TcTyVarBind TcTyVar TcType
119 type TcTyVarBinds = Bag TcTyVarBind
121 instance Outputable TcTyVarBind where
122 ppr (TcTyVarBind tv ty) = ppr tv <+> text ":=" <+> ppr ty
126 %************************************************************************
128 The main environment types
130 %************************************************************************
133 data Env gbl lcl -- Changes as we move into an expression
135 env_top :: HscEnv, -- Top-level stuff that never changes
136 -- Includes all info about imported things
138 env_us :: {-# UNPACK #-} !(IORef UniqSupply),
139 -- Unique supply for local varibles
141 env_gbl :: gbl, -- Info about things defined at the top level
142 -- of the module being compiled
144 env_lcl :: lcl -- Nested stuff; changes as we go into
147 -- TcGblEnv describes the top-level of the module at the
148 -- point at which the typechecker is finished work.
149 -- It is this structure that is handed on to the desugarer
153 tcg_mod :: Module, -- ^ Module being compiled
154 tcg_src :: HscSource,
155 -- ^ What kind of module (regular Haskell, hs-boot, ext-core)
157 tcg_rdr_env :: GlobalRdrEnv, -- ^ Top level envt; used during renaming
158 tcg_default :: Maybe [Type],
159 -- ^ Types used for defaulting. @Nothing@ => no @default@ decl
161 tcg_fix_env :: FixityEnv, -- ^ Just for things in this module
162 tcg_field_env :: RecFieldEnv, -- ^ Just for things in this module
164 tcg_type_env :: TypeEnv,
165 -- ^ Global type env for the module we are compiling now. All
166 -- TyCons and Classes (for this module) end up in here right away,
167 -- along with their derived constructors, selectors.
169 -- (Ids defined in this module start in the local envt, though they
170 -- move to the global envt during zonking)
172 tcg_type_env_var :: TcRef TypeEnv,
173 -- Used only to initialise the interface-file
174 -- typechecker in initIfaceTcRn, so that it can see stuff
175 -- bound in this module when dealing with hi-boot recursions
176 -- Updated at intervals (e.g. after dealing with types and classes)
178 tcg_inst_env :: InstEnv,
179 -- ^ Instance envt for /home-package/ modules; Includes the dfuns in
181 tcg_fam_inst_env :: FamInstEnv, -- ^ Ditto for family instances
183 -- Now a bunch of things about this module that are simply
184 -- accumulated, but never consulted until the end.
185 -- Nevertheless, it's convenient to accumulate them along
186 -- with the rest of the info from this module.
187 tcg_exports :: [AvailInfo], -- ^ What is exported
188 tcg_imports :: ImportAvails,
189 -- ^ Information about what was imported from where, including
190 -- things bound in this module.
193 -- ^ What is defined in this module and what is used.
194 -- The latter is used to generate
196 -- (a) version tracking; no need to recompile if these things have
197 -- not changed version stamp
199 -- (b) unused-import info
201 tcg_keep :: TcRef NameSet,
202 -- ^ Locally-defined top-level names to keep alive.
204 -- "Keep alive" means give them an Exported flag, so that the
205 -- simplifier does not discard them as dead code, and so that they
206 -- are exposed in the interface file (but not to export to the
209 -- Some things, like dict-fun Ids and default-method Ids are "born"
210 -- with the Exported flag on, for exactly the above reason, but some
211 -- we only discover as we go. Specifically:
213 -- * The to/from functions for generic data types
215 -- * Top-level variables appearing free in the RHS of an orphan
218 -- * Top-level variables appearing free in a TH bracket
220 tcg_th_used :: TcRef Bool,
221 -- ^ @True@ <=> Template Haskell syntax used.
223 -- We need this so that we can generate a dependency on the
224 -- Template Haskell package, becuase the desugarer is going
225 -- to emit loads of references to TH symbols. The reference
226 -- is implicit rather than explicit, so we have to zap a
229 tcg_dfun_n :: TcRef OccSet,
230 -- ^ Allows us to choose unique DFun names.
232 -- The next fields accumulate the payload of the module
233 -- The binds, rules and foreign-decl fiels are collected
234 -- initially in un-zonked form and are finally zonked in tcRnSrcDecls
236 tcg_rn_exports :: Maybe [Located (IE Name)],
237 tcg_rn_imports :: [LImportDecl Name],
238 -- Keep the renamed imports regardless. They are not
239 -- voluminous and are needed if you want to report unused imports
241 tcg_used_rdrnames :: TcRef (Set RdrName),
242 -- The set of used *imported* (not locally-defined) RdrNames
243 -- Used only to report unused import declarations
245 tcg_rn_decls :: Maybe (HsGroup Name),
246 -- ^ Renamed decls, maybe. @Nothing@ <=> Don't retain renamed
249 tcg_ev_binds :: Bag EvBind, -- Top-level evidence bindings
250 tcg_binds :: LHsBinds Id, -- Value bindings in this module
251 tcg_sigs :: NameSet, -- ...Top-level names that *lack* a signature
252 tcg_imp_specs :: [LTcSpecPrag], -- ...SPECIALISE prags for imported Ids
253 tcg_warns :: Warnings, -- ...Warnings and deprecations
254 tcg_anns :: [Annotation], -- ...Annotations
255 tcg_insts :: [Instance], -- ...Instances
256 tcg_fam_insts :: [FamInst], -- ...Family instances
257 tcg_rules :: [LRuleDecl Id], -- ...Rules
258 tcg_fords :: [LForeignDecl Id], -- ...Foreign import & exports
260 tcg_doc_hdr :: Maybe LHsDocString, -- ^ Maybe Haddock header docs
261 tcg_hpc :: AnyHpcUsage, -- ^ @True@ if any part of the
262 -- prog uses hpc instrumentation.
264 tcg_main :: Maybe Name -- ^ The Name of the main
265 -- function, if this module is
270 = RecFields (NameEnv [Name]) -- Maps a constructor name *in this module*
271 -- to the fields for that constructor
272 NameSet -- Set of all fields declared *in this module*;
273 -- used to suppress name-shadowing complaints
274 -- when using record wild cards
275 -- E.g. let fld = e in C {..}
276 -- This is used when dealing with ".." notation in record
277 -- construction and pattern matching.
278 -- The FieldEnv deals *only* with constructors defined in *this*
279 -- module. For imported modules, we get the same info from the
283 %************************************************************************
285 The interface environments
286 Used when dealing with IfaceDecls
288 %************************************************************************
293 -- The type environment for the module being compiled,
294 -- in case the interface refers back to it via a reference that
295 -- was originally a hi-boot file.
296 -- We need the module name so we can test when it's appropriate
297 -- to look in this env.
298 if_rec_types :: Maybe (Module, IfG TypeEnv)
299 -- Allows a read effect, so it can be in a mutable
300 -- variable; c.f. handling the external package type env
301 -- Nothing => interactive stuff, no loops possible
306 -- The module for the current IfaceDecl
307 -- So if we see f = \x -> x
308 -- it means M.f = \x -> x, where M is the if_mod
311 -- The field is used only for error reporting
312 -- if (say) there's a Lint error in it
314 -- Where the interface came from:
315 -- .hi file, or GHCi state, or ext core
316 -- plus which bit is currently being examined
318 if_tv_env :: UniqFM TyVar, -- Nested tyvar bindings
319 if_id_env :: UniqFM Id -- Nested id binding
324 %************************************************************************
326 The local typechecker environment
328 %************************************************************************
330 The Global-Env/Local-Env story
331 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332 During type checking, we keep in the tcg_type_env
333 * All types and classes
334 * All Ids derived from types and classes (constructors, selectors)
336 At the end of type checking, we zonk the local bindings,
337 and as we do so we add to the tcg_type_env
338 * Locally defined top-level Ids
340 Why? Because they are now Ids not TcIds. This final GlobalEnv is
341 a) fed back (via the knot) to typechecking the
342 unfoldings of interface signatures
343 b) used in the ModDetails of this module
346 data TcLclEnv -- Changes as we move inside an expression
347 -- Discarded after typecheck/rename; not passed on to desugarer
349 tcl_loc :: SrcSpan, -- Source span
350 tcl_ctxt :: [ErrCtxt], -- Error context, innermost on top
351 tcl_errs :: TcRef Messages, -- Place to accumulate errors
353 tcl_th_ctxt :: ThStage, -- Template Haskell context
354 tcl_arrow_ctxt :: ArrowCtxt, -- Arrow-notation context
356 tcl_rdr :: LocalRdrEnv, -- Local name envt
357 -- Maintained during renaming, of course, but also during
358 -- type checking, solely so that when renaming a Template-Haskell
359 -- splice we have the right environment for the renamer.
361 -- Does *not* include global name envt; may shadow it
362 -- Includes both ordinary variables and type variables;
363 -- they are kept distinct because tyvar have a different
364 -- occurrence contructor (Name.TvOcc)
365 -- We still need the unsullied global name env so that
366 -- we can look up record field names
368 tcl_env :: TcTypeEnv, -- The local type environment: Ids and
369 -- TyVars defined in this module
371 tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars"
372 -- Namely, the in-scope TyVars bound in tcl_env,
373 -- plus the tyvars mentioned in the types of Ids bound
375 -- Why mutable? see notes with tcGetGlobalTyVars
377 tcl_lie :: TcRef WantedConstraints, -- Place to accumulate type constraints
380 tcl_meta :: TcRef Unique, -- The next free unique for TcMetaTyVars
381 -- Guaranteed to be allocated linearly
382 tcl_untch :: Unique -- Any TcMetaTyVar with
383 -- unique >= tcl_untch is touchable
384 -- unique < tcl_untch is untouchable
387 type TcTypeEnv = NameEnv TcTyThing
390 {- Note [Given Insts]
392 Because of GADTs, we have to pass inwards the Insts provided by type signatures
393 and existential contexts. Consider
394 data T a where { T1 :: b -> b -> T [b] }
395 f :: Eq a => T a -> Bool
396 f (T1 x y) = [x]==[y]
398 The constructor T1 binds an existential variable 'b', and we need Eq [b].
399 Well, we have it, because Eq a refines to Eq [b], but we can only spot that if we
404 ---------------------------
405 -- Template Haskell stages and levels
406 ---------------------------
408 data ThStage -- See Note [Template Haskell state diagram] in TcSplice
409 = Splice -- Top-level splicing
410 -- This code will be run *at compile time*;
411 -- the result replaces the splice
414 | Comp -- Ordinary Haskell code
417 | Brack -- Inside brackets
418 ThStage -- Binding level = level(stage) + 1
419 (TcRef [PendingSplice]) -- Accumulate pending splices here
420 (TcRef WantedConstraints) -- and type constraints here
422 topStage, topAnnStage, topSpliceStage :: ThStage
425 topSpliceStage = Splice
427 instance Outputable ThStage where
428 ppr Splice = text "Splice"
429 ppr Comp = text "Comp"
430 ppr (Brack s _ _) = text "Brack" <> parens (ppr s)
433 -- See Note [Template Haskell levels] in TcSplice
434 -- Incremented when going inside a bracket,
435 -- decremented when going inside a splice
436 -- NB: ThLevel is one greater than the 'n' in Fig 2 of the
437 -- original "Template meta-programming for Haskell" paper
439 impLevel, outerLevel :: ThLevel
440 impLevel = 0 -- Imported things; they can be used inside a top level splice
441 outerLevel = 1 -- Things defined outside brackets
442 -- NB: Things at level 0 are not *necessarily* imported.
443 -- eg $( \b -> ... ) here b is bound at level 0
447 -- g1 = $(map ...) is OK
448 -- g2 = $(f ...) is not OK; because we havn't compiled f yet
450 thLevel :: ThStage -> ThLevel
453 thLevel (Brack s _ _) = thLevel s + 1
455 ---------------------------
456 -- Arrow-notation context
457 ---------------------------
460 In arrow notation, a variable bound by a proc (or enclosed let/kappa)
461 is not in scope to the left of an arrow tail (-<) or the head of (|..|).
466 Here, x is not in scope in e1, but it is in scope in e2. This can get
470 proc y -> (proc z -> e1) -< e2
472 Here, x and z are in scope in e1, but y is not. We implement this by
473 recording the environment when passing a proc (using newArrowScope),
474 and returning to that (using escapeArrowScope) on the left of -< and the
480 | ArrowCtxt (Env TcGblEnv TcLclEnv)
482 -- Record the current environment (outside a proc)
483 newArrowScope :: TcM a -> TcM a
486 env { env_lcl = (env_lcl env) { tcl_arrow_ctxt = ArrowCtxt env } }
488 -- Return to the stored environment (from the enclosing proc)
489 escapeArrowScope :: TcM a -> TcM a
491 = updEnv $ \ env -> case tcl_arrow_ctxt (env_lcl env) of
493 ArrowCtxt env' -> env'
495 ---------------------------
497 ---------------------------
500 = AGlobal TyThing -- Used only in the return type of a lookup
502 | ATcId { -- Ids defined in this module; may not be fully zonked
504 tct_level :: ThLevel }
506 | ATyVar Name TcType -- The type to which the lexically scoped type vaiable
507 -- is currently refined. We only need the Name
508 -- for error-message purposes; it is the corresponding
509 -- Name in the domain of the envt
511 | AThing TcKind -- Used temporarily, during kind checking, for the
512 -- tycons and clases in this recursive group
514 instance Outputable TcTyThing where -- Debugging only
515 ppr (AGlobal g) = pprTyThing g
516 ppr elt@(ATcId {}) = text "Identifier" <>
517 brackets (ppr (tct_id elt) <> dcolon
518 <> ppr (varType (tct_id elt)) <> comma
519 <+> ppr (tct_level elt))
520 ppr (ATyVar tv _) = text "Type variable" <+> quotes (ppr tv)
521 ppr (AThing k) = text "AThing" <+> ppr k
523 pprTcTyThingCategory :: TcTyThing -> SDoc
524 pprTcTyThingCategory (AGlobal thing) = pprTyThingCategory thing
525 pprTcTyThingCategory (ATyVar {}) = ptext (sLit "Type variable")
526 pprTcTyThingCategory (ATcId {}) = ptext (sLit "Local identifier")
527 pprTcTyThingCategory (AThing {}) = ptext (sLit "Kinded thing")
531 type ErrCtxt = (Bool, TidyEnv -> TcM (TidyEnv, Message))
532 -- Monadic so that we have a chance
533 -- to deal with bound type variables just before error
534 -- message construction
536 -- Bool: True <=> this is a landmark context; do not
537 -- discard it when trimming for display
541 %************************************************************************
543 Operations over ImportAvails
545 %************************************************************************
548 -- | 'ImportAvails' summarises what was imported from where, irrespective of
549 -- whether the imported things are actually used or not. It is used:
551 -- * when processing the export list,
553 -- * when constructing usage info for the interface file,
555 -- * to identify the list of directly imported modules for initialisation
556 -- purposes and for optimised overlap checking of family instances,
558 -- * when figuring out what things are really unused
562 imp_mods :: ModuleEnv [(ModuleName, Bool, SrcSpan)],
563 -- ^ Domain is all directly-imported modules
564 -- The 'ModuleName' is what the module was imported as, e.g. in
572 -- - @True@ => import was @import Foo ()@
574 -- - @False@ => import was some other form
578 -- (a) to help construct the usage information in the interface
579 -- file; if we import somethign we need to recompile if the
580 -- export version changes
582 -- (b) to specify what child modules to initialise
584 -- We need a full ModuleEnv rather than a ModuleNameEnv here,
585 -- because we might be importing modules of the same name from
586 -- different packages. (currently not the case, but might be in the
589 imp_dep_mods :: ModuleNameEnv (ModuleName, IsBootInterface),
590 -- ^ Home-package modules needed by the module being compiled
592 -- It doesn't matter whether any of these dependencies
593 -- are actually /used/ when compiling the module; they
594 -- are listed if they are below it at all. For
595 -- example, suppose M imports A which imports X. Then
596 -- compiling M might not need to consult X.hi, but X
597 -- is still listed in M's dependencies.
599 imp_dep_pkgs :: [PackageId],
600 -- ^ Packages needed by the module being compiled, whether directly,
601 -- or via other modules in this package, or via modules imported
602 -- from other packages.
604 imp_orphs :: [Module],
605 -- ^ Orphan modules below us in the import tree (and maybe including
606 -- us for imported modules)
608 imp_finsts :: [Module]
609 -- ^ Family instance modules below us in the import tree (and maybe
610 -- including us for imported modules)
613 mkModDeps :: [(ModuleName, IsBootInterface)]
614 -> ModuleNameEnv (ModuleName, IsBootInterface)
615 mkModDeps deps = foldl add emptyUFM deps
617 add env elt@(m,_) = addToUFM env m elt
619 emptyImportAvails :: ImportAvails
620 emptyImportAvails = ImportAvails { imp_mods = emptyModuleEnv,
621 imp_dep_mods = emptyUFM,
626 plusImportAvails :: ImportAvails -> ImportAvails -> ImportAvails
628 (ImportAvails { imp_mods = mods1,
629 imp_dep_mods = dmods1, imp_dep_pkgs = dpkgs1,
630 imp_orphs = orphs1, imp_finsts = finsts1 })
631 (ImportAvails { imp_mods = mods2,
632 imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2,
633 imp_orphs = orphs2, imp_finsts = finsts2 })
634 = ImportAvails { imp_mods = plusModuleEnv_C (++) mods1 mods2,
635 imp_dep_mods = plusUFM_C plus_mod_dep dmods1 dmods2,
636 imp_dep_pkgs = dpkgs1 `unionLists` dpkgs2,
637 imp_orphs = orphs1 `unionLists` orphs2,
638 imp_finsts = finsts1 `unionLists` finsts2 }
640 plus_mod_dep (m1, boot1) (m2, boot2)
641 = WARN( not (m1 == m2), (ppr m1 <+> ppr m2) $$ (ppr boot1 <+> ppr boot2) )
642 -- Check mod-names match
643 (m1, boot1 && boot2) -- If either side can "see" a non-hi-boot interface, use that
646 %************************************************************************
648 \subsection{Where from}
650 %************************************************************************
652 The @WhereFrom@ type controls where the renamer looks for an interface file
656 = ImportByUser IsBootInterface -- Ordinary user import (perhaps {-# SOURCE #-})
657 | ImportBySystem -- Non user import.
659 instance Outputable WhereFrom where
660 ppr (ImportByUser is_boot) | is_boot = ptext (sLit "{- SOURCE -}")
662 ppr ImportBySystem = ptext (sLit "{- SYSTEM -}")
666 %************************************************************************
670 These are forced to be in TcRnTypes because
671 TcLclEnv mentions WantedConstraints
672 WantedConstraint mentions CtLoc
673 CtLoc mentions ErrCtxt
676 v%************************************************************************
679 data Untouchables = NoUntouchables
683 -- A TcMetaTyvar is *touchable* iff its unique u satisfies
687 instance Outputable Untouchables where
688 ppr NoUntouchables = ptext (sLit "No untouchables")
689 ppr (TouchableRange low high) = ptext (sLit "Touchable range:") <+>
690 ppr low <+> char '-' <+> ppr high
692 isNoUntouchables :: Untouchables -> Bool
693 isNoUntouchables NoUntouchables = True
694 isNoUntouchables (TouchableRange {}) = False
696 inTouchableRange :: Untouchables -> TcTyVar -> Bool
697 inTouchableRange NoUntouchables _ = True
698 inTouchableRange (TouchableRange low high) tv
699 = uniq >= low && uniq < high
703 type WantedConstraints = Bag WantedConstraint
705 data WantedConstraint
706 = WcEvVar WantedEvVar
707 | WcImplic Implication
708 -- ToDo: add literals, methods
710 -- EvVar defined in module Var.lhs:
711 -- Evidence variables include all *quantifiable* constraints
713 -- implicit parameters
714 -- coercion variables
716 data WantedEvVar -- The sort of constraint over which one can lambda-abstract
718 EvVar -- The variable itself; make a binding for it please
719 WantedLoc -- How the constraint arose in the first place
720 -- (used for error messages only)
722 type WantedLoc = CtLoc CtOrigin
723 type GivenLoc = CtLoc SkolemInfo
727 ic_untch :: Untouchables, -- Untouchables: unification variables
728 -- free in the environment
729 ic_env :: TcTypeEnv, -- The type environment
730 -- Used only when generating error messages
731 -- Generally, ic_untch is a superset of tvsof(ic_env)
732 -- However, we don't zonk ic_env when zonking the Implication
733 -- Instead we do that when generating a skolem-escape error message
735 ic_skols :: TcTyVarSet, -- Introduced skolems
736 -- See Note [Skolems in an implication]
738 ic_scoped :: [TcTyVar], -- List of scoped variables to be unified
739 -- bijectively to a subset of ic_tyvars
740 -- Note [Scoped pattern variable]
742 ic_given :: [EvVar], -- Given evidence variables
743 -- (order does not matter)
745 ic_wanted :: WantedConstraints, -- Wanted constraints
747 ic_binds :: EvBindsVar, -- Points to the place to fill in the
748 -- abstraction and bindings
752 evVarsToWanteds :: WantedLoc -> [EvVar] -> WantedConstraints
753 evVarsToWanteds loc evs = listToBag [WcEvVar (WantedEvVar ev loc) | ev <- evs]
755 wantedEvVarLoc :: WantedEvVar -> WantedLoc
756 wantedEvVarLoc (WantedEvVar _ loc) = loc
758 wantedEvVarToVar :: WantedEvVar -> EvVar
759 wantedEvVarToVar (WantedEvVar ev _) = ev
761 wantedEvVarPred :: WantedEvVar -> PredType
762 wantedEvVarPred (WantedEvVar ev _) = evVarPred ev
764 splitWanteds :: WantedConstraints -> (Bag WantedEvVar, Bag Implication)
765 splitWanteds wanted = partitionBagWith pick wanted
767 pick (WcEvVar v) = Left v
768 pick (WcImplic i) = Right i
771 Note [Skolems in an implication]
772 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
773 The skolems in an implication are not there to perform a skolem escape
774 check. That happens because all the environment variables are in the
775 untouchables, and therefore cannot be unified with anything at all,
776 let alone the skolems.
778 Instead, ic_skols is used only when considering floating a constraint
779 outside the implication in TcSimplify.floatEqualities or
780 TcSimplify.approximateImplications
782 Note [Scoped pattern variables]
783 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
784 data T where K :: forall a,b. a -> b -> T
786 ...(case x of K (p::c) (q::d) -> ...)...
788 We create fresh MetaTvs for c,d, and later check that they are
789 bound bijectively to the skolems we created for a,b. So the
790 implication constraint looks like
791 ic_skols = {a',b'} -- Skolem tvs created from a,b
792 ic_scoped = {c',d'} -- Meta tvs created from c,d
795 emptyWanteds :: WantedConstraints
796 emptyWanteds = emptyBag
798 andWanteds :: WantedConstraints -> WantedConstraints -> WantedConstraints
799 andWanteds = unionBags
801 extendWanteds :: WantedConstraints -> WantedConstraint -> WantedConstraints
802 extendWanteds = snocBag
806 pprEvVars :: [EvVar] -> SDoc -- Print with their types
807 pprEvVars ev_vars = vcat (map pprEvVarWithType ev_vars)
809 pprEvVarTheta :: [EvVar] -> SDoc
810 pprEvVarTheta ev_vars = pprTheta (map evVarPred ev_vars)
812 pprEvVarWithType :: EvVar -> SDoc
813 pprEvVarWithType v = ppr v <+> dcolon <+> pprPred (evVarPred v)
815 pprWantedsWithLocs :: Bag WantedConstraint -> SDoc
816 pprWantedsWithLocs = foldrBag (($$) . pprWantedWithLoc) empty
818 pprWantedWithLoc :: WantedConstraint -> SDoc
819 pprWantedWithLoc (WcImplic i) = ppr i
820 pprWantedWithLoc (WcEvVar v) = pprWantedEvVarWithLoc v
822 instance Outputable WantedConstraint where
823 ppr (WcEvVar v) = ppr v
824 ppr (WcImplic i) = ppr i
826 -- Adding -ferror-spans makes the output more voluminous
827 instance Outputable WantedEvVar where
828 ppr wev | opt_ErrorSpans = pprWantedEvVarWithLoc wev
829 | otherwise = pprWantedEvVar wev
831 pprWantedEvVarWithLoc, pprWantedEvVar :: WantedEvVar -> SDoc
832 pprWantedEvVarWithLoc (WantedEvVar v loc) = hang (pprEvVarWithType v)
834 pprWantedEvVar (WantedEvVar v _) = pprEvVarWithType v
836 instance Outputable Implication where
837 ppr (Implic { ic_untch = untch, ic_skols = skols, ic_given = given
838 , ic_wanted = wanted, ic_binds = binds, ic_loc = loc })
839 = ptext (sLit "Implic") <+> braces
840 (sep [ ptext (sLit "Untouchables = ") <+> ppr untch
841 , ptext (sLit "Skolems = ") <+> ppr skols
842 , ptext (sLit "Given = ") <+> pprEvVars given
843 , ptext (sLit "Wanted = ") <+> ppr wanted
844 , ptext (sLit "Binds = ") <+> ppr binds
845 , pprSkolInfo (ctLocOrigin loc)
846 , ppr (ctLocSpan loc) ])
849 %************************************************************************
853 %************************************************************************
855 The 'CtLoc' and 'CtOrigin' types gives information about where a
856 *wanted constraint* came from. This is important for decent error
857 message reporting because dictionaries don't appear in the original
858 source code. Doubtless this type will evolve...
861 -------------------------------------------
862 data CtLoc orig = CtLoc orig SrcSpan [ErrCtxt]
864 ctLocSpan :: CtLoc o -> SrcSpan
865 ctLocSpan (CtLoc _ s _) = s
867 ctLocOrigin :: CtLoc o -> o
868 ctLocOrigin (CtLoc o _ _) = o
870 setCtLocOrigin :: CtLoc o -> o' -> CtLoc o'
871 setCtLocOrigin (CtLoc _ s c) o = CtLoc o s c
873 pushErrCtxt :: orig -> ErrCtxt -> CtLoc orig -> CtLoc orig
874 pushErrCtxt o err (CtLoc _ s errs) = CtLoc o s (err:errs)
876 pprArising :: CtOrigin -> SDoc
877 -- Used for the main, top-level error message
878 -- We've done special processing for TypeEq and FunDep origins
879 pprArising (TypeEqOrigin {}) = empty
880 pprArising FunDepOrigin = empty
881 pprArising orig = text "arising from" <+> ppr orig
883 pprArisingAt :: Outputable o => CtLoc o -> SDoc
884 pprArisingAt (CtLoc o s _) = sep [ text "arising from" <+> ppr o
885 , text "at" <+> ppr s]
887 -------------------------------------------
888 -- CtOrigin gives the origin of *wanted* constraints
890 = OccurrenceOf Name -- Occurrence of an overloaded identifier
891 | AppOrigin -- An application of some kind
893 | SpecPragOrigin Name -- Specialisation pragma for identifier
895 | TypeEqOrigin EqOrigin
897 | IPOccOrigin (IPName Name) -- Occurrence of an implicit parameter
899 | LiteralOrigin (HsOverLit Name) -- Occurrence of a literal
900 | NegateOrigin -- Occurrence of syntactic negation
902 | ArithSeqOrigin (ArithSeqInfo Name) -- [x..], [x..y] etc
903 | PArrSeqOrigin (ArithSeqInfo Name) -- [:x..y:] and [:x,y..z:]
905 | TupleOrigin -- (..,..)
906 | ExprSigOrigin -- e :: ty
907 | PatSigOrigin -- p :: ty
908 | PatOrigin -- Instantiating a polytyped pattern at a constructor
912 | ScOrigin -- Typechecking superclasses of an instance declaration
913 | DerivOrigin -- Typechecking deriving
914 | StandAloneDerivOrigin -- Typechecking stand-alone deriving
915 | DefaultOrigin -- Typechecking a default decl
916 | DoOrigin -- Arising from a do expression
917 | IfOrigin -- Arising from an if statement
918 | ProcOrigin -- Arising from a proc expression
919 | AnnOrigin -- An annotation
924 { uo_actual :: TcType
925 , uo_expected :: TcType }
927 instance Outputable CtOrigin where
930 pprO :: CtOrigin -> SDoc
931 pprO (OccurrenceOf name) = hsep [ptext (sLit "a use of"), quotes (ppr name)]
932 pprO AppOrigin = ptext (sLit "an application")
933 pprO (SpecPragOrigin name) = hsep [ptext (sLit "a specialisation pragma for"), quotes (ppr name)]
934 pprO (IPOccOrigin name) = hsep [ptext (sLit "a use of implicit parameter"), quotes (ppr name)]
935 pprO RecordUpdOrigin = ptext (sLit "a record update")
936 pprO ExprSigOrigin = ptext (sLit "an expression type signature")
937 pprO PatSigOrigin = ptext (sLit "a pattern type signature")
938 pprO PatOrigin = ptext (sLit "a pattern")
939 pprO ViewPatOrigin = ptext (sLit "a view pattern")
940 pprO IfOrigin = ptext (sLit "an if statement")
941 pprO (LiteralOrigin lit) = hsep [ptext (sLit "the literal"), quotes (ppr lit)]
942 pprO (ArithSeqOrigin seq) = hsep [ptext (sLit "the arithmetic sequence"), quotes (ppr seq)]
943 pprO (PArrSeqOrigin seq) = hsep [ptext (sLit "the parallel array sequence"), quotes (ppr seq)]
944 pprO SectionOrigin = ptext (sLit "an operator section")
945 pprO TupleOrigin = ptext (sLit "a tuple")
946 pprO NegateOrigin = ptext (sLit "a use of syntactic negation")
947 pprO ScOrigin = ptext (sLit "the superclasses of an instance declaration")
948 pprO DerivOrigin = ptext (sLit "the 'deriving' clause of a data type declaration")
949 pprO StandAloneDerivOrigin = ptext (sLit "a 'deriving' declaration")
950 pprO DefaultOrigin = ptext (sLit "a 'default' declaration")
951 pprO DoOrigin = ptext (sLit "a do statement")
952 pprO ProcOrigin = ptext (sLit "a proc expression")
953 pprO (TypeEqOrigin eq) = ptext (sLit "an equality") <+> ppr eq
954 pprO AnnOrigin = ptext (sLit "an annotation")
955 pprO FunDepOrigin = ptext (sLit "a functional dependency")
957 instance Outputable EqOrigin where
958 ppr (UnifyOrigin t1 t2) = ppr t1 <+> char '~' <+> ppr t2