2 % (c) The AQUA Project, Glasgow University, 1996-1998
7 tidyExpr, tidyVarOcc, tidyRule, tidyRules
10 #include "HsVersions.h"
13 import CoreUtils ( exprArity )
14 import Unify ( coreRefineTys )
15 import DataCon ( DataCon, isVanillaDataCon )
16 import Id ( Id, mkUserLocal, idInfo, setIdInfo, idUnique,
18 import IdInfo ( setArityInfo, vanillaIdInfo,
19 newStrictnessInfo, setAllStrictnessInfo,
20 newDemandInfo, setNewDemandInfo )
21 import Type ( Type, tidyType, tidyTyVarBndr, substTy, mkTvSubst )
22 import Var ( Var, TyVar, varName )
24 import UniqFM ( lookupUFM )
25 import Name ( Name, getOccName )
26 import OccName ( tidyOccName )
27 import SrcLoc ( noSrcLoc )
28 import Maybes ( orElse )
30 import Util ( mapAccumL )
34 This module contains "tidying" code for *nested* expressions, bindings, rules.
35 The code for *top-level* bindings is in TidyPgm.
37 %************************************************************************
39 \subsection{Tidying expressions, rules}
41 %************************************************************************
46 -> (TidyEnv, CoreBind)
48 tidyBind env (NonRec bndr rhs)
49 = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') ->
50 (env', NonRec bndr' (tidyExpr env' rhs))
52 tidyBind env (Rec prs)
53 = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') ->
54 map (tidyExpr env') (map snd prs) =: \ rhss' ->
55 (env', Rec (zip bndrs' rhss'))
58 ------------ Expressions --------------
59 tidyExpr :: TidyEnv -> CoreExpr -> CoreExpr
60 tidyExpr env (Var v) = Var (tidyVarOcc env v)
61 tidyExpr env (Type ty) = Type (tidyType env ty)
62 tidyExpr env (Lit lit) = Lit lit
63 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
64 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
66 tidyExpr env (Let b e)
67 = tidyBind env b =: \ (env', b') ->
68 Let b' (tidyExpr env' e)
70 tidyExpr env (Case e b ty alts)
71 = tidyBndr env b =: \ (env', b) ->
72 Case (tidyExpr env e) b (tidyType env ty)
73 (map (tidyAlt b env') alts)
75 tidyExpr env (Lam b e)
76 = tidyBndr env b =: \ (env', b) ->
77 Lam b (tidyExpr env' e)
79 ------------ Case alternatives --------------
80 tidyAlt case_bndr env (DataAlt con, vs, rhs)
81 | not (isVanillaDataCon con) -- GADT case
82 = tidyBndrs env tvs =: \ (env1, tvs') ->
84 env2 = refineTidyEnv env con tvs' scrut_ty
86 tidyBndrs env2 ids =: \ (env3, ids') ->
87 (DataAlt con, tvs' ++ ids', tidyExpr env3 rhs)
89 (tvs, ids) = span isTyVar vs
90 scrut_ty = idType case_bndr
92 tidyAlt case_bndr env (con, vs, rhs)
93 = tidyBndrs env vs =: \ (env', vs) ->
94 (con, vs, tidyExpr env' rhs)
96 refineTidyEnv :: TidyEnv -> DataCon -> [TyVar] -> Type -> TidyEnv
97 -- Refine the TidyEnv in the light of the type refinement from coreRefineTys
98 refineTidyEnv tidy_env@(occ_env, var_env) con tvs scrut_ty
99 = case coreRefineTys in_scope con tvs scrut_ty of
101 Just (tv_subst, all_bound_here)
102 | all_bound_here -- Local type refinement only
104 | otherwise -- Apply the refining subst to the tidy env
105 -- This ensures that occurences have the most refined type
106 -- And that means that exprType will work right everywhere
107 -> (occ_env, mapVarEnv (refine subst) var_env)
109 subst = mkTvSubst in_scope tv_subst
111 refine subst var | isId var = setIdType var (substTy subst (idType var))
114 in_scope = mkInScopeSet var_env -- Seldom used
116 ------------ Notes --------------
117 tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
118 tidyNote env note = note
120 ------------ Rules --------------
121 tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule]
122 tidyRules env [] = []
123 tidyRules env (rule : rules)
124 = tidyRule env rule =: \ rule ->
125 tidyRules env rules =: \ rules ->
128 tidyRule :: TidyEnv -> CoreRule -> CoreRule
129 tidyRule env rule@(BuiltinRule {}) = rule
130 tidyRule env rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs,
131 ru_fn = fn, ru_rough = mb_ns })
132 = tidyBndrs env bndrs =: \ (env', bndrs) ->
133 map (tidyExpr env') args =: \ args ->
134 rule { ru_bndrs = bndrs, ru_args = args,
135 ru_rhs = tidyExpr env' rhs,
136 ru_fn = tidyNameOcc env fn,
137 ru_rough = map (fmap (tidyNameOcc env')) mb_ns }
141 %************************************************************************
143 \subsection{Tidying non-top-level binders}
145 %************************************************************************
148 tidyNameOcc :: TidyEnv -> Name -> Name
149 -- In rules and instances, we have Names, and we must tidy them too
150 -- Fortunately, we can lookup in the VarEnv with a name
151 tidyNameOcc (_, var_env) n = case lookupUFM var_env n of
155 tidyVarOcc :: TidyEnv -> Var -> Var
156 tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
158 -- tidyBndr is used for lambda and case binders
159 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
161 | isTyVar var = tidyTyVarBndr env var
162 | otherwise = tidyIdBndr env var
164 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
165 tidyBndrs env vars = mapAccumL tidyBndr env vars
167 tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
168 -- Used for local (non-top-level) let(rec)s
169 tidyLetBndr env (id,rhs)
170 = ((tidy_env,new_var_env), final_id)
172 ((tidy_env,var_env), new_id) = tidyIdBndr env id
174 -- We need to keep around any interesting strictness and
175 -- demand info because later on we may need to use it when
176 -- converting to A-normal form.
178 -- f (g x), where f is strict in its argument, will be converted
179 -- into case (g x) of z -> f z by CorePrep, but only if f still
180 -- has its strictness info.
182 -- Similarly for the demand info - on a let binder, this tells
183 -- CorePrep to turn the let into a case.
185 -- Similarly arity info for eta expansion in CorePrep
187 final_id = new_id `setIdInfo` new_info
189 new_info = vanillaIdInfo
190 `setArityInfo` exprArity rhs
191 `setAllStrictnessInfo` newStrictnessInfo idinfo
192 `setNewDemandInfo` newDemandInfo idinfo
194 -- Override the env we get back from tidyId with the new IdInfo
195 -- so it gets propagated to the usage sites.
196 new_var_env = extendVarEnv var_env id final_id
198 -- Non-top-level variables
199 tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
200 tidyIdBndr env@(tidy_env, var_env) id
201 = -- do this pattern match strictly, otherwise we end up holding on to
202 -- stuff in the OccName.
203 case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') ->
205 -- Give the Id a fresh print-name, *and* rename its type
206 -- The SrcLoc isn't important now,
207 -- though we could extract it from the Id
209 -- All nested Ids now have the same IdInfo, namely vanillaIdInfo,
210 -- which should save some space.
211 -- But note that tidyLetBndr puts some of it back.
212 ty' = tidyType env (idType id)
213 id' = mkUserLocal occ' (idUnique id) ty' noSrcLoc
214 `setIdInfo` vanillaIdInfo
215 var_env' = extendVarEnv var_env id id'
217 ((tidy_env', var_env'), id')