2 % (c) The AQUA Project, Glasgow University, 1996-1998
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 TcMonoBinds, TcHsBinds, TcPat,
12 TcExpr, TcGRHSs, TcGRHS, TcMatch,
13 TcStmt, TcArithSeqInfo, TcRecordBinds,
14 TcHsModule, TcCoreExpr, TcDictBinds,
17 TypecheckedHsBinds, TypecheckedRuleDecl,
18 TypecheckedMonoBinds, TypecheckedPat,
19 TypecheckedHsExpr, TypecheckedArithSeqInfo,
20 TypecheckedStmt, TypecheckedForeignDecl,
21 TypecheckedMatch, TypecheckedHsModule,
22 TypecheckedGRHSs, TypecheckedGRHS,
23 TypecheckedRecordBinds, TypecheckedDictBinds,
24 TypecheckedMatchContext,
26 mkHsTyApp, mkHsDictApp, mkHsConApp,
27 mkHsTyLam, mkHsDictLam, mkHsLet,
29 -- re-exported from TcEnv
32 zonkTopBinds, zonkId, zonkIdBndr, zonkIdOcc, zonkExpr,
33 zonkForeignExports, zonkRules
36 #include "HsVersions.h"
39 import HsSyn -- oodles of it
42 import Id ( idName, idType, setIdType, Id )
43 import DataCon ( dataConWrapId )
44 import TcEnv ( tcLookupGlobal_maybe, tcExtendGlobalValEnv, TcEnv, TcId )
47 import TcMType ( zonkTcTypeToType, zonkTcTyVarToTyVar, zonkTcType, zonkTcSigTyVars )
48 import CoreSyn ( Expr )
49 import BasicTypes ( RecFlag(..) )
52 import HscTypes ( TyThing(..) )
59 The @Tc...@ datatypes are the ones that apply {\em during} type checking.
60 All the types in @Tc...@ things have mutable type-variables in them for
63 At the end of type checking we zonk everything to @Typechecked...@ datatypes,
64 which have immutable type variables in them.
67 type TcHsBinds = HsBinds TcId TcPat
68 type TcMonoBinds = MonoBinds TcId TcPat
69 type TcDictBinds = TcMonoBinds
70 type TcPat = OutPat TcId
71 type TcExpr = HsExpr TcId TcPat
72 type TcGRHSs = GRHSs TcId TcPat
73 type TcGRHS = GRHS TcId TcPat
74 type TcMatch = Match TcId TcPat
75 type TcStmt = Stmt TcId TcPat
76 type TcArithSeqInfo = ArithSeqInfo TcId TcPat
77 type TcRecordBinds = HsRecordBinds TcId TcPat
78 type TcHsModule = HsModule TcId TcPat
80 type TcCoreExpr = Expr TcId
81 type TcForeignExportDecl = ForeignDecl TcId
82 type TcRuleDecl = RuleDecl TcId TcPat
84 type TypecheckedPat = OutPat Id
85 type TypecheckedMonoBinds = MonoBinds Id TypecheckedPat
86 type TypecheckedDictBinds = TypecheckedMonoBinds
87 type TypecheckedHsBinds = HsBinds Id TypecheckedPat
88 type TypecheckedHsExpr = HsExpr Id TypecheckedPat
89 type TypecheckedArithSeqInfo = ArithSeqInfo Id TypecheckedPat
90 type TypecheckedStmt = Stmt Id TypecheckedPat
91 type TypecheckedMatch = Match Id TypecheckedPat
92 type TypecheckedMatchContext = HsMatchContext Id
93 type TypecheckedGRHSs = GRHSs Id TypecheckedPat
94 type TypecheckedGRHS = GRHS Id TypecheckedPat
95 type TypecheckedRecordBinds = HsRecordBinds Id TypecheckedPat
96 type TypecheckedHsModule = HsModule Id TypecheckedPat
97 type TypecheckedForeignDecl = ForeignDecl Id
98 type TypecheckedRuleDecl = RuleDecl Id TypecheckedPat
102 mkHsTyApp expr [] = expr
103 mkHsTyApp expr tys = TyApp expr tys
105 mkHsDictApp expr [] = expr
106 mkHsDictApp expr dict_vars = DictApp expr dict_vars
108 mkHsTyLam [] expr = expr
109 mkHsTyLam tyvars expr = TyLam tyvars expr
111 mkHsDictLam [] expr = expr
112 mkHsDictLam dicts expr = DictLam dicts expr
114 mkHsLet EmptyMonoBinds expr = expr
115 mkHsLet mbinds expr = HsLet (MonoBind mbinds [] Recursive) expr
117 mkHsConApp data_con tys args = foldl HsApp (HsVar (dataConWrapId data_con) `mkHsTyApp` tys) args
120 %************************************************************************
122 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
124 %************************************************************************
126 This zonking pass runs over the bindings
128 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
129 b) convert unbound TcTyVar to Void
130 c) convert each TcId to an Id by zonking its type
132 The type variables are converted by binding mutable tyvars to immutable ones
133 and then zonking as normal.
135 The Ids are converted by binding them in the normal Tc envt; that
136 way we maintain sharing; eg an Id is zonked at its binding site and they
137 all occurrences of that Id point to the common zonked copy
139 It's all pretty boring stuff, because HsSyn is such a large type, and
140 the environment manipulation is tiresome.
143 -- zonkId is used *during* typechecking just to zonk the Id's type
144 zonkId :: TcId -> NF_TcM TcId
146 = zonkTcType (idType id) `thenNF_Tc` \ ty' ->
147 returnNF_Tc (setIdType id ty')
149 -- zonkIdBndr is used *after* typechecking to get the Id's type
150 -- to its final form. The TyVarEnv give
151 zonkIdBndr :: TcId -> NF_TcM Id
153 = zonkTcTypeToType (idType id) `thenNF_Tc` \ ty' ->
154 returnNF_Tc (setIdType id ty')
156 zonkIdOcc :: TcId -> NF_TcM Id
158 = tcLookupGlobal_maybe (idName id) `thenNF_Tc` \ maybe_id' ->
159 -- We're even look up up superclass selectors and constructors;
160 -- even though zonking them is a no-op anyway, and the
161 -- superclass selectors aren't in the environment anyway.
162 -- But we don't want to call isLocalId to find out whether
163 -- it's a superclass selector (for example) because that looks
164 -- at the IdInfo field, which in turn be in a knot because of
165 -- the big knot in typecheckModule
167 new_id = case maybe_id' of
168 Just (AnId id') -> id'
169 other -> id -- WARN( isLocalId id, ppr id ) id
170 -- Oops: the warning can give a black hole
171 -- because it looks at the idinfo
178 zonkTopBinds :: TcMonoBinds -> NF_TcM (TypecheckedMonoBinds, TcEnv)
179 zonkTopBinds binds -- Top level is implicitly recursive
180 = fixNF_Tc (\ ~(_, new_ids) ->
181 tcExtendGlobalValEnv (bagToList new_ids) $
182 zonkMonoBinds binds `thenNF_Tc` \ (binds', new_ids) ->
183 tcGetEnv `thenNF_Tc` \ env ->
184 returnNF_Tc ((binds', env), new_ids)
185 ) `thenNF_Tc` \ (stuff, _) ->
188 zonkBinds :: TcHsBinds -> NF_TcM (TypecheckedHsBinds, TcEnv)
191 = go binds (\ binds' -> tcGetEnv `thenNF_Tc` \ env ->
192 returnNF_Tc (binds', env))
195 -- -> (TypecheckedHsBinds
196 -- -> NF_TcM (TypecheckedHsBinds, TcEnv)
198 -- -> NF_TcM (TypecheckedHsBinds, TcEnv)
200 go (ThenBinds b1 b2) thing_inside = go b1 $ \ b1' ->
202 thing_inside (b1' `ThenBinds` b2')
204 go EmptyBinds thing_inside = thing_inside EmptyBinds
206 go (MonoBind bind sigs is_rec) thing_inside
207 = ASSERT( null sigs )
208 fixNF_Tc (\ ~(_, new_ids) ->
209 tcExtendGlobalValEnv (bagToList new_ids) $
210 zonkMonoBinds bind `thenNF_Tc` \ (new_bind, new_ids) ->
211 thing_inside (mkMonoBind new_bind [] is_rec) `thenNF_Tc` \ stuff ->
212 returnNF_Tc (stuff, new_ids)
213 ) `thenNF_Tc` \ (stuff, _) ->
218 -------------------------------------------------------------------------
219 zonkMonoBinds :: TcMonoBinds
220 -> NF_TcM (TypecheckedMonoBinds, Bag Id)
222 zonkMonoBinds EmptyMonoBinds = returnNF_Tc (EmptyMonoBinds, emptyBag)
224 zonkMonoBinds (AndMonoBinds mbinds1 mbinds2)
225 = zonkMonoBinds mbinds1 `thenNF_Tc` \ (b1', ids1) ->
226 zonkMonoBinds mbinds2 `thenNF_Tc` \ (b2', ids2) ->
227 returnNF_Tc (b1' `AndMonoBinds` b2',
228 ids1 `unionBags` ids2)
230 zonkMonoBinds (PatMonoBind pat grhss locn)
231 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
232 zonkGRHSs grhss `thenNF_Tc` \ new_grhss ->
233 returnNF_Tc (PatMonoBind new_pat new_grhss locn, ids)
235 zonkMonoBinds (VarMonoBind var expr)
236 = zonkIdBndr var `thenNF_Tc` \ new_var ->
237 zonkExpr expr `thenNF_Tc` \ new_expr ->
238 returnNF_Tc (VarMonoBind new_var new_expr, unitBag new_var)
240 zonkMonoBinds (CoreMonoBind var core_expr)
241 = zonkIdBndr var `thenNF_Tc` \ new_var ->
242 returnNF_Tc (CoreMonoBind new_var core_expr, unitBag new_var)
244 zonkMonoBinds (FunMonoBind var inf ms locn)
245 = zonkIdBndr var `thenNF_Tc` \ new_var ->
246 mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms ->
247 returnNF_Tc (FunMonoBind new_var inf new_ms locn, unitBag new_var)
250 zonkMonoBinds (AbsBinds tyvars dicts exports inlines val_bind)
251 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
252 -- No need to extend tyvar env: the effects are
253 -- propagated through binding the tyvars themselves
255 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
256 tcExtendGlobalValEnv new_dicts $
258 fixNF_Tc (\ ~(_, _, val_bind_ids) ->
259 tcExtendGlobalValEnv (bagToList val_bind_ids) $
260 zonkMonoBinds val_bind `thenNF_Tc` \ (new_val_bind, val_bind_ids) ->
261 mapNF_Tc zonkExport exports `thenNF_Tc` \ new_exports ->
262 returnNF_Tc (new_val_bind, new_exports, val_bind_ids)
263 ) `thenNF_Tc ` \ (new_val_bind, new_exports, _) ->
265 new_globals = listToBag [global | (_, global, local) <- new_exports]
267 returnNF_Tc (AbsBinds new_tyvars new_dicts new_exports inlines new_val_bind,
270 zonkExport (tyvars, global, local)
271 = zonkTcSigTyVars tyvars `thenNF_Tc` \ new_tyvars ->
272 -- This isn't the binding occurrence of these tyvars
273 -- but they should *be* tyvars. Hence zonkTcSigTyVars.
274 zonkIdBndr global `thenNF_Tc` \ new_global ->
275 zonkIdOcc local `thenNF_Tc` \ new_local ->
276 returnNF_Tc (new_tyvars, new_global, new_local)
279 %************************************************************************
281 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
283 %************************************************************************
286 zonkMatch :: TcMatch -> NF_TcM TypecheckedMatch
288 zonkMatch (Match _ pats _ grhss)
289 = zonkPats pats `thenNF_Tc` \ (new_pats, new_ids) ->
290 tcExtendGlobalValEnv (bagToList new_ids) $
291 zonkGRHSs grhss `thenNF_Tc` \ new_grhss ->
292 returnNF_Tc (Match [] new_pats Nothing new_grhss)
294 -------------------------------------------------------------------------
296 -> NF_TcM TypecheckedGRHSs
298 zonkGRHSs (GRHSs grhss binds (Just ty))
299 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
302 zonk_grhs (GRHS guarded locn)
303 = zonkStmts guarded `thenNF_Tc` \ new_guarded ->
304 returnNF_Tc (GRHS new_guarded locn)
306 mapNF_Tc zonk_grhs grhss `thenNF_Tc` \ new_grhss ->
307 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
308 returnNF_Tc (GRHSs new_grhss new_binds (Just new_ty))
311 %************************************************************************
313 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
315 %************************************************************************
318 zonkExpr :: TcExpr -> NF_TcM TypecheckedHsExpr
321 = zonkIdOcc id `thenNF_Tc` \ id' ->
322 returnNF_Tc (HsVar id')
324 zonkExpr (HsIPVar id)
325 = zonkIdOcc id `thenNF_Tc` \ id' ->
326 returnNF_Tc (HsIPVar id')
328 zonkExpr (HsLit (HsRat f ty))
329 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
330 returnNF_Tc (HsLit (HsRat f new_ty))
332 zonkExpr (HsLit (HsLitLit lit ty))
333 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
334 returnNF_Tc (HsLit (HsLitLit lit new_ty))
337 = returnNF_Tc (HsLit lit)
339 -- HsOverLit doesn't appear in typechecker output
341 zonkExpr (HsLam match)
342 = zonkMatch match `thenNF_Tc` \ new_match ->
343 returnNF_Tc (HsLam new_match)
345 zonkExpr (HsApp e1 e2)
346 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
347 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
348 returnNF_Tc (HsApp new_e1 new_e2)
350 zonkExpr (OpApp e1 op fixity e2)
351 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
352 zonkExpr op `thenNF_Tc` \ new_op ->
353 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
354 returnNF_Tc (OpApp new_e1 new_op fixity new_e2)
356 zonkExpr (NegApp _) = panic "zonkExpr: NegApp"
357 zonkExpr (HsPar _) = panic "zonkExpr: HsPar"
359 zonkExpr (SectionL expr op)
360 = zonkExpr expr `thenNF_Tc` \ new_expr ->
361 zonkExpr op `thenNF_Tc` \ new_op ->
362 returnNF_Tc (SectionL new_expr new_op)
364 zonkExpr (SectionR op expr)
365 = zonkExpr op `thenNF_Tc` \ new_op ->
366 zonkExpr expr `thenNF_Tc` \ new_expr ->
367 returnNF_Tc (SectionR new_op new_expr)
369 zonkExpr (HsCase expr ms src_loc)
370 = zonkExpr expr `thenNF_Tc` \ new_expr ->
371 mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms ->
372 returnNF_Tc (HsCase new_expr new_ms src_loc)
374 zonkExpr (HsIf e1 e2 e3 src_loc)
375 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
376 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
377 zonkExpr e3 `thenNF_Tc` \ new_e3 ->
378 returnNF_Tc (HsIf new_e1 new_e2 new_e3 src_loc)
380 zonkExpr (HsLet binds expr)
381 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
383 zonkExpr expr `thenNF_Tc` \ new_expr ->
384 returnNF_Tc (HsLet new_binds new_expr)
386 zonkExpr (HsWith expr binds)
387 = zonkIPBinds binds `thenNF_Tc` \ new_binds ->
388 tcExtendGlobalValEnv (map fst new_binds) $
389 zonkExpr expr `thenNF_Tc` \ new_expr ->
390 returnNF_Tc (HsWith new_expr new_binds)
392 zonkIPBinds = mapNF_Tc zonkIPBind
394 zonkIdBndr n `thenNF_Tc` \ n' ->
395 zonkExpr e `thenNF_Tc` \ e' ->
398 zonkExpr (HsDo _ _ _) = panic "zonkExpr:HsDo"
400 zonkExpr (HsDoOut do_or_lc stmts return_id then_id zero_id ty src_loc)
401 = zonkStmts stmts `thenNF_Tc` \ new_stmts ->
402 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
403 zonkIdOcc return_id `thenNF_Tc` \ new_return_id ->
404 zonkIdOcc then_id `thenNF_Tc` \ new_then_id ->
405 zonkIdOcc zero_id `thenNF_Tc` \ new_zero_id ->
406 returnNF_Tc (HsDoOut do_or_lc new_stmts new_return_id new_then_id new_zero_id
409 zonkExpr (ExplicitList _) = panic "zonkExpr:ExplicitList"
411 zonkExpr (ExplicitListOut ty exprs)
412 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
413 mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs ->
414 returnNF_Tc (ExplicitListOut new_ty new_exprs)
416 zonkExpr (ExplicitTuple exprs boxed)
417 = mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs ->
418 returnNF_Tc (ExplicitTuple new_exprs boxed)
420 zonkExpr (RecordConOut data_con con_expr rbinds)
421 = zonkExpr con_expr `thenNF_Tc` \ new_con_expr ->
422 zonkRbinds rbinds `thenNF_Tc` \ new_rbinds ->
423 returnNF_Tc (RecordConOut data_con new_con_expr new_rbinds)
425 zonkExpr (RecordUpd _ _) = panic "zonkExpr:RecordUpd"
427 zonkExpr (RecordUpdOut expr ty dicts rbinds)
428 = zonkExpr expr `thenNF_Tc` \ new_expr ->
429 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
430 mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts ->
431 zonkRbinds rbinds `thenNF_Tc` \ new_rbinds ->
432 returnNF_Tc (RecordUpdOut new_expr new_ty new_dicts new_rbinds)
434 zonkExpr (ExprWithTySig _ _) = panic "zonkExpr:ExprWithTySig"
435 zonkExpr (ArithSeqIn _) = panic "zonkExpr:ArithSeqIn"
437 zonkExpr (ArithSeqOut expr info)
438 = zonkExpr expr `thenNF_Tc` \ new_expr ->
439 zonkArithSeq info `thenNF_Tc` \ new_info ->
440 returnNF_Tc (ArithSeqOut new_expr new_info)
442 zonkExpr (HsCCall fun args may_gc is_casm result_ty)
443 = mapNF_Tc zonkExpr args `thenNF_Tc` \ new_args ->
444 zonkTcTypeToType result_ty `thenNF_Tc` \ new_result_ty ->
445 returnNF_Tc (HsCCall fun new_args may_gc is_casm new_result_ty)
447 zonkExpr (HsSCC lbl expr)
448 = zonkExpr expr `thenNF_Tc` \ new_expr ->
449 returnNF_Tc (HsSCC lbl new_expr)
451 zonkExpr (TyLam tyvars expr)
452 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
453 -- No need to extend tyvar env; see AbsBinds
455 zonkExpr expr `thenNF_Tc` \ new_expr ->
456 returnNF_Tc (TyLam new_tyvars new_expr)
458 zonkExpr (TyApp expr tys)
459 = zonkExpr expr `thenNF_Tc` \ new_expr ->
460 mapNF_Tc zonkTcTypeToType tys `thenNF_Tc` \ new_tys ->
461 returnNF_Tc (TyApp new_expr new_tys)
463 zonkExpr (DictLam dicts expr)
464 = mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
465 tcExtendGlobalValEnv new_dicts $
466 zonkExpr expr `thenNF_Tc` \ new_expr ->
467 returnNF_Tc (DictLam new_dicts new_expr)
469 zonkExpr (DictApp expr dicts)
470 = zonkExpr expr `thenNF_Tc` \ new_expr ->
471 mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts ->
472 returnNF_Tc (DictApp new_expr new_dicts)
476 -------------------------------------------------------------------------
477 zonkArithSeq :: TcArithSeqInfo -> NF_TcM TypecheckedArithSeqInfo
479 zonkArithSeq (From e)
480 = zonkExpr e `thenNF_Tc` \ new_e ->
481 returnNF_Tc (From new_e)
483 zonkArithSeq (FromThen e1 e2)
484 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
485 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
486 returnNF_Tc (FromThen new_e1 new_e2)
488 zonkArithSeq (FromTo e1 e2)
489 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
490 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
491 returnNF_Tc (FromTo new_e1 new_e2)
493 zonkArithSeq (FromThenTo e1 e2 e3)
494 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
495 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
496 zonkExpr e3 `thenNF_Tc` \ new_e3 ->
497 returnNF_Tc (FromThenTo new_e1 new_e2 new_e3)
499 -------------------------------------------------------------------------
500 zonkStmts :: [TcStmt]
501 -> NF_TcM [TypecheckedStmt]
503 zonkStmts [] = returnNF_Tc []
505 zonkStmts (ParStmtOut bndrstmtss : stmts)
506 = mapNF_Tc (mapNF_Tc zonkId) bndrss `thenNF_Tc` \ new_bndrss ->
507 let new_binders = concat new_bndrss in
508 mapNF_Tc zonkStmts stmtss `thenNF_Tc` \ new_stmtss ->
509 tcExtendGlobalValEnv new_binders $
510 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
511 returnNF_Tc (ParStmtOut (zip new_bndrss new_stmtss) : new_stmts)
512 where (bndrss, stmtss) = unzip bndrstmtss
514 zonkStmts (ResultStmt expr locn : stmts)
515 = zonkExpr expr `thenNF_Tc` \ new_expr ->
516 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
517 returnNF_Tc (ResultStmt new_expr locn : new_stmts)
519 zonkStmts (ExprStmt expr locn : stmts)
520 = zonkExpr expr `thenNF_Tc` \ new_expr ->
521 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
522 returnNF_Tc (ExprStmt new_expr locn : new_stmts)
524 zonkStmts (LetStmt binds : stmts)
525 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
527 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
528 returnNF_Tc (LetStmt new_binds : new_stmts)
530 zonkStmts (BindStmt pat expr locn : stmts)
531 = zonkExpr expr `thenNF_Tc` \ new_expr ->
532 zonkPat pat `thenNF_Tc` \ (new_pat, new_ids) ->
533 tcExtendGlobalValEnv (bagToList new_ids) $
534 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
535 returnNF_Tc (BindStmt new_pat new_expr locn : new_stmts)
539 -------------------------------------------------------------------------
540 zonkRbinds :: TcRecordBinds -> NF_TcM TypecheckedRecordBinds
543 = mapNF_Tc zonk_rbind rbinds
545 zonk_rbind (field, expr, pun)
546 = zonkExpr expr `thenNF_Tc` \ new_expr ->
547 zonkIdOcc field `thenNF_Tc` \ new_field ->
548 returnNF_Tc (new_field, new_expr, pun)
551 %************************************************************************
553 \subsection[BackSubst-Pats]{Patterns}
555 %************************************************************************
558 zonkPat :: TcPat -> NF_TcM (TypecheckedPat, Bag Id)
561 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
562 returnNF_Tc (WildPat new_ty, emptyBag)
565 = zonkIdBndr v `thenNF_Tc` \ new_v ->
566 returnNF_Tc (VarPat new_v, unitBag new_v)
568 zonkPat (LazyPat pat)
569 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
570 returnNF_Tc (LazyPat new_pat, ids)
572 zonkPat (AsPat n pat)
573 = zonkIdBndr n `thenNF_Tc` \ new_n ->
574 zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
575 returnNF_Tc (AsPat new_n new_pat, new_n `consBag` ids)
577 zonkPat (ListPat ty pats)
578 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
579 zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
580 returnNF_Tc (ListPat new_ty new_pats, ids)
582 zonkPat (TuplePat pats boxed)
583 = zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
584 returnNF_Tc (TuplePat new_pats boxed, ids)
586 zonkPat (ConPat n ty tvs dicts pats)
587 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
588 mapNF_Tc zonkTcTyVarToTyVar tvs `thenNF_Tc` \ new_tvs ->
589 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
590 tcExtendGlobalValEnv new_dicts $
591 zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
592 returnNF_Tc (ConPat n new_ty new_tvs new_dicts new_pats,
593 listToBag new_dicts `unionBags` ids)
595 zonkPat (RecPat n ty tvs dicts rpats)
596 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
597 mapNF_Tc zonkTcTyVarToTyVar tvs `thenNF_Tc` \ new_tvs ->
598 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
599 tcExtendGlobalValEnv new_dicts $
600 mapAndUnzipNF_Tc zonk_rpat rpats `thenNF_Tc` \ (new_rpats, ids_s) ->
601 returnNF_Tc (RecPat n new_ty new_tvs new_dicts new_rpats,
602 listToBag new_dicts `unionBags` unionManyBags ids_s)
604 zonk_rpat (f, pat, pun)
605 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
606 returnNF_Tc ((f, new_pat, pun), ids)
608 zonkPat (LitPat lit ty)
609 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
610 returnNF_Tc (LitPat lit new_ty, emptyBag)
612 zonkPat (NPat lit ty expr)
613 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
614 zonkExpr expr `thenNF_Tc` \ new_expr ->
615 returnNF_Tc (NPat lit new_ty new_expr, emptyBag)
617 zonkPat (NPlusKPat n k ty e1 e2)
618 = zonkIdBndr n `thenNF_Tc` \ new_n ->
619 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
620 zonkExpr e1 `thenNF_Tc` \ new_e1 ->
621 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
622 returnNF_Tc (NPlusKPat new_n k new_ty new_e1 new_e2, unitBag new_n)
624 zonkPat (DictPat ds ms)
625 = mapNF_Tc zonkIdBndr ds `thenNF_Tc` \ new_ds ->
626 mapNF_Tc zonkIdBndr ms `thenNF_Tc` \ new_ms ->
627 returnNF_Tc (DictPat new_ds new_ms,
628 listToBag new_ds `unionBags` listToBag new_ms)
632 = returnNF_Tc ([], emptyBag)
635 = zonkPat pat `thenNF_Tc` \ (pat', ids1) ->
636 zonkPats pats `thenNF_Tc` \ (pats', ids2) ->
637 returnNF_Tc (pat':pats', ids1 `unionBags` ids2)
640 %************************************************************************
642 \subsection[BackSubst-Foreign]{Foreign exports}
644 %************************************************************************
648 zonkForeignExports :: [TcForeignExportDecl] -> NF_TcM [TypecheckedForeignDecl]
649 zonkForeignExports ls = mapNF_Tc zonkForeignExport ls
651 zonkForeignExport :: TcForeignExportDecl -> NF_TcM (TypecheckedForeignDecl)
652 zonkForeignExport (ForeignExport i hs_ty spec src_loc) =
653 zonkIdOcc i `thenNF_Tc` \ i' ->
654 returnNF_Tc (ForeignExport i' undefined spec src_loc)
658 zonkRules :: [TcRuleDecl] -> NF_TcM [TypecheckedRuleDecl]
659 zonkRules rs = mapNF_Tc zonkRule rs
661 zonkRule (HsRule name tyvars vars lhs rhs loc)
662 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
663 mapNF_Tc zonkIdBndr [v | RuleBndr v <- vars] `thenNF_Tc` \ new_bndrs ->
664 tcExtendGlobalValEnv new_bndrs $
665 zonkExpr lhs `thenNF_Tc` \ new_lhs ->
666 zonkExpr rhs `thenNF_Tc` \ new_rhs ->
667 returnNF_Tc (HsRule name new_tyvars (map RuleBndr new_bndrs) new_lhs new_rhs loc)
668 -- I hate this map RuleBndr stuff
670 zonkRule (IfaceRuleOut fun rule)
671 = zonkIdOcc fun `thenNF_Tc` \ fun' ->
672 returnNF_Tc (IfaceRuleOut fun' rule)