[project @ 2000-07-11 16:24:57 by simonmar]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcType.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TcType]{Types used in the typechecker}
5
6 \begin{code}
7 module TcType (
8   
9   TcTyVar,
10   TcTyVarSet,
11   newTyVar,
12   newTyVarTy,           -- Kind -> NF_TcM s TcType
13   newTyVarTys,          -- Int -> Kind -> NF_TcM s [TcType]
14
15   newTyVarTy_OpenKind,  -- NF_TcM s TcType
16   newOpenTypeKind,      -- NF_TcM s TcKind
17
18   -----------------------------------------
19   TcType, TcTauType, TcThetaType, TcRhoType,
20
21         -- Find the type to which a type variable is bound
22   tcPutTyVar,           -- :: TcTyVar -> TcType -> NF_TcM TcType
23   tcGetTyVar,           -- :: TcTyVar -> NF_TcM (Maybe TcType)  does shorting out
24
25
26   tcSplitRhoTy,
27
28   tcInstTyVars,
29   tcInstSigVar,
30   tcInstTcType,
31
32   typeToTcType,
33
34   tcTypeKind,           -- :: TcType -> NF_TcM s TcKind
35   --------------------------------
36   TcKind,
37   newKindVar, newKindVars,
38   kindToTcKind,
39   zonkTcKind,
40
41   --------------------------------
42   zonkTcTyVar, zonkTcTyVars, zonkTcTyVarBndr,
43   zonkTcType, zonkTcTypes, zonkTcClassConstraints, zonkTcThetaType,
44
45   zonkTcTypeToType, zonkTcTyVarToTyVar,
46   zonkTcKindToKind
47
48   ) where
49
50 #include "HsVersions.h"
51
52
53 -- friends:
54 import TypeRep          ( Type(..), Kind, TyNote(..), 
55                           typeCon, openTypeKind, boxedTypeKind, boxedKind, superKind, superBoxity
56                         )  -- friend
57 import Type             ( ThetaType, PredType(..),
58                           mkAppTy, mkTyConApp,
59                           splitPredTy_maybe, splitForAllTys, isNotUsgTy,
60                           isTyVarTy, mkTyVarTy, mkTyVarTys, 
61                         )
62 import Subst            ( Subst, mkTopTyVarSubst, substTy )
63 import TyCon            ( tyConKind, mkPrimTyCon )
64 import PrimRep          ( PrimRep(VoidRep) )
65 import Var              ( TyVar, tyVarKind, tyVarName, isTyVar, isMutTyVar, mkTyVar )
66
67 -- others:
68 import TcMonad
69 import TysWiredIn       ( voidTy )
70
71 import Name             ( NamedThing(..), setNameUnique, mkSysLocalName,
72                           mkDerivedName, mkDerivedTyConOcc
73                         )
74 import Unique           ( Unique, Uniquable(..) )
75 import Util             ( nOfThem )
76 import Outputable
77 \end{code}
78
79
80
81 Coercions
82 ~~~~~~~~~~
83 Type definitions are in TcMonad.lhs
84
85 \begin{code}
86 typeToTcType :: Type -> TcType
87 typeToTcType ty =  ty
88
89 kindToTcKind :: Kind -> TcKind
90 kindToTcKind kind = kind
91 \end{code}
92
93 Utility functions
94 ~~~~~~~~~~~~~~~~~
95 These tcSplit functions are like their non-Tc analogues, but they
96 follow through bound type variables.
97
98 No need for tcSplitForAllTy because a type variable can't be instantiated
99 to a for-all type.
100
101 \begin{code}
102 tcSplitRhoTy :: TcType -> NF_TcM s (TcThetaType, TcType)
103 tcSplitRhoTy t
104   = go t t []
105  where
106         -- A type variable is never instantiated to a dictionary type,
107         -- so we don't need to do a tcReadVar on the "arg".
108     go syn_t (FunTy arg res) ts = case splitPredTy_maybe arg of
109                                         Just pair -> go res res (pair:ts)
110                                         Nothing   -> returnNF_Tc (reverse ts, syn_t)
111     go syn_t (NoteTy _ t)    ts = go syn_t t ts
112     go syn_t (TyVarTy tv)    ts = tcGetTyVar tv         `thenNF_Tc` \ maybe_ty ->
113                                   case maybe_ty of
114                                     Just ty | not (isTyVarTy ty) -> go syn_t ty ts
115                                     other                        -> returnNF_Tc (reverse ts, syn_t)
116     go syn_t t               ts = returnNF_Tc (reverse ts, syn_t)
117 \end{code}
118
119
120 %************************************************************************
121 %*                                                                      *
122 \subsection{New type variables}
123 %*                                                                      *
124 %************************************************************************
125
126 \begin{code}
127 newTyVar :: Kind -> NF_TcM s TcTyVar
128 newTyVar kind
129   = tcGetUnique         `thenNF_Tc` \ uniq ->
130     tcNewMutTyVar (mkSysLocalName uniq SLIT("t")) kind
131
132 newTyVarTy  :: Kind -> NF_TcM s TcType
133 newTyVarTy kind
134   = newTyVar kind       `thenNF_Tc` \ tc_tyvar ->
135     returnNF_Tc (TyVarTy tc_tyvar)
136
137 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType]
138 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
139
140 newKindVar :: NF_TcM s TcKind
141 newKindVar
142   = tcGetUnique                                                 `thenNF_Tc` \ uniq ->
143     tcNewMutTyVar (mkSysLocalName uniq SLIT("k")) superKind     `thenNF_Tc` \ kv ->
144     returnNF_Tc (TyVarTy kv)
145
146 newKindVars :: Int -> NF_TcM s [TcKind]
147 newKindVars n = mapNF_Tc (\ _ -> newKindVar) (nOfThem n ())
148
149 -- Returns a type variable of kind (Type bv) where bv is a new boxity var
150 -- Used when you need a type variable that's definitely a , but you don't know
151 -- what kind of type (boxed or unboxed).
152 newTyVarTy_OpenKind :: NF_TcM s TcType
153 newTyVarTy_OpenKind = newOpenTypeKind   `thenNF_Tc` \ kind -> 
154                       newTyVarTy kind
155
156 newOpenTypeKind :: NF_TcM s TcKind
157 newOpenTypeKind = newTyVarTy superBoxity        `thenNF_Tc` \ bv ->
158                   returnNF_Tc (mkTyConApp typeCon [bv])
159 \end{code}
160
161
162 %************************************************************************
163 %*                                                                      *
164 \subsection{Type instantiation}
165 %*                                                                      *
166 %************************************************************************
167
168 Instantiating a bunch of type variables
169
170 \begin{code}
171 tcInstTyVars :: [TyVar] 
172              -> NF_TcM s ([TcTyVar], [TcType], Subst)
173
174 tcInstTyVars tyvars
175   = mapNF_Tc tcInstTyVar tyvars `thenNF_Tc` \ tc_tyvars ->
176     let
177         tys = mkTyVarTys tc_tyvars
178     in
179     returnNF_Tc (tc_tyvars, tys, mkTopTyVarSubst tyvars tys)
180                 -- Since the tyvars are freshly made,
181                 -- they cannot possibly be captured by
182                 -- any existing for-alls.  Hence mkTopTyVarSubst
183
184 tcInstTyVar tyvar
185   = tcGetUnique                 `thenNF_Tc` \ uniq ->
186     let
187         name = setNameUnique (tyVarName tyvar) uniq
188         -- Note that we don't change the print-name
189         -- This won't confuse the type checker but there's a chance
190         -- that two different tyvars will print the same way 
191         -- in an error message.  -dppr-debug will show up the difference
192         -- Better watch out for this.  If worst comes to worst, just
193         -- use mkSysLocalName.
194
195         kind = tyVarKind tyvar
196     in
197
198         -- Hack alert!  Certain system functions (like error) are quantified
199         -- over type variables with an 'open' kind (a :: ?).  When we instantiate
200         -- these tyvars we want to make a type variable whose kind is (Type bv)
201         -- where bv is a boxity variable.  This makes sure it's a type, but 
202         -- is open about its boxity.  We *don't* want to give the thing the
203         -- kind '?' (= Type AnyBox).  
204         --
205         -- This is all a hack to avoid giving error it's "proper" type:
206         --       error :: forall bv. forall a::Type bv. String -> a
207
208     (if kind == openTypeKind then
209         newOpenTypeKind 
210      else
211         returnNF_Tc kind)       `thenNF_Tc` \ kind' ->
212
213     tcNewMutTyVar name kind'
214
215 tcInstSigVar tyvar      -- Very similar to tcInstTyVar
216   = tcGetUnique         `thenNF_Tc` \ uniq ->
217     let 
218         name = setNameUnique (tyVarName tyvar) uniq
219         kind = tyVarKind tyvar
220     in
221     ASSERT( not (kind == openTypeKind) )        -- Shouldn't happen
222     tcNewSigTyVar name kind
223 \end{code}
224
225 @tcInstTcType@ instantiates the outer-level for-alls of a TcType with
226 fresh type variables, returning them and the instantiated body of the for-all.
227
228 \begin{code}
229 tcInstTcType :: TcType -> NF_TcM s ([TcTyVar], TcType)
230 tcInstTcType ty
231   = case splitForAllTys ty of
232         ([], _)       -> returnNF_Tc ([], ty)   -- Nothing to do
233         (tyvars, rho) -> tcInstTyVars tyvars            `thenNF_Tc` \ (tyvars', _, tenv)  ->
234                          returnNF_Tc (tyvars', substTy tenv rho)
235 \end{code}
236
237
238
239 %************************************************************************
240 %*                                                                      *
241 \subsection{Putting and getting  mutable type variables}
242 %*                                                                      *
243 %************************************************************************
244
245 \begin{code}
246 tcPutTyVar :: TcTyVar -> TcType -> NF_TcM s TcType
247 tcGetTyVar :: TcTyVar -> NF_TcM s (Maybe TcType)
248 \end{code}
249
250 Putting is easy:
251
252 \begin{code}
253 tcPutTyVar tyvar ty = tcWriteMutTyVar tyvar (Just ty)   `thenNF_Tc_`
254                       returnNF_Tc ty
255 \end{code}
256
257 Getting is more interesting.  The easy thing to do is just to read, thus:
258
259 \begin{verbatim}
260 tcGetTyVar tyvar = tcReadMutTyVar tyvar
261 \end{verbatim}
262
263 But it's more fun to short out indirections on the way: If this
264 version returns a TyVar, then that TyVar is unbound.  If it returns
265 any other type, then there might be bound TyVars embedded inside it.
266
267 We return Nothing iff the original box was unbound.
268
269 \begin{code}
270 tcGetTyVar tyvar
271   = ASSERT2( isMutTyVar tyvar, ppr tyvar )
272     tcReadMutTyVar tyvar                                `thenNF_Tc` \ maybe_ty ->
273     case maybe_ty of
274         Just ty -> short_out ty                         `thenNF_Tc` \ ty' ->
275                    tcWriteMutTyVar tyvar (Just ty')     `thenNF_Tc_`
276                    returnNF_Tc (Just ty')
277
278         Nothing    -> returnNF_Tc Nothing
279
280 short_out :: TcType -> NF_TcM s TcType
281 short_out ty@(TyVarTy tyvar)
282   | not (isMutTyVar tyvar)
283   = returnNF_Tc ty
284
285   | otherwise
286   = tcReadMutTyVar tyvar        `thenNF_Tc` \ maybe_ty ->
287     case maybe_ty of
288         Just ty' -> short_out ty'                       `thenNF_Tc` \ ty' ->
289                     tcWriteMutTyVar tyvar (Just ty')    `thenNF_Tc_`
290                     returnNF_Tc ty'
291
292         other    -> returnNF_Tc ty
293
294 short_out other_ty = returnNF_Tc other_ty
295 \end{code}
296
297
298 %************************************************************************
299 %*                                                                      *
300 \subsection{Zonking -- the exernal interfaces}
301 %*                                                                      *
302 %************************************************************************
303
304 -----------------  Type variables
305
306 \begin{code}
307 zonkTcTyVars :: [TcTyVar] -> NF_TcM s [TcType]
308 zonkTcTyVars tyvars = mapNF_Tc zonkTcTyVar tyvars
309
310 zonkTcTyVarBndr :: TcTyVar -> NF_TcM s TcTyVar
311 zonkTcTyVarBndr tyvar
312   = zonkTcTyVar tyvar   `thenNF_Tc` \ ty ->
313     case ty of
314         TyVarTy tyvar' -> returnNF_Tc tyvar'
315         _              -> pprTrace "zonkTcTyVarBndr" (ppr tyvar <+> ppr ty) $
316                           returnNF_Tc tyvar
317         
318 zonkTcTyVar :: TcTyVar -> NF_TcM s TcType
319 zonkTcTyVar tyvar = zonkTyVar (\ tv -> returnNF_Tc (TyVarTy tv)) tyvar
320 \end{code}
321
322 -----------------  Types
323
324 \begin{code}
325 zonkTcType :: TcType -> NF_TcM s TcType
326 zonkTcType ty = zonkType (\ tv -> returnNF_Tc (TyVarTy tv)) ty
327
328 zonkTcTypes :: [TcType] -> NF_TcM s [TcType]
329 zonkTcTypes tys = mapNF_Tc zonkTcType tys
330
331 zonkTcClassConstraints cts = mapNF_Tc zonk cts
332     where zonk (clas, tys)
333             = zonkTcTypes tys   `thenNF_Tc` \ new_tys ->
334               returnNF_Tc (clas, new_tys)
335
336 zonkTcThetaType :: TcThetaType -> NF_TcM s TcThetaType
337 zonkTcThetaType theta = mapNF_Tc zonkTcPredType theta
338
339 zonkTcPredType :: TcPredType -> NF_TcM s TcPredType
340 zonkTcPredType (Class c ts) =
341     zonkTcTypes ts      `thenNF_Tc` \ new_ts ->
342     returnNF_Tc (Class c new_ts)
343 zonkTcPredType (IParam n t) =
344     zonkTcType t        `thenNF_Tc` \ new_t ->
345     returnNF_Tc (IParam n new_t)
346
347 zonkTcKind :: TcKind -> NF_TcM s TcKind
348 zonkTcKind = zonkTcType
349 \end{code}
350
351 -------------------  These ...ToType, ...ToKind versions
352                      are used at the end of type checking
353
354 \begin{code}
355 zonkTcKindToKind :: TcKind -> NF_TcM s Kind
356 zonkTcKindToKind kind = zonkType zonk_unbound_kind_var kind
357   where
358         -- Zonk a mutable but unbound kind variable to
359         --      (Type Boxed)    if it has kind superKind
360         --      Boxed           if it has kind superBoxity
361     zonk_unbound_kind_var kv
362         | super_kind == superKind = tcPutTyVar kv boxedTypeKind
363         | otherwise               = ASSERT( super_kind == superBoxity )
364                                     tcPutTyVar kv boxedKind
365         where
366           super_kind = tyVarKind kv
367                         
368
369 zonkTcTypeToType :: TcType -> NF_TcM s Type
370 zonkTcTypeToType ty = zonkType zonk_unbound_tyvar ty
371   where
372         -- Zonk a mutable but unbound type variable to
373         --      Void            if it has kind (Type Boxed)
374         --      Voidxxx         otherwise
375     zonk_unbound_tyvar tv
376         = zonkTcKindToKind (tyVarKind tv)       `thenNF_Tc` \ kind ->
377           if kind == boxedTypeKind then
378                 tcPutTyVar tv voidTy    -- Just to avoid creating a new tycon in
379                                         -- this vastly common case
380           else
381                 tcPutTyVar tv (TyConApp (mk_void_tycon tv kind) [])
382
383     mk_void_tycon tv kind       -- Make a new TyCon with the same kind as the 
384                                 -- type variable tv.  Same name too, apart from
385                                 -- making it start with a colon (sigh)
386         = mkPrimTyCon tc_name kind 0 [] VoidRep
387         where
388           tc_name = mkDerivedName mkDerivedTyConOcc (getName tv) (getUnique tv)
389
390 -- zonkTcTyVarToTyVar is applied to the *binding* occurrence 
391 -- of a type variable, at the *end* of type checking.
392 -- It zonks the type variable, to get a mutable, but unbound, tyvar, tv;
393 -- zonks its kind, and then makes an immutable version of tv and binds tv to it.
394 -- Now any bound occurences of the original type variable will get 
395 -- zonked to the immutable version.
396
397 zonkTcTyVarToTyVar :: TcTyVar -> NF_TcM s TyVar
398 zonkTcTyVarToTyVar tv
399   = zonkTcKindToKind (tyVarKind tv)     `thenNF_Tc` \ kind ->
400     let
401                 -- Make an immutable version
402         immut_tv    = mkTyVar (tyVarName tv) kind
403         immut_tv_ty = mkTyVarTy immut_tv
404
405         zap tv = tcPutTyVar tv immut_tv_ty
406                 -- Bind the mutable version to the immutable one
407     in 
408         -- If the type variable is mutable, then bind it to immut_tv_ty
409         -- so that all other occurrences of the tyvar will get zapped too
410     zonkTyVar zap tv            `thenNF_Tc` \ ty2 ->
411     ASSERT2( immut_tv_ty == ty2, ppr tv $$ ppr immut_tv $$ ppr ty2 )
412
413     returnNF_Tc immut_tv
414 \end{code}
415
416
417 %************************************************************************
418 %*                                                                      *
419 \subsection{Zonking -- the main work-horses: zonkType, zonkTyVar}
420 %*                                                                      *
421 %*              For internal use only!                                  *
422 %*                                                                      *
423 %************************************************************************
424
425 \begin{code}
426 -- zonkType is used for Kinds as well
427
428 -- For unbound, mutable tyvars, zonkType uses the function given to it
429 -- For tyvars bound at a for-all, zonkType zonks them to an immutable
430 --      type variable and zonks the kind too
431
432 zonkType :: (TcTyVar -> NF_TcM s Type)  -- What to do with unbound mutable type variables
433                                         -- see zonkTcType, and zonkTcTypeToType
434          -> TcType
435          -> NF_TcM s Type
436 zonkType unbound_var_fn ty
437   = go ty
438   where
439     go (TyConApp tycon tys)       = mapNF_Tc go tys     `thenNF_Tc` \ tys' ->
440                                     returnNF_Tc (TyConApp tycon tys')
441
442     go (NoteTy (SynNote ty1) ty2) = go ty1              `thenNF_Tc` \ ty1' ->
443                                     go ty2              `thenNF_Tc` \ ty2' ->
444                                     returnNF_Tc (NoteTy (SynNote ty1') ty2')
445
446     go (NoteTy (FTVNote _) ty2)   = go ty2      -- Discard free-tyvar annotations
447
448     go (NoteTy (UsgNote usg) ty2) = go ty2              `thenNF_Tc` \ ty2' ->
449                                     returnNF_Tc (NoteTy (UsgNote usg) ty2')
450
451     go (NoteTy (UsgForAll uv) ty2)= go ty2              `thenNF_Tc` \ ty2' ->
452                                     returnNF_Tc (NoteTy (UsgForAll uv) ty2')
453
454     go (NoteTy (IPNote nm) ty2)   = go ty2              `thenNF_Tc` \ ty2' ->
455                                     returnNF_Tc (NoteTy (IPNote nm) ty2')
456
457     go (FunTy arg res)            = go arg              `thenNF_Tc` \ arg' ->
458                                     go res              `thenNF_Tc` \ res' ->
459                                     returnNF_Tc (FunTy arg' res')
460  
461     go (AppTy fun arg)            = go fun              `thenNF_Tc` \ fun' ->
462                                     go arg              `thenNF_Tc` \ arg' ->
463                                     returnNF_Tc (mkAppTy fun' arg')
464
465         -- The two interesting cases!
466     go (TyVarTy tyvar)            = zonkTyVar unbound_var_fn tyvar
467
468     go (ForAllTy tyvar ty)
469         = zonkTcTyVarToTyVar tyvar      `thenNF_Tc` \ tyvar' ->
470           go ty                         `thenNF_Tc` \ ty' ->
471           returnNF_Tc (ForAllTy tyvar' ty')
472
473
474 zonkTyVar :: (TcTyVar -> NF_TcM s Type)         -- What to do for an unbound mutable variable
475           -> TcTyVar -> NF_TcM s TcType
476 zonkTyVar unbound_var_fn tyvar 
477   | not (isMutTyVar tyvar)      -- Not a mutable tyvar.  This can happen when
478                                 -- zonking a forall type, when the bound type variable
479                                 -- needn't be mutable
480   = ASSERT( isTyVar tyvar )             -- Should not be any immutable kind vars
481     returnNF_Tc (TyVarTy tyvar)
482
483   | otherwise
484   =  tcGetTyVar tyvar   `thenNF_Tc` \ maybe_ty ->
485      case maybe_ty of
486           Nothing       -> unbound_var_fn tyvar                 -- Mutable and unbound
487           Just other_ty -> ASSERT( isNotUsgTy other_ty )
488                            zonkType unbound_var_fn other_ty     -- Bound
489 \end{code}
490
491 %************************************************************************
492 %*                                                                      *
493 \subsection{tcTypeKind}
494 %*                                                                      *
495 %************************************************************************
496
497 Sadly, we need a Tc version of typeKind, that looks though mutable
498 kind variables.  See the notes with Type.typeKind for the typeKindF nonsense
499
500 This is pretty gruesome.
501
502 \begin{code}
503 tcTypeKind :: TcType -> NF_TcM s TcKind
504
505 tcTypeKind (TyVarTy tyvar)      = returnNF_Tc (tyVarKind tyvar)
506 tcTypeKind (TyConApp tycon tys) = foldlTc (\k _ -> tcFunResultTy k) (tyConKind tycon) tys
507 tcTypeKind (NoteTy _ ty)        = tcTypeKind ty
508 tcTypeKind (AppTy fun arg)      = tcTypeKind fun        `thenNF_Tc` \ fun_kind ->
509                                   tcFunResultTy fun_kind
510 tcTypeKind (FunTy fun arg)      = tcTypeKindF arg
511 tcTypeKind (ForAllTy _ ty)      = tcTypeKindF ty
512
513 tcTypeKindF :: TcType -> NF_TcM s TcKind
514 tcTypeKindF (NoteTy _ ty)   = tcTypeKindF ty
515 tcTypeKindF (FunTy _ ty)    = tcTypeKindF ty
516 tcTypeKindF (ForAllTy _ ty) = tcTypeKindF ty
517 tcTypeKindF other           = tcTypeKind other  `thenNF_Tc` \ kind ->
518                               fix_up kind
519   where
520     fix_up (TyConApp kc _) | kc == typeCon = returnNF_Tc boxedTypeKind
521                 -- Functions at the type level are always boxed
522     fix_up (NoteTy _ kind)   = fix_up kind
523     fix_up kind@(TyVarTy tv) = tcGetTyVar tv    `thenNF_Tc` \ maybe_ty ->
524                                case maybe_ty of
525                                   Just kind' -> fix_up kind'
526                                   Nothing  -> returnNF_Tc kind
527     fix_up kind              = returnNF_Tc kind
528
529 tcFunResultTy (NoteTy _ ty)   = tcFunResultTy ty
530 tcFunResultTy (FunTy arg res) = returnNF_Tc res
531 tcFunResultTy (TyVarTy tv)    = tcGetTyVar tv   `thenNF_Tc` \ maybe_ty ->
532                                 case maybe_ty of
533                                   Just ty' -> tcFunResultTy ty'
534         -- The Nothing case, and the other cases for tcFunResultTy
535         -- should never happen... pattern match failure
536 \end{code}