Elm Exactly
A Standard Definition of Elm?Draft version 0.4 / Chapter 3

3Syntax of Modules

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 tycon\tycon begins with an upper-case letter; elsewhere alias is a var\var.

3.2Identifiers

There are no further identifier classes. The class ModId\cls{ModId} of module identifiers is introduced with the Core, because a module identifier is the qualifier of a long identifier longvar\longvar, longcon\longcon or longtycon\longtycon. A modid\modid is a sequence of one or more upper-case identifiers joined by . with no white space between them. At this level a modid\modid 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 modid\modid as modid′\modid' provides the qualifier modid′\modid' and not modid\modid, and an import without as provides its own modid\modid (Chapter 5). A qualifier never denotes the module in which it occurs. The identifiers declared at this level (the var\var of a value or a port, the tycon\tycon of a custom type or an alias, the con\con 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 var : ty\var\ \kw{:}\ \ty 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 var\var.

  • 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 module\modm to range over Module\cls{Module}, item\expitem to range over ExpItem\cls{ExpItem}, 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.

Module\cls{Module}modules
Header\cls{Header}module headers
Imports\cls{Imports}import lists
ExpList\cls{ExpList}exposing lists
ExpItem\cls{ExpItem}exposing items
TopDec\cls{TopDec}top-level declarations
DatBind\cls{DatBind}custom-type bindings
ConBind\cls{ConBind}constructor bindings
TypBind\cls{TypBind}type-alias bindings
TyVarSeq\cls{TyVarSeq}type-variable sequences
Figure 8: Modules Phrase Classes

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 datbind\datbind, as it joins the members of a Core valbind\valbind. 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 var\var.

A top-level value declaration is a valbind\valbind of the Core, whose bindings carry their annotations. A port declaration consists of a var\var and its type, and has no definition; which types a port may have is decided by elaboration (Section 5.4).

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 9: 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
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)
Figure 10: Grammar: Top-level Declarations and Bindings

Tested by: PortProblemDeclarationWithoutNameTest

In a constructor binding con ty1 ⋯ tyn\con\ \ty_1\ \cdots\ \ty_n each tyi\ty_i is a type variable, a longtycon\longtycon 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 conbind\conbind 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)
Figure 11: Default Imports

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 topdec\topdec are the value declarations, custom type declarations, type alias declarations and port declarations of which it is the sequence.

  • In a module, the topdec\topdec contains a port declaration only if the header is a port module header, and the topdec\topdec of a module with a port module header contains at least one port declaration.

    modules-ports
  • Every module begins with a header, module modid\modid exposing explist\explist or port module modid\modid exposing explist\explist. A text that does not begin with a header is not a module of Standard Elm.

    modules-header
  • In an import, the modid\modid after as consists of a single upper-case identifier.

    modules-import-alias
  • The qualifier Basics is provided only by an import of the module Basics: an import import modid as Basics\kw{import}\ \modid\ \kw{as}\ \kw{Basics} requires modid\modid to be Basics. Since no module of a program has the identifier of a module of the initial basis (the last item), the module Basics is always that of the initial basis, and the long identifiers Basics.True, Basics.False and Basics.negate that derived forms introduce (Appendix A) always denote its bindings.

    modules-basics-qualifier
  • An item (op\opv) occurs only in the exposing list of an import: a module declares no operator, so it has none to expose.

    modules-no-operator-exposing
  • In the exposing list of a header, each item var\var is bound by a value declaration or a port declaration of the module’s topdec\topdec; each item tycon\tycon is the tycon\tycon of a datbind\datbind or a typbind\typbind of the topdec\topdec; and each item tycon (..)\tycon\,\kw{(..)} is the tycon\tycon of a datbind\datbind, never of a typbind\typbind. In particular a constructor cannot be exposed by name: an item tycon\tycon names a type, and the constructors of a custom type are exposed only by tycon (..)\tycon\,\kw{(..)} or by (..).

    modules-header-exposing
  • No two items of the exposing list of a header may have the same identifier; thus tycon\tycon and tycon (..)\tycon\,\kw{(..)} do not both occur.

    modules-exposing-distinct
  • No value declaration of a topdec\topdec may contain a binding other than one of the form var ⟨: ty⟩ = exp\var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm; there is no pattern binding at the top level.

    modules-no-top-pattern
  • No topdec\topdec may bind the same var\var twice by its value declarations and port declarations together.

    modules-values-distinct
  • No topdec\topdec may declare the same tycon\tycon twice: custom types and aliases share one name space.

    modules-types-distinct
  • No topdec\topdec may declare the same con\con twice. This counts the con\con of each constructor binding of every datbind\datbind, and the tycon\tycon of each record alias, that is, each typbind\typbind tycon tyvarseq = ty\tycon\ \tyvarseq\ \kw{=}\ \ty whose ty\ty is, apart from enclosing parentheses, a record type { ⟨tyrow⟩ }\kw{\kwlb}\,\opt{\tyrow}\,\kw{\kwrb} without an extension variable; a record alias also declares a constructor of that name (Chapter 5).

    modules-constructors-distinct
  • No variable bound within an expression of a topdec\topdec (a parameter of a lambda, a variable of a pattern, or a variable bound in a let) may be a var\var bound by a value declaration or a port declaration of that topdec\topdec, 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.

    modules-no-shadowing
  • No tyvarseq\tyvarseq may contain the same tyvar\tyvar twice.

    modules-tyvarseq-distinct
  • In a custom-type binding tycon tyvarseq = conbind\tycon\ \tyvarseq\ \kw{=}\ \conbind, every tyvar\tyvar occurring in conbind\conbind, including the extension variable of a record type, occurs in tyvarseq\tyvarseq. A tyvar\tyvar of tyvarseq\tyvarseq need not occur in conbind\conbind.

    modules-custom-type-tyvars
  • In a type-alias binding tycon tyvarseq = ty\tycon\ \tyvarseq\ \kw{=}\ \ty, the tyvarseq\tyvarseq contains exactly the type variables occurring in ty\ty, including extension variables of record types: an alias has no unused parameter.

    modules-alias-tyvars
  • No alias may be recursive. For the type-alias bindings of a topdec\topdec, say that tycon\tycon refers to tycon′\tycon' if tycon′\tycon' is declared by a type-alias binding of the topdec\topdec and occurs, unqualified, in the type of the binding of tycon\tycon; this relation has no cycle. The tycon\tycon of a custom type is not related by it, so a cycle through a custom type is not excluded.

    modules-alias-not-recursive
  • An occurrence of a variable in an expression exp\expm is delayed if it lies within a lambda expression in exp\expm, and direct otherwise. For a value declaration valbind\valbind of a topdec\topdec, say that var→var′\var \to \var' if valbind\valbind contains a binding var ⟨: ty⟩ = exp\var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm and var′\var' is bound by valbind\valbind and has a direct occurrence in exp\expm. No value declaration of a topdec\topdec may have a cycle var1→⋯→varn→var1\var_1 \to \cdots \to \var_n \to \var_1 (n≥1)(n \geq 1). This takes the place, for the value declarations of a topdec\topdec, 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-recursion
  • The following restrictions concern a program, and Chapter 9 imposes them. The var\var of every port declaration of every module of a program is distinct from the var\var 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, main does not occur free in the expression of its own declaration, so main is not recursive, even through a delayed occurrence.

    modules-program

For an import, the conditions corresponding to those on the exposing list of a header depend on what the imported module exposes: an item tycon (..)\tycon\,\kw{(..)} that names an alias, an item tycon\tycon 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).

Discussions

Discuss this chapter·Search the discussions on GitHub

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