TcRnTypes.lhs 62.5 KB
 Dan Licata committed Oct 10, 2007 1   Gabor Greif committed Nov 21, 2012 2 % (c) The University of Glasgow 2006-2012  simonpj committed Sep 13, 2002 3 4 % (c) The GRASP Project, Glasgow University, 1992-2002 %  dterei committed Nov 01, 2011 5 6 7 8 9 10 11 12 13 14 15 16 17  Various types used during typechecking, please see TcRnMonad as well for operations on these types. You probably want to import it, instead of this module. All the monads exported here are built on top of the same IOEnv monad. The monad functions like a Reader monad in the way it passes the environment around. This is done to allow the environment to be manipulated in a stack like fashion when entering expressions... ect. For state that is global and should be returned at the end (e.g not part of the stack mechanism), you should use an TcRef (= IORef) to store them.  simonpj committed Sep 13, 2002 18 19 \begin{code} module TcRnTypes(  ian@well-typed.com committed Nov 02, 2012 20 21  TcRnIf, TcRn, TcM, RnM, IfM, IfL, IfG, -- The monad is opaque outside this module TcRef,  simonpj committed Sep 13, 2002 22   ian@well-typed.com committed Nov 02, 2012 23 24 25 26  -- The environment types Env(..), TcGblEnv(..), TcLclEnv(..), IfGblEnv(..), IfLclEnv(..),  simonpj committed Sep 13, 2002 27   ian@well-typed.com committed Nov 02, 2012 28 29 30 31  -- Ranamer types ErrCtxt, RecFieldEnv(..), ImportAvails(..), emptyImportAvails, plusImportAvails, WhereFrom(..), mkModDeps,  simonpj committed Sep 13, 2002 32   ian@well-typed.com committed Nov 02, 2012 33 34  -- Typechecker types TcTypeEnv, TcIdBinder(..), TcTyThing(..), PromotionErr(..),  Simon Peyton Jones committed Jul 10, 2012 35  pprTcTyThingCategory, pprPECategory,  simonpj committed Sep 13, 2002 36   ian@well-typed.com committed Nov 02, 2012 37 38 39  -- Template Haskell ThStage(..), topStage, topAnnStage, topSpliceStage, ThLevel, impLevel, outerLevel, thLevel,  simonpj committed Jun 24, 2003 40   ian@well-typed.com committed Nov 02, 2012 41 42  -- Arrows ArrowCtxt(NoArrowCtxt), newArrowScope, escapeArrowScope,  ross committed May 04, 2005 43   ian@well-typed.com committed Nov 02, 2012 44  -- Canonical constraints  Simon Peyton Jones committed Aug 28, 2012 45  Xi, Ct(..), Cts, emptyCts, andCts, andManyCts, dropDerivedWC,  46  singleCt, extendCts, isEmptyCts, isCTyEqCan, isCFunEqCan,  Simon Peyton Jones committed Jun 13, 2012 47  isCDictCan_Maybe, isCFunEqCan_Maybe,  ian@well-typed.com committed Nov 02, 2012 48  isCIrredEvCan, isCNonCanonical, isWantedCt, isDerivedCt,  Simon Peyton Jones committed Sep 17, 2012 49  isGivenCt, isHoleCt,  Simon Peyton Jones committed Sep 17, 2012 50  ctEvidence,  Simon Peyton Jones committed Aug 28, 2012 51  SubGoalDepth, mkNonCanonical, mkNonCanonicalCt,  ian@well-typed.com committed Nov 02, 2012 52  ctPred, ctEvPred, ctEvTerm, ctEvId,  dimitris committed Nov 16, 2011 53   simonpj@microsoft.com committed Jan 12, 2011 54  WantedConstraints(..), insolubleWC, emptyWC, isEmptyWC,  Simon Peyton Jones committed Sep 17, 2012 55  andWC, unionsWC, addFlats, addImplics, mkFlatWC, addInsols,  simonpj@microsoft.com committed Jan 12, 2011 56 57  Implication(..),  ian@well-typed.com committed Nov 02, 2012 58  CtLoc(..), ctLocSpan, ctLocEnv, ctLocOrigin,  Simon Peyton Jones committed Sep 17, 2012 59  ctLocDepth, bumpCtLocDepth,  Simon Peyton Jones committed Oct 04, 2012 60  setCtLocOrigin, setCtLocEnv,  ian@well-typed.com committed Nov 02, 2012 61  CtOrigin(..),  Simon Peyton Jones committed Sep 17, 2012 62  pushErrCtxt, pushErrCtxtSameOrigin,  simonpj@microsoft.com committed Sep 13, 2010 63   ian@well-typed.com committed Nov 02, 2012 64  SkolemInfo(..),  simonpj@microsoft.com committed Jan 12, 2011 65   Simon Peyton Jones committed Sep 17, 2012 66  CtEvidence(..),  Simon Peyton Jones committed May 07, 2012 67 68  mkGivenLoc, isWanted, isGiven,  Simon Peyton Jones committed Sep 17, 2012 69  isDerived, canSolve, canRewrite,  Simon Peyton Jones committed Aug 30, 2012 70  CtFlavour(..), ctEvFlavour, ctFlavour,  simonpj@microsoft.com committed Sep 13, 2010 71   ian@well-typed.com committed Nov 02, 2012 72  -- Pretty printing  Simon Peyton Jones committed Jan 12, 2012 73  pprEvVarTheta, pprWantedsWithLocs,  ian@well-typed.com committed Nov 02, 2012 74  pprEvVars, pprEvVarWithType,  simonpj@microsoft.com committed Jan 12, 2011 75  pprArising, pprArisingAt,  simonpj committed Sep 13, 2002 76   ian@well-typed.com committed Nov 02, 2012 77 78 79  -- Misc other types TcId, TcIdSet, TcTyVarBind(..), TcTyVarBinds  simonpj committed Sep 13, 2002 80 81 82 83  ) where #include "HsVersions.h"  simonpj@microsoft.com committed Sep 13, 2010 84 import HsSyn  Simon Marlow committed Oct 11, 2006 85 import HscTypes  Simon Peyton Jones committed May 07, 2012 86 import TcEvidence  Simon Marlow committed Oct 11, 2006 87 import Type  simonpj@microsoft.com committed Jan 12, 2011 88 import Class ( Class )  Simon Marlow committed Sep 21, 2011 89 import TyCon ( TyCon )  simonpj@microsoft.com committed Jan 12, 2011 90 import DataCon ( DataCon, dataConUserType )  Simon Marlow committed Oct 11, 2006 91 import TcType  simonpj@microsoft.com committed Oct 30, 2008 92 import Annotations  Simon Marlow committed Oct 11, 2006 93 94 import InstEnv import FamInstEnv  simonpj committed Oct 09, 2003 95 import IOEnv  Simon Marlow committed Oct 11, 2006 96 97 import RdrName import Name  simonpj committed Sep 13, 2002 98 import NameEnv  Simon Marlow committed Oct 11, 2006 99 import NameSet  Simon Marlow committed Sep 21, 2011 100 import Avail  Simon Marlow committed Oct 11, 2006 101 102 import Var import VarEnv  simonpj committed Sep 13, 2002 103 import Module  Simon Marlow committed Oct 11, 2006 104 105 106 import SrcLoc import VarSet import ErrUtils  simonpj@microsoft.com committed Oct 08, 2010 107 import UniqFM  Simon Marlow committed Oct 11, 2006 108 109 import UniqSupply import BasicTypes  simonpj committed Sep 13, 2002 110 import Bag  Ian Lynagh committed Jan 19, 2012 111 import DynFlags  simonpj committed Sep 13, 2002 112 import Outputable  Simon Marlow committed Oct 11, 2006 113 import ListSetOps  Ian Lynagh committed Mar 29, 2008 114 import FastString  Simon Marlow committed Oct 11, 2006 115   simonpj@microsoft.com committed Jul 06, 2009 116 import Data.Set (Set)  simonpj committed Sep 13, 2002 117 118 119 120 \end{code} %************************************************************************  ian@well-typed.com committed Nov 02, 2012 121 122 %* * Standard monad definition for TcRn  simonpj committed Sep 13, 2002 123  All the combinators for the monad can be found in TcRnMonad  ian@well-typed.com committed Nov 02, 2012 124 %* *  simonpj committed Sep 13, 2002 125 126 %************************************************************************  simonpj committed Oct 09, 2003 127 The monad itself has to be defined here, because it is mentioned by ErrCtxt  simonpj committed Sep 13, 2002 128 129  \begin{code}  ian@well-typed.com committed Nov 02, 2012 130 131 132 type TcRef a = IORef a type TcId = Id type TcIdSet = IdSet  simonpj@microsoft.com committed Sep 13, 2010 133   simonpj committed Sep 30, 2004 134   simonpj committed Oct 09, 2003 135 type TcRnIf a b c = IOEnv (Env a b) c  ian@well-typed.com committed Nov 02, 2012 136 type IfM lcl a = TcRnIf IfGblEnv lcl a -- Iface stuff  Simon Marlow committed Jul 25, 2006 137   ian@well-typed.com committed Nov 02, 2012 138 139 type IfG a = IfM () a -- Top level type IfL a = IfM IfLclEnv a -- Nested  simonpj committed Oct 09, 2003 140 type TcRn a = TcRnIf TcGblEnv TcLclEnv a  ian@well-typed.com committed Nov 02, 2012 141 142 type RnM a = TcRn a -- Historical type TcM a = TcRn a -- Historical  simonpj committed Sep 13, 2002 143 144 \end{code}  chak@cse.unsw.edu.au. committed Apr 27, 2009 145 146 147 148 149 150 151 152 153 154 155 156 Representation of type bindings to uninstantiated meta variables used during constraint solving. \begin{code} data TcTyVarBind = TcTyVarBind TcTyVar TcType type TcTyVarBinds = Bag TcTyVarBind instance Outputable TcTyVarBind where ppr (TcTyVarBind tv ty) = ppr tv <+> text ":=" <+> ppr ty \end{code}  simonpj committed Sep 13, 2002 157 158  %************************************************************************  dterei committed Nov 01, 2011 159 160 161 %* * The main environment types %* *  simonpj committed Sep 13, 2002 162 163 164 %************************************************************************ \begin{code}  dterei committed Nov 01, 2011 165 166 167 168 -- We 'stack' these envs through the Reader like monad infastructure -- as we move into an expression (although the change is focused in -- the lcl type). data Env gbl lcl  simonpj committed Sep 13, 2002 169  = Env {  dterei committed Nov 01, 2011 170 171  env_top :: HscEnv, -- Top-level stuff that never changes -- Includes all info about imported things  simonpj committed Sep 13, 2002 172   dterei committed Nov 01, 2011 173 174  env_us :: {-# UNPACK #-} !(IORef UniqSupply), -- Unique supply for local varibles  simonpj committed Sep 13, 2002 175   dterei committed Nov 01, 2011 176 177  env_gbl :: gbl, -- Info about things defined at the top level -- of the module being compiled  simonpj committed Sep 13, 2002 178   ian@well-typed.com committed Nov 02, 2012 179  env_lcl :: lcl -- Nested stuff; changes as we go into  simonpj committed Oct 09, 2003 180  }  simonpj committed Sep 13, 2002 181   Ian Lynagh committed Jan 19, 2012 182 183 instance ContainsDynFlags (Env gbl lcl) where extractDynFlags env = hsc_dflags (env_top env)  ian@well-typed.com committed Dec 08, 2012 184 185  replaceDynFlags env dflags = env {env_top = replaceDynFlags (env_top env) dflags}  Ian Lynagh committed Jan 19, 2012 186   ian@well-typed.com committed Nov 02, 2012 187 188 189 instance ContainsModule gbl => ContainsModule (Env gbl lcl) where extractModule env = extractModule (env_gbl env)  ian@well-typed.com committed Nov 02, 2012 190 -- TcGblEnv describes the top-level of the module at the  simonpj committed Sep 13, 2002 191 192 -- point at which the typechecker is finished work. -- It is this structure that is handed on to the desugarer  dterei committed Nov 01, 2011 193 194 -- For state that needs to be updated during the typechecking -- phase and returned at end, use a TcRef (= IORef).  simonpj committed Sep 13, 2002 195 196 197  data TcGblEnv = TcGblEnv {  ian@well-typed.com committed Nov 02, 2012 198 199  tcg_mod :: Module, -- ^ Module being compiled tcg_src :: HscSource,  Thomas Schilling committed Dec 11, 2008 200  -- ^ What kind of module (regular Haskell, hs-boot, ext-core)  simonpj committed Jan 27, 2005 201   ian@well-typed.com committed Nov 02, 2012 202 203  tcg_rdr_env :: GlobalRdrEnv, -- ^ Top level envt; used during renaming tcg_default :: Maybe [Type],  Thomas Schilling committed Dec 11, 2008 204  -- ^ Types used for defaulting. @Nothing@ => no @default@ decl  simonpj committed Oct 09, 2003 205   ian@well-typed.com committed Nov 02, 2012 206 207  tcg_fix_env :: FixityEnv, -- ^ Just for things in this module tcg_field_env :: RecFieldEnv, -- ^ Just for things in this module  simonpj committed Sep 13, 2002 208   ian@well-typed.com committed Nov 02, 2012 209  tcg_type_env :: TypeEnv,  Thomas Schilling committed Dec 11, 2008 210  -- ^ Global type env for the module we are compiling now. All  ian@well-typed.com committed Nov 02, 2012 211 212 213 214 215 216 217 218 219 220 221 222 223 224  -- TyCons and Classes (for this module) end up in here right away, -- along with their derived constructors, selectors. -- -- (Ids defined in this module start in the local envt, though they -- move to the global envt during zonking) tcg_type_env_var :: TcRef TypeEnv, -- Used only to initialise the interface-file -- typechecker in initIfaceTcRn, so that it can see stuff -- bound in this module when dealing with hi-boot recursions -- Updated at intervals (e.g. after dealing with types and classes) tcg_inst_env :: InstEnv, -- ^ Instance envt for all /home-package/ modules;  Simon Peyton Jones committed Feb 16, 2012 225  -- Includes the dfuns in tcg_insts  ian@well-typed.com committed Nov 02, 2012 226 227 228 229 230 231 232 233  tcg_fam_inst_env :: FamInstEnv, -- ^ Ditto for family instances -- Now a bunch of things about this module that are simply -- accumulated, but never consulted until the end. -- Nevertheless, it's convenient to accumulate them along -- with the rest of the info from this module. tcg_exports :: [AvailInfo], -- ^ What is exported tcg_imports :: ImportAvails,  Thomas Schilling committed Dec 11, 2008 234  -- ^ Information about what was imported from where, including  ian@well-typed.com committed Nov 02, 2012 235  -- things bound in this module. Also store Safe Haskell info  dterei committed Nov 01, 2011 236  -- here about transative trusted packaage requirements.  Thomas Schilling committed Dec 11, 2008 237   Simon Peyton Jones committed Nov 26, 2012 238 239 240  tcg_dus :: DefUses, -- ^ What is defined in this module and what is used. tcg_used_rdrnames :: TcRef (Set RdrName), -- See Note [Tracking unused binding and imports]  Thomas Schilling committed Dec 11, 2008 241   ian@well-typed.com committed Nov 02, 2012 242  tcg_keep :: TcRef NameSet,  Thomas Schilling committed Dec 11, 2008 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260  -- ^ Locally-defined top-level names to keep alive. -- -- "Keep alive" means give them an Exported flag, so that the -- simplifier does not discard them as dead code, and so that they -- are exposed in the interface file (but not to export to the -- user). -- -- Some things, like dict-fun Ids and default-method Ids are "born" -- with the Exported flag on, for exactly the above reason, but some -- we only discover as we go. Specifically: -- -- * The to/from functions for generic data types -- -- * Top-level variables appearing free in the RHS of an orphan -- rule -- -- * Top-level variables appearing free in a TH bracket  Simon Marlow committed Dec 02, 2010 261  tcg_th_used :: TcRef Bool,  Thomas Schilling committed Dec 11, 2008 262 263  -- ^ @True@ <=> Template Haskell syntax used. --  Simon Marlow committed Dec 02, 2010 264  -- We need this so that we can generate a dependency on the  Gabor Greif committed Jan 30, 2013 265  -- Template Haskell package, because the desugarer is going  Simon Marlow committed Dec 02, 2010 266 267  -- to emit loads of references to TH symbols. The reference -- is implicit rather than explicit, so we have to zap a  Thomas Schilling committed Dec 11, 2008 268 269  -- mutable variable.  Simon Marlow committed Jul 20, 2011 270 271 272 273 274  tcg_th_splice_used :: TcRef Bool, -- ^ @True@ <=> A Template Haskell splice was used. -- -- Splices disable recompilation avoidance (see #481)  ian@well-typed.com committed Nov 02, 2012 275  tcg_dfun_n :: TcRef OccSet,  Simon Marlow committed Jul 16, 2009 276  -- ^ Allows us to choose unique DFun names.  Thomas Schilling committed Dec 11, 2008 277   ian@well-typed.com committed Nov 02, 2012 278 279 280  -- The next fields accumulate the payload of the module -- The binds, rules and foreign-decl fiels are collected -- initially in un-zonked form and are finally zonked in tcRnSrcDecls  simonpj committed Apr 28, 2005 281   David Himmelstrup committed Feb 24, 2006 282  tcg_rn_exports :: Maybe [Located (IE Name)],  simonpj@microsoft.com committed Jul 06, 2009 283  tcg_rn_imports :: [LImportDecl Name],  ian@well-typed.com committed Nov 02, 2012 284 285  -- Keep the renamed imports regardless. They are not -- voluminous and are needed if you want to report unused imports  simonpj@microsoft.com committed Feb 10, 2010 286   ian@well-typed.com committed Nov 02, 2012 287  tcg_rn_decls :: Maybe (HsGroup Name),  Thomas Schilling committed Dec 11, 2008 288 289  -- ^ Renamed decls, maybe. @Nothing@ <=> Don't retain renamed -- decls.  simonmar committed Apr 27, 2005 290   Simon Marlow committed Nov 18, 2011 291  tcg_dependent_files :: TcRef [FilePath], -- ^ dependencies from addDependentFile  GregWeber committed Nov 05, 2011 292   ian@well-typed.com committed Nov 02, 2012 293 294 295  tcg_ev_binds :: Bag EvBind, -- Top-level evidence bindings tcg_binds :: LHsBinds Id, -- Value bindings in this module tcg_sigs :: NameSet, -- ...Top-level names that *lack* a signature  simonpj@microsoft.com committed Oct 07, 2010 296  tcg_imp_specs :: [LTcSpecPrag], -- ...SPECIALISE prags for imported Ids  ian@well-typed.com committed Nov 02, 2012 297 298  tcg_warns :: Warnings, -- ...Warnings and deprecations tcg_anns :: [Annotation], -- ...Annotations  dreixel committed Nov 11, 2011 299  tcg_tcs :: [TyCon], -- ...TyCons and Classes  ian@well-typed.com committed Nov 02, 2012 300  tcg_insts :: [ClsInst], -- ...Instances  eir@cis.upenn.edu committed Dec 21, 2012 301  tcg_fam_insts :: [FamInst Branched],-- ...Family instances  chak@cse.unsw.edu.au. committed Feb 20, 2011 302 303 304  tcg_rules :: [LRuleDecl Id], -- ...Rules tcg_fords :: [LForeignDecl Id], -- ...Foreign import & exports tcg_vects :: [LVectDecl Id], -- ...Vectorisation declarations  davve@dtek.chalmers.se committed Oct 05, 2006 305   ian@well-typed.com committed Nov 02, 2012 306  tcg_doc_hdr :: Maybe LHsDocString, -- ^ Maybe Haddock header docs  Simon Marlow committed Nov 30, 2009 307 308 309  tcg_hpc :: AnyHpcUsage, -- ^ @True@ if any part of the -- prog uses hpc instrumentation.  dterei committed Nov 01, 2011 310  tcg_main :: Maybe Name, -- ^ The Name of the main  Simon Marlow committed Nov 30, 2009 311 312  -- function, if this module is -- the main module.  Simon Marlow committed Jun 11, 2012 313 314 315  tcg_safeInfer :: TcRef Bool -- Has the typechecker -- inferred this module -- as -XSafe (Safe Haskell)  simonpj committed Sep 13, 2002 316  }  David Himmelstrup committed Jun 21, 2007 317   ian@well-typed.com committed Nov 02, 2012 318 319 320 instance ContainsModule TcGblEnv where extractModule env = tcg_mod env  ian@well-typed.com committed Nov 02, 2012 321 322 323 324 325 326 327 328 329 330 331 332 data RecFieldEnv = RecFields (NameEnv [Name]) -- Maps a constructor name *in this module* -- to the fields for that constructor NameSet -- Set of all fields declared *in this module*; -- used to suppress name-shadowing complaints -- when using record wild cards -- E.g. let fld = e in C {..} -- This is used when dealing with ".." notation in record -- construction and pattern matching. -- The FieldEnv deals *only* with constructors defined in *this* -- module. For imported modules, we get the same info from the -- TypeEnv  simonpj committed Sep 13, 2002 333 334 \end{code}  Simon Peyton Jones committed Nov 26, 2012 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 Note [Tracking unused binding and imports] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We gather two sorts of usage information * tcg_dus (defs/uses) Records *defined* Names (local, top-level) and *used* Names (local or imported) Used (a) to report "defined but not used" (see RnNames.reportUnusedNames) (b) to generate version-tracking usage info in interface files (see MkIface.mkUsedNames) This usage info is mainly gathered by the renamer's gathering of free-variables * tcg_used_rdrnames Records used *imported* (not locally-defined) RdrNames Used only to report unused import declarations Notice that they are RdrNames, not Names, so we can tell whether the reference was qualified or unqualified, which is esssential in deciding whether a particular import decl is unnecessary. This info isn't present in Names.  simonpj committed Oct 09, 2003 358 %************************************************************************  ian@well-typed.com committed Nov 02, 2012 359 360 361 362 %* * The interface environments Used when dealing with IfaceDecls %* *  simonpj committed Oct 09, 2003 363 364 365 %************************************************************************ \begin{code}  ian@well-typed.com committed Nov 02, 2012 366 data IfGblEnv  simonpj committed Oct 09, 2003 367  = IfGblEnv {  ian@well-typed.com committed Nov 02, 2012 368 369 370 371 372 373 374 375 376  -- The type environment for the module being compiled, -- in case the interface refers back to it via a reference that -- was originally a hi-boot file. -- We need the module name so we can test when it's appropriate -- to look in this env. if_rec_types :: Maybe (Module, IfG TypeEnv) -- Allows a read effect, so it can be in a mutable -- variable; c.f. handling the external package type env -- Nothing => interactive stuff, no loops possible  simonpj committed Oct 09, 2003 377 378 379 380  } data IfLclEnv = IfLclEnv {  ian@well-typed.com committed Nov 02, 2012 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395  -- The module for the current IfaceDecl -- So if we see f = \x -> x -- it means M.f = \x -> x, where M is the if_mod if_mod :: Module, -- The field is used only for error reporting -- if (say) there's a Lint error in it if_loc :: SDoc, -- Where the interface came from: -- .hi file, or GHCi state, or ext core -- plus which bit is currently being examined if_tv_env :: UniqFM TyVar, -- Nested tyvar bindings -- (and coercions) if_id_env :: UniqFM Id -- Nested id binding  simonpj committed Oct 09, 2003 396 397 398  } \end{code}  simonpj committed Sep 13, 2002 399 400  %************************************************************************  ian@well-typed.com committed Nov 02, 2012 401 402 403 %* * The local typechecker environment %* *  simonpj committed Sep 13, 2002 404 405 406 407 408 %************************************************************************ The Global-Env/Local-Env story ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ During type checking, we keep in the tcg_type_env  ian@well-typed.com committed Nov 02, 2012 409 410  * All types and classes * All Ids derived from types and classes (constructors, selectors)  simonpj committed Sep 13, 2002 411 412 413  At the end of type checking, we zonk the local bindings, and as we do so we add to the tcg_type_env  ian@well-typed.com committed Nov 02, 2012 414  * Locally defined top-level Ids  simonpj committed Sep 13, 2002 415 416  Why? Because they are now Ids not TcIds. This final GlobalEnv is  ian@well-typed.com committed Nov 02, 2012 417 418 419  a) fed back (via the knot) to typechecking the unfoldings of interface signatures b) used in the ModDetails of this module  simonpj committed Sep 13, 2002 420 421  \begin{code}  ian@well-typed.com committed Nov 02, 2012 422 423 data TcLclEnv -- Changes as we move inside an expression -- Discarded after typecheck/rename; not passed on to desugarer  simonpj committed Sep 13, 2002 424  = TcLclEnv {  ian@well-typed.com committed Nov 02, 2012 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444  tcl_loc :: SrcSpan, -- Source span tcl_ctxt :: [ErrCtxt], -- Error context, innermost on top tcl_untch :: Untouchables, -- Birthplace for new unification variables tcl_th_ctxt :: ThStage, -- Template Haskell context tcl_arrow_ctxt :: ArrowCtxt, -- Arrow-notation context tcl_rdr :: LocalRdrEnv, -- Local name envt -- Maintained during renaming, of course, but also during -- type checking, solely so that when renaming a Template-Haskell -- splice we have the right environment for the renamer. -- -- Does *not* include global name envt; may shadow it -- Includes both ordinary variables and type variables; -- they are kept distinct because tyvar have a different -- occurrence contructor (Name.TvOcc) -- We still need the unsullied global name env so that -- we can look up record field names tcl_env :: TcTypeEnv, -- The local type environment: -- Ids and TyVars defined in this module  Simon Peyton Jones committed Sep 17, 2012 445 446 447  tcl_bndrs :: [TcIdBinder], -- Stack of locally-bound Ids, innermost on top -- Used only for error reporting  Simon Peyton Jones committed Feb 16, 2012 448 449 450  tcl_tidy :: TidyEnv, -- Used for tidying types; contains all -- in-scope type variables (but not term variables)  ian@well-typed.com committed Nov 02, 2012 451 452 453 454 455  tcl_tyvars :: TcRef TcTyVarSet, -- The "global tyvars" -- Namely, the in-scope TyVars bound in tcl_env, -- plus the tyvars mentioned in the types of Ids bound -- in tcl_lenv.  chak@cse.unsw.edu.au. committed Apr 27, 2009 456 457  -- Why mutable? see notes with tcGetGlobalTyVars  ian@well-typed.com committed Nov 02, 2012 458 459  tcl_lie :: TcRef WantedConstraints, -- Place to accumulate type constraints tcl_errs :: TcRef Messages -- Place to accumulate errors  simonpj committed Sep 13, 2002 460 461  }  simonpj@microsoft.com committed Sep 13, 2010 462 type TcTypeEnv = NameEnv TcTyThing  Simon Peyton Jones committed Sep 17, 2012 463 data TcIdBinder = TcIdBndr TcId TopLevelFlag  simonpj committed Sep 30, 2004 464   simonpj committed Oct 01, 2004 465 466 {- Note [Given Insts] ~~~~~~~~~~~~~~~~~~  ian@well-typed.com committed Nov 02, 2012 467 Because of GADTs, we have to pass inwards the Insts provided by type signatures  simonpj committed Oct 01, 2004 468 and existential contexts. Consider  ian@well-typed.com committed Nov 02, 2012 469 470 471  data T a where { T1 :: b -> b -> T [b] } f :: Eq a => T a -> Bool f (T1 x y) = [x]==[y]  simonpj committed Oct 01, 2004 472 473  The constructor T1 binds an existential variable 'b', and we need Eq [b].  ian@well-typed.com committed Nov 02, 2012 474 Well, we have it, because Eq a refines to Eq [b], but we can only spot that if we  simonpj committed Oct 01, 2004 475 476 477 pass it inwards. -}  simonpj committed Sep 30, 2004 478   simonpj committed Jun 24, 2003 479 ---------------------------  ian@well-typed.com committed Nov 02, 2012 480 -- Template Haskell stages and levels  simonpj committed Jun 24, 2003 481 482 ---------------------------  ian@well-typed.com committed Nov 02, 2012 483 484 485 486 487 488 489 490 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  simonpj@microsoft.com committed Sep 10, 2009 491   ian@well-typed.com committed Nov 02, 2012 492 493 494 495  | Brack -- Inside brackets ThStage -- Binding level = level(stage) + 1 (TcRef [PendingSplice]) -- Accumulate pending splices here (TcRef WantedConstraints) -- and type constraints here  simonpj@microsoft.com committed Sep 10, 2009 496 497 498 499 500 501 502 503  topStage, topAnnStage, topSpliceStage :: ThStage topStage = Comp topAnnStage = Splice topSpliceStage = Splice instance Outputable ThStage where ppr Splice = text "Splice"  ian@well-typed.com committed Nov 02, 2012 504  ppr Comp = text "Comp"  simonpj@microsoft.com committed Sep 10, 2009 505 506  ppr (Brack s _ _) = text "Brack" <> parens (ppr s)  ian@well-typed.com committed Nov 02, 2012 507 type ThLevel = Int  simonpj@microsoft.com committed Sep 10, 2009 508  -- See Note [Template Haskell levels] in TcSplice  ian@well-typed.com committed Nov 02, 2012 509 510 511 512  -- 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  simonpj committed Nov 25, 2004 513   simonpj@microsoft.com committed Sep 10, 2009 514 impLevel, outerLevel :: ThLevel  ian@well-typed.com committed Nov 02, 2012 515 516 impLevel = 0 -- Imported things; they can be used inside a top level splice outerLevel = 1 -- Things defined outside brackets  simonpj@microsoft.com committed Sep 10, 2009 517 -- NB: Things at level 0 are not *necessarily* imported.  ian@well-typed.com committed Nov 02, 2012 518 -- eg $( \b -> ... ) here b is bound at level 0  simonpj committed Nov 25, 2004 519 --  ian@well-typed.com committed Nov 02, 2012 520 521 522 523 -- For example: -- f = ... -- g1 =$(map ...) is OK -- g2 = $(f ...) is not OK; because we havn't compiled f yet  simonpj committed Nov 25, 2004 524   simonpj@microsoft.com committed Sep 10, 2009 525 526 527 528 thLevel :: ThStage -> ThLevel thLevel Splice = 0 thLevel Comp = 1 thLevel (Brack s _ _) = thLevel s + 1  simonpj committed Sep 13, 2002 529   ross committed May 04, 2005 530 531 532 533 --------------------------- -- Arrow-notation context ---------------------------  Simon Peyton Jones committed Sep 01, 2012 534 535 {- Note [Escaping the arrow scope] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ross committed May 04, 2005 536 537 538 539 In arrow notation, a variable bound by a proc (or enclosed let/kappa) is not in scope to the left of an arrow tail (-<) or the head of (|..|). For example  ian@well-typed.com committed Nov 02, 2012 540  proc x -> (e1 -< e2)  ross committed May 04, 2005 541 542 543 544  Here, x is not in scope in e1, but it is in scope in e2. This can get a bit complicated:  ian@well-typed.com committed Nov 02, 2012 545 546  let x = 3 in proc y -> (proc z -> e1) -< e2  ross committed May 04, 2005 547   ian@well-typed.com committed Nov 02, 2012 548 Here, x and z are in scope in e1, but y is not.  Simon Peyton Jones committed Sep 17, 2012 549 550  We implement this by  ross committed May 04, 2005 551 552 553 recording the environment when passing a proc (using newArrowScope), and returning to that (using escapeArrowScope) on the left of -< and the head of (|..|).  Simon Peyton Jones committed Sep 01, 2012 554   ian@well-typed.com committed Nov 02, 2012 555 All this can be dealt with by the *renamer*; by the time we get to  Simon Peyton Jones committed Sep 01, 2012 556 the *type checker* we have sorted out the scopes  ross committed May 04, 2005 557 558 -}  ross committed May 05, 2005 559 560 561 data ArrowCtxt = NoArrowCtxt | ArrowCtxt (Env TcGblEnv TcLclEnv)  ross committed May 04, 2005 562 563 564 565 566  -- Record the current environment (outside a proc) newArrowScope :: TcM a -> TcM a newArrowScope = updEnv$ \env ->  ian@well-typed.com committed Nov 02, 2012 567  env { env_lcl = (env_lcl env) { tcl_arrow_ctxt = ArrowCtxt env } }  ross committed May 04, 2005 568 569 570  -- Return to the stored environment (from the enclosing proc) escapeArrowScope :: TcM a -> TcM a  ross committed May 05, 2005 571 572 escapeArrowScope = updEnv $\ env -> case tcl_arrow_ctxt (env_lcl env) of  ian@well-typed.com committed Nov 02, 2012 573 574  NoArrowCtxt -> env ArrowCtxt env' -> env'  simonpj committed Sep 13, 2002 575   simonpj committed Jun 24, 2003 576 577 578 579 --------------------------- -- TcTyThing ---------------------------  simonpj committed Sep 13, 2002 580 data TcTyThing  ian@well-typed.com committed Nov 02, 2012 581  = AGlobal TyThing -- Used only in the return type of a lookup  simonpj committed Dec 21, 2004 582   ian@well-typed.com committed Nov 02, 2012 583 584 585 586  | ATcId { -- Ids defined in this module; may not be fully zonked tct_id :: TcId, tct_closed :: TopLevelFlag, -- See Note [Bindings with closed types] tct_level :: ThLevel }  simonpj committed Dec 21, 2004 587   ian@well-typed.com committed Nov 02, 2012 588 589 590 591  | ATyVar Name TcTyVar -- The type variable to which the lexically scoped type -- variable is bound. We only need the Name -- for error-message purposes; it is the corresponding -- Name in the domain of the envt  simonpj committed Dec 21, 2004 592   dreixel committed Nov 11, 2011 593  | AThing TcKind -- Used temporarily, during kind checking, for the  ian@well-typed.com committed Nov 02, 2012 594  -- tycons and clases in this recursive group  dreixel committed Nov 11, 2011 595 596 597  -- Can be a mono-kind or a poly-kind; in TcTyClsDcls see -- Note [Type checking recursive type and class declarations]  ian@well-typed.com committed Nov 02, 2012 598  | APromotionErr PromotionErr  Simon Peyton Jones committed Jul 10, 2012 599   ian@well-typed.com committed Nov 02, 2012 600 data PromotionErr  Simon Peyton Jones committed Jul 10, 2012 601 602 603 604 605  = TyConPE -- TyCon used in a kind before we are ready -- data T :: T -> * where ... | ClassPE -- Ditto Class | FamDataConPE -- Data constructor for a data family  Simon Peyton Jones committed Jun 07, 2012 606  -- See Note [AFamDataCon: not promoting data family constructors] in TcRnDriver  dreixel committed Nov 11, 2011 607   Simon Peyton Jones committed Jul 10, 2012 608  | RecDataConPE -- Data constructor in a reuursive loop  Simon Peyton Jones committed Jun 07, 2012 609  -- See Note [ARecDataCon: recusion and promoting data constructors] in TcTyClsDecls  jpm@cs.ox.ac.uk committed Nov 21, 2012 610  | NoDataKinds -- -XDataKinds not enabled  simonpj committed Nov 28, 2002 611   ian@well-typed.com committed Nov 02, 2012 612 instance Outputable TcTyThing where -- Debugging only  613  ppr (AGlobal g) = pprTyThing g  ian@well-typed.com committed Nov 02, 2012 614 615  ppr elt@(ATcId {}) = text "Identifier" <> brackets (ppr (tct_id elt) <> dcolon  simonpj@microsoft.com committed Sep 13, 2010 616  <> ppr (varType (tct_id elt)) <> comma  ian@well-typed.com committed Nov 02, 2012 617 618  <+> ppr (tct_closed elt) <> comma <+> ppr (tct_level elt))  Simon Peyton Jones committed Aug 14, 2012 619  ppr (ATyVar n tv) = text "Type variable" <+> quotes (ppr n) <+> equals <+> ppr tv  simonpj committed Dec 30, 2003 620  ppr (AThing k) = text "AThing" <+> ppr k  Simon Peyton Jones committed Jul 10, 2012 621 622 623 624 625 626 627  ppr (APromotionErr err) = text "APromotionErr" <+> ppr err instance Outputable PromotionErr where ppr ClassPE = text "ClassPE" ppr TyConPE = text "TyConPE" ppr FamDataConPE = text "FamDataConPE" ppr RecDataConPE = text "RecDataConPE"  jpm@cs.ox.ac.uk committed Nov 21, 2012 628  ppr NoDataKinds = text "NoDataKinds"  simonpj committed Feb 04, 2005 629 630  pprTcTyThingCategory :: TcTyThing -> SDoc  Simon Peyton Jones committed Jul 10, 2012 631 632 633 634 635 636 637 638 639 640 641 pprTcTyThingCategory (AGlobal thing) = pprTyThingCategory thing pprTcTyThingCategory (ATyVar {}) = ptext (sLit "Type variable") pprTcTyThingCategory (ATcId {}) = ptext (sLit "Local identifier") pprTcTyThingCategory (AThing {}) = ptext (sLit "Kinded thing") pprTcTyThingCategory (APromotionErr pe) = pprPECategory pe pprPECategory :: PromotionErr -> SDoc pprPECategory ClassPE = ptext (sLit "Class") pprPECategory TyConPE = ptext (sLit "Type constructor") pprPECategory FamDataConPE = ptext (sLit "Data constructor") pprPECategory RecDataConPE = ptext (sLit "Data constructor")  jpm@cs.ox.ac.uk committed Nov 21, 2012 642 pprPECategory NoDataKinds = ptext (sLit "Data constructor")  simonpj committed Sep 13, 2002 643 644 \end{code}  Simon Peyton Jones committed Jun 07, 2012 645   Simon Peyton Jones committed Aug 16, 2011 646 647 Note [Bindings with closed types] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  Simon Peyton Jones committed Sep 01, 2011 648 649 650 651 652 Consider f x = let g ys = map not ys in ...  ian@well-typed.com committed Nov 02, 2012 653 Can we generalise 'g' under the OutsideIn algorithm? Yes,  dreixel committed Nov 11, 2011 654 because all g's free variables are top-level; that is they themselves  Simon Peyton Jones committed Sep 01, 2011 655 656 657 658 659 660 have no free type variables, and it is the type variables in the environment that makes things tricky for OutsideIn generalisation. Definition: A variable is "closed", and has tct_closed set to TopLevel,  ian@well-typed.com committed Nov 02, 2012 661  iff  Simon Peyton Jones committed Sep 01, 2011 662 663 664 665 666  a) all its free variables are imported, or are themselves closed b) generalisation is not restricted by the monomorphism restriction Under OutsideIn we are free to generalise a closed let-binding. This is an extension compared to the JFP paper on OutsideIn, which  ian@well-typed.com committed Nov 02, 2012 667 used "top-level" as a proxy for "closed". (It's not a good proxy  Simon Peyton Jones committed Sep 01, 2011 668 669 670 671 672 673 674 675 anyway -- the MR can make a top-level binding with a free type variable.) Note that: * A top-level binding may not be closed, if it suffer from the MR * A nested binding may be closed (eg 'g' in the example we started with) Indeed, that's the point; whether a function is defined at top level  ian@well-typed.com committed Nov 02, 2012 676  or nested is orthogonal to the question of whether or not it is closed  Simon Peyton Jones committed Sep 01, 2011 677 678 679 680 681 682  * A binding may be non-closed because it mentions a lexically scoped *type variable* Eg f :: forall a. blah f x = let g y = ...(y::a)...  Simon Peyton Jones committed Aug 16, 2011 683   simonpj committed Sep 13, 2002 684 \begin{code}  Simon Peyton Jones committed Jan 12, 2012 685 type ErrCtxt = (Bool, TidyEnv -> TcM (TidyEnv, MsgDoc))  ian@well-typed.com committed Nov 02, 2012 686 687 688  -- Monadic so that we have a chance -- to deal with bound type variables just before error -- message construction  simonpj@microsoft.com committed Jul 23, 2009 689   ian@well-typed.com committed Nov 02, 2012 690 691  -- Bool: True <=> this is a landmark context; do not -- discard it when trimming for display  simonpj committed Sep 13, 2002 692 693 694 695 \end{code} %************************************************************************  ian@well-typed.com committed Nov 02, 2012 696 697 698 %* * Operations over ImportAvails %* *  simonpj committed Sep 13, 2002 699 700 701 %************************************************************************ \begin{code}  Thomas Schilling committed Dec 11, 2008 702 703 704 705 706 707 708 709 710 711 712 713 -- | 'ImportAvails' summarises what was imported from where, irrespective of -- whether the imported things are actually used or not. It is used: -- -- * when processing the export list, -- -- * when constructing usage info for the interface file, -- -- * to identify the list of directly imported modules for initialisation -- purposes and for optimised overlap checking of family instances, -- -- * when figuring out what things are really unused --  ian@well-typed.com committed Nov 02, 2012 714 data ImportAvails  simonpj committed Sep 13, 2002 715  = ImportAvails {  ian@well-typed.com committed Nov 02, 2012 716 717  imp_mods :: ImportedMods, -- = ModuleEnv [(ModuleName, Bool, SrcSpan, Bool)],  Thomas Schilling committed Dec 11, 2008 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742  -- ^ Domain is all directly-imported modules -- The 'ModuleName' is what the module was imported as, e.g. in -- @ -- import Foo as Bar -- @ -- it is @Bar@. -- -- The 'Bool' means: -- -- - @True@ => import was @import Foo ()@ -- -- - @False@ => import was some other form -- -- Used -- -- (a) to help construct the usage information in the interface -- file; if we import somethign we need to recompile if the -- export version changes -- -- (b) to specify what child modules to initialise -- -- We need a full ModuleEnv rather than a ModuleNameEnv here, -- because we might be importing modules of the same name from -- different packages. (currently not the case, but might be in the -- future).  simonpj committed Oct 24, 2002 743   dterei committed Jun 29, 2011 744 745 746 747 748 749 750 751 752 753 754  imp_dep_mods :: ModuleNameEnv (ModuleName, IsBootInterface), -- ^ Home-package modules needed by the module being compiled -- -- It doesn't matter whether any of these dependencies -- are actually /used/ when compiling the module; they -- are listed if they are below it at all. For -- example, suppose M imports A which imports X. Then -- compiling M might not need to consult X.hi, but X -- is still listed in M's dependencies. imp_dep_pkgs :: [PackageId],  Thomas Schilling committed Dec 11, 2008 755 756 757  -- ^ Packages needed by the module being compiled, whether directly, -- or via other modules in this package, or via modules imported -- from other packages.  ian@well-typed.com committed Nov 02, 2012 758   dterei committed Jun 17, 2011 759 760 761 762 763 764 765 766 767  imp_trust_pkgs :: [PackageId], -- ^ This is strictly a subset of imp_dep_pkgs and records the -- packages the current module needs to trust for Safe Haskell -- compilation to succeed. A package is required to be trusted if -- we are dependent on a trustworthy module in that package. -- While perhaps making imp_dep_pkgs a tuple of (PackageId, Bool) -- where True for the bool indicates the package is required to be -- trusted is the more logical design, doing so complicates a lot -- of code not concerned with Safe Haskell.  dterei committed Jun 29, 2011 768  -- See Note [RnNames . Tracking Trust Transitively]  simonpj committed Oct 25, 2002 769   dterei committed Jun 29, 2011 770 771 772 773 774 775 776  imp_trust_own_pkg :: Bool, -- ^ Do we require that our own package is trusted? -- This is to handle efficiently the case where a Safe module imports -- a Trustworthy module that resides in the same package as it. -- See Note [RnNames . Trust Own Package] imp_orphs :: [Module],  Thomas Schilling committed Dec 11, 2008 777 778  -- ^ Orphan modules below us in the import tree (and maybe including -- us for imported modules)  chak@cse.unsw.edu.au. committed Oct 13, 2006 779   dterei committed Jun 29, 2011 780  imp_finsts :: [Module]  Thomas Schilling committed Dec 11, 2008 781 782  -- ^ Family instance modules below us in the import tree (and maybe -- including us for imported modules)  simonpj committed Sep 13, 2002 783 784  }  Simon Marlow committed Jul 25, 2006 785 mkModDeps :: [(ModuleName, IsBootInterface)]  ian@well-typed.com committed Nov 02, 2012 786  -> ModuleNameEnv (ModuleName, IsBootInterface)  Simon Marlow committed Jul 25, 2006 787 mkModDeps deps = foldl add emptyUFM deps  ian@well-typed.com committed Nov 02, 2012 788 789  where add env elt@(m,_) = addToUFM env m elt  simonpj committed Oct 09, 2003 790   simonpj committed Sep 13, 2002 791 emptyImportAvails :: ImportAvails  dterei committed Jun 29, 2011 792 793 794 795 796 797 798 799 800 801 802 emptyImportAvails = ImportAvails { imp_mods = emptyModuleEnv, imp_dep_mods = emptyUFM, imp_dep_pkgs = [], imp_trust_pkgs = [], imp_trust_own_pkg = False, imp_orphs = [], imp_finsts = [] } -- | Union two ImportAvails -- -- This function is a key part of Import handling, basically  Gabor Greif committed Nov 21, 2012 803 -- for each import we create a separate ImportAvails structure  dterei committed Jun 29, 2011 804 -- and then union them all together with this function.  simonpj committed Sep 13, 2002 805 806 plusImportAvails :: ImportAvails -> ImportAvails -> ImportAvails plusImportAvails  simonpj@microsoft.com committed Oct 13, 2006 807  (ImportAvails { imp_mods = mods1,  dterei committed Jun 29, 2011 808 809  imp_dep_mods = dmods1, imp_dep_pkgs = dpkgs1, imp_trust_pkgs = tpkgs1, imp_trust_own_pkg = tself1,  simonpj@microsoft.com committed Oct 13, 2006 810 811  imp_orphs = orphs1, imp_finsts = finsts1 }) (ImportAvails { imp_mods = mods2,  dterei committed Jun 29, 2011 812 813  imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2, imp_trust_pkgs = tpkgs2, imp_trust_own_pkg = tself2,  simonpj@microsoft.com committed Oct 13, 2006 814  imp_orphs = orphs2, imp_finsts = finsts2 })  dterei committed Jun 29, 2011 815 816 817 818 819 820 821  = ImportAvails { imp_mods = plusModuleEnv_C (++) mods1 mods2, imp_dep_mods = plusUFM_C plus_mod_dep dmods1 dmods2, imp_dep_pkgs = dpkgs1 unionLists dpkgs2, imp_trust_pkgs = tpkgs1 unionLists tpkgs2, imp_trust_own_pkg = tself1 || tself2, imp_orphs = orphs1 unionLists orphs2, imp_finsts = finsts1 unionLists finsts2 }  simonpj committed Sep 13, 2002 822  where  ian@well-typed.com committed Nov 02, 2012 823  plus_mod_dep (m1, boot1) (m2, boot2)  dterei committed Jun 29, 2011 824 825 826  = WARN( not (m1 == m2), (ppr m1 <+> ppr m2)$\$ (ppr boot1 <+> ppr boot2) ) -- Check mod-names match (m1, boot1 && boot2) -- If either side can "see" a non-hi-boot interface, use that  simonpj committed Sep 13, 2002 827 828 829 \end{code} %************************************************************************  ian@well-typed.com committed Nov 02, 2012 830 %* *  simonpj committed Sep 13, 2002 831 \subsection{Where from}  ian@well-typed.com committed Nov 02, 2012 832 %* *  simonpj committed Sep 13, 2002 833 834 835 836 837 %************************************************************************ The @WhereFrom@ type controls where the renamer looks for an interface file \begin{code}  ian@well-typed.com committed Nov 02, 2012 838 839 840 data WhereFrom = ImportByUser IsBootInterface -- Ordinary user import (perhaps {-# SOURCE #-}) | ImportBySystem -- Non user import.  841 842  | ImportByPlugin -- Importing a plugin; -- See Note [Care with plugin imports] in LoadIface  simonpj committed Sep 13, 2002 843 844  instance Outputable WhereFrom where  Ian Lynagh committed Apr 12, 2008 845  ppr (ImportByUser is_boot) | is_boot = ptext (sLit "{- SOURCE -}")  ian@well-typed.com committed Nov 02, 2012 846 847  | otherwise = empty ppr ImportBySystem = ptext (sLit "{- SYSTEM -}")  848  ppr ImportByPlugin = ptext (sLit "{- PLUGIN -}")  simonpj committed Sep 13, 2002 849 850 \end{code}  dimitris committed Nov 16, 2011 851 %************************************************************************  ian@well-typed.com committed Nov 02, 2012 852 %* *  dimitris committed Nov 16, 2011 853 854 855 %* Canonical constraints * %* * %* These are the constraints the low-level simplifier works with *  ian@well-typed.com committed Nov 02, 2012 856 %* *  dimitris committed Nov 16, 2011 857 858 859 860 %************************************************************************ \begin{code}  dimitris committed Apr 10, 2012 861 862 863 864 -- The syntax of xi types: -- xi ::= a | T xis | xis -> xis | ... | forall a. tau -- Two important notes: -- (i) No type families, unless we are under a ForAll  ian@well-typed.com committed Nov 02, 2012 865 866 -- (ii) Note that xi types can contain unexpanded type synonyms; -- however, the (transitive) expansions of those type synonyms  dimitris committed Apr 10, 2012 867 868 869 -- will not contain any type functions, unless we are under a ForAll. -- We enforce the structure of Xi types when we flatten (TcCanonical)  dimitris committed Nov 16, 2011 870 871 872 873 874 type Xi = Type -- In many comments, "xi" ranges over Xi type Cts = Bag Ct data Ct  ian@well-typed.com committed Nov 02, 2012 875  -- Atomic canonical constraints  dimitris committed Nov 16, 2011 876  = CDictCan { -- e.g. Num xi  Simon Peyton Jones committed Jul 23, 2012 877  cc_ev :: CtEvidence, -- See Note [Ct/evidence invariant]  ian@well-typed.com committed Nov 02, 2012 878  cc_class :: Class,  dimitris committed Nov 16, 2011 879 880  cc_tyargs :: [Xi],  Simon Peyton Jones committed Sep 17, 2012 881  cc_loc :: CtLoc  dimitris committed Nov 16, 2011 882 883 884  } | CIrredEvCan { -- These stand for yet-unknown predicates  Simon Peyton Jones committed Jul 23, 2012 885  cc_ev :: CtEvidence, -- See Note [Ct/evidence invariant]  ian@well-typed.com committed Nov 02, 2012 886  -- In CIrredEvCan, the ctev_pred of the evidence is flat  Simon Peyton Jones committed Sep 17, 2012 887  -- and hence it may only be of the form (tv xi1 xi2 ... xin)  Simon Peyton Jones committed Sep 01, 2012 888 889 890  -- Since, if it were a type constructor application, that'd make the -- whole constraint a CDictCan, or CTyEqCan. And it can't be -- a type family application either because it's a Xi type.  Simon Peyton Jones committed Sep 17, 2012 891  cc_loc :: CtLoc  dimitris committed Nov 16, 2011 892 893  }  ian@well-typed.com committed Nov 02, 2012 894 895  | CTyEqCan { -- tv ~ xi (recall xi means function free) -- Invariant:  dimitris committed Nov 16, 2011 896 897 898 899  -- * tv not in tvs(xi) (occurs check) -- * typeKind xi compatKind typeKind tv -- See Note [Spontaneous solving and kind compatibility] -- * We prefer unification variables on the left *JUST* for efficiency  Simon Peyton Jones committed Jul 23, 2012 900  cc_ev :: CtEvidence, -- See Note [Ct/evidence invariant]  ian@well-typed.com committed Nov 02, 2012 901  cc_tyvar :: TcTyVar,  dimitris committed Nov 16, 2011 902  cc_rhs :: Xi,  Simon Peyton Jones committed Sep 17, 2012 903  cc_loc :: CtLoc  dimitris committed Nov 16, 2011 904 905  }  ian@well-typed.com committed Nov 02, 2012 906 907  | CFunEqCan { -- F xis ~ xi -- Invariant: * isSynFamilyTyCon cc_fun  dimitris committed Nov 16, 2011 908  -- * typeKind (F xis) compatKind typeKind xi  Simon Peyton Jones committed Sep 01, 2012 909  cc_ev :: CtEvidence, -- See Note [Ct/evidence invariant]  ian@well-typed.com committed Nov 02, 2012 910 911 912 913  cc_fun :: TyCon, -- A type function cc_tyargs :: [Xi], -- Either under-saturated or exactly saturated cc_rhs :: Xi, -- *never* over-saturated (because if so -- we should have decomposed)  dimitris committed Nov 16, 2011 914   Simon Peyton Jones committed Sep 17, 2012 915  cc_loc :: CtLoc  ian@well-typed.com committed Nov 02, 2012 916   dimitris committed Nov 16, 2011 917 918  }  ian@well-typed.com committed Nov 02, 2012 919 920  | CNonCanonical { -- See Note [NonCanonical Semantics] cc_ev :: CtEvidence,  Simon Peyton Jones committed Sep 17, 2012 921  cc_loc :: CtLoc  dimitris committed Nov 16, 2011 922  }  Simon Peyton Jones committed Sep 17, 2012 923 924  | CHoleCan {  Simon Peyton Jones committed Sep 17, 2012 925  cc_ev :: CtEvidence,  Simon Peyton Jones committed Jan 30, 2013 926 927  cc_loc :: CtLoc, cc_occ :: OccName -- The name of this hole  Simon Peyton Jones committed Sep 17, 2012 928  }  dimitris committed Nov 29, 2011 929 930 \end{code}  Simon Peyton Jones committed Jul 23, 2012 931 932 933 Note [Ct/evidence invariant] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If ct :: Ct, then extra fields of 'ct' cache precisely the ctev_pred field  ian@well-typed.com committed Nov 02, 2012 934 of (cc_ev ct), and is fully rewritten wrt the substitution. Eg for CDictCan,  Simon Peyton Jones committed Jul 23, 2012 935 936  ctev_pred (cc_ev ct) = (cc_class ct) (cc_tyargs ct) This holds by construction; look at the unique place where CDictCan is  Simon Peyton Jones committed Oct 26, 2012 937 938 939 940 built (in TcCanonical). In contrast, the type of the evidence *term* (ccev_evtm or ctev_evar) in the evidence may *not* be fully zonked; we are careful not to look at it  Gabor Greif committed Nov 07, 2012 941 during constraint solving. See Note [Evidence field of CtEvidence]