[project @ 2001-07-10 11:32:28 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcMonoType.lhs
index 0e622a0..4ceae2b 100644 (file)
@@ -10,8 +10,9 @@ module TcMonoType ( tcHsType, tcHsRecType, tcIfaceType,
 
                        -- Kind checking
                    kcHsTyVar, kcHsTyVars, mkTyClTyVars,
-                   kcHsType, kcHsSigType, kcHsLiftedSigType, kcHsContext,
-                   tcTyVars, tcHsTyVars, mkImmutTyVars,
+                   kcHsType, kcHsSigType, kcHsSigTypes, 
+                   kcHsLiftedSigType, kcHsContext,
+                   tcScopedTyVars, tcHsTyVars, mkImmutTyVars,
 
                    TcSigInfo(..), tcTySig, mkTcSig, maybeSig,
                    checkSigTyVars, sigCtxt, sigPatCtxt
@@ -29,25 +30,25 @@ import TcEnv                ( tcExtendTyVarEnv, tcLookup, tcLookupGlobal,
                          tcGetGlobalTyVars, tcEnvTcIds, tcEnvTyVars,
                          TyThing(..), TcTyThing(..), tcExtendKindEnv
                        )
-import TcType          ( TcKind, TcTyVar, TcThetaType, TcTauType,
-                         newKindVar, tcInstSigVar,
-                         zonkKindEnv, zonkTcType, zonkTcTyVars, zonkTcTyVar
+import TcMType         ( newKindVar, tcInstSigVars, 
+                         zonkKindEnv, zonkTcType, zonkTcTyVars, zonkTcTyVar,
+                         unifyKind, unifyOpenTypeKind
                        )
-import Inst            ( Inst, InstOrigin(..), newMethodWithGivenTy, instToId )
-import FunDeps         ( grow )
-import TcUnify         ( unifyKind, unifyOpenTypeKind )
-import Unify           ( allDistinctTyVars )
-import Type            ( Type, Kind, PredType(..), ThetaType, SigmaType, TauType,
+import TcType          ( Type, Kind, SourceType(..), ThetaType, SigmaType, TauType,
                          mkTyVarTy, mkTyVarTys, mkFunTy, mkSynTy,
-                          zipFunTys, hoistForAllTys,
+                         tcSplitForAllTys, tcSplitRhoTy,
+                         hoistForAllTys, allDistinctTyVars,
+                          zipFunTys, 
                          mkSigmaTy, mkPredTy, mkTyConApp,
-                         mkAppTys, splitForAllTys, splitRhoTy, mkRhoTy,
+                         mkAppTys, mkRhoTy,
                          liftedTypeKind, unliftedTypeKind, mkArrowKind,
-                         mkArrowKinds, getTyVar_maybe, getTyVar, splitFunTy_maybe,
+                         mkArrowKinds, tcGetTyVar_maybe, tcGetTyVar, tcSplitFunTy_maybe,
                          tidyOpenType, tidyOpenTypes, tidyTyVar, tidyTyVars,
                          tyVarsOfType, tyVarsOfPred, mkForAllTys,
-                         isUnboxedTupleType, isForAllTy, isIPPred
+                         isUnboxedTupleType, tcIsForAllTy, isIPPred
                        )
+import Inst            ( Inst, InstOrigin(..), newMethodWithGivenTy, instToId )
+import FunDeps         ( grow )
 import PprType         ( pprType, pprTheta, pprPred )
 import Subst           ( mkTopTyVarSubst, substTy )
 import CoreFVs         ( idFreeTyVars )
@@ -117,6 +118,10 @@ But equally valid would be
                                a::(*->*)-> *, b::*->*
 
 \begin{code}
+-- tcHsTyVars is used for type variables in type signatures
+--     e.g. forall a. a->a
+-- They are immutable, because they scope only over the signature
+-- They may or may not be explicitly-kinded
 tcHsTyVars :: [HsTyVarBndr Name] 
           -> TcM a                             -- The kind checker
           -> ([TyVar] -> TcM b)
@@ -134,16 +139,22 @@ tcHsTyVars tv_names kind_check thing_inside
     in
     tcExtendTyVarEnv tyvars (thing_inside tyvars)
 
-tcTyVars :: [Name] 
-            -> TcM a                           -- The kind checker
-            -> TcM [TyVar]
-tcTyVars [] kind_check = returnTc []
-
-tcTyVars tv_names kind_check
+-- tcScopedTyVars is used for scoped type variables
+--     e.g.  \ (x::a) (y::a) -> x+y
+-- They never have explicit kinds (because this is source-code only)
+-- They are mutable (because they can get bound to a more specific type)
+tcScopedTyVars :: [Name] 
+              -> TcM a                         -- The kind checker
+              -> TcM b
+              -> TcM b
+tcScopedTyVars [] kind_check thing_inside = thing_inside
+
+tcScopedTyVars tv_names kind_check thing_inside
   = mapNF_Tc newNamedKindVar tv_names          `thenTc` \ kind_env ->
     tcExtendKindEnv kind_env kind_check                `thenTc_`
     zonkKindEnv kind_env                       `thenNF_Tc` \ tvs_w_kinds ->
-    listNF_Tc [tcNewSigTyVar name kind | (name,kind) <- tvs_w_kinds]
+    listTc [tcNewMutTyVar name kind | (name, kind) <- tvs_w_kinds]     `thenNF_Tc` \ tyvars ->
+    tcExtendTyVarEnv tyvars thing_inside
 \end{code}
     
 
@@ -178,7 +189,8 @@ kcTypeType ty
 ---------------------------
 kcHsSigType, kcHsLiftedSigType :: RenamedHsType -> TcM ()
        -- Used for type signatures
-kcHsSigType     = kcTypeType
+kcHsSigType      = kcTypeType
+kcHsSigTypes tys  = mapTc_ kcHsSigType tys
 kcHsLiftedSigType = kcLiftedType
 
 ---------------------------
@@ -200,6 +212,9 @@ kcHsType (HsFunTy ty1 ty2)
     kcTypeType ty2     `thenTc_`
     returnTc liftedTypeKind
 
+kcHsType (HsNumTy _)           -- The unit type for generics
+  = returnTc liftedTypeKind
+
 kcHsType ty@(HsOpTy ty1 op ty2)
   = kcTyVar op                         `thenTc` \ op_kind ->
     kcHsType ty1                       `thenTc` \ ty1_kind ->
@@ -227,7 +242,7 @@ kcHsType (HsForAllTy (Just tv_names) context ty)
 
 ---------------------------
 kcAppKind fun_kind arg_kind
-  = case splitFunTy_maybe fun_kind of 
+  = case tcSplitFunTy_maybe fun_kind of 
        Just (arg_kind', res_kind)
                -> unifyKind arg_kind arg_kind' `thenTc_`
                   returnTc res_kind
@@ -290,7 +305,7 @@ tcHsSigType and tcHsLiftedSigType are used for type signatures written by the pr
 \begin{code}
 tcHsSigType, tcHsLiftedSigType :: RenamedHsType -> TcM Type
   -- Do kind checking, and hoist for-alls to the top
-tcHsSigType       ty = kcTypeType   ty `thenTc_` tcHsType ty   
+tcHsSigType       ty = kcTypeType   ty `thenTc_` tcHsType ty
 tcHsLiftedSigType ty = kcLiftedType ty `thenTc_` tcHsType ty
 
 tcHsType    ::            RenamedHsType -> TcM Type
@@ -371,7 +386,10 @@ tc_type wimp_out (HsFunTy ty1 ty2)
   = tc_type wimp_out ty1                       `thenTc` \ tau_ty1 ->
        -- Function argument can be polymorphic, but
        -- must not be an unboxed tuple
-    checkTc (not (isUnboxedTupleType tau_ty1))
+       --
+       -- In a recursive loop we can't ask whether the thing is
+       -- unboxed -- might be a synonym inside a synonym inside a group
+    checkTc (isRec wimp_out || not (isUnboxedTupleType tau_ty1))
            (ubxArgTyErr ty1)                   `thenTc_`
     tc_type wimp_out ty2                       `thenTc` \ tau_ty2 ->
     returnTc (mkFunTy tau_ty1 tau_ty2)
@@ -433,9 +451,9 @@ tc_arg_type wimp_out arg_ty
   = tc_type wimp_out arg_ty
 
   | otherwise
-  = tc_type wimp_out arg_ty                                            `thenTc` \ arg_ty' ->
-    checkTc (not (isForAllTy arg_ty'))        (polyArgTyErr arg_ty)    `thenTc_`
-    checkTc (not (isUnboxedTupleType arg_ty')) (ubxArgTyErr arg_ty)    `thenTc_`
+  = tc_type wimp_out arg_ty                                                            `thenTc` \ arg_ty' ->
+    checkTc (isRec wimp_out || not (tcIsForAllTy arg_ty'))      (polyArgTyErr arg_ty)  `thenTc_`
+    checkTc (isRec wimp_out || not (isUnboxedTupleType arg_ty')) (ubxArgTyErr arg_ty)  `thenTc_`
     returnTc arg_ty'
 
 tc_arg_types wimp_out arg_tys = mapTc (tc_arg_type wimp_out) arg_tys
@@ -677,16 +695,17 @@ mkTcSig poly_id src_loc
        -- typechecking the rest of the program with the function bound
        -- to a pristine type, namely sigma_tc_ty
    let
-       (tyvars, rho) = splitForAllTys (idType poly_id)
+       (tyvars, rho) = tcSplitForAllTys (idType poly_id)
    in
-   mapNF_Tc tcInstSigVar tyvars                `thenNF_Tc` \ tyvars' ->
+   tcInstSigVars tyvars                        `thenNF_Tc` \ tyvars' ->
        -- Make *signature* type variables
 
    let
      tyvar_tys' = mkTyVarTys tyvars'
      rho' = substTy (mkTopTyVarSubst tyvars tyvar_tys') rho
        -- mkTopTyVarSubst because the tyvars' are fresh
-     (theta', tau') = splitRhoTy rho'
+
+     (theta', tau') = tcSplitRhoTy rho'
        -- This splitRhoTy tries hard to make sure that tau' is a type synonym
        -- wherever possible, which can improve interface files.
    in
@@ -781,7 +800,7 @@ checkSigTyVars sig_tyvars free_tyvars
     checkTcM (allDistinctTyVars sig_tys globals)
             (complain sig_tys globals) `thenTc_`
 
-    returnTc (map (getTyVar "checkSigTyVars") sig_tys)
+    returnTc (map (tcGetTyVar "checkSigTyVars") sig_tys)
 
   where
     complain sig_tys globals
@@ -797,7 +816,7 @@ checkSigTyVars sig_tyvars free_tyvars
        let
            in_scope_assoc = [ (zonked_tv, in_scope_tv) 
                             | (z_ty, in_scope_tv) <- in_scope_tys `zip` in_scope_tvs,
-                              Just zonked_tv <- [getTyVar_maybe z_ty]
+                              Just zonked_tv <- [tcGetTyVar_maybe z_ty]
                             ]
            in_scope_env = mkVarEnv in_scope_assoc
        in
@@ -819,17 +838,19 @@ checkSigTyVars sig_tyvars free_tyvars
                -- ty is what you get if you zonk sig_tyvar and then tidy it
                --
                -- acc maps a zonked type variable back to a signature type variable
-         = case getTyVar_maybe ty of {
+         = case tcGetTyVar_maybe ty of {
              Nothing ->                        -- Error (a)!
-                       returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar (ppr ty) : msgs) ;
+                       returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar (quotes (ppr ty)) : msgs) ;
 
              Just tv ->
 
            case lookupVarEnv acc tv of {
                Just sig_tyvar' ->      -- Error (b) or (d)!
-                       returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar (ppr sig_tyvar') : msgs) ;
+                       returnNF_Tc (tidy_env, acc, unify_msg sig_tyvar thing : msgs)
+                   where
+                       thing = ptext SLIT("another quantified type variable") <+> quotes (ppr sig_tyvar')
 
-               Nothing ->
+             ; Nothing ->
 
            if tv `elemVarSet` globals  -- Error (c)! Type variable escapes
                                        -- The least comprehensible, so put it last
@@ -913,7 +934,7 @@ escape_msg sig_tv tv globs frees
     vcat_first 0 (x:xs) = text "...others omitted..."
     vcat_first n (x:xs) = x $$ vcat_first (n-1) xs
 
-unify_msg tv thing = mk_msg tv <+> ptext SLIT("is unified with") <+> quotes thing
+unify_msg tv thing = mk_msg tv <+> ptext SLIT("is unified with") <+> thing
 mk_msg tv          = ptext SLIT("Quantified type variable") <+> quotes (ppr tv)
 \end{code}