[project @ 2003-01-24 11:26:39 by simonpj]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreTidy.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
7 %*                                                                      *
8 %************************************************************************
9
10 \begin{code}
11 module CoreTidy (
12         tidyBind, tidyExpr, 
13         tidyBndr, tidyBndrs, tidyVarOcc,
14         tidyIdRules, pprTidyIdRules
15     ) where
16
17 #include "HsVersions.h"
18
19 import CoreSyn
20 import CoreUtils        ( exprArity )
21 import PprCore          ( pprIdRules )
22 import Id               ( Id, mkUserLocal, idInfo, setIdInfo, idUnique, idType, idCoreRules )
23 import IdInfo           ( vanillaIdInfo, setArityInfo, 
24                           newStrictnessInfo, setAllStrictnessInfo,
25                           newDemandInfo, setNewDemandInfo )
26 import Type             ( tidyType, tidyTyVarBndr )
27 import Var              ( Var )
28 import VarEnv
29 import Name             ( getOccName )
30 import OccName          ( tidyOccName )
31 import SrcLoc           ( noSrcLoc )
32 import Maybes           ( orElse )
33 import Outputable
34 import Util             ( mapAccumL )
35 \end{code}
36
37
38 This module contains "tidying" code for *nested* expressions, bindings, rules.
39 The code for *top-level* bindings is in TidyPgm.
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Tidying expressions, rules}
44 %*                                                                      *
45 %************************************************************************
46
47 \begin{code}
48 tidyBind :: TidyEnv
49          -> CoreBind
50          ->  (TidyEnv, CoreBind)
51
52 tidyBind env (NonRec bndr rhs)
53   = tidyLetBndr env (bndr,rhs)          =: \ (env', bndr') ->
54     (env', NonRec bndr' (tidyExpr env' rhs))
55
56 tidyBind env (Rec prs)
57   = mapAccumL tidyLetBndr env prs       =: \ (env', bndrs') ->
58     map (tidyExpr env') (map snd prs)   =: \ rhss' ->
59     (env', Rec (zip bndrs' rhss'))
60
61
62 ------------  Expressions  --------------
63 tidyExpr env (Var v)    =  Var (tidyVarOcc env v)
64 tidyExpr env (Type ty)  =  Type (tidyType env ty)
65 tidyExpr env (Lit lit)  =  Lit lit
66 tidyExpr env (App f a)  =  App (tidyExpr env f) (tidyExpr env a)
67 tidyExpr env (Note n e) =  Note (tidyNote env n) (tidyExpr env e)
68
69 tidyExpr env (Let b e) 
70   = tidyBind env b      =: \ (env', b') ->
71     Let b' (tidyExpr env' e)
72
73 tidyExpr env (Case e b alts)
74   = tidyBndr env b      =: \ (env', b) ->
75     Case (tidyExpr env e) b (map (tidyAlt env') alts)
76
77 tidyExpr env (Lam b e)
78   = tidyBndr env b      =: \ (env', b) ->
79     Lam b (tidyExpr env' e)
80
81 ------------  Case alternatives  --------------
82 tidyAlt env (con, vs, rhs)
83   = tidyBndrs env vs    =: \ (env', vs) ->
84     (con, vs, tidyExpr env' rhs)
85
86 ------------  Notes  --------------
87 tidyNote env (Coerce t1 t2)  = Coerce (tidyType env t1) (tidyType env t2)
88 tidyNote env note            = note
89
90
91 ------------  Rules  --------------
92 tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule]
93 tidyIdRules env [] = []
94 tidyIdRules env ((fn,rule) : rules)
95   = tidyRule env rule           =: \ rule ->
96     tidyIdRules env rules       =: \ rules ->
97      ((tidyVarOcc env fn, rule) : rules)
98
99 tidyRule :: TidyEnv -> CoreRule -> CoreRule
100 tidyRule env rule@(BuiltinRule _ _) = rule
101 tidyRule env (Rule name act vars tpl_args rhs)
102   = tidyBndrs env vars                  =: \ (env', vars) ->
103     map (tidyExpr env') tpl_args        =: \ tpl_args ->
104      (Rule name act vars tpl_args (tidyExpr env' rhs))
105
106 pprTidyIdRules :: Id -> SDoc
107 pprTidyIdRules id = pprIdRules (tidyIdRules emptyTidyEnv (idCoreRules id))
108 \end{code}
109
110
111 %************************************************************************
112 %*                                                                      *
113 \subsection{Tidying non-top-level binders}
114 %*                                                                      *
115 %************************************************************************
116
117 \begin{code}
118 tidyVarOcc :: TidyEnv -> Var -> Var
119 tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
120
121 -- tidyBndr is used for lambda and case binders
122 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
123 tidyBndr env var
124   | isTyVar var = tidyTyVarBndr env var
125   | otherwise   = tidyIdBndr env var
126
127 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
128 tidyBndrs env vars = mapAccumL tidyBndr env vars
129
130 tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
131 -- Used for local (non-top-level) let(rec)s
132 tidyLetBndr env (id,rhs) 
133   = ((tidy_env,new_var_env), final_id)
134   where
135     ((tidy_env,var_env), new_id) = tidyIdBndr env id
136
137         -- We need to keep around any interesting strictness and demand info
138         -- because later on we may need to use it when converting to A-normal form.
139         -- eg.
140         --      f (g x),  where f is strict in its argument, will be converted
141         --      into  case (g x) of z -> f z  by CorePrep, but only if f still
142         --      has its strictness info.
143         --
144         -- Similarly for the demand info - on a let binder, this tells 
145         -- CorePrep to turn the let into a case.
146         --
147         -- Similarly arity info for eta expansion in CorePrep
148     final_id = new_id `setIdInfo` new_info
149     idinfo   = idInfo id
150     new_info = vanillaIdInfo 
151                 `setArityInfo`          exprArity rhs
152                 `setAllStrictnessInfo`  newStrictnessInfo idinfo
153                 `setNewDemandInfo`      newDemandInfo idinfo
154
155     -- Override the env we get back from tidyId with the new IdInfo
156     -- so it gets propagated to the usage sites.
157     new_var_env = extendVarEnv var_env id final_id
158
159 -- Non-top-level variables
160 tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
161 tidyIdBndr env@(tidy_env, var_env) id
162   = -- do this pattern match strictly, otherwise we end up holding on to
163     -- stuff in the OccName.
164     case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') -> 
165     let 
166         -- Give the Id a fresh print-name, *and* rename its type
167         -- The SrcLoc isn't important now, 
168         -- though we could extract it from the Id
169         -- 
170         -- All nested Ids now have the same IdInfo, namely none,
171         -- which should save some space.
172         -- But note that tidyLetBndr puts some of it back.
173         ty'               = tidyType env (idType id)
174         id'               = mkUserLocal occ' (idUnique id) ty' noSrcLoc
175         var_env'          = extendVarEnv var_env id id'
176     in
177      ((tidy_env', var_env'), id')
178    }
179 \end{code}
180
181 \begin{code}
182 m =: k = m `seq` k m
183 \end{code}
184
185