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 s)
13 newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType s]
15 -----------------------------------------
17 TcTauType, TcThetaType, TcRhoType,
19 -- Find the type to which a type variable is bound
20 tcWriteTyVar, -- :: TcTyVar s -> TcType s -> NF_TcM (TcType s)
21 tcReadTyVar, -- :: TcTyVar s -> NF_TcM (TcMaybe s)
24 tcSplitForAllTy, tcSplitRhoTy,
29 tcInstSigType, tcInstTcType, tcInstSigTcType,
32 zonkTcTyVars, zonkSigTyVar,
33 zonkTcType, zonkTcTypes, zonkTcThetaType,
40 #include "HsVersions.h"
44 import Type ( Type, ThetaType, GenType(..), mkAppTy,
45 tyVarsOfTypes, splitDictTy_maybe,
46 isTyVarTy, instantiateTy
48 import TyVar ( TyVar, GenTyVar(..), GenTyVarSet,
49 TyVarEnv, lookupTyVarEnv, addToTyVarEnv,
50 emptyTyVarEnv, zipTyVarEnv, tyVarSetToList
54 import Class ( Class )
55 import TyCon ( isFunTyCon )
59 import TysPrim ( voidTy )
61 import Unique ( Unique )
62 import UniqFM ( UniqFM )
63 import BasicTypes ( unused )
64 import Util ( nOfThem, panic )
74 type TcType s = GenType (TcBox s) -- Used during typechecker
75 -- Invariant on ForAllTy in TcTypes:
77 -- a cannot occur inside a MutTyVar in T; that is,
78 -- T is "flattened" before quantifying over a
80 type TcThetaType s = [(Class, [TcType s])]
81 type TcRhoType s = TcType s -- No ForAllTys
82 type TcTauType s = TcType s -- No DictTys or ForAllTys
84 type TcBox s = TcRef s (TcMaybe s)
86 data TcMaybe s = UnBound
89 -- Interestingly, you can't use (Maybe (TcType s)) instead of (TcMaybe s),
90 -- because you get a synonym loop if you do!
92 type TcTyVar s = GenTyVar (TcBox s)
93 type TcTyVarSet s = GenTyVarSet (TcBox s)
97 tcTyVarToTyVar :: TcTyVar s -> TyVar
98 tcTyVarToTyVar (TyVar uniq kind name _) = TyVar uniq kind name unused
103 These tcSplit functions are like their non-Tc analogues, but they
104 follow through bound type variables.
107 tcSplitForAllTy :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
111 go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
112 go syn_t (SynTy _ t) tvs = go syn_t t tvs
113 go syn_t (TyVarTy tv) tvs = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
115 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty tvs
116 other -> returnNF_Tc (reverse tvs, syn_t)
117 go syn_t t tvs = returnNF_Tc (reverse tvs, syn_t)
119 tcSplitRhoTy :: TcType s -> NF_TcM s (TcThetaType s, TcType s)
123 -- A type variable is never instantiated to a dictionary type,
124 -- so we don't need to do a tcReadVar on the "arg".
125 go syn_t (FunTy arg res) ts = case splitDictTy_maybe arg of
126 Just pair -> go res res (pair:ts)
127 Nothing -> returnNF_Tc (reverse ts, syn_t)
128 go syn_t (SynTy _ t) ts = go syn_t t ts
129 go syn_t (TyVarTy tv) ts = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
131 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty ts
132 other -> returnNF_Tc (reverse ts, syn_t)
133 go syn_t t ts = returnNF_Tc (reverse ts, syn_t)
141 newTcTyVar :: Kind -> NF_TcM s (TcTyVar s)
143 = tcGetUnique `thenNF_Tc` \ uniq ->
144 tcNewMutVar UnBound `thenNF_Tc` \ box ->
145 returnNF_Tc (TyVar uniq kind Nothing box)
147 newTyVarTy :: Kind -> NF_TcM s (TcType s)
149 = newTcTyVar kind `thenNF_Tc` \ tc_tyvar ->
150 returnNF_Tc (TyVarTy tc_tyvar)
152 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType s]
153 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
156 -- For signature type variables, use the user name for the type variable
157 tcInstTyVars, tcInstSigTyVars
159 -> NF_TcM s ([TcTyVar s], [TcType s], TyVarEnv (TcType s))
161 tcInstTyVars tyvars = inst_tyvars inst_tyvar tyvars
162 tcInstSigTyVars tyvars = inst_tyvars inst_sig_tyvar tyvars
164 inst_tyvars inst tyvars
165 = mapNF_Tc inst tyvars `thenNF_Tc` \ tc_tyvars ->
167 tys = map TyVarTy tc_tyvars
169 returnNF_Tc (tc_tyvars, tys, zipTyVarEnv tyvars tys)
171 inst_tyvar (TyVar _ kind name _)
172 = tcGetUnique `thenNF_Tc` \ uniq ->
173 tcNewMutVar UnBound `thenNF_Tc` \ box ->
174 returnNF_Tc (TyVar uniq kind Nothing box)
175 -- The "Nothing" means that it'll always print with its
176 -- unique (or something similar). If we leave the original (Just Name)
177 -- in there then error messages will say "can't match (T a) against (T a)"
179 inst_sig_tyvar (TyVar _ kind name _)
180 = tcGetUnique `thenNF_Tc` \ uniq ->
182 tcNewMutVar UnBound `thenNF_Tc` \ box ->
183 -- Was DontBind, but we've nuked that "optimisation"
185 returnNF_Tc (TyVar uniq kind name box)
186 -- We propagate the name of the sigature type variable
189 @tcInstType@ and @tcInstSigType@ both create a fresh instance of a
190 type, returning a @TcType@. All inner for-alls are instantiated with
193 The difference is that tcInstType instantiates all forall'd type
194 variables (and their bindees) with anonymous type variables, whereas
195 tcInstSigType instantiates them with named type variables.
196 @tcInstSigType@ also doesn't take an environment.
198 On the other hand, @tcInstTcType@ instantiates a TcType. It uses
199 instantiateTy which could take advantage of sharing some day.
202 tcInstTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
204 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
206 [] -> returnNF_Tc ([], ty) -- Nothing to do
207 other -> tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
208 returnNF_Tc (tyvars', instantiateTy tenv rho)
210 tcInstSigTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
212 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
214 [] -> returnNF_Tc ([], ty) -- Nothing to do
215 other -> tcInstSigTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
216 returnNF_Tc (tyvars', instantiateTy tenv rho)
218 tcInstType :: TyVarEnv (TcType s)
220 -> NF_TcM s (TcType s)
221 tcInstType tenv ty_to_inst
222 = tcConvert bind_fn occ_fn tenv ty_to_inst
225 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
226 Just ty -> returnNF_Tc ty
227 Nothing -> panic "tcInstType:1" --(vcat [ppr ty_to_inst,
230 tcInstSigType :: GenType flexi -> NF_TcM s (TcType s)
231 tcInstSigType ty_to_inst
232 = tcConvert bind_fn occ_fn emptyTyVarEnv ty_to_inst
234 bind_fn = inst_sig_tyvar -- Note: inst_sig_tyvar, not inst_tyvar
235 -- I don't think that can lead to strange error messages
236 -- of the form can't match (T a) against (T a)
237 -- See notes with inst_tyvar
239 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
240 Just ty -> returnNF_Tc ty
241 Nothing -> panic "tcInstType:2"-- (vcat [ppr ty_to_inst,
244 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
245 zonkTcTyVarToTyVar tv
246 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
247 case tv_ty of -- Should be a tyvar!
249 TyVarTy tv' -> returnNF_Tc (tcTyVarToTyVar tv')
251 _ -> --pprTrace "zonkTcTyVarToTyVar:" (hsep [ppr tv, ppr tv_ty]) $
252 returnNF_Tc (tcTyVarToTyVar tv)
255 zonkTcTypeToType :: TyVarEnv Type -> TcType s -> NF_TcM s Type
256 zonkTcTypeToType env ty
257 = tcConvert zonkTcTyVarToTyVar occ_fn env ty
260 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
262 BoundTo (TyVarTy tyvar') -> lookup env tyvar'
263 BoundTo other_ty -> tcConvert zonkTcTyVarToTyVar occ_fn env other_ty
264 other -> lookup env tyvar
266 lookup env tyvar = case lookupTyVarEnv env tyvar of
267 Just ty -> returnNF_Tc ty
268 Nothing -> returnNF_Tc voidTy -- Unbound type variables go to Void
271 tcConvert bind_fn occ_fn env ty_to_convert
272 = doo env ty_to_convert
274 doo env (TyConApp tycon tys) = mapNF_Tc (doo env) tys `thenNF_Tc` \ tys' ->
275 returnNF_Tc (TyConApp tycon tys')
277 doo env (SynTy ty1 ty2) = doo env ty1 `thenNF_Tc` \ ty1' ->
278 doo env ty2 `thenNF_Tc` \ ty2' ->
279 returnNF_Tc (SynTy ty1' ty2')
281 doo env (FunTy arg res) = doo env arg `thenNF_Tc` \ arg' ->
282 doo env res `thenNF_Tc` \ res' ->
283 returnNF_Tc (FunTy arg' res')
285 doo env (AppTy fun arg) = doo env fun `thenNF_Tc` \ fun' ->
286 doo env arg `thenNF_Tc` \ arg' ->
287 returnNF_Tc (mkAppTy fun' arg')
289 -- The two interesting cases!
290 doo env (TyVarTy tv) = occ_fn env tv
292 doo env (ForAllTy tyvar ty)
293 = bind_fn tyvar `thenNF_Tc` \ tyvar' ->
295 new_env = addToTyVarEnv env tyvar (TyVarTy tyvar')
297 doo new_env ty `thenNF_Tc` \ ty' ->
298 returnNF_Tc (ForAllTy tyvar' ty')
301 tcInstTheta :: TyVarEnv (TcType s) -> ThetaType -> NF_TcM s (TcThetaType s)
302 tcInstTheta tenv theta
305 go (clas,tys) = mapNF_Tc (tcInstType tenv) tys `thenNF_Tc` \ tc_tys ->
306 returnNF_Tc (clas, tc_tys)
309 Reading and writing TcTyVars
310 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312 tcWriteTyVar :: TcTyVar s -> TcType s -> NF_TcM s ()
313 tcReadTyVar :: TcTyVar s -> NF_TcM s (TcMaybe s)
319 tcWriteTyVar (TyVar uniq kind name box) ty = tcWriteMutVar box (BoundTo ty)
322 Reading is more interesting. The easy thing to do is just to read, thus:
324 tcReadTyVar (TyVar uniq kind name box) = tcReadMutVar box
327 But it's more fun to short out indirections on the way: If this
328 version returns a TyVar, then that TyVar is unbound. If it returns
329 any other type, then there might be bound TyVars embedded inside it.
331 We return Nothing iff the original box was unbound.
334 tcReadTyVar (TyVar uniq kind name box)
335 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
337 BoundTo ty -> short_out ty `thenNF_Tc` \ ty' ->
338 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
339 returnNF_Tc (BoundTo ty')
341 other -> returnNF_Tc other
343 short_out :: TcType s -> NF_TcM s (TcType s)
344 short_out ty@(TyVarTy (TyVar uniq kind name box))
345 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
347 BoundTo ty' -> short_out ty' `thenNF_Tc` \ ty' ->
348 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
351 other -> returnNF_Tc ty
353 short_out other_ty = returnNF_Tc other_ty
360 zonkTcTyVars :: TcTyVarSet s -> NF_TcM s (TcTyVarSet s)
362 = mapNF_Tc zonkTcTyVar (tyVarSetToList tyvars) `thenNF_Tc` \ tys ->
363 returnNF_Tc (tyVarsOfTypes tys)
365 zonkTcTyVar :: TcTyVar s -> NF_TcM s (TcType s)
367 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
369 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc ty -- tcReadTyVar never returns a bound tyvar
370 BoundTo other -> zonkTcType other
371 other -> returnNF_Tc (TyVarTy tyvar)
373 -- Signature type variables only get bound to each other,
375 zonkSigTyVar :: TcTyVar s -> NF_TcM s (TcTyVar s)
377 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
379 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc tyvar' -- tcReadTyVar never returns a bound tyvar
380 BoundTo other -> panic "zonkSigTyVar" -- Should only be bound to another tyvar
381 other -> returnNF_Tc tyvar
383 zonkTcTypes :: [TcType s] -> NF_TcM s [TcType s]
384 zonkTcTypes tys = mapNF_Tc zonkTcType tys
386 zonkTcThetaType :: TcThetaType s -> NF_TcM s (TcThetaType s)
387 zonkTcThetaType theta = mapNF_Tc zonk theta
389 zonk (c,ts) = zonkTcTypes ts `thenNF_Tc` \ new_ts ->
390 returnNF_Tc (c, new_ts)
392 zonkTcType :: TcType s -> NF_TcM s (TcType s)
394 zonkTcType (TyVarTy tyvar) = zonkTcTyVar tyvar
396 zonkTcType (AppTy ty1 ty2)
397 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
398 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
399 returnNF_Tc (mkAppTy ty1' ty2')
401 zonkTcType (TyConApp tc tys)
402 = mapNF_Tc zonkTcType tys `thenNF_Tc` \ tys' ->
403 returnNF_Tc (TyConApp tc tys')
405 zonkTcType (SynTy ty1 ty2)
406 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
407 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
408 returnNF_Tc (SynTy ty1' ty2')
410 zonkTcType (ForAllTy tv ty)
411 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
412 zonkTcType ty `thenNF_Tc` \ ty' ->
413 case tv_ty of -- Should be a tyvar!
414 TyVarTy tv' -> returnNF_Tc (ForAllTy tv' ty')
415 _ -> panic "zonkTcType"
416 -- pprTrace "zonkTcType:ForAllTy:" (hsep [ppr tv, ppr tv_ty]) $
417 -- returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')
419 zonkTcType (FunTy ty1 ty2)
420 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
421 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
422 returnNF_Tc (FunTy ty1' ty2')