7 newTyVarTy, -- Kind -> NF_TcM s (TcType s)
8 newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType s]
10 -----------------------------------------
12 TcTauType, TcThetaType, TcRhoType,
14 -- Find the type to which a type variable is bound
15 tcWriteTyVar, -- :: TcTyVar s -> TcType s -> NF_TcM (TcType s)
16 tcReadTyVar, -- :: TcTyVar s -> NF_TcM (TcMaybe s)
19 tcSplitForAllTy, tcSplitRhoTy,
24 tcInstSigType, tcInstTcType, tcInstSigTcType,
27 zonkTcTyVars, zonkSigTyVar,
28 zonkTcType, zonkTcTypes, zonkTcThetaType,
35 #include "HsVersions.h"
39 import Type ( Type, ThetaType, GenType(..), mkAppTy,
40 tyVarsOfTypes, getTyVar_maybe, splitDictTy_maybe,
41 splitForAllTys, splitRhoTy, isTyVarTy,
42 mkForAllTys, instantiateTy
44 import TyVar ( TyVar, GenTyVar(..), TyVarSet, GenTyVarSet,
45 TyVarEnv, lookupTyVarEnv, addToTyVarEnv,
46 emptyTyVarEnv, mkTyVarEnv, zipTyVarEnv,
51 import Class ( Class )
52 import TyCon ( isFunTyCon )
54 import TcKind ( TcKind )
57 import TysPrim ( voidTy )
59 import Name ( NamedThing(..) )
60 import Unique ( Unique )
61 import UniqFM ( UniqFM )
62 import Maybes ( assocMaybe )
63 import BasicTypes ( unused )
64 import Util ( zipEqual, nOfThem )
75 type TcType s = GenType (TcBox s) -- Used during typechecker
76 -- Invariant on ForAllTy in TcTypes:
78 -- a cannot occur inside a MutTyVar in T; that is,
79 -- T is "flattened" before quantifying over a
81 type TcThetaType s = [(Class, [TcType s])]
82 type TcRhoType s = TcType s -- No ForAllTys
83 type TcTauType s = TcType s -- No DictTys or ForAllTys
85 type TcBox s = TcRef s (TcMaybe s)
87 data TcMaybe s = UnBound
90 -- Interestingly, you can't use (Maybe (TcType s)) instead of (TcMaybe s),
91 -- because you get a synonym loop if you do!
93 type TcTyVar s = GenTyVar (TcBox s)
94 type TcTyVarSet s = GenTyVarSet (TcBox s)
98 tcTyVarToTyVar :: TcTyVar s -> TyVar
99 tcTyVarToTyVar (TyVar uniq kind name _) = TyVar uniq kind name unused
104 These tcSplit functions are like their non-Tc analogues, but they
105 follow through bound type variables.
108 tcSplitForAllTy :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
112 go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
113 go syn_t (SynTy _ t) tvs = go syn_t t tvs
114 go syn_t (TyVarTy tv) tvs = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
116 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty tvs
117 other -> returnNF_Tc (reverse tvs, syn_t)
118 go syn_t t tvs = returnNF_Tc (reverse tvs, syn_t)
120 tcSplitRhoTy :: TcType s -> NF_TcM s (TcThetaType s, TcType s)
124 -- A type variable is never instantiated to a dictionary type,
125 -- so we don't need to do a tcReadVar on the "arg".
126 go syn_t (FunTy arg res) ts = case splitDictTy_maybe arg of
127 Just pair -> go res res (pair:ts)
128 Nothing -> returnNF_Tc (reverse ts, syn_t)
129 go syn_t (SynTy _ t) ts = go syn_t t ts
130 go syn_t (TyVarTy tv) ts = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
132 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty ts
133 other -> returnNF_Tc (reverse ts, syn_t)
134 go syn_t t ts = returnNF_Tc (reverse ts, syn_t)
142 newTcTyVar :: Kind -> NF_TcM s (TcTyVar s)
144 = tcGetUnique `thenNF_Tc` \ uniq ->
145 tcNewMutVar UnBound `thenNF_Tc` \ box ->
146 returnNF_Tc (TyVar uniq kind Nothing box)
148 newTyVarTy :: Kind -> NF_TcM s (TcType s)
150 = newTcTyVar kind `thenNF_Tc` \ tc_tyvar ->
151 returnNF_Tc (TyVarTy tc_tyvar)
153 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType s]
154 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
157 -- For signature type variables, use the user name for the type variable
158 tcInstTyVars, tcInstSigTyVars
160 -> NF_TcM s ([TcTyVar s], [TcType s], TyVarEnv (TcType s))
162 tcInstTyVars tyvars = inst_tyvars inst_tyvar tyvars
163 tcInstSigTyVars tyvars = inst_tyvars inst_sig_tyvar tyvars
165 inst_tyvars inst tyvars
166 = mapNF_Tc inst tyvars `thenNF_Tc` \ tc_tyvars ->
168 tys = map TyVarTy tc_tyvars
170 returnNF_Tc (tc_tyvars, tys, zipTyVarEnv tyvars tys)
172 inst_tyvar (TyVar _ kind name _)
173 = tcGetUnique `thenNF_Tc` \ uniq ->
174 tcNewMutVar UnBound `thenNF_Tc` \ box ->
175 returnNF_Tc (TyVar uniq kind Nothing box)
176 -- The "Nothing" means that it'll always print with its
177 -- unique (or something similar). If we leave the original (Just Name)
178 -- in there then error messages will say "can't match (T a) against (T a)"
180 inst_sig_tyvar (TyVar _ kind name _)
181 = tcGetUnique `thenNF_Tc` \ uniq ->
183 tcNewMutVar UnBound `thenNF_Tc` \ box ->
184 -- Was DontBind, but we've nuked that "optimisation"
186 returnNF_Tc (TyVar uniq kind name box)
187 -- We propagate the name of the sigature type variable
190 @tcInstType@ and @tcInstSigType@ both create a fresh instance of a
191 type, returning a @TcType@. All inner for-alls are instantiated with
194 The difference is that tcInstType instantiates all forall'd type
195 variables (and their bindees) with anonymous type variables, whereas
196 tcInstSigType instantiates them with named type variables.
197 @tcInstSigType@ also doesn't take an environment.
199 On the other hand, @tcInstTcType@ instantiates a TcType. It uses
200 instantiateTy which could take advantage of sharing some day.
203 tcInstTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
205 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
207 [] -> returnNF_Tc ([], ty) -- Nothing to do
208 other -> tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
209 returnNF_Tc (tyvars', instantiateTy tenv rho)
211 tcInstSigTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
213 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
215 [] -> returnNF_Tc ([], ty) -- Nothing to do
216 other -> tcInstSigTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
217 returnNF_Tc (tyvars', instantiateTy tenv rho)
219 tcInstType :: TyVarEnv (TcType s)
221 -> NF_TcM s (TcType s)
222 tcInstType tenv ty_to_inst
223 = tcConvert bind_fn occ_fn tenv ty_to_inst
226 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
227 Just ty -> returnNF_Tc ty
228 Nothing -> panic "tcInstType:1" --(vcat [ppr ty_to_inst,
231 tcInstSigType :: GenType flexi -> NF_TcM s (TcType s)
232 tcInstSigType ty_to_inst
233 = tcConvert bind_fn occ_fn emptyTyVarEnv ty_to_inst
235 bind_fn = inst_sig_tyvar -- Note: inst_sig_tyvar, not inst_tyvar
236 -- I don't think that can lead to strange error messages
237 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
238 Just ty -> returnNF_Tc ty
239 Nothing -> panic "tcInstType:2"-- (vcat [ppr ty_to_inst,
242 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
243 zonkTcTyVarToTyVar tv
244 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
245 case tv_ty of -- Should be a tyvar!
247 TyVarTy tv' -> returnNF_Tc (tcTyVarToTyVar tv')
249 _ -> --pprTrace "zonkTcTyVarToTyVar:" (hsep [ppr tv, ppr tv_ty]) $
250 returnNF_Tc (tcTyVarToTyVar tv)
253 zonkTcTypeToType :: TyVarEnv Type -> TcType s -> NF_TcM s Type
254 zonkTcTypeToType env ty
255 = tcConvert zonkTcTyVarToTyVar occ_fn env ty
258 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
260 BoundTo (TyVarTy tyvar') -> lookup env tyvar'
261 BoundTo other_ty -> tcConvert zonkTcTyVarToTyVar occ_fn env other_ty
262 other -> lookup env tyvar
264 lookup env tyvar = case lookupTyVarEnv env tyvar of
265 Just ty -> returnNF_Tc ty
266 Nothing -> returnNF_Tc voidTy -- Unbound type variables go to Void
269 tcConvert bind_fn occ_fn env ty_to_convert
270 = doo env ty_to_convert
272 doo env (TyConApp tycon tys) = mapNF_Tc (doo env) tys `thenNF_Tc` \ tys' ->
273 returnNF_Tc (TyConApp tycon tys')
275 doo env (SynTy ty1 ty2) = doo env ty1 `thenNF_Tc` \ ty1' ->
276 doo env ty2 `thenNF_Tc` \ ty2' ->
277 returnNF_Tc (SynTy ty1' ty2')
279 doo env (FunTy arg res) = doo env arg `thenNF_Tc` \ arg' ->
280 doo env res `thenNF_Tc` \ res' ->
281 returnNF_Tc (FunTy arg' res')
283 doo env (AppTy fun arg) = doo env fun `thenNF_Tc` \ fun' ->
284 doo env arg `thenNF_Tc` \ arg' ->
285 returnNF_Tc (mkAppTy fun' arg')
287 -- The two interesting cases!
288 doo env (TyVarTy tv) = occ_fn env tv
290 doo env (ForAllTy tyvar ty)
291 = bind_fn tyvar `thenNF_Tc` \ tyvar' ->
293 new_env = addToTyVarEnv env tyvar (TyVarTy tyvar')
295 doo new_env ty `thenNF_Tc` \ ty' ->
296 returnNF_Tc (ForAllTy tyvar' ty')
299 tcInstTheta :: TyVarEnv (TcType s) -> ThetaType -> NF_TcM s (TcThetaType s)
300 tcInstTheta tenv theta
303 go (clas,tys) = mapNF_Tc (tcInstType tenv) tys `thenNF_Tc` \ tc_tys ->
304 returnNF_Tc (clas, tc_tys)
307 Reading and writing TcTyVars
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310 tcWriteTyVar :: TcTyVar s -> TcType s -> NF_TcM s ()
311 tcReadTyVar :: TcTyVar s -> NF_TcM s (TcMaybe s)
317 tcWriteTyVar (TyVar uniq kind name box) ty = tcWriteMutVar box (BoundTo ty)
320 Reading is more interesting. The easy thing to do is just to read, thus:
322 tcReadTyVar (TyVar uniq kind name box) = tcReadMutVar box
325 But it's more fun to short out indirections on the way: If this
326 version returns a TyVar, then that TyVar is unbound. If it returns
327 any other type, then there might be bound TyVars embedded inside it.
329 We return Nothing iff the original box was unbound.
332 tcReadTyVar (TyVar uniq kind name box)
333 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
335 BoundTo ty -> short_out ty `thenNF_Tc` \ ty' ->
336 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
337 returnNF_Tc (BoundTo ty')
339 other -> returnNF_Tc other
341 short_out :: TcType s -> NF_TcM s (TcType s)
342 short_out ty@(TyVarTy (TyVar uniq kind name box))
343 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
345 BoundTo ty' -> short_out ty' `thenNF_Tc` \ ty' ->
346 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
349 other -> returnNF_Tc ty
351 short_out other_ty = returnNF_Tc other_ty
358 zonkTcTyVars :: TcTyVarSet s -> NF_TcM s (TcTyVarSet s)
360 = mapNF_Tc zonkTcTyVar (tyVarSetToList tyvars) `thenNF_Tc` \ tys ->
361 returnNF_Tc (tyVarsOfTypes tys)
363 zonkTcTyVar :: TcTyVar s -> NF_TcM s (TcType s)
365 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
367 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc ty -- tcReadTyVar never returns a bound tyvar
368 BoundTo other -> zonkTcType other
369 other -> returnNF_Tc (TyVarTy tyvar)
371 -- Signature type variables only get bound to each other,
373 zonkSigTyVar :: TcTyVar s -> NF_TcM s (TcTyVar s)
375 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
377 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc tyvar' -- tcReadTyVar never returns a bound tyvar
378 BoundTo other -> panic "zonkSigTyVar" -- Should only be bound to another tyvar
379 other -> returnNF_Tc tyvar
381 zonkTcTypes :: [TcType s] -> NF_TcM s [TcType s]
382 zonkTcTypes tys = mapNF_Tc zonkTcType tys
384 zonkTcThetaType :: TcThetaType s -> NF_TcM s (TcThetaType s)
385 zonkTcThetaType theta = mapNF_Tc zonk theta
387 zonk (c,ts) = zonkTcTypes ts `thenNF_Tc` \ new_ts ->
388 returnNF_Tc (c, new_ts)
390 zonkTcType :: TcType s -> NF_TcM s (TcType s)
392 zonkTcType (TyVarTy tyvar) = zonkTcTyVar tyvar
394 zonkTcType (AppTy ty1 ty2)
395 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
396 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
397 returnNF_Tc (mkAppTy ty1' ty2')
399 zonkTcType (TyConApp tc tys)
400 = mapNF_Tc zonkTcType tys `thenNF_Tc` \ tys' ->
401 returnNF_Tc (TyConApp tc tys')
403 zonkTcType (SynTy ty1 ty2)
404 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
405 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
406 returnNF_Tc (SynTy ty1' ty2')
408 zonkTcType (ForAllTy tv ty)
409 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
410 zonkTcType ty `thenNF_Tc` \ ty' ->
411 case tv_ty of -- Should be a tyvar!
412 TyVarTy tv' -> returnNF_Tc (ForAllTy tv' ty')
413 _ -> panic "zonkTcType"
414 -- pprTrace "zonkTcType:ForAllTy:" (hsep [ppr tv, ppr tv_ty]) $
415 -- returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')
417 zonkTcType (FunTy ty1 ty2)
418 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
419 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
420 returnNF_Tc (FunTy ty1' ty2')