2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcMonoType]{Typechecking user-specified @MonoTypes@}
7 #include "HsVersions.h"
9 module TcMonoType ( tcHsType, tcHsTypeKind, tcContext, tcTyVarScope ) where
13 import HsSyn ( HsType(..), HsTyVar(..), Fake )
14 import RnHsSyn ( RenamedHsType(..), RenamedContext(..) )
17 import TcEnv ( tcLookupTyVar, tcLookupClass, tcLookupTyCon, tcExtendTyVarEnv )
18 import TcKind ( TcKind, mkTcTypeKind, mkBoxedTypeKind,
19 mkTcArrowKind, unifyKind, newKindVar,
20 kindToTcKind, tcDefaultKind
22 import Type ( GenType, SYN_IE(Type), SYN_IE(ThetaType),
23 mkTyVarTy, mkTyConTy, mkFunTy, mkAppTy, mkSynTy,
26 import TyVar ( GenTyVar, SYN_IE(TyVar), mkTyVar )
28 import PrelInfo ( cCallishClassKeys )
29 import TyCon ( TyCon )
30 import Name ( Name, OccName, isTvOcc, getOccName )
31 import TysWiredIn ( mkListTy, mkTupleTy )
32 import Unique ( Unique )
34 import UniqFM ( Uniquable(..) )
35 import Util ( zipWithEqual, zipLazy, panic{-, pprPanic ToDo:rm-} )
42 tcHsType and tcHsTypeKind
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 tcHsType checks that the type really is of kind Type!
48 tcHsType :: RenamedHsType -> TcM s Type
51 = tcHsTypeKind ty `thenTc` \ (kind,ty) ->
52 unifyKind kind mkTcTypeKind `thenTc_`
56 tcHsTypeKind does the real work. It returns a kind and a type.
59 tcHsTypeKind :: RenamedHsType -> TcM s (TcKind s, Type)
61 -- This equation isn't needed (the next one would handle it fine)
62 -- but it's rather a common case, so we handle it directly
63 tcHsTypeKind (MonoTyVar name)
64 | isTvOcc (getOccName name)
65 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
66 returnTc (kind, mkTyVarTy tyvar)
68 tcHsTypeKind ty@(MonoTyVar name)
71 tcHsTypeKind (MonoListTy _ ty)
72 = tcHsType ty `thenTc` \ tau_ty ->
73 returnTc (mkTcTypeKind, mkListTy tau_ty)
75 tcHsTypeKind (MonoTupleTy _ tys)
76 = mapTc tcHsType tys `thenTc` \ tau_tys ->
77 returnTc (mkTcTypeKind, mkTupleTy (length tys) tau_tys)
79 tcHsTypeKind (MonoFunTy ty1 ty2)
80 = tcHsType ty1 `thenTc` \ tau_ty1 ->
81 tcHsType ty2 `thenTc` \ tau_ty2 ->
82 returnTc (mkTcTypeKind, mkFunTy tau_ty1 tau_ty2)
84 tcHsTypeKind (MonoTyApp ty1 ty2)
87 tcHsTypeKind (HsForAllTy tv_names context ty)
88 = tcTyVarScope tv_names $ \ tyvars ->
89 tcContext context `thenTc` \ theta ->
90 tcHsType ty `thenTc` \ tau ->
91 -- For-all's are of kind type!
92 returnTc (mkTcTypeKind, mkSigmaTy tyvars theta tau)
94 -- for unfoldings only:
95 tcHsTypeKind (MonoDictTy class_name ty)
96 = tcHsTypeKind ty `thenTc` \ (arg_kind, arg_ty) ->
97 tcLookupClass class_name `thenTc` \ (class_kind, clas) ->
98 unifyKind class_kind arg_kind `thenTc_`
99 returnTc (mkTcTypeKind, mkDictTy clas arg_ty)
102 Help functions for type applications
103 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105 tcTyApp (MonoTyApp ty1 ty2) tys
106 = tcTyApp ty1 (ty2:tys)
113 = mapAndUnzipTc tcHsTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
114 tcFunType ty arg_tys `thenTc` \ (fun_kind, result_ty) ->
116 -- Check argument compatibility; special ca
117 newKindVar `thenNF_Tc` \ result_kind ->
118 unifyKind fun_kind (foldr mkTcArrowKind result_kind arg_kinds)
120 returnTc (result_kind, result_ty)
122 tcFunType (MonoTyVar name) arg_tys
123 | isTvOcc (getOccName name) -- Must be a type variable
124 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
125 returnTc (kind, foldl mkAppTy (mkTyVarTy tyvar) arg_tys)
127 | otherwise -- Must be a type constructor
128 = tcLookupTyCon name `thenTc` \ (kind,maybe_arity,tycon) ->
130 Nothing -> returnTc (kind, foldl mkAppTy (mkTyConTy tycon) arg_tys)
131 Just arity -> checkTc (arity == n_args) (err arity) `thenTc_`
132 returnTc (kind, mkSynTy tycon arg_tys)
134 err arity = arityErr "Type synonym constructor" name arity n_args
135 n_args = length arg_tys
138 = tcHsTypeKind ty `thenTc` \ (fun_kind, fun_ty) ->
139 returnTc (fun_kind, foldl mkAppTy fun_ty arg_tys)
147 tcContext :: RenamedContext -> TcM s ThetaType
148 tcContext context = mapTc tcClassAssertion context
150 tcClassAssertion (class_name, ty)
151 = checkTc (canBeUsedInContext class_name)
152 (naughtyCCallContextErr class_name) `thenTc_`
154 tcLookupClass class_name `thenTc` \ (class_kind, clas) ->
155 tcHsTypeKind ty `thenTc` \ (ty_kind, ty) ->
157 unifyKind class_kind ty_kind `thenTc_`
162 HACK warning: Someone discovered that @CCallable@ and @CReturnable@
163 could be used in contexts such as:
165 foo :: CCallable a => a -> PrimIO Int
168 Doing this utterly wrecks the whole point of introducing these
169 classes so we specifically check that this isn't being done.
172 canBeUsedInContext :: Name -> Bool
173 canBeUsedInContext n = not (uniqueOf n `elem` cCallishClassKeys)
176 Type variables, with knot tying!
177 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180 :: [HsTyVar Name] -- Names of some type variables
181 -> ([TyVar] -> TcM s a) -- Thing to type check in their scope
184 tcTyVarScope tyvar_names thing_inside
185 = mapAndUnzipNF_Tc tcHsTyVar tyvar_names `thenNF_Tc` \ (names, kinds) ->
187 fixTc (\ ~(rec_tyvars, _) ->
188 -- Ok to look at names, kinds, but not tyvars!
190 tcExtendTyVarEnv names (kinds `zipLazy` rec_tyvars)
191 (thing_inside rec_tyvars) `thenTc` \ result ->
193 -- Get the tyvar's Kinds from their TcKinds
194 mapNF_Tc tcDefaultKind kinds `thenNF_Tc` \ kinds' ->
196 -- Construct the real TyVars
198 tyvars = zipWithEqual "tcTyVarScope" mkTyVar names kinds'
200 returnTc (tyvars, result)
201 ) `thenTc` \ (_,result) ->
204 tcHsTyVar (UserTyVar name)
205 = newKindVar `thenNF_Tc` \ tc_kind ->
206 returnNF_Tc (name, tc_kind)
207 tcHsTyVar (IfaceTyVar name kind)
208 = returnNF_Tc (name, kindToTcKind kind)
214 naughtyCCallContextErr clas_name sty
215 = sep [ptext SLIT("Can't use class"), ppr sty clas_name, ptext SLIT("in a context")]