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 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
237 Just ty -> returnNF_Tc ty
238 Nothing -> panic "tcInstType:2"-- (vcat [ppr ty_to_inst,
241 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
242 zonkTcTyVarToTyVar tv
243 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
244 case tv_ty of -- Should be a tyvar!
246 TyVarTy tv' -> returnNF_Tc (tcTyVarToTyVar tv')
248 _ -> --pprTrace "zonkTcTyVarToTyVar:" (hsep [ppr tv, ppr tv_ty]) $
249 returnNF_Tc (tcTyVarToTyVar tv)
252 zonkTcTypeToType :: TyVarEnv Type -> TcType s -> NF_TcM s Type
253 zonkTcTypeToType env ty
254 = tcConvert zonkTcTyVarToTyVar occ_fn env ty
257 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
259 BoundTo (TyVarTy tyvar') -> lookup env tyvar'
260 BoundTo other_ty -> tcConvert zonkTcTyVarToTyVar occ_fn env other_ty
261 other -> lookup env tyvar
263 lookup env tyvar = case lookupTyVarEnv env tyvar of
264 Just ty -> returnNF_Tc ty
265 Nothing -> returnNF_Tc voidTy -- Unbound type variables go to Void
268 tcConvert bind_fn occ_fn env ty_to_convert
269 = doo env ty_to_convert
271 doo env (TyConApp tycon tys) = mapNF_Tc (doo env) tys `thenNF_Tc` \ tys' ->
272 returnNF_Tc (TyConApp tycon tys')
274 doo env (SynTy ty1 ty2) = doo env ty1 `thenNF_Tc` \ ty1' ->
275 doo env ty2 `thenNF_Tc` \ ty2' ->
276 returnNF_Tc (SynTy ty1' ty2')
278 doo env (FunTy arg res) = doo env arg `thenNF_Tc` \ arg' ->
279 doo env res `thenNF_Tc` \ res' ->
280 returnNF_Tc (FunTy arg' res')
282 doo env (AppTy fun arg) = doo env fun `thenNF_Tc` \ fun' ->
283 doo env arg `thenNF_Tc` \ arg' ->
284 returnNF_Tc (mkAppTy fun' arg')
286 -- The two interesting cases!
287 doo env (TyVarTy tv) = occ_fn env tv
289 doo env (ForAllTy tyvar ty)
290 = bind_fn tyvar `thenNF_Tc` \ tyvar' ->
292 new_env = addToTyVarEnv env tyvar (TyVarTy tyvar')
294 doo new_env ty `thenNF_Tc` \ ty' ->
295 returnNF_Tc (ForAllTy tyvar' ty')
298 tcInstTheta :: TyVarEnv (TcType s) -> ThetaType -> NF_TcM s (TcThetaType s)
299 tcInstTheta tenv theta
302 go (clas,tys) = mapNF_Tc (tcInstType tenv) tys `thenNF_Tc` \ tc_tys ->
303 returnNF_Tc (clas, tc_tys)
306 Reading and writing TcTyVars
307 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309 tcWriteTyVar :: TcTyVar s -> TcType s -> NF_TcM s ()
310 tcReadTyVar :: TcTyVar s -> NF_TcM s (TcMaybe s)
316 tcWriteTyVar (TyVar uniq kind name box) ty = tcWriteMutVar box (BoundTo ty)
319 Reading is more interesting. The easy thing to do is just to read, thus:
321 tcReadTyVar (TyVar uniq kind name box) = tcReadMutVar box
324 But it's more fun to short out indirections on the way: If this
325 version returns a TyVar, then that TyVar is unbound. If it returns
326 any other type, then there might be bound TyVars embedded inside it.
328 We return Nothing iff the original box was unbound.
331 tcReadTyVar (TyVar uniq kind name box)
332 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
334 BoundTo ty -> short_out ty `thenNF_Tc` \ ty' ->
335 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
336 returnNF_Tc (BoundTo ty')
338 other -> returnNF_Tc other
340 short_out :: TcType s -> NF_TcM s (TcType s)
341 short_out ty@(TyVarTy (TyVar uniq kind name box))
342 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
344 BoundTo ty' -> short_out ty' `thenNF_Tc` \ ty' ->
345 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
348 other -> returnNF_Tc ty
350 short_out other_ty = returnNF_Tc other_ty
357 zonkTcTyVars :: TcTyVarSet s -> NF_TcM s (TcTyVarSet s)
359 = mapNF_Tc zonkTcTyVar (tyVarSetToList tyvars) `thenNF_Tc` \ tys ->
360 returnNF_Tc (tyVarsOfTypes tys)
362 zonkTcTyVar :: TcTyVar s -> NF_TcM s (TcType s)
364 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
366 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc ty -- tcReadTyVar never returns a bound tyvar
367 BoundTo other -> zonkTcType other
368 other -> returnNF_Tc (TyVarTy tyvar)
370 -- Signature type variables only get bound to each other,
372 zonkSigTyVar :: TcTyVar s -> NF_TcM s (TcTyVar s)
374 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
376 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc tyvar' -- tcReadTyVar never returns a bound tyvar
377 BoundTo other -> panic "zonkSigTyVar" -- Should only be bound to another tyvar
378 other -> returnNF_Tc tyvar
380 zonkTcTypes :: [TcType s] -> NF_TcM s [TcType s]
381 zonkTcTypes tys = mapNF_Tc zonkTcType tys
383 zonkTcThetaType :: TcThetaType s -> NF_TcM s (TcThetaType s)
384 zonkTcThetaType theta = mapNF_Tc zonk theta
386 zonk (c,ts) = zonkTcTypes ts `thenNF_Tc` \ new_ts ->
387 returnNF_Tc (c, new_ts)
389 zonkTcType :: TcType s -> NF_TcM s (TcType s)
391 zonkTcType (TyVarTy tyvar) = zonkTcTyVar tyvar
393 zonkTcType (AppTy ty1 ty2)
394 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
395 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
396 returnNF_Tc (mkAppTy ty1' ty2')
398 zonkTcType (TyConApp tc tys)
399 = mapNF_Tc zonkTcType tys `thenNF_Tc` \ tys' ->
400 returnNF_Tc (TyConApp tc tys')
402 zonkTcType (SynTy ty1 ty2)
403 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
404 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
405 returnNF_Tc (SynTy ty1' ty2')
407 zonkTcType (ForAllTy tv ty)
408 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
409 zonkTcType ty `thenNF_Tc` \ ty' ->
410 case tv_ty of -- Should be a tyvar!
411 TyVarTy tv' -> returnNF_Tc (ForAllTy tv' ty')
412 _ -> panic "zonkTcType"
413 -- pprTrace "zonkTcType:ForAllTy:" (hsep [ppr tv, ppr tv_ty]) $
414 -- returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')
416 zonkTcType (FunTy ty1 ty2)
417 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
418 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
419 returnNF_Tc (FunTy ty1' ty2')