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
55 -- import Data.Traversable( traverse )
60 thenM :: Monad a => a b -> (b -> a c) -> a c
63 returnM :: Monad m => a -> m a
66 mappM :: (Monad m) => (a -> m b) -> [a] -> m [b]
71 %************************************************************************
73 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
75 %************************************************************************
77 Note: If @hsLPatType@ doesn't bear a strong resemblance to @exprType@,
78 then something is wrong.
80 hsLPatType :: OutPat Id -> Type
81 hsLPatType (L _ pat) = hsPatType pat
83 hsPatType :: Pat Id -> Type
84 hsPatType (ParPat pat) = hsLPatType pat
85 hsPatType (WildPat ty) = ty
86 hsPatType (VarPat var) = idType var
87 hsPatType (BangPat pat) = hsLPatType pat
88 hsPatType (LazyPat pat) = hsLPatType pat
89 hsPatType (LitPat lit) = hsLitType lit
90 hsPatType (AsPat var _) = idType (unLoc var)
91 hsPatType (ViewPat _ _ ty) = ty
92 hsPatType (ListPat _ ty) = mkListTy ty
93 hsPatType (PArrPat _ ty) = mkPArrTy ty
94 hsPatType (TuplePat _ _ ty) = ty
95 hsPatType (ConPatOut { pat_ty = ty }) = ty
96 hsPatType (SigPatOut _ ty) = ty
97 hsPatType (NPat lit _ _) = overLitType lit
98 hsPatType (NPlusKPat id _ _ _) = idType (unLoc id)
99 hsPatType (CoPat _ _ ty) = ty
100 hsPatType p = pprPanic "hsPatType" (ppr p)
102 hsLitType :: HsLit -> TcType
103 hsLitType (HsChar _) = charTy
104 hsLitType (HsCharPrim _) = charPrimTy
105 hsLitType (HsString _) = stringTy
106 hsLitType (HsStringPrim _) = addrPrimTy
107 hsLitType (HsInt _) = intTy
108 hsLitType (HsIntPrim _) = intPrimTy
109 hsLitType (HsWordPrim _) = wordPrimTy
110 hsLitType (HsInt64Prim _) = int64PrimTy
111 hsLitType (HsWord64Prim _) = word64PrimTy
112 hsLitType (HsInteger _ ty) = ty
113 hsLitType (HsRat _ ty) = ty
114 hsLitType (HsFloatPrim _) = floatPrimTy
115 hsLitType (HsDoublePrim _) = doublePrimTy
118 Overloaded literals. Here mainly becuase it uses isIntTy etc
121 shortCutLit :: OverLitVal -> TcType -> Maybe (HsExpr TcId)
122 shortCutLit (HsIntegral i) ty
123 | isIntTy ty && inIntRange i = Just (HsLit (HsInt i))
124 | isWordTy ty && inWordRange i = Just (mkLit wordDataCon (HsWordPrim i))
125 | isIntegerTy ty = Just (HsLit (HsInteger i ty))
126 | otherwise = shortCutLit (HsFractional (integralFractionalLit i)) ty
127 -- The 'otherwise' case is important
128 -- Consider (3 :: Float). Syntactically it looks like an IntLit,
129 -- so we'll call shortCutIntLit, but of course it's a float
130 -- This can make a big difference for programs with a lot of
131 -- literals, compiled without -O
133 shortCutLit (HsFractional f) ty
134 | isFloatTy ty = Just (mkLit floatDataCon (HsFloatPrim f))
135 | isDoubleTy ty = Just (mkLit doubleDataCon (HsDoublePrim f))
136 | otherwise = Nothing
138 shortCutLit (HsIsString s) ty
139 | isStringTy ty = Just (HsLit (HsString s))
140 | otherwise = Nothing
142 mkLit :: DataCon -> HsLit -> HsExpr Id
143 mkLit con lit = HsApp (nlHsVar (dataConWrapId con)) (nlHsLit lit)
145 ------------------------------
146 hsOverLitName :: OverLitVal -> Name
147 -- Get the canonical 'fromX' name for a particular OverLitVal
148 hsOverLitName (HsIntegral {}) = fromIntegerName
149 hsOverLitName (HsFractional {}) = fromRationalName
150 hsOverLitName (HsIsString {}) = fromStringName
153 %************************************************************************
155 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
157 %************************************************************************
160 -- zonkId is used *during* typechecking just to zonk the Id's type
161 zonkId :: TcId -> TcM TcId
163 = zonkTcType (idType id) `thenM` \ ty' ->
164 returnM (Id.setIdType id ty')
167 The rest of the zonking is done *after* typechecking.
168 The main zonking pass runs over the bindings
170 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
171 b) convert unbound TcTyVar to Void
172 c) convert each TcId to an Id by zonking its type
174 The type variables are converted by binding mutable tyvars to immutable ones
175 and then zonking as normal.
177 The Ids are converted by binding them in the normal Tc envt; that
178 way we maintain sharing; eg an Id is zonked at its binding site and they
179 all occurrences of that Id point to the common zonked copy
181 It's all pretty boring stuff, because HsSyn is such a large type, and
182 the environment manipulation is tiresome.
185 data ZonkEnv = ZonkEnv (TcType -> TcM Type) -- How to zonk a type
186 (VarEnv Var) -- What variables are in scope
187 -- Maps an Id or EvVar to its zonked version; both have the same Name
188 -- Note that all evidence (coercion variables as well as dictionaries)
189 -- are kept in the ZonkEnv
190 -- Only *type* abstraction is done by side effect
191 -- Is only consulted lazily; hence knot-tying
193 emptyZonkEnv :: ZonkEnv
194 emptyZonkEnv = ZonkEnv zonkTypeZapping emptyVarEnv
196 extendZonkEnv :: ZonkEnv -> [Var] -> ZonkEnv
197 extendZonkEnv (ZonkEnv zonk_ty env) ids
198 = ZonkEnv zonk_ty (extendVarEnvList env [(id,id) | id <- ids])
200 extendZonkEnv1 :: ZonkEnv -> Var -> ZonkEnv
201 extendZonkEnv1 (ZonkEnv zonk_ty env) id
202 = ZonkEnv zonk_ty (extendVarEnv env id id)
204 setZonkType :: ZonkEnv -> (TcType -> TcM Type) -> ZonkEnv
205 setZonkType (ZonkEnv _ env) zonk_ty = ZonkEnv zonk_ty env
207 zonkEnvIds :: ZonkEnv -> [Id]
208 zonkEnvIds (ZonkEnv _ env) = varEnvElts env
210 zonkIdOcc :: ZonkEnv -> TcId -> Id
211 -- Ids defined in this module should be in the envt;
212 -- ignore others. (Actually, data constructors are also
213 -- not LocalVars, even when locally defined, but that is fine.)
214 -- (Also foreign-imported things aren't currently in the ZonkEnv;
215 -- that's ok because they don't need zonking.)
217 -- Actually, Template Haskell works in 'chunks' of declarations, and
218 -- an earlier chunk won't be in the 'env' that the zonking phase
219 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
220 -- zonked. There's no point in looking it up there (except for error
221 -- checking), and it's not conveniently to hand; hence the simple
222 -- 'orElse' case in the LocalVar branch.
224 -- Even without template splices, in module Main, the checking of
225 -- 'main' is done as a separate chunk.
226 zonkIdOcc (ZonkEnv _zonk_ty env) id
227 | isLocalVar id = lookupVarEnv env id `orElse` id
230 zonkIdOccs :: ZonkEnv -> [TcId] -> [Id]
231 zonkIdOccs env ids = map (zonkIdOcc env) ids
233 -- zonkIdBndr is used *after* typechecking to get the Id's type
234 -- to its final form. The TyVarEnv give
235 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
237 = zonkTcTypeToType env (idType id) `thenM` \ ty' ->
238 returnM (Id.setIdType id ty')
240 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
241 zonkIdBndrs env ids = mappM (zonkIdBndr env) ids
243 zonkTopBndrs :: [TcId] -> TcM [Id]
244 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
246 zonkEvBndrsX :: ZonkEnv -> [EvVar] -> TcM (ZonkEnv, [Var])
247 zonkEvBndrsX = mapAccumLM zonkEvBndrX
249 zonkEvBndrX :: ZonkEnv -> EvVar -> TcM (ZonkEnv, EvVar)
250 -- Works for dictionaries and coercions
252 = do { var' <- zonkEvBndr env var
253 ; return (extendZonkEnv1 env var', var') }
255 zonkEvBndr :: ZonkEnv -> EvVar -> TcM EvVar
256 -- Works for dictionaries and coercions
257 -- Does not extend the ZonkEnv
259 = do { ty' <- zonkTcTypeToType env (varType var)
260 ; return (setVarType var ty') }
262 zonkEvVarOcc :: ZonkEnv -> EvVar -> EvVar
263 zonkEvVarOcc env v = zonkIdOcc env v
268 zonkTopExpr :: HsExpr TcId -> TcM (HsExpr Id)
269 zonkTopExpr e = zonkExpr emptyZonkEnv e
271 zonkTopLExpr :: LHsExpr TcId -> TcM (LHsExpr Id)
272 zonkTopLExpr e = zonkLExpr emptyZonkEnv e
274 zonkTopDecls :: Bag EvBind
275 -> LHsBinds TcId -> NameSet
276 -> [LRuleDecl TcId] -> [LVectDecl TcId] -> [LTcSpecPrag] -> [LForeignDecl TcId]
284 zonkTopDecls ev_binds binds sig_ns rules vects imp_specs fords
285 = do { (env1, ev_binds') <- zonkEvBinds emptyZonkEnv ev_binds
287 -- Warn about missing signatures
288 -- Do this only when we we have a type to offer
289 ; warn_missing_sigs <- woptM Opt_WarnMissingSigs
290 ; let sig_warn | warn_missing_sigs = topSigWarn sig_ns
291 | otherwise = noSigWarn
293 ; (env2, binds') <- zonkRecMonoBinds env1 sig_warn binds
294 -- Top level is implicitly recursive
295 ; rules' <- zonkRules env2 rules
296 ; vects' <- zonkVects env2 vects
297 ; specs' <- zonkLTcSpecPrags env2 imp_specs
298 ; fords' <- zonkForeignExports env2 fords
299 ; return (zonkEnvIds env2, ev_binds', binds', fords', specs', rules', vects') }
301 ---------------------------------------------
302 zonkLocalBinds :: ZonkEnv -> HsLocalBinds TcId -> TcM (ZonkEnv, HsLocalBinds Id)
303 zonkLocalBinds env EmptyLocalBinds
304 = return (env, EmptyLocalBinds)
306 zonkLocalBinds _ (HsValBinds (ValBindsIn {}))
307 = panic "zonkLocalBinds" -- Not in typechecker output
309 zonkLocalBinds env (HsValBinds vb@(ValBindsOut binds sigs))
310 = do { warn_missing_sigs <- woptM Opt_WarnMissingLocalSigs
311 ; let sig_warn | not warn_missing_sigs = noSigWarn
312 | otherwise = localSigWarn sig_ns
313 sig_ns = getTypeSigNames vb
314 ; (env1, new_binds) <- go env sig_warn binds
315 ; return (env1, HsValBinds (ValBindsOut new_binds sigs)) }
319 go env sig_warn ((r,b):bs)
320 = do { (env1, b') <- zonkRecMonoBinds env sig_warn b
321 ; (env2, bs') <- go env1 sig_warn bs
322 ; return (env2, (r,b'):bs') }
324 zonkLocalBinds env (HsIPBinds (IPBinds binds dict_binds))
325 = mappM (wrapLocM zonk_ip_bind) binds `thenM` \ new_binds ->
327 env1 = extendZonkEnv env [ipNameName n | L _ (IPBind n _) <- new_binds]
329 zonkTcEvBinds env1 dict_binds `thenM` \ (env2, new_dict_binds) ->
330 returnM (env2, HsIPBinds (IPBinds new_binds new_dict_binds))
332 zonk_ip_bind (IPBind n e)
333 = mapIPNameTc (zonkIdBndr env) n `thenM` \ n' ->
334 zonkLExpr env e `thenM` \ e' ->
335 returnM (IPBind n' e')
337 ---------------------------------------------
338 zonkRecMonoBinds :: ZonkEnv -> SigWarn -> LHsBinds TcId -> TcM (ZonkEnv, LHsBinds Id)
339 zonkRecMonoBinds env sig_warn binds
340 = fixM (\ ~(_, new_binds) -> do
341 { let env1 = extendZonkEnv env (collectHsBindsBinders new_binds)
342 ; binds' <- zonkMonoBinds env1 sig_warn binds
343 ; return (env1, binds') })
345 ---------------------------------------------
346 type SigWarn = Bool -> [Id] -> TcM ()
347 -- Missing-signature warning
348 -- The Bool is True for an AbsBinds, False otherwise
351 noSigWarn _ _ = return ()
353 topSigWarn :: NameSet -> SigWarn
354 topSigWarn sig_ns _ ids = mapM_ (topSigWarnId sig_ns) ids
356 topSigWarnId :: NameSet -> Id -> TcM ()
357 -- The NameSet is the Ids that *lack* a signature
358 -- We have to do it this way round because there are
359 -- lots of top-level bindings that are generated by GHC
360 -- and that don't have signatures
361 topSigWarnId sig_ns id
362 | idName id `elemNameSet` sig_ns = warnMissingSig msg id
363 | otherwise = return ()
365 msg = ptext (sLit "Top-level binding with no type signature:")
367 localSigWarn :: NameSet -> SigWarn
368 localSigWarn sig_ns is_abs_bind ids
369 | not is_abs_bind = return ()
370 | otherwise = mapM_ (localSigWarnId sig_ns) ids
372 localSigWarnId :: NameSet -> Id -> TcM ()
373 -- NameSet are the Ids that *have* type signatures
374 localSigWarnId sig_ns id
375 | not (isSigmaTy (idType id)) = return ()
376 | idName id `elemNameSet` sig_ns = return ()
377 | otherwise = warnMissingSig msg id
379 msg = ptext (sLit "Polymophic local binding with no type signature:")
381 warnMissingSig :: SDoc -> Id -> TcM ()
382 warnMissingSig msg id
383 = do { env0 <- tcInitTidyEnv
384 ; let (env1, tidy_ty) = tidyOpenType env0 (idType id)
385 ; addWarnTcM (env1, mk_msg tidy_ty) }
387 mk_msg ty = sep [ msg, nest 2 $ pprHsVar (idName id) <+> dcolon <+> ppr ty ]
389 ---------------------------------------------
390 zonkMonoBinds :: ZonkEnv -> SigWarn -> LHsBinds TcId -> TcM (LHsBinds Id)
391 zonkMonoBinds env sig_warn binds = mapBagM (wrapLocM (zonk_bind env sig_warn)) binds
393 zonk_bind :: ZonkEnv -> SigWarn -> HsBind TcId -> TcM (HsBind Id)
394 zonk_bind env sig_warn bind@(PatBind { pat_lhs = pat, pat_rhs = grhss, pat_rhs_ty = ty})
395 = do { (_env, new_pat) <- zonkPat env pat -- Env already extended
396 ; sig_warn False (collectPatBinders new_pat)
397 ; new_grhss <- zonkGRHSs env grhss
398 ; new_ty <- zonkTcTypeToType env ty
399 ; return (bind { pat_lhs = new_pat, pat_rhs = new_grhss, pat_rhs_ty = new_ty }) }
401 zonk_bind env sig_warn (VarBind { var_id = var, var_rhs = expr, var_inline = inl })
402 = do { new_var <- zonkIdBndr env var
403 ; sig_warn False [new_var]
404 ; new_expr <- zonkLExpr env expr
405 ; return (VarBind { var_id = new_var, var_rhs = new_expr, var_inline = inl }) }
407 zonk_bind env sig_warn bind@(FunBind { fun_id = L loc var, fun_matches = ms
408 , fun_co_fn = co_fn })
409 = do { new_var <- zonkIdBndr env var
410 ; sig_warn False [new_var]
411 ; (env1, new_co_fn) <- zonkCoFn env co_fn
412 ; new_ms <- zonkMatchGroup env1 ms
413 ; return (bind { fun_id = L loc new_var, fun_matches = new_ms
414 , fun_co_fn = new_co_fn }) }
416 zonk_bind env sig_warn (AbsBinds { abs_tvs = tyvars, abs_ev_vars = evs
417 , abs_ev_binds = ev_binds
418 , abs_exports = exports
419 , abs_binds = val_binds })
420 = ASSERT( all isImmutableTyVar tyvars )
421 do { (env1, new_evs) <- zonkEvBndrsX env evs
422 ; (env2, new_ev_binds) <- zonkTcEvBinds env1 ev_binds
423 ; (new_val_bind, new_exports) <- fixM $ \ ~(new_val_binds, _) ->
424 do { let env3 = extendZonkEnv env2 (collectHsBindsBinders new_val_binds)
425 ; new_val_binds <- zonkMonoBinds env3 noSigWarn val_binds
426 ; new_exports <- mapM (zonkExport env3) exports
427 ; return (new_val_binds, new_exports) }
428 ; sig_warn True (map abe_poly new_exports)
429 ; return (AbsBinds { abs_tvs = tyvars, abs_ev_vars = new_evs, abs_ev_binds = new_ev_binds
430 , abs_exports = new_exports, abs_binds = new_val_bind }) }
432 zonkExport env (ABE{ abe_wrap = wrap, abe_poly = poly_id
433 , abe_mono = mono_id, abe_prags = prags })
434 = zonkIdBndr env poly_id `thenM` \ new_poly_id ->
435 zonkCoFn env wrap `thenM` \ (_, new_wrap) ->
436 zonkSpecPrags env prags `thenM` \ new_prags ->
437 returnM (ABE{ abe_wrap = new_wrap, abe_poly = new_poly_id
438 , abe_mono = zonkIdOcc env mono_id, abe_prags = new_prags })
440 zonkSpecPrags :: ZonkEnv -> TcSpecPrags -> TcM TcSpecPrags
441 zonkSpecPrags _ IsDefaultMethod = return IsDefaultMethod
442 zonkSpecPrags env (SpecPrags ps) = do { ps' <- zonkLTcSpecPrags env ps
443 ; return (SpecPrags ps') }
445 zonkLTcSpecPrags :: ZonkEnv -> [LTcSpecPrag] -> TcM [LTcSpecPrag]
446 zonkLTcSpecPrags env ps
449 zonk_prag (L loc (SpecPrag id co_fn inl))
450 = do { (_, co_fn') <- zonkCoFn env co_fn
451 ; return (L loc (SpecPrag (zonkIdOcc env id) co_fn' inl)) }
454 %************************************************************************
456 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
458 %************************************************************************
461 zonkMatchGroup :: ZonkEnv -> MatchGroup TcId-> TcM (MatchGroup Id)
462 zonkMatchGroup env (MatchGroup ms ty)
463 = do { ms' <- mapM (zonkMatch env) ms
464 ; ty' <- zonkTcTypeToType env ty
465 ; return (MatchGroup ms' ty') }
467 zonkMatch :: ZonkEnv -> LMatch TcId-> TcM (LMatch Id)
468 zonkMatch env (L loc (Match pats _ grhss))
469 = do { (env1, new_pats) <- zonkPats env pats
470 ; new_grhss <- zonkGRHSs env1 grhss
471 ; return (L loc (Match new_pats Nothing new_grhss)) }
473 -------------------------------------------------------------------------
474 zonkGRHSs :: ZonkEnv -> GRHSs TcId -> TcM (GRHSs Id)
476 zonkGRHSs env (GRHSs grhss binds)
477 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
479 zonk_grhs (GRHS guarded rhs)
480 = zonkStmts new_env guarded `thenM` \ (env2, new_guarded) ->
481 zonkLExpr env2 rhs `thenM` \ new_rhs ->
482 returnM (GRHS new_guarded new_rhs)
484 mappM (wrapLocM zonk_grhs) grhss `thenM` \ new_grhss ->
485 returnM (GRHSs new_grhss new_binds)
488 %************************************************************************
490 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
492 %************************************************************************
495 zonkLExprs :: ZonkEnv -> [LHsExpr TcId] -> TcM [LHsExpr Id]
496 zonkLExpr :: ZonkEnv -> LHsExpr TcId -> TcM (LHsExpr Id)
497 zonkExpr :: ZonkEnv -> HsExpr TcId -> TcM (HsExpr Id)
499 zonkLExprs env exprs = mappM (zonkLExpr env) exprs
500 zonkLExpr env expr = wrapLocM (zonkExpr env) expr
502 zonkExpr env (HsVar id)
503 = returnM (HsVar (zonkIdOcc env id))
505 zonkExpr env (HsIPVar id)
506 = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
508 zonkExpr env (HsLit (HsRat f ty))
509 = zonkTcTypeToType env ty `thenM` \ new_ty ->
510 returnM (HsLit (HsRat f new_ty))
512 zonkExpr _ (HsLit lit)
513 = returnM (HsLit lit)
515 zonkExpr env (HsOverLit lit)
516 = do { lit' <- zonkOverLit env lit
517 ; return (HsOverLit lit') }
519 zonkExpr env (HsLam matches)
520 = zonkMatchGroup env matches `thenM` \ new_matches ->
521 returnM (HsLam new_matches)
523 zonkExpr env (HsApp e1 e2)
524 = zonkLExpr env e1 `thenM` \ new_e1 ->
525 zonkLExpr env e2 `thenM` \ new_e2 ->
526 returnM (HsApp new_e1 new_e2)
528 zonkExpr env (HsBracketOut body bs)
529 = mappM zonk_b bs `thenM` \ bs' ->
530 returnM (HsBracketOut body bs')
532 zonk_b (n,e) = zonkLExpr env e `thenM` \ e' ->
535 zonkExpr _ (HsSpliceE s) = WARN( True, ppr s ) -- Should not happen
536 returnM (HsSpliceE s)
538 zonkExpr env (OpApp e1 op fixity e2)
539 = zonkLExpr env e1 `thenM` \ new_e1 ->
540 zonkLExpr env op `thenM` \ new_op ->
541 zonkLExpr env e2 `thenM` \ new_e2 ->
542 returnM (OpApp new_e1 new_op fixity new_e2)
544 zonkExpr env (NegApp expr op)
545 = zonkLExpr env expr `thenM` \ new_expr ->
546 zonkExpr env op `thenM` \ new_op ->
547 returnM (NegApp new_expr new_op)
549 zonkExpr env (HsPar e)
550 = zonkLExpr env e `thenM` \new_e ->
551 returnM (HsPar new_e)
553 zonkExpr env (SectionL expr op)
554 = zonkLExpr env expr `thenM` \ new_expr ->
555 zonkLExpr env op `thenM` \ new_op ->
556 returnM (SectionL new_expr new_op)
558 zonkExpr env (SectionR op expr)
559 = zonkLExpr env op `thenM` \ new_op ->
560 zonkLExpr env expr `thenM` \ new_expr ->
561 returnM (SectionR new_op new_expr)
563 zonkExpr env (ExplicitTuple tup_args boxed)
564 = do { new_tup_args <- mapM zonk_tup_arg tup_args
565 ; return (ExplicitTuple new_tup_args boxed) }
567 zonk_tup_arg (Present e) = do { e' <- zonkLExpr env e; return (Present e') }
568 zonk_tup_arg (Missing t) = do { t' <- zonkTcTypeToType env t; return (Missing t') }
570 zonkExpr env (HsCase expr ms)
571 = zonkLExpr env expr `thenM` \ new_expr ->
572 zonkMatchGroup env ms `thenM` \ new_ms ->
573 returnM (HsCase new_expr new_ms)
575 zonkExpr env (HsIf e0 e1 e2 e3)
576 = do { new_e0 <- fmapMaybeM (zonkExpr env) e0
577 ; new_e1 <- zonkLExpr env e1
578 ; new_e2 <- zonkLExpr env e2
579 ; new_e3 <- zonkLExpr env e3
580 ; returnM (HsIf new_e0 new_e1 new_e2 new_e3) }
582 zonkExpr env (HsLet binds expr)
583 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
584 zonkLExpr new_env expr `thenM` \ new_expr ->
585 returnM (HsLet new_binds new_expr)
587 zonkExpr env (HsDo do_or_lc stmts ty)
588 = zonkStmts env stmts `thenM` \ (_, new_stmts) ->
589 zonkTcTypeToType env ty `thenM` \ new_ty ->
590 returnM (HsDo do_or_lc new_stmts new_ty)
592 zonkExpr env (ExplicitList ty exprs)
593 = zonkTcTypeToType env ty `thenM` \ new_ty ->
594 zonkLExprs env exprs `thenM` \ new_exprs ->
595 returnM (ExplicitList new_ty new_exprs)
597 zonkExpr env (ExplicitPArr ty exprs)
598 = zonkTcTypeToType env ty `thenM` \ new_ty ->
599 zonkLExprs env exprs `thenM` \ new_exprs ->
600 returnM (ExplicitPArr new_ty new_exprs)
602 zonkExpr env (RecordCon data_con con_expr rbinds)
603 = do { new_con_expr <- zonkExpr env con_expr
604 ; new_rbinds <- zonkRecFields env rbinds
605 ; return (RecordCon data_con new_con_expr new_rbinds) }
607 zonkExpr env (RecordUpd expr rbinds cons in_tys out_tys)
608 = do { new_expr <- zonkLExpr env expr
609 ; new_in_tys <- mapM (zonkTcTypeToType env) in_tys
610 ; new_out_tys <- mapM (zonkTcTypeToType env) out_tys
611 ; new_rbinds <- zonkRecFields env rbinds
612 ; return (RecordUpd new_expr new_rbinds cons new_in_tys new_out_tys) }
614 zonkExpr env (ExprWithTySigOut e ty)
615 = do { e' <- zonkLExpr env e
616 ; return (ExprWithTySigOut e' ty) }
618 zonkExpr _ (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
620 zonkExpr env (ArithSeq expr info)
621 = zonkExpr env expr `thenM` \ new_expr ->
622 zonkArithSeq env info `thenM` \ new_info ->
623 returnM (ArithSeq new_expr new_info)
625 zonkExpr env (PArrSeq expr info)
626 = zonkExpr env expr `thenM` \ new_expr ->
627 zonkArithSeq env info `thenM` \ new_info ->
628 returnM (PArrSeq new_expr new_info)
630 zonkExpr env (HsSCC lbl expr)
631 = zonkLExpr env expr `thenM` \ new_expr ->
632 returnM (HsSCC lbl new_expr)
634 zonkExpr env (HsTickPragma info expr)
635 = zonkLExpr env expr `thenM` \ new_expr ->
636 returnM (HsTickPragma info new_expr)
638 -- hdaume: core annotations
639 zonkExpr env (HsCoreAnn lbl expr)
640 = zonkLExpr env expr `thenM` \ new_expr ->
641 returnM (HsCoreAnn lbl new_expr)
643 -- arrow notation extensions
644 zonkExpr env (HsProc pat body)
645 = do { (env1, new_pat) <- zonkPat env pat
646 ; new_body <- zonkCmdTop env1 body
647 ; return (HsProc new_pat new_body) }
649 zonkExpr env (HsArrApp e1 e2 ty ho rl)
650 = zonkLExpr env e1 `thenM` \ new_e1 ->
651 zonkLExpr env e2 `thenM` \ new_e2 ->
652 zonkTcTypeToType env ty `thenM` \ new_ty ->
653 returnM (HsArrApp new_e1 new_e2 new_ty ho rl)
655 zonkExpr env (HsArrForm op fixity args)
656 = zonkLExpr env op `thenM` \ new_op ->
657 mappM (zonkCmdTop env) args `thenM` \ new_args ->
658 returnM (HsArrForm new_op fixity new_args)
660 zonkExpr env (HsWrap co_fn expr)
661 = zonkCoFn env co_fn `thenM` \ (env1, new_co_fn) ->
662 zonkExpr env1 expr `thenM` \ new_expr ->
663 return (HsWrap new_co_fn new_expr)
665 zonkExpr _ expr = pprPanic "zonkExpr" (ppr expr)
667 zonkCmdTop :: ZonkEnv -> LHsCmdTop TcId -> TcM (LHsCmdTop Id)
668 zonkCmdTop env cmd = wrapLocM (zonk_cmd_top env) cmd
670 zonk_cmd_top :: ZonkEnv -> HsCmdTop TcId -> TcM (HsCmdTop Id)
671 zonk_cmd_top env (HsCmdTop cmd stack_tys ty ids)
672 = zonkLExpr env cmd `thenM` \ new_cmd ->
673 zonkTcTypeToTypes env stack_tys `thenM` \ new_stack_tys ->
674 zonkTcTypeToType env ty `thenM` \ new_ty ->
675 mapSndM (zonkExpr env) ids `thenM` \ new_ids ->
676 returnM (HsCmdTop new_cmd new_stack_tys new_ty new_ids)
678 -------------------------------------------------------------------------
679 zonkCoFn :: ZonkEnv -> HsWrapper -> TcM (ZonkEnv, HsWrapper)
680 zonkCoFn env WpHole = return (env, WpHole)
681 zonkCoFn env (WpCompose c1 c2) = do { (env1, c1') <- zonkCoFn env c1
682 ; (env2, c2') <- zonkCoFn env1 c2
683 ; return (env2, WpCompose c1' c2') }
684 zonkCoFn env (WpCast co) = do { co' <- zonkTcCoToCo env co
685 ; return (env, WpCast co') }
686 zonkCoFn env (WpEvLam ev) = do { (env', ev') <- zonkEvBndrX env ev
687 ; return (env', WpEvLam ev') }
688 zonkCoFn env (WpEvApp arg) = do { arg' <- zonkEvTerm env arg
689 ; return (env, WpEvApp arg') }
690 zonkCoFn env (WpTyLam tv) = ASSERT( isImmutableTyVar tv )
691 return (env, WpTyLam tv)
692 zonkCoFn env (WpTyApp ty) = do { ty' <- zonkTcTypeToType env ty
693 ; return (env, WpTyApp ty') }
694 zonkCoFn env (WpLet bs) = do { (env1, bs') <- zonkTcEvBinds env bs
695 ; return (env1, WpLet bs') }
697 -------------------------------------------------------------------------
698 zonkOverLit :: ZonkEnv -> HsOverLit TcId -> TcM (HsOverLit Id)
699 zonkOverLit env lit@(OverLit { ol_witness = e, ol_type = ty })
700 = do { ty' <- zonkTcTypeToType env ty
701 ; e' <- zonkExpr env e
702 ; return (lit { ol_witness = e', ol_type = ty' }) }
704 -------------------------------------------------------------------------
705 zonkArithSeq :: ZonkEnv -> ArithSeqInfo TcId -> TcM (ArithSeqInfo Id)
707 zonkArithSeq env (From e)
708 = zonkLExpr env e `thenM` \ new_e ->
711 zonkArithSeq env (FromThen e1 e2)
712 = zonkLExpr env e1 `thenM` \ new_e1 ->
713 zonkLExpr env e2 `thenM` \ new_e2 ->
714 returnM (FromThen new_e1 new_e2)
716 zonkArithSeq env (FromTo e1 e2)
717 = zonkLExpr env e1 `thenM` \ new_e1 ->
718 zonkLExpr env e2 `thenM` \ new_e2 ->
719 returnM (FromTo new_e1 new_e2)
721 zonkArithSeq env (FromThenTo e1 e2 e3)
722 = zonkLExpr env e1 `thenM` \ new_e1 ->
723 zonkLExpr env e2 `thenM` \ new_e2 ->
724 zonkLExpr env e3 `thenM` \ new_e3 ->
725 returnM (FromThenTo new_e1 new_e2 new_e3)
728 -------------------------------------------------------------------------
729 zonkStmts :: ZonkEnv -> [LStmt TcId] -> TcM (ZonkEnv, [LStmt Id])
730 zonkStmts env [] = return (env, [])
731 zonkStmts env (s:ss) = do { (env1, s') <- wrapLocSndM (zonkStmt env) s
732 ; (env2, ss') <- zonkStmts env1 ss
733 ; return (env2, s' : ss') }
735 zonkStmt :: ZonkEnv -> Stmt TcId -> TcM (ZonkEnv, Stmt Id)
736 zonkStmt env (ParStmt stmts_w_bndrs mzip_op bind_op return_op)
737 = mappM zonk_branch stmts_w_bndrs `thenM` \ new_stmts_w_bndrs ->
739 new_binders = concat (map snd new_stmts_w_bndrs)
740 env1 = extendZonkEnv env new_binders
742 zonkExpr env1 mzip_op `thenM` \ new_mzip ->
743 zonkExpr env1 bind_op `thenM` \ new_bind ->
744 zonkExpr env1 return_op `thenM` \ new_return ->
745 return (env1, ParStmt new_stmts_w_bndrs new_mzip new_bind new_return)
747 zonk_branch (stmts, bndrs) = zonkStmts env stmts `thenM` \ (env1, new_stmts) ->
748 returnM (new_stmts, zonkIdOccs env1 bndrs)
750 zonkStmt env (RecStmt { recS_stmts = segStmts, recS_later_ids = lvs, recS_rec_ids = rvs
751 , recS_ret_fn = ret_id, recS_mfix_fn = mfix_id, recS_bind_fn = bind_id
752 , recS_rec_rets = rets, recS_ret_ty = ret_ty })
753 = do { new_rvs <- zonkIdBndrs env rvs
754 ; new_lvs <- zonkIdBndrs env lvs
755 ; new_ret_ty <- zonkTcTypeToType env ret_ty
756 ; new_ret_id <- zonkExpr env ret_id
757 ; new_mfix_id <- zonkExpr env mfix_id
758 ; new_bind_id <- zonkExpr env bind_id
759 ; let env1 = extendZonkEnv env new_rvs
760 ; (env2, new_segStmts) <- zonkStmts env1 segStmts
761 -- Zonk the ret-expressions in an envt that
762 -- has the polymorphic bindings in the envt
763 ; new_rets <- mapM (zonkExpr env2) rets
764 ; return (extendZonkEnv env new_lvs, -- Only the lvs are needed
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_ret_ty = new_ret_ty }) }
770 zonkStmt env (ExprStmt expr then_op guard_op ty)
771 = zonkLExpr env expr `thenM` \ new_expr ->
772 zonkExpr env then_op `thenM` \ new_then ->
773 zonkExpr env guard_op `thenM` \ new_guard ->
774 zonkTcTypeToType env ty `thenM` \ new_ty ->
775 returnM (env, ExprStmt new_expr new_then new_guard new_ty)
777 zonkStmt env (LastStmt expr ret_op)
778 = zonkLExpr env expr `thenM` \ new_expr ->
779 zonkExpr env ret_op `thenM` \ new_ret ->
780 returnM (env, LastStmt new_expr new_ret)
782 zonkStmt env (TransStmt { trS_stmts = stmts, trS_bndrs = binderMap
783 , trS_by = by, trS_form = form, trS_using = using
784 , trS_ret = return_op, trS_bind = bind_op, trS_fmap = liftM_op })
785 = do { (env', stmts') <- zonkStmts env stmts
786 ; binderMap' <- mappM (zonkBinderMapEntry env') binderMap
787 ; by' <- fmapMaybeM (zonkLExpr env') by
788 ; using' <- zonkLExpr env using
789 ; return_op' <- zonkExpr env' return_op
790 ; bind_op' <- zonkExpr env' bind_op
791 ; liftM_op' <- zonkExpr env' liftM_op
792 ; let env'' = extendZonkEnv env' (map snd binderMap')
793 ; return (env'', TransStmt { trS_stmts = stmts', trS_bndrs = binderMap'
794 , trS_by = by', trS_form = form, trS_using = using'
795 , trS_ret = return_op', trS_bind = bind_op', trS_fmap = liftM_op' }) }
797 zonkBinderMapEntry env (oldBinder, newBinder) = do
798 let oldBinder' = zonkIdOcc env oldBinder
799 newBinder' <- zonkIdBndr env newBinder
800 return (oldBinder', newBinder')
802 zonkStmt env (LetStmt binds)
803 = zonkLocalBinds env binds `thenM` \ (env1, new_binds) ->
804 returnM (env1, LetStmt new_binds)
806 zonkStmt env (BindStmt pat expr bind_op fail_op)
807 = do { new_expr <- zonkLExpr env expr
808 ; (env1, new_pat) <- zonkPat env pat
809 ; new_bind <- zonkExpr env bind_op
810 ; new_fail <- zonkExpr env fail_op
811 ; return (env1, BindStmt new_pat new_expr new_bind new_fail) }
813 -------------------------------------------------------------------------
814 zonkRecFields :: ZonkEnv -> HsRecordBinds TcId -> TcM (HsRecordBinds TcId)
815 zonkRecFields env (HsRecFields flds dd)
816 = do { flds' <- mappM zonk_rbind flds
817 ; return (HsRecFields flds' dd) }
820 = do { new_id <- wrapLocM (zonkIdBndr env) (hsRecFieldId fld)
821 ; new_expr <- zonkLExpr env (hsRecFieldArg fld)
822 ; return (fld { hsRecFieldId = new_id, hsRecFieldArg = new_expr }) }
824 -------------------------------------------------------------------------
825 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
826 mapIPNameTc f (IPName n) = f n `thenM` \ r -> returnM (IPName r)
830 %************************************************************************
832 \subsection[BackSubst-Pats]{Patterns}
834 %************************************************************************
837 zonkPat :: ZonkEnv -> OutPat TcId -> TcM (ZonkEnv, OutPat Id)
838 -- Extend the environment as we go, because it's possible for one
839 -- pattern to bind something that is used in another (inside or
841 zonkPat env pat = wrapLocSndM (zonk_pat env) pat
843 zonk_pat :: ZonkEnv -> Pat TcId -> TcM (ZonkEnv, Pat Id)
844 zonk_pat env (ParPat p)
845 = do { (env', p') <- zonkPat env p
846 ; return (env', ParPat p') }
848 zonk_pat env (WildPat ty)
849 = do { ty' <- zonkTcTypeToType env ty
850 ; return (env, WildPat ty') }
852 zonk_pat env (VarPat v)
853 = do { v' <- zonkIdBndr env v
854 ; return (extendZonkEnv1 env v', VarPat v') }
856 zonk_pat env (LazyPat pat)
857 = do { (env', pat') <- zonkPat env pat
858 ; return (env', LazyPat pat') }
860 zonk_pat env (BangPat pat)
861 = do { (env', pat') <- zonkPat env pat
862 ; return (env', BangPat pat') }
864 zonk_pat env (AsPat (L loc v) pat)
865 = do { v' <- zonkIdBndr env v
866 ; (env', pat') <- zonkPat (extendZonkEnv1 env v') pat
867 ; return (env', AsPat (L loc v') pat') }
869 zonk_pat env (ViewPat expr pat ty)
870 = do { expr' <- zonkLExpr env expr
871 ; (env', pat') <- zonkPat env pat
872 ; ty' <- zonkTcTypeToType env ty
873 ; return (env', ViewPat expr' pat' ty') }
875 zonk_pat env (ListPat pats ty)
876 = do { ty' <- zonkTcTypeToType env ty
877 ; (env', pats') <- zonkPats env pats
878 ; return (env', ListPat pats' ty') }
880 zonk_pat env (PArrPat pats ty)
881 = do { ty' <- zonkTcTypeToType env ty
882 ; (env', pats') <- zonkPats env pats
883 ; return (env', PArrPat pats' ty') }
885 zonk_pat env (TuplePat pats boxed ty)
886 = do { ty' <- zonkTcTypeToType env ty
887 ; (env', pats') <- zonkPats env pats
888 ; return (env', TuplePat pats' boxed ty') }
890 zonk_pat env p@(ConPatOut { pat_ty = ty, pat_dicts = evs, pat_binds = binds, pat_args = args })
891 = ASSERT( all isImmutableTyVar (pat_tvs p) )
892 do { new_ty <- zonkTcTypeToType env ty
893 ; (env1, new_evs) <- zonkEvBndrsX env evs
894 ; (env2, new_binds) <- zonkTcEvBinds env1 binds
895 ; (env', new_args) <- zonkConStuff env2 args
896 ; returnM (env', p { pat_ty = new_ty, pat_dicts = new_evs,
897 pat_binds = new_binds, pat_args = new_args }) }
899 zonk_pat env (LitPat lit) = return (env, LitPat lit)
901 zonk_pat env (SigPatOut pat ty)
902 = do { ty' <- zonkTcTypeToType env ty
903 ; (env', pat') <- zonkPat env pat
904 ; return (env', SigPatOut pat' ty') }
906 zonk_pat env (NPat lit mb_neg eq_expr)
907 = do { lit' <- zonkOverLit env lit
908 ; mb_neg' <- fmapMaybeM (zonkExpr env) mb_neg
909 ; eq_expr' <- zonkExpr env eq_expr
910 ; return (env, NPat lit' mb_neg' eq_expr') }
912 zonk_pat env (NPlusKPat (L loc n) lit e1 e2)
913 = do { n' <- zonkIdBndr env n
914 ; lit' <- zonkOverLit env lit
915 ; e1' <- zonkExpr env e1
916 ; e2' <- zonkExpr env e2
917 ; return (extendZonkEnv1 env n', NPlusKPat (L loc n') lit' e1' e2') }
919 zonk_pat env (CoPat co_fn pat ty)
920 = do { (env', co_fn') <- zonkCoFn env co_fn
921 ; (env'', pat') <- zonkPat env' (noLoc pat)
922 ; ty' <- zonkTcTypeToType env'' ty
923 ; return (env'', CoPat co_fn' (unLoc pat') ty') }
925 zonk_pat _ pat = pprPanic "zonk_pat" (ppr pat)
927 ---------------------------
928 zonkConStuff :: ZonkEnv
929 -> HsConDetails (OutPat TcId) (HsRecFields id (OutPat TcId))
931 HsConDetails (OutPat Id) (HsRecFields id (OutPat Id)))
932 zonkConStuff env (PrefixCon pats)
933 = do { (env', pats') <- zonkPats env pats
934 ; return (env', PrefixCon pats') }
936 zonkConStuff env (InfixCon p1 p2)
937 = do { (env1, p1') <- zonkPat env p1
938 ; (env', p2') <- zonkPat env1 p2
939 ; return (env', InfixCon p1' p2') }
941 zonkConStuff env (RecCon (HsRecFields rpats dd))
942 = do { (env', pats') <- zonkPats env (map hsRecFieldArg rpats)
943 ; let rpats' = zipWith (\rp p' -> rp { hsRecFieldArg = p' }) rpats pats'
944 ; returnM (env', RecCon (HsRecFields rpats' dd)) }
945 -- Field selectors have declared types; hence no zonking
947 ---------------------------
948 zonkPats :: ZonkEnv -> [OutPat TcId] -> TcM (ZonkEnv, [OutPat Id])
949 zonkPats env [] = return (env, [])
950 zonkPats env (pat:pats) = do { (env1, pat') <- zonkPat env pat
951 ; (env', pats') <- zonkPats env1 pats
952 ; return (env', pat':pats') }
955 %************************************************************************
957 \subsection[BackSubst-Foreign]{Foreign exports}
959 %************************************************************************
963 zonkForeignExports :: ZonkEnv -> [LForeignDecl TcId] -> TcM [LForeignDecl Id]
964 zonkForeignExports env ls = mappM (wrapLocM (zonkForeignExport env)) ls
966 zonkForeignExport :: ZonkEnv -> ForeignDecl TcId -> TcM (ForeignDecl Id)
967 zonkForeignExport env (ForeignExport i _hs_ty spec) =
968 returnM (ForeignExport (fmap (zonkIdOcc env) i) undefined spec)
969 zonkForeignExport _ for_imp
970 = returnM for_imp -- Foreign imports don't need zonking
974 zonkRules :: ZonkEnv -> [LRuleDecl TcId] -> TcM [LRuleDecl Id]
975 zonkRules env rs = mappM (wrapLocM (zonkRule env)) rs
977 zonkRule :: ZonkEnv -> RuleDecl TcId -> TcM (RuleDecl Id)
978 zonkRule env (HsRule name act (vars{-::[RuleBndr TcId]-}) lhs fv_lhs rhs fv_rhs)
979 = do { (env_rhs, new_bndrs) <- mapAccumLM zonk_bndr env vars
981 ; unbound_tv_set <- newMutVar emptyVarSet
982 ; let env_lhs = setZonkType env_rhs (zonkTypeCollecting unbound_tv_set)
983 -- We need to gather the type variables mentioned on the LHS so we can
984 -- quantify over them. Example:
990 -- {-# RULES "myrule" foo C = 1 #-}
992 -- After type checking the LHS becomes (foo a (C a))
993 -- and we do not want to zap the unbound tyvar 'a' to (), because
994 -- that limits the applicability of the rule. Instead, we
995 -- want to quantify over it!
997 -- It's easiest to find the free tyvars here. Attempts to do so earlier
998 -- are tiresome, because (a) the data type is big and (b) finding the
999 -- free type vars of an expression is necessarily monadic operation.
1000 -- (consider /\a -> f @ b, where b is side-effected to a)
1002 ; new_lhs <- zonkLExpr env_lhs lhs
1003 ; new_rhs <- zonkLExpr env_rhs rhs
1005 ; unbound_tvs <- readMutVar unbound_tv_set
1006 ; let final_bndrs :: [RuleBndr Var]
1007 final_bndrs = map (RuleBndr . noLoc) (varSetElems unbound_tvs) ++ new_bndrs
1009 ; return (HsRule name act final_bndrs new_lhs fv_lhs new_rhs fv_rhs) }
1011 zonk_bndr env (RuleBndr (L loc v))
1012 = do { (env', v') <- zonk_it env v; return (env', RuleBndr (L loc v')) }
1013 zonk_bndr _ (RuleBndrSig {}) = panic "zonk_bndr RuleBndrSig"
1016 | isId v = do { v' <- zonkIdBndr env v; return (extendZonkEnv1 env v', v') }
1017 | otherwise = ASSERT( isImmutableTyVar v) return (env, v)
1021 zonkVects :: ZonkEnv -> [LVectDecl TcId] -> TcM [LVectDecl Id]
1022 zonkVects env = mappM (wrapLocM (zonkVect env))
1024 zonkVect :: ZonkEnv -> VectDecl TcId -> TcM (VectDecl Id)
1025 zonkVect env (HsVect v Nothing)
1026 = do { v' <- wrapLocM (zonkIdBndr env) v
1027 ; return $ HsVect v' Nothing
1029 zonkVect env (HsVect v (Just e))
1030 = do { v' <- wrapLocM (zonkIdBndr env) v
1031 ; e' <- zonkLExpr env e
1032 ; return $ HsVect v' (Just e')
1034 zonkVect env (HsNoVect v)
1035 = do { v' <- wrapLocM (zonkIdBndr env) v
1036 ; return $ HsNoVect v'
1040 %************************************************************************
1042 Constraints and evidence
1044 %************************************************************************
1047 zonkEvTerm :: ZonkEnv -> EvTerm -> TcM EvTerm
1048 zonkEvTerm env (EvId v) = ASSERT2( isId v, ppr v )
1049 return (EvId (zonkIdOcc env v))
1050 zonkEvTerm env (EvCoercion co) = do { co' <- zonkTcCoToCo env co
1051 ; return (EvCoercion co') }
1052 zonkEvTerm env (EvCast v co) = ASSERT( isId v)
1053 do { co' <- zonkTcCoToCo env co
1054 ; return (EvCast (zonkIdOcc env v) co') }
1055 zonkEvTerm env (EvSuperClass d n) = return (EvSuperClass (zonkIdOcc env d) n)
1056 zonkEvTerm env (EvDFunApp df tys tms)
1057 = do { tys' <- zonkTcTypeToTypes env tys
1058 ; let tms' = map (zonkEvVarOcc env) tms
1059 ; return (EvDFunApp (zonkIdOcc env df) tys' tms') }
1061 zonkTcEvBinds :: ZonkEnv -> TcEvBinds -> TcM (ZonkEnv, TcEvBinds)
1062 zonkTcEvBinds env (TcEvBinds var) = do { (env', bs') <- zonkEvBindsVar env var
1063 ; return (env', EvBinds bs') }
1064 zonkTcEvBinds env (EvBinds bs) = do { (env', bs') <- zonkEvBinds env bs
1065 ; return (env', EvBinds bs') }
1067 zonkEvBindsVar :: ZonkEnv -> EvBindsVar -> TcM (ZonkEnv, Bag EvBind)
1068 zonkEvBindsVar env (EvBindsVar ref _) = do { bs <- readMutVar ref
1069 ; zonkEvBinds env (evBindMapBinds bs) }
1071 zonkEvBinds :: ZonkEnv -> Bag EvBind -> TcM (ZonkEnv, Bag EvBind)
1072 zonkEvBinds env binds
1073 = fixM (\ ~( _, new_binds) -> do
1074 { let env1 = extendZonkEnv env (collect_ev_bndrs new_binds)
1075 ; binds' <- mapBagM (zonkEvBind env1) binds
1076 ; return (env1, binds') })
1078 collect_ev_bndrs :: Bag EvBind -> [EvVar]
1079 collect_ev_bndrs = foldrBag add []
1080 add (EvBind var _) vars = var : vars
1082 zonkEvBind :: ZonkEnv -> EvBind -> TcM EvBind
1083 zonkEvBind env (EvBind var term)
1084 = do { var' <- zonkEvBndr env var
1085 ; term' <- zonkEvTerm env term
1086 ; return (EvBind var' term') }
1089 %************************************************************************
1093 %************************************************************************
1096 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
1097 zonkTcTypeToType (ZonkEnv zonk_ty _) ty = zonk_ty ty
1099 zonkTcTypeToTypes :: ZonkEnv -> [TcType] -> TcM [Type]
1100 zonkTcTypeToTypes env tys = mapM (zonkTcTypeToType env) tys
1102 zonkTypeCollecting :: TcRef TyVarSet -> TcType -> TcM Type
1103 -- This variant collects unbound type variables in a mutable variable
1104 zonkTypeCollecting unbound_tv_set
1105 = zonkType (mkZonkTcTyVar zonk_unbound_tyvar)
1107 zonk_unbound_tyvar tv
1108 = do { tv' <- zonkQuantifiedTyVar tv
1109 ; tv_set <- readMutVar unbound_tv_set
1110 ; writeMutVar unbound_tv_set (extendVarSet tv_set tv')
1111 ; return (mkTyVarTy tv') }
1113 zonkTypeZapping :: TcType -> TcM Type
1114 -- This variant is used for everything except the LHS of rules
1115 -- It zaps unbound type variables to (), or some other arbitrary type
1117 = zonkType (mkZonkTcTyVar zonk_unbound_tyvar) ty
1119 -- Zonk a mutable but unbound type variable to an arbitrary type
1120 -- We know it's unbound even though we don't carry an environment,
1121 -- because at the binding site for a type variable we bind the
1122 -- mutable tyvar to a fresh immutable one. So the mutable store
1123 -- plays the role of an environment. If we come across a mutable
1124 -- type variable that isn't so bound, it must be completely free.
1125 zonk_unbound_tyvar tv = do { let ty = anyTypeOfKind (tyVarKind tv)
1126 ; writeMetaTyVar tv ty
1129 zonkTcCoToCo :: ZonkEnv -> Coercion -> TcM Coercion
1133 go (CoVarCo cv) = return (CoVarCo (zonkEvVarOcc env cv))
1134 go (Refl ty) = do { ty' <- zonkTcTypeToType env ty
1135 ; return (Refl ty') }
1136 go (TyConAppCo tc cos) = do { cos' <- mapM go cos; return (mkTyConAppCo tc cos') }
1137 go (AxiomInstCo ax cos) = do { cos' <- mapM go cos; return (AxiomInstCo ax cos') }
1138 go (AppCo co1 co2) = do { co1' <- go co1; co2' <- go co2
1139 ; return (mkAppCo co1' co2') }
1140 go (UnsafeCo t1 t2) = do { t1' <- zonkTcTypeToType env t1
1141 ; t2' <- zonkTcTypeToType env t2
1142 ; return (mkUnsafeCo t1' t2') }
1143 go (SymCo co) = do { co' <- go co; return (mkSymCo co') }
1144 go (NthCo n co) = do { co' <- go co; return (mkNthCo n co') }
1145 go (TransCo co1 co2) = do { co1' <- go co1; co2' <- go co2
1146 ; return (mkTransCo co1' co2') }
1147 go (InstCo co ty) = do { co' <- go co; ty' <- zonkTcTypeToType env ty
1148 ; return (mkInstCo co' ty') }
1149 go (ForAllCo tv co) = ASSERT( isImmutableTyVar tv )
1150 do { co' <- go co; return (mkForAllCo tv co') }