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,
25 mkHsTyApp, mkHsDictApp, mkHsConApp,
26 mkHsTyLam, mkHsDictLam, mkHsLet,
29 -- re-exported from TcEnv
34 zonkTopBinds, zonkId, zonkIdOcc,
35 zonkForeignExports, zonkRules
38 #include "HsVersions.h"
41 import HsSyn -- oodles of it
44 import Id ( idName, idType, idUnfolding, setIdType, omitIfaceSigForId, isIP, Id )
45 import DataCon ( DataCon, dataConWrapId, splitProductType_maybe )
46 import TcEnv ( tcLookupValueMaybe, tcExtendGlobalValEnv, tcGetValueEnv,
47 ValueEnv, TcId, tcInstId
51 import TcType ( TcType, TcTyVar,
52 zonkTcTypeToType, zonkTcTyVarToTyVar, zonkTcTyVarBndr, zonkTcType
54 import Type ( mkTyVarTy, splitAlgTyConApp_maybe, isUnLiftedType, Type )
55 import Name ( isLocallyDefined )
57 import VarEnv ( TyVarEnv, emptyVarEnv, extendVarEnvList )
58 import VarSet ( isEmptyVarSet )
59 import CoreSyn ( Expr )
60 import CoreUnfold( unfoldingTemplate )
61 import BasicTypes ( RecFlag(..) )
71 The @Tc...@ datatypes are the ones that apply {\em during} type checking.
72 All the types in @Tc...@ things have mutable type-variables in them for
75 At the end of type checking we zonk everything to @Typechecked...@ datatypes,
76 which have immutable type variables in them.
79 type TcHsBinds = HsBinds TcId TcPat
80 type TcMonoBinds = MonoBinds TcId TcPat
81 type TcDictBinds = TcMonoBinds
82 type TcPat = OutPat TcId
83 type TcExpr = HsExpr TcId TcPat
84 type TcGRHSs = GRHSs TcId TcPat
85 type TcGRHS = GRHS TcId TcPat
86 type TcMatch = Match TcId TcPat
87 type TcStmt = Stmt TcId TcPat
88 type TcArithSeqInfo = ArithSeqInfo TcId TcPat
89 type TcRecordBinds = HsRecordBinds TcId TcPat
90 type TcHsModule = HsModule TcId TcPat
92 type TcCoreExpr = Expr TcId
93 type TcForeignExportDecl = ForeignDecl TcId
94 type TcRuleDecl = RuleDecl TcId TcPat
96 type TypecheckedPat = OutPat Id
97 type TypecheckedMonoBinds = MonoBinds Id TypecheckedPat
98 type TypecheckedDictBinds = TypecheckedMonoBinds
99 type TypecheckedHsBinds = HsBinds Id TypecheckedPat
100 type TypecheckedHsExpr = HsExpr Id TypecheckedPat
101 type TypecheckedArithSeqInfo = ArithSeqInfo Id TypecheckedPat
102 type TypecheckedStmt = Stmt Id TypecheckedPat
103 type TypecheckedMatch = Match Id TypecheckedPat
104 type TypecheckedGRHSs = GRHSs Id TypecheckedPat
105 type TypecheckedGRHS = GRHS Id TypecheckedPat
106 type TypecheckedRecordBinds = HsRecordBinds Id TypecheckedPat
107 type TypecheckedHsModule = HsModule Id TypecheckedPat
108 type TypecheckedForeignDecl = ForeignDecl Id
109 type TypecheckedRuleDecl = RuleDecl Id TypecheckedPat
113 mkHsTyApp expr [] = expr
114 mkHsTyApp expr tys = TyApp expr tys
116 mkHsDictApp expr [] = expr
117 mkHsDictApp expr dict_vars = DictApp expr dict_vars
119 mkHsTyLam [] expr = expr
120 mkHsTyLam tyvars expr = TyLam tyvars expr
122 mkHsDictLam [] expr = expr
123 mkHsDictLam dicts expr = DictLam dicts expr
125 mkHsLet EmptyMonoBinds expr = expr
126 mkHsLet mbinds expr = HsLet (MonoBind mbinds [] Recursive) expr
128 mkHsConApp data_con tys args = foldl HsApp (HsVar (dataConWrapId data_con) `mkHsTyApp` tys) args
130 idsToMonoBinds :: [Id] -> TcMonoBinds
132 = andMonoBindList [ CoreMonoBind id (unfoldingTemplate (idUnfolding id))
137 %************************************************************************
139 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
141 %************************************************************************
143 Some gruesome hackery for desugaring ccalls. It's here because if we put it
144 in Type.lhs we get irritating loops, and it's only used by TcInstDcls.lhs and
148 maybeBoxedPrimType :: Type -> Maybe (DataCon, Type)
149 maybeBoxedPrimType ty
150 = case splitProductType_maybe ty of -- Product data type
151 Just (tycon, tys_applied, data_con, [data_con_arg_ty]) -- constr has one arg
152 | isUnLiftedType data_con_arg_ty -- which is primitive
153 -> Just (data_con, data_con_arg_ty)
155 other_cases -> Nothing
158 %************************************************************************
160 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
162 %************************************************************************
164 This zonking pass runs over the bindings
166 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
167 b) convert unbound TcTyVar to Void
168 c) convert each TcId to an Id by zonking its type
170 The type variables are converted by binding mutable tyvars to immutable ones
171 and then zonking as normal.
173 The Ids are converted by binding them in the normal Tc envt; that
174 way we maintain sharing; eg an Id is zonked at its binding site and they
175 all occurrences of that Id point to the common zonked copy
177 It's all pretty boring stuff, because HsSyn is such a large type, and
178 the environment manipulation is tiresome.
181 -- zonkId is used *during* typechecking just to zonk the Id's type
182 zonkId :: TcId -> NF_TcM s TcId
184 = zonkTcType (idType id) `thenNF_Tc` \ ty' ->
185 returnNF_Tc (setIdType id ty')
187 -- zonkIdBndr is used *after* typechecking to get the Id's type
188 -- to its final form. The TyVarEnv give
189 zonkIdBndr :: TcId -> NF_TcM s Id
191 = zonkTcTypeToType (idType id) `thenNF_Tc` \ ty' ->
192 returnNF_Tc (setIdType id ty')
194 zonkIdOcc :: TcId -> NF_TcM s Id
196 | not (isLocallyDefined id) || omitIfaceSigForId id || isIP id
197 -- The omitIfaceSigForId thing may look wierd but it's quite
198 -- sensible really. We're avoiding looking up superclass selectors
199 -- and constructors; zonking them is a no-op anyway, and the
200 -- superclass selectors aren't in the environment anyway.
203 = tcLookupValueMaybe (idName id) `thenNF_Tc` \ maybe_id' ->
205 new_id = case maybe_id' of
207 Nothing -> pprTrace "zonkIdOcc: " (ppr id) id
214 zonkTopBinds :: TcMonoBinds -> NF_TcM s (TypecheckedMonoBinds, ValueEnv)
215 zonkTopBinds binds -- Top level is implicitly recursive
216 = fixNF_Tc (\ ~(_, new_ids) ->
217 tcExtendGlobalValEnv (bagToList new_ids) $
218 zonkMonoBinds binds `thenNF_Tc` \ (binds', new_ids) ->
219 tcGetValueEnv `thenNF_Tc` \ env ->
220 returnNF_Tc ((binds', env), new_ids)
221 ) `thenNF_Tc` \ (stuff, _) ->
224 zonkBinds :: TcHsBinds -> NF_TcM s (TypecheckedHsBinds, TcEnv)
227 = go binds (\ binds' -> tcGetEnv `thenNF_Tc` \ env ->
228 returnNF_Tc (binds', env))
231 -- -> (TypecheckedHsBinds
232 -- -> NF_TcM s (TypecheckedHsBinds, TcEnv)
234 -- -> NF_TcM s (TypecheckedHsBinds, TcEnv)
236 go (ThenBinds b1 b2) thing_inside = go b1 $ \ b1' ->
238 thing_inside (b1' `ThenBinds` b2')
240 go EmptyBinds thing_inside = thing_inside EmptyBinds
242 go (MonoBind bind sigs is_rec) thing_inside
243 = ASSERT( null sigs )
244 fixNF_Tc (\ ~(_, new_ids) ->
245 tcExtendGlobalValEnv (bagToList new_ids) $
246 zonkMonoBinds bind `thenNF_Tc` \ (new_bind, new_ids) ->
247 thing_inside (mkMonoBind new_bind [] is_rec) `thenNF_Tc` \ stuff ->
248 returnNF_Tc (stuff, new_ids)
249 ) `thenNF_Tc` \ (stuff, _) ->
254 -------------------------------------------------------------------------
255 zonkMonoBinds :: TcMonoBinds
256 -> NF_TcM s (TypecheckedMonoBinds, Bag Id)
258 zonkMonoBinds EmptyMonoBinds = returnNF_Tc (EmptyMonoBinds, emptyBag)
260 zonkMonoBinds (AndMonoBinds mbinds1 mbinds2)
261 = zonkMonoBinds mbinds1 `thenNF_Tc` \ (b1', ids1) ->
262 zonkMonoBinds mbinds2 `thenNF_Tc` \ (b2', ids2) ->
263 returnNF_Tc (b1' `AndMonoBinds` b2',
264 ids1 `unionBags` ids2)
266 zonkMonoBinds (PatMonoBind pat grhss locn)
267 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
268 zonkGRHSs grhss `thenNF_Tc` \ new_grhss ->
269 returnNF_Tc (PatMonoBind new_pat new_grhss locn, ids)
271 zonkMonoBinds (VarMonoBind var expr)
272 = zonkIdBndr var `thenNF_Tc` \ new_var ->
273 zonkExpr expr `thenNF_Tc` \ new_expr ->
274 returnNF_Tc (VarMonoBind new_var new_expr, unitBag new_var)
276 zonkMonoBinds (CoreMonoBind var core_expr)
277 = zonkIdBndr var `thenNF_Tc` \ new_var ->
278 returnNF_Tc (CoreMonoBind new_var core_expr, unitBag new_var)
280 zonkMonoBinds (FunMonoBind var inf ms locn)
281 = zonkIdBndr var `thenNF_Tc` \ new_var ->
282 mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms ->
283 returnNF_Tc (FunMonoBind new_var inf new_ms locn, unitBag new_var)
286 zonkMonoBinds (AbsBinds tyvars dicts exports inlines val_bind)
287 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
288 -- No need to extend tyvar env: the effects are
289 -- propagated through binding the tyvars themselves
291 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
292 tcExtendGlobalValEnv new_dicts $
294 fixNF_Tc (\ ~(_, _, val_bind_ids) ->
295 tcExtendGlobalValEnv (bagToList val_bind_ids) $
296 zonkMonoBinds val_bind `thenNF_Tc` \ (new_val_bind, val_bind_ids) ->
297 mapNF_Tc zonkExport exports `thenNF_Tc` \ new_exports ->
298 returnNF_Tc (new_val_bind, new_exports, val_bind_ids)
299 ) `thenNF_Tc ` \ (new_val_bind, new_exports, _) ->
301 new_globals = listToBag [global | (_, global, local) <- new_exports]
303 returnNF_Tc (AbsBinds new_tyvars new_dicts new_exports inlines new_val_bind,
306 zonkExport (tyvars, global, local)
307 = mapNF_Tc zonkTcTyVarBndr tyvars `thenNF_Tc` \ new_tyvars ->
308 zonkIdBndr global `thenNF_Tc` \ new_global ->
309 zonkIdOcc local `thenNF_Tc` \ new_local ->
310 returnNF_Tc (new_tyvars, new_global, new_local)
313 %************************************************************************
315 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
317 %************************************************************************
320 zonkMatch :: TcMatch -> NF_TcM s TypecheckedMatch
322 zonkMatch (Match _ pats _ grhss)
323 = zonkPats pats `thenNF_Tc` \ (new_pats, new_ids) ->
324 tcExtendGlobalValEnv (bagToList new_ids) $
325 zonkGRHSs grhss `thenNF_Tc` \ new_grhss ->
326 returnNF_Tc (Match [] new_pats Nothing new_grhss)
328 -------------------------------------------------------------------------
330 -> NF_TcM s TypecheckedGRHSs
332 zonkGRHSs (GRHSs grhss binds (Just ty))
333 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
336 zonk_grhs (GRHS guarded locn)
337 = zonkStmts guarded `thenNF_Tc` \ new_guarded ->
338 returnNF_Tc (GRHS new_guarded locn)
340 mapNF_Tc zonk_grhs grhss `thenNF_Tc` \ new_grhss ->
341 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
342 returnNF_Tc (GRHSs new_grhss new_binds (Just new_ty))
345 %************************************************************************
347 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
349 %************************************************************************
352 zonkExpr :: TcExpr -> NF_TcM s TypecheckedHsExpr
355 = zonkIdOcc id `thenNF_Tc` \ id' ->
356 returnNF_Tc (HsVar id')
358 zonkExpr (HsIPVar id)
359 = zonkIdOcc id `thenNF_Tc` \ id' ->
360 returnNF_Tc (HsIPVar id')
362 zonkExpr (HsLit _) = panic "zonkExpr:HsLit"
364 zonkExpr (HsLitOut lit ty)
365 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
366 returnNF_Tc (HsLitOut lit new_ty)
368 zonkExpr (HsLam match)
369 = zonkMatch match `thenNF_Tc` \ new_match ->
370 returnNF_Tc (HsLam new_match)
372 zonkExpr (HsApp e1 e2)
373 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
374 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
375 returnNF_Tc (HsApp new_e1 new_e2)
377 zonkExpr (OpApp e1 op fixity e2)
378 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
379 zonkExpr op `thenNF_Tc` \ new_op ->
380 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
381 returnNF_Tc (OpApp new_e1 new_op fixity new_e2)
383 zonkExpr (NegApp _ _) = panic "zonkExpr: NegApp"
384 zonkExpr (HsPar _) = panic "zonkExpr: HsPar"
386 zonkExpr (SectionL expr op)
387 = zonkExpr expr `thenNF_Tc` \ new_expr ->
388 zonkExpr op `thenNF_Tc` \ new_op ->
389 returnNF_Tc (SectionL new_expr new_op)
391 zonkExpr (SectionR op expr)
392 = zonkExpr op `thenNF_Tc` \ new_op ->
393 zonkExpr expr `thenNF_Tc` \ new_expr ->
394 returnNF_Tc (SectionR new_op new_expr)
396 zonkExpr (HsCase expr ms src_loc)
397 = zonkExpr expr `thenNF_Tc` \ new_expr ->
398 mapNF_Tc zonkMatch ms `thenNF_Tc` \ new_ms ->
399 returnNF_Tc (HsCase new_expr new_ms src_loc)
401 zonkExpr (HsIf e1 e2 e3 src_loc)
402 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
403 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
404 zonkExpr e3 `thenNF_Tc` \ new_e3 ->
405 returnNF_Tc (HsIf new_e1 new_e2 new_e3 src_loc)
407 zonkExpr (HsLet binds expr)
408 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
410 zonkExpr expr `thenNF_Tc` \ new_expr ->
411 returnNF_Tc (HsLet new_binds new_expr)
413 zonkExpr (HsWith expr binds)
414 = zonkExpr expr `thenNF_Tc` \ new_expr ->
415 zonkIPBinds binds `thenNF_Tc` \ new_binds ->
416 returnNF_Tc (HsWith new_expr new_binds)
418 zonkIPBinds = mapNF_Tc zonkIPBind
420 zonkExpr e `thenNF_Tc` \ e' ->
423 zonkExpr (HsDo _ _ _) = panic "zonkExpr:HsDo"
425 zonkExpr (HsDoOut do_or_lc stmts return_id then_id zero_id ty src_loc)
426 = zonkStmts stmts `thenNF_Tc` \ new_stmts ->
427 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
428 zonkIdOcc return_id `thenNF_Tc` \ new_return_id ->
429 zonkIdOcc then_id `thenNF_Tc` \ new_then_id ->
430 zonkIdOcc zero_id `thenNF_Tc` \ new_zero_id ->
431 returnNF_Tc (HsDoOut do_or_lc new_stmts new_return_id new_then_id new_zero_id
434 zonkExpr (ExplicitList _) = panic "zonkExpr:ExplicitList"
436 zonkExpr (ExplicitListOut ty exprs)
437 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
438 mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs ->
439 returnNF_Tc (ExplicitListOut new_ty new_exprs)
441 zonkExpr (ExplicitTuple exprs boxed)
442 = mapNF_Tc zonkExpr exprs `thenNF_Tc` \ new_exprs ->
443 returnNF_Tc (ExplicitTuple new_exprs boxed)
445 zonkExpr (RecordConOut data_con con_expr rbinds)
446 = zonkExpr con_expr `thenNF_Tc` \ new_con_expr ->
447 zonkRbinds rbinds `thenNF_Tc` \ new_rbinds ->
448 returnNF_Tc (RecordConOut data_con new_con_expr new_rbinds)
450 zonkExpr (RecordUpd _ _) = panic "zonkExpr:RecordUpd"
452 zonkExpr (RecordUpdOut expr ty dicts rbinds)
453 = zonkExpr expr `thenNF_Tc` \ new_expr ->
454 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
455 mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts ->
456 zonkRbinds rbinds `thenNF_Tc` \ new_rbinds ->
457 returnNF_Tc (RecordUpdOut new_expr new_ty new_dicts new_rbinds)
459 zonkExpr (ExprWithTySig _ _) = panic "zonkExpr:ExprWithTySig"
460 zonkExpr (ArithSeqIn _) = panic "zonkExpr:ArithSeqIn"
462 zonkExpr (ArithSeqOut expr info)
463 = zonkExpr expr `thenNF_Tc` \ new_expr ->
464 zonkArithSeq info `thenNF_Tc` \ new_info ->
465 returnNF_Tc (ArithSeqOut new_expr new_info)
467 zonkExpr (HsCCall fun args may_gc is_casm result_ty)
468 = mapNF_Tc zonkExpr args `thenNF_Tc` \ new_args ->
469 zonkTcTypeToType result_ty `thenNF_Tc` \ new_result_ty ->
470 returnNF_Tc (HsCCall fun new_args may_gc is_casm new_result_ty)
472 zonkExpr (HsSCC lbl expr)
473 = zonkExpr expr `thenNF_Tc` \ new_expr ->
474 returnNF_Tc (HsSCC lbl new_expr)
476 zonkExpr (TyLam tyvars expr)
477 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
478 -- No need to extend tyvar env; see AbsBinds
480 zonkExpr expr `thenNF_Tc` \ new_expr ->
481 returnNF_Tc (TyLam new_tyvars new_expr)
483 zonkExpr (TyApp expr tys)
484 = zonkExpr expr `thenNF_Tc` \ new_expr ->
485 mapNF_Tc zonkTcTypeToType tys `thenNF_Tc` \ new_tys ->
486 returnNF_Tc (TyApp new_expr new_tys)
488 zonkExpr (DictLam dicts expr)
489 = mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
490 tcExtendGlobalValEnv new_dicts $
491 zonkExpr expr `thenNF_Tc` \ new_expr ->
492 returnNF_Tc (DictLam new_dicts new_expr)
494 zonkExpr (DictApp expr dicts)
495 = zonkExpr expr `thenNF_Tc` \ new_expr ->
496 mapNF_Tc zonkIdOcc dicts `thenNF_Tc` \ new_dicts ->
497 returnNF_Tc (DictApp new_expr new_dicts)
501 -------------------------------------------------------------------------
502 zonkArithSeq :: TcArithSeqInfo -> NF_TcM s TypecheckedArithSeqInfo
504 zonkArithSeq (From e)
505 = zonkExpr e `thenNF_Tc` \ new_e ->
506 returnNF_Tc (From new_e)
508 zonkArithSeq (FromThen e1 e2)
509 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
510 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
511 returnNF_Tc (FromThen new_e1 new_e2)
513 zonkArithSeq (FromTo e1 e2)
514 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
515 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
516 returnNF_Tc (FromTo new_e1 new_e2)
518 zonkArithSeq (FromThenTo e1 e2 e3)
519 = zonkExpr e1 `thenNF_Tc` \ new_e1 ->
520 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
521 zonkExpr e3 `thenNF_Tc` \ new_e3 ->
522 returnNF_Tc (FromThenTo new_e1 new_e2 new_e3)
524 -------------------------------------------------------------------------
525 zonkStmts :: [TcStmt]
526 -> NF_TcM s [TypecheckedStmt]
528 zonkStmts [] = returnNF_Tc []
530 zonkStmts [ReturnStmt expr]
531 = zonkExpr expr `thenNF_Tc` \ new_expr ->
532 returnNF_Tc [ReturnStmt new_expr]
534 zonkStmts (ExprStmt expr locn : stmts)
535 = zonkExpr expr `thenNF_Tc` \ new_expr ->
536 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
537 returnNF_Tc (ExprStmt new_expr locn : new_stmts)
539 zonkStmts (GuardStmt expr locn : stmts)
540 = zonkExpr expr `thenNF_Tc` \ new_expr ->
541 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
542 returnNF_Tc (GuardStmt new_expr locn : new_stmts)
544 zonkStmts (LetStmt binds : stmts)
545 = zonkBinds binds `thenNF_Tc` \ (new_binds, new_env) ->
547 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
548 returnNF_Tc (LetStmt new_binds : new_stmts)
550 zonkStmts (BindStmt pat expr locn : stmts)
551 = zonkExpr expr `thenNF_Tc` \ new_expr ->
552 zonkPat pat `thenNF_Tc` \ (new_pat, new_ids) ->
553 tcExtendGlobalValEnv (bagToList new_ids) $
554 zonkStmts stmts `thenNF_Tc` \ new_stmts ->
555 returnNF_Tc (BindStmt new_pat new_expr locn : new_stmts)
559 -------------------------------------------------------------------------
560 zonkRbinds :: TcRecordBinds -> NF_TcM s TypecheckedRecordBinds
563 = mapNF_Tc zonk_rbind rbinds
565 zonk_rbind (field, expr, pun)
566 = zonkExpr expr `thenNF_Tc` \ new_expr ->
567 zonkIdOcc field `thenNF_Tc` \ new_field ->
568 returnNF_Tc (new_field, new_expr, pun)
571 %************************************************************************
573 \subsection[BackSubst-Pats]{Patterns}
575 %************************************************************************
578 zonkPat :: TcPat -> NF_TcM s (TypecheckedPat, Bag Id)
581 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
582 returnNF_Tc (WildPat new_ty, emptyBag)
585 = zonkIdBndr v `thenNF_Tc` \ new_v ->
586 returnNF_Tc (VarPat new_v, unitBag new_v)
588 zonkPat (LazyPat pat)
589 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
590 returnNF_Tc (LazyPat new_pat, ids)
592 zonkPat (AsPat n pat)
593 = zonkIdBndr n `thenNF_Tc` \ new_n ->
594 zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
595 returnNF_Tc (AsPat new_n new_pat, new_n `consBag` ids)
597 zonkPat (ListPat ty pats)
598 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
599 zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
600 returnNF_Tc (ListPat new_ty new_pats, ids)
602 zonkPat (TuplePat pats boxed)
603 = zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
604 returnNF_Tc (TuplePat new_pats boxed, ids)
606 zonkPat (ConPat n ty tvs dicts pats)
607 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
608 mapNF_Tc zonkTcTyVarToTyVar tvs `thenNF_Tc` \ new_tvs ->
609 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
610 tcExtendGlobalValEnv new_dicts $
611 zonkPats pats `thenNF_Tc` \ (new_pats, ids) ->
612 returnNF_Tc (ConPat n new_ty new_tvs new_dicts new_pats,
613 listToBag new_dicts `unionBags` ids)
615 zonkPat (RecPat n ty tvs dicts rpats)
616 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
617 mapNF_Tc zonkTcTyVarToTyVar tvs `thenNF_Tc` \ new_tvs ->
618 mapNF_Tc zonkIdBndr dicts `thenNF_Tc` \ new_dicts ->
619 tcExtendGlobalValEnv new_dicts $
620 mapAndUnzipNF_Tc zonk_rpat rpats `thenNF_Tc` \ (new_rpats, ids_s) ->
621 returnNF_Tc (RecPat n new_ty new_tvs new_dicts new_rpats,
622 listToBag new_dicts `unionBags` unionManyBags ids_s)
624 zonk_rpat (f, pat, pun)
625 = zonkPat pat `thenNF_Tc` \ (new_pat, ids) ->
626 returnNF_Tc ((f, new_pat, pun), ids)
628 zonkPat (LitPat lit ty)
629 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
630 returnNF_Tc (LitPat lit new_ty, emptyBag)
632 zonkPat (NPat lit ty expr)
633 = zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
634 zonkExpr expr `thenNF_Tc` \ new_expr ->
635 returnNF_Tc (NPat lit new_ty new_expr, emptyBag)
637 zonkPat (NPlusKPat n k ty e1 e2)
638 = zonkIdBndr n `thenNF_Tc` \ new_n ->
639 zonkTcTypeToType ty `thenNF_Tc` \ new_ty ->
640 zonkExpr e1 `thenNF_Tc` \ new_e1 ->
641 zonkExpr e2 `thenNF_Tc` \ new_e2 ->
642 returnNF_Tc (NPlusKPat new_n k new_ty new_e1 new_e2, unitBag new_n)
644 zonkPat (DictPat ds ms)
645 = mapNF_Tc zonkIdBndr ds `thenNF_Tc` \ new_ds ->
646 mapNF_Tc zonkIdBndr ms `thenNF_Tc` \ new_ms ->
647 returnNF_Tc (DictPat new_ds new_ms,
648 listToBag new_ds `unionBags` listToBag new_ms)
652 = returnNF_Tc ([], emptyBag)
655 = zonkPat pat `thenNF_Tc` \ (pat', ids1) ->
656 zonkPats pats `thenNF_Tc` \ (pats', ids2) ->
657 returnNF_Tc (pat':pats', ids1 `unionBags` ids2)
660 %************************************************************************
662 \subsection[BackSubst-Foreign]{Foreign exports}
664 %************************************************************************
668 zonkForeignExports :: [TcForeignExportDecl] -> NF_TcM s [TypecheckedForeignDecl]
669 zonkForeignExports ls = mapNF_Tc zonkForeignExport ls
671 zonkForeignExport :: TcForeignExportDecl -> NF_TcM s (TypecheckedForeignDecl)
672 zonkForeignExport (ForeignDecl i imp_exp hs_ty ext_nm cconv src_loc) =
673 zonkIdOcc i `thenNF_Tc` \ i' ->
674 returnNF_Tc (ForeignDecl i' imp_exp undefined ext_nm cconv src_loc)
678 zonkRules :: [TcRuleDecl] -> NF_TcM s [TypecheckedRuleDecl]
679 zonkRules rs = mapNF_Tc zonkRule rs
681 zonkRule (RuleDecl name tyvars vars lhs rhs loc)
682 = mapNF_Tc zonkTcTyVarToTyVar tyvars `thenNF_Tc` \ new_tyvars ->
683 mapNF_Tc zonkIdBndr [v | RuleBndr v <- vars] `thenNF_Tc` \ new_bndrs ->
684 tcExtendGlobalValEnv new_bndrs $
685 zonkExpr lhs `thenNF_Tc` \ new_lhs ->
686 zonkExpr rhs `thenNF_Tc` \ new_rhs ->
687 returnNF_Tc (RuleDecl name new_tyvars (map RuleBndr new_bndrs) new_lhs new_rhs loc)
688 -- I hate this map RuleBndr stuff
690 zonkRule (IfaceRuleDecl fun rule loc)
691 = returnNF_Tc (IfaceRuleDecl fun rule loc)