1{-
2(c) The University of Glasgow 2006
3(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4-}
5
6{-# LANGUAGE CPP, DeriveDataTypeable, FlexibleContexts #-}
7{-# LANGUAGE NamedFieldPuns #-}
8{-# LANGUAGE BangPatterns #-}
9
10-- | CoreSyn holds all the main data types for use by for the Glasgow Haskell Compiler midsection
11module CoreSyn (
12        -- * Main data types
13        Expr(..), Alt, Bind(..), AltCon(..), Arg,
14        Tickish(..), TickishScoping(..), TickishPlacement(..),
15        CoreProgram, CoreExpr, CoreAlt, CoreBind, CoreArg, CoreBndr,
16        TaggedExpr, TaggedAlt, TaggedBind, TaggedArg, TaggedBndr(..), deTagExpr,
17
18        -- * In/Out type synonyms
19        InId, InBind, InExpr, InAlt, InArg, InType, InKind,
20               InBndr, InVar, InCoercion, InTyVar, InCoVar,
21        OutId, OutBind, OutExpr, OutAlt, OutArg, OutType, OutKind,
22               OutBndr, OutVar, OutCoercion, OutTyVar, OutCoVar, MOutCoercion,
23
24        -- ** 'Expr' construction
25        mkLet, mkLets, mkLetNonRec, mkLetRec, mkLams,
26        mkApps, mkTyApps, mkCoApps, mkVarApps, mkTyArg,
27
28        mkIntLit, mkIntLitInt,
29        mkWordLit, mkWordLitWord,
30        mkWord64LitWord64, mkInt64LitInt64,
31        mkCharLit, mkStringLit,
32        mkFloatLit, mkFloatLitFloat,
33        mkDoubleLit, mkDoubleLitDouble,
34
35        mkConApp, mkConApp2, mkTyBind, mkCoBind,
36        varToCoreExpr, varsToCoreExprs,
37
38        isId, cmpAltCon, cmpAlt, ltAlt,
39
40        -- ** Simple 'Expr' access functions and predicates
41        bindersOf, bindersOfBinds, rhssOfBind, rhssOfAlts,
42        collectBinders, collectTyBinders, collectTyAndValBinders,
43        collectNBinders,
44        collectArgs, stripNArgs, collectArgsTicks, flattenBinds,
45
46        exprToType, exprToCoercion_maybe,
47        applyTypeToArg,
48
49        isValArg, isTypeArg, isCoArg, isTyCoArg, valArgCount, valBndrCount,
50        isRuntimeArg, isRuntimeVar,
51
52        -- * Tick-related functions
53        tickishCounts, tickishScoped, tickishScopesLike, tickishFloatable,
54        tickishCanSplit, mkNoCount, mkNoScope,
55        tickishIsCode, tickishPlace,
56        tickishContains,
57
58        -- * Unfolding data types
59        Unfolding(..),  UnfoldingGuidance(..), UnfoldingSource(..),
60
61        -- ** Constructing 'Unfolding's
62        noUnfolding, bootUnfolding, evaldUnfolding, mkOtherCon,
63        unSaturatedOk, needSaturated, boringCxtOk, boringCxtNotOk,
64
65        -- ** Predicates and deconstruction on 'Unfolding'
66        unfoldingTemplate, expandUnfolding_maybe,
67        maybeUnfoldingTemplate, otherCons,
68        isValueUnfolding, isEvaldUnfolding, isCheapUnfolding,
69        isExpandableUnfolding, isConLikeUnfolding, isCompulsoryUnfolding,
70        isStableUnfolding, isFragileUnfolding, hasSomeUnfolding,
71        isBootUnfolding,
72        canUnfold, neverUnfoldGuidance, isStableSource,
73
74        -- * Annotated expression data types
75        AnnExpr, AnnExpr'(..), AnnBind(..), AnnAlt,
76
77        -- ** Operations on annotated expressions
78        collectAnnArgs, collectAnnArgsTicks,
79
80        -- ** Operations on annotations
81        deAnnotate, deAnnotate', deAnnAlt, deAnnBind,
82        collectAnnBndrs, collectNAnnBndrs,
83
84        -- * Orphanhood
85        IsOrphan(..), isOrphan, notOrphan, chooseOrphanAnchor,
86
87        -- * Core rule data types
88        CoreRule(..), RuleBase,
89        RuleName, RuleFun, IdUnfoldingFun, InScopeEnv,
90        RuleEnv(..), mkRuleEnv, emptyRuleEnv,
91
92        -- ** Operations on 'CoreRule's
93        ruleArity, ruleName, ruleIdName, ruleActivation,
94        setRuleIdName, ruleModule,
95        isBuiltinRule, isLocalRule, isAutoRule,
96    ) where
97
98#include "HsVersions.h"
99
100import GhcPrelude
101
102import CostCentre
103import VarEnv( InScopeSet )
104import Var
105import Type
106import Coercion
107import Name
108import NameSet
109import NameEnv( NameEnv, emptyNameEnv )
110import Literal
111import DataCon
112import Module
113import BasicTypes
114import DynFlags
115import Outputable
116import Util
117import UniqSet
118import SrcLoc     ( RealSrcSpan, containsSpan )
119import Binary
120
121import Data.Data hiding (TyCon)
122import Data.Int
123import Data.Word
124
125infixl 4 `mkApps`, `mkTyApps`, `mkVarApps`, `App`, `mkCoApps`
126-- Left associative, so that we can say (f `mkTyApps` xs `mkVarApps` ys)
127
128{-
129************************************************************************
130*                                                                      *
131\subsection{The main data types}
132*                                                                      *
133************************************************************************
134
135These data types are the heart of the compiler
136-}
137
138-- | This is the data type that represents GHCs core intermediate language. Currently
139-- GHC uses System FC <https://www.microsoft.com/en-us/research/publication/system-f-with-type-equality-coercions/> for this purpose,
140-- which is closely related to the simpler and better known System F <http://en.wikipedia.org/wiki/System_F>.
141--
142-- We get from Haskell source to this Core language in a number of stages:
143--
144-- 1. The source code is parsed into an abstract syntax tree, which is represented
145--    by the data type 'GHC.Hs.Expr.HsExpr' with the names being 'RdrName.RdrNames'
146--
147-- 2. This syntax tree is /renamed/, which attaches a 'Unique.Unique' to every 'RdrName.RdrName'
148--    (yielding a 'Name.Name') to disambiguate identifiers which are lexically identical.
149--    For example, this program:
150--
151-- @
152--      f x = let f x = x + 1
153--            in f (x - 2)
154-- @
155--
156--    Would be renamed by having 'Unique's attached so it looked something like this:
157--
158-- @
159--      f_1 x_2 = let f_3 x_4 = x_4 + 1
160--                in f_3 (x_2 - 2)
161-- @
162--    But see Note [Shadowing] below.
163--
164-- 3. The resulting syntax tree undergoes type checking (which also deals with instantiating
165--    type class arguments) to yield a 'GHC.Hs.Expr.HsExpr' type that has 'Id.Id' as it's names.
166--
167-- 4. Finally the syntax tree is /desugared/ from the expressive 'GHC.Hs.Expr.HsExpr' type into
168--    this 'Expr' type, which has far fewer constructors and hence is easier to perform
169--    optimization, analysis and code generation on.
170--
171-- The type parameter @b@ is for the type of binders in the expression tree.
172--
173-- The language consists of the following elements:
174--
175-- *  Variables
176--    See Note [Variable occurrences in Core]
177--
178-- *  Primitive literals
179--
180-- *  Applications: note that the argument may be a 'Type'.
181--    See Note [CoreSyn let/app invariant]
182--    See Note [Levity polymorphism invariants]
183--
184-- *  Lambda abstraction
185--    See Note [Levity polymorphism invariants]
186--
187-- *  Recursive and non recursive @let@s. Operationally
188--    this corresponds to allocating a thunk for the things
189--    bound and then executing the sub-expression.
190--
191--    See Note [CoreSyn letrec invariant]
192--    See Note [CoreSyn let/app invariant]
193--    See Note [Levity polymorphism invariants]
194--    See Note [CoreSyn type and coercion invariant]
195--
196-- *  Case expression. Operationally this corresponds to evaluating
197--    the scrutinee (expression examined) to weak head normal form
198--    and then examining at most one level of resulting constructor (i.e. you
199--    cannot do nested pattern matching directly with this).
200--
201--    The binder gets bound to the value of the scrutinee,
202--    and the 'Type' must be that of all the case alternatives
203--
204--    IMPORTANT: see Note [Case expression invariants]
205--
206-- *  Cast an expression to a particular type.
207--    This is used to implement @newtype@s (a @newtype@ constructor or
208--    destructor just becomes a 'Cast' in Core) and GADTs.
209--
210-- *  Notes. These allow general information to be added to expressions
211--    in the syntax tree
212--
213-- *  A type: this should only show up at the top level of an Arg
214--
215-- *  A coercion
216
217{- Note [Why does Case have a 'Type' field?]
218~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219The obvious alternative is
220   exprType (Case scrut bndr alts)
221     | (_,_,rhs1):_ <- alts
222     = exprType rhs1
223
224But caching the type in the Case constructor
225  exprType (Case scrut bndr ty alts) = ty
226is better for at least three reasons:
227
228* It works when there are no alternatives (see case invarant 1 above)
229
230* It might be faster in deeply-nested situations.
231
232* It might not be quite the same as (exprType rhs) for one
233  of the RHSs in alts. Consider a phantom type synonym
234       type S a = Int
235   and we want to form the case expression
236        case x of { K (a::*) -> (e :: S a) }
237   Then exprType of the RHS is (S a), but we cannot make that be
238   the 'ty' in the Case constructor because 'a' is simply not in
239   scope there. Instead we must expand the synonym to Int before
240   putting it in the Case constructor.  See CoreUtils.mkSingleAltCase.
241
242   So we'd have to do synonym expansion in exprType which would
243   be inefficient.
244
245* The type stored in the case is checked with lintInTy. This checks
246  (among other things) that it does not mention any variables that are
247  not in scope. If we did not have the type there, it would be a bit
248  harder for Core Lint to reject case blah of Ex x -> x where
249      data Ex = forall a. Ex a.
250-}
251
252-- If you edit this type, you may need to update the GHC formalism
253-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
254data Expr b
255  = Var   Id
256  | Lit   Literal
257  | App   (Expr b) (Arg b)
258  | Lam   b (Expr b)
259  | Let   (Bind b) (Expr b)
260  | Case  (Expr b) b Type [Alt b]   -- See Note [Case expression invariants]
261                                    -- and Note [Why does Case have a 'Type' field?]
262  | Cast  (Expr b) Coercion
263  | Tick  (Tickish Id) (Expr b)
264  | Type  Type
265  | Coercion Coercion
266  deriving Data
267
268-- | Type synonym for expressions that occur in function argument positions.
269-- Only 'Arg' should contain a 'Type' at top level, general 'Expr' should not
270type Arg b = Expr b
271
272-- | A case split alternative. Consists of the constructor leading to the alternative,
273-- the variables bound from the constructor, and the expression to be executed given that binding.
274-- The default alternative is @(DEFAULT, [], rhs)@
275
276-- If you edit this type, you may need to update the GHC formalism
277-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
278type Alt b = (AltCon, [b], Expr b)
279
280-- | A case alternative constructor (i.e. pattern match)
281
282-- If you edit this type, you may need to update the GHC formalism
283-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
284data AltCon
285  = DataAlt DataCon   --  ^ A plain data constructor: @case e of { Foo x -> ... }@.
286                      -- Invariant: the 'DataCon' is always from a @data@ type, and never from a @newtype@
287
288  | LitAlt  Literal   -- ^ A literal: @case e of { 1 -> ... }@
289                      -- Invariant: always an *unlifted* literal
290                      -- See Note [Literal alternatives]
291
292  | DEFAULT           -- ^ Trivial alternative: @case e of { _ -> ... }@
293   deriving (Eq, Data)
294
295-- This instance is a bit shady. It can only be used to compare AltCons for
296-- a single type constructor. Fortunately, it seems quite unlikely that we'll
297-- ever need to compare AltCons for different type constructors.
298-- The instance adheres to the order described in [CoreSyn case invariants]
299instance Ord AltCon where
300  compare (DataAlt con1) (DataAlt con2) =
301    ASSERT( dataConTyCon con1 == dataConTyCon con2 )
302    compare (dataConTag con1) (dataConTag con2)
303  compare (DataAlt _) _ = GT
304  compare _ (DataAlt _) = LT
305  compare (LitAlt l1) (LitAlt l2) = compare l1 l2
306  compare (LitAlt _) DEFAULT = GT
307  compare DEFAULT DEFAULT = EQ
308  compare DEFAULT _ = LT
309
310-- | Binding, used for top level bindings in a module and local bindings in a @let@.
311
312-- If you edit this type, you may need to update the GHC formalism
313-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
314data Bind b = NonRec b (Expr b)
315            | Rec [(b, (Expr b))]
316  deriving Data
317
318{-
319Note [Shadowing]
320~~~~~~~~~~~~~~~~
321While various passes attempt to rename on-the-fly in a manner that
322avoids "shadowing" (thereby simplifying downstream optimizations),
323neither the simplifier nor any other pass GUARANTEES that shadowing is
324avoided. Thus, all passes SHOULD work fine even in the presence of
325arbitrary shadowing in their inputs.
326
327In particular, scrutinee variables `x` in expressions of the form
328`Case e x t` are often renamed to variables with a prefix
329"wild_". These "wild" variables may appear in the body of the
330case-expression, and further, may be shadowed within the body.
331
332So the Unique in a Var is not really unique at all.  Still, it's very
333useful to give a constant-time equality/ordering for Vars, and to give
334a key that can be used to make sets of Vars (VarSet), or mappings from
335Vars to other things (VarEnv).   Moreover, if you do want to eliminate
336shadowing, you can give a new Unique to an Id without changing its
337printable name, which makes debugging easier.
338
339Note [Literal alternatives]
340~~~~~~~~~~~~~~~~~~~~~~~~~~~
341Literal alternatives (LitAlt lit) are always for *un-lifted* literals.
342We have one literal, a literal Integer, that is lifted, and we don't
343allow in a LitAlt, because LitAlt cases don't do any evaluation. Also
344(see #5603) if you say
345    case 3 of
346      S# x -> ...
347      J# _ _ -> ...
348(where S#, J# are the constructors for Integer) we don't want the
349simplifier calling findAlt with argument (LitAlt 3).  No no.  Integer
350literals are an opaque encoding of an algebraic data type, not of
351an unlifted literal, like all the others.
352
353Also, we do not permit case analysis with literal patterns on floating-point
354types. See #9238 and Note [Rules for floating-point comparisons] in
355PrelRules for the rationale for this restriction.
356
357-------------------------- CoreSyn INVARIANTS ---------------------------
358
359Note [Variable occurrences in Core]
360~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361Variable /occurrences/ are never CoVars, though /bindings/ can be.
362All CoVars appear in Coercions.
363
364For example
365  \(c :: Age~#Int) (d::Int). d |> (sym c)
366Here 'c' is a CoVar, which is lambda-bound, but it /occurs/ in
367a Coercion, (sym c).
368
369Note [CoreSyn letrec invariant]
370~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371The right hand sides of all top-level and recursive @let@s
372/must/ be of lifted type (see "Type#type_classification" for
373the meaning of /lifted/ vs. /unlifted/).
374
375There is one exception to this rule, top-level @let@s are
376allowed to bind primitive string literals: see
377Note [CoreSyn top-level string literals].
378
379Note [CoreSyn top-level string literals]
380~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
381As an exception to the usual rule that top-level binders must be lifted,
382we allow binding primitive string literals (of type Addr#) of type Addr# at the
383top level. This allows us to share string literals earlier in the pipeline and
384crucially allows other optimizations in the Core2Core pipeline to fire.
385Consider,
386
387  f n = let a::Addr# = "foo"#
388        in \x -> blah
389
390In order to be able to inline `f`, we would like to float `a` to the top.
391Another option would be to inline `a`, but that would lead to duplicating string
392literals, which we want to avoid. See #8472.
393
394The solution is simply to allow top-level unlifted binders. We can't allow
395arbitrary unlifted expression at the top-level though, unlifted binders cannot
396be thunks, so we just allow string literals.
397
398We allow the top-level primitive string literals to be wrapped in Ticks
399in the same way they can be wrapped when nested in an expression.
400CoreToSTG currently discards Ticks around top-level primitive string literals.
401See #14779.
402
403Also see Note [Compilation plan for top-level string literals].
404
405Note [Compilation plan for top-level string literals]
406~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407Here is a summary on how top-level string literals are handled by various
408parts of the compilation pipeline.
409
410* In the source language, there is no way to bind a primitive string literal
411  at the top level.
412
413* In Core, we have a special rule that permits top-level Addr# bindings. See
414  Note [CoreSyn top-level string literals]. Core-to-core passes may introduce
415  new top-level string literals.
416
417* In STG, top-level string literals are explicitly represented in the syntax
418  tree.
419
420* A top-level string literal may end up exported from a module. In this case,
421  in the object file, the content of the exported literal is given a label with
422  the _bytes suffix.
423
424Note [CoreSyn let/app invariant]
425~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
426The let/app invariant
427     the right hand side of a non-recursive 'Let', and
428     the argument of an 'App',
429    /may/ be of unlifted type, but only if
430    the expression is ok-for-speculation
431    or the 'Let' is for a join point.
432
433This means that the let can be floated around
434without difficulty. For example, this is OK:
435
436   y::Int# = x +# 1#
437
438But this is not, as it may affect termination if the
439expression is floated out:
440
441   y::Int# = fac 4#
442
443In this situation you should use @case@ rather than a @let@. The function
444'CoreUtils.needsCaseBinding' can help you determine which to generate, or
445alternatively use 'MkCore.mkCoreLet' rather than this constructor directly,
446which will generate a @case@ if necessary
447
448The let/app invariant is initially enforced by mkCoreLet and mkCoreApp in
449coreSyn/MkCore.
450
451For discussion of some implications of the let/app invariant primops see
452Note [Checking versus non-checking primops] in PrimOp.
453
454Note [Case expression invariants]
455~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
456Case expressions are one of the more complicated elements of the Core
457language, and come with a number of invariants.  All of them should be
458checked by Core Lint.
459
4601. The list of alternatives may be empty;
461   See Note [Empty case alternatives]
462
4632. The 'DEFAULT' case alternative must be first in the list,
464   if it occurs at all.  Checked in CoreLint.checkCaseAlts.
465
4663. The remaining cases are in order of (strictly) increasing
467     tag  (for 'DataAlts') or
468     lit  (for 'LitAlts').
469   This makes finding the relevant constructor easy, and makes
470   comparison easier too.   Checked in CoreLint.checkCaseAlts.
471
4724. The list of alternatives must be exhaustive. An /exhaustive/ case
473   does not necessarily mention all constructors:
474
475   @
476        data Foo = Red | Green | Blue
477        ... case x of
478              Red   -> True
479              other -> f (case x of
480                              Green -> ...
481                              Blue  -> ... ) ...
482   @
483
484   The inner case does not need a @Red@ alternative, because @x@
485   can't be @Red@ at that program point.
486
487   This is not checked by Core Lint -- it's very hard to do so.
488   E.g. suppose that inner case was floated out, thus:
489         let a = case x of
490                   Green -> ...
491                   Blue  -> ... )
492         case x of
493           Red   -> True
494           other -> f a
495   Now it's really hard to see that the Green/Blue case is
496   exhaustive.  But it is.
497
498   If you have a case-expression that really /isn't/ exhaustive,
499   we may generate seg-faults.  Consider the Green/Blue case
500   above.  Since there are only two branches we may generate
501   code that tests for Green, and if not Green simply /assumes/
502   Blue (since, if the case is exhaustive, that's all that
503   remains).  Of course, if it's not Blue and we start fetching
504   fields that should be in a Blue constructor, we may die
505   horribly. See also Note [Core Lint guarantee] in CoreLint.
506
5075. Floating-point values must not be scrutinised against literals.
508   See #9238 and Note [Rules for floating-point comparisons]
509   in PrelRules for rationale.  Checked in lintCaseExpr;
510   see the call to isFloatingTy.
511
5126. The 'ty' field of (Case scrut bndr ty alts) is the type of the
513   /entire/ case expression.  Checked in lintAltExpr.
514   See also Note [Why does Case have a 'Type' field?].
515
5167. The type of the scrutinee must be the same as the type
517   of the case binder, obviously.  Checked in lintCaseExpr.
518
519Note [CoreSyn type and coercion invariant]
520~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
521We allow a /non-recursive/, /non-top-level/ let to bind type and
522coercion variables.  These can be very convenient for postponing type
523substitutions until the next run of the simplifier.
524
525* A type variable binding must have a RHS of (Type ty)
526
527* A coercion variable binding must have a RHS of (Coercion co)
528
529  It is possible to have terms that return a coercion, but we use
530  case-binding for those; e.g.
531     case (eq_sel d) of (co :: a ~# b) -> blah
532  where eq_sel :: (a~b) -> (a~#b)
533
534  Or even even
535      case (df @Int) of (co :: a ~# b) -> blah
536  Which is very exotic, and I think never encountered; but see
537  Note [Equality superclasses in quantified constraints]
538  in TcCanonical
539
540Note [CoreSyn case invariants]
541~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
542See #case_invariants#
543
544Note [Levity polymorphism invariants]
545~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
546The levity-polymorphism invariants are these (as per "Levity Polymorphism",
547PLDI '17):
548
549* The type of a term-binder must not be levity-polymorphic,
550  unless it is a let(rec)-bound join point
551     (see Note [Invariants on join points])
552
553* The type of the argument of an App must not be levity-polymorphic.
554
555A type (t::TYPE r) is "levity polymorphic" if 'r' has any free variables.
556
557For example
558  \(r::RuntimeRep). \(a::TYPE r). \(x::a). e
559is illegal because x's type has kind (TYPE r), which has 'r' free.
560
561See Note [Levity polymorphism checking] in DsMonad to see where these
562invariants are established for user-written code.
563
564Note [CoreSyn let goal]
565~~~~~~~~~~~~~~~~~~~~~~~
566* The simplifier tries to ensure that if the RHS of a let is a constructor
567  application, its arguments are trivial, so that the constructor can be
568  inlined vigorously.
569
570Note [Type let]
571~~~~~~~~~~~~~~~
572See #type_let#
573
574Note [Empty case alternatives]
575~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
576The alternatives of a case expression should be exhaustive.  But
577this exhaustive list can be empty!
578
579* A case expression can have empty alternatives if (and only if) the
580  scrutinee is bound to raise an exception or diverge. When do we know
581  this?  See Note [Bottoming expressions] in CoreUtils.
582
583* The possibility of empty alternatives is one reason we need a type on
584  the case expression: if the alternatives are empty we can't get the
585  type from the alternatives!
586
587* In the case of empty types (see Note [Bottoming expressions]), say
588    data T
589  we do NOT want to replace
590    case (x::T) of Bool {}   -->   error Bool "Inaccessible case"
591  because x might raise an exception, and *that*'s what we want to see!
592  (#6067 is an example.) To preserve semantics we'd have to say
593     x `seq` error Bool "Inaccessible case"
594  but the 'seq' is just a case, so we are back to square 1.  Or I suppose
595  we could say
596     x |> UnsafeCoerce T Bool
597  but that loses all trace of the fact that this originated with an empty
598  set of alternatives.
599
600* We can use the empty-alternative construct to coerce error values from
601  one type to another.  For example
602
603    f :: Int -> Int
604    f n = error "urk"
605
606    g :: Int -> (# Char, Bool #)
607    g x = case f x of { 0 -> ..., n -> ... }
608
609  Then if we inline f in g's RHS we get
610    case (error Int "urk") of (# Char, Bool #) { ... }
611  and we can discard the alternatives since the scrutinee is bottom to give
612    case (error Int "urk") of (# Char, Bool #) {}
613
614  This is nicer than using an unsafe coerce between Int ~ (# Char,Bool #),
615  if for no other reason that we don't need to instantiate the (~) at an
616  unboxed type.
617
618* We treat a case expression with empty alternatives as trivial iff
619  its scrutinee is (see CoreUtils.exprIsTrivial).  This is actually
620  important; see Note [Empty case is trivial] in CoreUtils
621
622* An empty case is replaced by its scrutinee during the CoreToStg
623  conversion; remember STG is un-typed, so there is no need for
624  the empty case to do the type conversion.
625
626Note [Join points]
627~~~~~~~~~~~~~~~~~~
628In Core, a *join point* is a specially tagged function whose only occurrences
629are saturated tail calls. A tail call can appear in these places:
630
631  1. In the branches (not the scrutinee) of a case
632  2. Underneath a let (value or join point)
633  3. Inside another join point
634
635We write a join-point declaration as
636  join j @a @b x y = e1 in e2,
637like a let binding but with "join" instead (or "join rec" for "let rec"). Note
638that we put the parameters before the = rather than using lambdas; this is
639because it's relevant how many parameters the join point takes *as a join
640point.* This number is called the *join arity,* distinct from arity because it
641counts types as well as values. Note that a join point may return a lambda! So
642  join j x = x + 1
643is different from
644  join j = \x -> x + 1
645The former has join arity 1, while the latter has join arity 0.
646
647The identifier for a join point is called a join id or a *label.* An invocation
648is called a *jump.* We write a jump using the jump keyword:
649
650  jump j 3
651
652The words *label* and *jump* are evocative of assembly code (or Cmm) for a
653reason: join points are indeed compiled as labeled blocks, and jumps become
654actual jumps (plus argument passing and stack adjustment). There is no closure
655allocated and only a fraction of the function-call overhead. Hence we would
656like as many functions as possible to become join points (see OccurAnal) and
657the type rules for join points ensure we preserve the properties that make them
658efficient.
659
660In the actual AST, a join point is indicated by the IdDetails of the binder: a
661local value binding gets 'VanillaId' but a join point gets a 'JoinId' with its
662join arity.
663
664For more details, see the paper:
665
666  Luke Maurer, Paul Downen, Zena Ariola, and Simon Peyton Jones. "Compiling
667  without continuations." Submitted to PLDI'17.
668
669  https://www.microsoft.com/en-us/research/publication/compiling-without-continuations/
670
671Note [Invariants on join points]
672~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
673Join points must follow these invariants:
674
675  1. All occurrences must be tail calls. Each of these tail calls must pass the
676     same number of arguments, counting both types and values; we call this the
677     "join arity" (to distinguish from regular arity, which only counts values).
678
679     See Note [Join points are less general than the paper]
680
681  2. For join arity n, the right-hand side must begin with at least n lambdas.
682     No ticks, no casts, just lambdas!  C.f. CoreUtils.joinRhsArity.
683
684     2a. Moreover, this same constraint applies to any unfolding of
685         the binder.  Reason: if we want to push a continuation into
686         the RHS we must push it into the unfolding as well.
687
688     2b. The Arity (in the IdInfo) of a join point is the number of value
689         binders in the top n lambdas, where n is the join arity.
690
691         So arity <= join arity; the former counts only value binders
692         while the latter counts all binders.
693         e.g. Suppose $j has join arity 1
694               let j = \x y. e in case x of { A -> j 1; B -> j 2 }
695         Then its ordinary arity is also 1, not 2.
696
697         The arity of a join point isn't very important; but short of setting
698         it to zero, it is helpful to have an invariant.  E.g. #17294.
699
700  3. If the binding is recursive, then all other bindings in the recursive group
701     must also be join points.
702
703  4. The binding's type must not be polymorphic in its return type (as defined
704     in Note [The polymorphism rule of join points]).
705
706However, join points have simpler invariants in other ways
707
708  5. A join point can have an unboxed type without the RHS being
709     ok-for-speculation (i.e. drop the let/app invariant)
710     e.g.  let j :: Int# = factorial x in ...
711
712  6. A join point can have a levity-polymorphic RHS
713     e.g.  let j :: r :: TYPE l = fail void# in ...
714     This happened in an intermediate program #13394
715
716Examples:
717
718  join j1  x = 1 + x in jump j (jump j x)  -- Fails 1: non-tail call
719  join j1' x = 1 + x in if even a
720                          then jump j1 a
721                          else jump j1 a b -- Fails 1: inconsistent calls
722  join j2  x = flip (+) x in j2 1 2        -- Fails 2: not enough lambdas
723  join j2' x = \y -> x + y in j3 1         -- Passes: extra lams ok
724  join j @a (x :: a) = x                   -- Fails 4: polymorphic in ret type
725
726Invariant 1 applies to left-hand sides of rewrite rules, so a rule for a join
727point must have an exact call as its LHS.
728
729Strictly speaking, invariant 3 is redundant, since a call from inside a lazy
730binding isn't a tail call. Since a let-bound value can't invoke a free join
731point, then, they can't be mutually recursive. (A Core binding group *can*
732include spurious extra bindings if the occurrence analyser hasn't run, so
733invariant 3 does still need to be checked.) For the rigorous definition of
734"tail call", see Section 3 of the paper (Note [Join points]).
735
736Invariant 4 is subtle; see Note [The polymorphism rule of join points].
737
738Invariant 6 is to enable code like this:
739
740  f = \(r :: RuntimeRep) (a :: TYPE r) (x :: T).
741      join j :: a
742           j = error @r @a "bloop"
743      in case x of
744           A -> j
745           B -> j
746           C -> error @r @a "blurp"
747
748Core Lint will check these invariants, anticipating that any binder whose
749OccInfo is marked AlwaysTailCalled will become a join point as soon as the
750simplifier (or simpleOptPgm) runs.
751
752Note [Join points are less general than the paper]
753~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
754In the paper "Compiling without continuations", this expression is
755perfectly valid:
756
757    join { j = \_ -> e }
758    in (case blah of       )
759       (  True  -> j void# ) arg
760       (  False -> blah    )
761
762assuming 'j' has arity 1.   Here the call to 'j' does not look like a
763tail call, but actually everything is fine. See Section 3, "Managing \Delta"
764in the paper.
765
766In GHC, however, we adopt a slightly more restrictive subset, in which
767join point calls must be tail calls.  I think we /could/ loosen it up, but
768in fact the simplifier ensures that we always get tail calls, and it makes
769the back end a bit easier I think.  Generally, just less to think about;
770nothing deeper than that.
771
772Note [The type of a join point]
773~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
774A join point has the same type it would have as a function. That is, if it takes
775an Int and a Bool and its body produces a String, its type is `Int -> Bool ->
776String`. Natural as this may seem, it can be awkward. A join point shouldn't be
777thought to "return" in the same sense a function does---a jump is one-way. This
778is crucial for understanding how case-of-case interacts with join points:
779
780  case (join
781          j :: Int -> Bool -> String
782          j x y = ...
783        in
784          jump j z w) of
785    "" -> True
786    _  -> False
787
788The simplifier will pull the case into the join point (see Note [Case-of-case
789and join points] in Simplify):
790
791  join
792    j :: Int -> Bool -> Bool -- changed!
793    j x y = case ... of "" -> True
794                        _  -> False
795  in
796    jump j z w
797
798The body of the join point now returns a Bool, so the label `j` has to have its
799type updated accordingly. Inconvenient though this may be, it has the advantage
800that 'CoreUtils.exprType' can still return a type for any expression, including
801a jump.
802
803This differs from the paper (see Note [Invariants on join points]). In the
804paper, we instead give j the type `Int -> Bool -> forall a. a`. Then each jump
805carries the "return type" as a parameter, exactly the way other non-returning
806functions like `error` work:
807
808  case (join
809          j :: Int -> Bool -> forall a. a
810          j x y = ...
811        in
812          jump j z w @String) of
813    "" -> True
814    _  -> False
815
816Now we can move the case inward and we only have to change the jump:
817
818  join
819    j :: Int -> Bool -> forall a. a
820    j x y = case ... of "" -> True
821                        _  -> False
822  in
823    jump j z w @Bool
824
825(Core Lint would still check that the body of the join point has the right type;
826that type would simply not be reflected in the join id.)
827
828Note [The polymorphism rule of join points]
829~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
830Invariant 4 of Note [Invariants on join points] forbids a join point to be
831polymorphic in its return type. That is, if its type is
832
833  forall a1 ... ak. t1 -> ... -> tn -> r
834
835where its join arity is k+n, none of the type parameters ai may occur free in r.
836
837In some way, this falls out of the fact that given
838
839  join
840     j @a1 ... @ak x1 ... xn = e1
841  in e2
842
843then all calls to `j` are in tail-call positions of `e`, and expressions in
844tail-call positions in `e` have the same type as `e`.
845Therefore the type of `e1` -- the return type of the join point -- must be the
846same as the type of e2.
847Since the type variables aren't bound in `e2`, its type can't include them, and
848thus neither can the type of `e1`.
849
850This unfortunately prevents the `go` in the following code from being a
851join-point:
852
853  iter :: forall a. Int -> (a -> a) -> a -> a
854  iter @a n f x = go @a n f x
855    where
856      go :: forall a. Int -> (a -> a) -> a -> a
857      go @a 0 _ x = x
858      go @a n f x = go @a (n-1) f (f x)
859
860In this case, a static argument transformation would fix that (see
861ticket #14620):
862
863  iter :: forall a. Int -> (a -> a) -> a -> a
864  iter @a n f x = go' @a n f x
865    where
866      go' :: Int -> (a -> a) -> a -> a
867      go' 0 _ x = x
868      go' n f x = go' (n-1) f (f x)
869
870In general, loopification could be employed to do that (see #14068.)
871
872Can we simply drop the requirement, and allow `go` to be a join-point? We
873could, and it would work. But we could not longer apply the case-of-join-point
874transformation universally. This transformation would do:
875
876  case (join go @a n f x = case n of 0 -> x
877                                     n -> go @a (n-1) f (f x)
878        in go @Bool n neg True) of
879    True -> e1; False -> e2
880
881 ===>
882
883  join go @a n f x = case n of 0 -> case x of True -> e1; False -> e2
884                          n -> go @a (n-1) f (f x)
885  in go @Bool n neg True
886
887but that is ill-typed, as `x` is type `a`, not `Bool`.
888
889
890This also justifies why we do not consider the `e` in `e |> co` to be in
891tail position: A cast changes the type, but the type must be the same. But
892operationally, casts are vacuous, so this is a bit unfortunate! See #14610 for
893ideas how to fix this.
894
895************************************************************************
896*                                                                      *
897            In/Out type synonyms
898*                                                                      *
899********************************************************************* -}
900
901{- Many passes apply a substitution, and it's very handy to have type
902   synonyms to remind us whether or not the substitution has been applied -}
903
904-- Pre-cloning or substitution
905type InBndr     = CoreBndr
906type InType     = Type
907type InKind     = Kind
908type InBind     = CoreBind
909type InExpr     = CoreExpr
910type InAlt      = CoreAlt
911type InArg      = CoreArg
912type InCoercion = Coercion
913
914-- Post-cloning or substitution
915type OutBndr     = CoreBndr
916type OutType     = Type
917type OutKind     = Kind
918type OutCoercion = Coercion
919type OutBind     = CoreBind
920type OutExpr     = CoreExpr
921type OutAlt      = CoreAlt
922type OutArg      = CoreArg
923type MOutCoercion = MCoercion
924
925
926{- *********************************************************************
927*                                                                      *
928              Ticks
929*                                                                      *
930************************************************************************
931-}
932
933-- | Allows attaching extra information to points in expressions
934
935-- If you edit this type, you may need to update the GHC formalism
936-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
937data Tickish id =
938    -- | An @{-# SCC #-}@ profiling annotation, either automatically
939    -- added by the desugarer as a result of -auto-all, or added by
940    -- the user.
941    ProfNote {
942      profNoteCC    :: CostCentre, -- ^ the cost centre
943      profNoteCount :: !Bool,      -- ^ bump the entry count?
944      profNoteScope :: !Bool       -- ^ scopes over the enclosed expression
945                                   -- (i.e. not just a tick)
946    }
947
948  -- | A "tick" used by HPC to track the execution of each
949  -- subexpression in the original source code.
950  | HpcTick {
951      tickModule :: Module,
952      tickId     :: !Int
953    }
954
955  -- | A breakpoint for the GHCi debugger.  This behaves like an HPC
956  -- tick, but has a list of free variables which will be available
957  -- for inspection in GHCi when the program stops at the breakpoint.
958  --
959  -- NB. we must take account of these Ids when (a) counting free variables,
960  -- and (b) substituting (don't substitute for them)
961  | Breakpoint
962    { breakpointId     :: !Int
963    , breakpointFVs    :: [id]  -- ^ the order of this list is important:
964                                -- it matches the order of the lists in the
965                                -- appropriate entry in HscTypes.ModBreaks.
966                                --
967                                -- Careful about substitution!  See
968                                -- Note [substTickish] in CoreSubst.
969    }
970
971  -- | A source note.
972  --
973  -- Source notes are pure annotations: Their presence should neither
974  -- influence compilation nor execution. The semantics are given by
975  -- causality: The presence of a source note means that a local
976  -- change in the referenced source code span will possibly provoke
977  -- the generated code to change. On the flip-side, the functionality
978  -- of annotated code *must* be invariant against changes to all
979  -- source code *except* the spans referenced in the source notes
980  -- (see "Causality of optimized Haskell" paper for details).
981  --
982  -- Therefore extending the scope of any given source note is always
983  -- valid. Note that it is still undesirable though, as this reduces
984  -- their usefulness for debugging and profiling. Therefore we will
985  -- generally try only to make use of this property where it is
986  -- necessary to enable optimizations.
987  | SourceNote
988    { sourceSpan :: RealSrcSpan -- ^ Source covered
989    , sourceName :: String      -- ^ Name for source location
990                                --   (uses same names as CCs)
991    }
992
993  deriving (Eq, Ord, Data)
994
995-- | A "counting tick" (where tickishCounts is True) is one that
996-- counts evaluations in some way.  We cannot discard a counting tick,
997-- and the compiler should preserve the number of counting ticks as
998-- far as possible.
999--
1000-- However, we still allow the simplifier to increase or decrease
1001-- sharing, so in practice the actual number of ticks may vary, except
1002-- that we never change the value from zero to non-zero or vice versa.
1003tickishCounts :: Tickish id -> Bool
1004tickishCounts n@ProfNote{} = profNoteCount n
1005tickishCounts HpcTick{}    = True
1006tickishCounts Breakpoint{} = True
1007tickishCounts _            = False
1008
1009
1010-- | Specifies the scoping behaviour of ticks. This governs the
1011-- behaviour of ticks that care about the covered code and the cost
1012-- associated with it. Important for ticks relating to profiling.
1013data TickishScoping =
1014    -- | No scoping: The tick does not care about what code it
1015    -- covers. Transformations can freely move code inside as well as
1016    -- outside without any additional annotation obligations
1017    NoScope
1018
1019    -- | Soft scoping: We want all code that is covered to stay
1020    -- covered.  Note that this scope type does not forbid
1021    -- transformations from happening, as long as all results of
1022    -- the transformations are still covered by this tick or a copy of
1023    -- it. For example
1024    --
1025    --   let x = tick<...> (let y = foo in bar) in baz
1026    --     ===>
1027    --   let x = tick<...> bar; y = tick<...> foo in baz
1028    --
1029    -- Is a valid transformation as far as "bar" and "foo" is
1030    -- concerned, because both still are scoped over by the tick.
1031    --
1032    -- Note though that one might object to the "let" not being
1033    -- covered by the tick any more. However, we are generally lax
1034    -- with this - constant costs don't matter too much, and given
1035    -- that the "let" was effectively merged we can view it as having
1036    -- lost its identity anyway.
1037    --
1038    -- Also note that this scoping behaviour allows floating a tick
1039    -- "upwards" in pretty much any situation. For example:
1040    --
1041    --   case foo of x -> tick<...> bar
1042    --     ==>
1043    --   tick<...> case foo of x -> bar
1044    --
1045    -- While this is always leagl, we want to make a best effort to
1046    -- only make us of this where it exposes transformation
1047    -- opportunities.
1048  | SoftScope
1049
1050    -- | Cost centre scoping: We don't want any costs to move to other
1051    -- cost-centre stacks. This means we not only want no code or cost
1052    -- to get moved out of their cost centres, but we also object to
1053    -- code getting associated with new cost-centre ticks - or
1054    -- changing the order in which they get applied.
1055    --
1056    -- A rule of thumb is that we don't want any code to gain new
1057    -- annotations. However, there are notable exceptions, for
1058    -- example:
1059    --
1060    --   let f = \y -> foo in tick<...> ... (f x) ...
1061    --     ==>
1062    --   tick<...> ... foo[x/y] ...
1063    --
1064    -- In-lining lambdas like this is always legal, because inlining a
1065    -- function does not change the cost-centre stack when the
1066    -- function is called.
1067  | CostCentreScope
1068
1069  deriving (Eq)
1070
1071-- | Returns the intended scoping rule for a Tickish
1072tickishScoped :: Tickish id -> TickishScoping
1073tickishScoped n@ProfNote{}
1074  | profNoteScope n        = CostCentreScope
1075  | otherwise              = NoScope
1076tickishScoped HpcTick{}    = NoScope
1077tickishScoped Breakpoint{} = CostCentreScope
1078   -- Breakpoints are scoped: eventually we're going to do call
1079   -- stacks, but also this helps prevent the simplifier from moving
1080   -- breakpoints around and changing their result type (see #1531).
1081tickishScoped SourceNote{} = SoftScope
1082
1083-- | Returns whether the tick scoping rule is at least as permissive
1084-- as the given scoping rule.
1085tickishScopesLike :: Tickish id -> TickishScoping -> Bool
1086tickishScopesLike t scope = tickishScoped t `like` scope
1087  where NoScope         `like` _               = True
1088        _               `like` NoScope         = False
1089        SoftScope       `like` _               = True
1090        _               `like` SoftScope       = False
1091        CostCentreScope `like` _               = True
1092
1093-- | Returns @True@ for ticks that can be floated upwards easily even
1094-- where it might change execution counts, such as:
1095--
1096--   Just (tick<...> foo)
1097--     ==>
1098--   tick<...> (Just foo)
1099--
1100-- This is a combination of @tickishSoftScope@ and
1101-- @tickishCounts@. Note that in principle splittable ticks can become
1102-- floatable using @mkNoTick@ -- even though there's currently no
1103-- tickish for which that is the case.
1104tickishFloatable :: Tickish id -> Bool
1105tickishFloatable t = t `tickishScopesLike` SoftScope && not (tickishCounts t)
1106
1107-- | Returns @True@ for a tick that is both counting /and/ scoping and
1108-- can be split into its (tick, scope) parts using 'mkNoScope' and
1109-- 'mkNoTick' respectively.
1110tickishCanSplit :: Tickish id -> Bool
1111tickishCanSplit ProfNote{profNoteScope = True, profNoteCount = True}
1112                   = True
1113tickishCanSplit _  = False
1114
1115mkNoCount :: Tickish id -> Tickish id
1116mkNoCount n | not (tickishCounts n)   = n
1117            | not (tickishCanSplit n) = panic "mkNoCount: Cannot split!"
1118mkNoCount n@ProfNote{}                = n {profNoteCount = False}
1119mkNoCount _                           = panic "mkNoCount: Undefined split!"
1120
1121mkNoScope :: Tickish id -> Tickish id
1122mkNoScope n | tickishScoped n == NoScope  = n
1123            | not (tickishCanSplit n)     = panic "mkNoScope: Cannot split!"
1124mkNoScope n@ProfNote{}                    = n {profNoteScope = False}
1125mkNoScope _                               = panic "mkNoScope: Undefined split!"
1126
1127-- | Return @True@ if this source annotation compiles to some backend
1128-- code. Without this flag, the tickish is seen as a simple annotation
1129-- that does not have any associated evaluation code.
1130--
1131-- What this means that we are allowed to disregard the tick if doing
1132-- so means that we can skip generating any code in the first place. A
1133-- typical example is top-level bindings:
1134--
1135--   foo = tick<...> \y -> ...
1136--     ==>
1137--   foo = \y -> tick<...> ...
1138--
1139-- Here there is just no operational difference between the first and
1140-- the second version. Therefore code generation should simply
1141-- translate the code as if it found the latter.
1142tickishIsCode :: Tickish id -> Bool
1143tickishIsCode SourceNote{} = False
1144tickishIsCode _tickish     = True  -- all the rest for now
1145
1146
1147-- | Governs the kind of expression that the tick gets placed on when
1148-- annotating for example using @mkTick@. If we find that we want to
1149-- put a tickish on an expression ruled out here, we try to float it
1150-- inwards until we find a suitable expression.
1151data TickishPlacement =
1152
1153    -- | Place ticks exactly on run-time expressions. We can still
1154    -- move the tick through pure compile-time constructs such as
1155    -- other ticks, casts or type lambdas. This is the most
1156    -- restrictive placement rule for ticks, as all tickishs have in
1157    -- common that they want to track runtime processes. The only
1158    -- legal placement rule for counting ticks.
1159    PlaceRuntime
1160
1161    -- | As @PlaceRuntime@, but we float the tick through all
1162    -- lambdas. This makes sense where there is little difference
1163    -- between annotating the lambda and annotating the lambda's code.
1164  | PlaceNonLam
1165
1166    -- | In addition to floating through lambdas, cost-centre style
1167    -- tickishs can also be moved from constructors, non-function
1168    -- variables and literals. For example:
1169    --
1170    --   let x = scc<...> C (scc<...> y) (scc<...> 3) in ...
1171    --
1172    -- Neither the constructor application, the variable or the
1173    -- literal are likely to have any cost worth mentioning. And even
1174    -- if y names a thunk, the call would not care about the
1175    -- evaluation context. Therefore removing all annotations in the
1176    -- above example is safe.
1177  | PlaceCostCentre
1178
1179  deriving (Eq)
1180
1181-- | Placement behaviour we want for the ticks
1182tickishPlace :: Tickish id -> TickishPlacement
1183tickishPlace n@ProfNote{}
1184  | profNoteCount n        = PlaceRuntime
1185  | otherwise              = PlaceCostCentre
1186tickishPlace HpcTick{}     = PlaceRuntime
1187tickishPlace Breakpoint{}  = PlaceRuntime
1188tickishPlace SourceNote{}  = PlaceNonLam
1189
1190-- | Returns whether one tick "contains" the other one, therefore
1191-- making the second tick redundant.
1192tickishContains :: Eq b => Tickish b -> Tickish b -> Bool
1193tickishContains (SourceNote sp1 n1) (SourceNote sp2 n2)
1194  = containsSpan sp1 sp2 && n1 == n2
1195    -- compare the String last
1196tickishContains t1 t2
1197  = t1 == t2
1198
1199{-
1200************************************************************************
1201*                                                                      *
1202                Orphans
1203*                                                                      *
1204************************************************************************
1205-}
1206
1207-- | Is this instance an orphan?  If it is not an orphan, contains an 'OccName'
1208-- witnessing the instance's non-orphanhood.
1209-- See Note [Orphans]
1210data IsOrphan
1211  = IsOrphan
1212  | NotOrphan OccName -- The OccName 'n' witnesses the instance's non-orphanhood
1213                      -- In that case, the instance is fingerprinted as part
1214                      -- of the definition of 'n's definition
1215    deriving Data
1216
1217-- | Returns true if 'IsOrphan' is orphan.
1218isOrphan :: IsOrphan -> Bool
1219isOrphan IsOrphan = True
1220isOrphan _ = False
1221
1222-- | Returns true if 'IsOrphan' is not an orphan.
1223notOrphan :: IsOrphan -> Bool
1224notOrphan NotOrphan{} = True
1225notOrphan _ = False
1226
1227chooseOrphanAnchor :: NameSet -> IsOrphan
1228-- Something (rule, instance) is relate to all the Names in this
1229-- list. Choose one of them to be an "anchor" for the orphan.  We make
1230-- the choice deterministic to avoid gratuitious changes in the ABI
1231-- hash (#4012).  Specifically, use lexicographic comparison of
1232-- OccName rather than comparing Uniques
1233--
1234-- NB: 'minimum' use Ord, and (Ord OccName) works lexicographically
1235--
1236chooseOrphanAnchor local_names
1237  | isEmptyNameSet local_names = IsOrphan
1238  | otherwise                  = NotOrphan (minimum occs)
1239  where
1240    occs = map nameOccName $ nonDetEltsUniqSet local_names
1241    -- It's OK to use nonDetEltsUFM here, see comments above
1242
1243instance Binary IsOrphan where
1244    put_ bh IsOrphan = putByte bh 0
1245    put_ bh (NotOrphan n) = do
1246        putByte bh 1
1247        put_ bh n
1248    get bh = do
1249        h <- getByte bh
1250        case h of
1251            0 -> return IsOrphan
1252            _ -> do
1253                n <- get bh
1254                return $ NotOrphan n
1255
1256{-
1257Note [Orphans]
1258~~~~~~~~~~~~~~
1259Class instances, rules, and family instances are divided into orphans
1260and non-orphans.  Roughly speaking, an instance/rule is an orphan if
1261its left hand side mentions nothing defined in this module.  Orphan-hood
1262has two major consequences
1263
1264 * A module that contains orphans is called an "orphan module".  If
1265   the module being compiled depends (transitively) on an oprhan
1266   module M, then M.hi is read in regardless of whether M is oherwise
1267   needed. This is to ensure that we don't miss any instance decls in
1268   M.  But it's painful, because it means we need to keep track of all
1269   the orphan modules below us.
1270
1271 * A non-orphan is not finger-printed separately.  Instead, for
1272   fingerprinting purposes it is treated as part of the entity it
1273   mentions on the LHS.  For example
1274      data T = T1 | T2
1275      instance Eq T where ....
1276   The instance (Eq T) is incorprated as part of T's fingerprint.
1277
1278   In contrast, orphans are all fingerprinted together in the
1279   mi_orph_hash field of the ModIface.
1280
1281   See MkIface.addFingerprints.
1282
1283Orphan-hood is computed
1284  * For class instances:
1285      when we make a ClsInst
1286    (because it is needed during instance lookup)
1287
1288  * For rules and family instances:
1289       when we generate an IfaceRule (MkIface.coreRuleToIfaceRule)
1290                     or IfaceFamInst (MkIface.instanceToIfaceInst)
1291-}
1292
1293{-
1294************************************************************************
1295*                                                                      *
1296\subsection{Transformation rules}
1297*                                                                      *
1298************************************************************************
1299
1300The CoreRule type and its friends are dealt with mainly in CoreRules,
1301but CoreFVs, Subst, PprCore, CoreTidy also inspect the representation.
1302-}
1303
1304-- | Gathers a collection of 'CoreRule's. Maps (the name of) an 'Id' to its rules
1305type RuleBase = NameEnv [CoreRule]
1306        -- The rules are unordered;
1307        -- we sort out any overlaps on lookup
1308
1309-- | A full rule environment which we can apply rules from.  Like a 'RuleBase',
1310-- but it also includes the set of visible orphans we use to filter out orphan
1311-- rules which are not visible (even though we can see them...)
1312data RuleEnv
1313    = RuleEnv { re_base          :: RuleBase
1314              , re_visible_orphs :: ModuleSet
1315              }
1316
1317mkRuleEnv :: RuleBase -> [Module] -> RuleEnv
1318mkRuleEnv rules vis_orphs = RuleEnv rules (mkModuleSet vis_orphs)
1319
1320emptyRuleEnv :: RuleEnv
1321emptyRuleEnv = RuleEnv emptyNameEnv emptyModuleSet
1322
1323-- | A 'CoreRule' is:
1324--
1325-- * \"Local\" if the function it is a rule for is defined in the
1326--   same module as the rule itself.
1327--
1328-- * \"Orphan\" if nothing on the LHS is defined in the same module
1329--   as the rule itself
1330data CoreRule
1331  = Rule {
1332        ru_name :: RuleName,            -- ^ Name of the rule, for communication with the user
1333        ru_act  :: Activation,          -- ^ When the rule is active
1334
1335        -- Rough-matching stuff
1336        -- see comments with InstEnv.ClsInst( is_cls, is_rough )
1337        ru_fn    :: Name,               -- ^ Name of the 'Id.Id' at the head of this rule
1338        ru_rough :: [Maybe Name],       -- ^ Name at the head of each argument to the left hand side
1339
1340        -- Proper-matching stuff
1341        -- see comments with InstEnv.ClsInst( is_tvs, is_tys )
1342        ru_bndrs :: [CoreBndr],         -- ^ Variables quantified over
1343        ru_args  :: [CoreExpr],         -- ^ Left hand side arguments
1344
1345        -- And the right-hand side
1346        ru_rhs   :: CoreExpr,           -- ^ Right hand side of the rule
1347                                        -- Occurrence info is guaranteed correct
1348                                        -- See Note [OccInfo in unfoldings and rules]
1349
1350        -- Locality
1351        ru_auto :: Bool,   -- ^ @True@  <=> this rule is auto-generated
1352                           --               (notably by Specialise or SpecConstr)
1353                           --   @False@ <=> generated at the user's behest
1354                           -- See Note [Trimming auto-rules] in TidyPgm
1355                           -- for the sole purpose of this field.
1356
1357        ru_origin :: !Module,   -- ^ 'Module' the rule was defined in, used
1358                                -- to test if we should see an orphan rule.
1359
1360        ru_orphan :: !IsOrphan, -- ^ Whether or not the rule is an orphan.
1361
1362        ru_local :: Bool        -- ^ @True@ iff the fn at the head of the rule is
1363                                -- defined in the same module as the rule
1364                                -- and is not an implicit 'Id' (like a record selector,
1365                                -- class operation, or data constructor).  This
1366                                -- is different from 'ru_orphan', where a rule
1367                                -- can avoid being an orphan if *any* Name in
1368                                -- LHS of the rule was defined in the same
1369                                -- module as the rule.
1370    }
1371
1372  -- | Built-in rules are used for constant folding
1373  -- and suchlike.  They have no free variables.
1374  -- A built-in rule is always visible (there is no such thing as
1375  -- an orphan built-in rule.)
1376  | BuiltinRule {
1377        ru_name  :: RuleName,   -- ^ As above
1378        ru_fn    :: Name,       -- ^ As above
1379        ru_nargs :: Int,        -- ^ Number of arguments that 'ru_try' consumes,
1380                                -- if it fires, including type arguments
1381        ru_try   :: RuleFun
1382                -- ^ This function does the rewrite.  It given too many
1383                -- arguments, it simply discards them; the returned 'CoreExpr'
1384                -- is just the rewrite of 'ru_fn' applied to the first 'ru_nargs' args
1385    }
1386                -- See Note [Extra args in rule matching] in Rules.hs
1387
1388type RuleFun = DynFlags -> InScopeEnv -> Id -> [CoreExpr] -> Maybe CoreExpr
1389type InScopeEnv = (InScopeSet, IdUnfoldingFun)
1390
1391type IdUnfoldingFun = Id -> Unfolding
1392-- A function that embodies how to unfold an Id if you need
1393-- to do that in the Rule.  The reason we need to pass this info in
1394-- is that whether an Id is unfoldable depends on the simplifier phase
1395
1396isBuiltinRule :: CoreRule -> Bool
1397isBuiltinRule (BuiltinRule {}) = True
1398isBuiltinRule _                = False
1399
1400isAutoRule :: CoreRule -> Bool
1401isAutoRule (BuiltinRule {}) = False
1402isAutoRule (Rule { ru_auto = is_auto }) = is_auto
1403
1404-- | The number of arguments the 'ru_fn' must be applied
1405-- to before the rule can match on it
1406ruleArity :: CoreRule -> Int
1407ruleArity (BuiltinRule {ru_nargs = n}) = n
1408ruleArity (Rule {ru_args = args})      = length args
1409
1410ruleName :: CoreRule -> RuleName
1411ruleName = ru_name
1412
1413ruleModule :: CoreRule -> Maybe Module
1414ruleModule Rule { ru_origin } = Just ru_origin
1415ruleModule BuiltinRule {} = Nothing
1416
1417ruleActivation :: CoreRule -> Activation
1418ruleActivation (BuiltinRule { })       = AlwaysActive
1419ruleActivation (Rule { ru_act = act }) = act
1420
1421-- | The 'Name' of the 'Id.Id' at the head of the rule left hand side
1422ruleIdName :: CoreRule -> Name
1423ruleIdName = ru_fn
1424
1425isLocalRule :: CoreRule -> Bool
1426isLocalRule = ru_local
1427
1428-- | Set the 'Name' of the 'Id.Id' at the head of the rule left hand side
1429setRuleIdName :: Name -> CoreRule -> CoreRule
1430setRuleIdName nm ru = ru { ru_fn = nm }
1431
1432{-
1433************************************************************************
1434*                                                                      *
1435                Unfoldings
1436*                                                                      *
1437************************************************************************
1438
1439The @Unfolding@ type is declared here to avoid numerous loops
1440-}
1441
1442-- | Records the /unfolding/ of an identifier, which is approximately the form the
1443-- identifier would have if we substituted its definition in for the identifier.
1444-- This type should be treated as abstract everywhere except in "CoreUnfold"
1445data Unfolding
1446  = NoUnfolding        -- ^ We have no information about the unfolding.
1447
1448  | BootUnfolding      -- ^ We have no information about the unfolding, because
1449                       -- this 'Id' came from an @hi-boot@ file.
1450                       -- See Note [Inlining and hs-boot files] in ToIface
1451                       -- for what this is used for.
1452
1453  | OtherCon [AltCon]  -- ^ It ain't one of these constructors.
1454                       -- @OtherCon xs@ also indicates that something has been evaluated
1455                       -- and hence there's no point in re-evaluating it.
1456                       -- @OtherCon []@ is used even for non-data-type values
1457                       -- to indicated evaluated-ness.  Notably:
1458                       --
1459                       -- > data C = C !(Int -> Int)
1460                       -- > case x of { C f -> ... }
1461                       --
1462                       -- Here, @f@ gets an @OtherCon []@ unfolding.
1463
1464  | DFunUnfolding {     -- The Unfolding of a DFunId
1465                        -- See Note [DFun unfoldings]
1466                        --     df = /\a1..am. \d1..dn. MkD t1 .. tk
1467                        --                                 (op1 a1..am d1..dn)
1468                        --                                 (op2 a1..am d1..dn)
1469        df_bndrs :: [Var],      -- The bound variables [a1..m],[d1..dn]
1470        df_con   :: DataCon,    -- The dictionary data constructor (never a newtype datacon)
1471        df_args  :: [CoreExpr]  -- Args of the data con: types, superclasses and methods,
1472    }                           -- in positional order
1473
1474  | CoreUnfolding {             -- An unfolding for an Id with no pragma,
1475                                -- or perhaps a NOINLINE pragma
1476                                -- (For NOINLINE, the phase, if any, is in the
1477                                -- InlinePragInfo for this Id.)
1478        uf_tmpl       :: CoreExpr,        -- Template; occurrence info is correct
1479        uf_src        :: UnfoldingSource, -- Where the unfolding came from
1480        uf_is_top     :: Bool,          -- True <=> top level binding
1481        uf_is_value   :: Bool,          -- exprIsHNF template (cached); it is ok to discard
1482                                        --      a `seq` on this variable
1483        uf_is_conlike :: Bool,          -- True <=> applicn of constructor or CONLIKE function
1484                                        --      Cached version of exprIsConLike
1485        uf_is_work_free :: Bool,                -- True <=> doesn't waste (much) work to expand
1486                                        --          inside an inlining
1487                                        --      Cached version of exprIsCheap
1488        uf_expandable :: Bool,          -- True <=> can expand in RULE matching
1489                                        --      Cached version of exprIsExpandable
1490        uf_guidance   :: UnfoldingGuidance      -- Tells about the *size* of the template.
1491    }
1492  -- ^ An unfolding with redundant cached information. Parameters:
1493  --
1494  --  uf_tmpl: Template used to perform unfolding;
1495  --           NB: Occurrence info is guaranteed correct:
1496  --               see Note [OccInfo in unfoldings and rules]
1497  --
1498  --  uf_is_top: Is this a top level binding?
1499  --
1500  --  uf_is_value: 'exprIsHNF' template (cached); it is ok to discard a 'seq' on
1501  --     this variable
1502  --
1503  --  uf_is_work_free:  Does this waste only a little work if we expand it inside an inlining?
1504  --     Basically this is a cached version of 'exprIsWorkFree'
1505  --
1506  --  uf_guidance:  Tells us about the /size/ of the unfolding template
1507
1508
1509------------------------------------------------
1510data UnfoldingSource
1511  = -- See also Note [Historical note: unfoldings for wrappers]
1512
1513    InlineRhs          -- The current rhs of the function
1514                       -- Replace uf_tmpl each time around
1515
1516  | InlineStable       -- From an INLINE or INLINABLE pragma
1517                       --   INLINE     if guidance is UnfWhen
1518                       --   INLINABLE  if guidance is UnfIfGoodArgs/UnfoldNever
1519                       -- (well, technically an INLINABLE might be made
1520                       -- UnfWhen if it was small enough, and then
1521                       -- it will behave like INLINE outside the current
1522                       -- module, but that is the way automatic unfoldings
1523                       -- work so it is consistent with the intended
1524                       -- meaning of INLINABLE).
1525                       --
1526                       -- uf_tmpl may change, but only as a result of
1527                       -- gentle simplification, it doesn't get updated
1528                       -- to the current RHS during compilation as with
1529                       -- InlineRhs.
1530                       --
1531                       -- See Note [InlineStable]
1532
1533  | InlineCompulsory   -- Something that *has* no binding, so you *must* inline it
1534                       -- Only a few primop-like things have this property
1535                       -- (see MkId.hs, calls to mkCompulsoryUnfolding).
1536                       -- Inline absolutely always, however boring the context.
1537
1538
1539
1540-- | 'UnfoldingGuidance' says when unfolding should take place
1541data UnfoldingGuidance
1542  = UnfWhen {   -- Inline without thinking about the *size* of the uf_tmpl
1543                -- Used (a) for small *and* cheap unfoldings
1544                --      (b) for INLINE functions
1545                -- See Note [INLINE for small functions] in CoreUnfold
1546      ug_arity    :: Arity,     -- Number of value arguments expected
1547
1548      ug_unsat_ok  :: Bool,     -- True <=> ok to inline even if unsaturated
1549      ug_boring_ok :: Bool      -- True <=> ok to inline even if the context is boring
1550                -- So True,True means "always"
1551    }
1552
1553  | UnfIfGoodArgs {     -- Arose from a normal Id; the info here is the
1554                        -- result of a simple analysis of the RHS
1555
1556      ug_args ::  [Int],  -- Discount if the argument is evaluated.
1557                          -- (i.e., a simplification will definitely
1558                          -- be possible).  One elt of the list per *value* arg.
1559
1560      ug_size :: Int,     -- The "size" of the unfolding.
1561
1562      ug_res :: Int       -- Scrutinee discount: the discount to substract if the thing is in
1563    }                     -- a context (case (thing args) of ...),
1564                          -- (where there are the right number of arguments.)
1565
1566  | UnfNever        -- The RHS is big, so don't inline it
1567  deriving (Eq)
1568
1569{-
1570Note [Historical note: unfoldings for wrappers]
1571~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1572We used to have a nice clever scheme in interface files for
1573wrappers. A wrapper's unfolding can be reconstructed from its worker's
1574id and its strictness. This decreased .hi file size (sometimes
1575significantly, for modules like GHC.Classes with many high-arity w/w
1576splits) and had a slight corresponding effect on compile times.
1577
1578However, when we added the second demand analysis, this scheme lead to
1579some Core lint errors. The second analysis could change the strictness
1580signatures, which sometimes resulted in a wrapper's regenerated
1581unfolding applying the wrapper to too many arguments.
1582
1583Instead of repairing the clever .hi scheme, we abandoned it in favor
1584of simplicity. The .hi sizes are usually insignificant (excluding the
1585+1M for base libraries), and compile time barely increases (~+1% for
1586nofib). The nicer upshot is that the UnfoldingSource no longer mentions
1587an Id, so, eg, substitutions need not traverse them.
1588
1589
1590Note [DFun unfoldings]
1591~~~~~~~~~~~~~~~~~~~~~~
1592The Arity in a DFunUnfolding is total number of args (type and value)
1593that the DFun needs to produce a dictionary.  That's not necessarily
1594related to the ordinary arity of the dfun Id, esp if the class has
1595one method, so the dictionary is represented by a newtype.  Example
1596
1597     class C a where { op :: a -> Int }
1598     instance C a -> C [a] where op xs = op (head xs)
1599
1600The instance translates to
1601
1602     $dfCList :: forall a. C a => C [a]  -- Arity 2!
1603     $dfCList = /\a.\d. $copList {a} d |> co
1604
1605     $copList :: forall a. C a => [a] -> Int  -- Arity 2!
1606     $copList = /\a.\d.\xs. op {a} d (head xs)
1607
1608Now we might encounter (op (dfCList {ty} d) a1 a2)
1609and we want the (op (dfList {ty} d)) rule to fire, because $dfCList
1610has all its arguments, even though its (value) arity is 2.  That's
1611why we record the number of expected arguments in the DFunUnfolding.
1612
1613Note that although it's an Arity, it's most convenient for it to give
1614the *total* number of arguments, both type and value.  See the use
1615site in exprIsConApp_maybe.
1616-}
1617
1618-- Constants for the UnfWhen constructor
1619needSaturated, unSaturatedOk :: Bool
1620needSaturated = False
1621unSaturatedOk = True
1622
1623boringCxtNotOk, boringCxtOk :: Bool
1624boringCxtOk    = True
1625boringCxtNotOk = False
1626
1627------------------------------------------------
1628noUnfolding :: Unfolding
1629-- ^ There is no known 'Unfolding'
1630evaldUnfolding :: Unfolding
1631-- ^ This unfolding marks the associated thing as being evaluated
1632
1633noUnfolding    = NoUnfolding
1634evaldUnfolding = OtherCon []
1635
1636-- | There is no known 'Unfolding', because this came from an
1637-- hi-boot file.
1638bootUnfolding :: Unfolding
1639bootUnfolding = BootUnfolding
1640
1641mkOtherCon :: [AltCon] -> Unfolding
1642mkOtherCon = OtherCon
1643
1644isStableSource :: UnfoldingSource -> Bool
1645-- Keep the unfolding template
1646isStableSource InlineCompulsory   = True
1647isStableSource InlineStable       = True
1648isStableSource InlineRhs          = False
1649
1650-- | Retrieves the template of an unfolding: panics if none is known
1651unfoldingTemplate :: Unfolding -> CoreExpr
1652unfoldingTemplate = uf_tmpl
1653
1654-- | Retrieves the template of an unfolding if possible
1655-- maybeUnfoldingTemplate is used mainly wnen specialising, and we do
1656-- want to specialise DFuns, so it's important to return a template
1657-- for DFunUnfoldings
1658maybeUnfoldingTemplate :: Unfolding -> Maybe CoreExpr
1659maybeUnfoldingTemplate (CoreUnfolding { uf_tmpl = expr })
1660  = Just expr
1661maybeUnfoldingTemplate (DFunUnfolding { df_bndrs = bndrs, df_con = con, df_args = args })
1662  = Just (mkLams bndrs (mkApps (Var (dataConWorkId con)) args))
1663maybeUnfoldingTemplate _
1664  = Nothing
1665
1666-- | The constructors that the unfolding could never be:
1667-- returns @[]@ if no information is available
1668otherCons :: Unfolding -> [AltCon]
1669otherCons (OtherCon cons) = cons
1670otherCons _               = []
1671
1672-- | Determines if it is certainly the case that the unfolding will
1673-- yield a value (something in HNF): returns @False@ if unsure
1674isValueUnfolding :: Unfolding -> Bool
1675        -- Returns False for OtherCon
1676isValueUnfolding (CoreUnfolding { uf_is_value = is_evald }) = is_evald
1677isValueUnfolding _                                          = False
1678
1679-- | Determines if it possibly the case that the unfolding will
1680-- yield a value. Unlike 'isValueUnfolding' it returns @True@
1681-- for 'OtherCon'
1682isEvaldUnfolding :: Unfolding -> Bool
1683        -- Returns True for OtherCon
1684isEvaldUnfolding (OtherCon _)                               = True
1685isEvaldUnfolding (CoreUnfolding { uf_is_value = is_evald }) = is_evald
1686isEvaldUnfolding _                                          = False
1687
1688-- | @True@ if the unfolding is a constructor application, the application
1689-- of a CONLIKE function or 'OtherCon'
1690isConLikeUnfolding :: Unfolding -> Bool
1691isConLikeUnfolding (OtherCon _)                             = True
1692isConLikeUnfolding (CoreUnfolding { uf_is_conlike = con })  = con
1693isConLikeUnfolding _                                        = False
1694
1695-- | Is the thing we will unfold into certainly cheap?
1696isCheapUnfolding :: Unfolding -> Bool
1697isCheapUnfolding (CoreUnfolding { uf_is_work_free = is_wf }) = is_wf
1698isCheapUnfolding _                                           = False
1699
1700isExpandableUnfolding :: Unfolding -> Bool
1701isExpandableUnfolding (CoreUnfolding { uf_expandable = is_expable }) = is_expable
1702isExpandableUnfolding _                                              = False
1703
1704expandUnfolding_maybe :: Unfolding -> Maybe CoreExpr
1705-- Expand an expandable unfolding; this is used in rule matching
1706--   See Note [Expanding variables] in Rules.hs
1707-- The key point here is that CONLIKE things can be expanded
1708expandUnfolding_maybe (CoreUnfolding { uf_expandable = True, uf_tmpl = rhs }) = Just rhs
1709expandUnfolding_maybe _                                                       = Nothing
1710
1711isCompulsoryUnfolding :: Unfolding -> Bool
1712isCompulsoryUnfolding (CoreUnfolding { uf_src = InlineCompulsory }) = True
1713isCompulsoryUnfolding _                                             = False
1714
1715isStableUnfolding :: Unfolding -> Bool
1716-- True of unfoldings that should not be overwritten
1717-- by a CoreUnfolding for the RHS of a let-binding
1718isStableUnfolding (CoreUnfolding { uf_src = src }) = isStableSource src
1719isStableUnfolding (DFunUnfolding {})               = True
1720isStableUnfolding _                                = False
1721
1722-- | Only returns False if there is no unfolding information available at all
1723hasSomeUnfolding :: Unfolding -> Bool
1724hasSomeUnfolding NoUnfolding   = False
1725hasSomeUnfolding BootUnfolding = False
1726hasSomeUnfolding _             = True
1727
1728isBootUnfolding :: Unfolding -> Bool
1729isBootUnfolding BootUnfolding = True
1730isBootUnfolding _             = False
1731
1732neverUnfoldGuidance :: UnfoldingGuidance -> Bool
1733neverUnfoldGuidance UnfNever = True
1734neverUnfoldGuidance _        = False
1735
1736isFragileUnfolding :: Unfolding -> Bool
1737-- An unfolding is fragile if it mentions free variables or
1738-- is otherwise subject to change.  A robust one can be kept.
1739-- See Note [Fragile unfoldings]
1740isFragileUnfolding (CoreUnfolding {}) = True
1741isFragileUnfolding (DFunUnfolding {}) = True
1742isFragileUnfolding _                  = False
1743  -- NoUnfolding, BootUnfolding, OtherCon are all non-fragile
1744
1745canUnfold :: Unfolding -> Bool
1746canUnfold (CoreUnfolding { uf_guidance = g }) = not (neverUnfoldGuidance g)
1747canUnfold _                                   = False
1748
1749{- Note [Fragile unfoldings]
1750~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1751An unfolding is "fragile" if it mentions free variables (and hence would
1752need substitution) or might be affected by optimisation.  The non-fragile
1753ones are
1754
1755   NoUnfolding, BootUnfolding
1756
1757   OtherCon {}    If we know this binder (say a lambda binder) will be
1758                  bound to an evaluated thing, we want to retain that
1759                  info in simpleOptExpr; see #13077.
1760
1761We consider even a StableUnfolding as fragile, because it needs substitution.
1762
1763Note [InlineStable]
1764~~~~~~~~~~~~~~~~~
1765When you say
1766      {-# INLINE f #-}
1767      f x = <rhs>
1768you intend that calls (f e) are replaced by <rhs>[e/x] So we
1769should capture (\x.<rhs>) in the Unfolding of 'f', and never meddle
1770with it.  Meanwhile, we can optimise <rhs> to our heart's content,
1771leaving the original unfolding intact in Unfolding of 'f'. For example
1772        all xs = foldr (&&) True xs
1773        any p = all . map p  {-# INLINE any #-}
1774We optimise any's RHS fully, but leave the InlineRule saying "all . map p",
1775which deforests well at the call site.
1776
1777So INLINE pragma gives rise to an InlineRule, which captures the original RHS.
1778
1779Moreover, it's only used when 'f' is applied to the
1780specified number of arguments; that is, the number of argument on
1781the LHS of the '=' sign in the original source definition.
1782For example, (.) is now defined in the libraries like this
1783   {-# INLINE (.) #-}
1784   (.) f g = \x -> f (g x)
1785so that it'll inline when applied to two arguments. If 'x' appeared
1786on the left, thus
1787   (.) f g x = f (g x)
1788it'd only inline when applied to three arguments.  This slightly-experimental
1789change was requested by Roman, but it seems to make sense.
1790
1791See also Note [Inlining an InlineRule] in CoreUnfold.
1792
1793
1794Note [OccInfo in unfoldings and rules]
1795~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1796In unfoldings and rules, we guarantee that the template is occ-analysed,
1797so that the occurrence info on the binders is correct.  This is important,
1798because the Simplifier does not re-analyse the template when using it. If
1799the occurrence info is wrong
1800  - We may get more simplifier iterations than necessary, because
1801    once-occ info isn't there
1802  - More seriously, we may get an infinite loop if there's a Rec
1803    without a loop breaker marked
1804
1805
1806************************************************************************
1807*                                                                      *
1808                  AltCon
1809*                                                                      *
1810************************************************************************
1811-}
1812
1813-- The Ord is needed for the FiniteMap used in the lookForConstructor
1814-- in SimplEnv.  If you declared that lookForConstructor *ignores*
1815-- constructor-applications with LitArg args, then you could get
1816-- rid of this Ord.
1817
1818instance Outputable AltCon where
1819  ppr (DataAlt dc) = ppr dc
1820  ppr (LitAlt lit) = ppr lit
1821  ppr DEFAULT      = text "__DEFAULT"
1822
1823cmpAlt :: (AltCon, a, b) -> (AltCon, a, b) -> Ordering
1824cmpAlt (con1, _, _) (con2, _, _) = con1 `cmpAltCon` con2
1825
1826ltAlt :: (AltCon, a, b) -> (AltCon, a, b) -> Bool
1827ltAlt a1 a2 = (a1 `cmpAlt` a2) == LT
1828
1829cmpAltCon :: AltCon -> AltCon -> Ordering
1830-- ^ Compares 'AltCon's within a single list of alternatives
1831-- DEFAULT comes out smallest, so that sorting by AltCon puts
1832-- alternatives in the order required: see Note [Case expression invariants]
1833cmpAltCon DEFAULT      DEFAULT     = EQ
1834cmpAltCon DEFAULT      _           = LT
1835
1836cmpAltCon (DataAlt d1) (DataAlt d2) = dataConTag d1 `compare` dataConTag d2
1837cmpAltCon (DataAlt _)  DEFAULT      = GT
1838cmpAltCon (LitAlt  l1) (LitAlt  l2) = l1 `compare` l2
1839cmpAltCon (LitAlt _)   DEFAULT      = GT
1840
1841cmpAltCon con1 con2 = WARN( True, text "Comparing incomparable AltCons" <+>
1842                                  ppr con1 <+> ppr con2 )
1843                      LT
1844
1845{-
1846************************************************************************
1847*                                                                      *
1848\subsection{Useful synonyms}
1849*                                                                      *
1850************************************************************************
1851
1852Note [CoreProgram]
1853~~~~~~~~~~~~~~~~~~
1854The top level bindings of a program, a CoreProgram, are represented as
1855a list of CoreBind
1856
1857 * Later bindings in the list can refer to earlier ones, but not vice
1858   versa.  So this is OK
1859      NonRec { x = 4 }
1860      Rec { p = ...q...x...
1861          ; q = ...p...x }
1862      Rec { f = ...p..x..f.. }
1863      NonRec { g = ..f..q...x.. }
1864   But it would NOT be ok for 'f' to refer to 'g'.
1865
1866 * The occurrence analyser does strongly-connected component analysis
1867   on each Rec binding, and splits it into a sequence of smaller
1868   bindings where possible.  So the program typically starts life as a
1869   single giant Rec, which is then dependency-analysed into smaller
1870   chunks.
1871-}
1872
1873-- If you edit this type, you may need to update the GHC formalism
1874-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
1875type CoreProgram = [CoreBind]   -- See Note [CoreProgram]
1876
1877-- | The common case for the type of binders and variables when
1878-- we are manipulating the Core language within GHC
1879type CoreBndr = Var
1880-- | Expressions where binders are 'CoreBndr's
1881type CoreExpr = Expr CoreBndr
1882-- | Argument expressions where binders are 'CoreBndr's
1883type CoreArg  = Arg  CoreBndr
1884-- | Binding groups where binders are 'CoreBndr's
1885type CoreBind = Bind CoreBndr
1886-- | Case alternatives where binders are 'CoreBndr's
1887type CoreAlt  = Alt  CoreBndr
1888
1889{-
1890************************************************************************
1891*                                                                      *
1892\subsection{Tagging}
1893*                                                                      *
1894************************************************************************
1895-}
1896
1897-- | Binders are /tagged/ with a t
1898data TaggedBndr t = TB CoreBndr t       -- TB for "tagged binder"
1899
1900type TaggedBind t = Bind (TaggedBndr t)
1901type TaggedExpr t = Expr (TaggedBndr t)
1902type TaggedArg  t = Arg  (TaggedBndr t)
1903type TaggedAlt  t = Alt  (TaggedBndr t)
1904
1905instance Outputable b => Outputable (TaggedBndr b) where
1906  ppr (TB b l) = char '<' <> ppr b <> comma <> ppr l <> char '>'
1907
1908deTagExpr :: TaggedExpr t -> CoreExpr
1909deTagExpr (Var v)                   = Var v
1910deTagExpr (Lit l)                   = Lit l
1911deTagExpr (Type ty)                 = Type ty
1912deTagExpr (Coercion co)             = Coercion co
1913deTagExpr (App e1 e2)               = App (deTagExpr e1) (deTagExpr e2)
1914deTagExpr (Lam (TB b _) e)          = Lam b (deTagExpr e)
1915deTagExpr (Let bind body)           = Let (deTagBind bind) (deTagExpr body)
1916deTagExpr (Case e (TB b _) ty alts) = Case (deTagExpr e) b ty (map deTagAlt alts)
1917deTagExpr (Tick t e)                = Tick t (deTagExpr e)
1918deTagExpr (Cast e co)               = Cast (deTagExpr e) co
1919
1920deTagBind :: TaggedBind t -> CoreBind
1921deTagBind (NonRec (TB b _) rhs) = NonRec b (deTagExpr rhs)
1922deTagBind (Rec prs)             = Rec [(b, deTagExpr rhs) | (TB b _, rhs) <- prs]
1923
1924deTagAlt :: TaggedAlt t -> CoreAlt
1925deTagAlt (con, bndrs, rhs) = (con, [b | TB b _ <- bndrs], deTagExpr rhs)
1926
1927{-
1928************************************************************************
1929*                                                                      *
1930\subsection{Core-constructing functions with checking}
1931*                                                                      *
1932************************************************************************
1933-}
1934
1935-- | Apply a list of argument expressions to a function expression in a nested fashion. Prefer to
1936-- use 'MkCore.mkCoreApps' if possible
1937mkApps    :: Expr b -> [Arg b]  -> Expr b
1938-- | Apply a list of type argument expressions to a function expression in a nested fashion
1939mkTyApps  :: Expr b -> [Type]   -> Expr b
1940-- | Apply a list of coercion argument expressions to a function expression in a nested fashion
1941mkCoApps  :: Expr b -> [Coercion] -> Expr b
1942-- | Apply a list of type or value variables to a function expression in a nested fashion
1943mkVarApps :: Expr b -> [Var] -> Expr b
1944-- | Apply a list of argument expressions to a data constructor in a nested fashion. Prefer to
1945-- use 'MkCore.mkCoreConApps' if possible
1946mkConApp      :: DataCon -> [Arg b] -> Expr b
1947
1948mkApps    f args = foldl' App                       f args
1949mkCoApps  f args = foldl' (\ e a -> App e (Coercion a)) f args
1950mkVarApps f vars = foldl' (\ e a -> App e (varToCoreExpr a)) f vars
1951mkConApp con args = mkApps (Var (dataConWorkId con)) args
1952
1953mkTyApps  f args = foldl' (\ e a -> App e (mkTyArg a)) f args
1954
1955mkConApp2 :: DataCon -> [Type] -> [Var] -> Expr b
1956mkConApp2 con tys arg_ids = Var (dataConWorkId con)
1957                            `mkApps` map Type tys
1958                            `mkApps` map varToCoreExpr arg_ids
1959
1960mkTyArg :: Type -> Expr b
1961mkTyArg ty
1962  | Just co <- isCoercionTy_maybe ty = Coercion co
1963  | otherwise                        = Type ty
1964
1965-- | Create a machine integer literal expression of type @Int#@ from an @Integer@.
1966-- If you want an expression of type @Int@ use 'MkCore.mkIntExpr'
1967mkIntLit      :: DynFlags -> Integer -> Expr b
1968-- | Create a machine integer literal expression of type @Int#@ from an @Int@.
1969-- If you want an expression of type @Int@ use 'MkCore.mkIntExpr'
1970mkIntLitInt   :: DynFlags -> Int     -> Expr b
1971
1972mkIntLit    dflags n = Lit (mkLitInt dflags n)
1973mkIntLitInt dflags n = Lit (mkLitInt dflags (toInteger n))
1974
1975-- | Create a machine word literal expression of type  @Word#@ from an @Integer@.
1976-- If you want an expression of type @Word@ use 'MkCore.mkWordExpr'
1977mkWordLit     :: DynFlags -> Integer -> Expr b
1978-- | Create a machine word literal expression of type  @Word#@ from a @Word@.
1979-- If you want an expression of type @Word@ use 'MkCore.mkWordExpr'
1980mkWordLitWord :: DynFlags -> Word -> Expr b
1981
1982mkWordLit     dflags w = Lit (mkLitWord dflags w)
1983mkWordLitWord dflags w = Lit (mkLitWord dflags (toInteger w))
1984
1985mkWord64LitWord64 :: Word64 -> Expr b
1986mkWord64LitWord64 w = Lit (mkLitWord64 (toInteger w))
1987
1988mkInt64LitInt64 :: Int64 -> Expr b
1989mkInt64LitInt64 w = Lit (mkLitInt64 (toInteger w))
1990
1991-- | Create a machine character literal expression of type @Char#@.
1992-- If you want an expression of type @Char@ use 'MkCore.mkCharExpr'
1993mkCharLit :: Char -> Expr b
1994-- | Create a machine string literal expression of type @Addr#@.
1995-- If you want an expression of type @String@ use 'MkCore.mkStringExpr'
1996mkStringLit :: String -> Expr b
1997
1998mkCharLit   c = Lit (mkLitChar c)
1999mkStringLit s = Lit (mkLitString s)
2000
2001-- | Create a machine single precision literal expression of type @Float#@ from a @Rational@.
2002-- If you want an expression of type @Float@ use 'MkCore.mkFloatExpr'
2003mkFloatLit :: Rational -> Expr b
2004-- | Create a machine single precision literal expression of type @Float#@ from a @Float@.
2005-- If you want an expression of type @Float@ use 'MkCore.mkFloatExpr'
2006mkFloatLitFloat :: Float -> Expr b
2007
2008mkFloatLit      f = Lit (mkLitFloat f)
2009mkFloatLitFloat f = Lit (mkLitFloat (toRational f))
2010
2011-- | Create a machine double precision literal expression of type @Double#@ from a @Rational@.
2012-- If you want an expression of type @Double@ use 'MkCore.mkDoubleExpr'
2013mkDoubleLit :: Rational -> Expr b
2014-- | Create a machine double precision literal expression of type @Double#@ from a @Double@.
2015-- If you want an expression of type @Double@ use 'MkCore.mkDoubleExpr'
2016mkDoubleLitDouble :: Double -> Expr b
2017
2018mkDoubleLit       d = Lit (mkLitDouble d)
2019mkDoubleLitDouble d = Lit (mkLitDouble (toRational d))
2020
2021-- | Bind all supplied binding groups over an expression in a nested let expression. Assumes
2022-- that the rhs satisfies the let/app invariant.  Prefer to use 'MkCore.mkCoreLets' if
2023-- possible, which does guarantee the invariant
2024mkLets        :: [Bind b] -> Expr b -> Expr b
2025-- | Bind all supplied binders over an expression in a nested lambda expression. Prefer to
2026-- use 'MkCore.mkCoreLams' if possible
2027mkLams        :: [b] -> Expr b -> Expr b
2028
2029mkLams binders body = foldr Lam body binders
2030mkLets binds body   = foldr mkLet body binds
2031
2032mkLet :: Bind b -> Expr b -> Expr b
2033-- The desugarer sometimes generates an empty Rec group
2034-- which Lint rejects, so we kill it off right away
2035mkLet (Rec []) body = body
2036mkLet bind     body = Let bind body
2037
2038-- | @mkLetNonRec bndr rhs body@ wraps @body@ in a @let@ binding @bndr@.
2039mkLetNonRec :: b -> Expr b -> Expr b -> Expr b
2040mkLetNonRec b rhs body = Let (NonRec b rhs) body
2041
2042-- | @mkLetRec binds body@ wraps @body@ in a @let rec@ with the given set of
2043-- @binds@ if binds is non-empty.
2044mkLetRec :: [(b, Expr b)] -> Expr b -> Expr b
2045mkLetRec [] body = body
2046mkLetRec bs body = Let (Rec bs) body
2047
2048-- | Create a binding group where a type variable is bound to a type. Per "CoreSyn#type_let",
2049-- this can only be used to bind something in a non-recursive @let@ expression
2050mkTyBind :: TyVar -> Type -> CoreBind
2051mkTyBind tv ty      = NonRec tv (Type ty)
2052
2053-- | Create a binding group where a type variable is bound to a type. Per "CoreSyn#type_let",
2054-- this can only be used to bind something in a non-recursive @let@ expression
2055mkCoBind :: CoVar -> Coercion -> CoreBind
2056mkCoBind cv co      = NonRec cv (Coercion co)
2057
2058-- | Convert a binder into either a 'Var' or 'Type' 'Expr' appropriately
2059varToCoreExpr :: CoreBndr -> Expr b
2060varToCoreExpr v | isTyVar v = Type (mkTyVarTy v)
2061                | isCoVar v = Coercion (mkCoVarCo v)
2062                | otherwise = ASSERT( isId v ) Var v
2063
2064varsToCoreExprs :: [CoreBndr] -> [Expr b]
2065varsToCoreExprs vs = map varToCoreExpr vs
2066
2067{-
2068************************************************************************
2069*                                                                      *
2070   Getting a result type
2071*                                                                      *
2072************************************************************************
2073
2074These are defined here to avoid a module loop between CoreUtils and CoreFVs
2075
2076-}
2077
2078applyTypeToArg :: Type -> CoreExpr -> Type
2079-- ^ Determines the type resulting from applying an expression with given type
2080-- to a given argument expression
2081applyTypeToArg fun_ty arg = piResultTy fun_ty (exprToType arg)
2082
2083-- | If the expression is a 'Type', converts. Otherwise,
2084-- panics. NB: This does /not/ convert 'Coercion' to 'CoercionTy'.
2085exprToType :: CoreExpr -> Type
2086exprToType (Type ty)     = ty
2087exprToType _bad          = pprPanic "exprToType" empty
2088
2089-- | If the expression is a 'Coercion', converts.
2090exprToCoercion_maybe :: CoreExpr -> Maybe Coercion
2091exprToCoercion_maybe (Coercion co) = Just co
2092exprToCoercion_maybe _             = Nothing
2093
2094{-
2095************************************************************************
2096*                                                                      *
2097\subsection{Simple access functions}
2098*                                                                      *
2099************************************************************************
2100-}
2101
2102-- | Extract every variable by this group
2103bindersOf  :: Bind b -> [b]
2104-- If you edit this function, you may need to update the GHC formalism
2105-- See Note [GHC Formalism] in coreSyn/CoreLint.hs
2106bindersOf (NonRec binder _) = [binder]
2107bindersOf (Rec pairs)       = [binder | (binder, _) <- pairs]
2108
2109-- | 'bindersOf' applied to a list of binding groups
2110bindersOfBinds :: [Bind b] -> [b]
2111bindersOfBinds binds = foldr ((++) . bindersOf) [] binds
2112
2113rhssOfBind :: Bind b -> [Expr b]
2114rhssOfBind (NonRec _ rhs) = [rhs]
2115rhssOfBind (Rec pairs)    = [rhs | (_,rhs) <- pairs]
2116
2117rhssOfAlts :: [Alt b] -> [Expr b]
2118rhssOfAlts alts = [e | (_,_,e) <- alts]
2119
2120-- | Collapse all the bindings in the supplied groups into a single
2121-- list of lhs\/rhs pairs suitable for binding in a 'Rec' binding group
2122flattenBinds :: [Bind b] -> [(b, Expr b)]
2123flattenBinds (NonRec b r : binds) = (b,r) : flattenBinds binds
2124flattenBinds (Rec prs1   : binds) = prs1 ++ flattenBinds binds
2125flattenBinds []                   = []
2126
2127-- | We often want to strip off leading lambdas before getting down to
2128-- business. Variants are 'collectTyBinders', 'collectValBinders',
2129-- and 'collectTyAndValBinders'
2130collectBinders         :: Expr b   -> ([b],     Expr b)
2131collectTyBinders       :: CoreExpr -> ([TyVar], CoreExpr)
2132collectValBinders      :: CoreExpr -> ([Id],    CoreExpr)
2133collectTyAndValBinders :: CoreExpr -> ([TyVar], [Id], CoreExpr)
2134-- | Strip off exactly N leading lambdas (type or value). Good for use with
2135-- join points.
2136collectNBinders        :: Int -> Expr b -> ([b], Expr b)
2137
2138collectBinders expr
2139  = go [] expr
2140  where
2141    go bs (Lam b e) = go (b:bs) e
2142    go bs e          = (reverse bs, e)
2143
2144collectTyBinders expr
2145  = go [] expr
2146  where
2147    go tvs (Lam b e) | isTyVar b = go (b:tvs) e
2148    go tvs e                     = (reverse tvs, e)
2149
2150collectValBinders expr
2151  = go [] expr
2152  where
2153    go ids (Lam b e) | isId b = go (b:ids) e
2154    go ids body               = (reverse ids, body)
2155
2156collectTyAndValBinders expr
2157  = (tvs, ids, body)
2158  where
2159    (tvs, body1) = collectTyBinders expr
2160    (ids, body)  = collectValBinders body1
2161
2162collectNBinders orig_n orig_expr
2163  = go orig_n [] orig_expr
2164  where
2165    go 0 bs expr      = (reverse bs, expr)
2166    go n bs (Lam b e) = go (n-1) (b:bs) e
2167    go _ _  _         = pprPanic "collectNBinders" $ int orig_n
2168
2169-- | Takes a nested application expression and returns the function
2170-- being applied and the arguments to which it is applied
2171collectArgs :: Expr b -> (Expr b, [Arg b])
2172collectArgs expr
2173  = go expr []
2174  where
2175    go (App f a) as = go f (a:as)
2176    go e         as = (e, as)
2177
2178-- | Attempt to remove the last N arguments of a function call.
2179-- Strip off any ticks or coercions encountered along the way and any
2180-- at the end.
2181stripNArgs :: Word -> Expr a -> Maybe (Expr a)
2182stripNArgs !n (Tick _ e) = stripNArgs n e
2183stripNArgs n (Cast f _) = stripNArgs n f
2184stripNArgs 0 e = Just e
2185stripNArgs n (App f _) = stripNArgs (n - 1) f
2186stripNArgs _ _ = Nothing
2187
2188-- | Like @collectArgs@, but also collects looks through floatable
2189-- ticks if it means that we can find more arguments.
2190collectArgsTicks :: (Tickish Id -> Bool) -> Expr b
2191                 -> (Expr b, [Arg b], [Tickish Id])
2192collectArgsTicks skipTick expr
2193  = go expr [] []
2194  where
2195    go (App f a)  as ts = go f (a:as) ts
2196    go (Tick t e) as ts
2197      | skipTick t      = go e as (t:ts)
2198    go e          as ts = (e, as, reverse ts)
2199
2200
2201{-
2202************************************************************************
2203*                                                                      *
2204\subsection{Predicates}
2205*                                                                      *
2206************************************************************************
2207
2208At one time we optionally carried type arguments through to runtime.
2209@isRuntimeVar v@ returns if (Lam v _) really becomes a lambda at runtime,
2210i.e. if type applications are actual lambdas because types are kept around
2211at runtime.  Similarly isRuntimeArg.
2212-}
2213
2214-- | Will this variable exist at runtime?
2215isRuntimeVar :: Var -> Bool
2216isRuntimeVar = isId
2217
2218-- | Will this argument expression exist at runtime?
2219isRuntimeArg :: CoreExpr -> Bool
2220isRuntimeArg = isValArg
2221
2222-- | Returns @True@ for value arguments, false for type args
2223-- NB: coercions are value arguments (zero width, to be sure,
2224-- like State#, but still value args).
2225isValArg :: Expr b -> Bool
2226isValArg e = not (isTypeArg e)
2227
2228-- | Returns @True@ iff the expression is a 'Type' or 'Coercion'
2229-- expression at its top level
2230isTyCoArg :: Expr b -> Bool
2231isTyCoArg (Type {})     = True
2232isTyCoArg (Coercion {}) = True
2233isTyCoArg _             = False
2234
2235-- | Returns @True@ iff the expression is a 'Coercion'
2236-- expression at its top level
2237isCoArg :: Expr b -> Bool
2238isCoArg (Coercion {}) = True
2239isCoArg _             = False
2240
2241-- | Returns @True@ iff the expression is a 'Type' expression at its
2242-- top level.  Note this does NOT include 'Coercion's.
2243isTypeArg :: Expr b -> Bool
2244isTypeArg (Type {}) = True
2245isTypeArg _         = False
2246
2247-- | The number of binders that bind values rather than types
2248valBndrCount :: [CoreBndr] -> Int
2249valBndrCount = count isId
2250
2251-- | The number of argument expressions that are values rather than types at their top level
2252valArgCount :: [Arg b] -> Int
2253valArgCount = count isValArg
2254
2255{-
2256************************************************************************
2257*                                                                      *
2258\subsection{Annotated core}
2259*                                                                      *
2260************************************************************************
2261-}
2262
2263-- | Annotated core: allows annotation at every node in the tree
2264type AnnExpr bndr annot = (annot, AnnExpr' bndr annot)
2265
2266-- | A clone of the 'Expr' type but allowing annotation at every tree node
2267data AnnExpr' bndr annot
2268  = AnnVar      Id
2269  | AnnLit      Literal
2270  | AnnLam      bndr (AnnExpr bndr annot)
2271  | AnnApp      (AnnExpr bndr annot) (AnnExpr bndr annot)
2272  | AnnCase     (AnnExpr bndr annot) bndr Type [AnnAlt bndr annot]
2273  | AnnLet      (AnnBind bndr annot) (AnnExpr bndr annot)
2274  | AnnCast     (AnnExpr bndr annot) (annot, Coercion)
2275                   -- Put an annotation on the (root of) the coercion
2276  | AnnTick     (Tickish Id) (AnnExpr bndr annot)
2277  | AnnType     Type
2278  | AnnCoercion Coercion
2279
2280-- | A clone of the 'Alt' type but allowing annotation at every tree node
2281type AnnAlt bndr annot = (AltCon, [bndr], AnnExpr bndr annot)
2282
2283-- | A clone of the 'Bind' type but allowing annotation at every tree node
2284data AnnBind bndr annot
2285  = AnnNonRec bndr (AnnExpr bndr annot)
2286  | AnnRec    [(bndr, AnnExpr bndr annot)]
2287
2288-- | Takes a nested application expression and returns the function
2289-- being applied and the arguments to which it is applied
2290collectAnnArgs :: AnnExpr b a -> (AnnExpr b a, [AnnExpr b a])
2291collectAnnArgs expr
2292  = go expr []
2293  where
2294    go (_, AnnApp f a) as = go f (a:as)
2295    go e               as = (e, as)
2296
2297collectAnnArgsTicks :: (Tickish Var -> Bool) -> AnnExpr b a
2298                       -> (AnnExpr b a, [AnnExpr b a], [Tickish Var])
2299collectAnnArgsTicks tickishOk expr
2300  = go expr [] []
2301  where
2302    go (_, AnnApp f a)  as ts = go f (a:as) ts
2303    go (_, AnnTick t e) as ts | tickishOk t
2304                              = go e as (t:ts)
2305    go e                as ts = (e, as, reverse ts)
2306
2307deAnnotate :: AnnExpr bndr annot -> Expr bndr
2308deAnnotate (_, e) = deAnnotate' e
2309
2310deAnnotate' :: AnnExpr' bndr annot -> Expr bndr
2311deAnnotate' (AnnType t)           = Type t
2312deAnnotate' (AnnCoercion co)      = Coercion co
2313deAnnotate' (AnnVar  v)           = Var v
2314deAnnotate' (AnnLit  lit)         = Lit lit
2315deAnnotate' (AnnLam  binder body) = Lam binder (deAnnotate body)
2316deAnnotate' (AnnApp  fun arg)     = App (deAnnotate fun) (deAnnotate arg)
2317deAnnotate' (AnnCast e (_,co))    = Cast (deAnnotate e) co
2318deAnnotate' (AnnTick tick body)   = Tick tick (deAnnotate body)
2319
2320deAnnotate' (AnnLet bind body)
2321  = Let (deAnnBind bind) (deAnnotate body)
2322deAnnotate' (AnnCase scrut v t alts)
2323  = Case (deAnnotate scrut) v t (map deAnnAlt alts)
2324
2325deAnnAlt :: AnnAlt bndr annot -> Alt bndr
2326deAnnAlt (con,args,rhs) = (con,args,deAnnotate rhs)
2327
2328deAnnBind  :: AnnBind b annot -> Bind b
2329deAnnBind (AnnNonRec var rhs) = NonRec var (deAnnotate rhs)
2330deAnnBind (AnnRec pairs) = Rec [(v,deAnnotate rhs) | (v,rhs) <- pairs]
2331
2332-- | As 'collectBinders' but for 'AnnExpr' rather than 'Expr'
2333collectAnnBndrs :: AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
2334collectAnnBndrs e
2335  = collect [] e
2336  where
2337    collect bs (_, AnnLam b body) = collect (b:bs) body
2338    collect bs body               = (reverse bs, body)
2339
2340-- | As 'collectNBinders' but for 'AnnExpr' rather than 'Expr'
2341collectNAnnBndrs :: Int -> AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
2342collectNAnnBndrs orig_n e
2343  = collect orig_n [] e
2344  where
2345    collect 0 bs body               = (reverse bs, body)
2346    collect n bs (_, AnnLam b body) = collect (n-1) (b:bs) body
2347    collect _ _  _                  = pprPanic "collectNBinders" $ int orig_n
2348