[project @ 1997-06-05 21:10:48 by sof]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcType.lhs
index a237dc6..7902ace 100644 (file)
@@ -3,26 +3,28 @@
 
 module TcType (
 
-  TcTyVar(..),
+  SYN_IE(TcTyVar),
   newTcTyVar,
   newTyVarTy,  -- Kind -> NF_TcM s (TcType s)
   newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType s]
 
 
-  TcTyVarSet(..),
+  SYN_IE(TcTyVarSet),
 
   -----------------------------------------
-  TcType(..), TcMaybe(..),
-  TcTauType(..), TcThetaType(..), TcRhoType(..),
+  SYN_IE(TcType), TcMaybe(..),
+  SYN_IE(TcTauType), SYN_IE(TcThetaType), SYN_IE(TcRhoType),
 
        -- Find the type to which a type variable is bound
   tcWriteTyVar,                -- :: TcTyVar s -> TcType s -> NF_TcM (TcType s)
   tcReadTyVar,         -- :: TcTyVar s -> NF_TcM (TcMaybe s)
 
 
+  tcSplitForAllTy, tcSplitRhoTy,
+
   tcInstTyVars,
   tcInstSigTyVars, 
-  tcInstType, tcInstSigType, tcInstTcType,
+  tcInstType, tcInstSigType, tcInstTcType, tcInstSigTcType,
   tcInstTheta, tcInstId,
 
   zonkTcTyVars,
@@ -36,24 +38,25 @@ module TcType (
 
 
 -- friends:
-import Type    ( Type(..), ThetaType(..), GenType(..),
+import Type    ( SYN_IE(Type), SYN_IE(ThetaType), GenType(..),
                  tyVarsOfTypes, getTyVar_maybe,
-                 splitForAllTy, splitRhoTy,
+                 splitForAllTy, splitRhoTy, isTyVarTy,
                  mkForAllTys, instantiateTy
                )
-import TyVar   ( TyVar(..), GenTyVar(..), TyVarSet(..), GenTyVarSet(..), 
-                 TyVarEnv(..), lookupTyVarEnv, addOneToTyVarEnv,
+import TyVar   ( SYN_IE(TyVar), GenTyVar(..), SYN_IE(TyVarSet), SYN_IE(GenTyVarSet), 
+                 SYN_IE(TyVarEnv), lookupTyVarEnv, addOneToTyVarEnv,
                  nullTyVarEnv, mkTyVarEnv,
                  tyVarSetToList
                )
 
 -- others:
-import Class   ( GenClass )
-import Id      ( idType )
+import Class   ( GenClass, SYN_IE(Class) )
+import TyCon   ( isFunTyCon )
+import Id      ( idType, SYN_IE(Id) )
 import Kind    ( Kind )
 import TcKind  ( TcKind )
-import TcMonad hiding ( rnMtoTcM )
-import Usage   ( Usage(..), GenUsage, UVar(..), duffUsage )
+import TcMonad
+import Usage   ( SYN_IE(Usage), GenUsage, SYN_IE(UVar), duffUsage )
 
 import TysPrim         ( voidTy )
 
@@ -61,12 +64,7 @@ IMP_Ubiq()
 import Unique          ( Unique )
 import UniqFM          ( UniqFM )
 import Maybes          ( assocMaybe )
-import Util            ( zipEqual, nOfThem, panic, pprPanic, pprTrace{-ToDo:rm-} )
-
-import Outputable      ( Outputable(..) )      -- Debugging messages
-import PprType         ( GenTyVar, GenType )
-import Pretty                                  -- ditto
-import PprStyle                ( PprStyle(..) )        -- ditto
+import Util            ( zipEqual, nOfThem, panic{-, pprPanic, pprTrace ToDo:rm-} )
 \end{code}
 
 
@@ -108,6 +106,41 @@ tcTyVarToTyVar :: TcTyVar s -> TyVar
 tcTyVarToTyVar (TyVar uniq kind name _) = TyVar uniq kind name duffUsage
 \end{code}
 
+Utility functions
+~~~~~~~~~~~~~~~~~
+These tcSplit functions are like their non-Tc analogues, but they
+follow through bound type variables.
+
+\begin{code}
+tcSplitForAllTy :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
+tcSplitForAllTy t 
+  = go t t []
+  where
+    go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
+    go syn_t (SynTy _ _ t)   tvs = go syn_t t tvs
+    go syn_t (TyVarTy tv)    tvs = tcReadTyVar tv      `thenNF_Tc` \ maybe_ty ->
+                                  case maybe_ty of
+                                       BoundTo ty | not (isTyVarTy ty) -> go syn_t ty tvs
+                                       other                           -> returnNF_Tc (reverse tvs, syn_t)
+    go syn_t t              tvs = returnNF_Tc (reverse tvs, syn_t)
+
+tcSplitRhoTy :: TcType s -> NF_TcM s ([(Class,TcType s)], TcType s)
+tcSplitRhoTy t
+  = go t t []
+ where
+    go syn_t (FunTy (DictTy c t _) r _) ts = go r r ((c,t):ts)
+    go syn_t (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
+                             | isFunTyCon tycon
+                             = go r r ((c,t):ts)
+    go syn_t (SynTy _ _ t) ts = go syn_t t ts
+    go syn_t (TyVarTy tv)  ts = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
+                               case maybe_ty of
+                                 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty ts
+                                 other                           -> returnNF_Tc (reverse ts, syn_t)
+    go syn_t t            ts = returnNF_Tc (reverse ts, syn_t)
+\end{code}
+
+
 Type instantiation
 ~~~~~~~~~~~~~~~~~~
 
@@ -163,13 +196,20 @@ instantiateTy which could take advantage of sharing some day.
 \begin{code}
 tcInstTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
 tcInstTcType ty
-  = case tyvars of
+  = tcSplitForAllTy ty         `thenNF_Tc` \ (tyvars, rho) -> 
+    case tyvars of
        []    -> returnNF_Tc ([], ty)   -- Nothing to do
        other -> tcInstTyVars tyvars            `thenNF_Tc` \ (tyvars', _, tenv)  ->
                 returnNF_Tc (tyvars', instantiateTy tenv rho)
-  where
-    (tyvars, rho) = splitForAllTy ty
 
+tcInstSigTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
+tcInstSigTcType ty
+  = tcSplitForAllTy ty         `thenNF_Tc` \ (tyvars, rho) ->
+    case tyvars of
+       []    -> returnNF_Tc ([], ty)   -- Nothing to do
+       other -> tcInstSigTyVars tyvars         `thenNF_Tc` \ (tyvars', _, tenv)  ->
+                returnNF_Tc (tyvars', instantiateTy tenv rho)
+    
 tcInstType :: [(GenTyVar flexi,TcType s)] 
           -> GenType (GenTyVar flexi) UVar 
           -> NF_TcM s (TcType s)
@@ -179,8 +219,8 @@ tcInstType tenv ty_to_inst
     bind_fn = inst_tyvar UnBound
     occ_fn env tyvar = case lookupTyVarEnv env tyvar of
                         Just ty -> returnNF_Tc ty
-                        Nothing -> pprPanic "tcInstType:" (ppAboves [ppr PprDebug ty_to_inst, 
-                                                                     ppr PprDebug tyvar])
+                        Nothing -> panic "tcInstType:1" --(vcat [ppr PprDebug ty_to_inst, 
+                                                       --            ppr PprDebug tyvar])
 
 tcInstSigType :: GenType (GenTyVar flexi) UVar -> NF_TcM s (TcType s)
 tcInstSigType ty_to_inst
@@ -189,8 +229,8 @@ tcInstSigType ty_to_inst
     bind_fn = inst_tyvar DontBind
     occ_fn env tyvar = case lookupTyVarEnv env tyvar of
                         Just ty -> returnNF_Tc ty
-                        Nothing -> pprPanic "tcInstType:" (ppAboves [ppr PprDebug ty_to_inst, 
-                                                                     ppr PprDebug tyvar])
+                        Nothing -> panic "tcInstType:2"-- (vcat [ppr PprDebug ty_to_inst, 
+                                                       --            ppr PprDebug tyvar])
 
 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
 zonkTcTyVarToTyVar tv
@@ -199,7 +239,7 @@ zonkTcTyVarToTyVar tv
 
       TyVarTy tv' ->    returnNF_Tc (tcTyVarToTyVar tv')
 
-      _ -> pprTrace "zonkTcTyVarToTyVar:" (ppCat [ppr PprDebug tv, ppr PprDebug tv_ty]) $
+      _ -> --pprTrace "zonkTcTyVarToTyVar:" (hsep [ppr PprDebug tv, ppr PprDebug tv_ty]) $
           returnNF_Tc (tcTyVarToTyVar tv)
 
 
@@ -220,37 +260,37 @@ zonkTcTypeToType env ty
 
 
 tcConvert bind_fn occ_fn env ty_to_convert
-  = do env ty_to_convert
+  = doo env ty_to_convert
   where
-    do env (TyConTy tycon usage) = returnNF_Tc (TyConTy tycon usage)
+    doo env (TyConTy tycon usage) = returnNF_Tc (TyConTy tycon usage)
 
-    do env (SynTy tycon tys ty)  = mapNF_Tc (do env) tys       `thenNF_Tc` \ tys' ->
-                                  do env ty                    `thenNF_Tc` \ ty' ->
+    doo env (SynTy tycon tys ty)  = mapNF_Tc (doo env) tys     `thenNF_Tc` \ tys' ->
+                                  doo env ty                   `thenNF_Tc` \ ty' ->
                                   returnNF_Tc (SynTy tycon tys' ty')
 
-    do env (FunTy arg res usage) = do env arg          `thenNF_Tc` \ arg' ->
-                                  do env res           `thenNF_Tc` \ res' ->
+    doo env (FunTy arg res usage) = doo env arg                `thenNF_Tc` \ arg' ->
+                                  doo env res          `thenNF_Tc` \ res' ->
                                   returnNF_Tc (FunTy arg' res' usage)
 
-    do env (AppTy fun arg)      = do env fun           `thenNF_Tc` \ fun' ->
-                                  do env arg           `thenNF_Tc` \ arg' ->
+    doo env (AppTy fun arg)     = doo env fun          `thenNF_Tc` \ fun' ->
+                                  doo env arg          `thenNF_Tc` \ arg' ->
                                   returnNF_Tc (AppTy fun' arg')
 
-    do env (DictTy clas ty usage)= do env ty           `thenNF_Tc` \ ty' ->
+    doo env (DictTy clas ty usage)= doo env ty         `thenNF_Tc` \ ty' ->
                                   returnNF_Tc (DictTy clas ty' usage)
 
-    do env (ForAllUsageTy u us ty) = do env ty `thenNF_Tc` \ ty' ->
+    doo env (ForAllUsageTy u us ty) = doo env ty       `thenNF_Tc` \ ty' ->
                                     returnNF_Tc (ForAllUsageTy u us ty')
 
        -- The two interesting cases!
-    do env (TyVarTy tv)         = occ_fn env tv
+    doo env (TyVarTy tv)        = occ_fn env tv
 
-    do env (ForAllTy tyvar ty)
+    doo env (ForAllTy tyvar ty)
        = bind_fn tyvar         `thenNF_Tc` \ tyvar' ->
          let
                new_env = addOneToTyVarEnv env tyvar (TyVarTy tyvar')
          in
-         do new_env ty         `thenNF_Tc` \ ty' ->
+         doo new_env ty                `thenNF_Tc` \ ty' ->
          returnNF_Tc (ForAllTy tyvar' ty')
 
 
@@ -367,7 +407,7 @@ zonkTcType (ForAllTy tv ty)
     case tv_ty of      -- Should be a tyvar!
       TyVarTy tv' -> 
                     returnNF_Tc (ForAllTy tv' ty')
-      _ -> pprTrace "zonkTcType:ForAllTy:" (ppCat [ppr PprDebug tv, ppr PprDebug tv_ty]) $
+      _ -> --pprTrace "zonkTcType:ForAllTy:" (hsep [ppr PprDebug tv, ppr PprDebug tv_ty]) $
           
           returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')