2.1Reserved Words¶
The following are the reserved words of Standard Elm. They may not be used as identifiers or as operators.
ascaseelseexposingifimportinletmoduleofportthentypewhere()[]{},.:=|->\_
The words exposing, import, module, port and type are used by Modules (Chapter 3). The word where is used by no phrase of Standard Elm: it belongs to the headers of effect modules, which are kernel-only syntax (Section 2.7). The words alias, infix, left, right, non, effect, command and subscription are not reserved; alias serves as a terminal only directly after type (Chapter 3), and elsewhere each of them is an identifier.
2.2Special Constants¶
Source text is a sequence of Unicode characters. A decimal digit is one of 0, …, 9; a hexadecimal digit is a decimal digit or one of a, …, f, A, …, F.
An integer constant (in decimal notation) is a non-empty sequence of decimal digits that does not begin with 0 unless it is 0 itself. An integer constant (in hexadecimal notation) is 0x followed by a non-empty sequence of hexadecimal digits. No integer constant carries a sign: a negated integer is a derived form (Section 2.5). An integer constant denotes a non-negative integer; its value as an Int is that integer normalised into the implementation’s Int range by , the normalisation of Int arithmetic, so that every integer constant has a value in (Appendix E). Examples: 0 42 0x1F 0xffffffff. Non-examples: 007 0X1F 1_000 0b101.
\n | line feed (U+000A) |
\r | carriage return (U+000D) |
\t | horizontal tab (U+0009) |
\" | " |
\' | ' |
\\ | \ |
\u{} | the character with code point , read in hexadecimal |
Tested by: AstralCharLiteralTest, CharAstralRecordFieldTest, CharLiteralSurrogateEscapeTest, CharUnicodeTest, ParseErrorStringEscapeTooFewDigitsTest, ParseErrorStringEscapeUnclosedTest, StandardA8SurrogateEscapeTest
A float constant is an integer constant in decimal notation followed by a fraction, by an exponent, or by a fraction and then an exponent. A fraction is . followed by a non-empty sequence of decimal digits; an exponent is e or E, then optionally + or -, then a non-empty sequence of decimal digits. The constant 0 may be followed by a fraction but not directly by an exponent. A float constant denotes the rational number that it writes in decimal notation. Examples: 0.5 3.14 6.02e23 1E-3 0.0e1. Non-examples: .5 1. 1.e5 0e5.
A character constant is a single quote ', then either one character other than ', \ and line feed, or one escape sequence, then a single quote. It denotes the character it contains, or the character its escape sequence denotes. Thus '' and 'ab' are not constants.
A string constant (single-line) is a double quote ", then a sequence of zero or more characters other than ", \ and line feed, and escape sequences, then a double quote. Three double quotes """ always begin a multi-line string constant, so "" is the empty string only when no third " follows it. A string constant (multi-line) is """, then a sequence of characters other than \, and escape sequences, that does not contain """, then """. The first """ after the opening one closes the constant; any further " immediately after it begins a new item. Line feeds and carriage returns inside a multi-line string constant are characters of the string, and so is a line feed directly after the opening """. A string constant denotes the sequence of characters it contains, each escape sequence standing for the character it denotes.
Each escape sequence begins with \ and stands for one character. The escape sequences are shown in Figure 1, subject to this restriction:
In
core-escape-unicode\u{}, is a sequence of four, five or six hexadecimal digits whose value is a Unicode scalar value, that is, at most10FFFFand outside the surrogate rangeD800toDFFF. No other sequence beginning with\is an escape sequence.
A tab character may occur, unescaped, in a character or string constant. How a character is represented as a value is an implementation parameter (Appendix E).
We denote by the class of special constants, that is, the integer, float, character and string constants; we use to range over .
2.3Comments¶
A line comment is -- and the characters after it up to the end of the line. A block comment is any character sequence within comment brackets {- -} in which comment brackets are properly nested. No space is allowed between the two characters of a comment bracket. A program in which a {- is unmatched is not a phrase of the language. A tab character may occur in a line comment but not in a block comment.
A comment bracket {- directly followed by | begins a doc comment, which extends, with comment brackets nested as in a block comment, to its matching -}. A doc comment is not a comment: it is an item of the grammar of Modules, which admits it only after a module header and before a top-level declaration (Chapter 3). No doc comment occurs in a Core phrase.
2.4Identifiers¶
The classes of identifiers for the Core are shown in Figure 2. We use , and so on to range over , , etc. For each class marked “long” there is a class of long identifiers: , and , over which , and range. Their syntax is
| ::= | identifier | ||
| qualified identifier | |||
| ::= | identifier | ||
| qualified identifier | |||
| ::= | identifier | ||
| qualified identifier |
with no space or comment on either side of each .. Throughout this document the term “identifier”, occurring without an adjective, refers to an identifier that is not qualified.
A letter is a character of one of the Unicode general categories Lu, Ll, Lt, Lm and Lo; the version of the Unicode standard that assigns these categories is an implementation parameter (Appendix E). The identifiers are formed as this restriction states:
A lower identifier is a letter of category Ll (which includes
core-identifier-charactersa, …,z) followed by a sequence of zero or more inner characters: letters, decimal digits and underbars_. An upper identifier is a letter of category Lu or Lt (which includeA, …,Z) followed by a sequence of zero or more inner characters. A letter of category Lm or Lo may continue an identifier but not begin one, and no identifier begins with_. In either case reserved words are excluded, and no component of a long identifier may be a reserved word: thusList.ifis not a long identifier.
An operator is a non-empty sequence of the symbols
+ - * / = . < > : & | ^ ? % !
that is not a reserved word. This means that =, :, | and -> are not operators, but ==, ::, || and |> are. Operators are never qualified.
A variable , a type variable and a record label are lower identifiers. A constructor and a type constructor are upper identifiers. A module identifier is a non-empty sequence of upper identifiers separated by ., with no space or comment around each .. A type variable whose name begins with number, comparable, appendable or compappend is constrained to a class of types (Section 4.4).
| value variables | long | ||
| value constructors | long | ||
| type variables | |||
| type constructors | long | ||
| record labels | |||
| module identifiers | |||
| operators |
The classes , and share the lower identifiers, and , and the upper identifiers. The syntax class of an occurrence of an identifier in a Core phrase is determined thus:
An upper identifier immediately followed by
.is a component of a module identifier. The following rules assume that all occurrences of module identifiers have been removed.Within a type expression, a lower identifier is a type variable and an upper identifier is a type constructor.
At the start of a component of a record expression, record update, record type or record pattern, and immediately after the
.of a field selection or field accessor (Appendix A), a lower identifier is a record label. A record label that forms a whole component of a record pattern, the derived pattern row of Appendix A, is also a variable, bound by that pattern.Elsewhere, a lower identifier is a variable and an upper identifier is a constructor.
For the remainder of this document we shall therefore assume that the classes are all disjoint.
2.5Lexical Analysis¶
Each item of lexical analysis is either a reserved word, a special constant, a long identifier, an operator, or a doc comment. Comments and the whitespace characters (space, line feed and carriage return) separate items and are otherwise ignored, except that the layout rule depends on the column at which each item begins (Section 2.6) and that the constructs listed below depend on whether two items are separated. A tab character may occur only within a line comment or within a character or string constant. Where the remaining text begins with -- or {-, a comment or doc comment is taken. Otherwise, at each stage the longest next item is taken. Thus types is an identifier, not type followed by s; within a run of symbols -- is part of an operator and begins no comment, so a*-b contains the single operator *-, and 2^-1 the operator ^-. Items are also subject to these restrictions:
A special constant, and the reserved word
core-constant-separated_, may not be followed immediately by an inner character, and the digits of an integer constant may be followed immediately by.only as part of a fraction; so12abc,1.xand_xare not sequences of items.The following constructs require two items to be adjacent, that is, to have no whitespace or comment between them:
the parentheses of the unit
(), and the operator and parentheses of an operator value();the
.of a long identifier and the items on either side of it;the
.of a field selection.and the items on either side of it, and the.of a field accessor.and the label after it. A.adjacent to a following label is the.of a field selection only if it is adjacent to the item before it and that item ends an atomic expression that may precede a field selection (Section 2.10); otherwise it begins a field accessor (Appendix A). In particular a.directly after a field accessor begins a field accessor;the negation symbol and the atomic expression it negates.
An item - is a negation symbol when it is adjacent to a following atomic expression and either it begins an operand of an expression, or it follows an operand from which it is separated by whitespace or a comment. A negation symbol negates the atomic expression after it, and no more; in the second case the negated expression is an argument of an application. Every other - is the operator -. Thus f -x applies f to the negation of x, while a - b and a-b are subtractions; -f x applies the negation of f to x; and (-) is the operator value of subtraction. Negation is a derived form (Appendix A).
2.6Layout¶
The phrase structure of Elm depends on indentation. Source text is divided into lines by line feeds. The column of an item is one more than the number of characters that precede it on its line, where a carriage return counts as no character. Each item is read under an indentation, a natural number . An item whose column is is
indented if and ;
aligned if ;
at the margin if .
These tests concern only the column at which an item begins: where the item before it ended, for instance on a later line than it began, is irrelevant. The layout rule is this restriction:
Every item of a phrase is indented, except the items that the constructs below require to be aligned or at the margin.
core-layout-rule
Only these constructs change the indentation; elsewhere an item is read under the indentation of the phrase that contains it.
Top-level declarations (Chapter 3). The first item of each top-level declaration is at the margin, and its remaining items are read under the indentation ; so each of them lies in column or beyond.
Let expressions. Let be the column of the reserved word
let. The item afterlethas a column with and , and is the indentation under which the definitions of theletare read: the first item of each later definition, and of each annotation, is aligned, and every other item of the definitions is indented. The reserved wordinand the body after it are read under the indentation in force atlet.Case expressions. The items of
caseofand the first item of the first match rule are read under the indentation in force atcase. The column of that first item is the indentation under which the match is read: the first item of each later match rule is aligned, and every other item of the match is indented.
An annotation is followed directly by a definition of the same , whose first item has the column of the annotation’s first item: at the margin at top level, aligned in a let. Each construct extends as far as possible: it takes in every following item that passes the test that applies to it and continues the phrase, and it ends before the first item that does not. Thus two definitions of one let, or two match rules of one case, never begin on the same line, while let x = 1 in x is a phrase on one line.
2.7Infixed Operators¶
An operator has a fixity: a precedence, which is one of the digits 0, …, 9, and an associativity, which is left, right or non. Operators are declared, with their fixities, only by kernel-only syntax. Kernel-only syntax, namely operator declarations, effect module headers and references to Elm.Kernel modules, lies outside Standard Elm. No program can therefore declare an operator, and the operators are those of the initial basis, the same in every program. Their fixities are shown in Figure 3; the operator table of the initial basis gives the meaning of each, its type scheme in Appendix C and its value in Appendix D. Operators of library modules beyond the initial basis are outside this definition.
| Precedence | Left | Right | Non |
| 9 | << | >> | |
| 8 | ^ | ||
| 7 | * / // | ||
| 6 | + - | ||
| 5 | ++ :: | ||
| 4 | == /= < > <= >= | ||
| 3 | && | ||
| 2 | || | ||
| 0 | |> | <| |
Tested by: InfixAssocLeftArithTest, InfixAssocPipesTest, InfixAssocRightConsAppendTest, InfixAssocRightPowTest, InfixLastOperandAnyExprTest, InfixMixedAssocComposeTest, InfixMixedAssocPipesTest, InfixNegatedOperandTest, InfixNonAssocCompareTest, InfixNonAssocEqTwiceTest, InfixNonAssocSeparatedLegalTest, InfixPrecAddOverConsTest, InfixPrecAndOverOrTest, InfixPrecAppendOverEqTest, InfixPrecApplicationFirstTest, InfixPrecCompareOverAndTest, InfixPrecComposeOverPipeTest, InfixPrecMulOverAddTest, InfixPrecOrOverPipeTest, InfixPrecPowOverMulTest
An expression of the form (), in which each is an operator and each operand is an application or an atomic expression, possibly negated, except that the last operand may be any expression, is an operator chain. Application binds more tightly than any operator. A chain is grouped so that an operator of higher precedence binds more tightly than one of lower precedence, subject to this restriction:
If and () of a chain have equal precedence and no operator of lower precedence stands between them, then either both associate to the left and the chain groups to the left, or both associate to the right and it groups to the right; otherwise the chain is not a phrase of the language.
core-operator-chain
Precisely, a chain is the infixed application in which has the least precedence in the chain and is, among the operators of that precedence, the rightmost if they associate to the left and the leftmost if they associate to the right; and are the chains, or single operands, to the left and right of , grouped by the same rule. For example,
a |> f |> g | parses as | (a |> f) |> g |
f <| g <| a | parses as | f <| (g <| a) |
x :: ys ++ zs | parses as | x :: (ys ++ zs) |
a < b == c | is illegal | |
f << g >> h | is illegal |
An operator value () is an atomic expression that denotes the operator as a value; it is the only way to use an operator other than in a chain. There are no sections, and no operator is qualified. The infixed application is a derived form (Appendix A); so are the infixed forms of && and ||, which evaluate their second operand only when needed, whereas the operator values (&&) and (||) are ordinary functions. In the semantic rules every chain has been grouped and every infixed application replaced by its equivalent form, since fixity affects only parsing.
2.8Derived Forms¶
Many syntactic forms of Elm have a meaning that is given by translation into a smaller set of forms, called the bare language. Among them are infixed application, negation, conditional expressions, list expressions with elements, list patterns with elements, pattern rows written as a label alone, field selection and field accessors, lambda expressions and function bindings with parameters beyond the first, and let expressions with several definitions. These derived forms, and their equivalent forms in the bare language, are given in Appendix A. Appendix B collects the bare and derived syntax into one grammar.
2.9Grammar¶
The phrase classes for the Core are shown in Figure 4. We use the variable to range over , etc. The grammatical rules for the Core are shown in Figures 5, 6 and 7.
| atomic expressions | |
| expression rows | |
| expressions | |
| matches | |
| match rules | |
| declarations | |
| value bindings | |
| atomic patterns | |
| pattern rows | |
| patterns | |
| type expressions | |
| type-expression rows |
The following conventions are adopted in presenting the grammatical rules, and in their interpretation:
The brackets enclose optional phrases.
Alternative forms for each phrase class are in order of decreasing precedence. L (resp. R) means left (resp. right) association.
The phrases , and extend as far right as the layout rule allows (Section 2.6). Such a phrase is written in parentheses when it is the argument of an application, or an operand of an operator chain other than the last.
In a type construction with , each argument is a type variable, a type construction without arguments, a record type expression, a unit or tuple type expression, or a parenthesised type expression; thus
Maybe List agivesMaybetwo arguments. Type construction binds more tightly than->.In patterns, a constructed pattern binds more tightly than
::, and::more tightly thanas; sox :: xs as lis a layered pattern whose pattern isx :: xs. The::of a pattern always denotes the list constructor of the initial basis. There is no negated constant in a pattern.The separator
;between match rules and the connectiveandbetween value bindings are abstract syntax: neither is an item of source text, andandis not a reserved word. In source text the match rules of a match, and the definitions of alet, are separated by layout (Section 2.6). Likewise the annotated value binding is written in source text as the annotation followed by the definition .In the bare language the declaration of a
letis a single value binding whose members form one group; aletwith several definitions is a derived form. A value binding in which is a variable is read by the first alternative for .Tuples have exactly two or three components.
| ::= | special constant | ||
| value variable | |||
| value constructor | |||
| operator value | |||
| unit | |||
| tuple | |||
| empty list | |||
| record | |||
| record update | |||
| local declaration | |||
| parentheses | |||
| ::= | expression row | ||
| ::= | atomic | ||
| application (L) | |||
| function | |||
| case analysis | |||
| ::= | match | ||
| ::= | match rule | ||
| ::= | value declaration | ||
| ::= | variable binding | ||
| destructuring binding |
Tested by: BadTupleATupleOfFourTest, InfixIfExtendsRightTest, InfixNoQualifiedOperatorTest, InfixNoSectionLeftTest, InfixNoSectionRightTest
| ::= | wildcard | ||
| special constant | |||
| variable | |||
| constructor | |||
| unit | |||
| tuple | |||
| empty list | |||
| record | |||
| parentheses | |||
| ::= | pattern row | ||
| ::= | atomic | ||
| constructed pattern () | |||
| list construction (R) | |||
| layered |
Tested by: ParseErrorCasePatternUnfinishedTest
| ::= | type variable | ||
| record type expression | |||
| extensible record type expression | |||
| unit type expression | |||
| tuple type expression | |||
| type construction () | |||
| function type expression (R) | |||
| parentheses | |||
| ::= | type-expression row |
Tested by: TypeExprUnitAppliedTest
2.10Syntactic Restrictions¶
The restrictions below are conditions on a program as it is written, before any derived form is replaced by its equivalent form; where a restriction names a derived form, the form is that of Appendix A. A program that breaks one of them is not a phrase of the language.
Records.
No expression row, pattern row or type-expression row may contain the same twice. This applies to the expression row of a record update.
core-row-labels-distinctNo pattern row of a program has the form : a program writes each component of a record pattern as a label alone, the derived form of Appendix A, whose equivalent form binds the variable spelt as the label. The bare form exists so that number specialisation (Chapter 6) can give the variable of a row a new name while keeping its label.
core-pattern-row-label-onlyIn a record update the item before
core-record-update-form|is a variable that is not qualified, and at least one expression row follows|; the grammar gives this shape, and an extensible record type expression likewise has at least one row.In a field selection (Appendix A), is a , a record, a record update, a field selection, or a phrase that begins with
core-field-selection(: a parenthesised expression, a tuple, the unit or an operator value. A field accessor is never followed by a field selection: in the second.begins a second field accessor, so the phrase is the application of to . Likewise"s".xis the application of a string constant to the field accessor.x.
Patterns and bound variables. The variable of a pattern row is the of a row and, for a row written as a label alone (Appendix A), the variable spelt as its label. The variables bound by a pattern are the variables that occur in it outside its pattern rows, the variables of its pattern rows, and the variables after its occurrences of as. the following: the parameters of a lambda expression, taken together; the parameters of a function binding, taken together; the pattern of a match rule; the pattern of a destructuring binding; and the definitions of one let, taken together, which bind the variables they define and the variables their destructuring patterns bind. The scope of a binding site is the body of the lambda expression, the right-hand side of the function binding, the expression of the match rule, or, for a destructuring binding and for the definitions of a let, the definitions of that let and its body.
No binding site may bind the same twice.
core-binding-site-distinctNo binding site may bind a that is already bound by a binding site in whose scope it lies. Constructors, type constructors and type variables are not subject to this restriction.
core-no-shadowingNo float constant may occur in a pattern.
core-no-float-pattern
The number of arguments in a constructed pattern is not restricted here: a constructed pattern with too few or too many arguments for its constructor does not elaborate (Chapter 4).
Value bindings.
In source text each annotation is followed directly by a definition of the same , laid out as Section 2.6 requires.
core-annotation-placementIn a
core-let-recursionlet, a definition refers to a definition (possibly itself) if a variable that the second binds occurs in the first. Every definition that lies on a cycle of this relation is a function binding with ; in particular, no destructuring binding and no definition without parameters lies on a cycle, even when is a lambda expression. The corresponding restriction for top-level declarations is given in Chapter 3.
There is no limit on the size of a phrase: the number of parameters, components, fields or constructors is unbounded, and the limits of an implementation are listed in Appendix E.