2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
8 Expr(..), Alt, Bind(..), Arg(..), Note(..),
9 CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
10 TaggedExpr, TaggedAlt, TaggedBind, TaggedArg,
13 mkApps, mkTyApps, mkValApps, mkVarApps,
14 mkLit, mkStringLit, mkConApp, mkPrimApp, mkNote,
15 bindNonRec, mkIfThenElse, varToCoreExpr,
17 bindersOf, bindersOfBinds, rhssOfBind, rhssOfAlts, isDeadBinder, isTyVar, isId,
18 collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
19 collectArgs, collectBindersIgnoringNotes,
23 isValArg, isTypeArg, valArgCount, valBndrCount,
25 -- Annotated expressions
26 AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt, deAnnotate,
29 CoreRules(..), -- Representation needed by friends
30 CoreRule(..), -- CoreSubst, CoreTidy, CoreFVs, PprCore only
31 emptyCoreRules, isEmptyCoreRules, rulesRhsFreeVars, rulesRules
34 #include "HsVersions.h"
36 import TysWiredIn ( boolTy, stringTy, nilDataCon )
37 import CostCentre ( CostCentre, isDupdCC, noCostCentre )
38 import Var ( Var, Id, TyVar, IdOrTyVar, isTyVar, isId, idType )
40 import Id ( mkWildId, getInlinePragma )
41 import Type ( Type, UsageAnn, mkTyVarTy, isUnLiftedType )
42 import IdInfo ( InlinePragInfo(..) )
43 import Const ( Con(..), DataCon, Literal(NoRepStr), PrimOp )
44 import TysWiredIn ( trueDataCon, falseDataCon )
49 %************************************************************************
51 \subsection{The main data types}
53 %************************************************************************
55 These data types are the heart of the compiler
58 infixl 8 `App` -- App brackets to the left
60 data Expr b -- "b" for the type of binders,
62 | Con Con [Arg b] -- Guaranteed saturated
63 -- The Con can be a DataCon, Literal, PrimOP
64 -- but cannot be DEFAULT
65 | App (Expr b) (Arg b)
67 | Let (Bind b) (Expr b)
68 | Case (Expr b) b [Alt b] -- Binder gets bound to value of scrutinee
69 -- DEFAULT case must be last, if it occurs at all
71 | Type Type -- This should only show up at the top
74 type Arg b = Expr b -- Can be a Type
76 type Alt b = (Con, [b], Expr b)
77 -- (DEFAULT, [], rhs) is the default alternative
78 -- The Con can be a Literal, DataCon, or DEFAULT, but cannot be PrimOp
80 data Bind b = NonRec b (Expr b)
87 Type -- The to-type: type of whole coerce expression
88 Type -- The from-type: type of enclosed expression
90 | InlineCall -- Instructs simplifier to inline
93 | InlineMe -- Instructs simplifer to treat the enclosed expression
94 -- as very small, and inline it at its call sites
96 | TermUsg -- A term-level usage annotation
97 UsageAnn -- (should not be a variable except during UsageSP inference)
101 %************************************************************************
103 \subsection{Transformation rules}
105 %************************************************************************
107 The CoreRule type and its friends are dealt with mainly in CoreRules,
108 but CoreFVs, Subst, PprCore, CoreTidy also inspect the representation.
113 IdOrTyVarSet -- Locally-defined free vars of RHSs
116 = Rule FAST_STRING -- Rule name
117 [CoreBndr] -- Forall'd variables
118 [CoreExpr] -- LHS args
121 emptyCoreRules :: CoreRules
122 emptyCoreRules = Rules [] emptyVarSet
124 isEmptyCoreRules :: CoreRules -> Bool
125 isEmptyCoreRules (Rules rs _) = null rs
127 rulesRhsFreeVars :: CoreRules -> IdOrTyVarSet
128 rulesRhsFreeVars (Rules _ fvs) = fvs
130 rulesRules :: CoreRules -> [CoreRule]
131 rulesRules (Rules rules _) = rules
135 %************************************************************************
137 \subsection{Useful synonyms}
139 %************************************************************************
144 type CoreBndr = IdOrTyVar
145 type CoreExpr = Expr CoreBndr
146 type CoreArg = Arg CoreBndr
147 type CoreBind = Bind CoreBndr
148 type CoreAlt = Alt CoreBndr
152 Binders are ``tagged'' with a \tr{t}:
155 type Tagged t = (CoreBndr, t)
157 type TaggedBind t = Bind (Tagged t)
158 type TaggedExpr t = Expr (Tagged t)
159 type TaggedArg t = Arg (Tagged t)
160 type TaggedAlt t = Alt (Tagged t)
164 %************************************************************************
166 \subsection{Core-constructing functions with checking}
168 %************************************************************************
171 mkApps :: Expr b -> [Arg b] -> Expr b
172 mkTyApps :: Expr b -> [Type] -> Expr b
173 mkValApps :: Expr b -> [Expr b] -> Expr b
174 mkVarApps :: CoreExpr -> [IdOrTyVar] -> CoreExpr
176 mkApps f args = foldl App f args
177 mkTyApps f args = foldl (\ e a -> App e (Type a)) f args
178 mkValApps f args = foldl (\ e a -> App e a) f args
179 mkVarApps f vars = foldl (\ e a -> App e (varToCoreExpr a)) f vars
181 mkLit :: Literal -> Expr b
182 mkStringLit :: String -> Expr b
183 mkConApp :: DataCon -> [Arg b] -> Expr b
184 mkPrimApp :: PrimOp -> [Arg b] -> Expr b
186 mkLit lit = Con (Literal lit) []
187 mkStringLit str = Con (Literal (NoRepStr (_PK_ str) stringTy)) []
188 mkConApp con args = Con (DataCon con) args
189 mkPrimApp op args = Con (PrimOp op) args
191 varToCoreExpr :: CoreBndr -> CoreExpr
192 varToCoreExpr v | isId v = Var v
193 | otherwise = Type (mkTyVarTy v)
197 mkLams :: [b] -> Expr b -> Expr b
198 mkLams binders body = foldr Lam body binders
202 mkLets :: [Bind b] -> Expr b -> Expr b
203 mkLets binds body = foldr Let body binds
205 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
206 -- (bindNonRec x r b) produces either
209 -- case r of x { _DEFAULT_ -> b }
211 -- depending on whether x is unlifted or not
212 -- It's used by the desugarer to avoid building bindings
213 -- that give Core Lint a heart attack. Actually the simplifier
214 -- deals with them perfectly well.
215 bindNonRec bndr rhs body
216 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
217 | otherwise = Let (NonRec bndr rhs) body
219 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
220 mkIfThenElse guard then_expr else_expr
221 = Case guard (mkWildId boolTy)
222 [ (DataCon trueDataCon, [], then_expr),
223 (DataCon falseDataCon, [], else_expr) ]
226 mkNote removes redundant coercions, and SCCs where possible
229 mkNote :: Note -> Expr b -> Expr b
230 mkNote (Coerce to_ty1 from_ty1) (Note (Coerce to_ty2 from_ty2) expr)
231 = ASSERT( from_ty1 == to_ty2 )
232 mkNote (Coerce to_ty1 from_ty2) expr
234 mkNote (SCC cc1) expr@(Note (SCC cc2) _)
235 | isDupdCC cc1 -- Discard the outer SCC provided we don't need
236 = expr -- to track its entry count
238 mkNote note@(SCC cc1) expr@(Lam x e) -- Move _scc_ inside lambda
239 = Lam x (mkNote note e)
241 -- Drop trivial InlineMe's
242 mkNote InlineMe expr@(Con _ _) = expr
243 mkNote InlineMe expr@(Var v) = expr
245 -- Slide InlineCall in around the function
246 -- No longer necessary I think (SLPJ Apr 99)
247 -- mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
248 -- mkNote InlineCall (Var v) = Note InlineCall (Var v)
249 -- mkNote InlineCall expr = expr
251 mkNote note expr = Note note expr
254 %************************************************************************
256 \subsection{Simple access functions}
258 %************************************************************************
261 bindersOf :: Bind b -> [b]
262 bindersOf (NonRec binder _) = [binder]
263 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
265 bindersOfBinds :: [Bind b] -> [b]
266 bindersOfBinds binds = foldr ((++) . bindersOf) [] binds
268 rhssOfBind :: Bind b -> [Expr b]
269 rhssOfBind (NonRec _ rhs) = [rhs]
270 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
272 rhssOfAlts :: [Alt b] -> [Expr b]
273 rhssOfAlts alts = [e | (_,_,e) <- alts]
275 isDeadBinder :: CoreBndr -> Bool
276 isDeadBinder bndr | isId bndr = case getInlinePragma bndr of
279 | otherwise = False -- TyVars count as not dead
281 flattenBinds :: [Bind b] -> [(b, Expr b)] -- Get all the lhs/rhs pairs
282 flattenBinds (NonRec b r : binds) = (b,r) : flattenBinds binds
283 flattenBinds (Rec prs1 : binds) = prs1 ++ flattenBinds binds
287 We often want to strip off leading lambdas before getting down to
288 business. @collectBinders@ is your friend.
290 We expect (by convention) type-, and value- lambdas in that
294 collectBinders :: Expr b -> ([b], Expr b)
295 collectBindersIgnoringNotes :: Expr b -> ([b], Expr b)
296 collectTyBinders :: CoreExpr -> ([TyVar], CoreExpr)
297 collectValBinders :: CoreExpr -> ([Id], CoreExpr)
298 collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
303 go bs (Lam b e) = go (b:bs) e
304 go bs e = (reverse bs, e)
306 -- This one ignores notes. It's used in CoreUnfold and StrAnal
307 -- when we aren't going to put the expression back together from
308 -- the pieces, so we don't mind losing the Notes
309 collectBindersIgnoringNotes expr
312 go bs (Lam b e) = go (b:bs) e
313 go bs (Note _ e) = go bs e
314 go bs e = (reverse bs, e)
316 collectTyAndValBinders expr
319 (tvs, body1) = collectTyBinders expr
320 (ids, body) = collectValBinders body1
322 collectTyBinders expr
325 go tvs (Lam b e) | isTyVar b = go (b:tvs) e
326 go tvs e = (reverse tvs, e)
328 collectValBinders expr
331 go ids (Lam b e) | isId b = go (b:ids) e
332 go ids body = (reverse ids, body)
336 @collectArgs@ takes an application expression, returning the function
337 and the arguments to which it is applied.
340 collectArgs :: Expr b -> (Expr b, [Arg b])
344 go (App f a) as = go f (a:as)
348 coreExprCc gets the cost centre enclosing an expression, if any.
349 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
352 coreExprCc :: Expr b -> CostCentre
353 coreExprCc (Note (SCC cc) e) = cc
354 coreExprCc (Note other_note e) = coreExprCc e
355 coreExprCc (Lam _ e) = coreExprCc e
356 coreExprCc other = noCostCentre
360 %************************************************************************
362 \subsection{Predicates}
364 %************************************************************************
367 isValArg (Type _) = False
368 isValArg other = True
370 isTypeArg (Type _) = True
371 isTypeArg other = False
373 valBndrCount :: [CoreBndr] -> Int
375 valBndrCount (b : bs) | isId b = 1 + valBndrCount bs
376 | otherwise = valBndrCount bs
378 valArgCount :: [Arg b] -> Int
380 valArgCount (Type _ : args) = valArgCount args
381 valArgCount (other : args) = 1 + valArgCount args
385 %************************************************************************
387 \subsection{Annotated core; annotation at every node in the tree}
389 %************************************************************************
392 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
394 data AnnExpr' bndr annot
396 | AnnCon Con [AnnExpr bndr annot]
397 | AnnLam bndr (AnnExpr bndr annot)
398 | AnnApp (AnnExpr bndr annot) (AnnExpr bndr annot)
399 | AnnCase (AnnExpr bndr annot) bndr [AnnAlt bndr annot]
400 | AnnLet (AnnBind bndr annot) (AnnExpr bndr annot)
401 | AnnNote Note (AnnExpr bndr annot)
404 type AnnAlt bndr annot = (Con, [bndr], AnnExpr bndr annot)
406 data AnnBind bndr annot
407 = AnnNonRec bndr (AnnExpr bndr annot)
408 | AnnRec [(bndr, AnnExpr bndr annot)]
412 deAnnotate :: AnnExpr bndr annot -> Expr bndr
414 deAnnotate (_, AnnType t) = Type t
415 deAnnotate (_, AnnVar v) = Var v
416 deAnnotate (_, AnnCon con args) = Con con (map deAnnotate args)
417 deAnnotate (_, AnnLam binder body)= Lam binder (deAnnotate body)
418 deAnnotate (_, AnnApp fun arg) = App (deAnnotate fun) (deAnnotate arg)
419 deAnnotate (_, AnnNote note body) = Note note (deAnnotate body)
421 deAnnotate (_, AnnLet bind body)
422 = Let (deAnnBind bind) (deAnnotate body)
424 deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
425 deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
427 deAnnotate (_, AnnCase scrut v alts)
428 = Case (deAnnotate scrut) v (map deAnnAlt alts)
430 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)