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(..),
20 import TcEnv ( tcLookupTyVar, tcLookupClass, tcLookupTyCon,
21 tcTyVarScope, tcTyVarScopeGivenKinds
23 import TcKind ( TcKind, mkTcTypeKind, mkBoxedTypeKind,
24 mkTcArrowKind, unifyKind, newKindVar,
27 import ErrUtils ( arityErr )
28 import Type ( GenType, Type(..), ThetaType(..),
29 mkTyVarTy, mkTyConTy, mkFunTy, mkAppTy,
32 import TyVar ( GenTyVar, TyVar(..), mkTyVar )
33 import PrelInfo ( mkListTy, mkTupleTy )
34 import Type ( mkDictTy )
35 import Class ( cCallishClassKeys )
36 import TyCon ( TyCon, Arity(..) )
37 import Unique ( Unique )
38 import Name ( Name(..), getNameShortName, isTyConName, getSynNameArity )
41 import Util ( zipWithEqual, panic )
45 tcMonoType and tcMonoTypeKind
46 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 tcMonoType checks that the type really is of kind Type!
51 tcMonoType :: RenamedMonoType -> TcM s Type
54 = tcMonoTypeKind ty `thenTc` \ (kind,ty) ->
55 unifyKind kind mkTcTypeKind `thenTc_`
59 tcMonoTypeKind does the real work. It returns a kind and a type.
62 tcMonoTypeKind :: RenamedMonoType -> TcM s (TcKind s, Type)
64 tcMonoTypeKind (MonoTyVar name)
65 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
66 returnTc (kind, mkTyVarTy tyvar)
69 tcMonoTypeKind (MonoListTy ty)
70 = tcMonoType ty `thenTc` \ tau_ty ->
71 returnTc (mkTcTypeKind, mkListTy tau_ty)
73 tcMonoTypeKind (MonoTupleTy tys)
74 = mapTc tcMonoType tys `thenTc` \ tau_tys ->
75 returnTc (mkTcTypeKind, mkTupleTy (length tys) tau_tys)
77 tcMonoTypeKind (MonoFunTy ty1 ty2)
78 = tcMonoType ty1 `thenTc` \ tau_ty1 ->
79 tcMonoType ty2 `thenTc` \ tau_ty2 ->
80 returnTc (mkTcTypeKind, mkFunTy tau_ty1 tau_ty2)
82 tcMonoTypeKind (MonoTyApp name tys)
83 = mapAndUnzipTc tcMonoTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
85 tc_mono_name name `thenNF_Tc` \ (fun_kind, maybe_arity, fun_ty) ->
87 newKindVar `thenNF_Tc` \ result_kind ->
88 unifyKind fun_kind (foldr mkTcArrowKind result_kind arg_kinds) `thenTc_`
90 -- Check for saturated application in the special case of
93 Just arity | arity /= n_args -> failTc (err arity)
97 returnTc (result_kind, foldl mkAppTy fun_ty arg_tys)
99 err arity = arityErr "Type synonym constructor" name arity n_args
102 -- for unfoldings only:
103 tcMonoTypeKind (MonoForAllTy tyvars_w_kinds ty)
104 = tcTyVarScopeGivenKinds names tc_kinds (\ tyvars ->
105 tcMonoTypeKind ty `thenTc` \ (kind, ty') ->
106 unifyKind kind mkTcTypeKind `thenTc_`
107 returnTc (mkTcTypeKind, ty')
110 (names, kinds) = unzip tyvars_w_kinds
111 tc_kinds = map kindToTcKind kinds
113 -- for unfoldings only:
114 tcMonoTypeKind (MonoDictTy class_name ty)
115 = tcMonoTypeKind ty `thenTc` \ (arg_kind, arg_ty) ->
116 tcLookupClass class_name `thenNF_Tc` \ (class_kind, clas) ->
117 unifyKind class_kind arg_kind `thenTc_`
118 returnTc (mkTcTypeKind, mkDictTy clas arg_ty)
121 tc_mono_name :: Name -> NF_TcM s (TcKind s, Maybe Arity, Type)
122 tc_mono_name name@(Short _ _) -- Must be a type variable
123 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
124 returnNF_Tc (kind, Nothing, mkTyVarTy tyvar)
126 tc_mono_name name | isTyConName name -- Must be a type constructor
127 = tcLookupTyCon name `thenNF_Tc` \ (kind,maybe_arity,tycon) ->
128 returnNF_Tc (kind, maybe_arity, mkTyConTy tycon)
130 tc_mono_name name -- Renamer should have got it right
131 = panic ("tc_mono_name:" ++ ppShow 1000 (ppr PprDebug name))
139 tcContext :: RenamedContext -> TcM s ThetaType
140 tcContext context = mapTc tcClassAssertion context
142 tcClassAssertion (class_name, tyvar_name)
143 = checkTc (canBeUsedInContext class_name)
144 (naughtyCCallContextErr class_name) `thenTc_`
146 tcLookupClass class_name `thenNF_Tc` \ (class_kind, clas) ->
147 tcLookupTyVar tyvar_name `thenNF_Tc` \ (tyvar_kind, tyvar) ->
149 unifyKind class_kind tyvar_kind `thenTc_`
151 returnTc (clas, mkTyVarTy tyvar)
154 HACK warning: Someone discovered that @_CCallable@ and @_CReturnable@
155 could be used in contexts such as:
157 foo :: _CCallable a => a -> PrimIO Int
160 Doing this utterly wrecks the whole point of introducing these
161 classes so we specifically check that this isn't being done.
164 canBeUsedInContext :: Name -> Bool
165 canBeUsedInContext (ClassName uniq _ _) = not (uniq `elem` cCallishClassKeys)
166 canBeUsedInContext other = True
173 tcPolyType :: RenamedPolyType -> TcM s Type
174 tcPolyType (HsForAllTy tyvar_names context ty)
175 = tcTyVarScope tyvar_names (\ tyvars ->
176 tcContext context `thenTc` \ theta ->
177 tcMonoType ty `thenTc` \ tau ->
178 returnTc (mkSigmaTy tyvars theta tau)
185 naughtyCCallContextErr clas_name sty
186 = ppSep [ppStr "Can't use class", ppr sty clas_name, ppStr "in a context"]