2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
6 TcHsSyn: Specialisations of the @HsSyn@ syntax for the typechecker
8 This module is an extension of @HsSyn@ syntax, for use in the type
13 mkHsConApp, mkHsDictLet, mkHsApp,
14 hsLitType, hsLPatType, hsPatType,
15 mkHsAppTy, mkSimpleHsAlt,
17 shortCutLit, hsOverLitName,
19 -- re-exported from TcMonad
22 zonkTopDecls, zonkTopExpr, zonkTopLExpr,
26 #include "HsVersions.h"
29 import HsSyn -- oodles of it
50 import DynFlags( DynFlag(..) )
58 thenM :: Monad a => a b -> (b -> a c) -> a c
61 returnM :: Monad m => a -> m a
64 mappM :: (Monad m) => (a -> m b) -> [a] -> m [b]
69 %************************************************************************
71 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
73 %************************************************************************
75 Note: If @hsLPatType@ doesn't bear a strong resemblance to @exprType@,
76 then something is wrong.
78 hsLPatType :: OutPat Id -> Type
79 hsLPatType (L _ pat) = hsPatType pat
81 hsPatType :: Pat Id -> Type
82 hsPatType (ParPat pat) = hsLPatType pat
83 hsPatType (WildPat ty) = ty
84 hsPatType (VarPat var) = idType var
85 hsPatType (VarPatOut var _) = idType var
86 hsPatType (BangPat pat) = hsLPatType pat
87 hsPatType (LazyPat pat) = hsLPatType pat
88 hsPatType (LitPat lit) = hsLitType lit
89 hsPatType (AsPat var _) = idType (unLoc var)
90 hsPatType (ViewPat _ _ ty) = ty
91 hsPatType (ListPat _ ty) = mkListTy ty
92 hsPatType (PArrPat _ ty) = mkPArrTy ty
93 hsPatType (TuplePat _ _ ty) = ty
94 hsPatType (ConPatOut { pat_ty = ty }) = ty
95 hsPatType (SigPatOut _ ty) = ty
96 hsPatType (NPat lit _ _) = overLitType lit
97 hsPatType (NPlusKPat id _ _ _) = idType (unLoc id)
98 hsPatType (CoPat _ _ ty) = ty
99 hsPatType p = pprPanic "hsPatType" (ppr p)
101 hsLitType :: HsLit -> TcType
102 hsLitType (HsChar _) = charTy
103 hsLitType (HsCharPrim _) = charPrimTy
104 hsLitType (HsString _) = stringTy
105 hsLitType (HsStringPrim _) = addrPrimTy
106 hsLitType (HsInt _) = intTy
107 hsLitType (HsIntPrim _) = intPrimTy
108 hsLitType (HsWordPrim _) = wordPrimTy
109 hsLitType (HsInteger _ ty) = ty
110 hsLitType (HsRat _ ty) = ty
111 hsLitType (HsFloatPrim _) = floatPrimTy
112 hsLitType (HsDoublePrim _) = doublePrimTy
115 Overloaded literals. Here mainly becuase it uses isIntTy etc
118 shortCutLit :: OverLitVal -> TcType -> Maybe (HsExpr TcId)
119 shortCutLit (HsIntegral i) ty
120 | isIntTy ty && inIntRange i = Just (HsLit (HsInt i))
121 | isWordTy ty && inWordRange i = Just (mkLit wordDataCon (HsWordPrim i))
122 | isIntegerTy ty = Just (HsLit (HsInteger i ty))
123 | otherwise = shortCutLit (HsFractional (fromInteger i)) ty
124 -- The 'otherwise' case is important
125 -- Consider (3 :: Float). Syntactically it looks like an IntLit,
126 -- so we'll call shortCutIntLit, but of course it's a float
127 -- This can make a big difference for programs with a lot of
128 -- literals, compiled without -O
130 shortCutLit (HsFractional f) ty
131 | isFloatTy ty = Just (mkLit floatDataCon (HsFloatPrim f))
132 | isDoubleTy ty = Just (mkLit doubleDataCon (HsDoublePrim f))
133 | otherwise = Nothing
135 shortCutLit (HsIsString s) ty
136 | isStringTy ty = Just (HsLit (HsString s))
137 | otherwise = Nothing
139 mkLit :: DataCon -> HsLit -> HsExpr Id
140 mkLit con lit = HsApp (nlHsVar (dataConWrapId con)) (nlHsLit lit)
142 ------------------------------
143 hsOverLitName :: OverLitVal -> Name
144 -- Get the canonical 'fromX' name for a particular OverLitVal
145 hsOverLitName (HsIntegral {}) = fromIntegerName
146 hsOverLitName (HsFractional {}) = fromRationalName
147 hsOverLitName (HsIsString {}) = fromStringName
150 %************************************************************************
152 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
154 %************************************************************************
157 -- zonkId is used *during* typechecking just to zonk the Id's type
158 zonkId :: TcId -> TcM TcId
160 = zonkTcType (idType id) `thenM` \ ty' ->
161 returnM (Id.setIdType id ty')
164 The rest of the zonking is done *after* typechecking.
165 The main zonking pass runs over the bindings
167 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
168 b) convert unbound TcTyVar to Void
169 c) convert each TcId to an Id by zonking its type
171 The type variables are converted by binding mutable tyvars to immutable ones
172 and then zonking as normal.
174 The Ids are converted by binding them in the normal Tc envt; that
175 way we maintain sharing; eg an Id is zonked at its binding site and they
176 all occurrences of that Id point to the common zonked copy
178 It's all pretty boring stuff, because HsSyn is such a large type, and
179 the environment manipulation is tiresome.
182 data ZonkEnv = ZonkEnv (TcType -> TcM Type) -- How to zonk a type
183 (VarEnv Var) -- What variables are in scope
184 -- Maps an Id or EvVar to its zonked version; both have the same Name
185 -- Note that all evidence (coercion variables as well as dictionaries)
186 -- are kept in the ZonkEnv
187 -- Only *type* abstraction is done by side effect
188 -- Is only consulted lazily; hence knot-tying
190 emptyZonkEnv :: ZonkEnv
191 emptyZonkEnv = ZonkEnv zonkTypeZapping emptyVarEnv
193 extendZonkEnv :: ZonkEnv -> [Var] -> ZonkEnv
194 extendZonkEnv (ZonkEnv zonk_ty env) ids
195 = ZonkEnv zonk_ty (extendVarEnvList env [(id,id) | id <- ids])
197 extendZonkEnv1 :: ZonkEnv -> Var -> ZonkEnv
198 extendZonkEnv1 (ZonkEnv zonk_ty env) id
199 = ZonkEnv zonk_ty (extendVarEnv env id id)
201 setZonkType :: ZonkEnv -> (TcType -> TcM Type) -> ZonkEnv
202 setZonkType (ZonkEnv _ env) zonk_ty = ZonkEnv zonk_ty env
204 zonkEnvIds :: ZonkEnv -> [Id]
205 zonkEnvIds (ZonkEnv _ env) = varEnvElts env
207 zonkIdOcc :: ZonkEnv -> TcId -> Id
208 -- Ids defined in this module should be in the envt;
209 -- ignore others. (Actually, data constructors are also
210 -- not LocalVars, even when locally defined, but that is fine.)
211 -- (Also foreign-imported things aren't currently in the ZonkEnv;
212 -- that's ok because they don't need zonking.)
214 -- Actually, Template Haskell works in 'chunks' of declarations, and
215 -- an earlier chunk won't be in the 'env' that the zonking phase
216 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
217 -- zonked. There's no point in looking it up there (except for error
218 -- checking), and it's not conveniently to hand; hence the simple
219 -- 'orElse' case in the LocalVar branch.
221 -- Even without template splices, in module Main, the checking of
222 -- 'main' is done as a separate chunk.
223 zonkIdOcc (ZonkEnv _zonk_ty env) id
224 | isLocalVar id = lookupVarEnv env id `orElse` id
227 zonkIdOccs :: ZonkEnv -> [TcId] -> [Id]
228 zonkIdOccs env ids = map (zonkIdOcc env) ids
230 -- zonkIdBndr is used *after* typechecking to get the Id's type
231 -- to its final form. The TyVarEnv give
232 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
234 = zonkTcTypeToType env (idType id) `thenM` \ ty' ->
235 returnM (Id.setIdType id ty')
237 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
238 zonkIdBndrs env ids = mappM (zonkIdBndr env) ids
240 zonkTopBndrs :: [TcId] -> TcM [Id]
241 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
243 zonkEvBndrsX :: ZonkEnv -> [EvVar] -> TcM (ZonkEnv, [Var])
244 zonkEvBndrsX = mapAccumLM zonkEvBndrX
246 zonkEvBndrX :: ZonkEnv -> EvVar -> TcM (ZonkEnv, EvVar)
247 -- Works for dictionaries and coercions
249 = do { var' <- zonkEvBndr env var
250 ; return (extendZonkEnv1 env var', var') }
252 zonkEvBndr :: ZonkEnv -> EvVar -> TcM EvVar
253 -- Works for dictionaries and coercions
254 -- Does not extend the ZonkEnv
256 = do { ty' <- zonkTcTypeToType env (varType var)
257 ; return (setVarType var ty') }
259 zonkEvVarOcc :: ZonkEnv -> EvVar -> EvVar
260 zonkEvVarOcc env v = zonkIdOcc env v
265 zonkTopExpr :: HsExpr TcId -> TcM (HsExpr Id)
266 zonkTopExpr e = zonkExpr emptyZonkEnv e
268 zonkTopLExpr :: LHsExpr TcId -> TcM (LHsExpr Id)
269 zonkTopLExpr e = zonkLExpr emptyZonkEnv e
271 zonkTopDecls :: Bag EvBind
272 -> LHsBinds TcId -> NameSet
273 -> [LRuleDecl TcId] -> [LTcSpecPrag] -> [LForeignDecl TcId]
280 zonkTopDecls ev_binds binds sig_ns rules imp_specs fords
281 = do { (env1, ev_binds') <- zonkEvBinds emptyZonkEnv ev_binds
283 -- Warn about missing signatures
284 -- Do this only when we we have a type to offer
285 ; warn_missing_sigs <- doptM Opt_WarnMissingSigs
286 ; let sig_warn | warn_missing_sigs = topSigWarn sig_ns
287 | otherwise = noSigWarn
289 ; (env2, binds') <- zonkRecMonoBinds env1 sig_warn binds
290 -- Top level is implicitly recursive
291 ; rules' <- zonkRules env2 rules
292 ; specs' <- zonkLTcSpecPrags env2 imp_specs
293 ; fords' <- zonkForeignExports env2 fords
294 ; return (zonkEnvIds env2, ev_binds', binds', fords', specs', rules') }
296 ---------------------------------------------
297 zonkLocalBinds :: ZonkEnv -> HsLocalBinds TcId -> TcM (ZonkEnv, HsLocalBinds Id)
298 zonkLocalBinds env EmptyLocalBinds
299 = return (env, EmptyLocalBinds)
301 zonkLocalBinds _ (HsValBinds (ValBindsIn {}))
302 = panic "zonkLocalBinds" -- Not in typechecker output
304 zonkLocalBinds env (HsValBinds vb@(ValBindsOut binds sigs))
305 = do { warn_missing_sigs <- doptM Opt_WarnMissingLocalSigs
306 ; let sig_warn | not warn_missing_sigs = noSigWarn
307 | otherwise = localSigWarn sig_ns
308 sig_ns = getTypeSigNames vb
309 ; (env1, new_binds) <- go env sig_warn binds
310 ; return (env1, HsValBinds (ValBindsOut new_binds sigs)) }
314 go env sig_warn ((r,b):bs)
315 = do { (env1, b') <- zonkRecMonoBinds env sig_warn b
316 ; (env2, bs') <- go env1 sig_warn bs
317 ; return (env2, (r,b'):bs') }
319 zonkLocalBinds env (HsIPBinds (IPBinds binds dict_binds))
320 = mappM (wrapLocM zonk_ip_bind) binds `thenM` \ new_binds ->
322 env1 = extendZonkEnv env [ipNameName n | L _ (IPBind n _) <- new_binds]
324 zonkTcEvBinds env1 dict_binds `thenM` \ (env2, new_dict_binds) ->
325 returnM (env2, HsIPBinds (IPBinds new_binds new_dict_binds))
327 zonk_ip_bind (IPBind n e)
328 = mapIPNameTc (zonkIdBndr env) n `thenM` \ n' ->
329 zonkLExpr env e `thenM` \ e' ->
330 returnM (IPBind n' e')
332 ---------------------------------------------
333 zonkRecMonoBinds :: ZonkEnv -> SigWarn -> LHsBinds TcId -> TcM (ZonkEnv, LHsBinds Id)
334 zonkRecMonoBinds env sig_warn binds
335 = fixM (\ ~(_, new_binds) -> do
336 { let env1 = extendZonkEnv env (collectHsBindsBinders new_binds)
337 ; binds' <- zonkMonoBinds env1 sig_warn binds
338 ; return (env1, binds') })
340 ---------------------------------------------
341 type SigWarn = Bool -> [Id] -> TcM ()
342 -- Missing-signature warning
343 -- The Bool is True for an AbsBinds, False otherwise
346 noSigWarn _ _ = return ()
348 topSigWarn :: NameSet -> SigWarn
349 topSigWarn sig_ns _ ids = mapM_ (topSigWarnId sig_ns) ids
351 topSigWarnId :: NameSet -> Id -> TcM ()
352 -- The NameSet is the Ids that *lack* a signature
353 -- We have to do it this way round because there are
354 -- lots of top-level bindings that are generated by GHC
355 -- and that don't have signatures
356 topSigWarnId sig_ns id
357 | idName id `elemNameSet` sig_ns = warnMissingSig msg id
358 | otherwise = return ()
360 msg = ptext (sLit "Top-level binding with no type signature:")
362 localSigWarn :: NameSet -> SigWarn
363 localSigWarn sig_ns is_abs_bind ids
364 | not is_abs_bind = return ()
365 | otherwise = mapM_ (localSigWarnId sig_ns) ids
367 localSigWarnId :: NameSet -> Id -> TcM ()
368 -- NameSet are the Ids that *have* type signatures
369 localSigWarnId sig_ns id
370 | not (isSigmaTy (idType id)) = return ()
371 | idName id `elemNameSet` sig_ns = return ()
372 | otherwise = warnMissingSig msg id
374 msg = ptext (sLit "Polymophic local binding with no type signature:")
376 warnMissingSig :: SDoc -> Id -> TcM ()
377 warnMissingSig msg id
378 = do { env0 <- tcInitTidyEnv
379 ; let (env1, tidy_ty) = tidyOpenType env0 (idType id)
380 ; addWarnTcM (env1, mk_msg tidy_ty) }
382 mk_msg ty = sep [ msg, nest 2 $ pprHsVar (idName id) <+> dcolon <+> ppr ty ]
384 ---------------------------------------------
385 zonkMonoBinds :: ZonkEnv -> SigWarn -> LHsBinds TcId -> TcM (LHsBinds Id)
386 zonkMonoBinds env sig_warn binds = mapBagM (wrapLocM (zonk_bind env sig_warn)) binds
388 zonk_bind :: ZonkEnv -> SigWarn -> HsBind TcId -> TcM (HsBind Id)
389 zonk_bind env sig_warn bind@(PatBind { pat_lhs = pat, pat_rhs = grhss, pat_rhs_ty = ty})
390 = do { (_env, new_pat) <- zonkPat env pat -- Env already extended
391 ; sig_warn False (collectPatBinders new_pat)
392 ; new_grhss <- zonkGRHSs env grhss
393 ; new_ty <- zonkTcTypeToType env ty
394 ; return (bind { pat_lhs = new_pat, pat_rhs = new_grhss, pat_rhs_ty = new_ty }) }
396 zonk_bind env sig_warn (VarBind { var_id = var, var_rhs = expr, var_inline = inl })
397 = do { new_var <- zonkIdBndr env var
398 ; sig_warn False [new_var]
399 ; new_expr <- zonkLExpr env expr
400 ; return (VarBind { var_id = new_var, var_rhs = new_expr, var_inline = inl }) }
402 zonk_bind env sig_warn bind@(FunBind { fun_id = L loc var, fun_matches = ms
403 , fun_co_fn = co_fn })
404 = do { new_var <- zonkIdBndr env var
405 ; sig_warn False [new_var]
406 ; (env1, new_co_fn) <- zonkCoFn env co_fn
407 ; new_ms <- zonkMatchGroup env1 ms
408 ; return (bind { fun_id = L loc new_var, fun_matches = new_ms
409 , fun_co_fn = new_co_fn }) }
411 zonk_bind env sig_warn (AbsBinds { abs_tvs = tyvars, abs_ev_vars = evs
412 , abs_ev_binds = ev_binds
413 , abs_exports = exports
414 , abs_binds = val_binds })
415 = ASSERT( all isImmutableTyVar tyvars )
416 do { (env1, new_evs) <- zonkEvBndrsX env evs
417 ; (env2, new_ev_binds) <- zonkTcEvBinds env1 ev_binds
418 ; (new_val_bind, new_exports) <- fixM $ \ ~(new_val_binds, _) ->
419 do { let env3 = extendZonkEnv env2 (collectHsBindsBinders new_val_binds)
420 ; new_val_binds <- zonkMonoBinds env3 noSigWarn val_binds
421 ; new_exports <- mapM (zonkExport env3) exports
422 ; return (new_val_binds, new_exports) }
423 ; sig_warn True [b | (_,b,_,_) <- new_exports]
424 ; return (AbsBinds { abs_tvs = tyvars, abs_ev_vars = new_evs, abs_ev_binds = new_ev_binds
425 , abs_exports = new_exports, abs_binds = new_val_bind }) }
427 zonkExport env (tyvars, global, local, prags)
428 -- The tyvars are already zonked
429 = zonkIdBndr env global `thenM` \ new_global ->
430 zonkSpecPrags env prags `thenM` \ new_prags ->
431 returnM (tyvars, new_global, zonkIdOcc env local, new_prags)
433 zonkSpecPrags :: ZonkEnv -> TcSpecPrags -> TcM TcSpecPrags
434 zonkSpecPrags _ IsDefaultMethod = return IsDefaultMethod
435 zonkSpecPrags env (SpecPrags ps) = do { ps' <- zonkLTcSpecPrags env ps
436 ; return (SpecPrags ps') }
438 zonkLTcSpecPrags :: ZonkEnv -> [LTcSpecPrag] -> TcM [LTcSpecPrag]
439 zonkLTcSpecPrags env ps
442 zonk_prag (L loc (SpecPrag id co_fn inl))
443 = do { (_, co_fn') <- zonkCoFn env co_fn
444 ; return (L loc (SpecPrag (zonkIdOcc env id) co_fn' inl)) }
447 %************************************************************************
449 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
451 %************************************************************************
454 zonkMatchGroup :: ZonkEnv -> MatchGroup TcId-> TcM (MatchGroup Id)
455 zonkMatchGroup env (MatchGroup ms ty)
456 = do { ms' <- mapM (zonkMatch env) ms
457 ; ty' <- zonkTcTypeToType env ty
458 ; return (MatchGroup ms' ty') }
460 zonkMatch :: ZonkEnv -> LMatch TcId-> TcM (LMatch Id)
461 zonkMatch env (L loc (Match pats _ grhss))
462 = do { (env1, new_pats) <- zonkPats env pats
463 ; new_grhss <- zonkGRHSs env1 grhss
464 ; return (L loc (Match new_pats Nothing new_grhss)) }
466 -------------------------------------------------------------------------
467 zonkGRHSs :: ZonkEnv -> GRHSs TcId -> TcM (GRHSs Id)
469 zonkGRHSs env (GRHSs grhss binds)
470 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
472 zonk_grhs (GRHS guarded rhs)
473 = zonkStmts new_env guarded `thenM` \ (env2, new_guarded) ->
474 zonkLExpr env2 rhs `thenM` \ new_rhs ->
475 returnM (GRHS new_guarded new_rhs)
477 mappM (wrapLocM zonk_grhs) grhss `thenM` \ new_grhss ->
478 returnM (GRHSs new_grhss new_binds)
481 %************************************************************************
483 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
485 %************************************************************************
488 zonkLExprs :: ZonkEnv -> [LHsExpr TcId] -> TcM [LHsExpr Id]
489 zonkLExpr :: ZonkEnv -> LHsExpr TcId -> TcM (LHsExpr Id)
490 zonkExpr :: ZonkEnv -> HsExpr TcId -> TcM (HsExpr Id)
492 zonkLExprs env exprs = mappM (zonkLExpr env) exprs
493 zonkLExpr env expr = wrapLocM (zonkExpr env) expr
495 zonkExpr env (HsVar id)
496 = returnM (HsVar (zonkIdOcc env id))
498 zonkExpr env (HsIPVar id)
499 = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
501 zonkExpr env (HsLit (HsRat f ty))
502 = zonkTcTypeToType env ty `thenM` \ new_ty ->
503 returnM (HsLit (HsRat f new_ty))
505 zonkExpr _ (HsLit lit)
506 = returnM (HsLit lit)
508 zonkExpr env (HsOverLit lit)
509 = do { lit' <- zonkOverLit env lit
510 ; return (HsOverLit lit') }
512 zonkExpr env (HsLam matches)
513 = zonkMatchGroup env matches `thenM` \ new_matches ->
514 returnM (HsLam new_matches)
516 zonkExpr env (HsApp e1 e2)
517 = zonkLExpr env e1 `thenM` \ new_e1 ->
518 zonkLExpr env e2 `thenM` \ new_e2 ->
519 returnM (HsApp new_e1 new_e2)
521 zonkExpr env (HsBracketOut body bs)
522 = mappM zonk_b bs `thenM` \ bs' ->
523 returnM (HsBracketOut body bs')
525 zonk_b (n,e) = zonkLExpr env e `thenM` \ e' ->
528 zonkExpr _ (HsSpliceE s) = WARN( True, ppr s ) -- Should not happen
529 returnM (HsSpliceE s)
531 zonkExpr env (OpApp e1 op fixity e2)
532 = zonkLExpr env e1 `thenM` \ new_e1 ->
533 zonkLExpr env op `thenM` \ new_op ->
534 zonkLExpr env e2 `thenM` \ new_e2 ->
535 returnM (OpApp new_e1 new_op fixity new_e2)
537 zonkExpr env (NegApp expr op)
538 = zonkLExpr env expr `thenM` \ new_expr ->
539 zonkExpr env op `thenM` \ new_op ->
540 returnM (NegApp new_expr new_op)
542 zonkExpr env (HsPar e)
543 = zonkLExpr env e `thenM` \new_e ->
544 returnM (HsPar new_e)
546 zonkExpr env (SectionL expr op)
547 = zonkLExpr env expr `thenM` \ new_expr ->
548 zonkLExpr env op `thenM` \ new_op ->
549 returnM (SectionL new_expr new_op)
551 zonkExpr env (SectionR op expr)
552 = zonkLExpr env op `thenM` \ new_op ->
553 zonkLExpr env expr `thenM` \ new_expr ->
554 returnM (SectionR new_op new_expr)
556 zonkExpr env (ExplicitTuple tup_args boxed)
557 = do { new_tup_args <- mapM zonk_tup_arg tup_args
558 ; return (ExplicitTuple new_tup_args boxed) }
560 zonk_tup_arg (Present e) = do { e' <- zonkLExpr env e; return (Present e') }
561 zonk_tup_arg (Missing t) = do { t' <- zonkTcTypeToType env t; return (Missing t') }
563 zonkExpr env (HsCase expr ms)
564 = zonkLExpr env expr `thenM` \ new_expr ->
565 zonkMatchGroup env ms `thenM` \ new_ms ->
566 returnM (HsCase new_expr new_ms)
568 zonkExpr env (HsIf e0 e1 e2 e3)
569 = do { new_e0 <- fmapMaybeM (zonkExpr env) e0
570 ; new_e1 <- zonkLExpr env e1
571 ; new_e2 <- zonkLExpr env e2
572 ; new_e3 <- zonkLExpr env e3
573 ; returnM (HsIf new_e0 new_e1 new_e2 new_e3) }
575 zonkExpr env (HsLet binds expr)
576 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
577 zonkLExpr new_env expr `thenM` \ new_expr ->
578 returnM (HsLet new_binds new_expr)
580 zonkExpr env (HsDo do_or_lc stmts body ty)
581 = zonkStmts env stmts `thenM` \ (new_env, new_stmts) ->
582 zonkLExpr new_env body `thenM` \ new_body ->
583 zonkTcTypeToType env ty `thenM` \ new_ty ->
584 zonkDo env do_or_lc `thenM` \ new_do_or_lc ->
585 returnM (HsDo new_do_or_lc new_stmts new_body new_ty)
587 zonkExpr env (ExplicitList ty exprs)
588 = zonkTcTypeToType env ty `thenM` \ new_ty ->
589 zonkLExprs env exprs `thenM` \ new_exprs ->
590 returnM (ExplicitList new_ty new_exprs)
592 zonkExpr env (ExplicitPArr ty exprs)
593 = zonkTcTypeToType env ty `thenM` \ new_ty ->
594 zonkLExprs env exprs `thenM` \ new_exprs ->
595 returnM (ExplicitPArr new_ty new_exprs)
597 zonkExpr env (RecordCon data_con con_expr rbinds)
598 = do { new_con_expr <- zonkExpr env con_expr
599 ; new_rbinds <- zonkRecFields env rbinds
600 ; return (RecordCon data_con new_con_expr new_rbinds) }
602 zonkExpr env (RecordUpd expr rbinds cons in_tys out_tys)
603 = do { new_expr <- zonkLExpr env expr
604 ; new_in_tys <- mapM (zonkTcTypeToType env) in_tys
605 ; new_out_tys <- mapM (zonkTcTypeToType env) out_tys
606 ; new_rbinds <- zonkRecFields env rbinds
607 ; return (RecordUpd new_expr new_rbinds cons new_in_tys new_out_tys) }
609 zonkExpr env (ExprWithTySigOut e ty)
610 = do { e' <- zonkLExpr env e
611 ; return (ExprWithTySigOut e' ty) }
613 zonkExpr _ (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
615 zonkExpr env (ArithSeq expr info)
616 = zonkExpr env expr `thenM` \ new_expr ->
617 zonkArithSeq env info `thenM` \ new_info ->
618 returnM (ArithSeq new_expr new_info)
620 zonkExpr env (PArrSeq expr info)
621 = zonkExpr env expr `thenM` \ new_expr ->
622 zonkArithSeq env info `thenM` \ new_info ->
623 returnM (PArrSeq new_expr new_info)
625 zonkExpr env (HsSCC lbl expr)
626 = zonkLExpr env expr `thenM` \ new_expr ->
627 returnM (HsSCC lbl new_expr)
629 zonkExpr env (HsTickPragma info expr)
630 = zonkLExpr env expr `thenM` \ new_expr ->
631 returnM (HsTickPragma info new_expr)
633 -- hdaume: core annotations
634 zonkExpr env (HsCoreAnn lbl expr)
635 = zonkLExpr env expr `thenM` \ new_expr ->
636 returnM (HsCoreAnn lbl new_expr)
638 -- arrow notation extensions
639 zonkExpr env (HsProc pat body)
640 = do { (env1, new_pat) <- zonkPat env pat
641 ; new_body <- zonkCmdTop env1 body
642 ; return (HsProc new_pat new_body) }
644 zonkExpr env (HsArrApp e1 e2 ty ho rl)
645 = zonkLExpr env e1 `thenM` \ new_e1 ->
646 zonkLExpr env e2 `thenM` \ new_e2 ->
647 zonkTcTypeToType env ty `thenM` \ new_ty ->
648 returnM (HsArrApp new_e1 new_e2 new_ty ho rl)
650 zonkExpr env (HsArrForm op fixity args)
651 = zonkLExpr env op `thenM` \ new_op ->
652 mappM (zonkCmdTop env) args `thenM` \ new_args ->
653 returnM (HsArrForm new_op fixity new_args)
655 zonkExpr env (HsWrap co_fn expr)
656 = zonkCoFn env co_fn `thenM` \ (env1, new_co_fn) ->
657 zonkExpr env1 expr `thenM` \ new_expr ->
658 return (HsWrap new_co_fn new_expr)
660 zonkExpr _ expr = pprPanic "zonkExpr" (ppr expr)
662 zonkCmdTop :: ZonkEnv -> LHsCmdTop TcId -> TcM (LHsCmdTop Id)
663 zonkCmdTop env cmd = wrapLocM (zonk_cmd_top env) cmd
665 zonk_cmd_top :: ZonkEnv -> HsCmdTop TcId -> TcM (HsCmdTop Id)
666 zonk_cmd_top env (HsCmdTop cmd stack_tys ty ids)
667 = zonkLExpr env cmd `thenM` \ new_cmd ->
668 zonkTcTypeToTypes env stack_tys `thenM` \ new_stack_tys ->
669 zonkTcTypeToType env ty `thenM` \ new_ty ->
670 mapSndM (zonkExpr env) ids `thenM` \ new_ids ->
671 returnM (HsCmdTop new_cmd new_stack_tys new_ty new_ids)
673 -------------------------------------------------------------------------
674 zonkCoFn :: ZonkEnv -> HsWrapper -> TcM (ZonkEnv, HsWrapper)
675 zonkCoFn env WpHole = return (env, WpHole)
676 zonkCoFn env (WpCompose c1 c2) = do { (env1, c1') <- zonkCoFn env c1
677 ; (env2, c2') <- zonkCoFn env1 c2
678 ; return (env2, WpCompose c1' c2') }
679 zonkCoFn env (WpCast co) = do { co' <- zonkTcTypeToType env co
680 ; return (env, WpCast co') }
681 zonkCoFn env (WpEvLam ev) = do { (env', ev') <- zonkEvBndrX env ev
682 ; return (env', WpEvLam ev') }
683 zonkCoFn env (WpEvApp arg) = do { arg' <- zonkEvTerm env arg
684 ; return (env, WpEvApp arg') }
685 zonkCoFn env (WpTyLam tv) = ASSERT( isImmutableTyVar tv )
686 return (env, WpTyLam tv)
687 zonkCoFn env (WpTyApp ty) = do { ty' <- zonkTcTypeToType env ty
688 ; return (env, WpTyApp ty') }
689 zonkCoFn env (WpLet bs) = do { (env1, bs') <- zonkTcEvBinds env bs
690 ; return (env1, WpLet bs') }
692 -------------------------------------------------------------------------
693 zonkDo :: ZonkEnv -> HsStmtContext Name -> TcM (HsStmtContext Name)
694 -- Only used for 'do', so the only Ids are in a MDoExpr table
695 zonkDo env (MDoExpr tbl) = do { tbl' <- mapSndM (zonkExpr env) tbl
696 ; return (MDoExpr tbl') }
697 zonkDo _ do_or_lc = return do_or_lc
699 -------------------------------------------------------------------------
700 zonkOverLit :: ZonkEnv -> HsOverLit TcId -> TcM (HsOverLit Id)
701 zonkOverLit env lit@(OverLit { ol_witness = e, ol_type = ty })
702 = do { ty' <- zonkTcTypeToType env ty
703 ; e' <- zonkExpr env e
704 ; return (lit { ol_witness = e', ol_type = ty' }) }
706 -------------------------------------------------------------------------
707 zonkArithSeq :: ZonkEnv -> ArithSeqInfo TcId -> TcM (ArithSeqInfo Id)
709 zonkArithSeq env (From e)
710 = zonkLExpr env e `thenM` \ new_e ->
713 zonkArithSeq env (FromThen e1 e2)
714 = zonkLExpr env e1 `thenM` \ new_e1 ->
715 zonkLExpr env e2 `thenM` \ new_e2 ->
716 returnM (FromThen new_e1 new_e2)
718 zonkArithSeq env (FromTo e1 e2)
719 = zonkLExpr env e1 `thenM` \ new_e1 ->
720 zonkLExpr env e2 `thenM` \ new_e2 ->
721 returnM (FromTo new_e1 new_e2)
723 zonkArithSeq env (FromThenTo e1 e2 e3)
724 = zonkLExpr env e1 `thenM` \ new_e1 ->
725 zonkLExpr env e2 `thenM` \ new_e2 ->
726 zonkLExpr env e3 `thenM` \ new_e3 ->
727 returnM (FromThenTo new_e1 new_e2 new_e3)
730 -------------------------------------------------------------------------
731 zonkStmts :: ZonkEnv -> [LStmt TcId] -> TcM (ZonkEnv, [LStmt Id])
732 zonkStmts env [] = return (env, [])
733 zonkStmts env (s:ss) = do { (env1, s') <- wrapLocSndM (zonkStmt env) s
734 ; (env2, ss') <- zonkStmts env1 ss
735 ; return (env2, s' : ss') }
737 zonkStmt :: ZonkEnv -> Stmt TcId -> TcM (ZonkEnv, Stmt Id)
738 zonkStmt env (ParStmt stmts_w_bndrs)
739 = mappM zonk_branch stmts_w_bndrs `thenM` \ new_stmts_w_bndrs ->
741 new_binders = concat (map snd new_stmts_w_bndrs)
742 env1 = extendZonkEnv env new_binders
744 return (env1, ParStmt new_stmts_w_bndrs)
746 zonk_branch (stmts, bndrs) = zonkStmts env stmts `thenM` \ (env1, new_stmts) ->
747 returnM (new_stmts, zonkIdOccs env1 bndrs)
749 zonkStmt env (RecStmt { recS_stmts = segStmts, recS_later_ids = lvs, recS_rec_ids = rvs
750 , recS_ret_fn = ret_id, recS_mfix_fn = mfix_id, recS_bind_fn = bind_id
751 , recS_rec_rets = rets, recS_dicts = binds })
752 = do { new_rvs <- zonkIdBndrs env rvs
753 ; new_lvs <- zonkIdBndrs env lvs
754 ; new_ret_id <- zonkExpr env ret_id
755 ; new_mfix_id <- zonkExpr env mfix_id
756 ; new_bind_id <- zonkExpr env bind_id
757 ; let env1 = extendZonkEnv env new_rvs
758 ; (env2, new_segStmts) <- zonkStmts env1 segStmts
759 -- Zonk the ret-expressions in an envt that
760 -- has the polymorphic bindings in the envt
761 ; new_rets <- mapM (zonkExpr env2) rets
762 ; let env3 = extendZonkEnv env new_lvs -- Only the lvs are needed
763 ; (env4, new_binds) <- zonkTcEvBinds env3 binds
765 RecStmt { recS_stmts = new_segStmts, recS_later_ids = new_lvs
766 , recS_rec_ids = new_rvs, recS_ret_fn = new_ret_id
767 , recS_mfix_fn = new_mfix_id, recS_bind_fn = new_bind_id
768 , recS_rec_rets = new_rets, recS_dicts = new_binds }) }
770 zonkStmt env (ExprStmt expr then_op ty)
771 = zonkLExpr env expr `thenM` \ new_expr ->
772 zonkExpr env then_op `thenM` \ new_then ->
773 zonkTcTypeToType env ty `thenM` \ new_ty ->
774 returnM (env, ExprStmt new_expr new_then new_ty)
776 zonkStmt env (TransformStmt stmts binders usingExpr maybeByExpr)
777 = do { (env', stmts') <- zonkStmts env stmts
778 ; let binders' = zonkIdOccs env' binders
779 ; usingExpr' <- zonkLExpr env' usingExpr
780 ; maybeByExpr' <- zonkMaybeLExpr env' maybeByExpr
781 ; return (env', TransformStmt stmts' binders' usingExpr' maybeByExpr') }
783 zonkStmt env (GroupStmt stmts binderMap by using)
784 = do { (env', stmts') <- zonkStmts env stmts
785 ; binderMap' <- mappM (zonkBinderMapEntry env') binderMap
786 ; by' <- fmapMaybeM (zonkLExpr env') by
787 ; using' <- fmapEitherM (zonkLExpr env) (zonkExpr env) using
788 ; let env'' = extendZonkEnv env' (map snd binderMap')
789 ; return (env'', GroupStmt stmts' binderMap' by' using') }
791 zonkBinderMapEntry env (oldBinder, newBinder) = do
792 let oldBinder' = zonkIdOcc env oldBinder
793 newBinder' <- zonkIdBndr env newBinder
794 return (oldBinder', newBinder')
796 zonkStmt env (LetStmt binds)
797 = zonkLocalBinds env binds `thenM` \ (env1, new_binds) ->
798 returnM (env1, LetStmt new_binds)
800 zonkStmt env (BindStmt pat expr bind_op fail_op)
801 = do { new_expr <- zonkLExpr env expr
802 ; (env1, new_pat) <- zonkPat env pat
803 ; new_bind <- zonkExpr env bind_op
804 ; new_fail <- zonkExpr env fail_op
805 ; return (env1, BindStmt new_pat new_expr new_bind new_fail) }
807 zonkMaybeLExpr :: ZonkEnv -> Maybe (LHsExpr TcId) -> TcM (Maybe (LHsExpr Id))
808 zonkMaybeLExpr _ Nothing = return Nothing
809 zonkMaybeLExpr env (Just e) = (zonkLExpr env e) >>= (return . Just)
812 -------------------------------------------------------------------------
813 zonkRecFields :: ZonkEnv -> HsRecordBinds TcId -> TcM (HsRecordBinds TcId)
814 zonkRecFields env (HsRecFields flds dd)
815 = do { flds' <- mappM zonk_rbind flds
816 ; return (HsRecFields flds' dd) }
819 = do { new_id <- wrapLocM (zonkIdBndr env) (hsRecFieldId fld)
820 ; new_expr <- zonkLExpr env (hsRecFieldArg fld)
821 ; return (fld { hsRecFieldId = new_id, hsRecFieldArg = new_expr }) }
823 -------------------------------------------------------------------------
824 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
825 mapIPNameTc f (IPName n) = f n `thenM` \ r -> returnM (IPName r)
829 %************************************************************************
831 \subsection[BackSubst-Pats]{Patterns}
833 %************************************************************************
836 zonkPat :: ZonkEnv -> OutPat TcId -> TcM (ZonkEnv, OutPat Id)
837 -- Extend the environment as we go, because it's possible for one
838 -- pattern to bind something that is used in another (inside or
840 zonkPat env pat = wrapLocSndM (zonk_pat env) pat
842 zonk_pat :: ZonkEnv -> Pat TcId -> TcM (ZonkEnv, Pat Id)
843 zonk_pat env (ParPat p)
844 = do { (env', p') <- zonkPat env p
845 ; return (env', ParPat p') }
847 zonk_pat env (WildPat ty)
848 = do { ty' <- zonkTcTypeToType env ty
849 ; return (env, WildPat ty') }
851 zonk_pat env (VarPat v)
852 = do { v' <- zonkIdBndr env v
853 ; return (extendZonkEnv1 env v', VarPat v') }
855 zonk_pat env (VarPatOut v binds)
856 = do { v' <- zonkIdBndr env v
857 ; (env', binds') <- zonkTcEvBinds (extendZonkEnv1 env v') binds
858 ; returnM (env', VarPatOut v' binds') }
860 zonk_pat env (LazyPat pat)
861 = do { (env', pat') <- zonkPat env pat
862 ; return (env', LazyPat pat') }
864 zonk_pat env (BangPat pat)
865 = do { (env', pat') <- zonkPat env pat
866 ; return (env', BangPat pat') }
868 zonk_pat env (AsPat (L loc v) pat)
869 = do { v' <- zonkIdBndr env v
870 ; (env', pat') <- zonkPat (extendZonkEnv1 env v') pat
871 ; return (env', AsPat (L loc v') pat') }
873 zonk_pat env (ViewPat expr pat ty)
874 = do { expr' <- zonkLExpr env expr
875 ; (env', pat') <- zonkPat env pat
876 ; ty' <- zonkTcTypeToType env ty
877 ; return (env', ViewPat expr' pat' ty') }
879 zonk_pat env (ListPat pats ty)
880 = do { ty' <- zonkTcTypeToType env ty
881 ; (env', pats') <- zonkPats env pats
882 ; return (env', ListPat pats' ty') }
884 zonk_pat env (PArrPat pats ty)
885 = do { ty' <- zonkTcTypeToType env ty
886 ; (env', pats') <- zonkPats env pats
887 ; return (env', PArrPat pats' ty') }
889 zonk_pat env (TuplePat pats boxed ty)
890 = do { ty' <- zonkTcTypeToType env ty
891 ; (env', pats') <- zonkPats env pats
892 ; return (env', TuplePat pats' boxed ty') }
894 zonk_pat env p@(ConPatOut { pat_ty = ty, pat_dicts = evs, pat_binds = binds, pat_args = args })
895 = ASSERT( all isImmutableTyVar (pat_tvs p) )
896 do { new_ty <- zonkTcTypeToType env ty
897 ; (env1, new_evs) <- zonkEvBndrsX env evs
898 ; (env2, new_binds) <- zonkTcEvBinds env1 binds
899 ; (env', new_args) <- zonkConStuff env2 args
900 ; returnM (env', p { pat_ty = new_ty, pat_dicts = new_evs,
901 pat_binds = new_binds, pat_args = new_args }) }
903 zonk_pat env (LitPat lit) = return (env, LitPat lit)
905 zonk_pat env (SigPatOut pat ty)
906 = do { ty' <- zonkTcTypeToType env ty
907 ; (env', pat') <- zonkPat env pat
908 ; return (env', SigPatOut pat' ty') }
910 zonk_pat env (NPat lit mb_neg eq_expr)
911 = do { lit' <- zonkOverLit env lit
912 ; mb_neg' <- fmapMaybeM (zonkExpr env) mb_neg
913 ; eq_expr' <- zonkExpr env eq_expr
914 ; return (env, NPat lit' mb_neg' eq_expr') }
916 zonk_pat env (NPlusKPat (L loc n) lit e1 e2)
917 = do { n' <- zonkIdBndr env n
918 ; lit' <- zonkOverLit env lit
919 ; e1' <- zonkExpr env e1
920 ; e2' <- zonkExpr env e2
921 ; return (extendZonkEnv1 env n', NPlusKPat (L loc n') lit' e1' e2') }
923 zonk_pat env (CoPat co_fn pat ty)
924 = do { (env', co_fn') <- zonkCoFn env co_fn
925 ; (env'', pat') <- zonkPat env' (noLoc pat)
926 ; ty' <- zonkTcTypeToType env'' ty
927 ; return (env'', CoPat co_fn' (unLoc pat') ty') }
929 zonk_pat _ pat = pprPanic "zonk_pat" (ppr pat)
931 ---------------------------
932 zonkConStuff :: ZonkEnv
933 -> HsConDetails (OutPat TcId) (HsRecFields id (OutPat TcId))
935 HsConDetails (OutPat Id) (HsRecFields id (OutPat Id)))
936 zonkConStuff env (PrefixCon pats)
937 = do { (env', pats') <- zonkPats env pats
938 ; return (env', PrefixCon pats') }
940 zonkConStuff env (InfixCon p1 p2)
941 = do { (env1, p1') <- zonkPat env p1
942 ; (env', p2') <- zonkPat env1 p2
943 ; return (env', InfixCon p1' p2') }
945 zonkConStuff env (RecCon (HsRecFields rpats dd))
946 = do { (env', pats') <- zonkPats env (map hsRecFieldArg rpats)
947 ; let rpats' = zipWith (\rp p' -> rp { hsRecFieldArg = p' }) rpats pats'
948 ; returnM (env', RecCon (HsRecFields rpats' dd)) }
949 -- Field selectors have declared types; hence no zonking
951 ---------------------------
952 zonkPats :: ZonkEnv -> [OutPat TcId] -> TcM (ZonkEnv, [OutPat Id])
953 zonkPats env [] = return (env, [])
954 zonkPats env (pat:pats) = do { (env1, pat') <- zonkPat env pat
955 ; (env', pats') <- zonkPats env1 pats
956 ; return (env', pat':pats') }
959 %************************************************************************
961 \subsection[BackSubst-Foreign]{Foreign exports}
963 %************************************************************************
967 zonkForeignExports :: ZonkEnv -> [LForeignDecl TcId] -> TcM [LForeignDecl Id]
968 zonkForeignExports env ls = mappM (wrapLocM (zonkForeignExport env)) ls
970 zonkForeignExport :: ZonkEnv -> ForeignDecl TcId -> TcM (ForeignDecl Id)
971 zonkForeignExport env (ForeignExport i _hs_ty spec) =
972 returnM (ForeignExport (fmap (zonkIdOcc env) i) undefined spec)
973 zonkForeignExport _ for_imp
974 = returnM for_imp -- Foreign imports don't need zonking
978 zonkRules :: ZonkEnv -> [LRuleDecl TcId] -> TcM [LRuleDecl Id]
979 zonkRules env rs = mappM (wrapLocM (zonkRule env)) rs
981 zonkRule :: ZonkEnv -> RuleDecl TcId -> TcM (RuleDecl Id)
982 zonkRule env (HsRule name act (vars{-::[RuleBndr TcId]-}) lhs fv_lhs rhs fv_rhs)
983 = do { (env_rhs, new_bndrs) <- mapAccumLM zonk_bndr env vars
985 ; unbound_tv_set <- newMutVar emptyVarSet
986 ; let env_lhs = setZonkType env_rhs (zonkTypeCollecting unbound_tv_set)
987 -- We need to gather the type variables mentioned on the LHS so we can
988 -- quantify over them. Example:
994 -- {-# RULES "myrule" foo C = 1 #-}
996 -- After type checking the LHS becomes (foo a (C a))
997 -- and we do not want to zap the unbound tyvar 'a' to (), because
998 -- that limits the applicability of the rule. Instead, we
999 -- want to quantify over it!
1001 -- It's easiest to find the free tyvars here. Attempts to do so earlier
1002 -- are tiresome, because (a) the data type is big and (b) finding the
1003 -- free type vars of an expression is necessarily monadic operation.
1004 -- (consider /\a -> f @ b, where b is side-effected to a)
1006 ; new_lhs <- zonkLExpr env_lhs lhs
1007 ; new_rhs <- zonkLExpr env_rhs rhs
1009 ; unbound_tvs <- readMutVar unbound_tv_set
1010 ; let final_bndrs :: [RuleBndr Var]
1011 final_bndrs = map (RuleBndr . noLoc) (varSetElems unbound_tvs) ++ new_bndrs
1013 ; return (HsRule name act final_bndrs new_lhs fv_lhs new_rhs fv_rhs) }
1015 zonk_bndr env (RuleBndr (L loc v))
1016 = do { (env', v') <- zonk_it env v; return (env', RuleBndr (L loc v')) }
1017 zonk_bndr _ (RuleBndrSig {}) = panic "zonk_bndr RuleBndrSig"
1020 | isId v = do { v' <- zonkIdBndr env v; return (extendZonkEnv1 env v', v') }
1021 | isCoVar v = do { v' <- zonkEvBndr env v; return (extendZonkEnv1 env v', v') }
1022 | otherwise = ASSERT( isImmutableTyVar v) return (env, v)
1026 %************************************************************************
1028 Constraints and evidence
1030 %************************************************************************
1033 zonkEvTerm :: ZonkEnv -> EvTerm -> TcM EvTerm
1034 zonkEvTerm env (EvId v) = ASSERT2( isId v, ppr v )
1035 return (EvId (zonkIdOcc env v))
1036 zonkEvTerm env (EvCoercion co) = do { co' <- zonkTcTypeToType env co
1037 ; return (EvCoercion co') }
1038 zonkEvTerm env (EvCast v co) = ASSERT( isId v)
1039 do { co' <- zonkTcTypeToType env co
1040 ; return (EvCast (zonkIdOcc env v) co') }
1041 zonkEvTerm env (EvSuperClass d n) = return (EvSuperClass (zonkIdOcc env d) n)
1042 zonkEvTerm env (EvDFunApp df tys tms)
1043 = do { tys' <- zonkTcTypeToTypes env tys
1044 ; let tms' = map (zonkEvVarOcc env) tms
1045 ; return (EvDFunApp (zonkIdOcc env df) tys' tms') }
1047 zonkTcEvBinds :: ZonkEnv -> TcEvBinds -> TcM (ZonkEnv, TcEvBinds)
1048 zonkTcEvBinds env (TcEvBinds var) = do { (env', bs') <- zonkEvBindsVar env var
1049 ; return (env', EvBinds bs') }
1050 zonkTcEvBinds env (EvBinds bs) = do { (env', bs') <- zonkEvBinds env bs
1051 ; return (env', EvBinds bs') }
1053 zonkEvBindsVar :: ZonkEnv -> EvBindsVar -> TcM (ZonkEnv, Bag EvBind)
1054 zonkEvBindsVar env (EvBindsVar ref _) = do { bs <- readMutVar ref
1055 ; zonkEvBinds env (evBindMapBinds bs) }
1057 zonkEvBinds :: ZonkEnv -> Bag EvBind -> TcM (ZonkEnv, Bag EvBind)
1058 zonkEvBinds env binds
1059 = fixM (\ ~( _, new_binds) -> do
1060 { let env1 = extendZonkEnv env (collect_ev_bndrs new_binds)
1061 ; binds' <- mapBagM (zonkEvBind env1) binds
1062 ; return (env1, binds') })
1064 collect_ev_bndrs :: Bag EvBind -> [EvVar]
1065 collect_ev_bndrs = foldrBag add []
1066 add (EvBind var _) vars = var : vars
1068 zonkEvBind :: ZonkEnv -> EvBind -> TcM EvBind
1069 zonkEvBind env (EvBind var term)
1070 = do { var' <- zonkEvBndr env var
1071 ; term' <- zonkEvTerm env term
1072 ; return (EvBind var' term') }
1075 %************************************************************************
1079 %************************************************************************
1082 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
1083 zonkTcTypeToType (ZonkEnv zonk_ty _) ty = zonk_ty ty
1085 zonkTcTypeToTypes :: ZonkEnv -> [TcType] -> TcM [Type]
1086 zonkTcTypeToTypes env tys = mapM (zonkTcTypeToType env) tys
1088 zonkTypeCollecting :: TcRef TyVarSet -> TcType -> TcM Type
1089 -- This variant collects unbound type variables in a mutable variable
1090 zonkTypeCollecting unbound_tv_set
1091 = zonkType (mkZonkTcTyVar zonk_unbound_tyvar)
1093 zonk_unbound_tyvar tv
1094 = do { tv' <- zonkQuantifiedTyVar tv
1095 ; tv_set <- readMutVar unbound_tv_set
1096 ; writeMutVar unbound_tv_set (extendVarSet tv_set tv')
1097 ; return (mkTyVarTy tv') }
1099 zonkTypeZapping :: TcType -> TcM Type
1100 -- This variant is used for everything except the LHS of rules
1101 -- It zaps unbound type variables to (), or some other arbitrary type
1103 = zonkType (mkZonkTcTyVar zonk_unbound_tyvar) ty
1105 -- Zonk a mutable but unbound type variable to an arbitrary type
1106 -- We know it's unbound even though we don't carry an environment,
1107 -- because at the binding site for a type variable we bind the
1108 -- mutable tyvar to a fresh immutable one. So the mutable store
1109 -- plays the role of an environment. If we come across a mutable
1110 -- type variable that isn't so bound, it must be completely free.
1111 zonk_unbound_tyvar tv = do { let ty = anyTypeOfKind (tyVarKind tv)
1112 ; writeMetaTyVar tv ty