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, TcTyVar, eqKind, isTypeKind, mkTyVarTy,
49 tcGetTyVar, isAnyTypeKind, mkTyConApp )
51 import TcMType ( zonkTcTyVarToTyVar, zonkType, zonkTcType, zonkTcTyVars,
53 import TysPrim ( charPrimTy, intPrimTy, floatPrimTy,
54 doublePrimTy, addrPrimTy
56 import TysWiredIn ( charTy, stringTy, intTy, integerTy,
57 mkListTy, mkPArrTy, mkTupleTy, unitTy,
58 voidTy, listTyCon, tupleTyCon )
59 import TyCon ( mkPrimTyCon, tyConKind )
60 import PrimRep ( PrimRep(VoidRep) )
61 import CoreSyn ( CoreExpr )
62 import Name ( getOccName, mkInternalName, mkDerivedTyConOcc )
63 import Var ( isId, isLocalVar, tyVarKind )
66 import BasicTypes ( RecFlag(..), Boxity(..), IPName(..), ipNameName, mapIPName )
67 import Maybes ( orElse )
68 import Unique ( Uniquable(..) )
69 import SrcLoc ( noSrcLoc )
78 The @Tc...@ datatypes are the ones that apply {\em during} type checking.
79 All the types in @Tc...@ things have mutable type-variables in them for
82 At the end of type checking we zonk everything to @Typechecked...@ datatypes,
83 which have immutable type variables in them.
86 type TcHsBinds = HsBinds TcId
87 type TcMonoBinds = MonoBinds TcId
88 type TcDictBinds = TcMonoBinds
89 type TcPat = OutPat TcId
90 type TcExpr = HsExpr TcId
91 type TcGRHSs = GRHSs TcId
92 type TcGRHS = GRHS TcId
93 type TcMatch = Match TcId
94 type TcStmt = Stmt TcId
95 type TcArithSeqInfo = ArithSeqInfo TcId
96 type TcRecordBinds = HsRecordBinds TcId
97 type TcHsModule = HsModule TcId
98 type TcForeignDecl = ForeignDecl TcId
99 type TcRuleDecl = RuleDecl TcId
101 type TypecheckedPat = OutPat Id
102 type TypecheckedMonoBinds = MonoBinds Id
103 type TypecheckedDictBinds = TypecheckedMonoBinds
104 type TypecheckedHsBinds = HsBinds Id
105 type TypecheckedHsExpr = HsExpr Id
106 type TypecheckedArithSeqInfo = ArithSeqInfo Id
107 type TypecheckedStmt = Stmt Id
108 type TypecheckedMatch = Match Id
109 type TypecheckedMatchContext = HsMatchContext Id
110 type TypecheckedGRHSs = GRHSs Id
111 type TypecheckedGRHS = GRHS Id
112 type TypecheckedRecordBinds = HsRecordBinds Id
113 type TypecheckedHsModule = HsModule Id
114 type TypecheckedForeignDecl = ForeignDecl Id
115 type TypecheckedRuleDecl = RuleDecl Id
116 type TypecheckedCoreBind = (Id, CoreExpr)
120 mkHsTyApp expr [] = expr
121 mkHsTyApp expr tys = TyApp expr tys
123 mkHsDictApp expr [] = expr
124 mkHsDictApp expr dict_vars = DictApp expr dict_vars
126 mkHsTyLam [] expr = expr
127 mkHsTyLam tyvars expr = TyLam tyvars expr
129 mkHsDictLam [] expr = expr
130 mkHsDictLam dicts expr = DictLam dicts expr
132 mkHsLet EmptyMonoBinds expr = expr
133 mkHsLet mbinds expr = HsLet (MonoBind mbinds [] Recursive) expr
135 mkHsConApp data_con tys args = foldl HsApp (HsVar (dataConWrapId data_con) `mkHsTyApp` tys) args
139 %************************************************************************
141 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
143 %************************************************************************
145 Note: If @hsPatType@ doesn't bear a strong resemblance to @exprType@,
146 then something is wrong.
148 hsPatType :: TypecheckedPat -> Type
150 hsPatType (ParPat pat) = hsPatType pat
151 hsPatType (WildPat ty) = ty
152 hsPatType (VarPat var) = idType var
153 hsPatType (LazyPat pat) = hsPatType pat
154 hsPatType (LitPat lit) = hsLitType lit
155 hsPatType (AsPat var pat) = idType var
156 hsPatType (ListPat _ ty) = mkListTy ty
157 hsPatType (PArrPat _ ty) = mkPArrTy ty
158 hsPatType (TuplePat pats box) = mkTupleTy box (length pats) (map hsPatType pats)
159 hsPatType (ConPatOut _ _ ty _ _) = ty
160 hsPatType (SigPatOut _ ty _) = ty
161 hsPatType (NPatOut lit ty _) = ty
162 hsPatType (NPlusKPatOut id _ _ _) = idType id
163 hsPatType (DictPat ds ms) = case (ds ++ ms) of
166 ds -> mkTupleTy Boxed (length ds) (map idType ds)
169 hsLitType :: HsLit -> TcType
170 hsLitType (HsChar c) = charTy
171 hsLitType (HsCharPrim c) = charPrimTy
172 hsLitType (HsString str) = stringTy
173 hsLitType (HsStringPrim s) = addrPrimTy
174 hsLitType (HsInt i) = intTy
175 hsLitType (HsIntPrim i) = intPrimTy
176 hsLitType (HsInteger i) = integerTy
177 hsLitType (HsRat _ ty) = ty
178 hsLitType (HsFloatPrim f) = floatPrimTy
179 hsLitType (HsDoublePrim d) = doublePrimTy
180 hsLitType (HsLitLit _ ty) = ty
184 -- zonkId is used *during* typechecking just to zonk the Id's type
185 zonkId :: TcId -> TcM TcId
187 = zonkTcType (idType id) `thenM` \ ty' ->
188 returnM (setIdType id ty')
192 %************************************************************************
194 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
196 %************************************************************************
198 This zonking pass runs over the bindings
200 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
201 b) convert unbound TcTyVar to Void
202 c) convert each TcId to an Id by zonking its type
204 The type variables are converted by binding mutable tyvars to immutable ones
205 and then zonking as normal.
207 The Ids are converted by binding them in the normal Tc envt; that
208 way we maintain sharing; eg an Id is zonked at its binding site and they
209 all occurrences of that Id point to the common zonked copy
211 It's all pretty boring stuff, because HsSyn is such a large type, and
212 the environment manipulation is tiresome.
215 data ZonkEnv = ZonkEnv (TcType -> TcM Type) -- How to zonk a type
216 (IdEnv Id) -- What variables are in scope
217 -- Maps an Id to its zonked version; both have the same Name
218 -- Is only consulted lazily; hence knot-tying
220 emptyZonkEnv = ZonkEnv zonkTypeZapping emptyVarEnv
222 extendZonkEnv :: ZonkEnv -> [Id] -> ZonkEnv
223 extendZonkEnv (ZonkEnv zonk_ty env) ids
224 = ZonkEnv zonk_ty (extendVarEnvList env [(id,id) | id <- ids])
226 setZonkType :: ZonkEnv -> (TcType -> TcM Type) -> ZonkEnv
227 setZonkType (ZonkEnv _ env) zonk_ty = ZonkEnv zonk_ty env
229 mkZonkEnv :: [Id] -> ZonkEnv
230 mkZonkEnv ids = extendZonkEnv emptyZonkEnv ids
232 zonkIdOcc :: ZonkEnv -> TcId -> Id
233 -- Ids defined in this module should be in the envt;
234 -- ignore others. (Actually, data constructors are also
235 -- not LocalVars, even when locally defined, but that is fine.)
237 -- Actually, Template Haskell works in 'chunks' of declarations, and
238 -- an earlier chunk won't be in the 'env' that the zonking phase
239 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
240 -- zonked. There's no point in looking it up there (except for error
241 -- checking), and it's not conveniently to hand; hence the simple
242 -- 'orElse' case in the LocalVar branch.
244 -- Even without template splices, in module Main, the checking of
245 -- 'main' is done as a separte chunk.
246 zonkIdOcc (ZonkEnv zonk_ty env) id
247 | isLocalVar id = lookupVarEnv env id `orElse` id
250 zonkIdOccs env ids = map (zonkIdOcc env) ids
252 -- zonkIdBndr is used *after* typechecking to get the Id's type
253 -- to its final form. The TyVarEnv give
254 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
256 = zonkTcTypeToType env (idType id) `thenM` \ ty' ->
257 returnM (setIdType id ty')
259 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
260 zonkIdBndrs env ids = mappM (zonkIdBndr env) ids
262 zonkTopBndrs :: [TcId] -> TcM [Id]
263 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
268 zonkTopExpr :: TcExpr -> TcM TypecheckedHsExpr
269 zonkTopExpr e = zonkExpr emptyZonkEnv e
271 zonkTopDecls :: TcMonoBinds -> [TcRuleDecl] -> [TcForeignDecl]
273 TypecheckedMonoBinds,
274 [TypecheckedForeignDecl],
275 [TypecheckedRuleDecl])
276 zonkTopDecls binds rules fords -- Top level is implicitly recursive
277 = fixM (\ ~(new_ids, _, _, _) ->
279 zonk_env = mkZonkEnv new_ids
281 zonkMonoBinds zonk_env binds `thenM` \ (binds', new_ids) ->
282 zonkRules zonk_env rules `thenM` \ rules' ->
283 zonkForeignExports zonk_env fords `thenM` \ fords' ->
285 returnM (bagToList new_ids, binds', fords', rules')
288 zonkTopBinds :: TcMonoBinds -> TcM ([Id], TypecheckedMonoBinds)
290 = fixM (\ ~(new_ids, _) ->
292 zonk_env = mkZonkEnv new_ids
294 zonkMonoBinds zonk_env binds `thenM` \ (binds', new_ids) ->
295 returnM (bagToList new_ids, binds')
298 ---------------------------------------------
299 zonkBinds :: ZonkEnv -> TcHsBinds -> TcM (ZonkEnv, TypecheckedHsBinds)
300 zonkBinds env EmptyBinds = returnM (env, EmptyBinds)
302 zonkBinds env (ThenBinds b1 b2)
303 = zonkBinds env b1 `thenM` \ (env1, b1') ->
304 zonkBinds env1 b2 `thenM` \ (env2, b2') ->
305 returnM (env2, b1' `ThenBinds` b2')
307 zonkBinds env (MonoBind bind sigs is_rec)
308 = ASSERT( null sigs )
309 fixM (\ ~(_, _, new_ids) ->
311 env1 = extendZonkEnv env (bagToList new_ids)
313 zonkMonoBinds env1 bind `thenM` \ (new_bind, new_ids) ->
314 returnM (env1, new_bind, new_ids)
315 ) `thenM` \ (env1, new_bind, _) ->
316 returnM (env1, mkMonoBind new_bind [] is_rec)
318 ---------------------------------------------
319 zonkMonoBinds :: ZonkEnv -> TcMonoBinds
320 -> TcM (TypecheckedMonoBinds, Bag Id)
322 zonkMonoBinds env EmptyMonoBinds = returnM (EmptyMonoBinds, emptyBag)
324 zonkMonoBinds env (AndMonoBinds mbinds1 mbinds2)
325 = zonkMonoBinds env mbinds1 `thenM` \ (b1', ids1) ->
326 zonkMonoBinds env mbinds2 `thenM` \ (b2', ids2) ->
327 returnM (b1' `AndMonoBinds` b2',
328 ids1 `unionBags` ids2)
330 zonkMonoBinds env (PatMonoBind pat grhss locn)
331 = zonkPat env pat `thenM` \ (new_pat, ids) ->
332 zonkGRHSs env grhss `thenM` \ new_grhss ->
333 returnM (PatMonoBind new_pat new_grhss locn, ids)
335 zonkMonoBinds env (VarMonoBind var expr)
336 = zonkIdBndr env var `thenM` \ new_var ->
337 zonkExpr env expr `thenM` \ new_expr ->
338 returnM (VarMonoBind new_var new_expr, unitBag new_var)
340 zonkMonoBinds env (CoreMonoBind var core_expr)
341 = zonkIdBndr env var `thenM` \ new_var ->
342 returnM (CoreMonoBind new_var core_expr, unitBag new_var)
344 zonkMonoBinds env (FunMonoBind var inf ms locn)
345 = zonkIdBndr env var `thenM` \ new_var ->
346 mappM (zonkMatch env) ms `thenM` \ new_ms ->
347 returnM (FunMonoBind new_var inf new_ms locn, unitBag new_var)
350 zonkMonoBinds env (AbsBinds tyvars dicts exports inlines val_bind)
351 = mappM zonkTcTyVarToTyVar tyvars `thenM` \ new_tyvars ->
352 -- No need to extend tyvar env: the effects are
353 -- propagated through binding the tyvars themselves
355 zonkIdBndrs env dicts `thenM` \ new_dicts ->
356 fixM (\ ~(_, _, val_bind_ids) ->
358 env1 = extendZonkEnv (extendZonkEnv env new_dicts)
359 (bagToList val_bind_ids)
361 zonkMonoBinds env1 val_bind `thenM` \ (new_val_bind, val_bind_ids) ->
362 mappM (zonkExport env1) exports `thenM` \ new_exports ->
363 returnM (new_val_bind, new_exports, val_bind_ids)
364 ) `thenM ` \ (new_val_bind, new_exports, _) ->
366 new_globals = listToBag [global | (_, global, local) <- new_exports]
368 returnM (AbsBinds new_tyvars new_dicts new_exports inlines new_val_bind,
371 zonkExport env (tyvars, global, local)
372 = zonkTcTyVars tyvars `thenM` \ tys ->
374 new_tyvars = map (tcGetTyVar "zonkExport") tys
375 -- This isn't the binding occurrence of these tyvars
376 -- but they should *be* tyvars. Hence tcGetTyVar.
378 zonkIdBndr env global `thenM` \ new_global ->
379 returnM (new_tyvars, new_global, zonkIdOcc env local)
382 %************************************************************************
384 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
386 %************************************************************************
389 zonkMatch :: ZonkEnv -> TcMatch -> TcM TypecheckedMatch
391 zonkMatch env (Match pats _ grhss)
392 = zonkPats env pats `thenM` \ (new_pats, new_ids) ->
393 zonkGRHSs (extendZonkEnv env (bagToList new_ids)) grhss `thenM` \ new_grhss ->
394 returnM (Match new_pats Nothing new_grhss)
396 -------------------------------------------------------------------------
397 zonkGRHSs :: ZonkEnv -> TcGRHSs -> TcM TypecheckedGRHSs
399 zonkGRHSs env (GRHSs grhss binds ty)
400 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
402 zonk_grhs (GRHS guarded locn)
403 = zonkStmts new_env guarded `thenM` \ new_guarded ->
404 returnM (GRHS new_guarded locn)
406 mappM zonk_grhs grhss `thenM` \ new_grhss ->
407 zonkTcTypeToType env ty `thenM` \ new_ty ->
408 returnM (GRHSs new_grhss new_binds new_ty)
411 %************************************************************************
413 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
415 %************************************************************************
418 zonkExpr :: ZonkEnv -> TcExpr -> TcM TypecheckedHsExpr
420 zonkExpr env (HsVar id)
421 = returnM (HsVar (zonkIdOcc env id))
423 zonkExpr env (HsIPVar id)
424 = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
426 zonkExpr env (HsLit (HsRat f ty))
427 = zonkTcTypeToType env ty `thenM` \ new_ty ->
428 returnM (HsLit (HsRat f new_ty))
430 zonkExpr env (HsLit (HsLitLit lit ty))
431 = zonkTcTypeToType env ty `thenM` \ new_ty ->
432 returnM (HsLit (HsLitLit lit new_ty))
434 zonkExpr env (HsLit lit)
435 = returnM (HsLit lit)
437 -- HsOverLit doesn't appear in typechecker output
439 zonkExpr env (HsLam match)
440 = zonkMatch env match `thenM` \ new_match ->
441 returnM (HsLam new_match)
443 zonkExpr env (HsApp e1 e2)
444 = zonkExpr env e1 `thenM` \ new_e1 ->
445 zonkExpr env e2 `thenM` \ new_e2 ->
446 returnM (HsApp new_e1 new_e2)
448 zonkExpr env (HsBracketOut body bs)
449 = mappM zonk_b bs `thenM` \ bs' ->
450 returnM (HsBracketOut body bs')
452 zonk_b (n,e) = zonkExpr env e `thenM` \ e' ->
455 zonkExpr env (HsSplice n e) = WARN( True, ppr e ) -- Should not happen
456 returnM (HsSplice n e)
458 zonkExpr env (OpApp e1 op fixity e2)
459 = zonkExpr env e1 `thenM` \ new_e1 ->
460 zonkExpr env op `thenM` \ new_op ->
461 zonkExpr env e2 `thenM` \ new_e2 ->
462 returnM (OpApp new_e1 new_op fixity new_e2)
464 zonkExpr env (NegApp _ _) = panic "zonkExpr env: NegApp"
466 zonkExpr env (HsPar e)
467 = zonkExpr env e `thenM` \new_e ->
468 returnM (HsPar new_e)
470 zonkExpr env (SectionL expr op)
471 = zonkExpr env expr `thenM` \ new_expr ->
472 zonkExpr env op `thenM` \ new_op ->
473 returnM (SectionL new_expr new_op)
475 zonkExpr env (SectionR op expr)
476 = zonkExpr env op `thenM` \ new_op ->
477 zonkExpr env expr `thenM` \ new_expr ->
478 returnM (SectionR new_op new_expr)
480 zonkExpr env (HsCase expr ms src_loc)
481 = zonkExpr env expr `thenM` \ new_expr ->
482 mappM (zonkMatch env) ms `thenM` \ new_ms ->
483 returnM (HsCase new_expr new_ms src_loc)
485 zonkExpr env (HsIf e1 e2 e3 src_loc)
486 = zonkExpr env e1 `thenM` \ new_e1 ->
487 zonkExpr env e2 `thenM` \ new_e2 ->
488 zonkExpr env e3 `thenM` \ new_e3 ->
489 returnM (HsIf new_e1 new_e2 new_e3 src_loc)
491 zonkExpr env (HsLet binds expr)
492 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
493 zonkExpr new_env expr `thenM` \ new_expr ->
494 returnM (HsLet new_binds new_expr)
496 zonkExpr env (HsWith expr binds is_with)
497 = mappM zonk_ip_bind binds `thenM` \ new_binds ->
499 env1 = extendZonkEnv env (map (ipNameName . fst) new_binds)
501 zonkExpr env1 expr `thenM` \ new_expr ->
502 returnM (HsWith new_expr new_binds is_with)
505 = mapIPNameTc (zonkIdBndr env) n `thenM` \ n' ->
506 zonkExpr env e `thenM` \ e' ->
509 zonkExpr env (HsDo do_or_lc stmts ids ty src_loc)
510 = zonkStmts env stmts `thenM` \ new_stmts ->
511 zonkTcTypeToType env ty `thenM` \ new_ty ->
512 returnM (HsDo do_or_lc new_stmts
516 zonkExpr env (ExplicitList ty exprs)
517 = zonkTcTypeToType env ty `thenM` \ new_ty ->
518 mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
519 returnM (ExplicitList new_ty new_exprs)
521 zonkExpr env (ExplicitPArr ty exprs)
522 = zonkTcTypeToType env ty `thenM` \ new_ty ->
523 mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
524 returnM (ExplicitPArr new_ty new_exprs)
526 zonkExpr env (ExplicitTuple exprs boxed)
527 = mappM (zonkExpr env) exprs `thenM` \ new_exprs ->
528 returnM (ExplicitTuple new_exprs boxed)
530 zonkExpr env (RecordConOut data_con con_expr rbinds)
531 = zonkExpr env con_expr `thenM` \ new_con_expr ->
532 zonkRbinds env rbinds `thenM` \ new_rbinds ->
533 returnM (RecordConOut data_con new_con_expr new_rbinds)
535 zonkExpr env (RecordUpd _ _) = panic "zonkExpr env:RecordUpd"
537 zonkExpr env (RecordUpdOut expr in_ty out_ty rbinds)
538 = zonkExpr env expr `thenM` \ new_expr ->
539 zonkTcTypeToType env in_ty `thenM` \ new_in_ty ->
540 zonkTcTypeToType env out_ty `thenM` \ new_out_ty ->
541 zonkRbinds env rbinds `thenM` \ new_rbinds ->
542 returnM (RecordUpdOut new_expr new_in_ty new_out_ty new_rbinds)
544 zonkExpr env (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
545 zonkExpr env (ArithSeqIn _) = panic "zonkExpr env:ArithSeqIn"
546 zonkExpr env (PArrSeqIn _) = panic "zonkExpr env:PArrSeqIn"
548 zonkExpr env (ArithSeqOut expr info)
549 = zonkExpr env expr `thenM` \ new_expr ->
550 zonkArithSeq env info `thenM` \ new_info ->
551 returnM (ArithSeqOut new_expr new_info)
553 zonkExpr env (PArrSeqOut expr info)
554 = zonkExpr env expr `thenM` \ new_expr ->
555 zonkArithSeq env info `thenM` \ new_info ->
556 returnM (PArrSeqOut new_expr new_info)
558 zonkExpr env (HsCCall fun args may_gc is_casm result_ty)
559 = mappM (zonkExpr env) args `thenM` \ new_args ->
560 zonkTcTypeToType env result_ty `thenM` \ new_result_ty ->
561 returnM (HsCCall fun new_args may_gc is_casm new_result_ty)
563 zonkExpr env (HsSCC lbl expr)
564 = zonkExpr env expr `thenM` \ new_expr ->
565 returnM (HsSCC lbl new_expr)
567 zonkExpr env (TyLam tyvars expr)
568 = mappM zonkTcTyVarToTyVar tyvars `thenM` \ new_tyvars ->
569 -- No need to extend tyvar env; see AbsBinds
571 zonkExpr env expr `thenM` \ new_expr ->
572 returnM (TyLam new_tyvars new_expr)
574 zonkExpr env (TyApp expr tys)
575 = zonkExpr env expr `thenM` \ new_expr ->
576 mappM (zonkTcTypeToType env) tys `thenM` \ new_tys ->
577 returnM (TyApp new_expr new_tys)
579 zonkExpr env (DictLam dicts expr)
580 = zonkIdBndrs env dicts `thenM` \ new_dicts ->
582 env1 = extendZonkEnv env new_dicts
584 zonkExpr env1 expr `thenM` \ new_expr ->
585 returnM (DictLam new_dicts new_expr)
587 zonkExpr env (DictApp expr dicts)
588 = zonkExpr env expr `thenM` \ new_expr ->
589 returnM (DictApp new_expr (zonkIdOccs env dicts))
593 -------------------------------------------------------------------------
594 zonkArithSeq :: ZonkEnv -> TcArithSeqInfo -> TcM TypecheckedArithSeqInfo
596 zonkArithSeq env (From e)
597 = zonkExpr env e `thenM` \ new_e ->
600 zonkArithSeq env (FromThen e1 e2)
601 = zonkExpr env e1 `thenM` \ new_e1 ->
602 zonkExpr env e2 `thenM` \ new_e2 ->
603 returnM (FromThen new_e1 new_e2)
605 zonkArithSeq env (FromTo e1 e2)
606 = zonkExpr env e1 `thenM` \ new_e1 ->
607 zonkExpr env e2 `thenM` \ new_e2 ->
608 returnM (FromTo new_e1 new_e2)
610 zonkArithSeq env (FromThenTo e1 e2 e3)
611 = zonkExpr env e1 `thenM` \ new_e1 ->
612 zonkExpr env e2 `thenM` \ new_e2 ->
613 zonkExpr env e3 `thenM` \ new_e3 ->
614 returnM (FromThenTo new_e1 new_e2 new_e3)
617 -------------------------------------------------------------------------
618 zonkStmts :: ZonkEnv -> [TcStmt] -> TcM [TypecheckedStmt]
620 zonkStmts env [] = returnM []
622 zonkStmts env (ParStmtOut bndrstmtss : stmts)
623 = mappM (mappM zonkId) bndrss `thenM` \ new_bndrss ->
624 mappM (zonkStmts env) stmtss `thenM` \ new_stmtss ->
626 new_binders = concat new_bndrss
627 env1 = extendZonkEnv env new_binders
629 zonkStmts env1 stmts `thenM` \ new_stmts ->
630 returnM (ParStmtOut (zip new_bndrss new_stmtss) : new_stmts)
632 (bndrss, stmtss) = unzip bndrstmtss
634 zonkStmts env (RecStmt vs segStmts : stmts)
635 = mappM zonkId vs `thenM` \ new_vs ->
637 env1 = extendZonkEnv env new_vs
639 zonkStmts env1 segStmts `thenM` \ new_segStmts ->
640 zonkStmts env1 stmts `thenM` \ new_stmts ->
641 returnM (RecStmt new_vs new_segStmts : new_stmts)
643 zonkStmts env (ResultStmt expr locn : stmts)
644 = zonkExpr env expr `thenM` \ new_expr ->
645 zonkStmts env stmts `thenM` \ new_stmts ->
646 returnM (ResultStmt new_expr locn : new_stmts)
648 zonkStmts env (ExprStmt expr ty locn : stmts)
649 = zonkExpr env expr `thenM` \ new_expr ->
650 zonkTcTypeToType env ty `thenM` \ new_ty ->
651 zonkStmts env stmts `thenM` \ new_stmts ->
652 returnM (ExprStmt new_expr new_ty locn : new_stmts)
654 zonkStmts env (LetStmt binds : stmts)
655 = zonkBinds env binds `thenM` \ (new_env, new_binds) ->
656 zonkStmts new_env stmts `thenM` \ new_stmts ->
657 returnM (LetStmt new_binds : new_stmts)
659 zonkStmts env (BindStmt pat expr locn : stmts)
660 = zonkExpr env expr `thenM` \ new_expr ->
661 zonkPat env pat `thenM` \ (new_pat, new_ids) ->
663 env1 = extendZonkEnv env (bagToList new_ids)
665 zonkStmts env1 stmts `thenM` \ new_stmts ->
666 returnM (BindStmt new_pat new_expr locn : new_stmts)
670 -------------------------------------------------------------------------
671 zonkRbinds :: ZonkEnv -> TcRecordBinds -> TcM TypecheckedRecordBinds
673 zonkRbinds env rbinds
674 = mappM zonk_rbind rbinds
676 zonk_rbind (field, expr)
677 = zonkExpr env expr `thenM` \ new_expr ->
678 returnM (zonkIdOcc env field, new_expr)
680 -------------------------------------------------------------------------
681 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
682 mapIPNameTc f (Dupable n) = f n `thenM` \ r -> returnM (Dupable r)
683 mapIPNameTc f (Linear n) = f n `thenM` \ r -> returnM (Linear r)
687 %************************************************************************
689 \subsection[BackSubst-Pats]{Patterns}
691 %************************************************************************
694 zonkPat :: ZonkEnv -> TcPat -> TcM (TypecheckedPat, Bag Id)
696 zonkPat env (ParPat p)
697 = zonkPat env p `thenM` \ (new_p, ids) ->
698 returnM (ParPat new_p, ids)
700 zonkPat env (WildPat ty)
701 = zonkTcTypeToType env ty `thenM` \ new_ty ->
702 returnM (WildPat new_ty, emptyBag)
704 zonkPat env (VarPat v)
705 = zonkIdBndr env v `thenM` \ new_v ->
706 returnM (VarPat new_v, unitBag new_v)
708 zonkPat env (LazyPat pat)
709 = zonkPat env pat `thenM` \ (new_pat, ids) ->
710 returnM (LazyPat new_pat, ids)
712 zonkPat env (AsPat n pat)
713 = zonkIdBndr env n `thenM` \ new_n ->
714 zonkPat env pat `thenM` \ (new_pat, ids) ->
715 returnM (AsPat new_n new_pat, new_n `consBag` ids)
717 zonkPat env (ListPat pats ty)
718 = zonkTcTypeToType env ty `thenM` \ new_ty ->
719 zonkPats env pats `thenM` \ (new_pats, ids) ->
720 returnM (ListPat new_pats new_ty, ids)
722 zonkPat env (PArrPat pats ty)
723 = zonkTcTypeToType env ty `thenM` \ new_ty ->
724 zonkPats env pats `thenM` \ (new_pats, ids) ->
725 returnM (PArrPat new_pats new_ty, ids)
727 zonkPat env (TuplePat pats boxed)
728 = zonkPats env pats `thenM` \ (new_pats, ids) ->
729 returnM (TuplePat new_pats boxed, ids)
731 zonkPat env (ConPatOut n stuff ty tvs dicts)
732 = zonkTcTypeToType env ty `thenM` \ new_ty ->
733 mappM zonkTcTyVarToTyVar tvs `thenM` \ new_tvs ->
734 zonkIdBndrs env dicts `thenM` \ new_dicts ->
736 env1 = extendZonkEnv env new_dicts
738 zonkConStuff env stuff `thenM` \ (new_stuff, ids) ->
739 returnM (ConPatOut n new_stuff new_ty new_tvs new_dicts,
740 listToBag new_dicts `unionBags` ids)
742 zonkPat env (LitPat lit) = returnM (LitPat lit, emptyBag)
744 zonkPat env (SigPatOut pat ty expr)
745 = zonkPat env pat `thenM` \ (new_pat, ids) ->
746 zonkTcTypeToType env ty `thenM` \ new_ty ->
747 zonkExpr env expr `thenM` \ new_expr ->
748 returnM (SigPatOut new_pat new_ty new_expr, ids)
750 zonkPat env (NPatOut lit ty expr)
751 = zonkTcTypeToType env ty `thenM` \ new_ty ->
752 zonkExpr env expr `thenM` \ new_expr ->
753 returnM (NPatOut lit new_ty new_expr, emptyBag)
755 zonkPat env (NPlusKPatOut n k e1 e2)
756 = zonkIdBndr env n `thenM` \ new_n ->
757 zonkExpr env e1 `thenM` \ new_e1 ->
758 zonkExpr env e2 `thenM` \ new_e2 ->
759 returnM (NPlusKPatOut new_n k new_e1 new_e2, unitBag new_n)
761 zonkPat env (DictPat ds ms)
762 = zonkIdBndrs env ds `thenM` \ new_ds ->
763 zonkIdBndrs env ms `thenM` \ new_ms ->
764 returnM (DictPat new_ds new_ms,
765 listToBag new_ds `unionBags` listToBag new_ms)
767 ---------------------------
768 zonkConStuff env (PrefixCon pats)
769 = zonkPats env pats `thenM` \ (new_pats, ids) ->
770 returnM (PrefixCon new_pats, ids)
772 zonkConStuff env (InfixCon p1 p2)
773 = zonkPat env p1 `thenM` \ (new_p1, ids1) ->
774 zonkPat env p2 `thenM` \ (new_p2, ids2) ->
775 returnM (InfixCon new_p1 new_p2, ids1 `unionBags` ids2)
777 zonkConStuff env (RecCon rpats)
778 = mapAndUnzipM zonk_rpat rpats `thenM` \ (new_rpats, ids_s) ->
779 returnM (RecCon new_rpats, unionManyBags ids_s)
782 = zonkPat env pat `thenM` \ (new_pat, ids) ->
783 returnM ((f, new_pat), ids)
785 ---------------------------
787 = returnM ([], emptyBag)
789 zonkPats env (pat:pats)
790 = zonkPat env pat `thenM` \ (pat', ids1) ->
791 zonkPats env pats `thenM` \ (pats', ids2) ->
792 returnM (pat':pats', ids1 `unionBags` ids2)
795 %************************************************************************
797 \subsection[BackSubst-Foreign]{Foreign exports}
799 %************************************************************************
803 zonkForeignExports :: ZonkEnv -> [TcForeignDecl] -> TcM [TypecheckedForeignDecl]
804 zonkForeignExports env ls = mappM (zonkForeignExport env) ls
806 zonkForeignExport :: ZonkEnv -> TcForeignDecl -> TcM (TypecheckedForeignDecl)
807 zonkForeignExport env (ForeignExport i hs_ty spec isDeprec src_loc) =
808 returnM (ForeignExport (zonkIdOcc env i) undefined spec isDeprec src_loc)
812 zonkRules :: ZonkEnv -> [TcRuleDecl] -> TcM [TypecheckedRuleDecl]
813 zonkRules env rs = mappM (zonkRule env) rs
815 zonkRule env (HsRule name act vars lhs rhs loc)
816 = mappM zonk_bndr vars `thenM` \ new_bndrs ->
817 newMutVar emptyVarSet `thenM` \ unbound_tv_set ->
819 env_rhs = extendZonkEnv env (filter isId new_bndrs)
820 -- Type variables don't need an envt
821 -- They are bound through the mutable mechanism
823 env_lhs = setZonkType env_rhs (zonkTypeCollecting unbound_tv_set)
824 -- We need to gather the type variables mentioned on the LHS so we can
825 -- quantify over them. Example:
831 -- {-# RULES "myrule" foo C = 1 #-}
833 -- After type checking the LHS becomes (foo a (C a))
834 -- and we do not want to zap the unbound tyvar 'a' to (), because
835 -- that limits the applicability of the rule. Instead, we
836 -- want to quantify over it!
838 -- It's easiest to find the free tyvars here. Attempts to do so earlier
839 -- are tiresome, because (a) the data type is big and (b) finding the
840 -- free type vars of an expression is necessarily monadic operation.
841 -- (consider /\a -> f @ b, where b is side-effected to a)
843 zonkExpr env_lhs lhs `thenM` \ new_lhs ->
844 zonkExpr env_rhs rhs `thenM` \ new_rhs ->
846 readMutVar unbound_tv_set `thenM` \ unbound_tvs ->
848 final_bndrs = map RuleBndr (varSetElems unbound_tvs ++ new_bndrs)
849 -- I hate this map RuleBndr stuff
851 returnM (HsRule name act final_bndrs new_lhs new_rhs loc)
853 zonk_bndr (RuleBndr v)
854 | isId v = zonkIdBndr env v
855 | otherwise = zonkTcTyVarToTyVar v
857 zonkRule env (IfaceRuleOut fun rule)
858 = returnM (IfaceRuleOut (zonkIdOcc env fun) rule)
862 %************************************************************************
864 \subsection[BackSubst-Foreign]{Foreign exports}
866 %************************************************************************
869 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
870 zonkTcTypeToType (ZonkEnv zonk_ty _) ty = zonk_ty ty
872 zonkTypeCollecting :: TcRef TyVarSet -> TcType -> TcM Type
873 -- This variant collects unbound type variables in a mutable variable
874 zonkTypeCollecting unbound_tv_set
875 = zonkType zonk_unbound_tyvar
877 zonk_unbound_tyvar tv
878 = zonkTcTyVarToTyVar tv `thenM` \ tv' ->
879 readMutVar unbound_tv_set `thenM` \ tv_set ->
880 writeMutVar unbound_tv_set (extendVarSet tv_set tv') `thenM_`
881 return (mkTyVarTy tv')
883 zonkTypeZapping :: TcType -> TcM Type
884 -- This variant is used for everything except the LHS of rules
885 -- It zaps unbound type variables to (), or some other arbitrary type
887 = zonkType zonk_unbound_tyvar ty
889 -- Zonk a mutable but unbound type variable to an arbitrary type
890 -- We know it's unbound even though we don't carry an environment,
891 -- because at the binding site for a type variable we bind the
892 -- mutable tyvar to a fresh immutable one. So the mutable store
893 -- plays the role of an environment. If we come across a mutable
894 -- type variable that isn't so bound, it must be completely free.
895 zonk_unbound_tyvar tv = putTcTyVar tv (mkArbitraryType tv)
898 -- When the type checker finds a type variable with no binding,
899 -- which means it can be instantiated with an arbitrary type, it
900 -- usually instantiates it to Void. Eg.
904 -- length Void (Nil Void)
906 -- But in really obscure programs, the type variable might have
907 -- a kind other than *, so we need to invent a suitably-kinded type.
911 -- List for kind *->*
912 -- Tuple for kind *->...*->*
914 -- which deals with most cases. (Previously, it only dealt with
917 -- In the other cases, it just makes up a TyCon with a suitable
918 -- kind. If this gets into an interface file, anyone reading that
919 -- file won't understand it. This is fixable (by making the client
920 -- of the interface file make up a TyCon too) but it is tiresome and
921 -- never happens, so I am leaving it
923 mkArbitraryType :: TcTyVar -> Type
924 -- Make up an arbitrary type whose kind is the same as the tyvar.
925 -- We'll use this to instantiate the (unbound) tyvar.
927 | isAnyTypeKind kind = voidTy -- The vastly common case
928 | otherwise = mkTyConApp tycon []
931 (args,res) = Type.splitFunTys kind -- Kinds are simple; use Type.splitFunTys
933 tycon | kind `eqKind` tyConKind listTyCon -- *->*
934 = listTyCon -- No tuples this size
936 | all isTypeKind args && isTypeKind res
937 = tupleTyCon Boxed (length args) -- *-> ... ->*->*
940 = pprTrace "Urk! Inventing strangely-kinded void TyCon:" (ppr tc_name $$ ppr kind) $
941 mkPrimTyCon tc_name kind 0 [] VoidRep
942 -- Same name as the tyvar, apart from making it start with a colon (sigh)
943 -- I dread to think what will happen if this gets out into an
944 -- interface file. Catastrophe likely. Major sigh.
946 tc_name = mkInternalName (getUnique tv) (mkDerivedTyConOcc (getOccName tv)) noSrcLoc