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