X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcRnTypes.lhs;h=ca17355ebafcc0077c65d8bcfaeed590da5c08d2;hp=c8d75509a3909714877e5f10de159d823e15d469;hb=af2e0d24abe49e06fdee4a95530af8a5c33da4a3;hpb=7bb3d1fc79521d591cd9f824893963141a7997b6 diff --git a/compiler/typecheck/TcRnTypes.lhs b/compiler/typecheck/TcRnTypes.lhs index c8d7550..ca17355 100644 --- a/compiler/typecheck/TcRnTypes.lhs +++ b/compiler/typecheck/TcRnTypes.lhs @@ -18,32 +18,44 @@ module TcRnTypes( WhereFrom(..), mkModDeps, -- Typechecker types - TcTyThing(..), pprTcTyThingCategory, RefinementVisibility(..), + TcTypeEnv, TcTyThing(..), pprTcTyThingCategory, -- Template Haskell ThStage(..), topStage, topAnnStage, topSpliceStage, - ThLevel, impLevel, topLevel, + ThLevel, impLevel, outerLevel, thLevel, -- Arrows ArrowCtxt(NoArrowCtxt), newArrowScope, escapeArrowScope, - -- Insts - Inst(..), EqInstCo, InstOrigin(..), InstLoc(..), - pprInstLoc, pprInstArising, instLocSpan, instLocOrigin, setInstLoc, - LIE, emptyLIE, unitLIE, plusLIE, consLIE, instLoc, instSpan, - plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE, + -- Constraints + Untouchables, + WantedConstraints, emptyWanteds, andWanteds, extendWanteds, + WantedConstraint(..), WantedEvVar(..), wantedEvVarLoc, + wantedEvVarToVar, wantedEvVarPred, splitWanteds, + + evVarsToWanteds, + Implication(..), + CtLoc(..), ctLocSpan, ctLocOrigin, setCtLocOrigin, + CtOrigin(..), EqOrigin(..), + WantedLoc, GivenLoc, + + SkolemInfo(..), + + -- Pretty printing + pprEvVarTheta, pprWantedsWithLocs, pprWantedWithLoc, + pprEvVars, pprEvVarWithType, + pprArising, pprArisingAt, -- Misc other types - TcId, TcIdSet, TcDictBinds, TcTyVarBind(..), TcTyVarBinds + TcId, TcIdSet, TcTyVarBind(..), TcTyVarBinds ) where #include "HsVersions.h" -import HsSyn hiding (LIE) +import HsSyn import HscTypes import Type -import Coercion import TcType import Annotations import InstEnv @@ -56,20 +68,18 @@ import NameSet import Var import VarEnv import Module -import LazyUniqFM +import UniqFM import SrcLoc import VarSet import ErrUtils import UniqSupply import BasicTypes -import Util import Bag import Outputable import ListSetOps import FastString +import StaticFlags( opt_ErrorSpans ) -import Data.Maybe -import Data.List import Data.Set (Set) \end{code} @@ -85,9 +95,9 @@ The monad itself has to be defined here, because it is mentioned by ErrCtxt \begin{code} type TcRef a = IORef a -type TcId = Id -- Type may be a TcType +type TcId = Id -- Type may be a TcType DV: WHAT?????????? type TcIdSet = IdSet -type TcDictBinds = DictBinds TcId -- Bag of dictionary bindings + type TcRnIf a b c = IOEnv (Env a b) c type IfM lcl a = TcRnIf IfGblEnv lcl a -- Iface stuff @@ -224,14 +234,8 @@ data TcGblEnv -- reference is implicit rather than explicit, so we have to zap a -- mutable variable. - tcg_dfun_n :: TcRef Int, - -- ^ Allows us to number off the names of DFuns. - -- - -- It's convenient to allocate an External Name for a DFun, with - -- a permanently-fixed unique, just like other top-level functions - -- defined in this module. But that means we need a canonical - -- occurrence name, distinct from all other dfuns in this module, - -- and this name supply serves that purpose (df1, df2, etc). + tcg_dfun_n :: TcRef OccSet, + -- ^ Allows us to choose unique DFun names. -- The next fields accumulate the payload of the module -- The binds, rules and foreign-decl fiels are collected @@ -241,11 +245,16 @@ data TcGblEnv tcg_rn_imports :: [LImportDecl Name], -- Keep the renamed imports regardless. They are not -- voluminous and are needed if you want to report unused imports + tcg_used_rdrnames :: TcRef (Set RdrName), + -- The set of used *imported* (not locally-defined) RdrNames + -- Used only to report unused import declarations + tcg_rn_decls :: Maybe (HsGroup Name), -- ^ Renamed decls, maybe. @Nothing@ <=> Don't retain renamed -- decls. + tcg_ev_binds :: Bag EvBind, -- Top-level evidence bindings tcg_binds :: LHsBinds Id, -- Value bindings in this module tcg_warns :: Warnings, -- ...Warnings and deprecations tcg_anns :: [Annotation], -- ...Annotations @@ -254,10 +263,13 @@ data TcGblEnv tcg_rules :: [LRuleDecl Id], -- ...Rules tcg_fords :: [LForeignDecl Id], -- ...Foreign import & exports - tcg_doc :: Maybe (HsDoc Name), -- ^ Maybe Haddock documentation - tcg_hmi :: HaddockModInfo Name, -- ^ Haddock module information - tcg_hpc :: AnyHpcUsage -- ^ @True@ if any part of the prog uses hpc - -- instrumentation. + tcg_doc_hdr :: Maybe LHsDocString, -- ^ Maybe Haddock header docs + tcg_hpc :: AnyHpcUsage, -- ^ @True@ if any part of the + -- prog uses hpc instrumentation. + + tcg_main :: Maybe Name -- ^ The Name of the main + -- function, if this module is + -- the main module. } data RecFieldEnv @@ -341,7 +353,7 @@ data TcLclEnv -- Changes as we move inside an expression -- Discarded after typecheck/rename; not passed on to desugarer = TcLclEnv { tcl_loc :: SrcSpan, -- Source span - tcl_ctxt :: ErrCtxt, -- Error context + tcl_ctxt :: [ErrCtxt], -- Error context, innermost on top tcl_errs :: TcRef Messages, -- Place to accumulate errors tcl_th_ctxt :: ThStage, -- Template Haskell context @@ -359,8 +371,8 @@ data TcLclEnv -- Changes as we move inside an expression -- We still need the unsullied global name env so that -- we can look up record field names - tcl_env :: NameEnv TcTyThing, -- The local type environment: Ids and - -- TyVars defined in this module + tcl_env :: TcTypeEnv, -- The local type environment: Ids and + -- TyVars defined in this module tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars" -- Namely, the in-scope TyVars bound in tcl_env, @@ -368,13 +380,12 @@ data TcLclEnv -- Changes as we move inside an expression -- in tcl_lenv. -- Why mutable? see notes with tcGetGlobalTyVars - tcl_lie :: TcRef LIE, -- Place to accumulate type constraints - - tcl_tybinds :: TcRef TcTyVarBinds -- Meta and coercion type variable - -- bindings accumulated during - -- constraint solving + tcl_lie :: TcRef WantedConstraints, -- Place to accumulate type constraints + tcl_untch :: Untouchables -- Untouchables } +type TcTypeEnv = NameEnv TcTyThing + {- Note [Given Insts] ~~~~~~~~~~~~~~~~~~ @@ -391,37 +402,55 @@ pass it inwards. -} --------------------------- --- Template Haskell levels +-- Template Haskell stages and levels --------------------------- +data ThStage -- See Note [Template Haskell state diagram] in TcSplice + = Splice -- Top-level splicing + -- This code will be run *at compile time*; + -- the result replaces the splice + -- Binding level = 0 + + | Comp -- Ordinary Haskell code + -- Binding level = 1 + + | Brack -- Inside brackets + ThStage -- Binding level = level(stage) + 1 + (TcRef [PendingSplice]) -- Accumulate pending splices here + (TcRef WantedConstraints) -- and type constraints here + +topStage, topAnnStage, topSpliceStage :: ThStage +topStage = Comp +topAnnStage = Splice +topSpliceStage = Splice + +instance Outputable ThStage where + ppr Splice = text "Splice" + ppr Comp = text "Comp" + ppr (Brack s _ _) = text "Brack" <> parens (ppr s) + type ThLevel = Int - -- Indicates how many levels of brackets we are inside - -- (always >= 0) + -- See Note [Template Haskell levels] in TcSplice -- Incremented when going inside a bracket, -- decremented when going inside a splice -- NB: ThLevel is one greater than the 'n' in Fig 2 of the -- original "Template meta-programming for Haskell" paper -impLevel, topLevel :: ThLevel -topLevel = 1 -- Things defined at top level of this module +impLevel, outerLevel :: ThLevel impLevel = 0 -- Imported things; they can be used inside a top level splice +outerLevel = 1 -- Things defined outside brackets +-- NB: Things at level 0 are not *necessarily* imported. +-- eg $( \b -> ... ) here b is bound at level 0 -- -- For example: -- f = ... -- g1 = $(map ...) is OK -- g2 = $(f ...) is not OK; because we havn't compiled f yet - -data ThStage - = Comp ThLevel -- Ordinary compiling, usually at level topLevel but annotations use a lower level - | Splice ThLevel -- Inside a splice - | Brack ThLevel -- Inside brackets; - (TcRef [PendingSplice]) -- accumulate pending splices here - (TcRef LIE) -- and type constraints here -topStage, topAnnStage, topSpliceStage :: ThStage -topStage = Comp topLevel -topAnnStage = Comp (topLevel - 1) -topSpliceStage = Splice (topLevel - 1) -- Stage for the body of a top-level splice +thLevel :: ThStage -> ThLevel +thLevel Splice = 0 +thLevel Comp = 1 +thLevel (Brack s _ _) = thLevel s + 1 --------------------------- -- Arrow-notation context @@ -471,41 +500,23 @@ data TcTyThing = AGlobal TyThing -- Used only in the return type of a lookup | ATcId { -- Ids defined in this module; may not be fully zonked - tct_id :: TcId, - tct_co :: RefinementVisibility, -- Previously: Maybe HsWrapper - -- Nothing <=> Do not apply a GADT type refinement - -- I am wobbly, or have no free - -- type variables - -- Just co <=> Apply any type refinement to me, - -- and record it in the coercion - tct_type :: TcType, -- Type of (coercion applied to id) + tct_id :: TcId, tct_level :: ThLevel } | ATyVar Name TcType -- The type to which the lexically scoped type vaiable -- is currently refined. We only need the Name - -- for error-message purposes + -- for error-message purposes; it is the corresponding + -- Name in the domain of the envt | AThing TcKind -- Used temporarily, during kind checking, for the -- tycons and clases in this recursive group -data RefinementVisibility - = Unrefineable -- Do not apply a GADT refinement - -- I have no free variables - - | Rigid HsWrapper -- Apply any refinement to me - -- and record it in the coercion - - | Wobbly -- Do not apply a GADT refinement - -- I am wobbly - - | WobblyInvisible -- Wobbly type, not available inside current - -- GADT refinement - instance Outputable TcTyThing where -- Debugging only ppr (AGlobal g) = pprTyThing g ppr elt@(ATcId {}) = text "Identifier" <> - ifPprDebug (brackets (ppr (tct_id elt) <> dcolon <> ppr (tct_type elt) <> comma - <+> ppr (tct_level elt) <+> ppr (tct_co elt))) + brackets (ppr (tct_id elt) <> dcolon + <> ppr (varType (tct_id elt)) <> comma + <+> ppr (tct_level elt)) ppr (ATyVar tv _) = text "Type variable" <+> quotes (ppr tv) ppr (AThing k) = text "AThing" <+> ppr k @@ -514,20 +525,16 @@ pprTcTyThingCategory (AGlobal thing) = pprTyThingCategory thing pprTcTyThingCategory (ATyVar {}) = ptext (sLit "Type variable") pprTcTyThingCategory (ATcId {}) = ptext (sLit "Local identifier") pprTcTyThingCategory (AThing {}) = ptext (sLit "Kinded thing") - -instance Outputable RefinementVisibility where - ppr Unrefineable = ptext (sLit "unrefineable") - ppr (Rigid co) = ptext (sLit "rigid") <+> ppr co - ppr Wobbly = ptext (sLit "wobbly") - ppr WobblyInvisible = ptext (sLit "wobbly-invisible") - \end{code} \begin{code} -type ErrCtxt = [TidyEnv -> TcM (TidyEnv, Message)] - -- Innermost first. Monadic so that we have a chance - -- to deal with bound type variables just before error - -- message construction +type ErrCtxt = (Bool, TidyEnv -> TcM (TidyEnv, Message)) + -- Monadic so that we have a chance + -- to deal with bound type variables just before error + -- message construction + + -- Bool: True <=> this is a landmark context; do not + -- discard it when trimming for display \end{code} @@ -658,269 +665,206 @@ instance Outputable WhereFrom where %************************************************************************ %* * -\subsection[Inst-types]{@Inst@ types} + Wanted constraints + + These are forced to be in TcRnTypes because + TcLclEnv mentions WantedConstraints + WantedConstraint mentions CtLoc + CtLoc mentions ErrCtxt + ErrCtxt mentions TcM %* * v%************************************************************************ -An @Inst@ is either a dictionary, an instance of an overloaded -literal, or an instance of an overloaded value. We call the latter a -``method'' even though it may not correspond to a class operation. -For example, we might have an instance of the @double@ function at -type Int, represented by +\begin{code} +type Untouchables = TcTyVarSet -- All MetaTyVars - Method 34 doubleId [Int] origin +type WantedConstraints = Bag WantedConstraint -In addition to the basic Haskell variants of 'Inst's, they can now also -represent implication constraints 'forall tvs. given => wanted' -and equality constraints 'co :: ty1 ~ ty2'. +data WantedConstraint + = WcEvVar WantedEvVar + | WcImplic Implication + -- ToDo: add literals, methods -NB: Equalities occur in two flavours: +-- EvVar defined in module Var.lhs: +-- Evidence variables include all *quantifiable* constraints +-- dictionaries +-- implicit parameters +-- coercion variables - (1) Dict {tci_pred = EqPred ty1 ty2} - (2) EqInst {tci_left = ty1, tci_right = ty2, tci_co = coe} +data WantedEvVar -- The sort of constraint over which one can lambda-abstract + = WantedEvVar + EvVar -- The variable itself; make a binding for it please + WantedLoc -- How the constraint arose in the first place + -- (used for error messages only) -The former arises from equalities in contexts, whereas the latter is used -whenever the type checker introduces an equality (e.g., during deferring -unification). +type WantedLoc = CtLoc CtOrigin +type GivenLoc = CtLoc SkolemInfo -I am not convinced that this duplication is necessary or useful! -=chak +data Implication + = Implic { + ic_env_tvs :: Untouchables, -- Untouchables: unification variables + -- free in the environment + ic_env :: TcTypeEnv, -- The type environment + -- Used only when generating error messages + -- Generally, ic_env_tvs = tvsof(ic_env) + -- However, we don't zonk ic_env when zonking the Implication + -- Instead we do that when generating a skolem-escape error message -\begin{code} -data Inst - = Dict { - tci_name :: Name, - tci_pred :: TcPredType, -- Class or implicit parameter only - tci_loc :: InstLoc - } + ic_skols :: TcTyVarSet, -- Introduced skolems + -- See Note [Skolems in an implication] - | ImplicInst { -- An implication constraint - -- forall tvs. given => wanted - tci_name :: Name, - tci_tyvars :: [TcTyVar], -- Quantified type variables - tci_given :: [Inst], -- Only Dicts and EqInsts - -- (no Methods, LitInsts, ImplicInsts) - tci_wanted :: [Inst], -- Only Dicts, EqInst, and ImplicInsts - -- (no Methods or LitInsts) + ic_scoped :: [TcTyVar], -- List of scoped variables to be unified + -- bijectively to a subset of ic_tyvars + -- Note [Scoped pattern variable] - tci_loc :: InstLoc - } - -- NB: the tci_given are not necessarily rigid + ic_given :: [EvVar], -- Given evidence variables + -- (order does not matter) - | Method { - tci_id :: TcId, -- The Id for the Inst + ic_wanted :: WantedConstraints, -- Wanted constraints - tci_oid :: TcId, -- The overloaded function - -- This function will be a global, local, or ClassOpId; - -- inside instance decls (only) it can also be an InstId! - -- The id needn't be completely polymorphic. - -- You'll probably find its name (for documentation purposes) - -- inside the InstOrigin + ic_binds :: EvBindsVar, -- Points to the place to fill in the + -- abstraction and bindings - tci_tys :: [TcType], -- The types to which its polymorphic tyvars - -- should be instantiated. - -- These types must saturate the Id's foralls. + ic_loc :: GivenLoc } - tci_theta :: TcThetaType, - -- The (types of the) dictionaries to which the function - -- must be applied to get the method +evVarsToWanteds :: WantedLoc -> [EvVar] -> WantedConstraints +evVarsToWanteds loc evs = listToBag [WcEvVar (WantedEvVar ev loc) | ev <- evs] - tci_loc :: InstLoc - } - -- INVARIANT 1: in (Method m f tys theta tau loc) - -- type of m = type of (f tys dicts(from theta)) - - -- INVARIANT 2: type of m must not be of form (Pred -> Tau) - -- Reason: two methods are considered equal if the - -- base Id matches, and the instantiating types - -- match. The TcThetaType should then match too. - -- This only bites in the call to tcInstClassOp in TcClassDcl.mkMethodBind - - | LitInst { - tci_name :: Name, - tci_lit :: HsOverLit Name, -- The literal from the occurrence site - -- INVARIANT: never a rebindable-syntax literal - -- Reason: tcSyntaxName does unification, and we - -- don't want to deal with that during tcSimplify, - -- when resolving LitInsts - - tci_ty :: TcType, -- The type at which the literal is used - tci_loc :: InstLoc - } - - | EqInst { -- delayed unification of the form - -- co :: ty1 ~ ty2 - tci_left :: TcType, -- ty1 -- both types are... - tci_right :: TcType, -- ty2 -- ...free of boxes - tci_co :: EqInstCo, -- co - tci_loc :: InstLoc, - - tci_name :: Name -- Debugging help only: this makes it easier to - -- follow where a constraint is used in a morass - -- of trace messages! Unlike other Insts, it - -- has no semantic significance whatsoever. - } +wantedEvVarLoc :: WantedEvVar -> WantedLoc +wantedEvVarLoc (WantedEvVar _ loc) = loc -type EqInstCo = Either -- Distinguish between given and wanted coercions - TcTyVar -- - a wanted equation, with a hole, to be filled - -- with a witness for the equality; for equation - -- arising from deferring unification, 'ty1' is - -- the actual and 'ty2' the expected type - Coercion -- - a given equation, with a coercion witnessing - -- the equality; a coercion that originates - -- from a signature or a GADT is a CoVar, but - -- after normalisation of coercions, they can - -- be arbitrary Coercions involving constructors - -- and pseudo-constructors like sym and trans. -\end{code} +wantedEvVarToVar :: WantedEvVar -> EvVar +wantedEvVarToVar (WantedEvVar ev _) = ev -@Insts@ are ordered by their class/type info, rather than by their -unique. This allows the context-reduction mechanism to use standard finite -maps to do their stuff. It's horrible that this code is here, rather -than with the Avails handling stuff in TcSimplify +wantedEvVarPred :: WantedEvVar -> PredType +wantedEvVarPred (WantedEvVar ev _) = evVarPred ev -\begin{code} -instance Ord Inst where - compare = cmpInst - -- Used *only* for AvailEnv in TcSimplify - -instance Eq Inst where - (==) i1 i2 = case i1 `cmpInst` i2 of - EQ -> True - _ -> False - -cmpInst :: Inst -> Inst -> Ordering -cmpInst d1@(Dict {}) d2@(Dict {}) = tci_pred d1 `tcCmpPred` tci_pred d2 -cmpInst (Dict {}) _ = LT - -cmpInst (Method {}) (Dict {}) = GT -cmpInst m1@(Method {}) m2@(Method {}) = (tci_oid m1 `compare` tci_oid m2) `thenCmp` - (tci_tys m1 `tcCmpTypes` tci_tys m2) -cmpInst (Method {}) _ = LT - -cmpInst (LitInst {}) (Dict {}) = GT -cmpInst (LitInst {}) (Method {}) = GT -cmpInst l1@(LitInst {}) l2@(LitInst {}) = (tci_lit l1 `compare` tci_lit l2) `thenCmp` - (tci_ty l1 `tcCmpType` tci_ty l2) -cmpInst (LitInst {}) _ = LT - - -- Implication constraints are compared by *name* - -- not by type; that is, we make no attempt to do CSE on them -cmpInst (ImplicInst {}) (Dict {}) = GT -cmpInst (ImplicInst {}) (Method {}) = GT -cmpInst (ImplicInst {}) (LitInst {}) = GT -cmpInst i1@(ImplicInst {}) i2@(ImplicInst {}) = tci_name i1 `compare` tci_name i2 -cmpInst (ImplicInst {}) _ = LT - - -- same for Equality constraints -cmpInst (EqInst {}) (Dict {}) = GT -cmpInst (EqInst {}) (Method {}) = GT -cmpInst (EqInst {}) (LitInst {}) = GT -cmpInst (EqInst {}) (ImplicInst {}) = GT -cmpInst i1@(EqInst {}) i2@(EqInst {}) = (tci_left i1 `tcCmpType` tci_left i2) `thenCmp` - (tci_right i1 `tcCmpType` tci_right i2) +splitWanteds :: WantedConstraints -> (Bag WantedEvVar, Bag Implication) +splitWanteds wanted = partitionBagWith pick wanted + where + pick (WcEvVar v) = Left v + pick (WcImplic i) = Right i \end{code} +Note [Skolems in an implication] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The skolems in an implication are not there to perform a skolem escape +check. That happens because all the environment variables are in the +untouchables, and therefore cannot be unified with anything at all, +let alone the skolems. -%************************************************************************ -%* * -\subsection[Inst-collections]{LIE: a collection of Insts} -%* * -%************************************************************************ - -\begin{code} --- FIXME: Rename this. It clashes with (Located (IE ...)) -type LIE = Bag Inst - -isEmptyLIE :: LIE -> Bool -isEmptyLIE = isEmptyBag - -emptyLIE :: LIE -emptyLIE = emptyBag - -unitLIE :: Inst -> LIE -unitLIE inst = unitBag inst +Instead, ic_skols is used only when considering floating a constraint +outside the implication in TcSimplify.floatEqualities or +TcSimplify.approximateImplications -mkLIE :: [Inst] -> LIE -mkLIE insts = listToBag insts +Note [Scoped pattern variables] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + data T where K :: forall a,b. a -> b -> T -plusLIE :: LIE -> LIE -> LIE -plusLIE lie1 lie2 = lie1 `unionBags` lie2 + ...(case x of K (p::c) (q::d) -> ...)... -plusLIEs :: [LIE] -> LIE -plusLIEs lies = unionManyBags lies +We create fresh MetaTvs for c,d, and later check that they are +bound bijectively to the skolems we created for a,b. So the +implication constraint looks like + ic_skols = {a',b'} -- Skolem tvs created from a,b + ic_scoped = {c',d'} -- Meta tvs created from c,d -lieToList :: LIE -> [Inst] -lieToList = bagToList +\begin{code} +emptyWanteds :: WantedConstraints +emptyWanteds = emptyBag -listToLIE :: [Inst] -> LIE -listToLIE = listToBag +andWanteds :: WantedConstraints -> WantedConstraints -> WantedConstraints +andWanteds = unionBags -consLIE :: Inst -> LIE -> LIE -consLIE inst lie = lie `snocBag` inst --- Putting the new Inst at the *end* of the bag is a half-hearted attempt --- to ensure that we tend to report the *leftmost* type-constraint error --- E.g. f :: [a] --- f = [1,2,3] --- we'd like to complain about the '1', not the '3'. --- --- "Half-hearted" because the rest of the type checker makes no great --- claims for retaining order in the constraint set. Still, this --- seems to improve matters slightly. Exampes: mdofail001, tcfail015 +extendWanteds :: WantedConstraints -> WantedConstraint -> WantedConstraints +extendWanteds = snocBag +\end{code} + +\begin{code} +pprEvVars :: [EvVar] -> SDoc -- Print with their types +pprEvVars ev_vars = vcat (map pprEvVarWithType ev_vars) + +pprEvVarTheta :: [EvVar] -> SDoc +pprEvVarTheta ev_vars = pprTheta (map evVarPred ev_vars) + +pprEvVarWithType :: EvVar -> SDoc +pprEvVarWithType v = ppr v <+> dcolon <+> pprPred (evVarPred v) + +pprWantedsWithLocs :: Bag WantedConstraint -> SDoc +pprWantedsWithLocs = foldrBag (($$) . pprWantedWithLoc) empty + +pprWantedWithLoc :: WantedConstraint -> SDoc +pprWantedWithLoc (WcImplic i) = ppr i +pprWantedWithLoc (WcEvVar v) = pprWantedEvVarWithLoc v + +instance Outputable WantedConstraint where + ppr (WcEvVar v) = ppr v + ppr (WcImplic i) = ppr i + +-- Adding -ferror-spans makes the output more voluminous +instance Outputable WantedEvVar where + ppr wev | opt_ErrorSpans = pprWantedEvVarWithLoc wev + | otherwise = pprWantedEvVar wev + +pprWantedEvVarWithLoc, pprWantedEvVar :: WantedEvVar -> SDoc +pprWantedEvVarWithLoc (WantedEvVar v loc) = hang (pprEvVarWithType v) + 2 (pprArisingAt loc) +pprWantedEvVar (WantedEvVar v _) = pprEvVarWithType v + +instance Outputable Implication where + ppr (Implic { ic_env_tvs = env_tvs, ic_skols = skols, ic_given = given + , ic_wanted = wanted, ic_binds = binds, ic_loc = loc }) + = ptext (sLit "Implic") <+> braces + (sep [ ptext (sLit "Untouchables = ") <+> ppr env_tvs + , ptext (sLit "Skolems = ") <+> ppr skols + , ptext (sLit "Given = ") <+> pprEvVars given + , ptext (sLit "Wanted = ") <+> ppr wanted + , ptext (sLit "Binds = ") <+> ppr binds + , pprSkolInfo (ctLocOrigin loc) + , ppr (ctLocSpan loc) ]) \end{code} - %************************************************************************ %* * -\subsection[Inst-origin]{The @InstOrigin@ type} + CtLoc, CtOrigin %* * %************************************************************************ -The @InstOrigin@ type gives information about where a dictionary came from. -This is important for decent error message reporting because dictionaries -don't appear in the original source code. Doubtless this type will evolve... - -It appears in TcMonad because there are a couple of error-message-generation -functions that deal with it. +The 'CtLoc' and 'CtOrigin' types gives information about where a +*wanted constraint* came from. This is important for decent error +message reporting because dictionaries don't appear in the original +source code. Doubtless this type will evolve... \begin{code} ------------------------------------------- -data InstLoc = InstLoc InstOrigin SrcSpan ErrCtxt - -instLoc :: Inst -> InstLoc -instLoc inst = tci_loc inst +data CtLoc orig = CtLoc orig SrcSpan [ErrCtxt] -setInstLoc :: Inst -> InstLoc -> Inst -setInstLoc inst new_loc = inst { tci_loc = new_loc } +ctLocSpan :: CtLoc o -> SrcSpan +ctLocSpan (CtLoc _ s _) = s -instSpan :: Inst -> SrcSpan -instSpan wanted = instLocSpan (instLoc wanted) +ctLocOrigin :: CtLoc o -> o +ctLocOrigin (CtLoc o _ _) = o -instLocSpan :: InstLoc -> SrcSpan -instLocSpan (InstLoc _ s _) = s +setCtLocOrigin :: CtLoc o -> o' -> CtLoc o' +setCtLocOrigin (CtLoc _ s c) o = CtLoc o s c -instLocOrigin :: InstLoc -> InstOrigin -instLocOrigin (InstLoc o _ _) = o +pprArising :: CtOrigin -> SDoc +pprArising (TypeEqOrigin {}) = empty +pprArising orig = text "arising from" <+> ppr orig -pprInstArising :: Inst -> SDoc -pprInstArising loc = ptext (sLit "arising from") <+> pprInstLoc (tci_loc loc) - -pprInstLoc :: InstLoc -> SDoc -pprInstLoc (InstLoc orig span _) = sep [ppr orig, text "at" <+> ppr span] +pprArisingAt :: CtLoc CtOrigin -> SDoc +pprArisingAt (CtLoc o s _) = sep [pprArising o, text "at" <+> ppr s] ------------------------------------------- -data InstOrigin - = SigOrigin SkolemInfo -- Pattern, class decl, inst decl etc; - -- Places that bind type variables and introduce - -- available constraints - - | IPBindOrigin (IPName Name) -- Binding site of an implicit parameter +-- CtOrigin gives the origin of *wanted* constraints +data CtOrigin + = OccurrenceOf Name -- Occurrence of an overloaded identifier + | AppOrigin -- An application of some kind - ------------------------------------------------------- - -- The rest are all occurrences: Insts that are 'wanted' - ------------------------------------------------------- - | OccurrenceOf Name -- Occurrence of an overloaded identifier | SpecPragOrigin Name -- Specialisation pragma for identifier + | TypeEqOrigin EqOrigin + | IPOccOrigin (IPName Name) -- Occurrence of an implicit parameter | LiteralOrigin (HsOverLit Name) -- Occurrence of a literal @@ -928,53 +872,55 @@ data InstOrigin | ArithSeqOrigin (ArithSeqInfo Name) -- [x..], [x..y] etc | PArrSeqOrigin (ArithSeqInfo Name) -- [:x..y:] and [:x,y..z:] + | SectionOrigin | TupleOrigin -- (..,..) - - | InstSigOrigin -- A dict occurrence arising from instantiating - -- a polymorphic type during a subsumption check - | ExprSigOrigin -- e :: ty + | PatSigOrigin -- p :: ty + | PatOrigin -- Instantiating a polytyped pattern at a constructor | RecordUpdOrigin | ViewPatOrigin - | InstScOrigin -- Typechecking superclasses of an instance declaration - - | NoScOrigin -- A very special hack; see TcSimplify, - -- Note [Recursive instances and superclases] - - + | ScOrigin -- Typechecking superclasses of an instance declaration | DerivOrigin -- Typechecking deriving | StandAloneDerivOrigin -- Typechecking stand-alone deriving | DefaultOrigin -- Typechecking a default decl | DoOrigin -- Arising from a do expression | ProcOrigin -- Arising from a proc expression - | ImplicOrigin SDoc -- An implication constraint - | EqOrigin -- A type equality | AnnOrigin -- An annotation -instance Outputable InstOrigin where - ppr (OccurrenceOf name) = hsep [ptext (sLit "a use of"), quotes (ppr name)] - ppr (SpecPragOrigin name) = hsep [ptext (sLit "a specialisation pragma for"), quotes (ppr name)] - ppr (IPOccOrigin name) = hsep [ptext (sLit "a use of implicit parameter"), quotes (ppr name)] - ppr (IPBindOrigin name) = hsep [ptext (sLit "a binding for implicit parameter"), quotes (ppr name)] - ppr RecordUpdOrigin = ptext (sLit "a record update") - ppr ExprSigOrigin = ptext (sLit "an expression type signature") - ppr ViewPatOrigin = ptext (sLit "a view pattern") - ppr (LiteralOrigin lit) = hsep [ptext (sLit "the literal"), quotes (ppr lit)] - ppr (ArithSeqOrigin seq) = hsep [ptext (sLit "the arithmetic sequence"), quotes (ppr seq)] - ppr (PArrSeqOrigin seq) = hsep [ptext (sLit "the parallel array sequence"), quotes (ppr seq)] - ppr TupleOrigin = ptext (sLit "a tuple") - ppr NegateOrigin = ptext (sLit "a use of syntactic negation") - ppr InstScOrigin = ptext (sLit "the superclasses of an instance declaration") - ppr NoScOrigin = ptext (sLit "an instance declaration") - ppr DerivOrigin = ptext (sLit "the 'deriving' clause of a data type declaration") - ppr StandAloneDerivOrigin = ptext (sLit "a 'deriving' declaration") - ppr DefaultOrigin = ptext (sLit "a 'default' declaration") - ppr DoOrigin = ptext (sLit "a do statement") - ppr ProcOrigin = ptext (sLit "a proc expression") - ppr (ImplicOrigin doc) = doc - ppr (SigOrigin info) = pprSkolInfo info - ppr EqOrigin = ptext (sLit "a type equality") - ppr InstSigOrigin = panic "ppr InstSigOrigin" - ppr AnnOrigin = ptext (sLit "an annotation") +data EqOrigin + = UnifyOrigin + { uo_actual :: TcType + , uo_expected :: TcType } + +instance Outputable CtOrigin where + ppr orig = pprO orig + +pprO :: CtOrigin -> SDoc +pprO (OccurrenceOf name) = hsep [ptext (sLit "a use of"), quotes (ppr name)] +pprO AppOrigin = ptext (sLit "an application") +pprO (SpecPragOrigin name) = hsep [ptext (sLit "a specialisation pragma for"), quotes (ppr name)] +pprO (IPOccOrigin name) = hsep [ptext (sLit "a use of implicit parameter"), quotes (ppr name)] +pprO RecordUpdOrigin = ptext (sLit "a record update") +pprO ExprSigOrigin = ptext (sLit "an expression type signature") +pprO PatSigOrigin = ptext (sLit "a pattern type signature") +pprO PatOrigin = ptext (sLit "a pattern") +pprO ViewPatOrigin = ptext (sLit "a view pattern") +pprO (LiteralOrigin lit) = hsep [ptext (sLit "the literal"), quotes (ppr lit)] +pprO (ArithSeqOrigin seq) = hsep [ptext (sLit "the arithmetic sequence"), quotes (ppr seq)] +pprO (PArrSeqOrigin seq) = hsep [ptext (sLit "the parallel array sequence"), quotes (ppr seq)] +pprO SectionOrigin = ptext (sLit "an operator section") +pprO TupleOrigin = ptext (sLit "a tuple") +pprO NegateOrigin = ptext (sLit "a use of syntactic negation") +pprO ScOrigin = ptext (sLit "the superclasses of an instance declaration") +pprO DerivOrigin = ptext (sLit "the 'deriving' clause of a data type declaration") +pprO StandAloneDerivOrigin = ptext (sLit "a 'deriving' declaration") +pprO DefaultOrigin = ptext (sLit "a 'default' declaration") +pprO DoOrigin = ptext (sLit "a do statement") +pprO ProcOrigin = ptext (sLit "a proc expression") +pprO (TypeEqOrigin eq) = ptext (sLit "an equality") <+> ppr eq +pprO AnnOrigin = ptext (sLit "an annotation") + +instance Outputable EqOrigin where + ppr (UnifyOrigin t1 t2) = ppr t1 <+> char '~' <+> ppr t2 \end{code}