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

BFull Grammar

The syntax of programs is given in Chapter 9. The syntax of long identifiers is that of Chapter 2.

The full grammar of the Core and of Modules consists of the grammar of Chapters 2 and 3, in which each derived form of Appendix A is added as an alternative of the phrase class it belongs to. The grammatical rules are shown in Figures 31, 32, 33, 34 and 35. An alternative whose label carries the mark † is a derived form, whose equivalent form is given in Appendix A; every other alternative is a phrase of the bare language, and has the same label as in Chapter 2 or 3. No subclass of Exp\cls{Exp} is introduced: which expressions may be the operands of an operator chain is stated in Section 2.7.

The grammatical conventions are those of Chapter 2, namely:

  • The brackets ⟨ ⟩\opt{\ } enclose optional phrases.

  • Alternative forms for each phrase class are in order of decreasing precedence. L (resp. R) means left (resp. right) association.

  • The infixed application, conjunction and disjunction alternatives are grouped by the fixities of Figure 3, as Section 2.7 describes, and not by their order in Figure 31.

  • Which item - is a negation symbol, and which . begins a field selection or a field accessor, is decided by lexical analysis (Section 2.5).

  • The precedence of types and of patterns, and the extent of lambda expressions, case expressions and let expressions, are as stated with the grammar of the Core (Figure 5 and the conventions before it); the extent of each phrase is decided by the layout rule (Sections 2.6 and 3.4).

  • The separator ; between match rules and between top-level declarations, and the connective and between the members of a value binding or of a custom-type binding, are abstract syntax. Neither is an item of source text: match rules, the definitions of a let and the declarations of a module are separated by layout, and and is not a reserved word. Likewise an annotated binding var : ty = exp\var\ \kw{:}\ \ty\ \kw{=}\ \expm is written in source text as the annotation var : ty\var\ \kw{:}\ \ty followed by a definition of var\var. The derived alternatives let valbind1 ⋯ valbindn in exp\kw{let}\ \valbind_1\ \cdots\ \valbind_n\ \kw{in}\ \expm and topdec1 ⋯ topdecn\topdec_1\ \cdots\ \topdec_n are the forms in which several definitions and declarations are written; in them each valbindi\valbind_i is a definition, that is, a value binding without and, and each topdeci\topdec_i is a single declaration, as listed in Section A.2.

The syntactic restrictions are those of Sections 2.10 and 3.7. They apply to phrases of the full grammar, that is, to a program as written, before any derived form is replaced by its equivalent form.

atexp\atexp::=scon\sconspecial constant
longvar\longvarvalue variable
longcon\longconvalue constructor
(op)\kw{(}\opv\kw{)}operator value
()\kw{()}unit
( exp1 , exp2 ⟨, exp3⟩ )\kw{(}\ \expm_1\ \kw{,}\ \expm_2\ \opt{\kw{,}\ \expm_3}\ \kw{)}tuple
[ ]\kw{[}\ \kw{]}empty list
[exp1, ⋯, expn]\kw{[}\expm_1\kw{,}\ \cdots\kw{,}\ \expm_n\kw{]}list (n≥1n \geq 1)†
{ ⟨exprow⟩ }\kw{\kwlb}\ \opt{\exprow}\ \kw{\kwrb}record
{ var | exprow }\kw{\kwlb}\ \var\ \kw{|}\ \exprow\ \kw{\kwrb}record update
.lab\kw{.}\labfield accessor†
atexp.lab\atexp\kw{.}\labfield selection†
-atexp\kw{-}\atexpnegation†
let dec in exp\kw{let}\ \dec\ \kw{in}\ \expmlocal declaration
let valbind1 ⋯ valbindn\kw{let}\ \valbind_1\ \cdots\ \valbind_ndefinitions†
in exp\quad\kw{in}\ \expm(n≥2n \geq 2)
( exp )\kw{(}\ \expm\ \kw{)}parentheses
exprow\exprow::=lab = exp ⟨, exprow⟩\lab\ \kw{=}\ \expm\ \opt{\kw{,}\ \exprow}expression row
exp\expm::=atexp\atexpatomic
exp atexp\expm\ \atexpapplication (L)
exp1 op exp2\expm_1\ \opv\ \expm_2infixed application†
exp1 && exp2\expm_1\ \kw{\&\&}\ \expm_2conjunction†
exp1 || exp2\expm_1\ \kw{||}\ \expm_2disjunction†
\ atpat -> exp\kw{\textbackslash}\,\atpat\ \kw{->}\ \expmfunction
\atpat1⋯atpatn -> exp\kw{\textbackslash}\atpat_1\cdots\atpat_n\ \kw{->}\ \expmfunction (n≥2n \geq 2)†
if exp1 then exp2 else exp3\kw{if}\ \expm_1\ \kw{then}\ \expm_2\ \kw{else}\ \expm_3conditional†
if exp1 then exp1′\kw{if}\ \expm_1\ \kw{then}\ \expm'_1conditional chain†
else if exp2 then exp2′ ⋯\quad\kw{else}\ \kw{if}\ \expm_2\ \kw{then}\ \expm'_2\ \cdots(n≥2n \geq 2)
else if expn then expn′\quad\kw{else}\ \kw{if}\ \expm_n\ \kw{then}\ \expm'_n
else exp\quad\kw{else}\ \expm
case exp of match\kw{case}\ \expm\ \kw{of}\ \matchcase analysis
match\match::=mrule ⟨; match⟩\mrule\ \opt{\kw{;}\ \match}match
mrule\mrule::=pat -> exp\pat\ \kw{->}\ \expmmatch rule
dec\dec::=valbind\valbindvalue declaration
valbind\valbind::=var ⟨: ty⟩ = exp ⟨and valbind⟩\var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm\ \opt{\kw{and}\ \valbind}variable binding
var ⟨: ty⟩ atpat1⋯atpatn\var\ \opt{\kw{:}\ \ty}\ \atpat_1\cdots\atpat_nfunction binding†
= exp ⟨and valbind⟩\qquad\kw{=}\ \expm\ \opt{\kw{and}\ \valbind}(n≥1n \geq 1)
atpat = exp\atpat\ \kw{=}\ \expmdestructuring binding

† derived form, Appendix A.

Figure 31: Full Grammar: Expressions, Matches, Declarations and Bindings

In the infixed application alternative, op\opv is neither && nor ||. The alternative -atexp\atexp is atomic, so that a negated expression may be the argument of an application.

atpat\atpat::=_\kw{\_}wildcard
scon\sconspecial constant
var\varvariable
longcon\longconconstructor
()\kw{()}unit
( pat1 , pat2 ⟨, pat3⟩ )\kw{(}\ \pat_1\ \kw{,}\ \pat_2\ \opt{\kw{,}\ \pat_3}\ \kw{)}tuple
[ ]\kw{[}\ \kw{]}empty list
[pat1, ⋯, patn]\kw{[}\pat_1\kw{,}\ \cdots\kw{,}\ \pat_n\kw{]}list (n≥1n \geq 1)†
{ ⟨patrow⟩ }\kw{\kwlb}\ \opt{\patrow}\ \kw{\kwrb}record
( pat )\kw{(}\ \pat\ \kw{)}parentheses
patrow\patrow::=lab = var ⟨, patrow⟩\lab\ \kw{=}\ \var\ \opt{\kw{,}\ \patrow}pattern row
lab ⟨, patrow⟩\lab\ \opt{\kw{,}\ \patrow}label as variable†
pat\pat::=atpat\atpatatomic
longcon atpat1 ⋯ atpatn\longcon\ \atpat_1\ \cdots\ \atpat_nconstructed pattern (n≥1n \geq 1)
pat1 :: pat2\pat_1\ \kw{::}\ \pat_2list construction (R)
pat as var\pat\ \kw{as}\ \varlayered

† derived form, Appendix A.

Figure 32: Full Grammar: Patterns
ty\ty::=tyvar\tyvartype variable
{ ⟨tyrow⟩ }\kw{\kwlb}\ \opt{\tyrow}\ \kw{\kwrb}record type expression
{ tyvar | tyrow }\kw{\kwlb}\ \tyvar\ \kw{|}\ \tyrow\ \kw{\kwrb}extensible record type expression
()\kw{()}unit type expression
( ty1 , ty2 ⟨, ty3⟩ )\kw{(}\ \ty_1\ \kw{,}\ \ty_2\ \opt{\kw{,}\ \ty_3}\ \kw{)}tuple type expression
longtycon ty1 ⋯ tyn\longtycon\ \ty_1\ \cdots\ \ty_ntype construction (n≥0n \geq 0)
ty -> ty′\ty\ \kw{->}\ \ty'function type expression (R)
( ty )\kw{(}\ \ty\ \kw{)}parentheses
tyrow\tyrow::=lab : ty ⟨, tyrow⟩\lab\ \kw{:}\ \ty\ \opt{\kw{,}\ \tyrow}type-expression row
Figure 33: Full Grammar: Type Expressions

Type expressions have no derived forms; Figure 33 is Figure 7 unchanged. Modules, headers, imports and exposing lists have no derived forms either: every module has a header, and Figure 34 is Figure 9 unchanged.

module\modm::=header imports topdec\header\ \imports\ \topdecmodule
header\header::=module modid exposing explist\kw{module}\ \modid\ \kw{exposing}\ \explistplain
port module modid exposing explist\kw{port}\ \kw{module}\ \modid\ \kw{exposing}\ \explistport module
imports\imports::=import modid ⟨as modid′⟩\kw{import}\ \modid\ \opt{\kw{as}\ \modid'}import
⟨exposing explist⟩ imports\quad\opt{\kw{exposing}\ \explist}\ \imports
empty
explist\explist::=(..)\kw{(..)}everything
( item1 , ⋯ , itemn )\kw{(}\,\expitem_1\ \kw{,}\ \cdots\ \kw{,}\ \expitem_n\,\kw{)}explicit
(n≥1)(n \geq 1)
item\expitem::=var\varvalue
(op)\kw{(}\opv\kw{)}operator
tycon\tycontype
tycon (..)\tycon\,\kw{(..)}open type
Figure 34: Full Grammar: Modules, Headers, Imports and Exposing Lists
topdec\topdec::=valbind\valbindvalue
type datbind\kw{type}\ \datbindcustom type
type alias typbind\kw{type}\ \kw{alias}\ \typbindtype alias
port var : ty\kw{port}\ \var\ \kw{:}\ \typort
topdec1 ; topdec2\topdec_1\ \kw{;}\ \topdec_2sequential
topdec1 ⋯ topdecn\topdec_1\ \cdots\ \topdec_nmodule body (n≥2n \geq 2)†
datbind\datbind::=tycon tyvarseq = conbind\tycon\ \tyvarseq\ \kw{=}\ \conbindcustom-type binding
⟨and datbind⟩\qquad\opt{\kw{and}\ \datbind}
conbind\conbind::=con ty1 ⋯ tyn ⟨| conbind⟩\con\ \ty_1\ \cdots\ \ty_n\ \opt{\kw{|}\ \conbind}constructor binding
(n≥0)(n \geq 0)
typbind\typbind::=tycon tyvarseq = ty\tycon\ \tyvarseq\ \kw{=}\ \tytype-alias binding
tyvarseq\tyvarseq::=tyvar1 ⋯ tyvark\tyvar_1\ \cdots\ \tyvar_ktype parameters
(k≥0)(k \geq 0)

† derived form, Appendix A.

Figure 35: Full Grammar: Top-level Declarations and Bindings

A top-level value declaration as written is a variable binding or a function binding without and; the function binding alternative of Figure 31 applies to it.

Discussions

Discuss this chapter·Search the discussions on GitHub

Discussions are on GitHub; one about this chapter has [ch:appendix-full-grammar] 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.