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
20 TcId, TcIdSet, TcDictBinds,
22 zonkTopDecls, zonkTopExpr, zonkTopLExpr,
26 #include "HsVersions.h"
29 import HsSyn -- oodles of it
56 thenM :: Monad a => a b -> (b -> a c) -> a c
59 thenM_ :: Monad a => a b -> a c -> a c
62 returnM :: Monad m => a -> m a
65 mappM :: (Monad m) => (a -> m b) -> [a] -> m [b]
70 %************************************************************************
72 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
74 %************************************************************************
76 Note: If @hsLPatType@ doesn't bear a strong resemblance to @exprType@,
77 then something is wrong.
79 hsLPatType :: OutPat Id -> Type
80 hsLPatType (L _ pat) = hsPatType pat
82 hsPatType :: Pat Id -> Type
83 hsPatType (ParPat pat) = hsLPatType pat
84 hsPatType (WildPat ty) = ty
85 hsPatType (VarPat var) = idType var
86 hsPatType (VarPatOut 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 (HsInteger _ ty) = ty
111 hsLitType (HsRat _ ty) = ty
112 hsLitType (HsFloatPrim _) = floatPrimTy
113 hsLitType (HsDoublePrim _) = doublePrimTy
116 Overloaded literals. Here mainly becuase it uses isIntTy etc
119 shortCutLit :: OverLitVal -> TcType -> Maybe (HsExpr TcId)
120 shortCutLit (HsIntegral i) ty
121 | isIntTy ty && inIntRange i = Just (HsLit (HsInt i))
122 | isWordTy ty && inWordRange i = Just (mkLit wordDataCon (HsWordPrim i))
123 | isIntegerTy ty = Just (HsLit (HsInteger i ty))
124 | otherwise = shortCutLit (HsFractional (fromInteger i)) ty
125 -- The 'otherwise' case is important
126 -- Consider (3 :: Float). Syntactically it looks like an IntLit,
127 -- so we'll call shortCutIntLit, but of course it's a float
128 -- This can make a big difference for programs with a lot of
129 -- literals, compiled without -O
131 shortCutLit (HsFractional f) ty
132 | isFloatTy ty = Just (mkLit floatDataCon (HsFloatPrim f))
133 | isDoubleTy ty = Just (mkLit doubleDataCon (HsDoublePrim f))
134 | otherwise = Nothing
136 shortCutLit (HsIsString s) ty
137 | isStringTy ty = Just (HsLit (HsString s))
138 | otherwise = Nothing
140 mkLit :: DataCon -> HsLit -> HsExpr Id
141 mkLit con lit = HsApp (nlHsVar (dataConWrapId con)) (nlHsLit lit)
143 ------------------------------
144 hsOverLitName :: OverLitVal -> Name
145 -- Get the canonical 'fromX' name for a particular OverLitVal
146 hsOverLitName (HsIntegral {}) = fromIntegerName
147 hsOverLitName (HsFractional {}) = fromRationalName
148 hsOverLitName (HsIsString {}) = fromStringName
151 %************************************************************************
153 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
155 %************************************************************************
158 -- zonkId is used *during* typechecking just to zonk the Id's type
159 zonkId :: TcId -> TcM TcId
161 = zonkTcType (idType id) `thenM` \ ty' ->
162 returnM (Id.setIdType id ty')
165 The rest of the zonking is done *after* typechecking.
166 The main zonking pass runs over the bindings
168 a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
169 b) convert unbound TcTyVar to Void
170 c) convert each TcId to an Id by zonking its type
172 The type variables are converted by binding mutable tyvars to immutable ones
173 and then zonking as normal.
175 The Ids are converted by binding them in the normal Tc envt; that
176 way we maintain sharing; eg an Id is zonked at its binding site and they
177 all occurrences of that Id point to the common zonked copy
179 It's all pretty boring stuff, because HsSyn is such a large type, and
180 the environment manipulation is tiresome.
183 data ZonkEnv = ZonkEnv (TcType -> TcM Type) -- How to zonk a type
184 (IdEnv Id) -- What variables are in scope
185 -- Maps an Id to its zonked version; both have the same Name
186 -- Is only consulted lazily; hence knot-tying
188 emptyZonkEnv :: ZonkEnv
189 emptyZonkEnv = ZonkEnv zonkTypeZapping emptyVarEnv
191 extendZonkEnv :: ZonkEnv -> [Id] -> ZonkEnv
192 extendZonkEnv (ZonkEnv zonk_ty env) ids
193 = ZonkEnv zonk_ty (extendVarEnvList env [(id,id) | id <- ids])
195 extendZonkEnv1 :: ZonkEnv -> Id -> ZonkEnv
196 extendZonkEnv1 (ZonkEnv zonk_ty env) id
197 = ZonkEnv zonk_ty (extendVarEnv env id id)
199 setZonkType :: ZonkEnv -> (TcType -> TcM Type) -> ZonkEnv
200 setZonkType (ZonkEnv _ env) zonk_ty = ZonkEnv zonk_ty env
202 zonkEnvIds :: ZonkEnv -> [Id]
203 zonkEnvIds (ZonkEnv _ env) = varEnvElts env
205 zonkIdOcc :: ZonkEnv -> TcId -> Id
206 -- Ids defined in this module should be in the envt;
207 -- ignore others. (Actually, data constructors are also
208 -- not LocalVars, even when locally defined, but that is fine.)
209 -- (Also foreign-imported things aren't currently in the ZonkEnv;
210 -- that's ok because they don't need zonking.)
212 -- Actually, Template Haskell works in 'chunks' of declarations, and
213 -- an earlier chunk won't be in the 'env' that the zonking phase
214 -- carries around. Instead it'll be in the tcg_gbl_env, already fully
215 -- zonked. There's no point in looking it up there (except for error
216 -- checking), and it's not conveniently to hand; hence the simple
217 -- 'orElse' case in the LocalVar branch.
219 -- Even without template splices, in module Main, the checking of
220 -- 'main' is done as a separate chunk.
221 zonkIdOcc (ZonkEnv _zonk_ty env) id
222 | isLocalVar id = lookupVarEnv env id `orElse` id
225 zonkIdOccs :: ZonkEnv -> [TcId] -> [Id]
226 zonkIdOccs env ids = map (zonkIdOcc env) ids
228 -- zonkIdBndr is used *after* typechecking to get the Id's type
229 -- to its final form. The TyVarEnv give
230 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
232 = zonkTcTypeToType env (idType id) `thenM` \ ty' ->
233 returnM (Id.setIdType id ty')
235 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
236 zonkIdBndrs env ids = mappM (zonkIdBndr env) ids
238 zonkDictBndrs :: ZonkEnv -> [Var] -> TcM [Var]
239 -- "Dictionary" binders can be coercion variables or dictionary variables
240 zonkDictBndrs env ids = mappM (zonkDictBndr env) ids
242 zonkDictBndr :: ZonkEnv -> Var -> TcM Var
243 zonkDictBndr env var | isTyVar var = zonkTyVarBndr env var
244 | otherwise = zonkIdBndr env var
246 zonkTopBndrs :: [TcId] -> TcM [Id]
247 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
249 -- Zonk the kind of a non-TC tyvar in case it is a coercion variable (their
250 -- kind contains types).
252 zonkTyVarBndr :: ZonkEnv -> TyVar -> TcM TyVar
255 = do { kind <- zonkTcTypeToType env (tyVarKind tv)
256 ; return $ setTyVarKind tv kind
258 | otherwise = return tv
263 zonkTopExpr :: HsExpr TcId -> TcM (HsExpr Id)
264 zonkTopExpr e = zonkExpr emptyZonkEnv e
266 zonkTopLExpr :: LHsExpr TcId -> TcM (LHsExpr Id)
267 zonkTopLExpr e = zonkLExpr emptyZonkEnv e
269 zonkTopDecls :: LHsBinds TcId -> [LRuleDecl TcId] -> [LForeignDecl TcId]
274 zonkTopDecls binds rules fords
275 = do { (env, binds') <- zonkRecMonoBinds emptyZonkEnv binds
276 -- Top level is implicitly recursive
277 ; rules' <- zonkRules env rules
278 ; fords' <- zonkForeignExports env fords
279 ; return (zonkEnvIds env, binds', fords', rules') }
281 ---------------------------------------------
282 zonkLocalBinds :: ZonkEnv -> HsLocalBinds TcId -> TcM (ZonkEnv, HsLocalBinds Id)
283 zonkLocalBinds env EmptyLocalBinds
284 = return (env, EmptyLocalBinds)
286 zonkLocalBinds env (HsValBinds binds)
287 = do { (env1, new_binds) <- zonkValBinds env binds
288 ; return (env1, HsValBinds new_binds) }
290 zonkLocalBinds env (HsIPBinds (IPBinds binds dict_binds))
291 = mappM (wrapLocM zonk_ip_bind) binds `thenM` \ new_binds ->
293 env1 = extendZonkEnv env [ipNameName n | L _ (IPBind n _) <- new_binds]
295 zonkRecMonoBinds env1 dict_binds `thenM` \ (env2, new_dict_binds) ->
296 returnM (env2, HsIPBinds (IPBinds new_binds new_dict_binds))
298 zonk_ip_bind (IPBind n e)
299 = mapIPNameTc (zonkIdBndr env) n `thenM` \ n' ->
300 zonkLExpr env e `thenM` \ e' ->
301 returnM (IPBind n' e')
304 ---------------------------------------------
305 zonkValBinds :: ZonkEnv -> HsValBinds TcId -> TcM (ZonkEnv, HsValBinds Id)
306 zonkValBinds _ (ValBindsIn _ _)
307 = panic "zonkValBinds" -- Not in typechecker output
308 zonkValBinds env (ValBindsOut binds sigs)
309 = do { (env1, new_binds) <- go env binds
310 ; return (env1, ValBindsOut new_binds sigs) }
312 go env [] = return (env, [])
313 go env ((r,b):bs) = do { (env1, b') <- zonkRecMonoBinds env b
314 ; (env2, bs') <- go env1 bs
315 ; return (env2, (r,b'):bs') }
317 ---------------------------------------------
318 zonkRecMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (ZonkEnv, LHsBinds Id)
319 zonkRecMonoBinds env binds
320 = fixM (\ ~(_, new_binds) -> do
321 { let env1 = extendZonkEnv env (collectHsBindsBinders new_binds)
322 ; binds' <- zonkMonoBinds env1 binds
323 ; return (env1, binds') })
325 ---------------------------------------------
326 zonkMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (LHsBinds Id)
327 zonkMonoBinds env binds = mapBagM (wrapLocM (zonk_bind env)) binds
329 zonk_bind :: ZonkEnv -> HsBind TcId -> TcM (HsBind Id)
330 zonk_bind env bind@(PatBind { pat_lhs = pat, pat_rhs = grhss, pat_rhs_ty = ty})
331 = do { (_env, new_pat) <- zonkPat env pat -- Env already extended
332 ; new_grhss <- zonkGRHSs env grhss
333 ; new_ty <- zonkTcTypeToType env ty
334 ; return (bind { pat_lhs = new_pat, pat_rhs = new_grhss, pat_rhs_ty = new_ty }) }
336 zonk_bind env (VarBind { var_id = var, var_rhs = expr, var_inline = inl })
337 = zonkIdBndr env var `thenM` \ new_var ->
338 zonkLExpr env expr `thenM` \ new_expr ->
339 returnM (VarBind { var_id = new_var, var_rhs = new_expr, var_inline = inl })
341 zonk_bind env bind@(FunBind { fun_id = var, fun_matches = ms, fun_co_fn = co_fn })
342 = wrapLocM (zonkIdBndr env) var `thenM` \ new_var ->
343 zonkCoFn env co_fn `thenM` \ (env1, new_co_fn) ->
344 zonkMatchGroup env1 ms `thenM` \ new_ms ->
345 returnM (bind { fun_id = new_var, fun_matches = new_ms, fun_co_fn = new_co_fn })
347 zonk_bind env (AbsBinds { abs_tvs = tyvars, abs_dicts = dicts,
348 abs_exports = exports, abs_binds = val_binds })
349 = ASSERT( all isImmutableTyVar tyvars )
350 zonkDictBndrs env dicts `thenM` \ new_dicts ->
351 fixM (\ ~(new_val_binds, _) ->
353 env1 = extendZonkEnv env new_dicts
354 env2 = extendZonkEnv env1 (collectHsBindsBinders new_val_binds)
356 zonkMonoBinds env2 val_binds `thenM` \ new_val_binds ->
357 mappM (zonkExport env2) exports `thenM` \ new_exports ->
358 returnM (new_val_binds, new_exports)
359 ) `thenM` \ (new_val_bind, new_exports) ->
360 returnM (AbsBinds { abs_tvs = tyvars, abs_dicts = new_dicts,
361 abs_exports = new_exports, abs_binds = new_val_bind })
363 zonkExport env (tyvars, global, local, prags)
364 -- The tyvars are already zonked
365 = zonkIdBndr env global `thenM` \ new_global ->
366 zonk_prags prags `thenM` \ new_prags ->
367 returnM (tyvars, new_global, zonkIdOcc env local, new_prags)
369 zonk_prags IsDefaultMethod = return IsDefaultMethod
370 zonk_prags (SpecPrags ps) = do { ps' <- mapM zonk_prag ps; return (SpecPrags ps') }
372 zonk_prag (L loc (SpecPrag co_fn inl))
373 = do { (_, co_fn') <- zonkCoFn env co_fn
374 ; return (L loc (SpecPrag co_fn' inl)) }
377 %************************************************************************
379 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
381 %************************************************************************
384 zonkMatchGroup :: ZonkEnv -> MatchGroup TcId-> TcM (MatchGroup Id)
385 zonkMatchGroup env (MatchGroup ms ty)
386 = do { ms' <- mapM (zonkMatch env) ms
387 ; ty' <- zonkTcTypeToType env ty
388 ; return (MatchGroup ms' ty') }
390 zonkMatch :: ZonkEnv -> LMatch TcId-> TcM (LMatch Id)
391 zonkMatch env (L loc (Match pats _ grhss))
392 = do { (env1, new_pats) <- zonkPats env pats
393 ; new_grhss <- zonkGRHSs env1 grhss
394 ; return (L loc (Match new_pats Nothing new_grhss)) }
396 -------------------------------------------------------------------------
397 zonkGRHSs :: ZonkEnv -> GRHSs TcId -> TcM (GRHSs Id)
399 zonkGRHSs env (GRHSs grhss binds)
400 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
402 zonk_grhs (GRHS guarded rhs)
403 = zonkStmts new_env guarded `thenM` \ (env2, new_guarded) ->
404 zonkLExpr env2 rhs `thenM` \ new_rhs ->
405 returnM (GRHS new_guarded new_rhs)
407 mappM (wrapLocM zonk_grhs) grhss `thenM` \ new_grhss ->
408 returnM (GRHSs new_grhss new_binds)
411 %************************************************************************
413 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
415 %************************************************************************
418 zonkLExprs :: ZonkEnv -> [LHsExpr TcId] -> TcM [LHsExpr Id]
419 zonkLExpr :: ZonkEnv -> LHsExpr TcId -> TcM (LHsExpr Id)
420 zonkExpr :: ZonkEnv -> HsExpr TcId -> TcM (HsExpr Id)
422 zonkLExprs env exprs = mappM (zonkLExpr env) exprs
423 zonkLExpr env expr = wrapLocM (zonkExpr env) expr
425 zonkExpr env (HsVar id)
426 = returnM (HsVar (zonkIdOcc env id))
428 zonkExpr env (HsIPVar id)
429 = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
431 zonkExpr env (HsLit (HsRat f ty))
432 = zonkTcTypeToType env ty `thenM` \ new_ty ->
433 returnM (HsLit (HsRat f new_ty))
435 zonkExpr _ (HsLit lit)
436 = returnM (HsLit lit)
438 zonkExpr env (HsOverLit lit)
439 = do { lit' <- zonkOverLit env lit
440 ; return (HsOverLit lit') }
442 zonkExpr env (HsLam matches)
443 = zonkMatchGroup env matches `thenM` \ new_matches ->
444 returnM (HsLam new_matches)
446 zonkExpr env (HsApp e1 e2)
447 = zonkLExpr env e1 `thenM` \ new_e1 ->
448 zonkLExpr env e2 `thenM` \ new_e2 ->
449 returnM (HsApp new_e1 new_e2)
451 zonkExpr env (HsBracketOut body bs)
452 = mappM zonk_b bs `thenM` \ bs' ->
453 returnM (HsBracketOut body bs')
455 zonk_b (n,e) = zonkLExpr env e `thenM` \ e' ->
458 zonkExpr _ (HsSpliceE s) = WARN( True, ppr s ) -- Should not happen
459 returnM (HsSpliceE s)
461 zonkExpr env (OpApp e1 op fixity e2)
462 = zonkLExpr env e1 `thenM` \ new_e1 ->
463 zonkLExpr env op `thenM` \ new_op ->
464 zonkLExpr env e2 `thenM` \ new_e2 ->
465 returnM (OpApp new_e1 new_op fixity new_e2)
467 zonkExpr env (NegApp expr op)
468 = zonkLExpr env expr `thenM` \ new_expr ->
469 zonkExpr env op `thenM` \ new_op ->
470 returnM (NegApp new_expr new_op)
472 zonkExpr env (HsPar e)
473 = zonkLExpr env e `thenM` \new_e ->
474 returnM (HsPar new_e)
476 zonkExpr env (SectionL expr op)
477 = zonkLExpr env expr `thenM` \ new_expr ->
478 zonkLExpr env op `thenM` \ new_op ->
479 returnM (SectionL new_expr new_op)
481 zonkExpr env (SectionR op expr)
482 = zonkLExpr env op `thenM` \ new_op ->
483 zonkLExpr env expr `thenM` \ new_expr ->
484 returnM (SectionR new_op new_expr)
486 zonkExpr env (ExplicitTuple tup_args boxed)
487 = do { new_tup_args <- mapM zonk_tup_arg tup_args
488 ; return (ExplicitTuple new_tup_args boxed) }
490 zonk_tup_arg (Present e) = do { e' <- zonkLExpr env e; return (Present e') }
491 zonk_tup_arg (Missing t) = do { t' <- zonkTcTypeToType env t; return (Missing t') }
493 zonkExpr env (HsCase expr ms)
494 = zonkLExpr env expr `thenM` \ new_expr ->
495 zonkMatchGroup env ms `thenM` \ new_ms ->
496 returnM (HsCase new_expr new_ms)
498 zonkExpr env (HsIf e1 e2 e3)
499 = zonkLExpr env e1 `thenM` \ new_e1 ->
500 zonkLExpr env e2 `thenM` \ new_e2 ->
501 zonkLExpr env e3 `thenM` \ new_e3 ->
502 returnM (HsIf new_e1 new_e2 new_e3)
504 zonkExpr env (HsLet binds expr)
505 = zonkLocalBinds env binds `thenM` \ (new_env, new_binds) ->
506 zonkLExpr new_env expr `thenM` \ new_expr ->
507 returnM (HsLet new_binds new_expr)
509 zonkExpr env (HsDo do_or_lc stmts body ty)
510 = zonkStmts env stmts `thenM` \ (new_env, new_stmts) ->
511 zonkLExpr new_env body `thenM` \ new_body ->
512 zonkTcTypeToType env ty `thenM` \ new_ty ->
513 returnM (HsDo (zonkDo env do_or_lc)
514 new_stmts new_body new_ty)
516 zonkExpr env (ExplicitList ty exprs)
517 = zonkTcTypeToType env ty `thenM` \ new_ty ->
518 zonkLExprs env exprs `thenM` \ new_exprs ->
519 returnM (ExplicitList new_ty new_exprs)
521 zonkExpr env (ExplicitPArr ty exprs)
522 = zonkTcTypeToType env ty `thenM` \ new_ty ->
523 zonkLExprs env exprs `thenM` \ new_exprs ->
524 returnM (ExplicitPArr new_ty new_exprs)
526 zonkExpr env (RecordCon data_con con_expr rbinds)
527 = do { new_con_expr <- zonkExpr env con_expr
528 ; new_rbinds <- zonkRecFields env rbinds
529 ; return (RecordCon data_con new_con_expr new_rbinds) }
531 zonkExpr env (RecordUpd expr rbinds cons in_tys out_tys)
532 = do { new_expr <- zonkLExpr env expr
533 ; new_in_tys <- mapM (zonkTcTypeToType env) in_tys
534 ; new_out_tys <- mapM (zonkTcTypeToType env) out_tys
535 ; new_rbinds <- zonkRecFields env rbinds
536 ; return (RecordUpd new_expr new_rbinds cons new_in_tys new_out_tys) }
538 zonkExpr env (ExprWithTySigOut e ty)
539 = do { e' <- zonkLExpr env e
540 ; return (ExprWithTySigOut e' ty) }
542 zonkExpr _ (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
544 zonkExpr env (ArithSeq expr info)
545 = zonkExpr env expr `thenM` \ new_expr ->
546 zonkArithSeq env info `thenM` \ new_info ->
547 returnM (ArithSeq new_expr new_info)
549 zonkExpr env (PArrSeq expr info)
550 = zonkExpr env expr `thenM` \ new_expr ->
551 zonkArithSeq env info `thenM` \ new_info ->
552 returnM (PArrSeq new_expr new_info)
554 zonkExpr env (HsSCC lbl expr)
555 = zonkLExpr env expr `thenM` \ new_expr ->
556 returnM (HsSCC lbl new_expr)
558 zonkExpr env (HsTickPragma info expr)
559 = zonkLExpr env expr `thenM` \ new_expr ->
560 returnM (HsTickPragma info new_expr)
562 -- hdaume: core annotations
563 zonkExpr env (HsCoreAnn lbl expr)
564 = zonkLExpr env expr `thenM` \ new_expr ->
565 returnM (HsCoreAnn lbl new_expr)
567 -- arrow notation extensions
568 zonkExpr env (HsProc pat body)
569 = do { (env1, new_pat) <- zonkPat env pat
570 ; new_body <- zonkCmdTop env1 body
571 ; return (HsProc new_pat new_body) }
573 zonkExpr env (HsArrApp e1 e2 ty ho rl)
574 = zonkLExpr env e1 `thenM` \ new_e1 ->
575 zonkLExpr env e2 `thenM` \ new_e2 ->
576 zonkTcTypeToType env ty `thenM` \ new_ty ->
577 returnM (HsArrApp new_e1 new_e2 new_ty ho rl)
579 zonkExpr env (HsArrForm op fixity args)
580 = zonkLExpr env op `thenM` \ new_op ->
581 mappM (zonkCmdTop env) args `thenM` \ new_args ->
582 returnM (HsArrForm new_op fixity new_args)
584 zonkExpr env (HsWrap co_fn expr)
585 = zonkCoFn env co_fn `thenM` \ (env1, new_co_fn) ->
586 zonkExpr env1 expr `thenM` \ new_expr ->
587 return (HsWrap new_co_fn new_expr)
589 zonkExpr _ expr = pprPanic "zonkExpr" (ppr expr)
591 zonkCmdTop :: ZonkEnv -> LHsCmdTop TcId -> TcM (LHsCmdTop Id)
592 zonkCmdTop env cmd = wrapLocM (zonk_cmd_top env) cmd
594 zonk_cmd_top :: ZonkEnv -> HsCmdTop TcId -> TcM (HsCmdTop Id)
595 zonk_cmd_top env (HsCmdTop cmd stack_tys ty ids)
596 = zonkLExpr env cmd `thenM` \ new_cmd ->
597 zonkTcTypeToTypes env stack_tys `thenM` \ new_stack_tys ->
598 zonkTcTypeToType env ty `thenM` \ new_ty ->
599 mapSndM (zonkExpr env) ids `thenM` \ new_ids ->
600 returnM (HsCmdTop new_cmd new_stack_tys new_ty new_ids)
602 -------------------------------------------------------------------------
603 zonkCoFn :: ZonkEnv -> HsWrapper -> TcM (ZonkEnv, HsWrapper)
604 zonkCoFn env WpHole = return (env, WpHole)
605 zonkCoFn env (WpCompose c1 c2) = do { (env1, c1') <- zonkCoFn env c1
606 ; (env2, c2') <- zonkCoFn env1 c2
607 ; return (env2, WpCompose c1' c2') }
608 zonkCoFn env (WpCast co) = do { co' <- zonkTcTypeToType env co
609 ; return (env, WpCast co') }
610 zonkCoFn env (WpLam id) = do { id' <- zonkDictBndr env id
611 ; let env1 = extendZonkEnv1 env id'
612 ; return (env1, WpLam id') }
613 zonkCoFn env (WpTyLam tv) = ASSERT( isImmutableTyVar tv )
614 do { tv' <- zonkTyVarBndr env tv
615 ; return (env, WpTyLam tv') }
616 zonkCoFn env (WpApp v)
617 | isTcTyVar v = do { co <- zonkTcTyVar v
618 ; return (env, WpTyApp co) }
619 -- Yuk! A mutable coercion variable is a TcTyVar
620 -- not a CoVar, so don't use isCoVar!
621 -- Yuk! A WpApp can't hold the zonked type,
622 -- so we switch to WpTyApp
623 | otherwise = return (env, WpApp (zonkIdOcc env v))
624 zonkCoFn env (WpTyApp ty) = do { ty' <- zonkTcTypeToType env ty
625 ; return (env, WpTyApp ty') }
626 zonkCoFn env (WpLet bs) = do { (env1, bs') <- zonkRecMonoBinds env bs
627 ; return (env1, WpLet bs') }
630 -------------------------------------------------------------------------
631 zonkDo :: ZonkEnv -> HsStmtContext Name -> HsStmtContext Name
632 -- Only used for 'do', so the only Ids are in a MDoExpr table
633 zonkDo env (MDoExpr tbl) = MDoExpr (mapSnd (zonkIdOcc env) tbl)
634 zonkDo _ do_or_lc = do_or_lc
636 -------------------------------------------------------------------------
637 zonkOverLit :: ZonkEnv -> HsOverLit TcId -> TcM (HsOverLit Id)
638 zonkOverLit env lit@(OverLit { ol_witness = e, ol_type = ty })
639 = do { ty' <- zonkTcTypeToType env ty
640 ; e' <- zonkExpr env e
641 ; return (lit { ol_witness = e', ol_type = ty' }) }
643 -------------------------------------------------------------------------
644 zonkArithSeq :: ZonkEnv -> ArithSeqInfo TcId -> TcM (ArithSeqInfo Id)
646 zonkArithSeq env (From e)
647 = zonkLExpr env e `thenM` \ new_e ->
650 zonkArithSeq env (FromThen e1 e2)
651 = zonkLExpr env e1 `thenM` \ new_e1 ->
652 zonkLExpr env e2 `thenM` \ new_e2 ->
653 returnM (FromThen new_e1 new_e2)
655 zonkArithSeq env (FromTo e1 e2)
656 = zonkLExpr env e1 `thenM` \ new_e1 ->
657 zonkLExpr env e2 `thenM` \ new_e2 ->
658 returnM (FromTo new_e1 new_e2)
660 zonkArithSeq env (FromThenTo e1 e2 e3)
661 = zonkLExpr env e1 `thenM` \ new_e1 ->
662 zonkLExpr env e2 `thenM` \ new_e2 ->
663 zonkLExpr env e3 `thenM` \ new_e3 ->
664 returnM (FromThenTo new_e1 new_e2 new_e3)
667 -------------------------------------------------------------------------
668 zonkStmts :: ZonkEnv -> [LStmt TcId] -> TcM (ZonkEnv, [LStmt Id])
669 zonkStmts env [] = return (env, [])
670 zonkStmts env (s:ss) = do { (env1, s') <- wrapLocSndM (zonkStmt env) s
671 ; (env2, ss') <- zonkStmts env1 ss
672 ; return (env2, s' : ss') }
674 zonkStmt :: ZonkEnv -> Stmt TcId -> TcM (ZonkEnv, Stmt Id)
675 zonkStmt env (ParStmt stmts_w_bndrs)
676 = mappM zonk_branch stmts_w_bndrs `thenM` \ new_stmts_w_bndrs ->
678 new_binders = concat (map snd new_stmts_w_bndrs)
679 env1 = extendZonkEnv env new_binders
681 return (env1, ParStmt new_stmts_w_bndrs)
683 zonk_branch (stmts, bndrs) = zonkStmts env stmts `thenM` \ (env1, new_stmts) ->
684 returnM (new_stmts, zonkIdOccs env1 bndrs)
686 zonkStmt env (RecStmt { recS_stmts = segStmts, recS_later_ids = lvs, recS_rec_ids = rvs
687 , recS_ret_fn = ret_id, recS_mfix_fn = mfix_id, recS_bind_fn = bind_id
688 , recS_rec_rets = rets, recS_dicts = binds })
689 = do { new_rvs <- zonkIdBndrs env rvs
690 ; new_lvs <- zonkIdBndrs env lvs
691 ; new_ret_id <- zonkExpr env ret_id
692 ; new_mfix_id <- zonkExpr env mfix_id
693 ; new_bind_id <- zonkExpr env bind_id
694 ; let env1 = extendZonkEnv env new_rvs
695 ; (env2, new_segStmts) <- zonkStmts env1 segStmts
696 -- Zonk the ret-expressions in an envt that
697 -- has the polymorphic bindings in the envt
698 ; new_rets <- mapM (zonkExpr env2) rets
699 ; let env3 = extendZonkEnv env new_lvs -- Only the lvs are needed
700 ; (env4, new_binds) <- zonkRecMonoBinds env3 binds
702 RecStmt { recS_stmts = new_segStmts, recS_later_ids = new_lvs
703 , recS_rec_ids = new_rvs, recS_ret_fn = new_ret_id
704 , recS_mfix_fn = new_mfix_id, recS_bind_fn = new_bind_id
705 , recS_rec_rets = new_rets, recS_dicts = new_binds }) }
707 zonkStmt env (ExprStmt expr then_op ty)
708 = zonkLExpr env expr `thenM` \ new_expr ->
709 zonkExpr env then_op `thenM` \ new_then ->
710 zonkTcTypeToType env ty `thenM` \ new_ty ->
711 returnM (env, ExprStmt new_expr new_then new_ty)
713 zonkStmt env (TransformStmt stmts binders usingExpr maybeByExpr)
714 = do { (env', stmts') <- zonkStmts env stmts
715 ; let binders' = zonkIdOccs env' binders
716 ; usingExpr' <- zonkLExpr env' usingExpr
717 ; maybeByExpr' <- zonkMaybeLExpr env' maybeByExpr
718 ; return (env', TransformStmt stmts' binders' usingExpr' maybeByExpr') }
720 zonkStmt env (GroupStmt stmts binderMap by using)
721 = do { (env', stmts') <- zonkStmts env stmts
722 ; binderMap' <- mappM (zonkBinderMapEntry env') binderMap
723 ; by' <- fmapMaybeM (zonkLExpr env') by
724 ; using' <- fmapEitherM (zonkLExpr env) (zonkExpr env) using
725 ; let env'' = extendZonkEnv env' (map snd binderMap')
726 ; return (env'', GroupStmt stmts' binderMap' by' using') }
728 zonkBinderMapEntry env (oldBinder, newBinder) = do
729 let oldBinder' = zonkIdOcc env oldBinder
730 newBinder' <- zonkIdBndr env newBinder
731 return (oldBinder', newBinder')
733 zonkStmt env (LetStmt binds)
734 = zonkLocalBinds env binds `thenM` \ (env1, new_binds) ->
735 returnM (env1, LetStmt new_binds)
737 zonkStmt env (BindStmt pat expr bind_op fail_op)
738 = do { new_expr <- zonkLExpr env expr
739 ; (env1, new_pat) <- zonkPat env pat
740 ; new_bind <- zonkExpr env bind_op
741 ; new_fail <- zonkExpr env fail_op
742 ; return (env1, BindStmt new_pat new_expr new_bind new_fail) }
744 zonkMaybeLExpr :: ZonkEnv -> Maybe (LHsExpr TcId) -> TcM (Maybe (LHsExpr Id))
745 zonkMaybeLExpr _ Nothing = return Nothing
746 zonkMaybeLExpr env (Just e) = (zonkLExpr env e) >>= (return . Just)
749 -------------------------------------------------------------------------
750 zonkRecFields :: ZonkEnv -> HsRecordBinds TcId -> TcM (HsRecordBinds TcId)
751 zonkRecFields env (HsRecFields flds dd)
752 = do { flds' <- mappM zonk_rbind flds
753 ; return (HsRecFields flds' dd) }
756 = do { new_id <- wrapLocM (zonkIdBndr env) (hsRecFieldId fld)
757 ; new_expr <- zonkLExpr env (hsRecFieldArg fld)
758 ; return (fld { hsRecFieldId = new_id, hsRecFieldArg = new_expr }) }
760 -------------------------------------------------------------------------
761 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
762 mapIPNameTc f (IPName n) = f n `thenM` \ r -> returnM (IPName r)
766 %************************************************************************
768 \subsection[BackSubst-Pats]{Patterns}
770 %************************************************************************
773 zonkPat :: ZonkEnv -> OutPat TcId -> TcM (ZonkEnv, OutPat Id)
774 -- Extend the environment as we go, because it's possible for one
775 -- pattern to bind something that is used in another (inside or
777 zonkPat env pat = wrapLocSndM (zonk_pat env) pat
779 zonk_pat :: ZonkEnv -> Pat TcId -> TcM (ZonkEnv, Pat Id)
780 zonk_pat env (ParPat p)
781 = do { (env', p') <- zonkPat env p
782 ; return (env', ParPat p') }
784 zonk_pat env (WildPat ty)
785 = do { ty' <- zonkTcTypeToType env ty
786 ; return (env, WildPat ty') }
788 zonk_pat env (VarPat v)
789 = do { v' <- zonkIdBndr env v
790 ; return (extendZonkEnv1 env v', VarPat v') }
792 zonk_pat env (VarPatOut v binds)
793 = do { v' <- zonkIdBndr env v
794 ; (env', binds') <- zonkRecMonoBinds (extendZonkEnv1 env v') binds
795 ; returnM (env', VarPatOut v' binds') }
797 zonk_pat env (LazyPat pat)
798 = do { (env', pat') <- zonkPat env pat
799 ; return (env', LazyPat pat') }
801 zonk_pat env (BangPat pat)
802 = do { (env', pat') <- zonkPat env pat
803 ; return (env', BangPat pat') }
805 zonk_pat env (AsPat (L loc v) pat)
806 = do { v' <- zonkIdBndr env v
807 ; (env', pat') <- zonkPat (extendZonkEnv1 env v') pat
808 ; return (env', AsPat (L loc v') pat') }
810 zonk_pat env (ViewPat expr pat ty)
811 = do { expr' <- zonkLExpr env expr
812 ; (env', pat') <- zonkPat env pat
813 ; ty' <- zonkTcTypeToType env ty
814 ; return (env', ViewPat expr' pat' ty') }
816 zonk_pat env (ListPat pats ty)
817 = do { ty' <- zonkTcTypeToType env ty
818 ; (env', pats') <- zonkPats env pats
819 ; return (env', ListPat pats' ty') }
821 zonk_pat env (PArrPat pats ty)
822 = do { ty' <- zonkTcTypeToType env ty
823 ; (env', pats') <- zonkPats env pats
824 ; return (env', PArrPat pats' ty') }
826 zonk_pat env (TuplePat pats boxed ty)
827 = do { ty' <- zonkTcTypeToType env ty
828 ; (env', pats') <- zonkPats env pats
829 ; return (env', TuplePat pats' boxed ty') }
831 zonk_pat env p@(ConPatOut { pat_ty = ty, pat_dicts = dicts, pat_binds = binds, pat_args = args })
832 = ASSERT( all isImmutableTyVar (pat_tvs p) )
833 do { new_ty <- zonkTcTypeToType env ty
834 ; new_dicts <- zonkDictBndrs env dicts
835 ; let env1 = extendZonkEnv env new_dicts
836 ; (env2, new_binds) <- zonkRecMonoBinds env1 binds
837 ; (env', new_args) <- zonkConStuff env2 args
838 ; returnM (env', p { pat_ty = new_ty, pat_dicts = new_dicts,
839 pat_binds = new_binds, pat_args = new_args }) }
841 zonk_pat env (LitPat lit) = return (env, LitPat lit)
843 zonk_pat env (SigPatOut pat ty)
844 = do { ty' <- zonkTcTypeToType env ty
845 ; (env', pat') <- zonkPat env pat
846 ; return (env', SigPatOut pat' ty') }
848 zonk_pat env (NPat lit mb_neg eq_expr)
849 = do { lit' <- zonkOverLit env lit
850 ; mb_neg' <- case mb_neg of
851 Nothing -> return Nothing
852 Just neg -> do { neg' <- zonkExpr env neg
853 ; return (Just neg') }
854 ; eq_expr' <- zonkExpr env eq_expr
855 ; return (env, NPat lit' mb_neg' eq_expr') }
857 zonk_pat env (NPlusKPat (L loc n) lit e1 e2)
858 = do { n' <- zonkIdBndr env n
859 ; lit' <- zonkOverLit env lit
860 ; e1' <- zonkExpr env e1
861 ; e2' <- zonkExpr env e2
862 ; return (extendZonkEnv1 env n', NPlusKPat (L loc n') lit' e1' e2') }
864 zonk_pat env (CoPat co_fn pat ty)
865 = do { (env', co_fn') <- zonkCoFn env co_fn
866 ; (env'', pat') <- zonkPat env' (noLoc pat)
867 ; ty' <- zonkTcTypeToType env'' ty
868 ; return (env'', CoPat co_fn' (unLoc pat') ty') }
870 zonk_pat _ pat = pprPanic "zonk_pat" (ppr pat)
872 ---------------------------
873 zonkConStuff :: ZonkEnv
874 -> HsConDetails (OutPat TcId) (HsRecFields id (OutPat TcId))
876 HsConDetails (OutPat Id) (HsRecFields id (OutPat Id)))
877 zonkConStuff env (PrefixCon pats)
878 = do { (env', pats') <- zonkPats env pats
879 ; return (env', PrefixCon pats') }
881 zonkConStuff env (InfixCon p1 p2)
882 = do { (env1, p1') <- zonkPat env p1
883 ; (env', p2') <- zonkPat env1 p2
884 ; return (env', InfixCon p1' p2') }
886 zonkConStuff env (RecCon (HsRecFields rpats dd))
887 = do { (env', pats') <- zonkPats env (map hsRecFieldArg rpats)
888 ; let rpats' = zipWith (\rp p' -> rp { hsRecFieldArg = p' }) rpats pats'
889 ; returnM (env', RecCon (HsRecFields rpats' dd)) }
890 -- Field selectors have declared types; hence no zonking
892 ---------------------------
893 zonkPats :: ZonkEnv -> [OutPat TcId] -> TcM (ZonkEnv, [OutPat Id])
894 zonkPats env [] = return (env, [])
895 zonkPats env (pat:pats) = do { (env1, pat') <- zonkPat env pat
896 ; (env', pats') <- zonkPats env1 pats
897 ; return (env', pat':pats') }
900 %************************************************************************
902 \subsection[BackSubst-Foreign]{Foreign exports}
904 %************************************************************************
908 zonkForeignExports :: ZonkEnv -> [LForeignDecl TcId] -> TcM [LForeignDecl Id]
909 zonkForeignExports env ls = mappM (wrapLocM (zonkForeignExport env)) ls
911 zonkForeignExport :: ZonkEnv -> ForeignDecl TcId -> TcM (ForeignDecl Id)
912 zonkForeignExport env (ForeignExport i _hs_ty spec) =
913 returnM (ForeignExport (fmap (zonkIdOcc env) i) undefined spec)
914 zonkForeignExport _ for_imp
915 = returnM for_imp -- Foreign imports don't need zonking
919 zonkRules :: ZonkEnv -> [LRuleDecl TcId] -> TcM [LRuleDecl Id]
920 zonkRules env rs = mappM (wrapLocM (zonkRule env)) rs
922 zonkRule :: ZonkEnv -> RuleDecl TcId -> TcM (RuleDecl Id)
923 zonkRule env (HsRule name act (vars{-::[RuleBndr TcId]-}) lhs fv_lhs rhs fv_rhs)
924 = mappM zonk_bndr vars `thenM` \ new_bndrs ->
925 newMutVar emptyVarSet `thenM` \ unbound_tv_set ->
927 env_rhs = extendZonkEnv env [id | b <- new_bndrs, let id = unLoc b, isId id]
928 -- Type variables don't need an envt
929 -- They are bound through the mutable mechanism
931 env_lhs = setZonkType env_rhs (zonkTypeCollecting unbound_tv_set)
932 -- We need to gather the type variables mentioned on the LHS so we can
933 -- quantify over them. Example:
939 -- {-# RULES "myrule" foo C = 1 #-}
941 -- After type checking the LHS becomes (foo a (C a))
942 -- and we do not want to zap the unbound tyvar 'a' to (), because
943 -- that limits the applicability of the rule. Instead, we
944 -- want to quantify over it!
946 -- It's easiest to find the free tyvars here. Attempts to do so earlier
947 -- are tiresome, because (a) the data type is big and (b) finding the
948 -- free type vars of an expression is necessarily monadic operation.
949 -- (consider /\a -> f @ b, where b is side-effected to a)
951 zonkLExpr env_lhs lhs `thenM` \ new_lhs ->
952 zonkLExpr env_rhs rhs `thenM` \ new_rhs ->
954 readMutVar unbound_tv_set `thenM` \ unbound_tvs ->
956 final_bndrs :: [Located Var]
957 final_bndrs = map noLoc (varSetElems unbound_tvs) ++ new_bndrs
959 returnM (HsRule name act (map RuleBndr final_bndrs) new_lhs fv_lhs new_rhs fv_rhs)
960 -- I hate this map RuleBndr stuff
962 zonk_bndr (RuleBndr v)
963 | isId (unLoc v) = wrapLocM (zonkIdBndr env) v
964 | otherwise = ASSERT( isImmutableTyVar (unLoc v) )
966 zonk_bndr (RuleBndrSig {}) = panic "zonk_bndr RuleBndrSig"
970 %************************************************************************
972 \subsection[BackSubst-Foreign]{Foreign exports}
974 %************************************************************************
977 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
978 zonkTcTypeToType (ZonkEnv zonk_ty _) ty = zonk_ty ty
980 zonkTcTypeToTypes :: ZonkEnv -> [TcType] -> TcM [Type]
981 zonkTcTypeToTypes env tys = mapM (zonkTcTypeToType env) tys
983 zonkTypeCollecting :: TcRef TyVarSet -> TcType -> TcM Type
984 -- This variant collects unbound type variables in a mutable variable
985 zonkTypeCollecting unbound_tv_set
986 = zonkType zonk_unbound_tyvar
988 zonk_unbound_tyvar tv
989 = zonkQuantifiedTyVar tv `thenM` \ tv' ->
990 readMutVar unbound_tv_set `thenM` \ tv_set ->
991 writeMutVar unbound_tv_set (extendVarSet tv_set tv') `thenM_`
992 return (mkTyVarTy tv')
994 zonkTypeZapping :: TcType -> TcM Type
995 -- This variant is used for everything except the LHS of rules
996 -- It zaps unbound type variables to (), or some other arbitrary type
998 = zonkType zonk_unbound_tyvar ty
1000 -- Zonk a mutable but unbound type variable to an arbitrary type
1001 -- We know it's unbound even though we don't carry an environment,
1002 -- because at the binding site for a type variable we bind the
1003 -- mutable tyvar to a fresh immutable one. So the mutable store
1004 -- plays the role of an environment. If we come across a mutable
1005 -- type variable that isn't so bound, it must be completely free.
1006 zonk_unbound_tyvar tv = do { let ty = anyTypeOfKind (tyVarKind tv)
1007 ; writeMetaTyVar tv ty