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, TcDictBinds,
17 TypecheckedHsBinds, TypecheckedRuleDecl,
18 TypecheckedMonoBinds, TypecheckedPat,
19 TypecheckedHsExpr, TypecheckedArithSeqInfo,
20 TypecheckedStmt, TypecheckedForeignDecl,
21 TypecheckedMatch, TypecheckedHsModule,
22 TypecheckedGRHSs, TypecheckedGRHS,
23 TypecheckedRecordBinds, TypecheckedDictBinds,
24 TypecheckedMatchContext, TypecheckedCoreBind,
26 mkHsTyApp, mkHsDictApp, mkHsConApp,
27 mkHsTyLam, mkHsDictLam, mkHsLet,
30 -- re-exported from TcMonad
33 zonkTopBinds, zonkTopDecls, zonkTopExpr,
37 #include "HsVersions.h"
40 import HsSyn -- oodles of it
43 import Id ( idType, setIdType, Id )
44 import DataCon ( dataConWrapId )
48 import TcType ( TcType, tcGetTyVar )
49 import TcMType ( zonkTcTypeToType, zonkTcTyVarToTyVar, zonkTcType, zonkTcTyVars )
50 import TysPrim ( charPrimTy, intPrimTy, floatPrimTy,
51 doublePrimTy, addrPrimTy
53 import TysWiredIn ( charTy, stringTy, intTy, integerTy,
54 mkListTy, mkPArrTy, mkTupleTy, unitTy )
55 import CoreSyn ( CoreExpr )
56 import Var ( isId, isLocalVar )
58 import BasicTypes ( RecFlag(..), Boxity(..), IPName(..), ipNameName, mapIPName )
59 import Maybes ( orElse )
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 TcHsBinds = HsBinds TcId
77 type TcMonoBinds = MonoBinds TcId
78 type TcDictBinds = TcMonoBinds
79 type TcPat = OutPat TcId
80 type TcExpr = HsExpr TcId
81 type TcGRHSs = GRHSs TcId
82 type TcGRHS = GRHS TcId
83 type TcMatch = Match TcId
84 type TcStmt = Stmt TcId
85 type TcArithSeqInfo = ArithSeqInfo TcId
86 type TcRecordBinds = HsRecordBinds TcId
87 type TcHsModule = HsModule TcId
88 type TcForeignDecl = ForeignDecl TcId
89 type TcRuleDecl = RuleDecl TcId
91 type TypecheckedPat = OutPat Id
92 type TypecheckedMonoBinds = MonoBinds Id
93 type TypecheckedDictBinds = TypecheckedMonoBinds
94 type TypecheckedHsBinds = HsBinds Id
95 type TypecheckedHsExpr = HsExpr Id
96 type TypecheckedArithSeqInfo = ArithSeqInfo Id
97 type TypecheckedStmt = Stmt Id
98 type TypecheckedMatch = Match Id
99 type TypecheckedMatchContext = HsMatchContext Id
100 type TypecheckedGRHSs = GRHSs Id
101 type TypecheckedGRHS = GRHS Id
102 type TypecheckedRecordBinds = HsRecordBinds Id
103 type TypecheckedHsModule = HsModule Id
104 type TypecheckedForeignDecl = ForeignDecl Id
105 type TypecheckedRuleDecl = RuleDecl Id
106 type TypecheckedCoreBind = (Id, CoreExpr)
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 mkHsLet EmptyMonoBinds expr = expr
123 mkHsLet mbinds expr = HsLet (MonoBind mbinds [] Recursive) expr
125 mkHsConApp data_con tys args = foldl HsApp (HsVar (dataConWrapId data_con) `mkHsTyApp` tys) args
129 %************************************************************************
131 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
133 %************************************************************************
135 Note: If @hsPatType@ doesn't bear a strong resemblance to @exprType@,
136 then something is wrong.
138 hsPatType :: TypecheckedPat -> Type
140 hsPatType (ParPat pat) = hsPatType pat
141 hsPatType (WildPat ty) = ty
142 hsPatType (VarPat var) = idType var
143 hsPatType (LazyPat pat) = hsPatType pat
144 hsPatType (LitPat lit) = hsLitType lit
145 hsPatType (AsPat var pat) = idType var
146 hsPatType (ListPat _ ty) = mkListTy ty
147 hsPatType (PArrPat _ ty) = mkPArrTy ty
148 hsPatType (TuplePat pats box) = mkTupleTy box (length pats) (map hsPatType pats)
149 hsPatType (ConPatOut _ _ ty _ _) = ty
150 hsPatType (SigPatOut _ ty _) = ty
151 hsPatType (NPatOut lit ty _) = ty
152 hsPatType (NPlusKPatOut id _ _ _) = idType id
153 hsPatType (DictPat ds ms) = case (ds ++ ms) of
156 ds -> mkTupleTy Boxed (length ds) (map idType ds)
159 hsLitType :: HsLit -> TcType
160 hsLitType (HsChar c) = charTy
161 hsLitType (HsCharPrim c) = charPrimTy
162 hsLitType (HsString str) = stringTy
163 hsLitType (HsStringPrim s) = addrPrimTy
164 hsLitType (HsInt i) = intTy
165 hsLitType (HsIntPrim i) = intPrimTy
166 hsLitType (HsInteger i) = integerTy
167 hsLitType (HsRat _ ty) = ty
168 hsLitType (HsFloatPrim f) = floatPrimTy
169 hsLitType (HsDoublePrim d) = doublePrimTy
170 hsLitType (HsLitLit _ ty) = ty
174 -- zonkId is used *during* typechecking just to zonk the Id's type
175 zonkId :: TcId -> TcM TcId
177 = zonkTcType (idType id) `thenM` \ ty' ->
178 returnM (setIdType id ty')
182 %************************************************************************
184 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
186 %************************************************************************
188 This zonking pass runs over the bindings
190 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
191 b) convert unbound TcTyVar to Void
192 c) convert each TcId to an Id by zonking its type
194 The type variables are converted by binding mutable tyvars to immutable ones
195 and then zonking as normal.
197 The Ids are converted by binding them in the normal Tc envt; that
198 way we maintain sharing; eg an Id is zonked at its binding site and they
199 all occurrences of that Id point to the common zonked copy
201 It's all pretty boring stuff, because HsSyn is such a large type, and
202 the environment manipulation is tiresome.
205 type ZonkEnv = IdEnv Id
206 -- Maps an Id to its zonked version; both have the same Name
207 -- Is only consulted lazily; hence knot-tying
209 emptyZonkEnv = emptyVarEnv
211 extendZonkEnv :: ZonkEnv -> [Id] -> ZonkEnv
212 extendZonkEnv env ids = extendVarEnvList env [(id,id) | id <- ids]
214 mkZonkEnv :: [Id] -> ZonkEnv
215 mkZonkEnv ids = extendZonkEnv emptyZonkEnv ids
217 zonkIdOcc :: ZonkEnv -> TcId -> Id
218 -- Ids defined in this module should be in the envt;
219 -- ignore others. (Actually, data constructors are also
220 -- not LocalVars, even when locally defined, but that is fine.)
222 -- Actually, Template Haskell works in 'chunks' of declarations, and
223 -- an earlier chunk won't be in the 'env' that the zonking phase
224 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
225 -- zonked. There's no point in looking it up there (except for error
226 -- checking), and it's not conveniently to hand; hence the simple
227 -- 'orElse' case in the LocalVar branch.
229 -- Even without template splices, in module Main, the checking of
230 -- 'main' is done as a separte chunk.
232 | isLocalVar id = lookupVarEnv env id `orElse` id
235 zonkIdOccs env ids = map (zonkIdOcc env) ids
237 -- zonkIdBndr is used *after* typechecking to get the Id's type
238 -- to its final form. The TyVarEnv give
239 zonkIdBndr :: TcId -> TcM Id
241 = zonkTcTypeToType (idType id) `thenM` \ ty' ->
242 returnM (setIdType id ty')
247 zonkTopExpr :: TcExpr -> TcM TypecheckedHsExpr
248 zonkTopExpr e = zonkExpr emptyZonkEnv e
250 zonkTopDecls :: TcMonoBinds -> [TcRuleDecl] -> [TcForeignDecl]
252 TypecheckedMonoBinds,
253 [TypecheckedForeignDecl],
254 [TypecheckedRuleDecl])
255 zonkTopDecls binds rules fords -- Top level is implicitly recursive
256 = fixM (\ ~(new_ids, _, _, _) ->
258 zonk_env = mkZonkEnv new_ids
260 zonkMonoBinds zonk_env binds `thenM` \ (binds', new_ids) ->
261 zonkRules zonk_env rules `thenM` \ rules' ->
262 zonkForeignExports zonk_env fords `thenM` \ fords' ->
264 returnM (bagToList new_ids, binds', fords', rules')
267 zonkTopBinds :: TcMonoBinds -> TcM ([Id], TypecheckedMonoBinds)
269 = fixM (\ ~(new_ids, _) ->
271 zonk_env = mkZonkEnv new_ids
273 zonkMonoBinds zonk_env binds `thenM` \ (binds', new_ids) ->
274 returnM (bagToList new_ids, binds')
277 ---------------------------------------------
278 zonkBinds :: ZonkEnv -> TcHsBinds -> TcM (ZonkEnv, TypecheckedHsBinds)
279 zonkBinds env EmptyBinds = returnM (env, EmptyBinds)
281 zonkBinds env (ThenBinds b1 b2)
282 = zonkBinds env b1 `thenM` \ (env1, b1') ->
283 zonkBinds env1 b2 `thenM` \ (env2, b2') ->
284 returnM (env2, b1' `ThenBinds` b2')
286 zonkBinds env (MonoBind bind sigs is_rec)
287 = ASSERT( null sigs )
288 fixM (\ ~(env1, _) ->
289 zonkMonoBinds env1 bind `thenM` \ (new_bind, new_ids) ->
291 env2 = extendZonkEnv env (bagToList new_ids)
293 returnM (env2, mkMonoBind new_bind [] is_rec)
296 ---------------------------------------------
297 zonkMonoBinds :: ZonkEnv -> TcMonoBinds
298 -> TcM (TypecheckedMonoBinds, Bag Id)
300 zonkMonoBinds env EmptyMonoBinds = returnM (EmptyMonoBinds, emptyBag)
302 zonkMonoBinds env (AndMonoBinds mbinds1 mbinds2)
303 = zonkMonoBinds env mbinds1 `thenM` \ (b1', ids1) ->
304 zonkMonoBinds env mbinds2 `thenM` \ (b2', ids2) ->
305 returnM (b1' `AndMonoBinds` b2',
306 ids1 `unionBags` ids2)
308 zonkMonoBinds env (PatMonoBind pat grhss locn)
309 = zonkPat env pat `thenM` \ (new_pat, ids) ->
310 zonkGRHSs env grhss `thenM` \ new_grhss ->
311 returnM (PatMonoBind new_pat new_grhss locn, ids)
313 zonkMonoBinds env (VarMonoBind var expr)
314 = zonkIdBndr var `thenM` \ new_var ->
315 zonkExpr env expr `thenM` \ new_expr ->
316 returnM (VarMonoBind new_var new_expr, unitBag new_var)
318 zonkMonoBinds env (CoreMonoBind var core_expr)
319 = zonkIdBndr var `thenM` \ new_var ->
320 returnM (CoreMonoBind new_var core_expr, unitBag new_var)
322 zonkMonoBinds env (FunMonoBind var inf ms locn)
323 = zonkIdBndr var `thenM` \ new_var ->
324 mappM (zonkMatch env) ms `thenM` \ new_ms ->
325 returnM (FunMonoBind new_var inf new_ms locn, unitBag new_var)
328 zonkMonoBinds env (AbsBinds tyvars dicts exports inlines val_bind)
329 = mappM zonkTcTyVarToTyVar tyvars `thenM` \ new_tyvars ->
330 -- No need to extend tyvar env: the effects are
331 -- propagated through binding the tyvars themselves
333 mappM zonkIdBndr dicts `thenM` \ new_dicts ->
334 fixM (\ ~(_, _, val_bind_ids) ->
336 env1 = extendZonkEnv (extendZonkEnv env new_dicts)
337 (bagToList val_bind_ids)
339 zonkMonoBinds env1 val_bind `thenM` \ (new_val_bind, val_bind_ids) ->
340 mappM (zonkExport env1) exports `thenM` \ new_exports ->
341 returnM (new_val_bind, new_exports, val_bind_ids)
342 ) `thenM ` \ (new_val_bind, new_exports, _) ->
344 new_globals = listToBag [global | (_, global, local) <- new_exports]
346 returnM (AbsBinds new_tyvars new_dicts new_exports inlines new_val_bind,
349 zonkExport env (tyvars, global, local)
350 = zonkTcTyVars tyvars `thenM` \ tys ->
352 new_tyvars = map (tcGetTyVar "zonkExport") tys
353 -- This isn't the binding occurrence of these tyvars
354 -- but they should *be* tyvars. Hence tcGetTyVar.
356 zonkIdBndr global `thenM` \ new_global ->
357 returnM (new_tyvars, new_global, zonkIdOcc env local)
360 %************************************************************************
362 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
364 %************************************************************************
367 zonkMatch :: ZonkEnv -> TcMatch -> TcM TypecheckedMatch
369 zonkMatch env (Match pats _ grhss)
370 = zonkPats env pats `thenM` \ (new_pats, new_ids) ->
371 zonkGRHSs (extendZonkEnv env (bagToList new_ids)) grhss `thenM` \ new_grhss ->
372 returnM (Match new_pats Nothing new_grhss)
374 -------------------------------------------------------------------------
375 zonkGRHSs :: ZonkEnv -> TcGRHSs -> TcM TypecheckedGRHSs
377 zonkGRHSs env (GRHSs grhss binds ty)
378 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
380 zonk_grhs (GRHS guarded locn)
381 = zonkStmts new_env guarded `thenM` \ new_guarded ->
382 returnM (GRHS new_guarded locn)
384 mappM zonk_grhs grhss `thenM` \ new_grhss ->
385 zonkTcTypeToType ty `thenM` \ new_ty ->
386 returnM (GRHSs new_grhss new_binds new_ty)
389 %************************************************************************
391 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
393 %************************************************************************
396 zonkExpr :: ZonkEnv -> TcExpr -> TcM TypecheckedHsExpr
398 zonkExpr env (HsVar id)
399 = returnM (HsVar (zonkIdOcc env id))
401 zonkExpr env (HsIPVar id)
402 = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
404 zonkExpr env (HsLit (HsRat f ty))
405 = zonkTcTypeToType ty `thenM` \ new_ty ->
406 returnM (HsLit (HsRat f new_ty))
408 zonkExpr env (HsLit (HsLitLit lit ty))
409 = zonkTcTypeToType ty `thenM` \ new_ty ->
410 returnM (HsLit (HsLitLit lit new_ty))
412 zonkExpr env (HsLit lit)
413 = returnM (HsLit lit)
415 -- HsOverLit doesn't appear in typechecker output
417 zonkExpr env (HsLam match)
418 = zonkMatch env match `thenM` \ new_match ->
419 returnM (HsLam new_match)
421 zonkExpr env (HsApp e1 e2)
422 = zonkExpr env e1 `thenM` \ new_e1 ->
423 zonkExpr env e2 `thenM` \ new_e2 ->
424 returnM (HsApp new_e1 new_e2)
426 zonkExpr env (HsBracketOut body bs)
427 = mappM zonk_b bs `thenM` \ bs' ->
428 returnM (HsBracketOut body bs')
430 zonk_b (n,e) = zonkExpr env e `thenM` \ e' ->
433 zonkExpr env (HsSplice n e) = WARN( True, ppr e ) -- Should not happen
434 returnM (HsSplice n e)
436 zonkExpr env (OpApp e1 op fixity e2)
437 = zonkExpr env e1 `thenM` \ new_e1 ->
438 zonkExpr env op `thenM` \ new_op ->
439 zonkExpr env e2 `thenM` \ new_e2 ->
440 returnM (OpApp new_e1 new_op fixity new_e2)
442 zonkExpr env (NegApp _ _) = panic "zonkExpr env: NegApp"
444 zonkExpr env (HsPar e)
445 = zonkExpr env e `thenM` \new_e ->
446 returnM (HsPar new_e)
448 zonkExpr env (SectionL expr op)
449 = zonkExpr env expr `thenM` \ new_expr ->
450 zonkExpr env op `thenM` \ new_op ->
451 returnM (SectionL new_expr new_op)
453 zonkExpr env (SectionR op expr)
454 = zonkExpr env op `thenM` \ new_op ->
455 zonkExpr env expr `thenM` \ new_expr ->
456 returnM (SectionR new_op new_expr)
458 zonkExpr env (HsCase expr ms src_loc)
459 = zonkExpr env expr `thenM` \ new_expr ->
460 mappM (zonkMatch env) ms `thenM` \ new_ms ->
461 returnM (HsCase new_expr new_ms src_loc)
463 zonkExpr env (HsIf e1 e2 e3 src_loc)
464 = zonkExpr env e1 `thenM` \ new_e1 ->
465 zonkExpr env e2 `thenM` \ new_e2 ->
466 zonkExpr env e3 `thenM` \ new_e3 ->
467 returnM (HsIf new_e1 new_e2 new_e3 src_loc)
469 zonkExpr env (HsLet binds expr)
470 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
471 zonkExpr new_env expr `thenM` \ new_expr ->
472 returnM (HsLet new_binds new_expr)
474 zonkExpr env (HsWith expr binds is_with)
475 = mappM zonk_ip_bind binds `thenM` \ new_binds ->
477 env1 = extendZonkEnv env (map (ipNameName . fst) new_binds)
479 zonkExpr env1 expr `thenM` \ new_expr ->
480 returnM (HsWith new_expr new_binds is_with)
483 = mapIPNameTc zonkIdBndr n `thenM` \ n' ->
484 zonkExpr env e `thenM` \ e' ->
487 zonkExpr env (HsDo do_or_lc stmts ids ty src_loc)
488 = zonkStmts env stmts `thenM` \ new_stmts ->
489 zonkTcTypeToType ty `thenM` \ new_ty ->
490 returnM (HsDo do_or_lc new_stmts
494 zonkExpr env (ExplicitList ty exprs)
495 = zonkTcTypeToType ty `thenM` \ new_ty ->
496 mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
497 returnM (ExplicitList new_ty new_exprs)
499 zonkExpr env (ExplicitPArr ty exprs)
500 = zonkTcTypeToType ty `thenM` \ new_ty ->
501 mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
502 returnM (ExplicitPArr new_ty new_exprs)
504 zonkExpr env (ExplicitTuple exprs boxed)
505 = mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
506 returnM (ExplicitTuple new_exprs boxed)
508 zonkExpr env (RecordConOut data_con con_expr rbinds)
509 = zonkExpr env con_expr `thenM` \ new_con_expr ->
510 zonkRbinds env rbinds `thenM` \ new_rbinds ->
511 returnM (RecordConOut data_con new_con_expr new_rbinds)
513 zonkExpr env (RecordUpd _ _) = panic "zonkExpr env:RecordUpd"
515 zonkExpr env (RecordUpdOut expr in_ty out_ty rbinds)
516 = zonkExpr env expr `thenM` \ new_expr ->
517 zonkTcTypeToType in_ty `thenM` \ new_in_ty ->
518 zonkTcTypeToType out_ty `thenM` \ new_out_ty ->
519 zonkRbinds env rbinds `thenM` \ new_rbinds ->
520 returnM (RecordUpdOut new_expr new_in_ty new_out_ty new_rbinds)
522 zonkExpr env (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
523 zonkExpr env (ArithSeqIn _) = panic "zonkExpr env:ArithSeqIn"
524 zonkExpr env (PArrSeqIn _) = panic "zonkExpr env:PArrSeqIn"
526 zonkExpr env (ArithSeqOut expr info)
527 = zonkExpr env expr `thenM` \ new_expr ->
528 zonkArithSeq env info `thenM` \ new_info ->
529 returnM (ArithSeqOut new_expr new_info)
531 zonkExpr env (PArrSeqOut expr info)
532 = zonkExpr env expr `thenM` \ new_expr ->
533 zonkArithSeq env info `thenM` \ new_info ->
534 returnM (PArrSeqOut new_expr new_info)
536 zonkExpr env (HsCCall fun args may_gc is_casm result_ty)
537 = mappM (zonkExpr env) args `thenM` \ new_args ->
538 zonkTcTypeToType result_ty `thenM` \ new_result_ty ->
539 returnM (HsCCall fun new_args may_gc is_casm new_result_ty)
541 zonkExpr env (HsSCC lbl expr)
542 = zonkExpr env expr `thenM` \ new_expr ->
543 returnM (HsSCC lbl new_expr)
545 zonkExpr env (TyLam tyvars expr)
546 = mappM zonkTcTyVarToTyVar tyvars `thenM` \ new_tyvars ->
547 -- No need to extend tyvar env; see AbsBinds
549 zonkExpr env expr `thenM` \ new_expr ->
550 returnM (TyLam new_tyvars new_expr)
552 zonkExpr env (TyApp expr tys)
553 = zonkExpr env expr `thenM` \ new_expr ->
554 mappM zonkTcTypeToType tys `thenM` \ new_tys ->
555 returnM (TyApp new_expr new_tys)
557 zonkExpr env (DictLam dicts expr)
558 = mappM zonkIdBndr dicts `thenM` \ new_dicts ->
560 env1 = extendZonkEnv env new_dicts
562 zonkExpr env1 expr `thenM` \ new_expr ->
563 returnM (DictLam new_dicts new_expr)
565 zonkExpr env (DictApp expr dicts)
566 = zonkExpr env expr `thenM` \ new_expr ->
567 returnM (DictApp new_expr (zonkIdOccs env dicts))
571 -------------------------------------------------------------------------
572 zonkArithSeq :: ZonkEnv -> TcArithSeqInfo -> TcM TypecheckedArithSeqInfo
574 zonkArithSeq env (From e)
575 = zonkExpr env e `thenM` \ new_e ->
578 zonkArithSeq env (FromThen e1 e2)
579 = zonkExpr env e1 `thenM` \ new_e1 ->
580 zonkExpr env e2 `thenM` \ new_e2 ->
581 returnM (FromThen new_e1 new_e2)
583 zonkArithSeq env (FromTo e1 e2)
584 = zonkExpr env e1 `thenM` \ new_e1 ->
585 zonkExpr env e2 `thenM` \ new_e2 ->
586 returnM (FromTo new_e1 new_e2)
588 zonkArithSeq env (FromThenTo e1 e2 e3)
589 = zonkExpr env e1 `thenM` \ new_e1 ->
590 zonkExpr env e2 `thenM` \ new_e2 ->
591 zonkExpr env e3 `thenM` \ new_e3 ->
592 returnM (FromThenTo new_e1 new_e2 new_e3)
594 -------------------------------------------------------------------------
595 zonkStmts :: ZonkEnv -> [TcStmt] -> TcM [TypecheckedStmt]
597 zonkStmts env [] = returnM []
599 zonkStmts env (ParStmtOut bndrstmtss : stmts)
600 = mappM (mappM zonkId) bndrss `thenM` \ new_bndrss ->
601 mappM (zonkStmts env) stmtss `thenM` \ new_stmtss ->
603 new_binders = concat new_bndrss
604 env1 = extendZonkEnv env new_binders
606 zonkStmts env1 stmts `thenM` \ new_stmts ->
607 returnM (ParStmtOut (zip new_bndrss new_stmtss) : new_stmts)
609 (bndrss, stmtss) = unzip bndrstmtss
611 zonkStmts env (ResultStmt expr locn : stmts)
612 = zonkExpr env expr `thenM` \ new_expr ->
613 zonkStmts env stmts `thenM` \ new_stmts ->
614 returnM (ResultStmt new_expr locn : new_stmts)
616 zonkStmts env (ExprStmt expr ty locn : stmts)
617 = zonkExpr env expr `thenM` \ new_expr ->
618 zonkTcTypeToType ty `thenM` \ new_ty ->
619 zonkStmts env stmts `thenM` \ new_stmts ->
620 returnM (ExprStmt new_expr new_ty locn : new_stmts)
622 zonkStmts env (LetStmt binds : stmts)
623 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
624 zonkStmts new_env stmts `thenM` \ new_stmts ->
625 returnM (LetStmt new_binds : new_stmts)
627 zonkStmts env (BindStmt pat expr locn : stmts)
628 = zonkExpr env expr `thenM` \ new_expr ->
629 zonkPat env pat `thenM` \ (new_pat, new_ids) ->
631 env1 = extendZonkEnv env (bagToList new_ids)
633 zonkStmts env1 stmts `thenM` \ new_stmts ->
634 returnM (BindStmt new_pat new_expr locn : new_stmts)
638 -------------------------------------------------------------------------
639 zonkRbinds :: ZonkEnv -> TcRecordBinds -> TcM TypecheckedRecordBinds
641 zonkRbinds env rbinds
642 = mappM zonk_rbind rbinds
644 zonk_rbind (field, expr)
645 = zonkExpr env expr `thenM` \ new_expr ->
646 returnM (zonkIdOcc env field, new_expr)
648 -------------------------------------------------------------------------
649 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
650 mapIPNameTc f (Dupable n) = f n `thenM` \ r -> returnM (Dupable r)
651 mapIPNameTc f (Linear n) = f n `thenM` \ r -> returnM (Linear r)
655 %************************************************************************
657 \subsection[BackSubst-Pats]{Patterns}
659 %************************************************************************
662 zonkPat :: ZonkEnv -> TcPat -> TcM (TypecheckedPat, Bag Id)
664 zonkPat env (ParPat p)
665 = zonkPat env p `thenM` \ (new_p, ids) ->
666 returnM (ParPat new_p, ids)
668 zonkPat env (WildPat ty)
669 = zonkTcTypeToType ty `thenM` \ new_ty ->
670 returnM (WildPat new_ty, emptyBag)
672 zonkPat env (VarPat v)
673 = zonkIdBndr v `thenM` \ new_v ->
674 returnM (VarPat new_v, unitBag new_v)
676 zonkPat env (LazyPat pat)
677 = zonkPat env pat `thenM` \ (new_pat, ids) ->
678 returnM (LazyPat new_pat, ids)
680 zonkPat env (AsPat n pat)
681 = zonkIdBndr n `thenM` \ new_n ->
682 zonkPat env pat `thenM` \ (new_pat, ids) ->
683 returnM (AsPat new_n new_pat, new_n `consBag` ids)
685 zonkPat env (ListPat pats ty)
686 = zonkTcTypeToType ty `thenM` \ new_ty ->
687 zonkPats env pats `thenM` \ (new_pats, ids) ->
688 returnM (ListPat new_pats new_ty, ids)
690 zonkPat env (PArrPat pats ty)
691 = zonkTcTypeToType ty `thenM` \ new_ty ->
692 zonkPats env pats `thenM` \ (new_pats, ids) ->
693 returnM (PArrPat new_pats new_ty, ids)
695 zonkPat env (TuplePat pats boxed)
696 = zonkPats env pats `thenM` \ (new_pats, ids) ->
697 returnM (TuplePat new_pats boxed, ids)
699 zonkPat env (ConPatOut n stuff ty tvs dicts)
700 = zonkTcTypeToType ty `thenM` \ new_ty ->
701 mappM zonkTcTyVarToTyVar tvs `thenM` \ new_tvs ->
702 mappM zonkIdBndr dicts `thenM` \ new_dicts ->
704 env1 = extendZonkEnv env new_dicts
706 zonkConStuff env stuff `thenM` \ (new_stuff, ids) ->
707 returnM (ConPatOut n new_stuff new_ty new_tvs new_dicts,
708 listToBag new_dicts `unionBags` ids)
710 zonkPat env (LitPat lit) = returnM (LitPat lit, emptyBag)
712 zonkPat env (SigPatOut pat ty expr)
713 = zonkPat env pat `thenM` \ (new_pat, ids) ->
714 zonkTcTypeToType ty `thenM` \ new_ty ->
715 zonkExpr env expr `thenM` \ new_expr ->
716 returnM (SigPatOut new_pat new_ty new_expr, ids)
718 zonkPat env (NPatOut lit ty expr)
719 = zonkTcTypeToType ty `thenM` \ new_ty ->
720 zonkExpr env expr `thenM` \ new_expr ->
721 returnM (NPatOut lit new_ty new_expr, emptyBag)
723 zonkPat env (NPlusKPatOut n k e1 e2)
724 = zonkIdBndr n `thenM` \ new_n ->
725 zonkExpr env e1 `thenM` \ new_e1 ->
726 zonkExpr env e2 `thenM` \ new_e2 ->
727 returnM (NPlusKPatOut new_n k new_e1 new_e2, unitBag new_n)
729 zonkPat env (DictPat ds ms)
730 = mappM zonkIdBndr ds `thenM` \ new_ds ->
731 mappM zonkIdBndr ms `thenM` \ new_ms ->
732 returnM (DictPat new_ds new_ms,
733 listToBag new_ds `unionBags` listToBag new_ms)
735 ---------------------------
736 zonkConStuff env (PrefixCon pats)
737 = zonkPats env pats `thenM` \ (new_pats, ids) ->
738 returnM (PrefixCon new_pats, ids)
740 zonkConStuff env (InfixCon p1 p2)
741 = zonkPat env p1 `thenM` \ (new_p1, ids1) ->
742 zonkPat env p2 `thenM` \ (new_p2, ids2) ->
743 returnM (InfixCon new_p1 new_p2, ids1 `unionBags` ids2)
745 zonkConStuff env (RecCon rpats)
746 = mapAndUnzipM zonk_rpat rpats `thenM` \ (new_rpats, ids_s) ->
747 returnM (RecCon new_rpats, unionManyBags ids_s)
750 = zonkPat env pat `thenM` \ (new_pat, ids) ->
751 returnM ((f, new_pat), ids)
753 ---------------------------
755 = returnM ([], emptyBag)
757 zonkPats env (pat:pats)
758 = zonkPat env pat `thenM` \ (pat', ids1) ->
759 zonkPats env pats `thenM` \ (pats', ids2) ->
760 returnM (pat':pats', ids1 `unionBags` ids2)
763 %************************************************************************
765 \subsection[BackSubst-Foreign]{Foreign exports}
767 %************************************************************************
771 zonkForeignExports :: ZonkEnv -> [TcForeignDecl] -> TcM [TypecheckedForeignDecl]
772 zonkForeignExports env ls = mappM (zonkForeignExport env) ls
774 zonkForeignExport :: ZonkEnv -> TcForeignDecl -> TcM (TypecheckedForeignDecl)
775 zonkForeignExport env (ForeignExport i hs_ty spec isDeprec src_loc) =
776 returnM (ForeignExport (zonkIdOcc env i) undefined spec isDeprec src_loc)
780 zonkRules :: ZonkEnv -> [TcRuleDecl] -> TcM [TypecheckedRuleDecl]
781 zonkRules env rs = mappM (zonkRule env) rs
783 zonkRule env (HsRule name act vars lhs rhs loc)
784 = mappM zonk_bndr vars `thenM` \ new_bndrs ->
786 env1 = extendZonkEnv env (filter isId new_bndrs)
787 -- Type variables don't need an envt
788 -- They are bound through the mutable mechanism
790 zonkExpr env1 lhs `thenM` \ new_lhs ->
791 zonkExpr env1 rhs `thenM` \ new_rhs ->
792 returnM (HsRule name act (map RuleBndr new_bndrs) new_lhs new_rhs loc)
793 -- I hate this map RuleBndr stuff
795 zonk_bndr (RuleBndr v)
796 | isId v = zonkIdBndr v
797 | otherwise = zonkTcTyVarToTyVar v
799 zonkRule env (IfaceRuleOut fun rule)
800 = returnM (IfaceRuleOut (zonkIdOcc env fun) rule)