2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcMonoType]{Typechecking user-specified @MonoTypes@}
7 #include "HsVersions.h"
9 module TcMonoType ( tcPolyType, tcMonoType, tcMonoTypeKind, tcContext ) where
13 import HsSyn ( PolyType(..), MonoType(..), Fake )
14 import RnHsSyn ( RenamedPolyType(..), RenamedMonoType(..),
15 RenamedContext(..), RnName(..),
16 isRnLocal, isRnClass, isRnTyCon
19 import TcMonad hiding ( rnMtoTcM )
20 import TcEnv ( tcLookupTyVar, tcLookupClass, tcLookupTyCon,
21 tcTyVarScope, tcTyVarScopeGivenKinds
23 import TcKind ( TcKind, mkTcTypeKind, mkBoxedTypeKind,
24 mkTcArrowKind, unifyKind, newKindVar,
27 import Type ( GenType, SYN_IE(Type), SYN_IE(ThetaType),
28 mkTyVarTy, mkTyConTy, mkFunTy, mkAppTy, mkSynTy,
31 import TyVar ( GenTyVar, SYN_IE(TyVar) )
32 import Class ( cCallishClassKeys )
33 import TyCon ( TyCon )
34 import TysWiredIn ( mkListTy, mkTupleTy )
35 import Unique ( Unique )
38 import Util ( zipWithEqual, panic{-, pprPanic ToDo:rm-} )
42 tcMonoType and tcMonoTypeKind
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 tcMonoType checks that the type really is of kind Type!
48 tcMonoType :: RenamedMonoType -> TcM s Type
51 = tcMonoTypeKind ty `thenTc` \ (kind,ty) ->
52 unifyKind kind mkTcTypeKind `thenTc_`
56 tcMonoTypeKind does the real work. It returns a kind and a type.
59 tcMonoTypeKind :: RenamedMonoType -> TcM s (TcKind s, Type)
61 tcMonoTypeKind (MonoTyVar name)
62 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
63 returnTc (kind, mkTyVarTy tyvar)
66 tcMonoTypeKind (MonoListTy ty)
67 = tcMonoType ty `thenTc` \ tau_ty ->
68 returnTc (mkTcTypeKind, mkListTy tau_ty)
70 tcMonoTypeKind (MonoTupleTy tys)
71 = mapTc tcMonoType tys `thenTc` \ tau_tys ->
72 returnTc (mkTcTypeKind, mkTupleTy (length tys) tau_tys)
74 tcMonoTypeKind (MonoFunTy ty1 ty2)
75 = tcMonoType ty1 `thenTc` \ tau_ty1 ->
76 tcMonoType ty2 `thenTc` \ tau_ty2 ->
77 returnTc (mkTcTypeKind, mkFunTy tau_ty1 tau_ty2)
79 tcMonoTypeKind (MonoTyApp name tys)
80 | isRnLocal name -- Must be a type variable
81 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
82 tcMonoTyApp kind (mkTyVarTy tyvar) tys
84 | otherwise {-isRnTyCon name-} -- Must be a type constructor
85 = tcLookupTyCon name `thenNF_Tc` \ (kind,maybe_arity,tycon) ->
87 Just arity -> tcSynApp name kind arity tycon tys -- synonum
88 Nothing -> tcMonoTyApp kind (mkTyConTy tycon) tys -- newtype or data
91 -- = pprPanic "tcMonoTypeKind:" (ppr PprDebug name)
93 -- for unfoldings only:
94 tcMonoTypeKind (MonoForAllTy tyvars_w_kinds ty)
95 = tcTyVarScopeGivenKinds names tc_kinds (\ tyvars ->
96 tcMonoTypeKind ty `thenTc` \ (kind, ty') ->
97 unifyKind kind mkTcTypeKind `thenTc_`
98 returnTc (mkTcTypeKind, ty')
101 (rn_names, kinds) = unzip tyvars_w_kinds
102 names = map de_rn rn_names
103 tc_kinds = map kindToTcKind kinds
106 -- for unfoldings only:
107 tcMonoTypeKind (MonoDictTy class_name ty)
108 = tcMonoTypeKind ty `thenTc` \ (arg_kind, arg_ty) ->
109 tcLookupClass class_name `thenNF_Tc` \ (class_kind, clas) ->
110 unifyKind class_kind arg_kind `thenTc_`
111 returnTc (mkTcTypeKind, mkDictTy clas arg_ty)
114 Help functions for type applications
115 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117 tcMonoTyApp fun_kind fun_ty tys
118 = mapAndUnzipTc tcMonoTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
119 newKindVar `thenNF_Tc` \ result_kind ->
120 unifyKind fun_kind (foldr mkTcArrowKind result_kind arg_kinds) `thenTc_`
121 returnTc (result_kind, foldl mkAppTy fun_ty arg_tys)
123 tcSynApp name syn_kind arity tycon tys
124 = mapAndUnzipTc tcMonoTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
125 newKindVar `thenNF_Tc` \ result_kind ->
126 unifyKind syn_kind (foldr mkTcArrowKind result_kind arg_kinds) `thenTc_`
128 -- Check that it's applied to the right number of arguments
129 checkTc (arity == n_args) (err arity) `thenTc_`
130 returnTc (result_kind, mkSynTy tycon arg_tys)
132 err arity = arityErr "Type synonym constructor" name arity n_args
141 tcContext :: RenamedContext -> TcM s ThetaType
142 tcContext context = mapTc tcClassAssertion context
144 tcClassAssertion (class_name, tyvar_name)
145 = checkTc (canBeUsedInContext class_name)
146 (naughtyCCallContextErr class_name) `thenTc_`
148 tcLookupClass class_name `thenNF_Tc` \ (class_kind, clas) ->
149 tcLookupTyVar tyvar_name `thenNF_Tc` \ (tyvar_kind, tyvar) ->
151 unifyKind class_kind tyvar_kind `thenTc_`
153 returnTc (clas, mkTyVarTy tyvar)
156 HACK warning: Someone discovered that @CCallable@ and @CReturnable@
157 could be used in contexts such as:
159 foo :: CCallable a => a -> PrimIO Int
162 Doing this utterly wrecks the whole point of introducing these
163 classes so we specifically check that this isn't being done.
166 canBeUsedInContext :: RnName -> Bool
168 = isRnClass n && not (uniqueOf n `elem` cCallishClassKeys)
174 tcPolyType :: RenamedPolyType -> TcM s Type
175 tcPolyType (HsForAllTy tyvar_names context ty)
176 = tcTyVarScope names (\ tyvars ->
177 tcContext context `thenTc` \ theta ->
178 tcMonoType ty `thenTc` \ tau ->
179 returnTc (mkSigmaTy tyvars theta tau)
182 names = map de_rn tyvar_names
189 naughtyCCallContextErr clas_name sty
190 = ppSep [ppStr "Can't use class", ppr sty clas_name, ppStr "in a context"]