2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcType]{Types used in the typechecker}
12 newTyVarTy, -- Kind -> NF_TcM s TcType
13 newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType]
15 newTyVarTy_OpenKind, -- NF_TcM s TcType
16 newOpenTypeKind, -- NF_TcM s TcKind
18 -----------------------------------------
19 TcType, TcTauType, TcThetaType, TcRhoType,
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
34 tcTypeKind, -- :: TcType -> NF_TcM s TcKind
35 --------------------------------
37 newKindVar, newKindVars,
41 --------------------------------
42 zonkTcTyVar, zonkTcTyVars, zonkTcTyVarBndr,
43 zonkTcType, zonkTcTypes, zonkTcClassConstraints, zonkTcThetaType,
45 zonkTcTypeToType, zonkTcTyVarToTyVar,
50 #include "HsVersions.h"
54 import TypeRep ( Type(..), Kind, TyNote(..),
55 typeCon, openTypeKind, boxedTypeKind, boxedKind, superKind, superBoxity
57 import Type ( ThetaType, PredType(..),
59 splitPredTy_maybe, splitForAllTys, isNotUsgTy,
60 isTyVarTy, mkTyVarTy, mkTyVarTys,
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 )
69 import TysWiredIn ( voidTy )
71 import Name ( NamedThing(..), setNameUnique, mkSysLocalName,
72 mkDerivedName, mkDerivedTyConOcc
74 import Unique ( Unique, Uniquable(..) )
75 import Util ( nOfThem )
83 Type definitions are in TcMonad.lhs
86 typeToTcType :: Type -> TcType
89 kindToTcKind :: Kind -> TcKind
90 kindToTcKind kind = kind
95 These tcSplit functions are like their non-Tc analogues, but they
96 follow through bound type variables.
98 No need for tcSplitForAllTy because a type variable can't be instantiated
102 tcSplitRhoTy :: TcType -> NF_TcM s (TcThetaType, TcType)
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 ->
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)
120 %************************************************************************
122 \subsection{New type variables}
124 %************************************************************************
127 newTyVar :: Kind -> NF_TcM s TcTyVar
129 = tcGetUnique `thenNF_Tc` \ uniq ->
130 tcNewMutTyVar (mkSysLocalName uniq SLIT("t")) kind
132 newTyVarTy :: Kind -> NF_TcM s TcType
134 = newTyVar kind `thenNF_Tc` \ tc_tyvar ->
135 returnNF_Tc (TyVarTy tc_tyvar)
137 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType]
138 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
140 newKindVar :: NF_TcM s TcKind
142 = tcGetUnique `thenNF_Tc` \ uniq ->
143 tcNewMutTyVar (mkSysLocalName uniq SLIT("k")) superKind `thenNF_Tc` \ kv ->
144 returnNF_Tc (TyVarTy kv)
146 newKindVars :: Int -> NF_TcM s [TcKind]
147 newKindVars n = mapNF_Tc (\ _ -> newKindVar) (nOfThem n ())
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 ->
156 newOpenTypeKind :: NF_TcM s TcKind
157 newOpenTypeKind = newTyVarTy superBoxity `thenNF_Tc` \ bv ->
158 returnNF_Tc (mkTyConApp typeCon [bv])
162 %************************************************************************
164 \subsection{Type instantiation}
166 %************************************************************************
168 Instantiating a bunch of type variables
171 tcInstTyVars :: [TyVar]
172 -> NF_TcM s ([TcTyVar], [TcType], Subst)
175 = mapNF_Tc tcInstTyVar tyvars `thenNF_Tc` \ tc_tyvars ->
177 tys = mkTyVarTys tc_tyvars
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
185 = tcGetUnique `thenNF_Tc` \ uniq ->
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.
195 kind = tyVarKind tyvar
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).
205 -- This is all a hack to avoid giving error it's "proper" type:
206 -- error :: forall bv. forall a::Type bv. String -> a
208 (if kind == openTypeKind then
211 returnNF_Tc kind) `thenNF_Tc` \ kind' ->
213 tcNewMutTyVar name kind'
215 tcInstSigVar tyvar -- Very similar to tcInstTyVar
216 = tcGetUnique `thenNF_Tc` \ uniq ->
218 name = setNameUnique (tyVarName tyvar) uniq
219 kind = tyVarKind tyvar
221 ASSERT( not (kind == openTypeKind) ) -- Shouldn't happen
222 tcNewSigTyVar name kind
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.
229 tcInstTcType :: TcType -> NF_TcM s ([TcTyVar], TcType)
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)
239 %************************************************************************
241 \subsection{Putting and getting mutable type variables}
243 %************************************************************************
246 tcPutTyVar :: TcTyVar -> TcType -> NF_TcM s TcType
247 tcGetTyVar :: TcTyVar -> NF_TcM s (Maybe TcType)
253 tcPutTyVar tyvar ty = tcWriteMutTyVar tyvar (Just ty) `thenNF_Tc_`
257 Getting is more interesting. The easy thing to do is just to read, thus:
260 tcGetTyVar tyvar = tcReadMutTyVar tyvar
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.
267 We return Nothing iff the original box was unbound.
271 = ASSERT2( isMutTyVar tyvar, ppr tyvar )
272 tcReadMutTyVar tyvar `thenNF_Tc` \ maybe_ty ->
274 Just ty -> short_out ty `thenNF_Tc` \ ty' ->
275 tcWriteMutTyVar tyvar (Just ty') `thenNF_Tc_`
276 returnNF_Tc (Just ty')
278 Nothing -> returnNF_Tc Nothing
280 short_out :: TcType -> NF_TcM s TcType
281 short_out ty@(TyVarTy tyvar)
282 | not (isMutTyVar tyvar)
286 = tcReadMutTyVar tyvar `thenNF_Tc` \ maybe_ty ->
288 Just ty' -> short_out ty' `thenNF_Tc` \ ty' ->
289 tcWriteMutTyVar tyvar (Just ty') `thenNF_Tc_`
292 other -> returnNF_Tc ty
294 short_out other_ty = returnNF_Tc other_ty
298 %************************************************************************
300 \subsection{Zonking -- the exernal interfaces}
302 %************************************************************************
304 ----------------- Type variables
307 zonkTcTyVars :: [TcTyVar] -> NF_TcM s [TcType]
308 zonkTcTyVars tyvars = mapNF_Tc zonkTcTyVar tyvars
310 zonkTcTyVarBndr :: TcTyVar -> NF_TcM s TcTyVar
311 zonkTcTyVarBndr tyvar
312 = zonkTcTyVar tyvar `thenNF_Tc` \ ty ->
314 TyVarTy tyvar' -> returnNF_Tc tyvar'
315 _ -> pprTrace "zonkTcTyVarBndr" (ppr tyvar <+> ppr ty) $
318 zonkTcTyVar :: TcTyVar -> NF_TcM s TcType
319 zonkTcTyVar tyvar = zonkTyVar (\ tv -> returnNF_Tc (TyVarTy tv)) tyvar
322 ----------------- Types
325 zonkTcType :: TcType -> NF_TcM s TcType
326 zonkTcType ty = zonkType (\ tv -> returnNF_Tc (TyVarTy tv)) ty
328 zonkTcTypes :: [TcType] -> NF_TcM s [TcType]
329 zonkTcTypes tys = mapNF_Tc zonkTcType tys
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)
336 zonkTcThetaType :: TcThetaType -> NF_TcM s TcThetaType
337 zonkTcThetaType theta = mapNF_Tc zonkTcPredType theta
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)
347 zonkTcKind :: TcKind -> NF_TcM s TcKind
348 zonkTcKind = zonkTcType
351 ------------------- These ...ToType, ...ToKind versions
352 are used at the end of type checking
355 zonkTcKindToKind :: TcKind -> NF_TcM s Kind
356 zonkTcKindToKind kind = zonkType zonk_unbound_kind_var kind
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
366 super_kind = tyVarKind kv
369 zonkTcTypeToType :: TcType -> NF_TcM s Type
370 zonkTcTypeToType ty = zonkType zonk_unbound_tyvar ty
372 -- Zonk a mutable but unbound type variable to
373 -- Void if it has kind (Type Boxed)
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
381 tcPutTyVar tv (TyConApp (mk_void_tycon tv kind) [])
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
388 tc_name = mkDerivedName mkDerivedTyConOcc (getName tv) (getUnique tv)
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.
397 zonkTcTyVarToTyVar :: TcTyVar -> NF_TcM s TyVar
398 zonkTcTyVarToTyVar tv
399 = zonkTcKindToKind (tyVarKind tv) `thenNF_Tc` \ kind ->
401 -- Make an immutable version
402 immut_tv = mkTyVar (tyVarName tv) kind
403 immut_tv_ty = mkTyVarTy immut_tv
405 zap tv = tcPutTyVar tv immut_tv_ty
406 -- Bind the mutable version to the immutable one
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 )
417 %************************************************************************
419 \subsection{Zonking -- the main work-horses: zonkType, zonkTyVar}
421 %* For internal use only! *
423 %************************************************************************
426 -- zonkType is used for Kinds as well
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
432 zonkType :: (TcTyVar -> NF_TcM s Type) -- What to do with unbound mutable type variables
433 -- see zonkTcType, and zonkTcTypeToType
436 zonkType unbound_var_fn ty
439 go (TyConApp tycon tys) = mapNF_Tc go tys `thenNF_Tc` \ tys' ->
440 returnNF_Tc (TyConApp tycon tys')
442 go (NoteTy (SynNote ty1) ty2) = go ty1 `thenNF_Tc` \ ty1' ->
443 go ty2 `thenNF_Tc` \ ty2' ->
444 returnNF_Tc (NoteTy (SynNote ty1') ty2')
446 go (NoteTy (FTVNote _) ty2) = go ty2 -- Discard free-tyvar annotations
448 go (NoteTy (UsgNote usg) ty2) = go ty2 `thenNF_Tc` \ ty2' ->
449 returnNF_Tc (NoteTy (UsgNote usg) ty2')
451 go (NoteTy (UsgForAll uv) ty2)= go ty2 `thenNF_Tc` \ ty2' ->
452 returnNF_Tc (NoteTy (UsgForAll uv) ty2')
454 go (NoteTy (IPNote nm) ty2) = go ty2 `thenNF_Tc` \ ty2' ->
455 returnNF_Tc (NoteTy (IPNote nm) ty2')
457 go (FunTy arg res) = go arg `thenNF_Tc` \ arg' ->
458 go res `thenNF_Tc` \ res' ->
459 returnNF_Tc (FunTy arg' res')
461 go (AppTy fun arg) = go fun `thenNF_Tc` \ fun' ->
462 go arg `thenNF_Tc` \ arg' ->
463 returnNF_Tc (mkAppTy fun' arg')
465 -- The two interesting cases!
466 go (TyVarTy tyvar) = zonkTyVar unbound_var_fn tyvar
468 go (ForAllTy tyvar ty)
469 = zonkTcTyVarToTyVar tyvar `thenNF_Tc` \ tyvar' ->
470 go ty `thenNF_Tc` \ ty' ->
471 returnNF_Tc (ForAllTy tyvar' ty')
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)
484 = tcGetTyVar tyvar `thenNF_Tc` \ maybe_ty ->
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
491 %************************************************************************
493 \subsection{tcTypeKind}
495 %************************************************************************
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
500 This is pretty gruesome.
503 tcTypeKind :: TcType -> NF_TcM s TcKind
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
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 ->
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 ->
525 Just kind' -> fix_up kind'
526 Nothing -> returnNF_Tc kind
527 fix_up kind = returnNF_Tc kind
529 tcFunResultTy (NoteTy _ ty) = tcFunResultTy ty
530 tcFunResultTy (FunTy arg res) = returnNF_Tc res
531 tcFunResultTy (TyVarTy tv) = tcGetTyVar tv `thenNF_Tc` \ maybe_ty ->
533 Just ty' -> tcFunResultTy ty'
534 -- The Nothing case, and the other cases for tcFunResultTy
535 -- should never happen... pattern match failure