Elm Exactly
A Standard Definition of Elm?Draft version 0.4 / Appendix A

ADerived Forms

Several derived forms are provided in the Core and in Modules; they are presented in Figures 27, 28, 29 and 30. Each derived form is given with its equivalent form, and each row of the tables is a rewriting rule

Derived form  ⟹  Equivalent form \text{Derived form} \;\Longrightarrow\; \text{Equivalent form}

which may be applied to any subphrase, repeatedly, until the phrase is a phrase of the bare language of Chapters 2 and 3. Within a figure the rows are ordered, and the figures follow one another, so that every equivalent form uses only bare syntax and the derived forms of earlier rows. Two rows, the grouping of the definitions of a let and the grouping of the declarations of a module, are not local rewritings; their equivalent forms are defined in Sections A.1 and A.2. See Appendix B for the full grammar, including all the derived forms.

In equivalent forms, Basics.True and Basics.False are the constructors of the type Bool\tBool, Basics.negate is the negation function, and :: is the list constructor. They are looked up as any other identifier of the module in which the derived form occurs. Because only an import of the module Basics provides the qualifier Basics (Section 3.7), Basics.True, Basics.False and Basics.negate always denote the bindings of the initial basis (Appendices C and D), whatever the module imports or declares; :: is bound in the initial environment E0E_0 of those appendices, from which every module is elaborated and evaluated, and no program declares it. Being qualified, or an operator, none of them is a free occurrence of a variable in the sense below.

In the first row of Figure 27, exp1 op exp2\expm_1\ \opv\ \expm_2 is an infixed application as grouped by the resolution of Section 2.7: exp1\expm_1 and exp2\expm_2 are the two operands of op\opv after grouping, and op\opv is neither && nor ||. Each operator of a chain is rewritten in this way, outermost first. The infixed forms of && and || have rows of their own, which make them conditionals; the operator values (&&) and (||) are bare, and denote functions of the initial basis that take both arguments as values. In the row for negation, -atexp\kw{-}\atexp is a negation as defined in Section 2.5; it is atomic, so that it may be the argument of an application. In the row for field access, the atomic expressions that may precede .lab\kw{.}\lab are restricted in Section 2.10 (the restrictions on records); after any other atomic expression, and after a field accessor, a . begins a field accessor (Section 2.5), so .lab1.lab2\kw{.}\lab_1\kw{.}\lab_2 is the application of .lab1\kw{.}\lab_1 to .lab2\kw{.}\lab_2. In the equivalent form of the field accessor .lab\kw{.}\lab, the record pattern { lab = lab }\kw{\kwlb}\ \lab\ \kw{=}\ \lab\ \kw{\kwrb} binds the variable spelt as the label lab\lab, and the body of the lambda is that variable. The pattern row is written in the bare syntax, lab = var\lab\ \kw{=}\ \var with var\var spelt as lab\lab, since the pattern row written as a label alone is a derived form of a later figure. An annotation var : ty\var\ \kw{:}\ \ty and the definition of var\var that follows it (Chapter 2) are written together, here as in the bare grammar, as one value binding with : ty\kw{:}\ \ty after var\var. Thus a definition is a value binding without and, of the form var ⟨: ty⟩ atpat1⋯atpatn = exp\var\ \opt{\kw{:}\ \ty}\ \atpat_1\cdots\atpat_n\ \kw{=}\ \expm (n≥0n\geq 0) or atpat = exp\atpat\ \kw{=}\ \expm.

The syntactic restrictions of Chapters 2 and 3 apply to a program as written, before any derived form in it is replaced by its equivalent form (Section 2.10), and are not checked again on equivalent forms. In particular the variable bound in the equivalent form of .lab\kw{.}\lab may have the name of a variable in scope, and a restriction that distinguishes a definition with parameters from one without refers to the definition as written. An occurrence of a variable in a phrase is free if it is unqualified and is not within the scope of a binding of that variable, by a pattern or by a value binding, made inside the phrase. No equivalent form adds or removes a free occurrence of a variable, so the groupings of Sections A.1 and A.2 are the same whichever other derived forms have already been rewritten.

No equivalent form introduces a new identifier, so no table carries a note of fresh identifiers. The only variables bound by equivalent forms are the one bound by the equivalent form of .lab\kw{.}\lab and the one bound by the equivalent form of a pattern row written as a label alone; each is spelt as its label.

Expressions exp\expm

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
exp1 op exp2\expm_1\ \opv\ \expm_2(op) (exp1) (exp2)\kw{(}\opv\kw{)}\ \kw{(}\expm_1\kw{)}\ \kw{(}\expm_2\kw{)}infix
if exp1 then exp2 else exp3\kw{if}\ \expm_1\ \kw{then}\ \expm_2\ \kw{else}\ \expm_3case exp1 of Basics.True -> (exp2)case exp1 of ; Basics.False -> exp3\begin{array}{l} \kw{case}\ \expm_1\ \kw{of}\ \kw{Basics.True}\ \kw{->}\ \kw{(}\expm_2\kw{)}\\ \phantom{\kw{case}\ \expm_1\ \kw{of}}\ \kw{;}\ \kw{Basics.False}\ \kw{->}\ \expm_3 \end{array}if
if exp1 then exp1′else if exp2 then exp2′ ⋯else if expn then expn′else exp\begin{array}{l} \kw{if}\ \expm_1\ \kw{then}\ \expm'_1\\ \kw{else}\ \kw{if}\ \expm_2\ \kw{then}\ \expm'_2\ \cdots\\ \kw{else}\ \kw{if}\ \expm_n\ \kw{then}\ \expm'_n\\ \kw{else}\ \expm \end{array}if exp1 then exp1′ else (  if exp2 then exp2′ else ( ⋯    if expn then expn′ else exp )⋯)(n≥2)\begin{array}{l} \kw{if}\ \expm_1\ \kw{then}\ \expm'_1\ \kw{else}\ \kw{(}\\ \;\kw{if}\ \expm_2\ \kw{then}\ \expm'_2\ \kw{else}\ \kw{(}\ \cdots\\ \;\;\kw{if}\ \expm_n\ \kw{then}\ \expm'_n\ \kw{else}\ \expm \ \kw{)}\cdots\kw{)}\\ \qquad (n\geq 2) \end{array}if-chain
exp1 && exp2\expm_1\ \kw{\&\&}\ \expm_2if exp1 then exp2 else Basics.False\kw{if}\ \expm_1\ \kw{then}\ \expm_2\ \kw{else}\ \kw{Basics.False}and
exp1 || exp2\expm_1\ \kw{||}\ \expm_2if exp1 then Basics.True else exp2\kw{if}\ \expm_1\ \kw{then}\ \kw{Basics.True}\ \kw{else}\ \expm_2or
\atpat1⋯atpatn -> exp\kw{\textbackslash}\atpat_1\cdots\atpat_n\ \kw{->}\ \expm\atpat1 -> ⋯ \atpatn -> exp(n≥2)\kw{\textbackslash}\atpat_1\ \kw{->}\ \cdots\ \kw{\textbackslash}\atpat_n\ \kw{->}\ \expm \quad (n\geq 2)lambda-params

Atomic Expressions atexp\atexp

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
-atexp\kw{-}\atexp(Basics.negate atexp)\kw{(}\kw{Basics.negate}\ \atexp\kw{)}negation
[exp1, ⋯, expn]\kw{[}\expm_1\kw{,}\ \cdots\kw{,}\ \expm_n\kw{]}(exp1 :: ⋯ :: expn :: [ ])(n≥1)\kw{(}\expm_1\ \kw{::}\ \cdots\ \kw{::}\ \expm_n\ \kw{::}\ \kw{[ ]}\kw{)} \qquad (n\geq 1)list
.lab\kw{.}\lab(\{ lab = lab } -> lab)\kw{(}\kw{\textbackslash}\kw{\kwlb}\ \lab\ \kw{=}\ \lab\ \kw{\kwrb}\ \kw{->}\ \lab\kw{)}field-accessor
atexp.lab\atexp\kw{.}\lab(.lab atexp)\kw{(}\kw{.}\lab\ \atexp\kw{)}field-selection
Figure 27: Derived forms of Expressions and Atomic Expressions

Atomic Patterns atpat\atpat

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
[pat1, ⋯, patn]\kw{[}\pat_1\kw{,}\ \cdots\kw{,}\ \pat_n\kw{]}(pat1 :: (pat2 :: ⋯ (patn :: [ ])⋯))(n≥1)\kw{(}\pat_1\ \kw{::}\ \kw{(}\pat_2\ \kw{::}\ \cdots\ \kw{(}\pat_n\ \kw{::}\ \kw{[ ]}\kw{)}\cdots\kw{)}\kw{)} \quad (n\geq 1)list-pattern

Pattern Rows patrow\patrow

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
lab ⟨, patrow⟩\lab\ \opt{\kw{,}\ \patrow}lab = var ⟨, patrow⟩(var spelt as lab)\lab\ \kw{=}\ \var\ \opt{\kw{,}\ \patrow} \qquad (\text{$\var$ spelt as $\lab$})pattern-row-label
Figure 28: Derived forms of Patterns and Pattern Rows

In the equivalent form for lists, each :: is an infixed application whose right operand is everything to its right:

exp1 :: (exp2 :: ⋯ (expn :: [ ])⋯). \expm_1\ \kw{::}\ \kw{(}\expm_2\ \kw{::}\ \cdots\ \kw{(}\expm_n\ \kw{::}\ \kw{[ ]}\kw{)}\cdots\kw{)}.

In the equivalent form for list patterns, each :: is the bare pattern pat :: pat\pat\ \kw{::}\ \pat, the parentheses make the result an atomic pattern, and for n=1n=1 the equivalent form is (pat1 :: [ ])\kw{(}\pat_1\ \kw{::}\ \kw{[ ]}\kw{)}. In the row for pattern rows, var\var is the variable spelt as lab\lab: the pattern { h, n } stands for { h = h, n = n }. A program writes every pattern row in this derived form (Section 2.10); only the copies of number specialisation (Section 6.2) have pattern rows whose variable is spelt otherwise.

Value Bindings valbind\valbind

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
var ⟨: ty⟩ atpat1⋯atpatn = exp⟨and valbind⟩\begin{array}{l} \var\ \opt{\kw{:}\ \ty}\ \atpat_1\cdots\atpat_n\ \kw{=}\ \expm\\ \quad\opt{\kw{and}\ \valbind} \end{array}var ⟨: ty⟩ = \atpat1⋯atpatn -> exp⟨and valbind⟩(n≥1)\begin{array}{l} \var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \kw{\textbackslash}\atpat_1\cdots\atpat_n\ \kw{->}\ \expm\\ \quad\opt{\kw{and}\ \valbind} \qquad (n\geq 1) \end{array}function-binding

Declarations dec\dec

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
let valbind1 ⋯ valbindn in exp\kw{let}\ \valbind_1\ \cdots\ \valbind_n\ \kw{in}\ \expmlet dec1 in ⋯ let deck in exp(see Section A.1)(n≥2)\begin{array}{l} \kw{let}\ \dec_1\ \kw{in}\ \cdots\ \kw{let}\ \dec_k\ \kw{in}\ \expm\\ \text{(see Section~A.1)} \qquad (n\geq 2) \end{array}let-grouping
Figure 29: Derived forms of Value Bindings and Declarations

Top-level Declarations topdec\topdec

Derived Form\textnormal{Derived Form}Equivalent Form\textnormal{Equivalent Form}
topdec1 ⋯ topdecn\topdec_1\ \cdots\ \topdec_ntype datbind ;type alias typbind1′ ; ⋯ type alias typbindl′ ;topdec1′ ; ⋯ ; topdeck′(see Section A.2)(n≥2)\begin{array}{l} \kw{type}\ \datbind\ \kw{;}\\ \kw{type alias}\ \typbind'_1\ \kw{;}\ \cdots\ \kw{type alias}\ \typbind'_l\ \kw{;}\\ \topdec'_1\ \kw{;}\ \cdots\ \kw{;}\ \topdec'_k\\ \text{(see Section~A.2)} \qquad (n\geq 2) \end{array}module-grouping
Figure 30: Derived forms of Top-level Declarations

No derived form supplies the header of a module: every module is written with one (Chapter 3).

A.1Grouping the Definitions of a Let

A let expression as written has definitions valbind1,…,valbindn\valbind_1,\ldots,\valbind_n (n≥2n\geq 2), each a value binding without and. The bare language has let dec in exp\kw{let}\ \dec\ \kw{in}\ \expm only for a single group of bindings joined by the abstract and, so the definitions are grouped and ordered as follows.

The variables bound by a definition, and its right-hand side, are given by the following table, in which m≥1m\geq 1 and a definition of the first two forms may carry an annotation : ty\kw{:}\ \ty after var\var:

DefinitionBoundRight-hand sidevar = expvarexpvar atpat1⋯atpatm = expvar\atpat1⋯atpatm -> expatpat = expthose of atpatexp \begin{array}{lll} \text{Definition} & \text{Bound} & \text{Right-hand side}\\[2pt] \var\ \kw{=}\ \expm & \var & \expm\\ \var\ \atpat_1\cdots\atpat_m\ \kw{=}\ \expm & \var & \kw{\textbackslash}\atpat_1\cdots\atpat_m\ \kw{->}\ \expm\\ \atpat\ \kw{=}\ \expm & \text{those of}\ \atpat & \expm \end{array}

The variables bound by a pattern are its variable patterns, the variables of its pattern rows (Section 2.10), and the variables following as in it. A definition valbindi\valbind_i refers to a definition valbindj\valbind_j if a variable bound by valbindj\valbind_j occurs free in the right-hand side of valbindi\valbind_i; j=ij=i is allowed. Two definitions are in the same component if each can be reached from the other by a chain of zero or more steps of “refers to”. The components partition the definitions. A component is recursive if it has more than one member, or if its one member refers to itself. The members of the recursive components are exactly the definitions that lie on a cycle of “refers to”, and the restriction on recursion in Section 2.10 requires each of them to be a definition with parameters; in particular a definition atpat = exp\atpat\ \kw{=}\ \expm is never in a recursive component. Number the components 1,…,k1,\ldots,k so that whenever a member of component aa refers to a member of component b≠ab\neq a, then b<ab<a. Such a numbering exists, because a chain of “refers to” that returns to its start never leaves a component. For each aa, let valbindi1,…,valbindip\valbind_{i_1},\ldots,\valbind_{i_p}, with i1<⋯<ipi_1<\cdots<i_p, be the members of component aa, and let

deca  =  valbindi1 and ⋯ and valbindip. \dec_a \;=\; \valbind_{i_1}\ \kw{and}\ \cdots\ \kw{and}\ \valbind_{i_p}.

Then the equivalent form is given by

let valbind1 ⋯ valbindn in exp⟹  let dec1 in let dec2 in ⋯ let deck in exp. \begin{array}{l} \kw{let}\ \valbind_1\ \cdots\ \valbind_n\ \kw{in}\ \expm\\ \quad\Longrightarrow\; \kw{let}\ \dec_1\ \kw{in}\ \kw{let}\ \dec_2\ \kw{in}\ \cdots\ \kw{let}\ \dec_k\ \kw{in}\ \expm . \end{array}

An annotation is part of the definition it annotates, and so goes with that definition into deca\dec_a.

When two components are independent (neither can be reached from the other) the numbering is not unique, and which admissible numbering is chosen does not matter: independent components bind distinct variables and neither refers to the other, the bindings of a component add no free type variable to the context (Clos\Clos generalises every type variable not free in the context, Section 4.8), and evaluation has neither state nor exceptions, so each component elaborates and evaluates to the same result whichever independent components precede it.

A.2Grouping the Declarations of a Module

The declarations of a module body as written stand in any order. Each declaration topdeci\topdec_i is one of the following, where m≥0m\geq 0:

type datbindia custom type, declaring one type;type alias typbindian alias, declaring one alias;port var : tya port;var ⟨: ty⟩ atpat1⋯atpatm = expa definition. \begin{array}{ll} \kw{type}\ \datbind_i & \text{a custom type, declaring one type;}\\ \kw{type alias}\ \typbind_i & \text{an alias, declaring one alias;}\\ \kw{port}\ \var\ \kw{:}\ \ty & \text{a port;}\\ \var\ \opt{\kw{:}\ \ty}\ \atpat_1\cdots\atpat_m\ \kw{=}\ \expm & \text{a definition.} \end{array}

A module body has no definitions of the form atpat = exp\atpat\ \kw{=}\ \expm (Chapter 3). A module body as written is always rewritten by this row, even when its declarations already stand in an order the row could produce. The equivalent form

type datbind ;type alias typbind1′ ; ⋯ type alias typbindl′ ;topdec1′ ; ⋯ ; topdeck′ \begin{array}{l} \kw{type}\ \datbind\ \kw{;}\\ \kw{type alias}\ \typbind'_1\ \kw{;}\ \cdots\ \kw{type alias}\ \typbind'_l\ \kw{;}\\ \topdec'_1\ \kw{;}\ \cdots\ \kw{;}\ \topdec'_k \end{array}

is built as follows, where ; is the abstract separator of top-level declarations in the bare grammar (Chapter 3); a part that is omitted below is omitted with its separator.

  1. Custom types. If type datbindi1,…,type datbindip\kw{type}\ \datbind_{i_1},\ldots, \kw{type}\ \datbind_{i_p}, with i1<⋯<ipi_1<\cdots<i_p, are the custom types of the body, then all of them form one group,

    datbind  =  datbindi1 and ⋯ and datbindip, \datbind \;=\; \datbind_{i_1}\ \kw{and}\ \cdots\ \kw{and}\ \datbind_{i_p},

    whose members may refer to one another. If p=0p=0, the declaration type datbind\kw{type}\ \datbind is omitted.

  2. Aliases. An alias mentions another alias of the body if the type constructor that the other binds occurs, unqualified, in its body. The aliases typbind1′,…,typbindl′\typbind'_1,\ldots,\typbind'_l are those of the body, ordered so that each comes after every alias it mentions. Such an order exists, because the restrictions of Chapter 3 forbid a chain of mentions that returns to its start. If l=0l=0, there are no alias declarations.

  3. Ports and definitions. A port port var : ty\kw{port}\ \var\ \kw{:}\ \ty binds var\var and has no right-hand side, so it refers to nothing; a definition binds its var\var and has the right-hand side of Section A.1. Over the ports and definitions of the body, “refers to”, components and their numbering 1,…,k1,\ldots,k are defined as in Section A.1. A port is alone in its component. For each aa, topdeca′\topdec'_a is the port if component aa is a port, and otherwise is the value group

    valbindi1 and ⋯ and valbindiq, \valbind_{i_1}\ \kw{and}\ \cdots\ \kw{and}\ \valbind_{i_q},

    where valbindi1,…,valbindiq\valbind_{i_1},\ldots,\valbind_{i_q}, with i1<⋯<iqi_1<\cdots<i_q, are the members of component aa, each with its annotation.

The type constructors bound by datbind\datbind and by the aliases are in scope throughout these type declarations: a constructor binding in datbind\datbind may mention an alias of the body, and an alias may mention a custom type of datbind\datbind. Chapter 5 elaborates the group datbind\datbind together with the aliases that follow it.

The values that the type declarations bind (the constructors of datbind\datbind and the constructors of record aliases) are thus in scope in every port and definition. As for a let, which order is chosen among aliases neither of which mentions the other, and which admissible numbering is chosen for the components, does not matter: an alias denotes the same type function whichever unrelated aliases precede it, and a component of ports and definitions elaborates and evaluates to the same result whichever independent components precede it, for the reasons given in Section A.1.

Discussions

Discuss this chapter·Search the discussions on GitHub

Discussions are on GitHub; one about this chapter has [ch:appendix-derived-forms] in its title.

A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.