This chapter defines the static semantics of Modules. It introduces interfaces, which record what a module makes available to the modules that import it, module environments, port environments and static bases, together with provider maps, which record which imported module supplies each identifier that a module may use. The rules infer that a module elaborates, in a basis, to the part of a basis it contributes and to the environment of its own declarations; on the way they infer what the imports, exposing lists, top-level declarations and type declarations of the module elaborate to. Since Elm modules export top-level names only, there are no signatures, functors or sharing, and no matching of an environment against a specification. The admissible types of ports are defined in Section 5.4.
5.1Semantic Objects¶
The simple objects for the static semantics of Modules are exactly as for the Core (Section 4.1). The compound objects are those for the Core, augmented by those in Figure 16.
An interface is an environment whose qualified environment is empty. It is what a module exposes: the types, values and constructors that another module may name after importing it. Every type that a module declares is either private, if , or exposed. An exposed custom type is open if its constructors belong to , and closed if none of them does; in both cases is the type structure of the type, constructors included. An exposed type alias is bound in , and an exposed record alias brings its constructor, of status , into . In a type structure of an interface, contains a constructor of status exactly when the type constructor is a custom type, since a custom type has at least one constructor and an alias has none of status (Section 5.5).
A module environment maps the identifier of each module elaborated so far to its interface. A port environment maps the name of each port to its direction, for an outgoing port and for an incoming one, and to its payload type, the type of the values it carries. A static basis consists of the set of type names generated so far, the interfaces of the modules elaborated so far, and the ports they declare; port names are global to a program (Chapter 9). We write for the union of and , and for the union of and ; as in the Core, the unions are disjoint, so a type constructor and a value constructor spelt alike are different members of . We let range over and over , and over . For an environment whose qualified environment is empty, means , a subset of . For a set and a module identifier , means .
A provider map maps each identifier that the imports of a module make available, unqualified or qualified, to the set of modules that provide it. An identifier with more than one provider is ambiguous.
The operations of projection, injection and modification are as for the Core (Section 4.3). Moreover, for an environment we write for , that is, modified by and extended with the type names of ; this extends the notation of the Core.
5.2Interfaces¶
The interface that a module exposes is determined by the environment of its own declarations and by the exposing list of its header. For an environment whose qualified environment is empty, is defined as follows:
where, in the last two lines, and . It is defined when every identifier of an item is bound in , which the syntactic restrictions of a header ensure (Section 3.7); a header exposes no operator. Thus (..) exposes every type open and every value, constructor and port; an item exposes a custom type closed and a record alias with its constructor; and an item exposes a custom type open. No condition relates the exposed values to the exposed types: a value may be exposed whose type mentions a private type. An interface is well formed if, for every and every with , also . The rules rely on the following property, which may easily be demonstrated from the rules for type declarations.
If is the environment to which the top-level declarations of a module elaborate, and is defined, then is well formed and componentwise.
The interfaces of the initial basis (Appendix C) are well formed.
5.3Provider Maps¶
The imports of a module are read in order, and each one adds to a provider map. For a module identifier and sets , the provider map is in which replaces the providers of the identifiers in and joins the providers of those in : if , then
where is taken to be when . For , let and , except that for the module List of the initial basis
where is the environment of the type constructor List and the list constructors [] and :: given in Appendix C. The environment that a provider map gives in is , where
and, for each such ,
Thus an ambiguous identifier is bound in no component of , and a phrase that uses it does not elaborate, while one that does not use it is unaffected. The rules add a module to only when the last part of is bound by or , so is defined for every provider map that arises.
Before any import is read, the type List and the list constructors of are available, provided by the module List:
where the first List is a type constructor and the others are module identifiers.
Operators are not bound in environments: the type scheme and status of an operator are given by the static operator table of the initial basis, in every module alike (Section 4.3). For a module identifier , is the set of operators that the module declares. For a module of the initial basis it is given in Appendix C (Figure 40); for every other module it is empty, since no program declares an operator. The default imports of Figure 11, taken in order, form a phrase of the class , which we write . For any , we write for the import list consisting of the default imports followed by those of .
5.4Port Types¶
The values that may cross a port are those of the types for which holds. is the smallest predicate on types that satisfies the conditions of Figure 17; it holds for no other type. In the figure, , , and are the type names of the initial basis for Maybe, Array, Bytes.Bytes and Json.Encode.Value (Appendix C), whatever a program binds to type constructors of the same spelling. The type Json.Decode.Value is an alias of Json.Encode.Value, and so denotes as well.
Types are semantic objects, in which every alias has been replaced by its expansion (Section 4.5), so an alias of a port type is a port type. Every port type is built from type names of the initial basis alone. Figure 18 states the same predicate by kinds of type, with the reason each excluded kind is excluded; the encoding of the values of each port type as boundary values, which are JSON values in which byte sequences may stand, is given in Section 8.7.
| Type | Port? | Reason |
Int, Float, String, Bool | yes | JSON numbers, strings and Booleans |
Bytes | yes | byte sequences, which are not JSON values |
() | yes | JSON null |
Json.Encode.Value, Json.Decode.Value | yes | any JSON value, never a byte sequence |
List , Array | if is | JSON arrays |
Maybe | if is | null or the value |
| 2- and 3-tuples | if each component is | JSON arrays |
| closed records | if each field is | JSON objects |
Char | no | excluded in Standard Elm |
| function types | no | no JSON representation |
| type variables, constrained ones included | no | no JSON representation |
| extensible records | no | unknown fields |
custom types other than Bool and Maybe: Result, Order, Never, Dict, Set, and every custom type of a program | no | no canonical encoding |
Cmd, Sub, Task, and other abstract types of the basis | no | no JSON representation |
5.5Inference Rules¶
As for the Core, the rules of the static semantics of Modules allow sentences of the form
to be inferred, where in this case is a basis, a basis paired with a provider map, an environment, a module identifier paired with an interface, a context, or a context paired with a type, and is a semantic object or a tuple of semantic objects. The sentence is pronounced “ elaborates to in ”. Hypotheses not of this form are side conditions. The convention for options is the same as for the Core static semantics.
The rules use the following notation for phrases. For a type-variable sequence that parametrises a phrase (the of a custom-type binding or the of a type-alias binding), we write when, for each , is the variable named by (Section 4.1) that is a row variable if occurs in the phrase as the extension variable of a record type expression , and a type variable otherwise. For a type constructor we write when is spelt as . The partial function is defined on a type expression that is a record type expression (), possibly enclosed in parentheses, and gives the sequence of its labels in the order written:
It is undefined on every other type expression, an extensible record type expression or a type constructor among them.
The type names of a basis are intended to include those of its interfaces. The following property can be demonstrated.
Let be a basis with , and let be inferred. Then , and , and are subsets of .
Moreover, in every instance of rule (57), is determined by and the phrase, up to the choice of the type names in that are not in .
Whether a module is a port module matters only to the syntactic restrictions, which allow port declarations in it and require at least one. The exposing list of a header elaborates by the operation of Section 5.2, which the syntactic restrictions make defined; the rules of the groups Exposing Lists and Exposing Items below are for the exposing lists of imports.
In rule (48), is if the first option is present and if it is absent.
Every identifier that the module exposes becomes available under the qualifier , and the identifiers listed by the exposing list become available unqualified. With as, the qualifier is instead of : after import Dict as D the identifier Dict.empty is not available through this import. Several imports may share a qualifier; their identifiers are joined under it, and an identifier exposed under one qualifier by two modules is ambiguous. Since the imports are read from left to right, a later import replaces what an earlier one provides for the identifiers in its . A module that is not in cannot be imported.
A value is joined with what earlier imports provide, so a value exposed by name by two modules is ambiguous.
A type listed by name replaces any type of that name provided by an earlier import: after import Foo exposing (Maybe) the type constructor Maybe denotes the type of Foo, not that of the default import. For a record alias, holds its constructor, which is joined; for a custom type it is empty, so the constructors of a type are not imported by this item. A constructor cannot be imported by name: in import Maybe exposing (Just), is undefined, and no rule applies.
The side condition holds only for a custom type, so Alias(..) does not elaborate for an alias. For a type that its module exposes closed, no constructor is in , so the item elaborates, imports the type and imports no constructors: after import A exposing (T(..)), where A exposes T closed, the constructors of T are not available.
The rule is that for declarations in the Core (rule (19)): a group of top-level values is recursive, an unannotated member is monomorphic within its group, and the closure makes every top-level value polymorphic in its other uses. At the top level the restriction on recursive values is syntactic and weaker than in a let (Section 3.7), so f = \n -> f n elaborates here.
The phrase consists of the custom types of a module, as one group, followed by its aliases, as Appendix A arranges them (Section A.2); here , and when the option is absent, in which case the phrase begins with the first alias, and when it is . Since occurs on both sides, every type constructor of the phrase is in scope in all of it: a constructor may take an argument whose type is an alias declared in the phrase, and an alias may mention a custom type of the phrase or another alias. The equation has a solution determined by and the phrase up to the choice of new type names (Section 5.5), because the type function of a custom type is its type name alone, and no alias refers to itself through aliases. The side condition on makes the type names of the custom types new. The side condition keeps the equation from justifying itself: the closures of rules (61) and (64) are taken in , so without it an environment whose constructors keep a free variable, as K : a -> T a for type T a = K a, would be a solution too; with it, the constructors are closed and is determined as the property above says. Rule (60) does not split such a phrase (its side condition), so the type declarations of a module are elaborated together, and their order does not matter: a custom type may mention an alias that follows it. A local type spelt as an imported one is a different type, and the unqualified type constructor denotes the local one, in the declarations before it as well as after it: with type T = T String followed by type alias String = Int, the constructor T takes an Int.
The variables of the annotation are made explicit, as for an annotated value. The argument of must be a type variable, so port send : String -> Cmd Msg does not elaborate, nor does port send : Cmd msg, which has no payload, nor port send : Int -> Int
-> Cmd msg. Since excludes type variables, is the only variable of , and the closure generalises it. The type is that of the initial basis; a type of a program spelt Cmd is a different type. A port is a value of status , so it is used and exposed like any other value; records its direction and payload type, by which Chapter 8 gives it its meaning, and Chapter 9 requires the names of the ports of a program to be distinct. Thus port send : Char -> Cmd msg does not elaborate, while port
send : List { id : Int, name : String } -> Cmd msg does.
The variable occurs in both places, so port recv : (Int -> a) -> Sub b does not elaborate. As for an outgoing port, port recv : (Json.Decode.Value -> msg) -> Sub msg elaborates, and port recv : (Result String Int -> msg) -> Sub msg does not.
Each declaration is elaborated in a context that contains the declarations before it. A type-declaration phrase, whose parts are separated by ; too, is not split: rule (57) elaborates it as a whole, so that a custom type sees the aliases that follow it. , a declaration of the module replaces an imported binding of the same identifier, or makes an ambiguous one available. The syntactic restrictions ensure that and .
The parameters are made explicit while the constructors elaborate, and are generalised by the closure, since at the top level. A parameter need not occur in , so phantom types are allowed. A parameter used as the extension variable of a record type is a row variable; is then the open record type with no fields of its own, and an argument of in that position is a record type. The side condition gives the custom types of one group distinct type names; rule (57) makes them new. The syntactic restrictions ensure that and .
When the type of is , so a constructor without arguments is a value of the custom type. The syntactic restrictions ensure .
An alias generates no type name: it denotes a type function, and a type expression that applies it denotes the expansion (rule (28)). Thus after type alias Id = Int, the types Id and Int are the same type. By the syntactic restrictions the parameters are exactly the type variables of , so the type function has no unused parameter. Rules (63) and (64) both cover the production; which applies is decided by the form of . An alias of an extensible record type, or of another alias, defines no constructor, even when that alias is a record alias.
A record alias also defines a constructor, spelt as the alias, whose arguments are the fields in the order written: after type alias P = { y : Int, x : Int }, the expression P 1 2 denotes the record { y = 1, x = 2 }. Its status is , so it may be applied but may not occur in a pattern (rule (43)). It is exposed and imported together with the alias (rule (54)).
5.6Principal Derivations¶
Instances and principal derivations are defined for a module as for a Core phrase (Section 4.10), with one addition: a module generates type names, which a derivation chooses freely outside (rules (61) and (57)). Let and be derivations for a module in a basis . Then is an instance of when, for some bijection from the type names that generates to those that generates, is an instance, in the sense of Section 4.10, of the derivation obtained from by replacing each such type name by ; the sets of corresponding contexts are then equal. A derivation for a module in is principal when every derivation for the module in is an instance of it.
Every module that elaborates in a basis has a principal derivation in , and any two of its principal derivations differ only by a renaming of type and row variables and of the type names they generate.
A derivation for a module combines derivations for its top-level declarations, whose contexts the rules determine from and the earlier declarations, up to the type names chosen; so the property follows from that of the Core. The proof is not given here.
5.7Further Restrictions¶
The further restrictions of the Core (Section 4.11) apply to the expressions and patterns of every top-level declaration, judged, as in the Core, on the principal derivation for the module (Section 5.6). The following conditions concern a program rather than a module, and Chapter 9 imposes them: the type of main in the main module, a main in any other module being an ordinary value; the distinctness of the names of the ports of a program from one another and from the names of the managers of the platform (Chapter 8); the absence of any reference of main to itself, directly or through a delayed occurrence; and the distinctness of the identifiers of the modules of a program from those of the modules of the initial basis. Some implementations accept further programs, such as those with a further type of flags for main, or reject programs that Standard Elm admits, such as those with Bytes in a payload type, or those that exceed limits on the sizes of records, constructors and functions. None of these acceptances or limits is part of Standard Elm; each is recorded in Appendix E.
The default imports are read before the imports of the module, so an import written in the module may replace a type that a default import exposes. The context C holds only what the imports provide. The declarations of the module are added by rule (60) to the unqualified environment and never under a qualifier, so a module cannot refer to its own names by qualification: in a module
M,M.xrefers to an import with the qualifierM, if there is one. The header is elaborated last, in the environment E of the declarations of the module alone; hence a module never exposes what it imports. The set T′ holds every type name that the module generates, private types included, since an exposed value may have a type that mentions a private type. An import names a module of MEofB, and modid∈/Dom(MEofB), so no module imports itself and no cycle of imports elaborates; Chapter 9 elaborates the modules of a program in an order in which each follows the modules it imports. Nothing here constrains a value namedmain: only themainof the main module of a program is constrained, in Chapter 9.