d20bb9149177deb7942c442ef17ce9ef26d6f9fa
[ghc-hetmet.git] / ghc / compiler / typecheck / TcMonoType.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[TcMonoType]{Typechecking user-specified @MonoTypes@}
5
6 \begin{code}
7 module TcMonoType ( tcHsType, tcHsTypeKind, tcContext, tcTyVarScope ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( HsType(..), HsTyVar(..), pprContext )
12 import RnHsSyn          ( RenamedHsType, RenamedContext )
13
14 import TcMonad
15 import TcEnv            ( tcLookupTyVar, tcLookupClass, tcLookupTyCon, tcExtendTyVarEnv )
16 import TcKind           ( TcKind, mkBoxedTypeKind, mkTypeKind, mkArrowKind,
17                           unifyKind, unifyKinds, newKindVar,
18                           kindToTcKind, tcDefaultKind
19                         )
20 import Type             ( Type, ThetaType, 
21                           mkTyVarTy, mkFunTy, mkSynTy,
22                           mkSigmaTy, mkDictTy, mkTyConApp, mkAppTys
23                         )
24 import TyVar            ( TyVar, mkTyVar )
25 import PrelInfo         ( cCallishClassKeys )
26 import TyCon            ( TyCon )
27 import Name             ( Name, OccName, isTvOcc, getOccName )
28 import TysWiredIn       ( mkListTy, mkTupleTy )
29 import Unique           ( Unique, Uniquable(..) )
30 import Util             ( zipWithEqual, zipLazy )
31 import Outputable
32 \end{code}
33
34
35 tcHsType and tcHsTypeKind
36 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38 tcHsType checks that the type really is of kind Type!
39
40 \begin{code}
41 tcHsType :: RenamedHsType -> TcM s Type
42
43 tcHsType ty
44   = tcAddErrCtxt (typeCtxt ty)          $
45     tc_hs_type ty
46
47 tc_hs_type ty
48   = tc_hs_type_kind ty                  `thenTc` \ (kind,ty) ->
49         -- Check that it really is a type
50     unifyKind mkTypeKind kind           `thenTc_`
51     returnTc ty
52 \end{code}
53
54 tcHsTypeKind does the real work.  It returns a kind and a type.
55
56 \begin{code}
57 tcHsTypeKind :: RenamedHsType -> TcM s (TcKind s, Type)
58
59 tcHsTypeKind ty
60   = tcAddErrCtxt (typeCtxt ty)          $
61     tc_hs_type_kind ty
62
63
64         -- This equation isn't needed (the next one would handle it fine)
65         -- but it's rather a common case, so we handle it directly
66 tc_hs_type_kind (MonoTyVar name)
67   | isTvOcc (getOccName name)
68   = tcLookupTyVar name                  `thenNF_Tc` \ (kind,tyvar) ->
69     returnTc (kind, mkTyVarTy tyvar)
70
71 tc_hs_type_kind ty@(MonoTyVar name)
72   = tcFunType ty []
73     
74 tc_hs_type_kind (MonoListTy _ ty)
75   = tc_hs_type ty       `thenTc` \ tau_ty ->
76     returnTc (mkBoxedTypeKind, mkListTy tau_ty)
77
78 tc_hs_type_kind (MonoTupleTy _ tys)
79   = mapTc tc_hs_type  tys       `thenTc` \ tau_tys ->
80     returnTc (mkBoxedTypeKind, mkTupleTy (length tys) tau_tys)
81
82 tc_hs_type_kind (MonoFunTy ty1 ty2)
83   = tc_hs_type ty1      `thenTc` \ tau_ty1 ->
84     tc_hs_type ty2      `thenTc` \ tau_ty2 ->
85     returnTc (mkBoxedTypeKind, mkFunTy tau_ty1 tau_ty2)
86
87 tc_hs_type_kind (MonoTyApp ty1 ty2)
88   = tcTyApp ty1 [ty2]
89
90 tc_hs_type_kind (HsForAllTy tv_names context ty)
91   = tcTyVarScope tv_names                       $ \ tyvars ->
92         tcContext context                       `thenTc` \ theta ->
93         tc_hs_type ty                           `thenTc` \ tau ->
94                 -- For-all's are of kind type!
95         returnTc (mkBoxedTypeKind, mkSigmaTy tyvars theta tau)
96
97 -- for unfoldings, and instance decls, only:
98 tc_hs_type_kind (MonoDictTy class_name tys)
99   = tcClassAssertion (class_name, tys)  `thenTc` \ (clas, arg_tys) ->
100     returnTc (mkBoxedTypeKind, mkDictTy clas arg_tys)
101 \end{code}
102
103 Help functions for type applications
104 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105 \begin{code}
106 tcTyApp (MonoTyApp ty1 ty2) tys
107   = tcTyApp ty1 (ty2:tys)
108
109 tcTyApp ty tys
110   | null tys
111   = tcFunType ty []
112
113   | otherwise
114   = mapAndUnzipTc tc_hs_type_kind tys   `thenTc` \ (arg_kinds, arg_tys) ->
115     tcFunType ty arg_tys                `thenTc` \ (fun_kind, result_ty) ->
116
117         -- Check argument compatibility
118     newKindVar                          `thenNF_Tc` \ result_kind ->
119     unifyKind fun_kind (foldr mkArrowKind result_kind arg_kinds)
120                                         `thenTc_`
121     returnTc (result_kind, result_ty)
122
123 -- (tcFunType ty arg_tys) returns (kind-of ty, mkAppTys ty arg_tys)
124 -- But not quite; for synonyms it checks the correct arity, and builds a SynTy
125 --      hence the rather strange functionality.
126
127 tcFunType (MonoTyVar name) arg_tys
128   | isTvOcc (getOccName name)   -- Must be a type variable
129   = tcLookupTyVar name                  `thenNF_Tc` \ (kind,tyvar) ->
130     returnTc (kind, mkAppTys (mkTyVarTy tyvar) arg_tys)
131
132   | otherwise                   -- Must be a type constructor
133   = tcLookupTyCon name                  `thenTc` \ (tycon_kind,maybe_arity, tycon) ->
134     case maybe_arity of
135         Nothing    ->   -- Data type or newtype 
136                       returnTc (tycon_kind, mkTyConApp tycon arg_tys)
137
138         Just arity ->   -- Type synonym
139                       checkTc (arity <= n_args) err_msg `thenTc_`
140                       returnTc (tycon_kind, result_ty)
141                    where
142                         -- It's OK to have an *over-applied* type synonym
143                         --      data Tree a b = ...
144                         --      type Foo a = Tree [a]
145                         --      f :: Foo a b -> ...
146                       result_ty = mkAppTys (mkSynTy tycon (take arity arg_tys))
147                                            (drop arity arg_tys)
148                       err_msg = arityErr "Type synonym constructor" name arity n_args
149                       n_args  = length arg_tys
150
151 tcFunType ty arg_tys
152   = tc_hs_type_kind ty          `thenTc` \ (fun_kind, fun_ty) ->
153     returnTc (fun_kind, mkAppTys fun_ty arg_tys)
154 \end{code}
155
156
157 Contexts
158 ~~~~~~~~
159 \begin{code}
160
161 tcContext :: RenamedContext -> TcM s ThetaType
162 tcContext context
163   = tcAddErrCtxt (thetaCtxt context) $
164
165         --Someone discovered that @CCallable@ and @CReturnable@
166         -- could be used in contexts such as:
167         --      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.
170         --
171         -- We *don't* do this check in tcClassAssertion, because that's
172         -- called when checking a HsDictTy, and we don't want to reject
173         --      instance CCallable Int 
174         -- etc. Ugh!
175     mapTc check_naughty context `thenTc_`
176
177     mapTc tcClassAssertion context
178
179  where
180    check_naughty (class_name, _) 
181      = checkTc (not (uniqueOf class_name `elem` cCallishClassKeys))
182                (naughtyCCallContextErr class_name)
183
184 tcClassAssertion (class_name, tys)
185   = tcLookupClass class_name            `thenTc` \ (class_kinds, clas) ->
186     mapAndUnzipTc tc_hs_type_kind tys   `thenTc` \ (ty_kinds, tc_tys) ->
187
188         -- Check with kind mis-match
189     let
190         arity = length class_kinds
191         n_tys = length ty_kinds
192         err   = arityErr "Class" class_name arity n_tys
193     in
194     checkTc (arity == n_tys) err        `thenTc_`
195     unifyKinds class_kinds ty_kinds     `thenTc_`
196
197     returnTc (clas, tc_tys)
198 \end{code}
199
200
201 Type variables, with knot tying!
202 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203 \begin{code}
204 tcTyVarScope
205         :: [HsTyVar Name]               -- Names of some type variables
206         -> ([TyVar] -> TcM s a)         -- Thing to type check in their scope
207         -> TcM s a                      -- Result
208
209 tcTyVarScope tyvar_names thing_inside
210   = mapAndUnzipNF_Tc tcHsTyVar tyvar_names      `thenNF_Tc` \ (names, kinds) ->
211
212     fixTc (\ ~(rec_tyvars, _) ->
213                 -- Ok to look at names, kinds, but not tyvars!
214
215         tcExtendTyVarEnv names (kinds `zipLazy` rec_tyvars)
216                          (thing_inside rec_tyvars)              `thenTc` \ result ->
217  
218                 -- Get the tyvar's Kinds from their TcKinds
219         mapNF_Tc tcDefaultKind kinds                            `thenNF_Tc` \ kinds' ->
220
221                 -- Construct the real TyVars
222         let
223           tyvars = zipWithEqual "tcTyVarScope" mkTyVar names kinds'
224         in
225         returnTc (tyvars, result)
226     )                                   `thenTc` \ (_,result) ->
227     returnTc result
228
229 tcHsTyVar (UserTyVar name)
230   = newKindVar          `thenNF_Tc` \ tc_kind ->
231     returnNF_Tc (name, tc_kind)
232 tcHsTyVar (IfaceTyVar name kind)
233   = returnNF_Tc (name, kindToTcKind kind)
234 \end{code}
235
236 Errors and contexts
237 ~~~~~~~~~~~~~~~~~~~
238 \begin{code}
239 naughtyCCallContextErr clas_name
240   = sep [ptext SLIT("Can't use class"), quotes (ppr clas_name), ptext SLIT("in a context")]
241
242 typeCtxt ty = ptext SLIT("In the type") <+> quotes (ppr ty)
243
244 thetaCtxt theta = ptext SLIT("In the context") <+> quotes (pprContext theta)
245 \end{code}