2 #include "HsVersions.h"
5 SYN_IE(TcIdBndr), TcIdOcc(..),
7 -----------------------------------------
11 newTyVarTy, -- Kind -> NF_TcM s (TcType s)
12 newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType s]
14 -----------------------------------------
15 SYN_IE(TcType), TcMaybe(..),
16 SYN_IE(TcTauType), SYN_IE(TcThetaType), SYN_IE(TcRhoType),
18 -- Find the type to which a type variable is bound
19 tcWriteTyVar, -- :: TcTyVar s -> TcType s -> NF_TcM (TcType s)
20 tcReadTyVar, -- :: TcTyVar s -> NF_TcM (TcMaybe s)
23 tcSplitForAllTy, tcSplitRhoTy,
27 tcInstType, tcInstSigType, tcInstTcType, tcInstSigTcType,
28 tcInstTheta, tcInstId,
41 import Type ( SYN_IE(Type), SYN_IE(ThetaType), GenType(..),
42 tyVarsOfTypes, getTyVar_maybe,
43 splitForAllTy, splitRhoTy, isTyVarTy,
44 mkForAllTys, instantiateTy
46 import TyVar ( SYN_IE(TyVar), GenTyVar(..), SYN_IE(TyVarSet), SYN_IE(GenTyVarSet),
47 SYN_IE(TyVarEnv), lookupTyVarEnv, addOneToTyVarEnv,
48 nullTyVarEnv, mkTyVarEnv,
51 import PprType ( GenType, GenTyVar ) -- Instances only
54 import Class ( GenClass, SYN_IE(Class) )
55 import TyCon ( isFunTyCon )
56 import Id ( idType, GenId, SYN_IE(Id) )
58 import TcKind ( TcKind )
60 import Usage ( SYN_IE(Usage), GenUsage, SYN_IE(UVar), duffUsage )
62 import TysPrim ( voidTy )
65 import Name ( NamedThing(..) )
66 import Unique ( Unique )
67 import UniqFM ( UniqFM )
68 import Maybes ( assocMaybe )
69 import Outputable ( Outputable(..) )
70 import Util ( zipEqual, nOfThem, panic{-, pprPanic, pprTrace ToDo:rm-} )
79 type TcIdBndr s = GenId (TcType s) -- Binders are all TcTypes
80 data TcIdOcc s = TcId (TcIdBndr s) -- Bindees may be either
83 instance Eq (TcIdOcc s) where
84 (TcId id1) == (TcId id2) = id1 == id2
85 (RealId id1) == (RealId id2) = id1 == id2
88 instance Outputable (TcIdOcc s) where
89 ppr sty (TcId id) = ppr sty id
90 ppr sty (RealId id) = ppr sty id
92 instance NamedThing (TcIdOcc s) where
93 getName (TcId id) = getName id
94 getName (RealId id) = getName id
99 type TcType s = GenType (TcTyVar s) UVar -- Used during typechecker
100 -- Invariant on ForAllTy in TcTypes:
102 -- a cannot occur inside a MutTyVar in T; that is,
103 -- T is "flattened" before quantifying over a
105 type TcThetaType s = [(Class, TcType s)]
106 type TcRhoType s = TcType s -- No ForAllTys
107 type TcTauType s = TcType s -- No DictTys or ForAllTys
109 type Box s = MutableVar s (TcMaybe s)
111 data TcMaybe s = UnBound
113 | DontBind -- This variant is used for tyvars
114 -- arising from type signatures, or
115 -- existentially quantified tyvars;
116 -- The idea is that we must not unify
117 -- such tyvars with anything except
120 -- Interestingly, you can't use (Maybe (TcType s)) instead of (TcMaybe s),
121 -- because you get a synonym loop if you do!
123 type TcTyVar s = GenTyVar (Box s)
124 type TcTyVarSet s = GenTyVarSet (Box s)
128 tcTyVarToTyVar :: TcTyVar s -> TyVar
129 tcTyVarToTyVar (TyVar uniq kind name _) = TyVar uniq kind name duffUsage
134 These tcSplit functions are like their non-Tc analogues, but they
135 follow through bound type variables.
138 tcSplitForAllTy :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
142 go syn_t (ForAllTy tv t) tvs = go t t (tv:tvs)
143 go syn_t (SynTy _ _ t) tvs = go syn_t t tvs
144 go syn_t (TyVarTy tv) tvs = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
146 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty tvs
147 other -> returnNF_Tc (reverse tvs, syn_t)
148 go syn_t t tvs = returnNF_Tc (reverse tvs, syn_t)
150 tcSplitRhoTy :: TcType s -> NF_TcM s ([(Class,TcType s)], TcType s)
154 go syn_t (FunTy (DictTy c t _) r _) ts = go r r ((c,t):ts)
155 go syn_t (AppTy (AppTy (TyConTy tycon _) (DictTy c t _)) r) ts
158 go syn_t (SynTy _ _ t) ts = go syn_t t ts
159 go syn_t (TyVarTy tv) ts = tcReadTyVar tv `thenNF_Tc` \ maybe_ty ->
161 BoundTo ty | not (isTyVarTy ty) -> go syn_t ty ts
162 other -> returnNF_Tc (reverse ts, syn_t)
163 go syn_t t ts = returnNF_Tc (reverse ts, syn_t)
171 newTcTyVar :: Kind -> NF_TcM s (TcTyVar s)
173 = tcGetUnique `thenNF_Tc` \ uniq ->
174 tcNewMutVar UnBound `thenNF_Tc` \ box ->
175 returnNF_Tc (TyVar uniq kind Nothing box)
177 newTyVarTy :: Kind -> NF_TcM s (TcType s)
179 = newTcTyVar kind `thenNF_Tc` \ tc_tyvar ->
180 returnNF_Tc (TyVarTy tc_tyvar)
182 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType s]
183 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
186 -- For signature type variables, mark them as "DontBind"
187 tcInstTyVars, tcInstSigTyVars
189 -> NF_TcM s ([TcTyVar s], [TcType s], [(GenTyVar flexi, TcType s)])
191 tcInstTyVars tyvars = inst_tyvars UnBound tyvars
192 tcInstSigTyVars tyvars = inst_tyvars DontBind tyvars
194 inst_tyvars initial_cts tyvars
195 = mapNF_Tc (inst_tyvar initial_cts) tyvars `thenNF_Tc` \ tc_tyvars ->
197 tys = map TyVarTy tc_tyvars
199 returnNF_Tc (tc_tyvars, tys, zipEqual "inst_tyvars" tyvars tys)
201 inst_tyvar initial_cts (TyVar _ kind name _)
202 = tcGetUnique `thenNF_Tc` \ uniq ->
203 tcNewMutVar initial_cts `thenNF_Tc` \ box ->
204 returnNF_Tc (TyVar uniq kind Nothing box)
205 -- The "Nothing" means that it'll always print with its
206 -- unique (or something similar). If we leave the original (Just Name)
207 -- in there then error messages will say "can't match (T a) against (T a)"
210 @tcInstType@ and @tcInstSigType@ both create a fresh instance of a
211 type, returning a @TcType@. All inner for-alls are instantiated with
214 The difference is that tcInstType instantiates all forall'd type
215 variables (and their bindees) with UnBound type variables, whereas
216 tcInstSigType instantiates them with DontBind types variables.
217 @tcInstSigType@ also doesn't take an environment.
219 On the other hand, @tcInstTcType@ instantiates a TcType. It uses
220 instantiateTy which could take advantage of sharing some day.
223 tcInstTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
225 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
227 [] -> returnNF_Tc ([], ty) -- Nothing to do
228 other -> tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
229 returnNF_Tc (tyvars', instantiateTy tenv rho)
231 tcInstSigTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
233 = tcSplitForAllTy ty `thenNF_Tc` \ (tyvars, rho) ->
235 [] -> returnNF_Tc ([], ty) -- Nothing to do
236 other -> tcInstSigTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
237 returnNF_Tc (tyvars', instantiateTy tenv rho)
239 tcInstType :: [(GenTyVar flexi,TcType s)]
240 -> GenType (GenTyVar flexi) UVar
241 -> NF_TcM s (TcType s)
242 tcInstType tenv ty_to_inst
243 = tcConvert bind_fn occ_fn (mkTyVarEnv tenv) ty_to_inst
245 bind_fn = inst_tyvar UnBound
246 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
247 Just ty -> returnNF_Tc ty
248 Nothing -> panic "tcInstType:1" --(vcat [ppr PprDebug ty_to_inst,
249 -- ppr PprDebug tyvar])
251 tcInstSigType :: GenType (GenTyVar flexi) UVar -> NF_TcM s (TcType s)
252 tcInstSigType ty_to_inst
253 = tcConvert bind_fn occ_fn nullTyVarEnv ty_to_inst
255 bind_fn = inst_tyvar DontBind
256 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
257 Just ty -> returnNF_Tc ty
258 Nothing -> panic "tcInstType:2"-- (vcat [ppr PprDebug ty_to_inst,
259 -- ppr PprDebug tyvar])
261 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
262 zonkTcTyVarToTyVar tv
263 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
264 case tv_ty of -- Should be a tyvar!
266 TyVarTy tv' -> returnNF_Tc (tcTyVarToTyVar tv')
268 _ -> --pprTrace "zonkTcTyVarToTyVar:" (hsep [ppr PprDebug tv, ppr PprDebug tv_ty]) $
269 returnNF_Tc (tcTyVarToTyVar tv)
272 zonkTcTypeToType :: TyVarEnv Type -> TcType s -> NF_TcM s Type
273 zonkTcTypeToType env ty
274 = tcConvert zonkTcTyVarToTyVar occ_fn env ty
277 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
279 BoundTo (TyVarTy tyvar') -> lookup env tyvar'
280 BoundTo other_ty -> tcConvert zonkTcTyVarToTyVar occ_fn env other_ty
281 other -> lookup env tyvar
283 lookup env tyvar = case lookupTyVarEnv env tyvar of
284 Just ty -> returnNF_Tc ty
285 Nothing -> returnNF_Tc voidTy -- Unbound type variables go to Void
288 tcConvert bind_fn occ_fn env ty_to_convert
289 = doo env ty_to_convert
291 doo env (TyConTy tycon usage) = returnNF_Tc (TyConTy tycon usage)
293 doo env (SynTy tycon tys ty) = mapNF_Tc (doo env) tys `thenNF_Tc` \ tys' ->
294 doo env ty `thenNF_Tc` \ ty' ->
295 returnNF_Tc (SynTy tycon tys' ty')
297 doo env (FunTy arg res usage) = doo env arg `thenNF_Tc` \ arg' ->
298 doo env res `thenNF_Tc` \ res' ->
299 returnNF_Tc (FunTy arg' res' usage)
301 doo env (AppTy fun arg) = doo env fun `thenNF_Tc` \ fun' ->
302 doo env arg `thenNF_Tc` \ arg' ->
303 returnNF_Tc (AppTy fun' arg')
305 doo env (DictTy clas ty usage)= doo env ty `thenNF_Tc` \ ty' ->
306 returnNF_Tc (DictTy clas ty' usage)
308 doo env (ForAllUsageTy u us ty) = doo env ty `thenNF_Tc` \ ty' ->
309 returnNF_Tc (ForAllUsageTy u us ty')
311 -- The two interesting cases!
312 doo env (TyVarTy tv) = occ_fn env tv
314 doo env (ForAllTy tyvar ty)
315 = bind_fn tyvar `thenNF_Tc` \ tyvar' ->
317 new_env = addOneToTyVarEnv env tyvar (TyVarTy tyvar')
319 doo new_env ty `thenNF_Tc` \ ty' ->
320 returnNF_Tc (ForAllTy tyvar' ty')
323 tcInstTheta :: [(TyVar,TcType s)] -> ThetaType -> NF_TcM s (TcThetaType s)
324 tcInstTheta tenv theta
327 go (clas,ty) = tcInstType tenv ty `thenNF_Tc` \ tc_ty ->
328 returnNF_Tc (clas, tc_ty)
330 -- A useful function that takes an occurrence of a global thing
331 -- and instantiates its type with fresh type variables
333 -> NF_TcM s ([TcTyVar s], -- It's instantiated type
339 (tyvars, rho) = splitForAllTy (idType id)
341 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
342 tcInstType tenv rho `thenNF_Tc` \ rho' ->
344 (theta', tau') = splitRhoTy rho'
346 returnNF_Tc (tyvars', theta', tau')
349 Reading and writing TcTyVars
350 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352 tcWriteTyVar :: TcTyVar s -> TcType s -> NF_TcM s ()
353 tcReadTyVar :: TcTyVar s -> NF_TcM s (TcMaybe s)
359 tcWriteTyVar (TyVar uniq kind name box) ty = tcWriteMutVar box (BoundTo ty)
362 Reading is more interesting. The easy thing to do is just to read, thus:
364 tcReadTyVar (TyVar uniq kind name box) = tcReadMutVar box
367 But it's more fun to short out indirections on the way: If this
368 version returns a TyVar, then that TyVar is unbound. If it returns
369 any other type, then there might be bound TyVars embedded inside it.
371 We return Nothing iff the original box was unbound.
374 tcReadTyVar (TyVar uniq kind name box)
375 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
377 BoundTo ty -> short_out ty `thenNF_Tc` \ ty' ->
378 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
379 returnNF_Tc (BoundTo ty')
381 other -> returnNF_Tc other
383 short_out :: TcType s -> NF_TcM s (TcType s)
384 short_out ty@(TyVarTy (TyVar uniq kind name box))
385 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
387 BoundTo ty' -> short_out ty' `thenNF_Tc` \ ty' ->
388 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
391 other -> returnNF_Tc ty
393 short_out other_ty = returnNF_Tc other_ty
400 zonkTcTyVars :: TcTyVarSet s -> NF_TcM s (TcTyVarSet s)
402 = mapNF_Tc zonkTcTyVar (tyVarSetToList tyvars) `thenNF_Tc` \ tys ->
403 returnNF_Tc (tyVarsOfTypes tys)
405 zonkTcTyVar :: TcTyVar s -> NF_TcM s (TcType s)
407 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
409 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc ty -- tcReadTyVar never returns a bound tyvar
410 BoundTo other -> zonkTcType other
411 other -> returnNF_Tc (TyVarTy tyvar)
413 zonkTcType :: TcType s -> NF_TcM s (TcType s)
415 zonkTcType (TyVarTy tyvar) = zonkTcTyVar tyvar
417 zonkTcType (AppTy ty1 ty2)
418 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
419 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
420 returnNF_Tc (AppTy ty1' ty2')
422 zonkTcType (TyConTy tc u)
423 = returnNF_Tc (TyConTy tc u)
425 zonkTcType (SynTy tc tys ty)
426 = mapNF_Tc zonkTcType tys `thenNF_Tc` \ tys' ->
427 zonkTcType ty `thenNF_Tc` \ ty' ->
428 returnNF_Tc (SynTy tc tys' ty')
430 zonkTcType (ForAllTy tv ty)
431 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
432 zonkTcType ty `thenNF_Tc` \ ty' ->
433 case tv_ty of -- Should be a tyvar!
435 returnNF_Tc (ForAllTy tv' ty')
436 _ -> --pprTrace "zonkTcType:ForAllTy:" (hsep [ppr PprDebug tv, ppr PprDebug tv_ty]) $
438 returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')
440 zonkTcType (ForAllUsageTy uv uvs ty)
441 = panic "zonk:ForAllUsageTy"
443 zonkTcType (FunTy ty1 ty2 u)
444 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
445 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
446 returnNF_Tc (FunTy ty1' ty2' u)
448 zonkTcType (DictTy c ty u)
449 = zonkTcType ty `thenNF_Tc` \ ty' ->
450 returnNF_Tc (DictTy c ty' u)