[project @ 2001-01-03 11:18:51 by simonmar]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcBinds.lhs
index f308e33..4f81c0d 100644 (file)
@@ -4,7 +4,7 @@
 \section[TcBinds]{TcBinds}
 
 \begin{code}
-module TcBinds ( tcBindsAndThen, tcTopBindsAndThen,
+module TcBinds ( tcBindsAndThen, tcTopBinds,
                 tcSpecSigs, tcBindWithSigs ) where
 
 #include "HsVersions.h"
@@ -12,6 +12,7 @@ module TcBinds ( tcBindsAndThen, tcTopBindsAndThen,
 import {-# SOURCE #-} TcMatches ( tcGRHSs, tcMatchesFun )
 import {-# SOURCE #-} TcExpr  ( tcExpr )
 
+import CmdLineOpts     ( opt_NoMonomorphismRestriction )
 import HsSyn           ( HsExpr(..), HsBinds(..), MonoBinds(..), Sig(..), StmtCtxt(..),
                          Match(..), collectMonoBinders, andMonoBinds
                        )
@@ -40,7 +41,8 @@ import TcType         ( TcThetaType, newTyVarTy, newTyVar,
                        )
 import TcUnify         ( unifyTauTy, unifyTauTyLists )
 
-import Id              ( mkVanillaId, setInlinePragma, idFreeTyVars )
+import CoreFVs         ( idFreeTyVars )
+import Id              ( mkVanillaId, setInlinePragma )
 import Var             ( idType, idName )
 import IdInfo          ( InlinePragInfo(..) )
 import Name            ( Name, getOccName, getSrcLoc )
@@ -48,9 +50,9 @@ import NameSet
 import Type            ( mkTyVarTy, tyVarsOfTypes, mkTyConApp,
                          mkForAllTys, mkFunTys, 
                          mkPredTy, mkForAllTy, isUnLiftedType, 
-                         isUnboxedType, unboxedTypeKind, boxedTypeKind, openTypeKind
+                         unliftedTypeKind, liftedTypeKind, openTypeKind
                        )
-import FunDeps         ( tyVarFunDep, oclose )
+import FunDeps         ( oclose )
 import Var             ( tyVarKind )
 import VarSet
 import Bag
@@ -58,7 +60,7 @@ import Util           ( isIn )
 import Maybes          ( maybeToBool )
 import BasicTypes      ( TopLevelFlag(..), RecFlag(..), isNotTopLevel )
 import FiniteMap       ( listToFM, lookupFM )
-import PrelNames       ( ioTyConKey, mainKey, hasKey )
+import PrelNames       ( ioTyConName, mainKey, hasKey )
 import Outputable
 \end{code}
 
@@ -95,14 +97,22 @@ At the top-level the LIE is sure to contain nothing but constant
 dictionaries, which we resolve at the module level.
 
 \begin{code}
-tcTopBindsAndThen, tcBindsAndThen
+tcTopBinds :: RenamedHsBinds -> TcM ((TcMonoBinds, TcEnv), LIE)
+tcTopBinds binds
+  = tc_binds_and_then TopLevel glue binds      $
+    tcGetEnv                                   `thenNF_Tc` \ env ->
+    returnTc ((EmptyMonoBinds, env), emptyLIE)
+  where
+    glue is_rec binds1 (binds2, thing) = (binds1 `AndMonoBinds` binds2, thing)
+
+
+tcBindsAndThen
        :: (RecFlag -> TcMonoBinds -> thing -> thing)           -- Combinator
        -> RenamedHsBinds
        -> TcM (thing, LIE)
        -> TcM (thing, LIE)
 
-tcTopBindsAndThen = tc_binds_and_then TopLevel
-tcBindsAndThen    = tc_binds_and_then NotTopLevel
+tcBindsAndThen = tc_binds_and_then NotTopLevel
 
 tc_binds_and_then top_lvl combiner EmptyBinds do_next
   = do_next
@@ -132,7 +142,7 @@ tc_binds_and_then top_lvl combiner (MonoBind bind sigs is_rec) do_next
 
        -- Create specialisations of functions bound here
        -- We want to keep non-recursive things non-recursive
-       -- so that we desugar unboxed bindings correctly
+       -- so that we desugar unlifted bindings correctly
       case (top_lvl, is_rec) of
 
                -- For the top level don't bother will all this bindInstsOfLocalFuns stuff
@@ -230,7 +240,7 @@ tcBindWithSigs top_lvl mbind tc_ty_sigs inline_sigs is_rec
        -- If typechecking the binds fails, then return with each
        -- signature-less binder given type (forall a.a), to minimise subsequent
        -- error messages
-       newTyVar boxedTypeKind          `thenNF_Tc` \ alpha_tv ->
+       newTyVar liftedTypeKind         `thenNF_Tc` \ alpha_tv ->
        let
          forall_a_a    = mkForAllTy alpha_tv (mkTyVarTy alpha_tv)
           binder_names  = collectMonoBinders mbind
@@ -271,7 +281,7 @@ tcBindWithSigs top_lvl mbind tc_ty_sigs inline_sigs is_rec
     getTyVarsToGen is_unrestricted mono_id_tys lie_req `thenNF_Tc` \ (tyvars_not_to_gen, tyvars_to_gen) ->
 
        -- Finally, zonk the generalised type variables to real TyVars
-       -- This commits any unbound kind variables to boxed kind
+       -- This commits any unbound kind variables to lifted kind
        -- I'm a little worried that such a kind variable might be
        -- free in the environment, but I don't think it's possible for
        -- this to happen when the type variable is not free in the envt
@@ -352,10 +362,10 @@ tcBindWithSigs top_lvl mbind tc_ty_sigs inline_sigs is_rec
        returnTc ()
     )                                                  `thenTc_`
 
-    ASSERT( not (any ((== unboxedTypeKind) . tyVarKind) real_tyvars_to_gen_list) )
+    ASSERT( not (any ((== unliftedTypeKind) . tyVarKind) real_tyvars_to_gen_list) )
                -- The instCantBeGeneralised stuff in tcSimplify should have
                -- already raised an error if we're trying to generalise an 
-               -- unboxed tyvar (NB: unboxed tyvars are always introduced 
+               -- unlifted tyvar (NB: unlifted tyvars are always introduced 
                -- along with a class constraint) and it's better done there 
                -- because we have more precise origin information.
                -- That's why we just use an ASSERT here.
@@ -400,10 +410,10 @@ tcBindWithSigs top_lvl mbind tc_ty_sigs inline_sigs is_rec
        pat_binders :: [Name]
        pat_binders = collectMonoBinders (justPatBindings mbind EmptyMonoBinds)
     in
-       -- CHECK FOR UNBOXED BINDERS IN PATTERN BINDINGS
+       -- CHECK FOR UNLIFTED BINDERS IN PATTERN BINDINGS
     mapTc (\id -> checkTc (not (idName id `elem` pat_binders
-                               && isUnboxedType (idType id)))
-                         (unboxedPatBindErr id)) zonked_mono_ids
+                               && isUnLiftedType (idType id)))
+                         (unliftedPatBindErr id)) zonked_mono_ids
                                `thenTc_`
 
         -- BUILD RESULTS
@@ -419,7 +429,8 @@ tcBindWithSigs top_lvl mbind tc_ty_sigs inline_sigs is_rec
     )
   where
     tysig_names     = [name | (TySigInfo name _ _ _ _ _ _ _) <- tc_ty_sigs]
-    is_unrestricted = isUnRestrictedGroup tysig_names mbind
+    is_unrestricted | opt_NoMonomorphismRestriction = True
+                   | otherwise                     = isUnRestrictedGroup tysig_names mbind
 
 justPatBindings bind@(PatMonoBind _ _ _) binds = bind `andMonoBinds` binds
 justPatBindings (AndMonoBinds b1 b2) binds = 
@@ -548,8 +559,8 @@ getTyVarsToGen is_unrestricted mono_id_tys lie
          -- Then we should generalise over b too; otherwise it will be
          -- reported as ambiguous.
        zonkFunDeps fds         `thenNF_Tc` \ fds' ->
-       let tvFundep        = tyVarFunDep fds'
-           extended_tyvars = oclose tvFundep body_tyvars
+       let 
+           extended_tyvars = oclose fds' body_tyvars
        in
        returnNF_Tc (emptyVarSet, extended_tyvars)
     else
@@ -712,8 +723,8 @@ tcMonoBinds mbinds tc_ty_sigs is_rec
        -- Figure out the appropriate kind for the pattern,
        -- and generate a suitable type variable 
     kind = case is_rec of
-               Recursive    -> boxedTypeKind   -- Recursive, so no unboxed types
-               NonRecursive -> openTypeKind    -- Non-recursive, so we permit unboxed types
+               Recursive    -> liftedTypeKind  -- Recursive, so no unlifted types
+               NonRecursive -> openTypeKind    -- Non-recursive, so we permit unlifted types
 \end{code}
 
 %************************************************************************
@@ -737,7 +748,7 @@ checkSigMatch top_lvl binder_names mono_ids sigs
   =    -- First unify the main_id with IO t, for any old t
     tcSetErrCtxt mainTyCheckCtxt (
        tcLookupTyCon ioTyConName               `thenTc`    \ ioTyCon ->
-       newTyVarTy boxedTypeKind                `thenNF_Tc` \ t_tv ->
+       newTyVarTy liftedTypeKind               `thenNF_Tc` \ t_tv ->
        unifyTauTy ((mkTyConApp ioTyCon [t_tv]))
                   (idType main_mono_id)
     )                                          `thenTc_`
@@ -750,8 +761,7 @@ checkSigMatch top_lvl binder_names mono_ids sigs
        -- which is just waht check_one_sig looks for
     mapTc check_one_sig sigs                   `thenTc_`
     mapTc check_main_ctxt sigs                 `thenTc_` 
-
-           returnTc (Just ([], emptyLIE))
+    returnTc (Just ([], emptyLIE))
 
   | not (null sigs)
   = mapTc check_one_sig sigs                   `thenTc_`
@@ -905,8 +915,8 @@ valSpecSigCtxt v ty
         nest 4 (ppr v <+> dcolon <+> ppr ty)]
 
 -----------------------------------------------
-unboxedPatBindErr id
-  = ptext SLIT("variable in a lazy pattern binding has unboxed type: ")
+unliftedPatBindErr id
+  = ptext SLIT("variable in a lazy pattern binding has unlifted type: ")
         <+> quotes (ppr id)
 
 -----------------------------------------------
@@ -934,7 +944,7 @@ mainTyCheckCtxt
 
 -----------------------------------------------
 unliftedBindErr flavour mbind
-  = hang (text flavour <+> ptext SLIT("bindings for unlifted types aren't allowed"))
+  = hang (text flavour <+> ptext SLIT("bindings for unlifted types aren't allowed:"))
         4 (ppr mbind)
 
 existentialExplode mbinds