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,
12 mkLets, mkLetBinds, mkLams,
13 mkApps, mkTyApps, mkValApps,
14 mkLit, mkStringLit, mkConApp, mkPrimApp, mkNote, mkNilExpr,
15 bindNonRec, mkIfThenElse, varToCoreExpr,
17 bindersOf, rhssOfBind, rhssOfAlts, isDeadBinder, isTyVar, isId,
18 collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
22 isValArg, isTypeArg, valArgCount,
24 -- Annotated expressions
25 AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt, deAnnotate
28 #include "HsVersions.h"
30 import TysWiredIn ( boolTy, stringTy, nilDataCon )
31 import CostCentre ( CostCentre, isDupdCC, noCostCentre )
32 import Var ( Var, Id, TyVar, IdOrTyVar, isTyVar, isId, idType )
33 import Id ( mkWildId, getInlinePragma )
34 import Type ( Type, UsageAnn, mkTyVarTy, isUnLiftedType )
35 import IdInfo ( InlinePragInfo(..) )
36 import Const ( Con(..), DataCon, Literal(NoRepStr), PrimOp )
37 import TysWiredIn ( trueDataCon, falseDataCon )
41 %************************************************************************
43 \subsection{The main data types}
45 %************************************************************************
47 These data types are the heart of the compiler
50 data Expr b -- "b" for the type of binders,
52 | Con Con [Arg b] -- Guaranteed saturated
53 -- The Con can be a DataCon, Literal, PrimOP
54 -- but cannot be DEFAULT
55 | App (Expr b) (Arg b)
57 | Let (Bind b) (Expr b)
58 | Case (Expr b) b [Alt b] -- Binder gets bound to value of scrutinee
59 -- DEFAULT case must be last, if it occurs at all
61 | Type Type -- This should only show up at the top
64 type Arg b = Expr b -- Can be a Type
66 type Alt b = (Con, [b], Expr b)
67 -- (DEFAULT, [], rhs) is the default alternative
68 -- The Con can be a Literal, DataCon, or DEFAULT, but cannot be PrimOp
70 data Bind b = NonRec b (Expr b)
77 Type -- The to-type: type of whole coerce expression
78 Type -- The from-type: type of enclosed expression
80 | InlineCall -- Instructs simplifier to inline
83 | TermUsg -- A term-level usage annotation
84 UsageAnn -- (should not be a variable except during UsageSP inference)
88 %************************************************************************
90 \subsection{Useful synonyms}
92 %************************************************************************
97 type CoreBndr = IdOrTyVar
98 type CoreExpr = Expr CoreBndr
99 type CoreArg = Arg CoreBndr
100 type CoreBind = Bind CoreBndr
101 type CoreAlt = Alt CoreBndr
105 Binders are ``tagged'' with a \tr{t}:
108 type Tagged t = (CoreBndr, t)
110 type TaggedBind t = Bind (Tagged t)
111 type TaggedExpr t = Expr (Tagged t)
112 type TaggedArg t = Arg (Tagged t)
113 type TaggedAlt t = Alt (Tagged t)
117 %************************************************************************
119 \subsection{Core-constructing functions with checking}
121 %************************************************************************
124 mkApps :: Expr b -> [Arg b] -> Expr b
125 mkTyApps :: Expr b -> [Type] -> Expr b
126 mkValApps :: Expr b -> [Expr b] -> Expr b
128 mkApps f args = foldl App f args
129 mkTyApps f args = foldl (\ e a -> App e (Type a)) f args
130 mkValApps f args = foldl (\ e a -> App e a) f args
132 mkLit :: Literal -> Expr b
133 mkStringLit :: String -> Expr b
134 mkConApp :: DataCon -> [Arg b] -> Expr b
135 mkPrimApp :: PrimOp -> [Arg b] -> Expr b
137 mkLit lit = Con (Literal lit) []
138 mkStringLit str = Con (Literal (NoRepStr (_PK_ str) stringTy)) []
139 mkConApp con args = Con (DataCon con) args
140 mkPrimApp op args = Con (PrimOp op) args
142 mkNilExpr :: Type -> CoreExpr
143 mkNilExpr ty = Con (DataCon nilDataCon) [Type ty]
145 varToCoreExpr :: CoreBndr -> CoreExpr
146 varToCoreExpr v | isId v = Var v
147 | otherwise = Type (mkTyVarTy v)
151 mkLams :: [b] -> Expr b -> Expr b
152 mkLams binders body = foldr Lam body binders
156 mkLets :: [Bind b] -> Expr b -> Expr b
157 mkLets binds body = foldr Let body binds
159 mkLetBinds :: [CoreBind] -> CoreExpr -> CoreExpr
160 -- mkLetBinds is like mkLets, but it uses bindNonRec to
161 -- make a case binding for unlifted things
162 mkLetBinds [] body = body
163 mkLetBinds (NonRec b r : binds) body = bindNonRec b r (mkLetBinds binds body)
164 mkLetBinds (bind : binds) body = Let bind (mkLetBinds binds body)
166 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
167 -- (bindNonRec x r b) produces either
170 -- case r of x { _DEFAULT_ -> b }
172 -- depending on whether x is unlifted or not
173 bindNonRec bndr rhs body
174 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
175 | otherwise = Let (NonRec bndr rhs) body
177 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
178 mkIfThenElse guard then_expr else_expr
179 = Case guard (mkWildId boolTy)
180 [ (DataCon trueDataCon, [], then_expr),
181 (DataCon falseDataCon, [], else_expr) ]
184 mkNote removes redundant coercions, and SCCs where possible
187 mkNote :: Note -> Expr b -> Expr b
188 mkNote (Coerce to_ty1 from_ty1) (Note (Coerce to_ty2 from_ty2) expr)
189 = ASSERT( from_ty1 == to_ty2 )
190 mkNote (Coerce to_ty1 from_ty2) expr
192 mkNote (SCC cc1) expr@(Note (SCC cc2) _)
193 | isDupdCC cc1 -- Discard the outer SCC provided we don't need
194 = expr -- to track its entry count
196 mkNote note@(SCC cc1) expr@(Lam x e) -- Move _scc_ inside lambda
197 = Lam x (mkNote note e)
199 -- Slide InlineCall in around the function
200 mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
201 mkNote InlineCall (Var v) = Note InlineCall (Var v)
202 mkNote InlineCall expr = expr
204 mkNote note expr = Note note expr
207 %************************************************************************
209 \subsection{Simple access functions}
211 %************************************************************************
214 bindersOf :: Bind b -> [b]
215 bindersOf (NonRec binder _) = [binder]
216 bindersOf (Rec pairs) = [binder | (binder, _) <- pairs]
218 rhssOfBind :: Bind b -> [Expr b]
219 rhssOfBind (NonRec _ rhs) = [rhs]
220 rhssOfBind (Rec pairs) = [rhs | (_,rhs) <- pairs]
222 rhssOfAlts :: [Alt b] -> [Expr b]
223 rhssOfAlts alts = [e | (_,_,e) <- alts]
225 isDeadBinder :: CoreBndr -> Bool
226 isDeadBinder bndr | isId bndr = case getInlinePragma bndr of
229 | otherwise = False -- TyVars count as not dead
232 We often want to strip off leading lambdas before getting down to
233 business. @collectBinders@ is your friend.
235 We expect (by convention) type-, and value- lambdas in that
239 collectBinders :: Expr b -> ([b], Expr b)
240 collectTyBinders :: CoreExpr -> ([TyVar], CoreExpr)
241 collectValBinders :: CoreExpr -> ([Id], CoreExpr)
242 collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
244 collectTyAndValBinders expr
247 (tvs, body1) = collectTyBinders expr
248 (ids, body) = collectValBinders body1
253 go tvs (Lam b e) = go (b:tvs) e
254 go tvs e = (reverse tvs, e)
256 collectTyBinders expr
259 go tvs (Lam b e) | isTyVar b = go (b:tvs) e
260 go tvs e = (reverse tvs, e)
262 collectValBinders expr
265 go ids (Lam b e) | isId b = go (b:ids) e
266 go ids body = (reverse ids, body)
270 @collectArgs@ takes an application expression, returning the function
271 and the arguments to which it is applied.
274 collectArgs :: Expr b -> (Expr b, [Arg b])
278 go (App f a) as = go f (a:as)
282 coreExprCc gets the cost centre enclosing an expression, if any.
283 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
286 coreExprCc :: Expr b -> CostCentre
287 coreExprCc (Note (SCC cc) e) = cc
288 coreExprCc (Note other_note e) = coreExprCc e
289 coreExprCc (Lam _ e) = coreExprCc e
290 coreExprCc other = noCostCentre
294 %************************************************************************
296 \subsection{Predicates}
298 %************************************************************************
301 isValArg (Type _) = False
302 isValArg other = True
304 isTypeArg (Type _) = True
305 isTypeArg other = False
307 valArgCount :: [Arg b] -> Int
309 valArgCount (Type _ : args) = valArgCount args
310 valArgCount (other : args) = 1 + valArgCount args
314 %************************************************************************
316 \subsection{Annotated core; annotation at every node in the tree}
318 %************************************************************************
321 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
323 data AnnExpr' bndr annot
325 | AnnCon Con [AnnExpr bndr annot]
326 | AnnLam bndr (AnnExpr bndr annot)
327 | AnnApp (AnnExpr bndr annot) (AnnExpr bndr annot)
328 | AnnCase (AnnExpr bndr annot) bndr [AnnAlt bndr annot]
329 | AnnLet (AnnBind bndr annot) (AnnExpr bndr annot)
330 | AnnNote Note (AnnExpr bndr annot)
333 type AnnAlt bndr annot = (Con, [bndr], AnnExpr bndr annot)
335 data AnnBind bndr annot
336 = AnnNonRec bndr (AnnExpr bndr annot)
337 | AnnRec [(bndr, AnnExpr bndr annot)]
341 deAnnotate :: AnnExpr bndr annot -> Expr bndr
343 deAnnotate (_, AnnType t) = Type t
344 deAnnotate (_, AnnVar v) = Var v
345 deAnnotate (_, AnnCon con args) = Con con (map deAnnotate args)
346 deAnnotate (_, AnnLam binder body)= Lam binder (deAnnotate body)
347 deAnnotate (_, AnnApp fun arg) = App (deAnnotate fun) (deAnnotate arg)
348 deAnnotate (_, AnnNote note body) = Note note (deAnnotate body)
350 deAnnotate (_, AnnLet bind body)
351 = Let (deAnnBind bind) (deAnnotate body)
353 deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
354 deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
356 deAnnotate (_, AnnCase scrut v alts)
357 = Case (deAnnotate scrut) v (map deAnnAlt alts)
359 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)