For Modules there are further reserved words and one further symbol, but no further special constants or identifier classes; comments and lexical analysis are as for the Core. Every module begins with a header, which is followed by a sequence of imports and a sequence of top-level declarations; a module makes its top-level names available to other modules by name only. The derived forms for modules appear in Appendix A (Chapter A). The infix declarations, effect module headers and imports of kernel modules that Elm admits only in its own core packages are not part of Standard Elm.
3.1Reserved Words¶
The following reserved words are used in Modules.
as exposing import module port type ..
The words are among the reserved words of the Core (Chapter 2), so none of them may be used as an identifier anywhere in a program; as is also used in patterns. The symbol .. occurs only in exposing lists, where white space may separate it from the parentheses that enclose it; since it is reserved, it is not an operator.
The word alias is not reserved. It has its special meaning only directly after the reserved word type, where a lower-case word cannot otherwise stand, since every declared begins with an upper-case letter; elsewhere alias is a .
3.2Identifiers¶
There are no further identifier classes. The class of module identifiers is introduced with the Core, because a module identifier is the qualifier of a long identifier , or . A is a sequence of one or more upper-case identifiers joined by . with no white space between them. At this level a has two further uses: it names a module, in a header and in an import, and it names an alias for an imported module, after as in an import.
The qualifiers that may be used in a module, and the module each denotes, are determined by the module’s imports: an import import as provides the qualifier and not , and an import without as provides its own (Chapter 5). A qualifier never denotes the module in which it occurs. The identifiers declared at this level (the of a value or a port, the of a custom type or an alias, the of a constructor) are never qualified where they are declared. As in the Core, the class of each identifier occurrence is determined by the grammatical rules which follow.
3.3Infixed Operators¶
Standard Elm has no fixity declarations. The operators that may be used in infix position in a module are those of the initial basis (Section 2.7). The default imports (Section 3.6) expose every one of them to every module, and the meaning of an operator is fixed by the initial basis, whatever the module imports. Each has the precedence and associativity given for it in Section 2.7, and infixed expressions are resolved as described there.
3.4Layout¶
The layout rule of Section 2.6 governs the phrases within a top-level declaration. At the top level of a module it takes the following form.
The header, each import and each top-level declaration begins in column 1.
Every other token of a header, an import or a top-level declaration lies in a column greater than 1. Hence a token in column 1 ends the header, import or declaration before it.
The annotation of a top-level value begins in column 1, and the definition it annotates begins in column 1 on a later line, with only white space and comments between them; the definition repeats .
A doc comment may stand directly after the header and directly before a top-level declaration, and nowhere else at the top level; the token that follows a doc comment begins in column 1. Doc comments have no meaning and are omitted from the grammar.
3.5Grammar for Modules¶
The phrase classes for Modules are shown in Figure 8. We use the variable to range over , to range over , and the lower-case name of each other class to range over that class. The conventions adopted in presenting the grammatical rules for Modules are the same as for the Core. The grammatical rules are shown in Figures 9 and 10.
| modules | |
| module headers | |
| import lists | |
| exposing lists | |
| exposing items | |
| top-level declarations | |
| custom-type bindings | |
| constructor bindings | |
| type-alias bindings | |
| type-variable sequences |
The grammar is that of the bare language. A module as written is brought to this form by the derived forms of Appendix A, which gather the custom types of a module into a single custom-type binding, and arrange its aliases and value definitions in dependency order, the values in groups. The header is written in every module and no derived form supplies it. Two symbols of the bare grammar are never written in a program. The symbol and joins the members of one group: the custom types of a , as it joins the members of a Core . The symbol ; separates two top-level declarations, and stands for the boundary between declarations that the layout of Section 3.4 marks by column 1. Neither symbol is reserved; in particular and is an ordinary .
A top-level value declaration is a of the Core, whose bindings carry their annotations. A port declaration consists of a and its type, and has no definition; which types a port may have is decided by elaboration (Section 5.4).
| ::= | module | ||
| ::= | plain | ||
| port module | |||
| ::= | import | ||
| empty | |||
| ::= | everything | ||
| explicit | |||
| ::= | value | ||
| operator | |||
| type | |||
| open type |
| ::= | value | ||
| custom type | |||
| type alias | |||
| port | |||
| sequential | |||
| ::= | custom-type binding | ||
| ::= | constructor binding | ||
| ::= | type-alias binding | ||
| ::= | type parameters | ||
Tested by: PortProblemDeclarationWithoutNameTest
In a constructor binding each is a type variable, a without arguments, or a type enclosed in parentheses or braces, as for the arguments of a type constructor in the Core (Chapter 2); so a function type, or a type constructor applied to arguments, is parenthesised when it is the argument of a constructor. The constructor bindings of a are separated by |, which binds less tightly than any type. In a type-alias binding and a port declaration the type extends to the end of the declaration.
3.6Default Imports¶
The imports of every module, as elaborated (Chapter 5) and evaluated (Chapter 7), are the default imports of Figure 11 followed by the imports written in the module. Since the default imports come first, an import written in the module may replace a type that a default import exposes.
import Basics exposing (..) |
import Debug |
import List exposing ((::)) |
import Maybe exposing (Maybe(..)) |
import Result exposing (Result(..)) |
import String exposing (String) |
import Char exposing (Char) |
import Tuple |
import Platform exposing (Program) |
import Platform.Cmd as Cmd exposing (Cmd) |
import Platform.Sub as Sub exposing (Sub) |
3.7Syntactic Restrictions¶
The following restrictions are conditions on a module as a phrase, except those of the last item, which are conditions on a program. Each is decidable without type information. In them, the declarations of a are the value declarations, custom type declarations, type alias declarations and port declarations of which it is the sequence.
In a module, the contains a port declaration only if the header is a port module header, and the of a module with a port module header contains at least one port declaration.
modules-portsEvery module begins with a header,
modules-headermoduleexposingorport moduleexposing. A text that does not begin with a header is not a module of Standard Elm.In an import, the after
modules-import-aliasasconsists of a single upper-case identifier.The qualifier
modules-basics-qualifierBasicsis provided only by an import of the moduleBasics: an import requires to beBasics. Since no module of a program has the identifier of a module of the initial basis (the last item), the moduleBasicsis always that of the initial basis, and the long identifiersBasics.True,Basics.FalseandBasics.negatethat derived forms introduce (Appendix A) always denote its bindings.An item
modules-no-operator-exposing()occurs only in the exposing list of an import: a module declares no operator, so it has none to expose.In the exposing list of a header, each item is bound by a value declaration or a port declaration of the module’s ; each item is the of a or a of the ; and each item is the of a , never of a . In particular a constructor cannot be exposed by name: an item names a type, and the constructors of a custom type are exposed only by or by
modules-header-exposing(..).No two items of the exposing list of a header may have the same identifier; thus and do not both occur.
modules-exposing-distinctNo value declaration of a may contain a binding other than one of the form ; there is no pattern binding at the top level.
modules-no-top-patternNo may bind the same twice by its value declarations and port declarations together.
modules-values-distinctNo may declare the same twice: custom types and aliases share one name space.
modules-types-distinctNo may declare the same twice. This counts the of each constructor binding of every , and the of each record alias, that is, each whose is, apart from enclosing parentheses, a record type without an extension variable; a record alias also declares a constructor of that name (Chapter 5).
modules-constructors-distinctNo variable bound within an expression of a (a parameter of a lambda, a variable of a pattern, or a variable bound in a
modules-no-shadowinglet) may be a bound by a value declaration or a port declaration of that , wherever the declaration stands. This extends to top-level names the restriction on shadowing of Section 2.10. Variables declared by imported modules may be bound again.No may contain the same twice.
modules-tyvarseq-distinctIn a custom-type binding , every occurring in , including the extension variable of a record type, occurs in . A of need not occur in .
modules-custom-type-tyvarsIn a type-alias binding , the contains exactly the type variables occurring in , including extension variables of record types: an alias has no unused parameter.
modules-alias-tyvarsNo alias may be recursive. For the type-alias bindings of a , say that refers to if is declared by a type-alias binding of the and occurs, unqualified, in the type of the binding of ; this relation has no cycle. The of a custom type is not related by it, so a cycle through a custom type is not excluded.
modules-alias-not-recursiveAn occurrence of a variable in an expression is delayed if it lies within a lambda expression in , and direct otherwise. For a value declaration of a , say that if contains a binding and is bound by and has a direct occurrence in . No value declaration of a may have a cycle . This takes the place, for the value declarations of a , of the restriction on recursive groups of Section 2.10. Since a binding with parameters is a derived form whose equivalent form is a lambda expression (Appendix A), every occurrence in its body is delayed, and a rejected cycle passes only through values without parameters. A delayed occurrence used during the evaluation of the very value it refers to passes this restriction; no evaluation rule applies to it (Chapter 7).
modules-value-recursionThe following restrictions concern a program, and Chapter 9 imposes them. The of every port declaration of every module of a program is distinct from the of every other port declaration of the program and from the name of every manager of the platform (Chapter 8). No module of a program has the identifier of a module of the initial basis. In the main module,
modules-programmaindoes not occur free in the expression of its own declaration, somainis not recursive, even through a delayed occurrence.
For an import, the conditions corresponding to those on the exposing list of a header depend on what the imported module exposes: an item that names an alias, an item that names a constructor rather than a type, and an item that the imported module does not expose are rejected by the elaboration of imports (Chapter 5).