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, Uniquable(..) )
34 import Util ( zipWithEqual, zipLazy, panic{-, pprPanic ToDo:rm-} )
41 tcHsType and tcHsTypeKind
42 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 tcHsType checks that the type really is of kind Type!
47 tcHsType :: RenamedHsType -> TcM s Type
50 = tcHsTypeKind ty `thenTc` \ (kind,ty) ->
51 unifyKind kind mkTcTypeKind `thenTc_`
55 tcHsTypeKind does the real work. It returns a kind and a type.
58 tcHsTypeKind :: RenamedHsType -> TcM s (TcKind s, Type)
60 -- This equation isn't needed (the next one would handle it fine)
61 -- but it's rather a common case, so we handle it directly
62 tcHsTypeKind (MonoTyVar name)
63 | isTvOcc (getOccName name)
64 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
65 returnTc (kind, mkTyVarTy tyvar)
67 tcHsTypeKind ty@(MonoTyVar name)
70 tcHsTypeKind (MonoListTy _ ty)
71 = tcHsType ty `thenTc` \ tau_ty ->
72 returnTc (mkTcTypeKind, mkListTy tau_ty)
74 tcHsTypeKind (MonoTupleTy _ tys)
75 = mapTc tcHsType tys `thenTc` \ tau_tys ->
76 returnTc (mkTcTypeKind, mkTupleTy (length tys) tau_tys)
78 tcHsTypeKind (MonoFunTy ty1 ty2)
79 = tcHsType ty1 `thenTc` \ tau_ty1 ->
80 tcHsType ty2 `thenTc` \ tau_ty2 ->
81 returnTc (mkTcTypeKind, mkFunTy tau_ty1 tau_ty2)
83 tcHsTypeKind (MonoTyApp ty1 ty2)
86 tcHsTypeKind (HsForAllTy tv_names context ty)
87 = tcTyVarScope tv_names $ \ tyvars ->
88 tcContext context `thenTc` \ theta ->
89 tcHsType ty `thenTc` \ tau ->
90 -- For-all's are of kind type!
91 returnTc (mkTcTypeKind, mkSigmaTy tyvars theta tau)
93 -- for unfoldings only:
94 tcHsTypeKind (MonoDictTy class_name ty)
95 = tcHsTypeKind ty `thenTc` \ (arg_kind, arg_ty) ->
96 tcLookupClass class_name `thenTc` \ (class_kind, clas) ->
97 unifyKind class_kind arg_kind `thenTc_`
98 returnTc (mkTcTypeKind, mkDictTy clas arg_ty)
101 Help functions for type applications
102 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104 tcTyApp (MonoTyApp ty1 ty2) tys
105 = tcTyApp ty1 (ty2:tys)
112 = mapAndUnzipTc tcHsTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
113 tcFunType ty arg_tys `thenTc` \ (fun_kind, result_ty) ->
115 -- Check argument compatibility; special ca
116 newKindVar `thenNF_Tc` \ result_kind ->
117 unifyKind fun_kind (foldr mkTcArrowKind result_kind arg_kinds)
119 returnTc (result_kind, result_ty)
121 tcFunType (MonoTyVar name) arg_tys
122 | isTvOcc (getOccName name) -- Must be a type variable
123 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
124 returnTc (kind, foldl mkAppTy (mkTyVarTy tyvar) arg_tys)
126 | otherwise -- Must be a type constructor
127 = tcLookupTyCon name `thenTc` \ (kind,maybe_arity,tycon) ->
129 Nothing -> returnTc (kind, foldl mkAppTy (mkTyConTy tycon) arg_tys)
130 Just arity -> checkTc (arity == n_args) (err arity) `thenTc_`
131 returnTc (kind, mkSynTy tycon arg_tys)
133 err arity = arityErr "Type synonym constructor" name arity n_args
134 n_args = length arg_tys
137 = tcHsTypeKind ty `thenTc` \ (fun_kind, fun_ty) ->
138 returnTc (fun_kind, foldl mkAppTy fun_ty arg_tys)
146 tcContext :: RenamedContext -> TcM s ThetaType
147 tcContext context = mapTc tcClassAssertion context
149 tcClassAssertion (class_name, ty)
150 = checkTc (canBeUsedInContext class_name)
151 (naughtyCCallContextErr class_name) `thenTc_`
153 tcLookupClass class_name `thenTc` \ (class_kind, clas) ->
154 tcHsTypeKind ty `thenTc` \ (ty_kind, ty) ->
156 unifyKind class_kind ty_kind `thenTc_`
161 HACK warning: Someone discovered that @CCallable@ and @CReturnable@
162 could be used in contexts such as:
164 foo :: CCallable a => a -> PrimIO Int
167 Doing this utterly wrecks the whole point of introducing these
168 classes so we specifically check that this isn't being done.
171 canBeUsedInContext :: Name -> Bool
172 canBeUsedInContext n = not (uniqueOf n `elem` cCallishClassKeys)
175 Type variables, with knot tying!
176 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179 :: [HsTyVar Name] -- Names of some type variables
180 -> ([TyVar] -> TcM s a) -- Thing to type check in their scope
183 tcTyVarScope tyvar_names thing_inside
184 = mapAndUnzipNF_Tc tcHsTyVar tyvar_names `thenNF_Tc` \ (names, kinds) ->
186 fixTc (\ ~(rec_tyvars, _) ->
187 -- Ok to look at names, kinds, but not tyvars!
189 tcExtendTyVarEnv names (kinds `zipLazy` rec_tyvars)
190 (thing_inside rec_tyvars) `thenTc` \ result ->
192 -- Get the tyvar's Kinds from their TcKinds
193 mapNF_Tc tcDefaultKind kinds `thenNF_Tc` \ kinds' ->
195 -- Construct the real TyVars
197 tyvars = zipWithEqual "tcTyVarScope" mkTyVar names kinds'
199 returnTc (tyvars, result)
200 ) `thenTc` \ (_,result) ->
203 tcHsTyVar (UserTyVar name)
204 = newKindVar `thenNF_Tc` \ tc_kind ->
205 returnNF_Tc (name, tc_kind)
206 tcHsTyVar (IfaceTyVar name kind)
207 = returnNF_Tc (name, kindToTcKind kind)
213 naughtyCCallContextErr clas_name sty
214 = sep [ptext SLIT("Can't use class"), ppr sty clas_name, ptext SLIT("in a context")]