0a2bd0d9d1c5abc97654ecf83bd56d51057944ec
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[CoreSyn]{A data type for the Haskell compiler midsection}
5
6 \begin{code}
7 module CoreSyn (
8         Expr(..), Alt, Bind(..), AltCon(..), Arg, Note(..),
9         CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
10         TaggedExpr, TaggedAlt, TaggedBind, TaggedArg, TaggedBndr(..),
11
12         mkLets, mkLams, 
13         mkApps, mkTyApps, mkValApps, mkVarApps,
14         mkLit, mkIntLitInt, mkIntLit, 
15         mkConApp, 
16         varToCoreExpr,
17
18         isTyVar, isId, cmpAltCon, cmpAlt, ltAlt,
19         bindersOf, bindersOfBinds, rhssOfBind, rhssOfAlts, 
20         collectBinders, collectTyBinders, collectValBinders, collectTyAndValBinders,
21         collectArgs, 
22         coreExprCc,
23         flattenBinds, 
24
25         isValArg, isTypeArg, valArgCount, valBndrCount, isRuntimeArg, isRuntimeVar,
26
27         -- Unfoldings
28         Unfolding(..),  UnfoldingGuidance(..),  -- Both abstract everywhere but in CoreUnfold.lhs
29         noUnfolding, evaldUnfolding, mkOtherCon,
30         unfoldingTemplate, maybeUnfoldingTemplate, otherCons, 
31         isValueUnfolding, isEvaldUnfolding, isCheapUnfolding, isCompulsoryUnfolding,
32         hasUnfolding, hasSomeUnfolding, neverUnfold,
33
34         -- Seq stuff
35         seqRules, seqExpr, seqExprs, seqUnfolding,
36
37         -- Annotated expressions
38         AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt, 
39         deAnnotate, deAnnotate', deAnnAlt, collectAnnBndrs,
40
41         -- Core rules
42         CoreRules(..),  -- Representation needed by friends
43         CoreRule(..),   -- CoreSubst, CoreTidy, CoreFVs, PprCore only
44         IdCoreRule(..), isOrphanRule,
45         RuleName,
46         emptyCoreRules, isEmptyCoreRules, rulesRhsFreeVars, rulesRules,
47         isBuiltinRule, ruleName
48     ) where
49
50 #include "HsVersions.h"
51
52 import CmdLineOpts      ( opt_RuntimeTypes )
53 import CostCentre       ( CostCentre, noCostCentre )
54 import Var              ( Var, Id, TyVar, isTyVar, isId )
55 import Type             ( Type, mkTyVarTy, seqType )
56 import Literal          ( Literal, mkMachInt )
57 import DataCon          ( DataCon, dataConWorkId, dataConTag )
58 import BasicTypes       ( Activation )
59 import VarSet
60 import FastString
61 import Outputable
62 \end{code}
63
64 %************************************************************************
65 %*                                                                      *
66 \subsection{The main data types}
67 %*                                                                      *
68 %************************************************************************
69
70 These data types are the heart of the compiler
71
72 \begin{code}
73 infixl 8 `App`  -- App brackets to the left
74
75 data Expr b     -- "b" for the type of binders, 
76   = Var   Id
77   | Lit   Literal
78   | App   (Expr b) (Arg b)
79   | Lam   b (Expr b)
80   | Let   (Bind b) (Expr b)
81   | Case  (Expr b) b Type [Alt b]       -- Binder gets bound to value of scrutinee
82         -- Invariant: The list of alternatives is ALWAYS EXHAUSTIVE,
83         --            meaning that it covers all cases that can occur
84         --            See the example below
85         --
86         -- Invariant: The DEFAULT case must be *first*, if it occurs at all
87         -- Invariant: The remaining cases are in order of increasing 
88         --              tag     (for DataAlts)
89         --              lit     (for LitAlts)
90         --            This makes finding the relevant constructor easy,
91         --            and makes comparison easier too
92   | Note  Note (Expr b)
93   | Type  Type                  -- This should only show up at the top
94                                 -- level of an Arg
95
96 -- An "exhausive" case does not necessarily mention all constructors:
97 --      data Foo = Red | Green | Blue
98 --
99 --      ...case x of 
100 --              Red   -> True
101 --              other -> f (case x of 
102 --                              Green -> ...
103 --                              Blue  -> ... )
104 -- The inner case does not need a Red alternative, because x can't be Red at
105 -- that program point.
106
107
108 type Arg b = Expr b             -- Can be a Type
109
110 type Alt b = (AltCon, [b], Expr b)      -- (DEFAULT, [], rhs) is the default alternative
111
112 data AltCon = DataAlt DataCon
113             | LitAlt  Literal
114             | DEFAULT
115          deriving (Eq, Ord)
116
117
118 data Bind b = NonRec b (Expr b)
119               | Rec [(b, (Expr b))]
120
121 data Note
122   = SCC CostCentre
123
124   | Coerce      
125         Type            -- The to-type:   type of whole coerce expression
126         Type            -- The from-type: type of enclosed expression
127
128   | InlineCall          -- Instructs simplifier to inline
129                         -- the enclosed call
130
131   | InlineMe            -- Instructs simplifer to treat the enclosed expression
132                         -- as very small, and inline it at its call sites
133
134   | CoreNote String     -- A generic core annotation, propagated but not used by GHC
135
136 -- NOTE: we also treat expressions wrapped in InlineMe as
137 -- 'cheap' and 'dupable' (in the sense of exprIsCheap, exprIsDupable)
138 -- What this means is that we obediently inline even things that don't
139 -- look like valuse.  This is sometimes important:
140 --      {-# INLINE f #-}
141 --      f = g . h
142 -- Here, f looks like a redex, and we aren't going to inline (.) because it's
143 -- inside an INLINE, so it'll stay looking like a redex.  Nevertheless, we 
144 -- should inline f even inside lambdas.  In effect, we should trust the programmer.
145 \end{code}
146
147 INVARIANTS:
148
149 * The RHS of a letrec, and the RHSs of all top-level lets,
150   must be of LIFTED type.
151
152 * The RHS of a let, may be of UNLIFTED type, but only if the expression 
153   is ok-for-speculation.  This means that the let can be floated around 
154   without difficulty.  e.g.
155         y::Int# = x +# 1#       ok
156         y::Int# = fac 4#        not ok [use case instead]
157
158 * The argument of an App can be of any type.
159
160 * The simplifier tries to ensure that if the RHS of a let is a constructor
161   application, its arguments are trivial, so that the constructor can be
162   inlined vigorously.
163
164
165 %************************************************************************
166 %*                                                                      *
167 \subsection{Transformation rules}
168 %*                                                                      *
169 %************************************************************************
170
171 The CoreRule type and its friends are dealt with mainly in CoreRules,
172 but CoreFVs, Subst, PprCore, CoreTidy also inspect the representation.
173
174 \begin{code}
175 data CoreRules 
176   = Rules [CoreRule]
177           VarSet                -- Locally-defined free vars of RHSs
178
179 emptyCoreRules :: CoreRules
180 emptyCoreRules = Rules [] emptyVarSet
181
182 isEmptyCoreRules :: CoreRules -> Bool
183 isEmptyCoreRules (Rules rs _) = null rs
184
185 rulesRhsFreeVars :: CoreRules -> VarSet
186 rulesRhsFreeVars (Rules _ fvs) = fvs
187
188 rulesRules :: CoreRules -> [CoreRule]
189 rulesRules (Rules rules _) = rules
190 \end{code}
191
192 \begin{code}
193 type RuleName = FastString
194 data IdCoreRule = IdCoreRule Id         -- A rule for this Id
195                              Bool       -- True <=> orphan rule
196                              CoreRule   -- The rule itself
197
198 isOrphanRule :: IdCoreRule -> Bool
199 isOrphanRule (IdCoreRule _ is_orphan _) = is_orphan
200
201 data CoreRule
202   = Rule RuleName
203          Activation     -- When the rule is active
204          [CoreBndr]     -- Forall'd variables
205          [CoreExpr]     -- LHS args
206          CoreExpr       -- RHS
207
208   | BuiltinRule         -- Built-in rules are used for constant folding
209         RuleName        -- and suchlike.  It has no free variables.
210         ([CoreExpr] -> Maybe CoreExpr)
211
212 isBuiltinRule (BuiltinRule _ _) = True
213 isBuiltinRule _                 = False
214
215 ruleName :: CoreRule -> RuleName
216 ruleName (Rule n _ _ _ _)  = n
217 ruleName (BuiltinRule n _) = n
218 \end{code}
219
220
221 %************************************************************************
222 %*                                                                      *
223 \subsection{@Unfolding@ type}
224 %*                                                                      *
225 %************************************************************************
226
227 The @Unfolding@ type is declared here to avoid numerous loops, but it
228 should be abstract everywhere except in CoreUnfold.lhs
229
230 \begin{code}
231 data Unfolding
232   = NoUnfolding
233
234   | OtherCon [AltCon]           -- It ain't one of these
235                                 -- (OtherCon xs) also indicates that something has been evaluated
236                                 -- and hence there's no point in re-evaluating it.
237                                 -- OtherCon [] is used even for non-data-type values
238                                 -- to indicated evaluated-ness.  Notably:
239                                 --      data C = C !(Int -> Int)
240                                 --      case x of { C f -> ... }
241                                 -- Here, f gets an OtherCon [] unfolding.
242
243   | CompulsoryUnfolding CoreExpr        -- There is no "original" definition,
244                                         -- so you'd better unfold.
245
246   | CoreUnfolding                       -- An unfolding with redundant cached information
247                 CoreExpr                -- Template; binder-info is correct
248                 Bool                    -- True <=> top level binding
249                 Bool                    -- exprIsValue template (cached); it is ok to discard a `seq` on
250                                         --      this variable
251                 Bool                    -- True <=> doesn't waste (much) work to expand inside an inlining
252                                         --      Basically it's exprIsCheap
253                 UnfoldingGuidance       -- Tells about the *size* of the template.
254
255
256 data UnfoldingGuidance
257   = UnfoldNever
258   | UnfoldIfGoodArgs    Int     -- and "n" value args
259
260                         [Int]   -- Discount if the argument is evaluated.
261                                 -- (i.e., a simplification will definitely
262                                 -- be possible).  One elt of the list per *value* arg.
263
264                         Int     -- The "size" of the unfolding; to be elaborated
265                                 -- later. ToDo
266
267                         Int     -- Scrutinee discount: the discount to substract if the thing is in
268                                 -- a context (case (thing args) of ...),
269                                 -- (where there are the right number of arguments.)
270
271 noUnfolding    = NoUnfolding
272 evaldUnfolding = OtherCon []
273
274 mkOtherCon = OtherCon
275
276 seqUnfolding :: Unfolding -> ()
277 seqUnfolding (CoreUnfolding e top b1 b2 g)
278   = seqExpr e `seq` top `seq` b1 `seq` b2 `seq` seqGuidance g
279 seqUnfolding other = ()
280
281 seqGuidance (UnfoldIfGoodArgs n ns a b) = n `seq` sum ns `seq` a `seq` b `seq` ()
282 seqGuidance other                       = ()
283 \end{code}
284
285 \begin{code}
286 unfoldingTemplate :: Unfolding -> CoreExpr
287 unfoldingTemplate (CoreUnfolding expr _ _ _ _) = expr
288 unfoldingTemplate (CompulsoryUnfolding expr)   = expr
289 unfoldingTemplate other = panic "getUnfoldingTemplate"
290
291 maybeUnfoldingTemplate :: Unfolding -> Maybe CoreExpr
292 maybeUnfoldingTemplate (CoreUnfolding expr _ _ _ _) = Just expr
293 maybeUnfoldingTemplate (CompulsoryUnfolding expr)   = Just expr
294 maybeUnfoldingTemplate other                        = Nothing
295
296 otherCons :: Unfolding -> [AltCon]
297 otherCons (OtherCon cons) = cons
298 otherCons other           = []
299
300 isValueUnfolding :: Unfolding -> Bool
301         -- Returns False for OtherCon
302 isValueUnfolding (CoreUnfolding _ _ is_evald _ _) = is_evald
303 isValueUnfolding other                            = False
304
305 isEvaldUnfolding :: Unfolding -> Bool
306         -- Returns True for OtherCon
307 isEvaldUnfolding (OtherCon _)                     = True
308 isEvaldUnfolding (CoreUnfolding _ _ is_evald _ _) = is_evald
309 isEvaldUnfolding other                            = False
310
311 isCheapUnfolding :: Unfolding -> Bool
312 isCheapUnfolding (CoreUnfolding _ _ _ is_cheap _) = is_cheap
313 isCheapUnfolding other                            = False
314
315 isCompulsoryUnfolding :: Unfolding -> Bool
316 isCompulsoryUnfolding (CompulsoryUnfolding _) = True
317 isCompulsoryUnfolding other                   = False
318
319 hasUnfolding :: Unfolding -> Bool
320 hasUnfolding (CoreUnfolding _ _ _ _ _) = True
321 hasUnfolding (CompulsoryUnfolding _)   = True
322 hasUnfolding other                     = False
323
324 hasSomeUnfolding :: Unfolding -> Bool
325 hasSomeUnfolding NoUnfolding = False
326 hasSomeUnfolding other       = True
327
328 neverUnfold :: Unfolding -> Bool
329 neverUnfold NoUnfolding                         = True
330 neverUnfold (OtherCon _)                        = True
331 neverUnfold (CoreUnfolding _ _ _ _ UnfoldNever) = True
332 neverUnfold other                               = False
333 \end{code}
334
335
336 %************************************************************************
337 %*                                                                      *
338 \subsection{The main data type}
339 %*                                                                      *
340 %************************************************************************
341
342 \begin{code}
343 -- The Ord is needed for the FiniteMap used in the lookForConstructor
344 -- in SimplEnv.  If you declared that lookForConstructor *ignores*
345 -- constructor-applications with LitArg args, then you could get
346 -- rid of this Ord.
347
348 instance Outputable AltCon where
349   ppr (DataAlt dc) = ppr dc
350   ppr (LitAlt lit) = ppr lit
351   ppr DEFAULT      = ptext SLIT("__DEFAULT")
352
353 instance Show AltCon where
354   showsPrec p con = showsPrecSDoc p (ppr con)
355
356 cmpAlt :: Alt b -> Alt b -> Ordering
357 cmpAlt (con1, _, _) (con2, _, _) = con1 `cmpAltCon` con2
358
359 ltAlt :: Alt b -> Alt b -> Bool
360 ltAlt a1 a2 = case a1 `cmpAlt` a2 of { LT -> True; other -> False }
361
362 cmpAltCon :: AltCon -> AltCon -> Ordering
363 -- Compares AltCons within a single list of alternatives
364 cmpAltCon DEFAULT      DEFAULT     = EQ
365 cmpAltCon DEFAULT      con         = LT
366
367 cmpAltCon (DataAlt d1) (DataAlt d2) = dataConTag d1 `compare` dataConTag d2
368 cmpAltCon (DataAlt _)  DEFAULT      = GT
369 cmpAltCon (LitAlt  l1) (LitAlt  l2) = l1 `compare` l2
370 cmpAltCon (LitAlt _)   DEFAULT      = GT
371
372 cmpAltCon con1 con2 = WARN( True, text "Comparing incomparable AltCons" <+> 
373                                   ppr con1 <+> ppr con2 )
374                       LT
375 \end{code}
376
377
378 %************************************************************************
379 %*                                                                      *
380 \subsection{Useful synonyms}
381 %*                                                                      *
382 %************************************************************************
383
384 The common case
385
386 \begin{code}
387 type CoreBndr = Var
388 type CoreExpr = Expr CoreBndr
389 type CoreArg  = Arg  CoreBndr
390 type CoreBind = Bind CoreBndr
391 type CoreAlt  = Alt  CoreBndr
392 \end{code}
393
394 Binders are ``tagged'' with a \tr{t}:
395
396 \begin{code}
397 data TaggedBndr t = TB CoreBndr t       -- TB for "tagged binder"
398
399 type TaggedBind t = Bind (TaggedBndr t)
400 type TaggedExpr t = Expr (TaggedBndr t)
401 type TaggedArg  t = Arg  (TaggedBndr t)
402 type TaggedAlt  t = Alt  (TaggedBndr t)
403
404 instance Outputable b => Outputable (TaggedBndr b) where
405   ppr (TB b l) = char '<' <> ppr b <> comma <> ppr l <> char '>'
406
407 instance Outputable b => OutputableBndr (TaggedBndr b) where
408   pprBndr _ b = ppr b   -- Simple
409 \end{code}
410
411
412 %************************************************************************
413 %*                                                                      *
414 \subsection{Core-constructing functions with checking}
415 %*                                                                      *
416 %************************************************************************
417
418 \begin{code}
419 mkApps    :: Expr b -> [Arg b]  -> Expr b
420 mkTyApps  :: Expr b -> [Type]   -> Expr b
421 mkValApps :: Expr b -> [Expr b] -> Expr b
422 mkVarApps :: Expr b -> [Var] -> Expr b
423
424 mkApps    f args = foldl App                       f args
425 mkTyApps  f args = foldl (\ e a -> App e (Type a)) f args
426 mkValApps f args = foldl (\ e a -> App e a)        f args
427 mkVarApps f vars = foldl (\ e a -> App e (varToCoreExpr a)) f vars
428
429 mkLit         :: Literal -> Expr b
430 mkIntLit      :: Integer -> Expr b
431 mkIntLitInt   :: Int     -> Expr b
432 mkConApp      :: DataCon -> [Arg b] -> Expr b
433 mkLets        :: [Bind b] -> Expr b -> Expr b
434 mkLams        :: [b] -> Expr b -> Expr b
435
436 mkLit lit         = Lit lit
437 mkConApp con args = mkApps (Var (dataConWorkId con)) args
438
439 mkLams binders body = foldr Lam body binders
440 mkLets binds body   = foldr Let body binds
441
442 mkIntLit    n = Lit (mkMachInt n)
443 mkIntLitInt n = Lit (mkMachInt (toInteger n))
444
445 varToCoreExpr :: CoreBndr -> Expr b
446 varToCoreExpr v | isId v    = Var v
447                 | otherwise = Type (mkTyVarTy v)
448 \end{code}
449
450
451 %************************************************************************
452 %*                                                                      *
453 \subsection{Simple access functions}
454 %*                                                                      *
455 %************************************************************************
456
457 \begin{code}
458 bindersOf  :: Bind b -> [b]
459 bindersOf (NonRec binder _) = [binder]
460 bindersOf (Rec pairs)       = [binder | (binder, _) <- pairs]
461
462 bindersOfBinds :: [Bind b] -> [b]
463 bindersOfBinds binds = foldr ((++) . bindersOf) [] binds
464
465 rhssOfBind :: Bind b -> [Expr b]
466 rhssOfBind (NonRec _ rhs) = [rhs]
467 rhssOfBind (Rec pairs)    = [rhs | (_,rhs) <- pairs]
468
469 rhssOfAlts :: [Alt b] -> [Expr b]
470 rhssOfAlts alts = [e | (_,_,e) <- alts]
471
472 flattenBinds :: [Bind b] -> [(b, Expr b)]       -- Get all the lhs/rhs pairs
473 flattenBinds (NonRec b r : binds) = (b,r) : flattenBinds binds
474 flattenBinds (Rec prs1   : binds) = prs1 ++ flattenBinds binds
475 flattenBinds []                   = []
476 \end{code}
477
478 We often want to strip off leading lambdas before getting down to
479 business.  @collectBinders@ is your friend.
480
481 We expect (by convention) type-, and value- lambdas in that
482 order.
483
484 \begin{code}
485 collectBinders               :: Expr b -> ([b],         Expr b)
486 collectTyBinders             :: CoreExpr -> ([TyVar],     CoreExpr)
487 collectValBinders            :: CoreExpr -> ([Id],        CoreExpr)
488 collectTyAndValBinders       :: CoreExpr -> ([TyVar], [Id], CoreExpr)
489
490 collectBinders expr
491   = go [] expr
492   where
493     go bs (Lam b e) = go (b:bs) e
494     go bs e          = (reverse bs, e)
495
496 collectTyAndValBinders expr
497   = (tvs, ids, body)
498   where
499     (tvs, body1) = collectTyBinders expr
500     (ids, body)  = collectValBinders body1
501
502 collectTyBinders expr
503   = go [] expr
504   where
505     go tvs (Lam b e) | isTyVar b = go (b:tvs) e
506     go tvs e                     = (reverse tvs, e)
507
508 collectValBinders expr
509   = go [] expr
510   where
511     go ids (Lam b e) | isId b = go (b:ids) e
512     go ids body               = (reverse ids, body)
513 \end{code}
514
515
516 @collectArgs@ takes an application expression, returning the function
517 and the arguments to which it is applied.
518
519 \begin{code}
520 collectArgs :: Expr b -> (Expr b, [Arg b])
521 collectArgs expr
522   = go expr []
523   where
524     go (App f a) as = go f (a:as)
525     go e         as = (e, as)
526 \end{code}
527
528 coreExprCc gets the cost centre enclosing an expression, if any.
529 It looks inside lambdas because (scc "foo" \x.e) = \x.scc "foo" e
530
531 \begin{code}
532 coreExprCc :: Expr b -> CostCentre
533 coreExprCc (Note (SCC cc) e)   = cc
534 coreExprCc (Note other_note e) = coreExprCc e
535 coreExprCc (Lam _ e)           = coreExprCc e
536 coreExprCc other               = noCostCentre
537 \end{code}
538
539
540
541 %************************************************************************
542 %*                                                                      *
543 \subsection{Predicates}
544 %*                                                                      *
545 %************************************************************************
546
547 @isRuntimeVar v@ returns if (Lam v _) really becomes a lambda at runtime,
548 i.e. if type applications are actual lambdas because types are kept around
549 at runtime.  
550
551 Similarly isRuntimeArg.  
552
553 \begin{code}
554 isRuntimeVar :: Var -> Bool
555 isRuntimeVar | opt_RuntimeTypes = \v -> True
556              | otherwise        = \v -> isId v
557
558 isRuntimeArg :: CoreExpr -> Bool
559 isRuntimeArg | opt_RuntimeTypes = \e -> True
560              | otherwise        = \e -> isValArg e
561 \end{code}
562
563 \begin{code}
564 isValArg (Type _) = False
565 isValArg other    = True
566
567 isTypeArg (Type _) = True
568 isTypeArg other    = False
569
570 valBndrCount :: [CoreBndr] -> Int
571 valBndrCount []                   = 0
572 valBndrCount (b : bs) | isId b    = 1 + valBndrCount bs
573                       | otherwise = valBndrCount bs
574
575 valArgCount :: [Arg b] -> Int
576 valArgCount []              = 0
577 valArgCount (Type _ : args) = valArgCount args
578 valArgCount (other  : args) = 1 + valArgCount args
579 \end{code}
580
581
582 %************************************************************************
583 %*                                                                      *
584 \subsection{Seq stuff}
585 %*                                                                      *
586 %************************************************************************
587
588 \begin{code}
589 seqExpr :: CoreExpr -> ()
590 seqExpr (Var v)         = v `seq` ()
591 seqExpr (Lit lit)       = lit `seq` ()
592 seqExpr (App f a)       = seqExpr f `seq` seqExpr a
593 seqExpr (Lam b e)       = seqBndr b `seq` seqExpr e
594 seqExpr (Let b e)       = seqBind b `seq` seqExpr e
595 -- gaw 2004
596 seqExpr (Case e b t as) = seqExpr e `seq` seqBndr b `seq` seqType t `seq` seqAlts as
597 seqExpr (Note n e)      = seqNote n `seq` seqExpr e
598 seqExpr (Type t)        = seqType t
599
600 seqExprs [] = ()
601 seqExprs (e:es) = seqExpr e `seq` seqExprs es
602
603 seqNote (Coerce t1 t2) = seqType t1 `seq` seqType t2
604 seqNote (CoreNote s)   = s `seq` ()
605 seqNote other          = ()
606
607 seqBndr b = b `seq` ()
608
609 seqBndrs [] = ()
610 seqBndrs (b:bs) = seqBndr b `seq` seqBndrs bs
611
612 seqBind (NonRec b e) = seqBndr b `seq` seqExpr e
613 seqBind (Rec prs)    = seqPairs prs
614
615 seqPairs [] = ()
616 seqPairs ((b,e):prs) = seqBndr b `seq` seqExpr e `seq` seqPairs prs
617
618 seqAlts [] = ()
619 seqAlts ((c,bs,e):alts) = seqBndrs bs `seq` seqExpr e `seq` seqAlts alts
620
621 seqRules :: CoreRules -> ()
622 seqRules (Rules rules fvs) = seq_rules rules `seq` seqVarSet fvs
623
624 seq_rules [] = ()
625 seq_rules (Rule fs _ bs es e : rules) = seqBndrs bs `seq` seqExprs (e:es) `seq` seq_rules rules
626 seq_rules (BuiltinRule _ _   : rules) = seq_rules rules
627 \end{code}
628
629
630
631 %************************************************************************
632 %*                                                                      *
633 \subsection{Annotated core; annotation at every node in the tree}
634 %*                                                                      *
635 %************************************************************************
636
637 \begin{code}
638 type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
639
640 data AnnExpr' bndr annot
641   = AnnVar      Id
642   | AnnLit      Literal
643   | AnnLam      bndr (AnnExpr bndr annot)
644   | AnnApp      (AnnExpr bndr annot) (AnnExpr bndr annot)
645 -- gaw 2004
646   | AnnCase     (AnnExpr bndr annot) bndr Type [AnnAlt bndr annot]
647   | AnnLet      (AnnBind bndr annot) (AnnExpr bndr annot)
648   | AnnNote     Note (AnnExpr bndr annot)
649   | AnnType     Type
650
651 type AnnAlt bndr annot = (AltCon, [bndr], AnnExpr bndr annot)
652
653 data AnnBind bndr annot
654   = AnnNonRec bndr (AnnExpr bndr annot)
655   | AnnRec    [(bndr, AnnExpr bndr annot)]
656 \end{code}
657
658 \begin{code}
659 deAnnotate :: AnnExpr bndr annot -> Expr bndr
660 deAnnotate (_, e) = deAnnotate' e
661
662 deAnnotate' (AnnType t)           = Type t
663 deAnnotate' (AnnVar  v)           = Var v
664 deAnnotate' (AnnLit  lit)         = Lit lit
665 deAnnotate' (AnnLam  binder body) = Lam binder (deAnnotate body)
666 deAnnotate' (AnnApp  fun arg)     = App (deAnnotate fun) (deAnnotate arg)
667 deAnnotate' (AnnNote note body)   = Note note (deAnnotate body)
668
669 deAnnotate' (AnnLet bind body)
670   = Let (deAnnBind bind) (deAnnotate body)
671   where
672     deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
673     deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
674
675 -- gaw 2004
676 deAnnotate' (AnnCase scrut v t alts)
677   = Case (deAnnotate scrut) v t (map deAnnAlt alts)
678
679 deAnnAlt :: AnnAlt bndr annot -> Alt bndr
680 deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)
681 \end{code}
682
683 \begin{code}
684 collectAnnBndrs :: AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
685 collectAnnBndrs e
686   = collect [] e
687   where
688     collect bs (_, AnnLam b body) = collect (b:bs) body
689     collect bs body               = (reverse bs, body)
690 \end{code}