[project @ 2000-10-18 12:47:55 by sewardj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcEnv.lhs
index 61f1437..63f91ae 100644 (file)
@@ -1,23 +1,25 @@
 \begin{code}
 module TcEnv(
        TcId, TcIdSet, 
-       TyThing(..), TyThingDetails(..),
+       TyThing(..), TyThingDetails(..), TcTyThing(..),
 
        -- Getting stuff from the environment
        TcEnv, initTcEnv, 
-       tcEnvTyCons, tcEnvClasses, tcEnvIds, tcEnvTcIds,
+       tcEnvTyCons, tcEnvClasses, tcEnvIds, tcEnvTcIds, tcEnvTyVars,
+       getTcGST, getTcGEnv,
        
        -- Instance environment
        tcGetInstEnv, tcSetInstEnv, 
 
        -- Global environment
        tcExtendGlobalEnv, tcExtendGlobalValEnv, 
-       tcLookupTy, tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon,
+       tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon,
+       tcLookupGlobal_maybe, tcLookupGlobal,
 
        -- Local environment
        tcExtendKindEnv, 
        tcExtendTyVarEnv, tcExtendTyVarEnvForMeths, 
-       tcExtendLocalValEnv,
+       tcExtendLocalValEnv, tcLookup,
 
        -- Global type variables
        tcGetGlobalTyVars, tcExtendGlobalTyVars,
@@ -27,50 +29,54 @@ module TcEnv(
 
        -- New Ids
        newLocalId, newSpecPragmaId,
-       newDefaultMethodName, newDFunName
+       newDefaultMethodName, newDFunName,
+
+       -- ???
+       tcSetEnv, explicitLookupId
   ) where
 
 #include "HsVersions.h"
 
 import TcMonad
-import TcType  ( TcKind,  TcType, TcTyVar, TcTyVarSet, TcThetaType,
-                 tcInstTyVars, zonkTcTyVars,
-               )
-import Id      ( mkUserLocal, isDataConWrapId_maybe )
-import IdInfo  ( vanillaIdInfo )
-import MkId    ( mkSpecPragmaId )
-import Var     ( TyVar, Id, setVarName,
-                 idType, lazySetIdInfo, idInfo, tyVarKind, UVar,
-               )
+import TcType          ( TcKind,  TcType, TcTyVar, TcTyVarSet, TcThetaType,
+                         tcInstTyVars, zonkTcTyVars,
+                       )
+import Id              ( mkUserLocal, isDataConWrapId_maybe )
+import IdInfo          ( vanillaIdInfo )
+import MkId            ( mkSpecPragmaId )
+import Var             ( TyVar, Id, setVarName,
+                         idType, lazySetIdInfo, idInfo, tyVarKind, UVar,
+                       )
 import VarSet
-import VarEnv  ( TyVarSubstEnv )
-import Type    ( Kind, Type, superKind,
-                 tyVarsOfType, tyVarsOfTypes,
-                 splitForAllTys, splitRhoTy, splitFunTys,
-                 splitAlgTyConApp_maybe, getTyVar, getDFunTyKey
-               )
-import DataCon ( DataCon )
-import TyCon   ( TyCon, tyConKind, tyConArity, isSynTyCon )
-import Class   ( Class, ClassOpItem, ClassContext, classTyCon )
-import Subst   ( substTy )
-import Name    ( Name, OccName, Provenance(..), ExportFlag(..), NamedThing(..), 
-                 nameOccName, nameModule, getSrcLoc, mkGlobalName,
-                 maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
-                 NameEnv, emptyNameEnv, lookupNameEnv, nameEnvElts, 
-                 extendNameEnv, extendNameEnvList
-               )
-import OccName ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
-import Module  ( Module )
-import Unify   ( unifyTyListsX, matchTys )
-import HscTypes        ( ModDetails(..), lookupTypeEnv )
-import Unique  ( pprUnique10, Unique, Uniquable(..) )
+import Type            ( Kind, Type, superKind,
+                         tyVarsOfType, tyVarsOfTypes,
+                         splitForAllTys, splitRhoTy, splitFunTys,
+                         splitAlgTyConApp_maybe, getTyVar, getDFunTyKey
+                       )
+import DataCon         ( DataCon )
+import TyCon           ( TyCon, tyConKind, tyConArity, isSynTyCon )
+import Class           ( Class, ClassOpItem, ClassContext, classTyCon )
+import Subst           ( substTy )
+import Name            ( Name, OccName, NamedThing(..), 
+                         nameOccName, nameModule, getSrcLoc, mkGlobalName,
+                         isLocallyDefined,
+                         NameEnv, emptyNameEnv, lookupNameEnv, nameEnvElts, 
+                         extendNameEnv, extendNameEnvList
+                       )
+import OccName         ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
+import Module          ( Module )
+import HscTypes                ( ModDetails(..), InstEnv, lookupTypeEnv, TyThing(..),
+                         GlobalSymbolTable, Provenance(..) )
+import Unique          ( pprUnique10, Unique, Uniquable(..) )
 import UniqFM
-import Unique  ( Uniquable(..) )
-import Util    ( zipEqual, zipWith3Equal, mapAccumL )
-import SrcLoc  ( SrcLoc )
+import Unique          ( Uniquable(..) )
+import Util            ( zipEqual, zipWith3Equal, mapAccumL )
+import SrcLoc          ( SrcLoc )
 import FastString      ( FastString )
-import Maybes
 import Outputable
+import TcInstUtil      ( emptyInstEnv )
+
+import IOExts          ( newIORef )
 \end{code}
 
 %************************************************************************
@@ -87,10 +93,10 @@ data TcEnv
   = TcEnv {
        tcGST    :: GlobalSymbolTable,  -- The symbol table at the moment we began this compilation
 
-       tcInst   :: InstEnv,            -- All instances (both imported and in this module)
+       tcInsts  :: InstEnv,            -- All instances (both imported and in this module)
 
-       tcGEnv   :: NameEnv TyThing     -- The global type environment we've accumulated while
-                                       -- compiling this module:
+       tcGEnv   :: NameEnv TyThing,    -- The global type environment we've accumulated while
+                   {- TypeEnv -}       -- compiling this module:
                                        --      types and classes (both imported and local)
                                        --      imported Ids
                                        -- (Ids defined in this module are in the local envt)
@@ -138,13 +144,13 @@ data TcTyThing
 --     3. Then we zonk the kind variable.
 --     4. Now we know the kind for 'a', and we add (a -> ATyVar a::K) to the environment
 
-initTcEnv :: GlobalSymbolTable -> InstEnv -> IO TcEnv
-initTcEnv gst inst_env
-  = do { gtv_var <- newIORef emptyVarSet
-        return (TcEnv { tcGST = gst,
-                        tcGEnv = emptyNameEnv, 
-                        tcInst = inst_env,
-                        tcLEnv = emptyNameEnv,
+initTcEnv :: GlobalSymbolTable -> IO TcEnv
+initTcEnv gst
+  = do { gtv_var <- newIORef emptyVarSet ;
+        return (TcEnv { tcGST    = gst,
+                        tcGEnv   = emptyNameEnv,
+                        tcInsts  = emptyInstEnv,
+                        tcLEnv   = emptyNameEnv,
                         tcTyVars = gtv_var
         })}
 
@@ -154,6 +160,9 @@ tcEnvIds     env = [id | AnId   id <- nameEnvElts (tcGEnv env)]
 tcEnvTyVars  env = [tv | ATyVar tv <- nameEnvElts (tcLEnv env)]
 tcEnvTcIds   env = [id | ATcId  id <- nameEnvElts (tcLEnv env)]
 
+getTcGST  (TcEnv { tcGST = gst })   = gst
+getTcGEnv (TcEnv { tcGEnv = genv }) = genv
+
 -- This data type is used to help tie the knot
 -- when type checking type and class declarations
 data TyThingDetails = SynTyDetails Type
@@ -172,17 +181,17 @@ data TyThingDetails = SynTyDetails Type
 lookup_global :: TcEnv -> Name -> Maybe TyThing
        -- Try the global envt and then the global symbol table
 lookup_global env name 
-  = case lookupNameEnv (tcGEnv env) name of {
-       Just thing -> Just thing ;
+  = case lookupNameEnv (tcGEnv env) name of
+       Just thing -> Just thing
        Nothing    -> lookupTypeEnv (tcGST env) name
 
 lookup_local :: TcEnv -> Name -> Maybe TcTyThing
        -- Try the local envt and then try the global
 lookup_local env name
- = case lookupNameEnv (tcLEnv env) name of
-       Just thing -> Just thing ;
+  = case lookupNameEnv (tcLEnv env) name of
+       Just thing -> Just thing
        Nothing    -> case lookup_global env name of
-                       Just thing -> AGlobal thing
+                       Just thing -> Just (AGlobal thing)
                        Nothing    -> Nothing
 
 explicitLookupId :: TcEnv -> Name -> Maybe Id
@@ -262,7 +271,7 @@ newDFunName mod clas (ty:_) loc
     tcGetUnique                        `thenNF_Tc` \ uniq ->
     returnNF_Tc (mkGlobalName uniq mod
                              (mkDFunOcc dfun_string inst_uniq) 
-                             (LocalDef loc Exported))
+                             loc)
   where
        -- Any string that is somewhat unique will do
     dfun_string = occNameString (getOccName clas) ++ occNameString (getDFunTyKey ty)
@@ -272,7 +281,7 @@ newDefaultMethodName op_name loc
   = tcGetUnique                        `thenNF_Tc` \ uniq ->
     returnNF_Tc (mkGlobalName uniq (nameModule op_name)
                              (mkDefaultMethodOcc (getOccName op_name))
-                             (LocalDef loc Exported))
+                             loc)
 \end{code}
 
 
@@ -308,6 +317,7 @@ A variety of global lookups, when we know what we are looking for.
 
 \begin{code}
 tcLookupGlobal :: Name -> NF_TcM TyThing
+tcLookupGlobal name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_thing ->
     case maybe_thing of
        Just thing -> returnNF_Tc thing
@@ -317,15 +327,15 @@ tcLookupGlobalId :: Name -> NF_TcM Id
 tcLookupGlobalId name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_id ->
     case maybe_id of
-       Just (AnId clas) -> returnNF_Tc id
+       Just (AnId clas) -> returnNF_Tc clas
        other            -> notFound "tcLookupGlobalId:" name
        
 tcLookupDataCon :: Name -> TcM DataCon
 tcLookupDataCon con_name
   = tcLookupGlobalId con_name          `thenNF_Tc` \ con_id ->
-    case isDataConWrapId_maybe con_id of {
+    case isDataConWrapId_maybe con_id of
        Just data_con -> returnTc data_con
-       Nothing       -> failWithTc (badCon con_id);
+       Nothing       -> failWithTc (badCon con_id)
 
 
 tcLookupClass :: Name -> NF_TcM Class
@@ -435,7 +445,7 @@ tcExtendLocalValEnv names_w_ids thing_inside
 tcExtendGlobalTyVars extra_global_tvs thing_inside
   = tcGetEnv                                           `thenNF_Tc` \ env ->
     tc_extend_gtvs (tcTyVars env) extra_global_tvs     `thenNF_Tc` \ gtvs' ->
-    tcSetEnv (env {tcTyVars = gtvs') thing_inside
+    tcSetEnv (env {tcTyVars = gtvs'}) thing_inside
 
 tc_extend_gtvs gtvs extra_global_tvs
   = tcReadMutVar gtvs                  `thenNF_Tc` \ global_tvs ->
@@ -469,12 +479,12 @@ tcGetGlobalTyVars
 \begin{code}
 tcGetInstEnv :: NF_TcM InstEnv
 tcGetInstEnv = tcGetEnv        `thenNF_Tc` \ env -> 
-              returnNF_Tc (tcInst env)
+              returnNF_Tc (tcInsts env)
 
 tcSetInstEnv :: InstEnv -> TcM a -> TcM a
 tcSetInstEnv ie thing_inside
   = tcGetEnv   `thenNF_Tc` \ env ->
-    tcSetEnv (env {tcInst = ie}) thing_inside
+    tcSetEnv (env {tcInsts = ie}) thing_inside
 \end{code}    
 
 
@@ -487,6 +497,6 @@ tcSetInstEnv ie thing_inside
 \begin{code}
 badCon con_id = quotes (ppr con_id) <+> ptext SLIT("is not a data constructor")
 
-notFound where name = failWithTc (text where <> colon <+> quotes (ppr name) <+> 
+notFound wheRe name = failWithTc (text wheRe <> colon <+> quotes (ppr name) <+> 
                                  ptext SLIT("is not in scope"))
 \end{code}