2 #include "HsVersions.h"
8 newTyVarTy, -- Kind -> NF_TcM s (TcType s)
9 newTyVarTys, -- Int -> Kind -> NF_TcM s [TcType s]
14 -----------------------------------------
15 TcType(..), TcMaybe(..),
16 TcTauType(..), TcThetaType(..), 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)
25 tcInstType, tcInstSigType, tcInstTcType,
26 tcInstTheta, tcInstId,
39 import Type ( Type(..), ThetaType(..), GenType(..),
40 tyVarsOfTypes, getTyVar_maybe,
41 splitForAllTy, splitRhoTy,
42 mkForAllTys, instantiateTy
44 import TyVar ( TyVar(..), GenTyVar(..), TyVarSet(..), GenTyVarSet(..),
45 TyVarEnv(..), lookupTyVarEnv, addOneToTyVarEnv,
46 nullTyVarEnv, mkTyVarEnv,
51 import Class ( GenClass )
54 import TcKind ( TcKind )
55 import TcMonad hiding ( rnMtoTcM )
56 import Usage ( Usage(..), GenUsage, UVar(..), duffUsage )
58 import TysPrim ( voidTy )
61 import Unique ( Unique )
62 import UniqFM ( UniqFM )
63 import Maybes ( assocMaybe )
64 import Util ( zipEqual, nOfThem, panic, pprPanic, pprTrace{-ToDo:rm-} )
66 import Outputable ( Outputable(..) ) -- Debugging messages
67 import PprType ( GenTyVar, GenType )
68 import Pretty -- ditto
69 import PprStyle ( PprStyle(..) ) -- ditto
78 type TcType s = GenType (TcTyVar s) UVar -- Used during typechecker
79 -- Invariant on ForAllTy in TcTypes:
81 -- a cannot occur inside a MutTyVar in T; that is,
82 -- T is "flattened" before quantifying over a
84 type TcThetaType s = [(Class, TcType s)]
85 type TcRhoType s = TcType s -- No ForAllTys
86 type TcTauType s = TcType s -- No DictTys or ForAllTys
88 type Box s = MutableVar s (TcMaybe s)
90 data TcMaybe s = UnBound
92 | DontBind -- This variant is used for tyvars
93 -- arising from type signatures, or
94 -- existentially quantified tyvars;
95 -- The idea is that we must not unify
96 -- such tyvars with anything except
99 -- Interestingly, you can't use (Maybe (TcType s)) instead of (TcMaybe s),
100 -- because you get a synonym loop if you do!
102 type TcTyVar s = GenTyVar (Box s)
103 type TcTyVarSet s = GenTyVarSet (Box s)
107 tcTyVarToTyVar :: TcTyVar s -> TyVar
108 tcTyVarToTyVar (TyVar uniq kind name _) = TyVar uniq kind name duffUsage
115 newTcTyVar :: Kind -> NF_TcM s (TcTyVar s)
117 = tcGetUnique `thenNF_Tc` \ uniq ->
118 tcNewMutVar UnBound `thenNF_Tc` \ box ->
119 returnNF_Tc (TyVar uniq kind Nothing box)
121 newTyVarTy :: Kind -> NF_TcM s (TcType s)
123 = newTcTyVar kind `thenNF_Tc` \ tc_tyvar ->
124 returnNF_Tc (TyVarTy tc_tyvar)
126 newTyVarTys :: Int -> Kind -> NF_TcM s [TcType s]
127 newTyVarTys n kind = mapNF_Tc newTyVarTy (nOfThem n kind)
130 -- For signature type variables, mark them as "DontBind"
131 tcInstTyVars, tcInstSigTyVars
133 -> NF_TcM s ([TcTyVar s], [TcType s], [(GenTyVar flexi, TcType s)])
135 tcInstTyVars tyvars = inst_tyvars UnBound tyvars
136 tcInstSigTyVars tyvars = inst_tyvars DontBind tyvars
138 inst_tyvars initial_cts tyvars
139 = mapNF_Tc (inst_tyvar initial_cts) tyvars `thenNF_Tc` \ tc_tyvars ->
141 tys = map TyVarTy tc_tyvars
143 returnNF_Tc (tc_tyvars, tys, zipEqual "inst_tyvars" tyvars tys)
145 inst_tyvar initial_cts (TyVar _ kind name _)
146 = tcGetUnique `thenNF_Tc` \ uniq ->
147 tcNewMutVar initial_cts `thenNF_Tc` \ box ->
148 returnNF_Tc (TyVar uniq kind name box)
151 @tcInstType@ and @tcInstSigType@ both create a fresh instance of a
152 type, returning a @TcType@. All inner for-alls are instantiated with
155 The difference is that tcInstType instantiates all forall'd type
156 variables (and their bindees) with UnBound type variables, whereas
157 tcInstSigType instantiates them with DontBind types variables.
158 @tcInstSigType@ also doesn't take an environment.
160 On the other hand, @tcInstTcType@ instantiates a TcType. It uses
161 instantiateTy which could take advantage of sharing some day.
164 tcInstTcType :: TcType s -> NF_TcM s ([TcTyVar s], TcType s)
167 [] -> returnNF_Tc ([], ty) -- Nothing to do
168 other -> tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', _, tenv) ->
169 returnNF_Tc (tyvars', instantiateTy tenv rho)
171 (tyvars, rho) = splitForAllTy ty
173 tcInstType :: [(GenTyVar flexi,TcType s)]
174 -> GenType (GenTyVar flexi) UVar
175 -> NF_TcM s (TcType s)
176 tcInstType tenv ty_to_inst
177 = tcConvert bind_fn occ_fn (mkTyVarEnv tenv) ty_to_inst
179 bind_fn = inst_tyvar UnBound
180 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
181 Just ty -> returnNF_Tc ty
182 Nothing -> pprPanic "tcInstType:" (ppAboves [ppr PprDebug ty_to_inst,
185 tcInstSigType :: GenType (GenTyVar flexi) UVar -> NF_TcM s (TcType s)
186 tcInstSigType ty_to_inst
187 = tcConvert bind_fn occ_fn nullTyVarEnv ty_to_inst
189 bind_fn = inst_tyvar DontBind
190 occ_fn env tyvar = case lookupTyVarEnv env tyvar of
191 Just ty -> returnNF_Tc ty
192 Nothing -> pprPanic "tcInstType:" (ppAboves [ppr PprDebug ty_to_inst,
195 zonkTcTyVarToTyVar :: TcTyVar s -> NF_TcM s TyVar
196 zonkTcTyVarToTyVar tv
197 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
198 case tv_ty of -- Should be a tyvar!
200 TyVarTy tv' -> returnNF_Tc (tcTyVarToTyVar tv')
202 _ -> pprTrace "zonkTcTyVarToTyVar:" (ppCat [ppr PprDebug tv, ppr PprDebug tv_ty]) $
203 returnNF_Tc (tcTyVarToTyVar tv)
206 zonkTcTypeToType :: TyVarEnv Type -> TcType s -> NF_TcM s Type
207 zonkTcTypeToType env ty
208 = tcConvert zonkTcTyVarToTyVar occ_fn env ty
211 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
213 BoundTo (TyVarTy tyvar') -> lookup env tyvar'
214 BoundTo other_ty -> tcConvert zonkTcTyVarToTyVar occ_fn env other_ty
215 other -> lookup env tyvar
217 lookup env tyvar = case lookupTyVarEnv env tyvar of
218 Just ty -> returnNF_Tc ty
219 Nothing -> returnNF_Tc voidTy -- Unbound type variables go to Void
222 tcConvert bind_fn occ_fn env ty_to_convert
223 = do env ty_to_convert
225 do env (TyConTy tycon usage) = returnNF_Tc (TyConTy tycon usage)
227 do env (SynTy tycon tys ty) = mapNF_Tc (do env) tys `thenNF_Tc` \ tys' ->
228 do env ty `thenNF_Tc` \ ty' ->
229 returnNF_Tc (SynTy tycon tys' ty')
231 do env (FunTy arg res usage) = do env arg `thenNF_Tc` \ arg' ->
232 do env res `thenNF_Tc` \ res' ->
233 returnNF_Tc (FunTy arg' res' usage)
235 do env (AppTy fun arg) = do env fun `thenNF_Tc` \ fun' ->
236 do env arg `thenNF_Tc` \ arg' ->
237 returnNF_Tc (AppTy fun' arg')
239 do env (DictTy clas ty usage)= do env ty `thenNF_Tc` \ ty' ->
240 returnNF_Tc (DictTy clas ty' usage)
242 do env (ForAllUsageTy u us ty) = do env ty `thenNF_Tc` \ ty' ->
243 returnNF_Tc (ForAllUsageTy u us ty')
245 -- The two interesting cases!
246 do env (TyVarTy tv) = occ_fn env tv
248 do env (ForAllTy tyvar ty)
249 = bind_fn tyvar `thenNF_Tc` \ tyvar' ->
251 new_env = addOneToTyVarEnv env tyvar (TyVarTy tyvar')
253 do new_env ty `thenNF_Tc` \ ty' ->
254 returnNF_Tc (ForAllTy tyvar' ty')
257 tcInstTheta :: [(TyVar,TcType s)] -> ThetaType -> NF_TcM s (TcThetaType s)
258 tcInstTheta tenv theta
261 go (clas,ty) = tcInstType tenv ty `thenNF_Tc` \ tc_ty ->
262 returnNF_Tc (clas, tc_ty)
264 -- A useful function that takes an occurrence of a global thing
265 -- and instantiates its type with fresh type variables
267 -> NF_TcM s ([TcTyVar s], -- It's instantiated type
273 (tyvars, rho) = splitForAllTy (idType id)
275 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
276 tcInstType tenv rho `thenNF_Tc` \ rho' ->
278 (theta', tau') = splitRhoTy rho'
280 returnNF_Tc (tyvars', theta', tau')
283 Reading and writing TcTyVars
284 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286 tcWriteTyVar :: TcTyVar s -> TcType s -> NF_TcM s ()
287 tcReadTyVar :: TcTyVar s -> NF_TcM s (TcMaybe s)
293 tcWriteTyVar (TyVar uniq kind name box) ty = tcWriteMutVar box (BoundTo ty)
296 Reading is more interesting. The easy thing to do is just to read, thus:
298 tcReadTyVar (TyVar uniq kind name box) = tcReadMutVar box
301 But it's more fun to short out indirections on the way: If this
302 version returns a TyVar, then that TyVar is unbound. If it returns
303 any other type, then there might be bound TyVars embedded inside it.
305 We return Nothing iff the original box was unbound.
308 tcReadTyVar (TyVar uniq kind name box)
309 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
311 BoundTo ty -> short_out ty `thenNF_Tc` \ ty' ->
312 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
313 returnNF_Tc (BoundTo ty')
315 other -> returnNF_Tc other
317 short_out :: TcType s -> NF_TcM s (TcType s)
318 short_out ty@(TyVarTy (TyVar uniq kind name box))
319 = tcReadMutVar box `thenNF_Tc` \ maybe_ty ->
321 BoundTo ty' -> short_out ty' `thenNF_Tc` \ ty' ->
322 tcWriteMutVar box (BoundTo ty') `thenNF_Tc_`
325 other -> returnNF_Tc ty
327 short_out other_ty = returnNF_Tc other_ty
334 zonkTcTyVars :: TcTyVarSet s -> NF_TcM s (TcTyVarSet s)
336 = mapNF_Tc zonkTcTyVar (tyVarSetToList tyvars) `thenNF_Tc` \ tys ->
337 returnNF_Tc (tyVarsOfTypes tys)
339 zonkTcTyVar :: TcTyVar s -> NF_TcM s (TcType s)
341 = tcReadTyVar tyvar `thenNF_Tc` \ maybe_ty ->
343 BoundTo ty@(TyVarTy tyvar') -> returnNF_Tc ty
344 BoundTo other -> zonkTcType other
345 other -> returnNF_Tc (TyVarTy tyvar)
347 zonkTcType :: TcType s -> NF_TcM s (TcType s)
349 zonkTcType (TyVarTy tyvar) = zonkTcTyVar tyvar
351 zonkTcType (AppTy ty1 ty2)
352 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
353 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
354 returnNF_Tc (AppTy ty1' ty2')
356 zonkTcType (TyConTy tc u)
357 = returnNF_Tc (TyConTy tc u)
359 zonkTcType (SynTy tc tys ty)
360 = mapNF_Tc zonkTcType tys `thenNF_Tc` \ tys' ->
361 zonkTcType ty `thenNF_Tc` \ ty' ->
362 returnNF_Tc (SynTy tc tys' ty')
364 zonkTcType (ForAllTy tv ty)
365 = zonkTcTyVar tv `thenNF_Tc` \ tv_ty ->
366 zonkTcType ty `thenNF_Tc` \ ty' ->
367 case tv_ty of -- Should be a tyvar!
369 returnNF_Tc (ForAllTy tv' ty')
370 _ -> pprTrace "zonkTcType:ForAllTy:" (ppCat [ppr PprDebug tv, ppr PprDebug tv_ty]) $
372 returnNF_Tc (ForAllTy tv{-(tcTyVarToTyVar tv)-} ty')
374 zonkTcType (ForAllUsageTy uv uvs ty)
375 = panic "zonk:ForAllUsageTy"
377 zonkTcType (FunTy ty1 ty2 u)
378 = zonkTcType ty1 `thenNF_Tc` \ ty1' ->
379 zonkTcType ty2 `thenNF_Tc` \ ty2' ->
380 returnNF_Tc (FunTy ty1' ty2' u)
382 zonkTcType (DictTy c ty u)
383 = zonkTcType ty `thenNF_Tc` \ ty' ->
384 returnNF_Tc (DictTy c ty' u)