`-fext-core`

. We expect that GHC 6.10 will be
+ consistent with this definition.Core,as its internal program representation within the + compiler’s simplification phase. Core resembles a subset of + Haskell, but with explicit type annotations in the style of the + polymorphic lambda calculus (F

Core Lintpass in GHC + typechecks Core in between transformation passes (at least when + the user enables linting by setting a compiler flag), verifying + that transformations preserve type-correctness. Finally, GHC’s + back-end translates Core into STG-machine code

`utils/ext-core`

.`fibonacci`

+ `%module`

mident { tdef ; }{ vdefg ; }
+ `%data`

qtycon { tbind } `= {`

[ cdef {`;`

cdef } ] `}`

+ `%newtype`

qtycon qtycon { tbind } `=`

ty
+ `@`

tbind }{ aty }`%rec {`

vdef { `;`

vdef } `}`

`::`

ty `=`

exp`(`

exp `)`

`\`

{ binder }`%let`

vdefg `%in`

exp`%case (`

aty `)`

exp `%of`

vbind `{`

alt { `;`

alt } `}`

`%cast`

exp aty`%note`

" { char } " exp`%external ccall "`

{ char } `"`

aty`%dynexternal ccall`

aty`%label "`

{ char } `"`

`@`

aty`@`

tbind }{ vbind } `&arw;`

exp`&arw;`

exp`%_ &arw;`

exp`@`

tbind`(`

tyvar `::`

kind `)`

`(`

var `::`

ty `)`

`(`

[`-`

] { digit }`::`

ty `)`

`(`

[`-`

] { digit }`%`

{ digit }`::`

ty `)`

`( '`

char `' ::`

ty `)`

`( "`

{ char } `" ::`

ty `)`

`\x`

hex hex`(`

ty `)`

`%trans`

aty aty`%sym`

aty`%unsafe`

aty aty`%left`

aty`%right`

aty`%inst`

aty aty`%forall`

{ tbind }`.`

ty`&arw;`

ty`*`

`#`

`?`

`:=:`

bty`(`

kind `)`

`&arw;`

kind`:`

uname`.`

tycon`.`

dcon`.`

] var`a`

∣ `b`

∣ … ∣ `z`

∣ `_`

`A`

∣ `B`

∣ … ∣ `Z`

`0`

∣ `1`

∣ … ∣ `9`

`let`

bindings, algebraic type definitions, constructors, and
+ `case`

expressions, and primitive types, literals and operators. Its
+ type system is richer than that of System F, supporting explicit
+ type equality coercions and type functions.`base:GHC.Base`

is the module identifier for GHC’s Base
+ module. Its name is `Base`

, and it lives in the GHC hierarchy
+ within the `base`

package. Section 5.8 of the GHC users’ guide
+ explains package names `newtype`

+ declarations, each defining a type constructor and a
+ coercion name; and
+ `base:GHC.Base.Bool`

, `base:GHC.Base.True`

). Internal identifiers
+ are visible only within the defining module. All type and data
+ constructors are external, and are always defined and referenced
+ using fully qualified names (with dots).`main:MyModule.foo = ...`

);
+ otherwise, it is internal (e.g., `bar = ...`

). Note that
+ Core’s notion of an external identifier does not necessarily
+ coincide with that of exportedidentifier in a Haskell source + module. An identifier can be an external identifier in Core, but + not be exported by the original Haskell source + module.

`Main.foo = ... Main.bar ...`

, where
+ `Main.foo`

is inlineable. Since `bar`

appears in `foo`

’s unfolding, it is
+ defined and referenced with an external name, even if `bar`

was not
+ exported by the original source module.
+ `ghc-prim:GHC.Prim`

,
+ which exports the built-intypes and values that must be + provided by any implementation of Core (including GHC). Details + of this module are in the Primitive Module section.

`main:ZCMain.main`

of type `base:GHC.IOBase.IO a`

(for some
+ type `a`

). (Note that the strangely named wrapper for `main`

is the
+ one exception to the rule that qualified names defined within a
+ module `m`

must have module name `m`

.)`base:GHC.Base`

, which implement parts of the Haskell standard
+ library. In principle, these modules are ordinary Haskell
+ modules, with no special status. In practice, the requirement on
+ the type of `main:Main.main`

implies that every program will
+ contain a large subset of the standard library modules.`mident`

),`tycon`

),`tyvar`

),`dcon`

),`var`

).shadowone another: the scope of the definition of a given + variable never contains a redefinition of the same variable. + Type variables may be shadowed. Thus, if a term variable has + multiple definitions within a module, all those definitions must + be local (let-bound). The only exception to this rule is that + (necessarily closed) types labelling

`%external`

expressions may
+ contain `tyvar`

bindings that shadow outer bindings.`Z`

and `z`

are used to introduce escape sequences
+ for non-alphabetic characters such as dollar sign `$`

(`zd`

), hash `#`

+ (`zh`

), plus `+`

(`zp`

), etc. This is the same encoding used in `.hi`

+ files and in the back-end of GHC itself, except that we
+ sometimes change an initial `z`

to `Z`

, or vice-versa, in order to
+ maintain case distinctions.`base:GHC.Base.foo`

is rendered as
+ `base:GHCziBase.foo`

. A parser may reconstruct the module
+ hierarchy, or regard `GHCziBase`

as a flat name.`&arw;`

). (The prefix identifier for
+ this constructor is `ghc-prim:GHC.Prim.ZLzmzgZR`

; this should
+ only appear in unapplied or partially applied form.)`Intzh`

) that are predefined in the `GHC.Prim`

module, but
+ have no special syntax. `%data`

and `%newtype`

declarations
+ introduce additional type constructors, as described below.
+ Type constructors are distinguished solely by name.`newtype`

declaration,
+ has an equality kind.`ByteArrayzh`

). Peyton-Jones
+ and Launchbury `*`

, `#`

, and `?`

. The three base kinds
+ distinguish the liftedness of the types they classify: `*`

+ represents lifted types; `#`

represents unlifted types; and `?`

is
+ the openkind, representing a type that may be either lifted + or unlifted. Of these, only

`*`

ever appears in Core type
+ declarations generated from user code; the other two are
+ needed to describe certain types in primitive (or otherwise
+ specially-generated) code (which, after optimization, could
+ potentially appear anywhere).`ghc-prim:GHC.Prim`

) has a type of kind `#`

or `?`

.`Int`

has kind `*`

, and `Int#`

has kind `#`

.`&arw;`

k`&arw;`

has
+ kind `* &arw; (* &arw; *)`

. Since Haskell allows abstracting
+ over type constructors, type variables may have higher kinds;
+ however, much more commonly they have kind `*`

, so that is the
+ default if a type binder omits a kind.`type`

declarations).`%forall`

types;`&arw;`

b ≡ `ghc-prim:GHC.Prim.ZLzmzgZR`

a b`data`

declaration. For example, the
+ source declaration
+
+ `*&arw;*`

and two data constructors with types
+
+ `Fork`

has arity 2 and `Leaf`

has
+ arity 1.`T`

is
+ shown in the Expression Forms section.`%newtype`

declaration introduces a new type constructor
+ and an associated representation type, corresponding to a source
+ Haskell `newtype`

declaration. However, unlike in source Haskell,
+ a `%newtype`

declaration does not introduce any data constructors.`%newtype`

declaration also introduces a new coercion
+ (syntactically, just another type constructor) that implies an
+ axiom equating the type constructor, applied to any type
+ variables bound by the `%newtype`

, to the representation type.`newtype`

declaration implies that the types `U`

and `Bool`

have
+ equivalent representations, and the coercion axiom `ZCCoU`

+ provides evidence that `U`

is equivalent to `Bool`

. Notice that in
+ the body of `u`

, the boolean value `True`

is cast to type `U`

using
+ the primitive symmetry rule applied to `ZCCoU`

: that is, using a
+ coercion of kind `Bool :=: U`

. And in the body of `v`

, `u`

is cast
+ back to type `Bool`

using the axiom `ZCCoU`

.`case`

in the Haskell source code above translates
+ to a `cast`

in the corresponding Core code. That is because
+ operationally, a `case`

on a value whose type is declared by a
+ `newtype`

declaration is a no-op. Unlike a `case`

on any other
+ value, such a `case`

does no evaluation: its only function is to
+ coerce its scrutinee’s type.`@ symbol`

. (In
+ abstractions, the `@`

plays essentially the same role as the more
+ usual Λ symbol.) For example, the Haskell source declaration
+
+ `*`

. For example, given our previous definition of type `A`

:
+
+ `%let`

expressions in the
+ usual way.`%case`

. `%case`

+ expressions are permitted over values of any type, although they
+ will normally be algebraic or primitive types (with literal
+ values). Evaluating a `%case`

forces the evaluation of the
+ expression being tested (the scrutinee). The value of the + scrutinee is bound to the variable following the

`%of`

keyword,
+ which is in scope in all alternatives; this is useful when the
+ scrutinee is a non-atomic expression (see next example). The
+ scrutinee is preceded by the type of the entire `%case`

+ expression: that is, the result type that all of the `%case`

+ alternatives have (this is intended to make type reconstruction
+ easier in the presence of type equality coercions).`%case`

, all the case alternatives must be labeled
+ with distinct data constructors from the algebraic type,
+ followed by any existential type variable bindings (see below),
+ and typed term variable bindings corresponding to the data
+ constructor’s arguments. The number of variables must match the
+ data constructor’s arity.`%case`

over a value of an
+ existentially-quantified algebraic type, the alternative must
+ include extra local type bindings for the
+ existentially-quantified variables. For example, given
+
+ `%case`

over literal alternatives, all the case alternatives
+ must be distinct literals of the same primitive type.`%_`

), whose right-hand side will be
+ evaluated if none of the other alternatives match. The default
+ is optional except for in a case over a primitive type, or when
+ there are no other alternatives. If the case is over neither an
+ algebraic type nor a primitive type, then the list of
+ alternatives must contain a default alternative and nothing
+ else. For algebraic cases, the set of alternatives need not be
+ exhaustive, even if no default is given; if alternatives are
+ missing, this implies that GHC has deduced that they cannot
+ occur.`%cast`

is used to manipulate newtypes, as described in
+ the Newtype section. The `%cast`

expression
+ takes an expression and a coercion: syntactically, the coercion
+ is an arbitrary type, but it must have an equality kind. In an
+ expression `(cast e co)`

, if `e :: T`

and `co`

has kind `T :=: U`

, then
+ the overall expression has type `U`

`co`

must be a
+ coercion whose left-hand side is `T`

.`%coerce`

expression that existed in previous
+ versions of Core, this means that `%cast`

is (almost) type-safe:
+ the coercion argument provides evidence that can be verified by
+ a typechecker. There are still unsafe `%cast`

s, corresponding to
+ the unsafe `%coerce`

construct that existed in old versions of
+ Core, because there is a primitive unsafe coercion type that can
+ be used to cast arbitrary types to each other. GHC uses this for
+ such purposes as coercing the return type of a function (such as
+ error) which is guaranteed to never return:
+
+ `%cast`

has no operational meaning and is only used in
+ typechecking.`%note`

expression carries arbitrary internal information that
+ GHC finds interesting. The information is encoded as a string.
+ Expression notes currently generated by GHC include the inlining
+ pragma (`InlineMe`

) and cost-center labels for profiling.`%external`

expression denotes an external identifier, which has
+ the indicated type (always expressed in terms of Haskell
+ primitive types). External Core supports two kinds of external
+ calls: `%external`

and `%dynexternal`

. Only the former is supported
+ by the current set of stand-alone Core tools. In addition, there
+ is a `%label`

construct which GHC may generate but which the Core
+ tools do not support.well-behavedCore + programs in which constructor and primitive applications are + fully saturated, and in which non-trivial expresssions of + unlifted kind (

`#`

) appear only as scrutinees in `%case`

+ expressions. Any program can easily be put into this form; a
+ separately available preprocessor illustrates how. In the
+ remainder of this section, we use Coreto mean

well-behaved+ Core.

`main:ZCMain.main`

.`let`

-bound (local)).
+ `case`

expression; or`%external`

functions cause side-effects to
+ state threads or to the real world. Where the ordering of these
+ side-effects matters, Core already forces this order with data
+ dependencies on the pseudo-values representing the threads.`raisezh`

and
+ `handlezh`

primitives: for example, by using a handler stack.
+ Again, real-world threading guarantees that they will execute in
+ the correct order.`ghc-prim:GHC.Prim`

. Nearly all
+ the primitives are required in order to cover GHC’s implementation
+ of the Haskell98 standard prelude; the only operators that can be
+ completely omitted are those supporting the byte-code interpreter,
+ parallelism, and foreign objects. Some of the concurrency
+ primitives are needed, but can be given degenerate implementations
+ if it desired to target a purely sequential backend (see Section
+ the Non-concurrent Back End section).`ThreadIdzh`

can be represented by a singleton type, whose
+ (unique) value is returned by `myThreadIdzh`

.
+ `forkzh`

can just die with an unimplementedmessage. +

`killThreadzh`

and `yieldzh`

can also just die unimplemented+ since in a one-thread world, the only thread a thread can + kill is itself, and if a thread yields the program hangs. +

`MVarzh a`

can be represented by `MutVarzh (Maybe a)`

; where a
+ concurrent implementation would block, the sequential
+ implementation can just die with a suitable message (since
+ no other thread exists to unblock it).
+ `waitReadzh`

and `waitWritezh`

can be implemented using a `select`

+ with no timeout.
+ `Intzh`

`Wordzh`

`Addrzh`

`Charzh`

`Floatzh`

`Doublezh`

`Charzh`

`Addrzh`