2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
13 tidyIdRules, pprTidyIdRules
16 #include "HsVersions.h"
19 import CoreUtils ( exprArity )
20 import Unify ( coreRefineTys )
21 import PprCore ( pprIdRules )
22 import DataCon ( DataCon, isVanillaDataCon )
23 import Id ( Id, mkUserLocal, idInfo, setIdInfo, idUnique,
24 idType, setIdType, idCoreRules )
25 import IdInfo ( setArityInfo, vanillaIdInfo,
26 newStrictnessInfo, setAllStrictnessInfo,
27 newDemandInfo, setNewDemandInfo )
28 import Type ( Type, tidyType, tidyTyVarBndr, substTy, mkTvSubst )
29 import Var ( Var, TyVar )
31 import Name ( getOccName )
32 import OccName ( tidyOccName )
33 import SrcLoc ( noSrcLoc )
34 import Maybes ( orElse )
36 import Util ( mapAccumL )
40 This module contains "tidying" code for *nested* expressions, bindings, rules.
41 The code for *top-level* bindings is in TidyPgm.
43 %************************************************************************
45 \subsection{Tidying expressions, rules}
47 %************************************************************************
52 -> (TidyEnv, CoreBind)
54 tidyBind env (NonRec bndr rhs)
55 = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') ->
56 (env', NonRec bndr' (tidyExpr env' rhs))
58 tidyBind env (Rec prs)
59 = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') ->
60 map (tidyExpr env') (map snd prs) =: \ rhss' ->
61 (env', Rec (zip bndrs' rhss'))
64 ------------ Expressions --------------
65 tidyExpr :: TidyEnv -> CoreExpr -> CoreExpr
66 tidyExpr env (Var v) = Var (tidyVarOcc env v)
67 tidyExpr env (Type ty) = Type (tidyType env ty)
68 tidyExpr env (Lit lit) = Lit lit
69 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
70 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
72 tidyExpr env (Let b e)
73 = tidyBind env b =: \ (env', b') ->
74 Let b' (tidyExpr env' e)
76 tidyExpr env (Case e b ty alts)
77 = tidyBndr env b =: \ (env', b) ->
78 Case (tidyExpr env e) b (tidyType env ty)
79 (map (tidyAlt b env') alts)
81 tidyExpr env (Lam b e)
82 = tidyBndr env b =: \ (env', b) ->
83 Lam b (tidyExpr env' e)
85 ------------ Case alternatives --------------
86 tidyAlt case_bndr env (DataAlt con, vs, rhs)
87 | not (isVanillaDataCon con) -- GADT case
88 = tidyBndrs env tvs =: \ (env1, tvs') ->
90 env2 = refineTidyEnv env con tvs' scrut_ty
92 tidyBndrs env2 ids =: \ (env3, ids') ->
93 (DataAlt con, tvs' ++ ids', tidyExpr env3 rhs)
95 (tvs, ids) = span isTyVar vs
96 scrut_ty = idType case_bndr
98 tidyAlt case_bndr env (con, vs, rhs)
99 = tidyBndrs env vs =: \ (env', vs) ->
100 (con, vs, tidyExpr env' rhs)
102 refineTidyEnv :: TidyEnv -> DataCon -> [TyVar] -> Type -> TidyEnv
103 -- Refine the TidyEnv in the light of the type refinement from coreRefineTys
104 refineTidyEnv tidy_env@(occ_env, var_env) con tvs scrut_ty
105 = case coreRefineTys in_scope con tvs scrut_ty of
107 Just (tv_subst, all_bound_here)
108 | all_bound_here -- Local type refinement only
110 | otherwise -- Apply the refining subst to the tidy env
111 -- This ensures that occurences have the most refined type
112 -- And that means that exprType will work right everywhere
113 -> (occ_env, mapVarEnv (refine subst) var_env)
115 subst = mkTvSubst in_scope tv_subst
117 refine subst var | isId var = setIdType var (substTy subst (idType var))
120 in_scope = mkInScopeSet var_env -- Seldom used
122 ------------ Notes --------------
123 tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
124 tidyNote env note = note
127 ------------ Rules --------------
128 tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule]
129 tidyIdRules env [] = []
130 tidyIdRules env (IdCoreRule fn is_orph rule : rules)
131 = tidyRule env rule =: \ rule ->
132 tidyIdRules env rules =: \ rules ->
133 (IdCoreRule (tidyVarOcc env fn) is_orph rule : rules)
135 tidyRule :: TidyEnv -> CoreRule -> CoreRule
136 tidyRule env rule@(BuiltinRule _ _) = rule
137 tidyRule env (Rule name act vars tpl_args rhs)
138 = tidyBndrs env vars =: \ (env', vars) ->
139 map (tidyExpr env') tpl_args =: \ tpl_args ->
140 (Rule name act vars tpl_args (tidyExpr env' rhs))
142 pprTidyIdRules :: Id -> SDoc
143 pprTidyIdRules id = pprIdRules (tidyIdRules emptyTidyEnv (idCoreRules id))
147 %************************************************************************
149 \subsection{Tidying non-top-level binders}
151 %************************************************************************
154 tidyVarOcc :: TidyEnv -> Var -> Var
155 tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
157 -- tidyBndr is used for lambda and case binders
158 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
160 | isTyVar var = tidyTyVarBndr env var
161 | otherwise = tidyIdBndr env var
163 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
164 tidyBndrs env vars = mapAccumL tidyBndr env vars
166 tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
167 -- Used for local (non-top-level) let(rec)s
168 tidyLetBndr env (id,rhs)
169 = ((tidy_env,new_var_env), final_id)
171 ((tidy_env,var_env), new_id) = tidyIdBndr env id
173 -- We need to keep around any interesting strictness and
174 -- demand info because later on we may need to use it when
175 -- converting to A-normal form.
177 -- f (g x), where f is strict in its argument, will be converted
178 -- into case (g x) of z -> f z by CorePrep, but only if f still
179 -- has its strictness info.
181 -- Similarly for the demand info - on a let binder, this tells
182 -- CorePrep to turn the let into a case.
184 -- Similarly arity info for eta expansion in CorePrep
186 final_id = new_id `setIdInfo` new_info
188 new_info = vanillaIdInfo
189 `setArityInfo` exprArity rhs
190 `setAllStrictnessInfo` newStrictnessInfo idinfo
191 `setNewDemandInfo` newDemandInfo idinfo
193 -- Override the env we get back from tidyId with the new IdInfo
194 -- so it gets propagated to the usage sites.
195 new_var_env = extendVarEnv var_env id final_id
197 -- Non-top-level variables
198 tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
199 tidyIdBndr env@(tidy_env, var_env) id
200 = -- do this pattern match strictly, otherwise we end up holding on to
201 -- stuff in the OccName.
202 case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') ->
204 -- Give the Id a fresh print-name, *and* rename its type
205 -- The SrcLoc isn't important now,
206 -- though we could extract it from the Id
208 -- All nested Ids now have the same IdInfo, namely vanillaIdInfo,
209 -- which should save some space.
210 -- But note that tidyLetBndr puts some of it back.
211 ty' = tidyType env (idType id)
212 id' = mkUserLocal occ' (idUnique id) ty' noSrcLoc
213 `setIdInfo` vanillaIdInfo
214 var_env' = extendVarEnv var_env id id'
216 ((tidy_env', var_env'), id')