Drop pre-AMP compatibility CPP conditionals
[ghc.git] / compiler / typecheck / TcHsSyn.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The AQUA Project, Glasgow University, 1996-1998
4
5
6 TcHsSyn: Specialisations of the @HsSyn@ syntax for the typechecker
7
8 This module is an extension of @HsSyn@ syntax, for use in the type
9 checker.
10 -}
11
12 {-# LANGUAGE CPP #-}
13
14 module TcHsSyn (
15 mkHsConApp, mkHsDictLet, mkHsApp,
16 hsLitType, hsLPatType, hsPatType,
17 mkHsAppTy, mkSimpleHsAlt,
18 nlHsIntLit,
19 shortCutLit, hsOverLitName,
20 conLikeResTy,
21
22 -- re-exported from TcMonad
23 TcId, TcIdSet,
24
25 zonkTopDecls, zonkTopExpr, zonkTopLExpr,
26 zonkTopBndrs, zonkTyBndrsX,
27 emptyZonkEnv, mkEmptyZonkEnv,
28 zonkTcTypeToType, zonkTcTypeToTypes, zonkTyVarOcc,
29 zonkCoToCo
30 ) where
31
32 #include "HsVersions.h"
33
34 import HsSyn
35 import Id
36 import TcRnMonad
37 import PrelNames
38 import TcType
39 import TcMType
40 import TcEvidence
41 import TysPrim
42 import TysWiredIn
43 import Type
44 import Coercion
45 import ConLike
46 import DataCon
47 import Name
48 import Var
49 import VarSet
50 import VarEnv
51 import DynFlags
52 import Literal
53 import BasicTypes
54 import Maybes
55 import SrcLoc
56 import Bag
57 import Outputable
58 import Util
59 import qualified GHC.LanguageExtensions as LangExt
60
61 import Control.Monad
62 import Data.List ( partition )
63
64 {-
65 ************************************************************************
66 * *
67 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
68 * *
69 ************************************************************************
70
71 Note: If @hsLPatType@ doesn't bear a strong resemblance to @exprType@,
72 then something is wrong.
73 -}
74
75 hsLPatType :: OutPat Id -> Type
76 hsLPatType (L _ pat) = hsPatType pat
77
78 hsPatType :: Pat Id -> Type
79 hsPatType (ParPat pat) = hsLPatType pat
80 hsPatType (WildPat ty) = ty
81 hsPatType (VarPat (L _ var)) = idType var
82 hsPatType (BangPat pat) = hsLPatType pat
83 hsPatType (LazyPat pat) = hsLPatType pat
84 hsPatType (LitPat lit) = hsLitType lit
85 hsPatType (AsPat var _) = idType (unLoc var)
86 hsPatType (ViewPat _ _ ty) = ty
87 hsPatType (ListPat _ ty Nothing) = mkListTy ty
88 hsPatType (ListPat _ _ (Just (ty,_))) = ty
89 hsPatType (PArrPat _ ty) = mkPArrTy ty
90 hsPatType (TuplePat _ bx tys) = mkTupleTy bx tys
91 hsPatType (ConPatOut { pat_con = L _ con, pat_arg_tys = tys })
92 = conLikeResTy con tys
93 hsPatType (SigPatOut _ ty) = ty
94 hsPatType (NPat (L _ lit) _ _) = overLitType lit
95 hsPatType (NPlusKPat id _ _ _) = idType (unLoc id)
96 hsPatType (CoPat _ _ ty) = ty
97 hsPatType p = pprPanic "hsPatType" (ppr p)
98
99
100 hsLitType :: HsLit -> TcType
101 hsLitType (HsChar _ _) = charTy
102 hsLitType (HsCharPrim _ _) = charPrimTy
103 hsLitType (HsString _ _) = stringTy
104 hsLitType (HsStringPrim _ _) = addrPrimTy
105 hsLitType (HsInt _ _) = intTy
106 hsLitType (HsIntPrim _ _) = intPrimTy
107 hsLitType (HsWordPrim _ _) = wordPrimTy
108 hsLitType (HsInt64Prim _ _) = int64PrimTy
109 hsLitType (HsWord64Prim _ _) = word64PrimTy
110 hsLitType (HsInteger _ _ ty) = ty
111 hsLitType (HsRat _ ty) = ty
112 hsLitType (HsFloatPrim _) = floatPrimTy
113 hsLitType (HsDoublePrim _) = doublePrimTy
114
115 -- Overloaded literals. Here mainly because it uses isIntTy etc
116
117 shortCutLit :: DynFlags -> OverLitVal -> TcType -> Maybe (HsExpr TcId)
118 shortCutLit dflags (HsIntegral src i) ty
119 | isIntTy ty && inIntRange dflags i = Just (HsLit (HsInt src i))
120 | isWordTy ty && inWordRange dflags i
121 = Just (mkLit wordDataCon (HsWordPrim src i))
122 | isIntegerTy ty = Just (HsLit (HsInteger src i ty))
123 | otherwise = shortCutLit dflags (HsFractional (integralFractionalLit 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
129
130 shortCutLit _ (HsFractional f) ty
131 | isFloatTy ty = Just (mkLit floatDataCon (HsFloatPrim f))
132 | isDoubleTy ty = Just (mkLit doubleDataCon (HsDoublePrim f))
133 | otherwise = Nothing
134
135 shortCutLit _ (HsIsString src s) ty
136 | isStringTy ty = Just (HsLit (HsString src s))
137 | otherwise = Nothing
138
139 mkLit :: DataCon -> HsLit -> HsExpr Id
140 mkLit con lit = HsApp (nlHsVar (dataConWrapId con)) (nlHsLit lit)
141
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
148
149 {-
150 ************************************************************************
151 * *
152 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
153 * *
154 ************************************************************************
155
156 The rest of the zonking is done *after* typechecking.
157 The main zonking pass runs over the bindings
158
159 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
160 b) convert unbound TcTyVar to Void
161 c) convert each TcId to an Id by zonking its type
162
163 The type variables are converted by binding mutable tyvars to immutable ones
164 and then zonking as normal.
165
166 The Ids are converted by binding them in the normal Tc envt; that
167 way we maintain sharing; eg an Id is zonked at its binding site and they
168 all occurrences of that Id point to the common zonked copy
169
170 It's all pretty boring stuff, because HsSyn is such a large type, and
171 the environment manipulation is tiresome.
172 -}
173
174 type UnboundTyVarZonker = TcTyVar -> TcM Type
175 -- How to zonk an unbound type variable
176 -- Note [Zonking the LHS of a RULE]
177
178 -- | A ZonkEnv carries around several bits.
179 -- The UnboundTyVarZonker just zaps unbouned meta-tyvars to Any (as
180 -- defined in zonkTypeZapping), except on the LHS of rules. See
181 -- Note [Zonking the LHS of a RULE]. The (TyCoVarEnv TyVar) and is just
182 -- an optimisation: when binding a tyvar or covar, we zonk the kind right away
183 -- and add a mapping to the env. This prevents re-zonking the kind at
184 -- every occurrence. But this is *just* an optimisation.
185 -- The final (IdEnv Var) optimises zonking for
186 -- Ids. It is knot-tied. We must be careful never to put coercion variables
187 -- (which are Ids, after all) in the knot-tied env, because coercions can
188 -- appear in types, and we sometimes inspect a zonked type in this module.
189 data ZonkEnv
190 = ZonkEnv
191 UnboundTyVarZonker
192 (TyCoVarEnv TyVar)
193 (IdEnv Var) -- What variables are in scope
194 -- Maps an Id or EvVar to its zonked version; both have the same Name
195 -- Note that all evidence (coercion variables as well as dictionaries)
196 -- are kept in the ZonkEnv
197 -- Only *type* abstraction is done by side effect
198 -- Is only consulted lazily; hence knot-tying
199
200 instance Outputable ZonkEnv where
201 ppr (ZonkEnv _ _ty_env var_env) = vcat (map ppr (varEnvElts var_env))
202
203
204 -- The EvBinds have to already be zonked, but that's usually the case.
205 emptyZonkEnv :: ZonkEnv
206 emptyZonkEnv = mkEmptyZonkEnv zonkTypeZapping
207
208 mkEmptyZonkEnv :: UnboundTyVarZonker -> ZonkEnv
209 mkEmptyZonkEnv zonker = ZonkEnv zonker emptyVarEnv emptyVarEnv
210
211 -- | Extend the knot-tied environment.
212 extendIdZonkEnvRec :: ZonkEnv -> [Var] -> ZonkEnv
213 extendIdZonkEnvRec (ZonkEnv zonk_ty ty_env id_env) ids
214 -- NB: Don't look at the var to decide which env't to put it in. That
215 -- would end up knot-tying all the env'ts.
216 = ZonkEnv zonk_ty ty_env (extendVarEnvList id_env [(id,id) | id <- ids])
217 -- Given coercion variables will actually end up here. That's OK though:
218 -- coercion variables are never looked up in the knot-tied env't, so zonking
219 -- them simply doesn't get optimised. No one gets hurt. An improvement (?)
220 -- would be to do SCC analysis in zonkEvBinds and then only knot-tie the
221 -- recursive groups. But perhaps the time it takes to do the analysis is
222 -- more than the savings.
223
224 extendZonkEnv :: ZonkEnv -> [Var] -> ZonkEnv
225 extendZonkEnv (ZonkEnv zonk_ty tyco_env id_env) vars
226 = ZonkEnv zonk_ty (extendVarEnvList tyco_env [(tv,tv) | tv <- tycovars])
227 (extendVarEnvList id_env [(id,id) | id <- ids])
228 where (tycovars, ids) = partition isTyCoVar vars
229
230 extendIdZonkEnv1 :: ZonkEnv -> Var -> ZonkEnv
231 extendIdZonkEnv1 (ZonkEnv zonk_ty ty_env id_env) id
232 = ZonkEnv zonk_ty ty_env (extendVarEnv id_env id id)
233
234 extendTyZonkEnv1 :: ZonkEnv -> TyVar -> ZonkEnv
235 extendTyZonkEnv1 (ZonkEnv zonk_ty ty_env id_env) ty
236 = ZonkEnv zonk_ty (extendVarEnv ty_env ty ty) id_env
237
238 setZonkType :: ZonkEnv -> UnboundTyVarZonker -> ZonkEnv
239 setZonkType (ZonkEnv _ ty_env id_env) zonk_ty
240 = ZonkEnv zonk_ty ty_env id_env
241
242 zonkEnvIds :: ZonkEnv -> [Id]
243 zonkEnvIds (ZonkEnv _ _ id_env) = varEnvElts id_env
244
245 zonkIdOcc :: ZonkEnv -> TcId -> Id
246 -- Ids defined in this module should be in the envt;
247 -- ignore others. (Actually, data constructors are also
248 -- not LocalVars, even when locally defined, but that is fine.)
249 -- (Also foreign-imported things aren't currently in the ZonkEnv;
250 -- that's ok because they don't need zonking.)
251 --
252 -- Actually, Template Haskell works in 'chunks' of declarations, and
253 -- an earlier chunk won't be in the 'env' that the zonking phase
254 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
255 -- zonked. There's no point in looking it up there (except for error
256 -- checking), and it's not conveniently to hand; hence the simple
257 -- 'orElse' case in the LocalVar branch.
258 --
259 -- Even without template splices, in module Main, the checking of
260 -- 'main' is done as a separate chunk.
261 zonkIdOcc (ZonkEnv _zonk_ty _ty_env id_env) id
262 | isLocalVar id = lookupVarEnv id_env id `orElse`
263 id
264 | otherwise = id
265
266 zonkIdOccs :: ZonkEnv -> [TcId] -> [Id]
267 zonkIdOccs env ids = map (zonkIdOcc env) ids
268
269 -- zonkIdBndr is used *after* typechecking to get the Id's type
270 -- to its final form. The TyVarEnv give
271 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
272 zonkIdBndr env id
273 = do ty' <- zonkTcTypeToType env (idType id)
274 return (setIdType id ty')
275
276 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
277 zonkIdBndrs env ids = mapM (zonkIdBndr env) ids
278
279 zonkTopBndrs :: [TcId] -> TcM [Id]
280 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
281
282 zonkFieldOcc :: ZonkEnv -> FieldOcc TcId -> TcM (FieldOcc Id)
283 zonkFieldOcc env (FieldOcc lbl sel) = fmap (FieldOcc lbl) $ zonkIdBndr env sel
284
285 zonkEvBndrsX :: ZonkEnv -> [EvVar] -> TcM (ZonkEnv, [Var])
286 zonkEvBndrsX = mapAccumLM zonkEvBndrX
287
288 zonkEvBndrX :: ZonkEnv -> EvVar -> TcM (ZonkEnv, EvVar)
289 -- Works for dictionaries and coercions
290 zonkEvBndrX env var
291 = do { var' <- zonkEvBndr env var
292 ; return (extendZonkEnv env [var'], var') }
293
294 zonkEvBndr :: ZonkEnv -> EvVar -> TcM EvVar
295 -- Works for dictionaries and coercions
296 -- Does not extend the ZonkEnv
297 zonkEvBndr env var
298 = do { let var_ty = varType var
299 ; ty <-
300 {-# SCC "zonkEvBndr_zonkTcTypeToType" #-}
301 zonkTcTypeToType env var_ty
302 ; return (setVarType var ty) }
303
304 zonkEvVarOcc :: ZonkEnv -> EvVar -> TcM EvTerm
305 zonkEvVarOcc env v
306 | isCoVar v
307 = EvCoercion <$> zonkCoVarOcc env v
308 | otherwise
309 = return (EvId $ zonkIdOcc env v)
310
311 zonkTyBndrsX :: ZonkEnv -> [TyVar] -> TcM (ZonkEnv, [TyVar])
312 zonkTyBndrsX = mapAccumLM zonkTyBndrX
313
314 zonkTyBndrX :: ZonkEnv -> TyVar -> TcM (ZonkEnv, TyVar)
315 -- This guarantees to return a TyVar (not a TcTyVar)
316 -- then we add it to the envt, so all occurrences are replaced
317 zonkTyBndrX env tv
318 = ASSERT( isImmutableTyVar tv )
319 do { ki <- zonkTcTypeToType env (tyVarKind tv)
320 -- Internal names tidy up better, for iface files.
321 ; let tv' = mkTyVar (tyVarName tv) ki
322 ; return (extendTyZonkEnv1 env tv', tv') }
323
324 zonkTopExpr :: HsExpr TcId -> TcM (HsExpr Id)
325 zonkTopExpr e = zonkExpr emptyZonkEnv e
326
327 zonkTopLExpr :: LHsExpr TcId -> TcM (LHsExpr Id)
328 zonkTopLExpr e = zonkLExpr emptyZonkEnv e
329
330 zonkTopDecls :: Bag EvBind
331 -> LHsBinds TcId
332 -> [LRuleDecl TcId] -> [LVectDecl TcId] -> [LTcSpecPrag] -> [LForeignDecl TcId]
333 -> TcM ([Id],
334 Bag EvBind,
335 LHsBinds Id,
336 [LForeignDecl Id],
337 [LTcSpecPrag],
338 [LRuleDecl Id],
339 [LVectDecl Id])
340 zonkTopDecls ev_binds binds rules vects imp_specs fords
341 = do { (env1, ev_binds') <- zonkEvBinds emptyZonkEnv ev_binds
342 ; (env2, binds') <- zonkRecMonoBinds env1 binds
343 -- Top level is implicitly recursive
344 ; rules' <- zonkRules env2 rules
345 ; vects' <- zonkVects env2 vects
346 ; specs' <- zonkLTcSpecPrags env2 imp_specs
347 ; fords' <- zonkForeignExports env2 fords
348 ; return (zonkEnvIds env2, ev_binds', binds', fords', specs', rules', vects') }
349
350 ---------------------------------------------
351 zonkLocalBinds :: ZonkEnv -> HsLocalBinds TcId -> TcM (ZonkEnv, HsLocalBinds Id)
352 zonkLocalBinds env EmptyLocalBinds
353 = return (env, EmptyLocalBinds)
354
355 zonkLocalBinds _ (HsValBinds (ValBindsIn {}))
356 = panic "zonkLocalBinds" -- Not in typechecker output
357
358 zonkLocalBinds env (HsValBinds (ValBindsOut binds sigs))
359 = do { (env1, new_binds) <- go env binds
360 ; return (env1, HsValBinds (ValBindsOut new_binds sigs)) }
361 where
362 go env []
363 = return (env, [])
364 go env ((r,b):bs)
365 = do { (env1, b') <- zonkRecMonoBinds env b
366 ; (env2, bs') <- go env1 bs
367 ; return (env2, (r,b'):bs') }
368
369 zonkLocalBinds env (HsIPBinds (IPBinds binds dict_binds)) = do
370 new_binds <- mapM (wrapLocM zonk_ip_bind) binds
371 let
372 env1 = extendIdZonkEnvRec env [ n | L _ (IPBind (Right n) _) <- new_binds]
373 (env2, new_dict_binds) <- zonkTcEvBinds env1 dict_binds
374 return (env2, HsIPBinds (IPBinds new_binds new_dict_binds))
375 where
376 zonk_ip_bind (IPBind n e)
377 = do n' <- mapIPNameTc (zonkIdBndr env) n
378 e' <- zonkLExpr env e
379 return (IPBind n' e')
380
381 ---------------------------------------------
382 zonkRecMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (ZonkEnv, LHsBinds Id)
383 zonkRecMonoBinds env binds
384 = fixM (\ ~(_, new_binds) -> do
385 { let env1 = extendIdZonkEnvRec env (collectHsBindsBinders new_binds)
386 ; binds' <- zonkMonoBinds env1 binds
387 ; return (env1, binds') })
388
389 ---------------------------------------------
390 zonkMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (LHsBinds Id)
391 zonkMonoBinds env binds = mapBagM (zonk_lbind env) binds
392
393 zonk_lbind :: ZonkEnv -> LHsBind TcId -> TcM (LHsBind Id)
394 zonk_lbind env = wrapLocM (zonk_bind env)
395
396 zonk_bind :: ZonkEnv -> HsBind TcId -> TcM (HsBind Id)
397 zonk_bind env bind@(PatBind { pat_lhs = pat, pat_rhs = grhss, pat_rhs_ty = ty})
398 = do { (_env, new_pat) <- zonkPat env pat -- Env already extended
399 ; new_grhss <- zonkGRHSs env zonkLExpr grhss
400 ; new_ty <- zonkTcTypeToType env ty
401 ; return (bind { pat_lhs = new_pat, pat_rhs = new_grhss, pat_rhs_ty = new_ty }) }
402
403 zonk_bind env (VarBind { var_id = var, var_rhs = expr, var_inline = inl })
404 = do { new_var <- zonkIdBndr env var
405 ; new_expr <- zonkLExpr env expr
406 ; return (VarBind { var_id = new_var, var_rhs = new_expr, var_inline = inl }) }
407
408 zonk_bind env bind@(FunBind { fun_id = L loc var, fun_matches = ms
409 , fun_co_fn = co_fn })
410 = do { new_var <- zonkIdBndr env var
411 ; (env1, new_co_fn) <- zonkCoFn env co_fn
412 ; new_ms <- zonkMatchGroup env1 zonkLExpr ms
413 ; return (bind { fun_id = L loc new_var, fun_matches = new_ms
414 , fun_co_fn = new_co_fn }) }
415
416 zonk_bind env (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 { (env0, new_tyvars) <- zonkTyBndrsX env tyvars
422 ; (env1, new_evs) <- zonkEvBndrsX env0 evs
423 ; (env2, new_ev_binds) <- zonkTcEvBinds_s env1 ev_binds
424 ; (new_val_bind, new_exports) <- fixM $ \ ~(new_val_binds, _) ->
425 do { let env3 = extendIdZonkEnvRec env2
426 (collectHsBindsBinders new_val_binds)
427 ; new_val_binds <- zonkMonoBinds env3 val_binds
428 ; new_exports <- mapM (zonkExport env3) exports
429 ; return (new_val_binds, new_exports) }
430 ; return (AbsBinds { abs_tvs = new_tyvars, abs_ev_vars = new_evs
431 , abs_ev_binds = new_ev_binds
432 , abs_exports = new_exports, abs_binds = new_val_bind }) }
433 where
434 zonkExport env (ABE{ abe_wrap = wrap, abe_inst_wrap = inst_wrap
435 , abe_poly = poly_id
436 , abe_mono = mono_id, abe_prags = prags })
437 = do new_poly_id <- zonkIdBndr env poly_id
438 (_, new_wrap) <- zonkCoFn env wrap
439 (_, new_inst_wrap) <- zonkCoFn env inst_wrap
440 new_prags <- zonkSpecPrags env prags
441 return (ABE{ abe_wrap = new_wrap, abe_inst_wrap = new_inst_wrap
442 , abe_poly = new_poly_id
443 , abe_mono = zonkIdOcc env mono_id
444 , abe_prags = new_prags })
445
446 zonk_bind env (PatSynBind bind@(PSB { psb_id = L loc id
447 , psb_args = details
448 , psb_def = lpat
449 , psb_dir = dir }))
450 = do { id' <- zonkIdBndr env id
451 ; details' <- zonkPatSynDetails env details
452 ; (env1, lpat') <- zonkPat env lpat
453 ; (_env2, dir') <- zonkPatSynDir env1 dir
454 ; return $ PatSynBind $
455 bind { psb_id = L loc id'
456 , psb_args = details'
457 , psb_def = lpat'
458 , psb_dir = dir' } }
459
460 zonkPatSynDetails :: ZonkEnv
461 -> HsPatSynDetails (Located TcId)
462 -> TcM (HsPatSynDetails (Located Id))
463 zonkPatSynDetails env = traverse (wrapLocM $ zonkIdBndr env)
464
465 zonkPatSynDir :: ZonkEnv -> HsPatSynDir TcId -> TcM (ZonkEnv, HsPatSynDir Id)
466 zonkPatSynDir env Unidirectional = return (env, Unidirectional)
467 zonkPatSynDir env ImplicitBidirectional = return (env, ImplicitBidirectional)
468 zonkPatSynDir env (ExplicitBidirectional mg) = do
469 mg' <- zonkMatchGroup env zonkLExpr mg
470 return (env, ExplicitBidirectional mg')
471
472 zonkSpecPrags :: ZonkEnv -> TcSpecPrags -> TcM TcSpecPrags
473 zonkSpecPrags _ IsDefaultMethod = return IsDefaultMethod
474 zonkSpecPrags env (SpecPrags ps) = do { ps' <- zonkLTcSpecPrags env ps
475 ; return (SpecPrags ps') }
476
477 zonkLTcSpecPrags :: ZonkEnv -> [LTcSpecPrag] -> TcM [LTcSpecPrag]
478 zonkLTcSpecPrags env ps
479 = mapM zonk_prag ps
480 where
481 zonk_prag (L loc (SpecPrag id co_fn inl))
482 = do { (_, co_fn') <- zonkCoFn env co_fn
483 ; return (L loc (SpecPrag (zonkIdOcc env id) co_fn' inl)) }
484
485 {-
486 ************************************************************************
487 * *
488 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
489 * *
490 ************************************************************************
491 -}
492
493 zonkMatchGroup :: ZonkEnv
494 -> (ZonkEnv -> Located (body TcId) -> TcM (Located (body Id)))
495 -> MatchGroup TcId (Located (body TcId)) -> TcM (MatchGroup Id (Located (body Id)))
496 zonkMatchGroup env zBody (MG { mg_alts = L l ms, mg_arg_tys = arg_tys
497 , mg_res_ty = res_ty, mg_origin = origin })
498 = do { ms' <- mapM (zonkMatch env zBody) ms
499 ; arg_tys' <- zonkTcTypeToTypes env arg_tys
500 ; res_ty' <- zonkTcTypeToType env res_ty
501 ; return (MG { mg_alts = L l ms', mg_arg_tys = arg_tys'
502 , mg_res_ty = res_ty', mg_origin = origin }) }
503
504 zonkMatch :: ZonkEnv
505 -> (ZonkEnv -> Located (body TcId) -> TcM (Located (body Id)))
506 -> LMatch TcId (Located (body TcId)) -> TcM (LMatch Id (Located (body Id)))
507 zonkMatch env zBody (L loc (Match mf pats _ grhss))
508 = do { (env1, new_pats) <- zonkPats env pats
509 ; new_grhss <- zonkGRHSs env1 zBody grhss
510 ; return (L loc (Match mf new_pats Nothing new_grhss)) }
511
512 -------------------------------------------------------------------------
513 zonkGRHSs :: ZonkEnv
514 -> (ZonkEnv -> Located (body TcId) -> TcM (Located (body Id)))
515 -> GRHSs TcId (Located (body TcId)) -> TcM (GRHSs Id (Located (body Id)))
516
517 zonkGRHSs env zBody (GRHSs grhss (L l binds)) = do
518 (new_env, new_binds) <- zonkLocalBinds env binds
519 let
520 zonk_grhs (GRHS guarded rhs)
521 = do (env2, new_guarded) <- zonkStmts new_env zonkLExpr guarded
522 new_rhs <- zBody env2 rhs
523 return (GRHS new_guarded new_rhs)
524 new_grhss <- mapM (wrapLocM zonk_grhs) grhss
525 return (GRHSs new_grhss (L l new_binds))
526
527 {-
528 ************************************************************************
529 * *
530 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
531 * *
532 ************************************************************************
533 -}
534
535 zonkLExprs :: ZonkEnv -> [LHsExpr TcId] -> TcM [LHsExpr Id]
536 zonkLExpr :: ZonkEnv -> LHsExpr TcId -> TcM (LHsExpr Id)
537 zonkExpr :: ZonkEnv -> HsExpr TcId -> TcM (HsExpr Id)
538
539 zonkLExprs env exprs = mapM (zonkLExpr env) exprs
540 zonkLExpr env expr = wrapLocM (zonkExpr env) expr
541
542 zonkExpr env (HsVar (L l id))
543 = return (HsVar (L l (zonkIdOcc env id)))
544
545 zonkExpr _ (HsIPVar id)
546 = return (HsIPVar id)
547
548 zonkExpr _ (HsOverLabel l)
549 = return (HsOverLabel l)
550
551 zonkExpr env (HsLit (HsRat f ty))
552 = do new_ty <- zonkTcTypeToType env ty
553 return (HsLit (HsRat f new_ty))
554
555 zonkExpr _ (HsLit lit)
556 = return (HsLit lit)
557
558 zonkExpr env (HsOverLit lit)
559 = do { lit' <- zonkOverLit env lit
560 ; return (HsOverLit lit') }
561
562 zonkExpr env (HsLam matches)
563 = do new_matches <- zonkMatchGroup env zonkLExpr matches
564 return (HsLam new_matches)
565
566 zonkExpr env (HsLamCase arg matches)
567 = do new_arg <- zonkTcTypeToType env arg
568 new_matches <- zonkMatchGroup env zonkLExpr matches
569 return (HsLamCase new_arg new_matches)
570
571 zonkExpr env (HsApp e1 e2)
572 = do new_e1 <- zonkLExpr env e1
573 new_e2 <- zonkLExpr env e2
574 return (HsApp new_e1 new_e2)
575
576 zonkExpr _ e@(HsRnBracketOut _ _)
577 = pprPanic "zonkExpr: HsRnBracketOut" (ppr e)
578
579 zonkExpr env (HsTcBracketOut body bs)
580 = do bs' <- mapM zonk_b bs
581 return (HsTcBracketOut body bs')
582 where
583 zonk_b (PendingTcSplice n e) = do e' <- zonkLExpr env e
584 return (PendingTcSplice n e')
585
586 zonkExpr _ (HsSpliceE s) = WARN( True, ppr s ) -- Should not happen
587 return (HsSpliceE s)
588
589 zonkExpr env (OpApp e1 op fixity e2)
590 = do new_e1 <- zonkLExpr env e1
591 new_op <- zonkLExpr env op
592 new_e2 <- zonkLExpr env e2
593 return (OpApp new_e1 new_op fixity new_e2)
594
595 zonkExpr env (NegApp expr op)
596 = do new_expr <- zonkLExpr env expr
597 new_op <- zonkExpr env op
598 return (NegApp new_expr new_op)
599
600 zonkExpr env (HsPar e)
601 = do new_e <- zonkLExpr env e
602 return (HsPar new_e)
603
604 zonkExpr env (SectionL expr op)
605 = do new_expr <- zonkLExpr env expr
606 new_op <- zonkLExpr env op
607 return (SectionL new_expr new_op)
608
609 zonkExpr env (SectionR op expr)
610 = do new_op <- zonkLExpr env op
611 new_expr <- zonkLExpr env expr
612 return (SectionR new_op new_expr)
613
614 zonkExpr env (ExplicitTuple tup_args boxed)
615 = do { new_tup_args <- mapM zonk_tup_arg tup_args
616 ; return (ExplicitTuple new_tup_args boxed) }
617 where
618 zonk_tup_arg (L l (Present e)) = do { e' <- zonkLExpr env e
619 ; return (L l (Present e')) }
620 zonk_tup_arg (L l (Missing t)) = do { t' <- zonkTcTypeToType env t
621 ; return (L l (Missing t')) }
622
623 zonkExpr env (HsCase expr ms)
624 = do new_expr <- zonkLExpr env expr
625 new_ms <- zonkMatchGroup env zonkLExpr ms
626 return (HsCase new_expr new_ms)
627
628 zonkExpr env (HsIf e0 e1 e2 e3)
629 = do { new_e0 <- fmapMaybeM (zonkExpr env) e0
630 ; new_e1 <- zonkLExpr env e1
631 ; new_e2 <- zonkLExpr env e2
632 ; new_e3 <- zonkLExpr env e3
633 ; return (HsIf new_e0 new_e1 new_e2 new_e3) }
634
635 zonkExpr env (HsMultiIf ty alts)
636 = do { alts' <- mapM (wrapLocM zonk_alt) alts
637 ; ty' <- zonkTcTypeToType env ty
638 ; return $ HsMultiIf ty' alts' }
639 where zonk_alt (GRHS guard expr)
640 = do { (env', guard') <- zonkStmts env zonkLExpr guard
641 ; expr' <- zonkLExpr env' expr
642 ; return $ GRHS guard' expr' }
643
644 zonkExpr env (HsLet (L l binds) expr)
645 = do (new_env, new_binds) <- zonkLocalBinds env binds
646 new_expr <- zonkLExpr new_env expr
647 return (HsLet (L l new_binds) new_expr)
648
649 zonkExpr env (HsDo do_or_lc (L l stmts) ty)
650 = do (_, new_stmts) <- zonkStmts env zonkLExpr stmts
651 new_ty <- zonkTcTypeToType env ty
652 return (HsDo do_or_lc (L l new_stmts) new_ty)
653
654 zonkExpr env (ExplicitList ty wit exprs)
655 = do new_ty <- zonkTcTypeToType env ty
656 new_wit <- zonkWit env wit
657 new_exprs <- zonkLExprs env exprs
658 return (ExplicitList new_ty new_wit new_exprs)
659 where zonkWit _ Nothing = return Nothing
660 zonkWit env (Just fln) = do new_fln <- zonkExpr env fln
661 return (Just new_fln)
662
663 zonkExpr env (ExplicitPArr ty exprs)
664 = do new_ty <- zonkTcTypeToType env ty
665 new_exprs <- zonkLExprs env exprs
666 return (ExplicitPArr new_ty new_exprs)
667
668 zonkExpr env expr@(RecordCon { rcon_con_expr = con_expr, rcon_flds = rbinds })
669 = do { new_con_expr <- zonkExpr env con_expr
670 ; new_rbinds <- zonkRecFields env rbinds
671 ; return (expr { rcon_con_expr = new_con_expr
672 , rcon_flds = new_rbinds }) }
673
674 zonkExpr env (RecordUpd { rupd_expr = expr, rupd_flds = rbinds
675 , rupd_cons = cons, rupd_in_tys = in_tys
676 , rupd_out_tys = out_tys, rupd_wrap = req_wrap })
677 = do { new_expr <- zonkLExpr env expr
678 ; new_in_tys <- mapM (zonkTcTypeToType env) in_tys
679 ; new_out_tys <- mapM (zonkTcTypeToType env) out_tys
680 ; new_rbinds <- zonkRecUpdFields env rbinds
681 ; (_, new_recwrap) <- zonkCoFn env req_wrap
682 ; return (RecordUpd { rupd_expr = new_expr, rupd_flds = new_rbinds
683 , rupd_cons = cons, rupd_in_tys = new_in_tys
684 , rupd_out_tys = new_out_tys, rupd_wrap = new_recwrap }) }
685
686 zonkExpr env (ExprWithTySigOut e ty)
687 = do { e' <- zonkLExpr env e
688 ; return (ExprWithTySigOut e' ty) }
689
690 zonkExpr env (ArithSeq expr wit info)
691 = do new_expr <- zonkExpr env expr
692 new_wit <- zonkWit env wit
693 new_info <- zonkArithSeq env info
694 return (ArithSeq new_expr new_wit new_info)
695 where zonkWit _ Nothing = return Nothing
696 zonkWit env (Just fln) = do new_fln <- zonkExpr env fln
697 return (Just new_fln)
698
699 zonkExpr env (PArrSeq expr info)
700 = do new_expr <- zonkExpr env expr
701 new_info <- zonkArithSeq env info
702 return (PArrSeq new_expr new_info)
703
704 zonkExpr env (HsSCC src lbl expr)
705 = do new_expr <- zonkLExpr env expr
706 return (HsSCC src lbl new_expr)
707
708 zonkExpr env (HsTickPragma src info expr)
709 = do new_expr <- zonkLExpr env expr
710 return (HsTickPragma src info new_expr)
711
712 -- hdaume: core annotations
713 zonkExpr env (HsCoreAnn src lbl expr)
714 = do new_expr <- zonkLExpr env expr
715 return (HsCoreAnn src lbl new_expr)
716
717 -- arrow notation extensions
718 zonkExpr env (HsProc pat body)
719 = do { (env1, new_pat) <- zonkPat env pat
720 ; new_body <- zonkCmdTop env1 body
721 ; return (HsProc new_pat new_body) }
722
723 -- StaticPointers extension
724 zonkExpr env (HsStatic expr)
725 = HsStatic <$> zonkLExpr env expr
726
727 zonkExpr env (HsWrap co_fn expr)
728 = do (env1, new_co_fn) <- zonkCoFn env co_fn
729 new_expr <- zonkExpr env1 expr
730 return (HsWrap new_co_fn new_expr)
731
732 zonkExpr _ (HsUnboundVar v)
733 = return (HsUnboundVar v)
734
735 -- nothing to do here. The payload is an LHsType, not a Type.
736 zonkExpr _ e@(HsTypeOut {}) = return e
737
738 zonkExpr _ expr = pprPanic "zonkExpr" (ppr expr)
739
740 -------------------------------------------------------------------------
741
742 zonkLCmd :: ZonkEnv -> LHsCmd TcId -> TcM (LHsCmd Id)
743 zonkCmd :: ZonkEnv -> HsCmd TcId -> TcM (HsCmd Id)
744
745 zonkLCmd env cmd = wrapLocM (zonkCmd env) cmd
746
747 zonkCmd env (HsCmdWrap w cmd)
748 = do { (env1, w') <- zonkCoFn env w
749 ; cmd' <- zonkCmd env1 cmd
750 ; return (HsCmdWrap w' cmd') }
751 zonkCmd env (HsCmdArrApp e1 e2 ty ho rl)
752 = do new_e1 <- zonkLExpr env e1
753 new_e2 <- zonkLExpr env e2
754 new_ty <- zonkTcTypeToType env ty
755 return (HsCmdArrApp new_e1 new_e2 new_ty ho rl)
756
757 zonkCmd env (HsCmdArrForm op fixity args)
758 = do new_op <- zonkLExpr env op
759 new_args <- mapM (zonkCmdTop env) args
760 return (HsCmdArrForm new_op fixity new_args)
761
762 zonkCmd env (HsCmdApp c e)
763 = do new_c <- zonkLCmd env c
764 new_e <- zonkLExpr env e
765 return (HsCmdApp new_c new_e)
766
767 zonkCmd env (HsCmdLam matches)
768 = do new_matches <- zonkMatchGroup env zonkLCmd matches
769 return (HsCmdLam new_matches)
770
771 zonkCmd env (HsCmdPar c)
772 = do new_c <- zonkLCmd env c
773 return (HsCmdPar new_c)
774
775 zonkCmd env (HsCmdCase expr ms)
776 = do new_expr <- zonkLExpr env expr
777 new_ms <- zonkMatchGroup env zonkLCmd ms
778 return (HsCmdCase new_expr new_ms)
779
780 zonkCmd env (HsCmdIf eCond ePred cThen cElse)
781 = do { new_eCond <- fmapMaybeM (zonkExpr env) eCond
782 ; new_ePred <- zonkLExpr env ePred
783 ; new_cThen <- zonkLCmd env cThen
784 ; new_cElse <- zonkLCmd env cElse
785 ; return (HsCmdIf new_eCond new_ePred new_cThen new_cElse) }
786
787 zonkCmd env (HsCmdLet (L l binds) cmd)
788 = do (new_env, new_binds) <- zonkLocalBinds env binds
789 new_cmd <- zonkLCmd new_env cmd
790 return (HsCmdLet (L l new_binds) new_cmd)
791
792 zonkCmd env (HsCmdDo (L l stmts) ty)
793 = do (_, new_stmts) <- zonkStmts env zonkLCmd stmts
794 new_ty <- zonkTcTypeToType env ty
795 return (HsCmdDo (L l new_stmts) new_ty)
796
797
798
799
800
801 zonkCmdTop :: ZonkEnv -> LHsCmdTop TcId -> TcM (LHsCmdTop Id)
802 zonkCmdTop env cmd = wrapLocM (zonk_cmd_top env) cmd
803
804 zonk_cmd_top :: ZonkEnv -> HsCmdTop TcId -> TcM (HsCmdTop Id)
805 zonk_cmd_top env (HsCmdTop cmd stack_tys ty ids)
806 = do new_cmd <- zonkLCmd env cmd
807 new_stack_tys <- zonkTcTypeToType env stack_tys
808 new_ty <- zonkTcTypeToType env ty
809 new_ids <- mapSndM (zonkExpr env) ids
810 return (HsCmdTop new_cmd new_stack_tys new_ty new_ids)
811
812 -------------------------------------------------------------------------
813 zonkCoFn :: ZonkEnv -> HsWrapper -> TcM (ZonkEnv, HsWrapper)
814 zonkCoFn env WpHole = return (env, WpHole)
815 zonkCoFn env (WpCompose c1 c2) = do { (env1, c1') <- zonkCoFn env c1
816 ; (env2, c2') <- zonkCoFn env1 c2
817 ; return (env2, WpCompose c1' c2') }
818 zonkCoFn env (WpFun c1 c2 t1) = do { (env1, c1') <- zonkCoFn env c1
819 ; (env2, c2') <- zonkCoFn env1 c2
820 ; t1' <- zonkTcTypeToType env2 t1
821 ; return (env2, WpFun c1' c2' t1') }
822 zonkCoFn env (WpCast co) = do { co' <- zonkCoToCo env co
823 ; return (env, WpCast co') }
824 zonkCoFn env (WpEvLam ev) = do { (env', ev') <- zonkEvBndrX env ev
825 ; return (env', WpEvLam ev') }
826 zonkCoFn env (WpEvApp arg) = do { arg' <- zonkEvTerm env arg
827 ; return (env, WpEvApp arg') }
828 zonkCoFn env (WpTyLam tv) = ASSERT( isImmutableTyVar tv )
829 do { (env', tv') <- zonkTyBndrX env tv
830 ; return (env', WpTyLam tv') }
831 zonkCoFn env (WpTyApp ty) = do { ty' <- zonkTcTypeToType env ty
832 ; return (env, WpTyApp ty') }
833 zonkCoFn env (WpLet bs) = do { (env1, bs') <- zonkTcEvBinds env bs
834 ; return (env1, WpLet bs') }
835
836 -------------------------------------------------------------------------
837 zonkOverLit :: ZonkEnv -> HsOverLit TcId -> TcM (HsOverLit Id)
838 zonkOverLit env lit@(OverLit { ol_witness = e, ol_type = ty })
839 = do { ty' <- zonkTcTypeToType env ty
840 ; e' <- zonkExpr env e
841 ; return (lit { ol_witness = e', ol_type = ty' }) }
842
843 -------------------------------------------------------------------------
844 zonkArithSeq :: ZonkEnv -> ArithSeqInfo TcId -> TcM (ArithSeqInfo Id)
845
846 zonkArithSeq env (From e)
847 = do new_e <- zonkLExpr env e
848 return (From new_e)
849
850 zonkArithSeq env (FromThen e1 e2)
851 = do new_e1 <- zonkLExpr env e1
852 new_e2 <- zonkLExpr env e2
853 return (FromThen new_e1 new_e2)
854
855 zonkArithSeq env (FromTo e1 e2)
856 = do new_e1 <- zonkLExpr env e1
857 new_e2 <- zonkLExpr env e2
858 return (FromTo new_e1 new_e2)
859
860 zonkArithSeq env (FromThenTo e1 e2 e3)
861 = do new_e1 <- zonkLExpr env e1
862 new_e2 <- zonkLExpr env e2
863 new_e3 <- zonkLExpr env e3
864 return (FromThenTo new_e1 new_e2 new_e3)
865
866
867 -------------------------------------------------------------------------
868 zonkStmts :: ZonkEnv
869 -> (ZonkEnv -> Located (body TcId) -> TcM (Located (body Id)))
870 -> [LStmt TcId (Located (body TcId))] -> TcM (ZonkEnv, [LStmt Id (Located (body Id))])
871 zonkStmts env _ [] = return (env, [])
872 zonkStmts env zBody (s:ss) = do { (env1, s') <- wrapLocSndM (zonkStmt env zBody) s
873 ; (env2, ss') <- zonkStmts env1 zBody ss
874 ; return (env2, s' : ss') }
875
876 zonkStmt :: ZonkEnv
877 -> (ZonkEnv -> Located (body TcId) -> TcM (Located (body Id)))
878 -> Stmt TcId (Located (body TcId)) -> TcM (ZonkEnv, Stmt Id (Located (body Id)))
879 zonkStmt env _ (ParStmt stmts_w_bndrs mzip_op bind_op)
880 = do { new_stmts_w_bndrs <- mapM zonk_branch stmts_w_bndrs
881 ; let new_binders = [b | ParStmtBlock _ bs _ <- new_stmts_w_bndrs, b <- bs]
882 env1 = extendIdZonkEnvRec env new_binders
883 ; new_mzip <- zonkExpr env1 mzip_op
884 ; new_bind <- zonkExpr env1 bind_op
885 ; return (env1, ParStmt new_stmts_w_bndrs new_mzip new_bind) }
886 where
887 zonk_branch (ParStmtBlock stmts bndrs return_op)
888 = do { (env1, new_stmts) <- zonkStmts env zonkLExpr stmts
889 ; new_return <- zonkExpr env1 return_op
890 ; return (ParStmtBlock new_stmts (zonkIdOccs env1 bndrs) new_return) }
891
892 zonkStmt env zBody (RecStmt { recS_stmts = segStmts, recS_later_ids = lvs, recS_rec_ids = rvs
893 , recS_ret_fn = ret_id, recS_mfix_fn = mfix_id, recS_bind_fn = bind_id
894 , recS_later_rets = later_rets, recS_rec_rets = rec_rets
895 , recS_ret_ty = ret_ty })
896 = do { new_rvs <- zonkIdBndrs env rvs
897 ; new_lvs <- zonkIdBndrs env lvs
898 ; new_ret_ty <- zonkTcTypeToType env ret_ty
899 ; new_ret_id <- zonkExpr env ret_id
900 ; new_mfix_id <- zonkExpr env mfix_id
901 ; new_bind_id <- zonkExpr env bind_id
902 ; let env1 = extendIdZonkEnvRec env new_rvs
903 ; (env2, new_segStmts) <- zonkStmts env1 zBody segStmts
904 -- Zonk the ret-expressions in an envt that
905 -- has the polymorphic bindings in the envt
906 ; new_later_rets <- mapM (zonkExpr env2) later_rets
907 ; new_rec_rets <- mapM (zonkExpr env2) rec_rets
908 ; return (extendIdZonkEnvRec env new_lvs, -- Only the lvs are needed
909 RecStmt { recS_stmts = new_segStmts, recS_later_ids = new_lvs
910 , recS_rec_ids = new_rvs, recS_ret_fn = new_ret_id
911 , recS_mfix_fn = new_mfix_id, recS_bind_fn = new_bind_id
912 , recS_later_rets = new_later_rets
913 , recS_rec_rets = new_rec_rets, recS_ret_ty = new_ret_ty }) }
914
915 zonkStmt env zBody (BodyStmt body then_op guard_op ty)
916 = do new_body <- zBody env body
917 new_then <- zonkExpr env then_op
918 new_guard <- zonkExpr env guard_op
919 new_ty <- zonkTcTypeToType env ty
920 return (env, BodyStmt new_body new_then new_guard new_ty)
921
922 zonkStmt env zBody (LastStmt body noret ret_op)
923 = do new_body <- zBody env body
924 new_ret <- zonkExpr env ret_op
925 return (env, LastStmt new_body noret new_ret)
926
927 zonkStmt env _ (TransStmt { trS_stmts = stmts, trS_bndrs = binderMap
928 , trS_by = by, trS_form = form, trS_using = using
929 , trS_ret = return_op, trS_bind = bind_op, trS_fmap = liftM_op })
930 = do { (env', stmts') <- zonkStmts env zonkLExpr stmts
931 ; binderMap' <- mapM (zonkBinderMapEntry env') binderMap
932 ; by' <- fmapMaybeM (zonkLExpr env') by
933 ; using' <- zonkLExpr env using
934 ; return_op' <- zonkExpr env' return_op
935 ; bind_op' <- zonkExpr env' bind_op
936 ; liftM_op' <- zonkExpr env' liftM_op
937 ; let env'' = extendIdZonkEnvRec env' (map snd binderMap')
938 ; return (env'', TransStmt { trS_stmts = stmts', trS_bndrs = binderMap'
939 , trS_by = by', trS_form = form, trS_using = using'
940 , trS_ret = return_op', trS_bind = bind_op', trS_fmap = liftM_op' }) }
941 where
942 zonkBinderMapEntry env (oldBinder, newBinder) = do
943 let oldBinder' = zonkIdOcc env oldBinder
944 newBinder' <- zonkIdBndr env newBinder
945 return (oldBinder', newBinder')
946
947 zonkStmt env _ (LetStmt (L l binds))
948 = do (env1, new_binds) <- zonkLocalBinds env binds
949 return (env1, LetStmt (L l new_binds))
950
951 zonkStmt env zBody (BindStmt pat body bind_op fail_op)
952 = do { new_body <- zBody env body
953 ; (env1, new_pat) <- zonkPat env pat
954 ; new_bind <- zonkExpr env bind_op
955 ; new_fail <- zonkExpr env fail_op
956 ; return (env1, BindStmt new_pat new_body new_bind new_fail) }
957
958 zonkStmt env _zBody (ApplicativeStmt args mb_join body_ty)
959 = do { (env', args') <- zonk_args env args
960 ; new_mb_join <- traverse (zonkExpr env) mb_join
961 ; new_body_ty <- zonkTcTypeToType env' body_ty
962 ; return (env', ApplicativeStmt args' new_mb_join new_body_ty) }
963 where
964 zonk_args env [] = return (env, [])
965 zonk_args env ((op, arg) : groups)
966 = do { (env1, arg') <- zonk_arg env arg
967 ; op' <- zonkExpr env1 op
968 ; (env2, ss) <- zonk_args env1 groups
969 ; return (env2, (op', arg') : ss) }
970
971 zonk_arg env (ApplicativeArgOne pat expr)
972 = do { (env1, new_pat) <- zonkPat env pat
973 ; new_expr <- zonkLExpr env expr
974 ; return (env1, ApplicativeArgOne new_pat new_expr) }
975 zonk_arg env (ApplicativeArgMany stmts ret pat)
976 = do { (env1, new_stmts) <- zonkStmts env zonkLExpr stmts
977 ; new_ret <- zonkExpr env1 ret
978 ; (env2, new_pat) <- zonkPat env pat
979 ; return (env2, ApplicativeArgMany new_stmts new_ret new_pat) }
980
981 -------------------------------------------------------------------------
982 zonkRecFields :: ZonkEnv -> HsRecordBinds TcId -> TcM (HsRecordBinds TcId)
983 zonkRecFields env (HsRecFields flds dd)
984 = do { flds' <- mapM zonk_rbind flds
985 ; return (HsRecFields flds' dd) }
986 where
987 zonk_rbind (L l fld)
988 = do { new_id <- wrapLocM (zonkFieldOcc env) (hsRecFieldLbl fld)
989 ; new_expr <- zonkLExpr env (hsRecFieldArg fld)
990 ; return (L l (fld { hsRecFieldLbl = new_id
991 , hsRecFieldArg = new_expr })) }
992
993 zonkRecUpdFields :: ZonkEnv -> [LHsRecUpdField TcId] -> TcM [LHsRecUpdField TcId]
994 zonkRecUpdFields env = mapM zonk_rbind
995 where
996 zonk_rbind (L l fld)
997 = do { new_id <- wrapLocM (zonkFieldOcc env) (hsRecUpdFieldOcc fld)
998 ; new_expr <- zonkLExpr env (hsRecFieldArg fld)
999 ; return (L l (fld { hsRecFieldLbl = fmap ambiguousFieldOcc new_id
1000 , hsRecFieldArg = new_expr })) }
1001
1002 -------------------------------------------------------------------------
1003 mapIPNameTc :: (a -> TcM b) -> Either (Located HsIPName) a
1004 -> TcM (Either (Located HsIPName) b)
1005 mapIPNameTc _ (Left x) = return (Left x)
1006 mapIPNameTc f (Right x) = do r <- f x
1007 return (Right r)
1008
1009 {-
1010 ************************************************************************
1011 * *
1012 \subsection[BackSubst-Pats]{Patterns}
1013 * *
1014 ************************************************************************
1015 -}
1016
1017 zonkPat :: ZonkEnv -> OutPat TcId -> TcM (ZonkEnv, OutPat Id)
1018 -- Extend the environment as we go, because it's possible for one
1019 -- pattern to bind something that is used in another (inside or
1020 -- to the right)
1021 zonkPat env pat = wrapLocSndM (zonk_pat env) pat
1022
1023 zonk_pat :: ZonkEnv -> Pat TcId -> TcM (ZonkEnv, Pat Id)
1024 zonk_pat env (ParPat p)
1025 = do { (env', p') <- zonkPat env p
1026 ; return (env', ParPat p') }
1027
1028 zonk_pat env (WildPat ty)
1029 = do { ty' <- zonkTcTypeToType env ty
1030 ; return (env, WildPat ty') }
1031
1032 zonk_pat env (VarPat (L l v))
1033 = do { v' <- zonkIdBndr env v
1034 ; return (extendIdZonkEnv1 env v', VarPat (L l v')) }
1035
1036 zonk_pat env (LazyPat pat)
1037 = do { (env', pat') <- zonkPat env pat
1038 ; return (env', LazyPat pat') }
1039
1040 zonk_pat env (BangPat pat)
1041 = do { (env', pat') <- zonkPat env pat
1042 ; return (env', BangPat pat') }
1043
1044 zonk_pat env (AsPat (L loc v) pat)
1045 = do { v' <- zonkIdBndr env v
1046 ; (env', pat') <- zonkPat (extendIdZonkEnv1 env v') pat
1047 ; return (env', AsPat (L loc v') pat') }
1048
1049 zonk_pat env (ViewPat expr pat ty)
1050 = do { expr' <- zonkLExpr env expr
1051 ; (env', pat') <- zonkPat env pat
1052 ; ty' <- zonkTcTypeToType env ty
1053 ; return (env', ViewPat expr' pat' ty') }
1054
1055 zonk_pat env (ListPat pats ty Nothing)
1056 = do { ty' <- zonkTcTypeToType env ty
1057 ; (env', pats') <- zonkPats env pats
1058 ; return (env', ListPat pats' ty' Nothing) }
1059
1060 zonk_pat env (ListPat pats ty (Just (ty2,wit)))
1061 = do { wit' <- zonkExpr env wit
1062 ; ty2' <- zonkTcTypeToType env ty2
1063 ; ty' <- zonkTcTypeToType env ty
1064 ; (env', pats') <- zonkPats env pats
1065 ; return (env', ListPat pats' ty' (Just (ty2',wit'))) }
1066
1067 zonk_pat env (PArrPat pats ty)
1068 = do { ty' <- zonkTcTypeToType env ty
1069 ; (env', pats') <- zonkPats env pats
1070 ; return (env', PArrPat pats' ty') }
1071
1072 zonk_pat env (TuplePat pats boxed tys)
1073 = do { tys' <- mapM (zonkTcTypeToType env) tys
1074 ; (env', pats') <- zonkPats env pats
1075 ; return (env', TuplePat pats' boxed tys') }
1076
1077 zonk_pat env p@(ConPatOut { pat_arg_tys = tys, pat_tvs = tyvars
1078 , pat_dicts = evs, pat_binds = binds
1079 , pat_args = args, pat_wrap = wrapper })
1080 = ASSERT( all isImmutableTyVar tyvars )
1081 do { new_tys <- mapM (zonkTcTypeToType env) tys
1082 ; (env0, new_tyvars) <- zonkTyBndrsX env tyvars
1083 -- Must zonk the existential variables, because their
1084 -- /kind/ need potential zonking.
1085 -- cf typecheck/should_compile/tc221.hs
1086 ; (env1, new_evs) <- zonkEvBndrsX env0 evs
1087 ; (env2, new_binds) <- zonkTcEvBinds env1 binds
1088 ; (env3, new_wrapper) <- zonkCoFn env2 wrapper
1089 ; (env', new_args) <- zonkConStuff env3 args
1090 ; return (env', p { pat_arg_tys = new_tys,
1091 pat_tvs = new_tyvars,
1092 pat_dicts = new_evs,
1093 pat_binds = new_binds,
1094 pat_args = new_args,
1095 pat_wrap = new_wrapper}) }
1096
1097 zonk_pat env (LitPat lit) = return (env, LitPat lit)
1098
1099 zonk_pat env (SigPatOut pat ty)
1100 = do { ty' <- zonkTcTypeToType env ty
1101 ; (env', pat') <- zonkPat env pat
1102 ; return (env', SigPatOut pat' ty') }
1103
1104 zonk_pat env (NPat (L l lit) mb_neg eq_expr)
1105 = do { lit' <- zonkOverLit env lit
1106 ; mb_neg' <- fmapMaybeM (zonkExpr env) mb_neg
1107 ; eq_expr' <- zonkExpr env eq_expr
1108 ; return (env, NPat (L l lit') mb_neg' eq_expr') }
1109
1110 zonk_pat env (NPlusKPat (L loc n) (L l lit) e1 e2)
1111 = do { n' <- zonkIdBndr env n
1112 ; lit' <- zonkOverLit env lit
1113 ; e1' <- zonkExpr env e1
1114 ; e2' <- zonkExpr env e2
1115 ; return (extendIdZonkEnv1 env n',
1116 NPlusKPat (L loc n') (L l lit') e1' e2') }
1117
1118 zonk_pat env (CoPat co_fn pat ty)
1119 = do { (env', co_fn') <- zonkCoFn env co_fn
1120 ; (env'', pat') <- zonkPat env' (noLoc pat)
1121 ; ty' <- zonkTcTypeToType env'' ty
1122 ; return (env'', CoPat co_fn' (unLoc pat') ty') }
1123
1124 zonk_pat _ pat = pprPanic "zonk_pat" (ppr pat)
1125
1126 ---------------------------
1127 zonkConStuff :: ZonkEnv
1128 -> HsConDetails (OutPat TcId) (HsRecFields id (OutPat TcId))
1129 -> TcM (ZonkEnv,
1130 HsConDetails (OutPat Id) (HsRecFields id (OutPat Id)))
1131 zonkConStuff env (PrefixCon pats)
1132 = do { (env', pats') <- zonkPats env pats
1133 ; return (env', PrefixCon pats') }
1134
1135 zonkConStuff env (InfixCon p1 p2)
1136 = do { (env1, p1') <- zonkPat env p1
1137 ; (env', p2') <- zonkPat env1 p2
1138 ; return (env', InfixCon p1' p2') }
1139
1140 zonkConStuff env (RecCon (HsRecFields rpats dd))
1141 = do { (env', pats') <- zonkPats env (map (hsRecFieldArg . unLoc) rpats)
1142 ; let rpats' = zipWith (\(L l rp) p' -> L l (rp { hsRecFieldArg = p' }))
1143 rpats pats'
1144 ; return (env', RecCon (HsRecFields rpats' dd)) }
1145 -- Field selectors have declared types; hence no zonking
1146
1147 ---------------------------
1148 zonkPats :: ZonkEnv -> [OutPat TcId] -> TcM (ZonkEnv, [OutPat Id])
1149 zonkPats env [] = return (env, [])
1150 zonkPats env (pat:pats) = do { (env1, pat') <- zonkPat env pat
1151 ; (env', pats') <- zonkPats env1 pats
1152 ; return (env', pat':pats') }
1153
1154 {-
1155 ************************************************************************
1156 * *
1157 \subsection[BackSubst-Foreign]{Foreign exports}
1158 * *
1159 ************************************************************************
1160 -}
1161
1162 zonkForeignExports :: ZonkEnv -> [LForeignDecl TcId] -> TcM [LForeignDecl Id]
1163 zonkForeignExports env ls = mapM (wrapLocM (zonkForeignExport env)) ls
1164
1165 zonkForeignExport :: ZonkEnv -> ForeignDecl TcId -> TcM (ForeignDecl Id)
1166 zonkForeignExport env (ForeignExport { fd_name = i, fd_co = co, fd_fe = spec })
1167 = return (ForeignExport { fd_name = fmap (zonkIdOcc env) i
1168 , fd_sig_ty = undefined, fd_co = co
1169 , fd_fe = spec })
1170 zonkForeignExport _ for_imp
1171 = return for_imp -- Foreign imports don't need zonking
1172
1173 zonkRules :: ZonkEnv -> [LRuleDecl TcId] -> TcM [LRuleDecl Id]
1174 zonkRules env rs = mapM (wrapLocM (zonkRule env)) rs
1175
1176 zonkRule :: ZonkEnv -> RuleDecl TcId -> TcM (RuleDecl Id)
1177 zonkRule env (HsRule name act (vars{-::[RuleBndr TcId]-}) lhs fv_lhs rhs fv_rhs)
1178 = do { unbound_tkv_set <- newMutVar emptyVarSet
1179 ; let kind_var_set = identify_kind_vars vars
1180 env_rule = setZonkType env (zonkTvCollecting kind_var_set unbound_tkv_set)
1181 -- See Note [Zonking the LHS of a RULE]
1182
1183 ; (env_inside, new_bndrs) <- mapAccumLM zonk_bndr env_rule vars
1184
1185 ; new_lhs <- zonkLExpr env_inside lhs
1186 ; new_rhs <- zonkLExpr env_inside rhs
1187
1188 ; unbound_tkvs <- readMutVar unbound_tkv_set
1189
1190 ; let final_bndrs :: [LRuleBndr Var]
1191 final_bndrs = map (noLoc . RuleBndr . noLoc)
1192 (varSetElemsWellScoped unbound_tkvs)
1193 ++ new_bndrs
1194
1195 ; return $
1196 HsRule name act final_bndrs new_lhs fv_lhs new_rhs fv_rhs }
1197 where
1198 zonk_bndr env (L l (RuleBndr (L loc v)))
1199 = do { (env', v') <- zonk_it env v
1200 ; return (env', L l (RuleBndr (L loc v'))) }
1201 zonk_bndr _ (L _ (RuleBndrSig {})) = panic "zonk_bndr RuleBndrSig"
1202
1203 zonk_it env v
1204 | isId v = do { v' <- zonkIdBndr env v
1205 ; return (extendIdZonkEnvRec env [v'], v') }
1206 | otherwise = ASSERT( isImmutableTyVar v)
1207 zonkTyBndrX env v
1208 -- DV: used to be return (env,v) but that is plain
1209 -- wrong because we may need to go inside the kind
1210 -- of v and zonk there!
1211
1212 -- returns the set of type variables mentioned in the kind of another
1213 -- type. This is used only when -XPolyKinds is not set.
1214 identify_kind_vars :: [LRuleBndr TcId] -> TyVarSet
1215 identify_kind_vars rule_bndrs
1216 = let vars = map strip_rulebndr rule_bndrs in
1217 unionVarSets (map (\v -> if isTyVar v
1218 then tyCoVarsOfType (tyVarKind v)
1219 else emptyVarSet) vars)
1220
1221 strip_rulebndr (L _ (RuleBndr (L _ v))) = v
1222 strip_rulebndr (L _ (RuleBndrSig {})) = panic "strip_rulebndr zonkRule"
1223
1224 zonkVects :: ZonkEnv -> [LVectDecl TcId] -> TcM [LVectDecl Id]
1225 zonkVects env = mapM (wrapLocM (zonkVect env))
1226
1227 zonkVect :: ZonkEnv -> VectDecl TcId -> TcM (VectDecl Id)
1228 zonkVect env (HsVect s v e)
1229 = do { v' <- wrapLocM (zonkIdBndr env) v
1230 ; e' <- zonkLExpr env e
1231 ; return $ HsVect s v' e'
1232 }
1233 zonkVect env (HsNoVect s v)
1234 = do { v' <- wrapLocM (zonkIdBndr env) v
1235 ; return $ HsNoVect s v'
1236 }
1237 zonkVect _env (HsVectTypeOut s t rt)
1238 = return $ HsVectTypeOut s t rt
1239 zonkVect _ (HsVectTypeIn _ _ _ _) = panic "TcHsSyn.zonkVect: HsVectTypeIn"
1240 zonkVect _env (HsVectClassOut c)
1241 = return $ HsVectClassOut c
1242 zonkVect _ (HsVectClassIn _ _) = panic "TcHsSyn.zonkVect: HsVectClassIn"
1243 zonkVect _env (HsVectInstOut i)
1244 = return $ HsVectInstOut i
1245 zonkVect _ (HsVectInstIn _) = panic "TcHsSyn.zonkVect: HsVectInstIn"
1246
1247 {-
1248 ************************************************************************
1249 * *
1250 Constraints and evidence
1251 * *
1252 ************************************************************************
1253 -}
1254
1255 zonkEvTerm :: ZonkEnv -> EvTerm -> TcM EvTerm
1256 zonkEvTerm env (EvId v) = ASSERT2( isId v, ppr v )
1257 zonkEvVarOcc env v
1258 zonkEvTerm env (EvCoercion co) = do { co' <- zonkCoToCo env co
1259 ; return (EvCoercion co') }
1260 zonkEvTerm env (EvCast tm co) = do { tm' <- zonkEvTerm env tm
1261 ; co' <- zonkCoToCo env co
1262 ; return (mkEvCast tm' co') }
1263 zonkEvTerm _ (EvLit l) = return (EvLit l)
1264
1265 zonkEvTerm env (EvTypeable ty ev) =
1266 do { ev' <- zonkEvTypeable env ev
1267 ; ty' <- zonkTcTypeToType env ty
1268 ; return (EvTypeable ty' ev') }
1269 zonkEvTerm env (EvCallStack cs)
1270 = case cs of
1271 EvCsEmpty -> return (EvCallStack cs)
1272 EvCsPushCall n l tm -> do { tm' <- zonkEvTerm env tm
1273 ; return (EvCallStack (EvCsPushCall n l tm')) }
1274
1275 zonkEvTerm env (EvSuperClass d n) = do { d' <- zonkEvTerm env d
1276 ; return (EvSuperClass d' n) }
1277 zonkEvTerm env (EvDFunApp df tys tms)
1278 = do { tys' <- zonkTcTypeToTypes env tys
1279 ; tms' <- mapM (zonkEvTerm env) tms
1280 ; return (EvDFunApp (zonkIdOcc env df) tys' tms') }
1281 zonkEvTerm env (EvDelayedError ty msg)
1282 = do { ty' <- zonkTcTypeToType env ty
1283 ; return (EvDelayedError ty' msg) }
1284
1285 zonkEvTypeable :: ZonkEnv -> EvTypeable -> TcM EvTypeable
1286 zonkEvTypeable env (EvTypeableTyCon ts)
1287 = do { ts' <- mapM (zonkEvTerm env) ts
1288 ; return $ EvTypeableTyCon ts' }
1289 zonkEvTypeable env (EvTypeableTyApp t1 t2)
1290 = do { t1' <- zonkEvTerm env t1
1291 ; t2' <- zonkEvTerm env t2
1292 ; return (EvTypeableTyApp t1' t2') }
1293 zonkEvTypeable env (EvTypeableTyLit t1)
1294 = do { t1' <- zonkEvTerm env t1
1295 ; return (EvTypeableTyLit t1') }
1296
1297 zonkTcEvBinds_s :: ZonkEnv -> [TcEvBinds] -> TcM (ZonkEnv, [TcEvBinds])
1298 zonkTcEvBinds_s env bs = do { (env, bs') <- mapAccumLM zonk_tc_ev_binds env bs
1299 ; return (env, [EvBinds (unionManyBags bs')]) }
1300
1301 zonkTcEvBinds :: ZonkEnv -> TcEvBinds -> TcM (ZonkEnv, TcEvBinds)
1302 zonkTcEvBinds env bs = do { (env', bs') <- zonk_tc_ev_binds env bs
1303 ; return (env', EvBinds bs') }
1304
1305 zonk_tc_ev_binds :: ZonkEnv -> TcEvBinds -> TcM (ZonkEnv, Bag EvBind)
1306 zonk_tc_ev_binds env (TcEvBinds var) = zonkEvBindsVar env var
1307 zonk_tc_ev_binds env (EvBinds bs) = zonkEvBinds env bs
1308
1309 zonkEvBindsVar :: ZonkEnv -> EvBindsVar -> TcM (ZonkEnv, Bag EvBind)
1310 zonkEvBindsVar env (EvBindsVar ref _) = do { bs <- readMutVar ref
1311 ; zonkEvBinds env (evBindMapBinds bs) }
1312
1313 zonkEvBinds :: ZonkEnv -> Bag EvBind -> TcM (ZonkEnv, Bag EvBind)
1314 zonkEvBinds env binds
1315 = {-# SCC "zonkEvBinds" #-}
1316 fixM (\ ~( _, new_binds) -> do
1317 { let env1 = extendIdZonkEnvRec env (collect_ev_bndrs new_binds)
1318 ; binds' <- mapBagM (zonkEvBind env1) binds
1319 ; return (env1, binds') })
1320 where
1321 collect_ev_bndrs :: Bag EvBind -> [EvVar]
1322 collect_ev_bndrs = foldrBag add []
1323 add (EvBind { eb_lhs = var }) vars = var : vars
1324
1325 zonkEvBind :: ZonkEnv -> EvBind -> TcM EvBind
1326 zonkEvBind env bind@(EvBind { eb_lhs = var, eb_rhs = term })
1327 = do { var' <- {-# SCC "zonkEvBndr" #-} zonkEvBndr env var
1328
1329 -- Optimise the common case of Refl coercions
1330 -- See Note [Optimise coercion zonking]
1331 -- This has a very big effect on some programs (eg Trac #5030)
1332
1333 ; term' <- case getEqPredTys_maybe (idType var') of
1334 Just (r, ty1, ty2) | ty1 `eqType` ty2
1335 -> return (EvCoercion (mkTcReflCo r ty1))
1336 _other -> zonkEvTerm env term
1337
1338 ; return (bind { eb_lhs = var', eb_rhs = term' }) }
1339
1340 {-
1341 ************************************************************************
1342 * *
1343 Zonking types
1344 * *
1345 ************************************************************************
1346
1347 Note [Zonking the LHS of a RULE]
1348 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1349 We need to gather the type variables mentioned on the LHS so we can
1350 quantify over them. Example:
1351 data T a = C
1352
1353 foo :: T a -> Int
1354 foo C = 1
1355
1356 {-# RULES "myrule" foo C = 1 #-}
1357
1358 After type checking the LHS becomes (foo a (C a))
1359 and we do not want to zap the unbound tyvar 'a' to (), because
1360 that limits the applicability of the rule. Instead, we
1361 want to quantify over it!
1362
1363 It's easiest to get zonkTvCollecting to gather the free tyvars
1364 here. Attempts to do so earlier are tiresome, because (a) the data
1365 type is big and (b) finding the free type vars of an expression is
1366 necessarily monadic operation. (consider /\a -> f @ b, where b is
1367 side-effected to a)
1368
1369 And that in turn is why ZonkEnv carries the function to use for
1370 type variables!
1371
1372 Note [Zonking mutable unbound type or kind variables]
1373 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1374 In zonkTypeZapping, we zonk mutable but unbound type or kind variables to an
1375 arbitrary type. We know if they are unbound even though we don't carry an
1376 environment, because at the binding site for a variable we bind the mutable
1377 var to a fresh immutable one. So the mutable store plays the role of an
1378 environment. If we come across a mutable variable that isn't so bound, it
1379 must be completely free. We zonk the expected kind to make sure we don't get
1380 some unbound meta variable as the kind.
1381
1382 Note that since we have kind polymorphism, zonk_unbound_tyvar will handle both
1383 type and kind variables. Consider the following datatype:
1384
1385 data Phantom a = Phantom Int
1386
1387 The type of Phantom is (forall (k : *). forall (a : k). Int). Both `a` and
1388 `k` are unbound variables. We want to zonk this to
1389 (forall (k : Any *). forall (a : Any (Any *)). Int).
1390
1391 Note [Optimise coercion zonking]
1392 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1393 When optimising evidence binds we may come across situations where
1394 a coercion looks like
1395 cv = ReflCo ty
1396 or cv1 = cv2
1397 where the type 'ty' is big. In such cases it is a waste of time to zonk both
1398 * The variable on the LHS
1399 * The coercion on the RHS
1400 Rather, we can zonk the variable, and if its type is (ty ~ ty), we can just
1401 use Refl on the right, ignoring the actual coercion on the RHS.
1402
1403 This can have a very big effect, because the constraint solver sometimes does go
1404 to a lot of effort to prove Refl! (Eg when solving 10+3 = 10+3; cf Trac #5030)
1405
1406 -}
1407
1408 zonkTyVarOcc :: ZonkEnv -> TyVar -> TcM TcType
1409 zonkTyVarOcc env@(ZonkEnv zonk_unbound_tyvar tv_env _) tv
1410 | isTcTyVar tv
1411 = case tcTyVarDetails tv of
1412 SkolemTv {} -> lookup_in_env
1413 RuntimeUnk {} -> lookup_in_env
1414 FlatSkol ty -> zonkTcTypeToType env ty
1415 MetaTv { mtv_ref = ref }
1416 -> do { cts <- readMutVar ref
1417 ; case cts of
1418 Flexi -> do { kind <- {-# SCC "zonkKind1" #-}
1419 zonkTcTypeToType env (tyVarKind tv)
1420 ; zonk_unbound_tyvar (setTyVarKind tv kind) }
1421 Indirect ty -> do { zty <- zonkTcTypeToType env ty
1422 -- Small optimisation: shortern-out indirect steps
1423 -- so that the old type may be more easily collected.
1424 ; writeMutVar ref (Indirect zty)
1425 ; return zty } }
1426 | otherwise
1427 = lookup_in_env
1428 where
1429 lookup_in_env -- Look up in the env just as we do for Ids
1430 = case lookupVarEnv tv_env tv of
1431 Nothing -> mkTyVarTy <$> updateTyVarKindM (zonkTcTypeToType env) tv
1432 Just tv' -> return (mkTyVarTy tv')
1433
1434 zonkCoVarOcc :: ZonkEnv -> CoVar -> TcM Coercion
1435 zonkCoVarOcc env@(ZonkEnv _ tyco_env _) cv
1436 | Just cv' <- lookupVarEnv tyco_env cv -- don't look in the knot-tied env
1437 = return $ mkCoVarCo cv'
1438 | otherwise
1439 = mkCoVarCo <$> updateVarTypeM (zonkTcTypeToType env) cv
1440
1441 zonkCoHole :: ZonkEnv -> CoercionHole
1442 -> Role -> Type -> Type -- these are all redundant with
1443 -- the details in the hole,
1444 -- unzonked
1445 -> TcM Coercion
1446 zonkCoHole env h r t1 t2
1447 = do { contents <- unpackCoercionHole_maybe h
1448 ; case contents of
1449 Just co -> do { co <- zonkCoToCo env co
1450 ; checkCoercionHole co h r t1 t2 }
1451
1452 -- This next case should happen only in the presence of
1453 -- (undeferred) type errors. Originally, I put in a panic
1454 -- here, but that caused too many uses of `failIfErrsM`.
1455 Nothing -> do { traceTc "Zonking unfilled coercion hole" (ppr h)
1456 ; when debugIsOn $
1457 whenNoErrs $
1458 MASSERT2( False
1459 , text "Type-correct unfilled coercion hole"
1460 <+> ppr h )
1461 ; t1 <- zonkTcTypeToType env t1
1462 ; t2 <- zonkTcTypeToType env t2
1463 ; return $ mkHoleCo h r t1 t2 } }
1464
1465 zonk_tycomapper :: TyCoMapper ZonkEnv TcM
1466 zonk_tycomapper = TyCoMapper
1467 { tcm_smart = True -- Establish type invariants
1468 -- See Note [Type-checking inside the knot] in TcHsType
1469 , tcm_tyvar = zonkTyVarOcc
1470 , tcm_covar = zonkCoVarOcc
1471 , tcm_hole = zonkCoHole
1472 , tcm_tybinder = \env tv _vis -> zonkTyBndrX env tv }
1473
1474 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
1475 zonkTcTypeToType = mapType zonk_tycomapper
1476
1477 zonkTcTypeToTypes :: ZonkEnv -> [TcType] -> TcM [Type]
1478 zonkTcTypeToTypes env tys = mapM (zonkTcTypeToType env) tys
1479
1480 zonkCoToCo :: ZonkEnv -> Coercion -> TcM Coercion
1481 zonkCoToCo = mapCoercion zonk_tycomapper
1482
1483 zonkTvCollecting :: TyVarSet -> TcRef TyVarSet -> UnboundTyVarZonker
1484 -- This variant collects unbound type variables in a mutable variable
1485 -- Works on both types and kinds
1486 zonkTvCollecting kind_vars unbound_tv_set tv
1487 = do { poly_kinds <- xoptM LangExt.PolyKinds
1488 ; if tv `elemVarSet` kind_vars && not poly_kinds then defaultKindVar tv else do
1489 { ty_or_tv <- zonkQuantifiedTyVarOrType tv
1490 ; case ty_or_tv of
1491 Right ty -> return ty
1492 Left tv' -> do
1493 { tv_set <- readMutVar unbound_tv_set
1494 ; writeMutVar unbound_tv_set (extendVarSet tv_set tv')
1495 ; return (mkTyVarTy tv') } } }
1496
1497 zonkTypeZapping :: UnboundTyVarZonker
1498 -- This variant is used for everything except the LHS of rules
1499 -- It zaps unbound type variables to (), or some other arbitrary type
1500 -- Works on both types and kinds
1501 zonkTypeZapping tv
1502 = do { let ty | isLevityVar tv = liftedDataConTy
1503 | otherwise = anyTypeOfKind (tyVarKind tv)
1504 ; writeMetaTyVar tv ty
1505 ; return ty }