+import TcType ( TcKind, TcType, TcTyVar, TcTyVarSet,
+ zonkTcTyVarsAndFV
+ )
+import Id ( idName, mkSpecPragmaId, mkUserLocal, isDataConWrapId_maybe )
+import IdInfo ( vanillaIdInfo )
+import Var ( TyVar, Id, idType, lazySetIdInfo, idInfo )
+import VarSet
+import Type ( Type, ThetaType,
+ tyVarsOfTypes, splitDFunTy,
+ getDFunTyKey, tyConAppTyCon
+ )
+import DataCon ( DataCon )
+import TyCon ( TyCon )
+import Class ( Class, ClassOpItem )
+import Name ( Name, OccName, NamedThing(..),
+ nameOccName, getSrcLoc, mkLocalName, isLocalName,
+ nameIsLocalOrFrom
+ )
+import NameEnv ( NameEnv, lookupNameEnv, nameEnvElts, elemNameEnv,
+ extendNameEnvList, emptyNameEnv, plusNameEnv )
+import OccName ( mkDFunOcc, occNameString )
+import HscTypes ( DFunId,
+ PackageTypeEnv, TypeEnv,
+ extendTypeEnvList, extendTypeEnvWithIds,
+ typeEnvTyCons, typeEnvClasses, typeEnvIds,
+ HomeSymbolTable
+ )
+import Module ( Module )
+import InstEnv ( InstEnv, emptyInstEnv )
+import HscTypes ( lookupType, TyThing(..) )
+import Util ( zipEqual )
+import SrcLoc ( SrcLoc )
+import qualified PrelNames
+import Outputable
+
+import IOExts ( newIORef )
+\end{code}
+
+%************************************************************************
+%* *
+\subsection{TcEnv}
+%* *
+%************************************************************************
+
+\begin{code}
+type TcId = Id -- Type may be a TcType
+type TcIdSet = IdSet
+
+data TcEnv
+ = TcEnv {
+ tcSyntaxMap :: PrelNames.SyntaxMap, -- The syntax map (usually the identity)
+
+ tcGST :: Name -> Maybe TyThing, -- The type environment at the moment we began this compilation
+
+ tcInsts :: InstEnv, -- All instances (both imported and in this module)
+
+ tcGEnv :: TypeEnv, -- The global type environment we've accumulated while
+ {- NameEnv TyThing-} -- compiling this module:
+ -- types and classes (both imported and local)
+ -- imported Ids
+ -- (Ids defined in this module start in the local envt,
+ -- though they move to the global envt during zonking)
+
+ tcLEnv :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars
+ -- defined in this module
+
+ tcTyVars :: TcRef TcTyVarSet -- The "global tyvars"
+ -- Namely, the in-scope TyVars bound in tcLEnv, plus the tyvars
+ -- mentioned in the types of Ids bound in tcLEnv
+ -- Why mutable? see notes with tcGetGlobalTyVars
+ }
+
+\end{code}
+
+The Global-Env/Local-Env story
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+During type checking, we keep in the GlobalEnv
+ * All types and classes
+ * All Ids derived from types and classes (constructors, selectors)
+ * Imported Ids
+
+At the end of type checking, we zonk the local bindings,
+and as we do so we add to the GlobalEnv
+ * Locally defined top-level Ids
+
+Why? Because they are now Ids not TcIds. This final GlobalEnv is
+used thus:
+ a) fed back (via the knot) to typechecking the
+ unfoldings of interface signatures