2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[TcHsSyn]{Specialisations of the @HsSyn@ syntax for the typechecker}
6 This module is an extension of @HsSyn@ syntax, for use in the type
11 TcIdBndr(..), TcIdOcc(..),
13 TcMonoBinds(..), TcHsBinds(..), TcBind(..), TcPat(..),
14 TcExpr(..), TcGRHSsAndBinds(..), TcGRHS(..), TcMatch(..),
15 TcQual(..), TcStmt(..), TcArithSeqInfo(..), TcRecordBinds(..),
18 TypecheckedHsBinds(..), TypecheckedBind(..),
19 TypecheckedMonoBinds(..), TypecheckedPat(..),
20 TypecheckedHsExpr(..), TypecheckedArithSeqInfo(..),
21 TypecheckedQual(..), TypecheckedStmt(..),
22 TypecheckedMatch(..), TypecheckedHsModule(..),
23 TypecheckedGRHSsAndBinds(..), TypecheckedGRHS(..),
24 TypecheckedRecordBinds(..),
26 mkHsTyApp, mkHsDictApp,
27 mkHsTyLam, mkHsDictLam,
37 import HsSyn -- oodles of it
38 import Id ( GenId(..), IdDetails, PragmaInfo, -- Can meddle modestly with Ids
40 IdEnv(..), growIdEnvList, lookupIdEnv
44 import Name ( Name{--O only-} )
45 import TcMonad hiding ( rnMtoTcM )
46 import TcType ( TcType(..), TcMaybe, TcTyVar(..),
47 zonkTcTypeToType, zonkTcTyVarToTyVar,
50 import Usage ( UVar(..) )
51 import Util ( zipEqual, panic, pprPanic, pprTrace )
53 import PprType ( GenType, GenTyVar ) -- instances
54 import Type ( mkTyVarTy )
55 import TyVar ( GenTyVar {- instances -},
56 TyVarEnv(..), growTyVarEnvList ) -- instances
57 import TysWiredIn ( voidTy )
58 import Unique ( Unique ) -- instances
68 The @Tc...@ datatypes are the ones that apply {\em during} type checking.
69 All the types in @Tc...@ things have mutable type-variables in them for
72 At the end of type checking we zonk everything to @Typechecked...@ datatypes,
73 which have immutable type variables in them.
76 type TcIdBndr s = GenId (TcType s) -- Binders are all TcTypes
77 data TcIdOcc s = TcId (TcIdBndr s) -- Bindees may be either
80 type TcHsBinds s = HsBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
81 type TcBind s = Bind (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
82 type TcMonoBinds s = MonoBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
83 type TcPat s = OutPat (TcTyVar s) UVar (TcIdOcc s)
84 type TcExpr s = HsExpr (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
85 type TcGRHSsAndBinds s = GRHSsAndBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
86 type TcGRHS s = GRHS (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
87 type TcMatch s = Match (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
88 type TcQual s = Qual (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
89 type TcStmt s = Stmt (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
90 type TcArithSeqInfo s = ArithSeqInfo (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
91 type TcRecordBinds s = HsRecordBinds (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
92 type TcHsModule s = HsModule (TcTyVar s) UVar (TcIdOcc s) (TcPat s)
94 type TypecheckedPat = OutPat TyVar UVar Id
95 type TypecheckedMonoBinds = MonoBinds TyVar UVar Id TypecheckedPat
96 type TypecheckedHsBinds = HsBinds TyVar UVar Id TypecheckedPat
97 type TypecheckedBind = Bind TyVar UVar Id TypecheckedPat
98 type TypecheckedHsExpr = HsExpr TyVar UVar Id TypecheckedPat
99 type TypecheckedArithSeqInfo = ArithSeqInfo TyVar UVar Id TypecheckedPat
100 type TypecheckedQual = Qual TyVar UVar Id TypecheckedPat
101 type TypecheckedStmt = Stmt TyVar UVar Id TypecheckedPat
102 type TypecheckedMatch = Match TyVar UVar Id TypecheckedPat
103 type TypecheckedGRHSsAndBinds = GRHSsAndBinds TyVar UVar Id TypecheckedPat
104 type TypecheckedGRHS = GRHS TyVar UVar Id TypecheckedPat
105 type TypecheckedRecordBinds = HsRecordBinds TyVar UVar Id TypecheckedPat
106 type TypecheckedHsModule = HsModule TyVar UVar Id TypecheckedPat
110 mkHsTyApp expr [] = expr
111 mkHsTyApp expr tys = TyApp expr tys
113 mkHsDictApp expr [] = expr
114 mkHsDictApp expr dict_vars = DictApp expr dict_vars
116 mkHsTyLam [] expr = expr
117 mkHsTyLam tyvars expr = TyLam tyvars expr
119 mkHsDictLam [] expr = expr
120 mkHsDictLam dicts expr = DictLam dicts expr
122 tcIdType :: TcIdOcc s -> TcType s
123 tcIdType (TcId id) = idType id
124 tcIdType (RealId id) = pprPanic "tcIdType:" (ppr PprDebug id)
130 instance Eq (TcIdOcc s) where
131 (TcId id1) == (TcId id2) = id1 == id2
132 (RealId id1) == (RealId id2) = id1 == id2
135 instance Outputable (TcIdOcc s) where
136 ppr sty (TcId id) = ppr sty id
137 ppr sty (RealId id) = ppr sty id
139 instance NamedThing (TcIdOcc s) where
140 getName (TcId id) = getName id
141 getName (RealId id) = getName id
145 %************************************************************************
147 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
149 %************************************************************************
151 This zonking pass runs over the bindings
153 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
154 b) convert unbound TcTyVar to Void
156 We pass an environment around so that
157 a) we know which TyVars are unbound
158 b) we maintain sharing; eg an Id is zonked at its binding site and they
159 all occurrences of that Id point to the common zonked copy
161 It's all pretty boring stuff, because HsSyn is such a large type, and
162 the environment manipulation is tiresome.
166 zonkIdBndr :: TyVarEnv Type -> TcIdOcc s -> NF_TcM s Id
167 zonkIdBndr te (TcId (Id u n ty details prags info))
168 = zonkTcTypeToType te ty `thenNF_Tc` \ ty' ->
169 returnNF_Tc (Id u n ty' details prags info)
171 zonkIdBndr te (RealId id) = returnNF_Tc id
173 zonkIdOcc :: IdEnv Id -> TcIdOcc s -> Id
174 zonkIdOcc ve (RealId id) = id
175 zonkIdOcc ve (TcId id) = case (lookupIdEnv ve id) of
177 Nothing -> pprTrace "zonkIdOcc: " (ppr PprDebug id) $
178 Id u n voidTy details prags info
180 Id u n _ details prags info = id
182 extend_ve ve ids = growIdEnvList ve [(id,id) | id <- ids]
183 extend_te te tyvars = growTyVarEnvList te [(tyvar, mkTyVarTy tyvar) | tyvar <- tyvars]
187 -- Implicitly mutually recursive, which is overkill,
188 -- but it means that later ones see earlier ones
189 zonkDictBinds te ve dbs
190 = fixNF_Tc (\ ~(_,new_ve) ->
191 zonkDictBindsLocal te new_ve dbs `thenNF_Tc` \ (new_binds, dict_ids) ->
192 returnNF_Tc (new_binds, extend_ve ve dict_ids)
195 -- The ..Local version assumes the caller has set up
196 -- a ve that contains all the things bound here
197 zonkDictBindsLocal te ve [] = returnNF_Tc ([], [])
199 zonkDictBindsLocal te ve ((dict,rhs) : binds)
200 = zonkIdBndr te dict `thenNF_Tc` \ new_dict ->
201 zonkExpr te ve rhs `thenNF_Tc` \ new_rhs ->
202 zonkDictBindsLocal te ve binds `thenNF_Tc` \ (new_binds, dict_ids) ->
203 returnNF_Tc ((new_dict,new_rhs) : new_binds,
208 zonkBinds :: TyVarEnv Type -> IdEnv Id
209 -> TcHsBinds s -> NF_TcM s (TypecheckedHsBinds, IdEnv Id)
211 zonkBinds te ve EmptyBinds = returnNF_Tc (EmptyBinds, ve)
213 zonkBinds te ve (ThenBinds binds1 binds2)
214 = zonkBinds te ve binds1 `thenNF_Tc` \ (new_binds1, ve1) ->
215 zonkBinds te ve1 binds2 `thenNF_Tc` \ (new_binds2, ve2) ->
216 returnNF_Tc (ThenBinds new_binds1 new_binds2, ve2)
218 zonkBinds te ve (SingleBind bind)
219 = fixNF_Tc (\ ~(_,new_ve) ->
220 zonkBind te new_ve bind `thenNF_Tc` \ (new_bind, new_ids) ->
221 returnNF_Tc (SingleBind new_bind, extend_ve ve new_ids)
224 zonkBinds te ve (AbsBinds tyvars dicts locprs dict_binds val_bind)
225 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
227 new_te = extend_te te new_tyvars
229 mapNF_Tc (zonkIdBndr new_te) dicts `thenNF_Tc` \ new_dicts ->
230 mapNF_Tc (zonkIdBndr new_te) globals `thenNF_Tc` \ new_globals ->
232 ve1 = extend_ve ve new_globals
233 ve2 = extend_ve ve1 new_dicts
235 fixNF_Tc (\ ~(_, ve3) ->
236 zonkDictBindsLocal new_te ve3 dict_binds `thenNF_Tc` \ (new_dict_binds, ds) ->
237 zonkBind new_te ve3 val_bind `thenNF_Tc` \ (new_val_bind, ls) ->
239 new_locprs = zipEqual "zonkBinds" (map (zonkIdOcc ve3) locals) new_globals
241 returnNF_Tc (AbsBinds new_tyvars new_dicts new_locprs new_dict_binds new_val_bind,
242 extend_ve ve2 (ds++ls))
243 ) `thenNF_Tc` \ (binds, _) ->
244 returnNF_Tc (binds, ve1) -- Yes, the "ve1" is right (SLPJ)
246 (locals, globals) = unzip locprs
250 -------------------------------------------------------------------------
251 zonkBind :: TyVarEnv Type -> IdEnv Id
252 -> TcBind s -> NF_TcM s (TypecheckedBind, [Id])
254 zonkBind te ve EmptyBind = returnNF_Tc (EmptyBind, [])
256 zonkBind te ve (NonRecBind mbinds)
257 = zonkMonoBinds te ve mbinds `thenNF_Tc` \ (new_mbinds, new_ids) ->
258 returnNF_Tc (NonRecBind new_mbinds, new_ids)
260 zonkBind te ve (RecBind mbinds)
261 = zonkMonoBinds te ve mbinds `thenNF_Tc` \ (new_mbinds, new_ids) ->
262 returnNF_Tc (RecBind new_mbinds, new_ids)
264 -------------------------------------------------------------------------
265 zonkMonoBinds :: TyVarEnv Type -> IdEnv Id
266 -> TcMonoBinds s -> NF_TcM s (TypecheckedMonoBinds, [Id])
268 zonkMonoBinds te ve EmptyMonoBinds = returnNF_Tc (EmptyMonoBinds, [])
270 zonkMonoBinds te ve (AndMonoBinds mbinds1 mbinds2)
271 = zonkMonoBinds te ve mbinds1 `thenNF_Tc` \ (new_mbinds1, ids1) ->
272 zonkMonoBinds te ve mbinds2 `thenNF_Tc` \ (new_mbinds2, ids2) ->
273 returnNF_Tc (AndMonoBinds new_mbinds1 new_mbinds2, ids1 ++ ids2)
275 zonkMonoBinds te ve (PatMonoBind pat grhss_w_binds locn)
276 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
277 zonkGRHSsAndBinds te ve grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds ->
278 returnNF_Tc (PatMonoBind new_pat new_grhss_w_binds locn, ids)
280 zonkMonoBinds te ve (VarMonoBind var expr)
281 = zonkIdBndr te var `thenNF_Tc` \ new_var ->
282 zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
283 returnNF_Tc (VarMonoBind new_var new_expr, [new_var])
285 zonkMonoBinds te ve (FunMonoBind var inf ms locn)
286 = zonkIdBndr te var `thenNF_Tc` \ new_var ->
287 mapNF_Tc (zonkMatch te ve) ms `thenNF_Tc` \ new_ms ->
288 returnNF_Tc (FunMonoBind new_var inf new_ms locn, [new_var])
291 %************************************************************************
293 \subsection[BackSubst-Match-GRHSs]{Match and GRHSsAndBinds}
295 %************************************************************************
298 zonkMatch :: TyVarEnv Type -> IdEnv Id
299 -> TcMatch s -> NF_TcM s TypecheckedMatch
301 zonkMatch te ve (PatMatch pat match)
302 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
304 new_ve = extend_ve ve ids
306 zonkMatch te new_ve match `thenNF_Tc` \ new_match ->
307 returnNF_Tc (PatMatch new_pat new_match)
309 zonkMatch te ve (GRHSMatch grhss_w_binds)
310 = zonkGRHSsAndBinds te ve grhss_w_binds `thenNF_Tc` \ new_grhss_w_binds ->
311 returnNF_Tc (GRHSMatch new_grhss_w_binds)
313 zonkMatch te ve (SimpleMatch expr)
314 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
315 returnNF_Tc (SimpleMatch new_expr)
317 -------------------------------------------------------------------------
318 zonkGRHSsAndBinds :: TyVarEnv Type -> IdEnv Id
320 -> NF_TcM s TypecheckedGRHSsAndBinds
322 zonkGRHSsAndBinds te ve (GRHSsAndBindsOut grhss binds ty)
323 = zonkBinds te ve binds `thenNF_Tc` \ (new_binds, new_ve) ->
325 zonk_grhs (GRHS guard expr locn)
326 = zonkExpr te new_ve guard `thenNF_Tc` \ new_guard ->
327 zonkExpr te new_ve expr `thenNF_Tc` \ new_expr ->
328 returnNF_Tc (GRHS new_guard new_expr locn)
330 zonk_grhs (OtherwiseGRHS expr locn)
331 = zonkExpr te new_ve expr `thenNF_Tc` \ new_expr ->
332 returnNF_Tc (OtherwiseGRHS new_expr locn)
334 mapNF_Tc zonk_grhs grhss `thenNF_Tc` \ new_grhss ->
335 zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
336 returnNF_Tc (GRHSsAndBindsOut new_grhss new_binds new_ty)
339 %************************************************************************
341 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
343 %************************************************************************
346 zonkExpr :: TyVarEnv Type -> IdEnv Id
347 -> TcExpr s -> NF_TcM s TypecheckedHsExpr
349 zonkExpr te ve (HsVar name)
350 = returnNF_Tc (HsVar (zonkIdOcc ve name))
352 zonkExpr te ve (HsLit _) = panic "zonkExpr te ve:HsLit"
354 zonkExpr te ve (HsLitOut lit ty)
355 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
356 returnNF_Tc (HsLitOut lit new_ty)
358 zonkExpr te ve (HsLam match)
359 = zonkMatch te ve match `thenNF_Tc` \ new_match ->
360 returnNF_Tc (HsLam new_match)
362 zonkExpr te ve (HsApp e1 e2)
363 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
364 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
365 returnNF_Tc (HsApp new_e1 new_e2)
367 zonkExpr te ve (OpApp e1 op e2)
368 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
369 zonkExpr te ve op `thenNF_Tc` \ new_op ->
370 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
371 returnNF_Tc (OpApp new_e1 new_op new_e2)
373 zonkExpr te ve (NegApp _ _) = panic "zonkExpr te ve:NegApp"
374 zonkExpr te ve (HsPar _) = panic "zonkExpr te ve:HsPar"
376 zonkExpr te ve (SectionL expr op)
377 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
378 zonkExpr te ve op `thenNF_Tc` \ new_op ->
379 returnNF_Tc (SectionL new_expr new_op)
381 zonkExpr te ve (SectionR op expr)
382 = zonkExpr te ve op `thenNF_Tc` \ new_op ->
383 zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
384 returnNF_Tc (SectionR new_op new_expr)
386 zonkExpr te ve (HsCase expr ms src_loc)
387 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
388 mapNF_Tc (zonkMatch te ve) ms `thenNF_Tc` \ new_ms ->
389 returnNF_Tc (HsCase new_expr new_ms src_loc)
391 zonkExpr te ve (HsIf e1 e2 e3 src_loc)
392 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
393 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
394 zonkExpr te ve e3 `thenNF_Tc` \ new_e3 ->
395 returnNF_Tc (HsIf new_e1 new_e2 new_e3 src_loc)
397 zonkExpr te ve (HsLet binds expr)
398 = zonkBinds te ve binds `thenNF_Tc` \ (new_binds, new_ve) ->
399 zonkExpr te new_ve expr `thenNF_Tc` \ new_expr ->
400 returnNF_Tc (HsLet new_binds new_expr)
402 zonkExpr te ve (HsDo _ _) = panic "zonkExpr te ve:HsDo"
404 zonkExpr te ve (HsDoOut stmts m_id mz_id src_loc)
405 = zonkStmts te ve stmts `thenNF_Tc` \ new_stmts ->
406 returnNF_Tc (HsDoOut new_stmts m_new mz_new src_loc)
408 m_new = zonkIdOcc ve m_id
409 mz_new = zonkIdOcc ve mz_id
411 zonkExpr te ve (ListComp expr quals)
412 = zonkQuals te ve quals `thenNF_Tc` \ (new_quals, new_ve) ->
413 zonkExpr te new_ve expr `thenNF_Tc` \ new_expr ->
414 returnNF_Tc (ListComp new_expr new_quals)
416 zonkExpr te ve (ExplicitList _) = panic "zonkExpr te ve:ExplicitList"
418 zonkExpr te ve (ExplicitListOut ty exprs)
419 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
420 mapNF_Tc (zonkExpr te ve) exprs `thenNF_Tc` \ new_exprs ->
421 returnNF_Tc (ExplicitListOut new_ty new_exprs)
423 zonkExpr te ve (ExplicitTuple exprs)
424 = mapNF_Tc (zonkExpr te ve) exprs `thenNF_Tc` \ new_exprs ->
425 returnNF_Tc (ExplicitTuple new_exprs)
427 zonkExpr te ve (RecordCon con rbinds)
428 = zonkExpr te ve con `thenNF_Tc` \ new_con ->
429 zonkRbinds te ve rbinds `thenNF_Tc` \ new_rbinds ->
430 returnNF_Tc (RecordCon new_con new_rbinds)
432 zonkExpr te ve (RecordUpd _ _) = panic "zonkExpr te ve:RecordUpd"
434 zonkExpr te ve (RecordUpdOut expr dicts rbinds)
435 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
436 zonkRbinds te ve rbinds `thenNF_Tc` \ new_rbinds ->
437 returnNF_Tc (RecordUpdOut new_expr new_dicts new_rbinds)
439 new_dicts = map (zonkIdOcc ve) dicts
441 zonkExpr te ve (ExprWithTySig _ _) = panic "zonkExpr te ve:ExprWithTySig"
442 zonkExpr te ve (ArithSeqIn _) = panic "zonkExpr te ve:ArithSeqIn"
444 zonkExpr te ve (ArithSeqOut expr info)
445 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
446 zonkArithSeq te ve info `thenNF_Tc` \ new_info ->
447 returnNF_Tc (ArithSeqOut new_expr new_info)
449 zonkExpr te ve (CCall fun args may_gc is_casm result_ty)
450 = mapNF_Tc (zonkExpr te ve) args `thenNF_Tc` \ new_args ->
451 zonkTcTypeToType te result_ty `thenNF_Tc` \ new_result_ty ->
452 returnNF_Tc (CCall fun new_args may_gc is_casm new_result_ty)
454 zonkExpr te ve (HsSCC label expr)
455 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
456 returnNF_Tc (HsSCC label new_expr)
458 zonkExpr te ve (TyLam tyvars expr)
459 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
461 new_te = extend_te te new_tyvars
463 zonkExpr new_te ve expr `thenNF_Tc` \ new_expr ->
464 returnNF_Tc (TyLam new_tyvars new_expr)
466 zonkExpr te ve (TyApp expr tys)
467 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
468 mapNF_Tc (zonkTcTypeToType te) tys `thenNF_Tc` \ new_tys ->
469 returnNF_Tc (TyApp new_expr new_tys)
471 zonkExpr te ve (DictLam dicts expr)
472 = mapNF_Tc (zonkIdBndr te) dicts `thenNF_Tc` \ new_dicts ->
474 new_ve = extend_ve ve new_dicts
476 zonkExpr te new_ve expr `thenNF_Tc` \ new_expr ->
477 returnNF_Tc (DictLam new_dicts new_expr)
479 zonkExpr te ve (DictApp expr dicts)
480 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
481 returnNF_Tc (DictApp new_expr new_dicts)
483 new_dicts = map (zonkIdOcc ve) dicts
485 zonkExpr te ve (ClassDictLam dicts methods expr)
486 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
487 returnNF_Tc (ClassDictLam new_dicts new_methods new_expr)
489 new_dicts = map (zonkIdOcc ve) dicts
490 new_methods = map (zonkIdOcc ve) methods
493 zonkExpr te ve (Dictionary dicts methods)
494 = returnNF_Tc (Dictionary new_dicts new_methods)
496 new_dicts = map (zonkIdOcc ve) dicts
497 new_methods = map (zonkIdOcc ve) methods
499 zonkExpr te ve (SingleDict name)
500 = returnNF_Tc (SingleDict (zonkIdOcc ve name))
502 zonkExpr te ve (HsCon con tys vargs)
503 = mapNF_Tc (zonkTcTypeToType te) tys `thenNF_Tc` \ new_tys ->
504 mapNF_Tc (zonkExpr te ve) vargs `thenNF_Tc` \ new_vargs ->
505 returnNF_Tc (HsCon con new_tys new_vargs)
507 -------------------------------------------------------------------------
508 zonkArithSeq :: TyVarEnv Type -> IdEnv Id
509 -> TcArithSeqInfo s -> NF_TcM s TypecheckedArithSeqInfo
511 zonkArithSeq te ve (From e)
512 = zonkExpr te ve e `thenNF_Tc` \ new_e ->
513 returnNF_Tc (From new_e)
515 zonkArithSeq te ve (FromThen e1 e2)
516 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
517 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
518 returnNF_Tc (FromThen new_e1 new_e2)
520 zonkArithSeq te ve (FromTo e1 e2)
521 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
522 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
523 returnNF_Tc (FromTo new_e1 new_e2)
525 zonkArithSeq te ve (FromThenTo e1 e2 e3)
526 = zonkExpr te ve e1 `thenNF_Tc` \ new_e1 ->
527 zonkExpr te ve e2 `thenNF_Tc` \ new_e2 ->
528 zonkExpr te ve e3 `thenNF_Tc` \ new_e3 ->
529 returnNF_Tc (FromThenTo new_e1 new_e2 new_e3)
531 -------------------------------------------------------------------------
532 zonkQuals :: TyVarEnv Type -> IdEnv Id
533 -> [TcQual s] -> NF_TcM s ([TypecheckedQual], IdEnv Id)
536 = returnNF_Tc ([], ve)
538 zonkQuals te ve (GeneratorQual pat expr : quals)
539 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
540 zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
542 new_ve = extend_ve ve ids
544 zonkQuals te new_ve quals `thenNF_Tc` \ (new_quals, final_ve) ->
545 returnNF_Tc (GeneratorQual new_pat new_expr : new_quals, final_ve)
547 zonkQuals te ve (FilterQual expr : quals)
548 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
549 zonkQuals te ve quals `thenNF_Tc` \ (new_quals, final_ve) ->
550 returnNF_Tc (FilterQual new_expr : new_quals, final_ve)
552 zonkQuals te ve (LetQual binds : quals)
553 = zonkBinds te ve binds `thenNF_Tc` \ (new_binds, new_ve) ->
554 zonkQuals te new_ve quals `thenNF_Tc` \ (new_quals, final_ve) ->
555 returnNF_Tc (LetQual new_binds : new_quals, final_ve)
557 -------------------------------------------------------------------------
558 zonkStmts :: TyVarEnv Type -> IdEnv Id
559 -> [TcStmt s] -> NF_TcM s [TypecheckedStmt]
564 zonkStmts te ve (BindStmt pat expr src_loc : stmts)
565 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
566 zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
568 new_ve = extend_ve ve ids
570 zonkStmts te new_ve stmts `thenNF_Tc` \ new_stmts ->
571 returnNF_Tc (BindStmt new_pat new_expr src_loc : new_stmts)
573 zonkStmts te ve (ExprStmt expr src_loc : stmts)
574 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
575 zonkStmts te ve stmts `thenNF_Tc` \ new_stmts ->
576 returnNF_Tc (ExprStmt new_expr src_loc : new_stmts)
578 zonkStmts te ve (LetStmt binds : stmts)
579 = zonkBinds te ve binds `thenNF_Tc` \ (new_binds, new_ve) ->
580 zonkStmts te new_ve stmts `thenNF_Tc` \ new_stmts ->
581 returnNF_Tc (LetStmt new_binds : new_stmts)
583 -------------------------------------------------------------------------
584 zonkRbinds :: TyVarEnv Type -> IdEnv Id
585 -> TcRecordBinds s -> NF_TcM s TypecheckedRecordBinds
587 zonkRbinds te ve rbinds
588 = mapNF_Tc zonk_rbind rbinds
590 zonk_rbind (field, expr, pun)
591 = zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
592 returnNF_Tc (zonkIdOcc ve field, new_expr, pun)
595 %************************************************************************
597 \subsection[BackSubst-Pats]{Patterns}
599 %************************************************************************
602 zonkPat :: TyVarEnv Type -> IdEnv Id
603 -> TcPat s -> NF_TcM s (TypecheckedPat, [Id])
605 zonkPat te ve (WildPat ty)
606 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
607 returnNF_Tc (WildPat new_ty, [])
609 zonkPat te ve (VarPat v)
610 = zonkIdBndr te v `thenNF_Tc` \ new_v ->
611 returnNF_Tc (VarPat new_v, [new_v])
613 zonkPat te ve (LazyPat pat)
614 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
615 returnNF_Tc (LazyPat new_pat, ids)
617 zonkPat te ve (AsPat n pat)
618 = zonkIdBndr te n `thenNF_Tc` \ new_n ->
619 zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
620 returnNF_Tc (AsPat new_n new_pat, new_n:ids)
622 zonkPat te ve (ConPat n ty pats)
623 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
624 zonkPats te ve pats `thenNF_Tc` \ (new_pats, ids) ->
625 returnNF_Tc (ConPat n new_ty new_pats, ids)
627 zonkPat te ve (ConOpPat pat1 op pat2 ty)
628 = zonkPat te ve pat1 `thenNF_Tc` \ (new_pat1, ids1) ->
629 zonkPat te ve pat2 `thenNF_Tc` \ (new_pat2, ids2) ->
630 zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
631 returnNF_Tc (ConOpPat new_pat1 op new_pat2 new_ty, ids1 ++ ids2)
633 zonkPat te ve (ListPat ty pats)
634 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
635 zonkPats te ve pats `thenNF_Tc` \ (new_pats, ids) ->
636 returnNF_Tc (ListPat new_ty new_pats, ids)
638 zonkPat te ve (TuplePat pats)
639 = zonkPats te ve pats `thenNF_Tc` \ (new_pats, ids) ->
640 returnNF_Tc (TuplePat new_pats, ids)
642 zonkPat te ve (RecPat n ty rpats)
643 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
644 mapAndUnzipNF_Tc zonk_rpat rpats `thenNF_Tc` \ (new_rpats, ids_s) ->
645 returnNF_Tc (RecPat n new_ty new_rpats, concat ids_s)
647 zonk_rpat (f, pat, pun)
648 = zonkPat te ve pat `thenNF_Tc` \ (new_pat, ids) ->
649 returnNF_Tc ((f, new_pat, pun), ids)
651 zonkPat te ve (LitPat lit ty)
652 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
653 returnNF_Tc (LitPat lit new_ty, [])
655 zonkPat te ve (NPat lit ty expr)
656 = zonkTcTypeToType te ty `thenNF_Tc` \ new_ty ->
657 zonkExpr te ve expr `thenNF_Tc` \ new_expr ->
658 returnNF_Tc (NPat lit new_ty new_expr, [])
660 zonkPat te ve (DictPat ds ms)
661 = mapNF_Tc (zonkIdBndr te) ds `thenNF_Tc` \ new_ds ->
662 mapNF_Tc (zonkIdBndr te) ms `thenNF_Tc` \ new_ms ->
663 returnNF_Tc (DictPat new_ds new_ms, new_ds ++ new_ms)
667 = returnNF_Tc ([], [])
668 zonkPats te ve (pat:pats)
669 = zonkPat te ve pat `thenNF_Tc` \ (pat', ids1) ->
670 zonkPats te ve pats `thenNF_Tc` \ (pats', ids2) ->
671 returnNF_Tc (pat':pats', ids1 ++ ids2)