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 Type ( GenType, Type(..), ThetaType(..),
28 mkTyVarTy, mkTyConTy, mkFunTy, mkAppTy, mkSynTy,
31 import TyVar ( GenTyVar, TyVar(..), mkTyVar )
32 import PrelInfo ( mkListTy, mkTupleTy )
33 import Type ( mkDictTy )
34 import Class ( cCallishClassKeys )
35 import TyCon ( TyCon, Arity(..) )
36 import Unique ( Unique )
37 import Name ( Name(..), getNameShortName, isTyConName, getSynNameArity )
40 import Util ( zipWithEqual, panic )
44 tcMonoType and tcMonoTypeKind
45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47 tcMonoType checks that the type really is of kind Type!
50 tcMonoType :: RenamedMonoType -> TcM s Type
53 = tcMonoTypeKind ty `thenTc` \ (kind,ty) ->
54 unifyKind kind mkTcTypeKind `thenTc_`
58 tcMonoTypeKind does the real work. It returns a kind and a type.
61 tcMonoTypeKind :: RenamedMonoType -> TcM s (TcKind s, Type)
63 tcMonoTypeKind (MonoTyVar name)
64 = tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
65 returnTc (kind, mkTyVarTy tyvar)
68 tcMonoTypeKind (MonoListTy ty)
69 = tcMonoType ty `thenTc` \ tau_ty ->
70 returnTc (mkTcTypeKind, mkListTy tau_ty)
72 tcMonoTypeKind (MonoTupleTy tys)
73 = mapTc tcMonoType tys `thenTc` \ tau_tys ->
74 returnTc (mkTcTypeKind, mkTupleTy (length tys) tau_tys)
76 tcMonoTypeKind (MonoFunTy ty1 ty2)
77 = tcMonoType ty1 `thenTc` \ tau_ty1 ->
78 tcMonoType ty2 `thenTc` \ tau_ty2 ->
79 returnTc (mkTcTypeKind, mkFunTy tau_ty1 tau_ty2)
81 tcMonoTypeKind (MonoTyApp name@(Short _ _) tys)
82 = -- Must be a type variable
83 tcLookupTyVar name `thenNF_Tc` \ (kind,tyvar) ->
84 tcMonoTyApp kind (mkTyVarTy tyvar) tys
86 tcMonoTypeKind (MonoTyApp name tys)
87 | isTyConName name -- Must be a type constructor
88 = tcLookupTyCon name `thenNF_Tc` \ (kind,maybe_arity,tycon) ->
90 Just arity -> tcSynApp name kind arity tycon tys -- synonum
91 Nothing -> tcMonoTyApp kind (mkTyConTy tycon) tys -- newtype or data
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 (names, kinds) = unzip tyvars_w_kinds
102 tc_kinds = map kindToTcKind kinds
104 -- for unfoldings only:
105 tcMonoTypeKind (MonoDictTy class_name ty)
106 = tcMonoTypeKind ty `thenTc` \ (arg_kind, arg_ty) ->
107 tcLookupClass class_name `thenNF_Tc` \ (class_kind, clas) ->
108 unifyKind class_kind arg_kind `thenTc_`
109 returnTc (mkTcTypeKind, mkDictTy clas arg_ty)
112 Help functions for type applications
113 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115 tcMonoTyApp fun_kind fun_ty tys
116 = mapAndUnzipTc tcMonoTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
117 newKindVar `thenNF_Tc` \ result_kind ->
118 unifyKind fun_kind (foldr mkTcArrowKind result_kind arg_kinds) `thenTc_`
119 returnTc (result_kind, foldl mkAppTy fun_ty arg_tys)
121 tcSynApp name syn_kind arity tycon tys
122 = mapAndUnzipTc tcMonoTypeKind tys `thenTc` \ (arg_kinds, arg_tys) ->
123 newKindVar `thenNF_Tc` \ result_kind ->
124 unifyKind syn_kind (foldr mkTcArrowKind result_kind arg_kinds) `thenTc_`
126 -- Check that it's applied to the right number of arguments
127 checkTc (arity == n_args) (err arity) `thenTc_`
128 returnTc (result_kind, mkSynTy tycon arg_tys)
130 err arity = arityErr "Type synonym constructor" name arity n_args
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"]