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

5Static Semantics for Modules

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.

II∈\inInterface\Interface=Env= \Env
ME\ME∈\inModEnv\ModEnv=ModId→finInterface= \cls{ModId} \finmap \Interface
PE\PE∈\inPortEnv\PortEnv=Var→fin{out,in}×Type= \cls{Var} \finmap \{\smdirout, \smdirin\} \times \Type
B or T,ME,PEB\ \text{or}\ T, \ME, \PE∈\inBasis\Basis=Fin (TyName)×ModEnv×PortEnv= \Fin(\TyName) \times \ModEnv \times \PortEnv
π\pi∈\inProvMap\smProvMap=LongId→fin(Fin (ModId)∖{∅})= \smLongId \finmap (\Fin(\cls{ModId}) \setminus \{\emptyset\})
Figure 16: Further Compound Semantic Objects

An interface II 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 tycon∉Dom (TEofI)\tycon \notin \Dom(\TE \of I), or exposed. An exposed custom type is open if its constructors belong to Dom (VEofI)\Dom(\VE \of I), and closed if none of them does; in both cases I(tycon)I(\tycon) is the type structure of the type, constructors included. An exposed type alias is bound in TEofI\TE \of I, and an exposed record alias brings its constructor, of status v\isv, into VEofI\VE \of I. In a type structure (θ,VE)(\theta, \VE) of an interface, VE\VE contains a constructor of status c\isc 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 c\isc (Section 5.5).

A module environment ME\ME maps the identifier of each module elaborated so far to its interface. A port environment PE\PE maps the name of each port to its direction, out\smdirout for an outgoing port and in\smdirin for an incoming one, and to its payload type, the type of the values it carries. A static basis B=T,ME,PEB = T, \ME, \PE 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 Id\smId for the union of Var∪Con\cls{Var} \cup \cls{Con} and TyCon\cls{TyCon}, and LongId\smLongId for the union of LongVar∪LongCon\cls{LongVar} \cup \cls{LongCon} and LongTyCon\cls{LongTyCon}; as in the Core, the unions are disjoint, so a type constructor and a value constructor spelt alike are different members of Id\smId. We let id\smid range over Id\smId and longid\smlongid over LongId\smLongId, and NN over Fin (LongId)\Fin(\smLongId). For an environment EE whose qualified environment is empty, Dom E\Dom E means Dom (TEofE)∪Dom (VEofE)\Dom(\TE \of E) \cup \Dom(\VE \of E), a subset of Id\smId. For a set N⊆IdN \subseteq \smId and a module identifier modid\modid, modid.N\modid.N means {modid.id ; id∈N}\{\modid.\smid \,;\, \smid \in N\}.

A provider map π\pi 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 EE we write C⊕EC \oplus E for C+(tynames E,∅,E)C + (\tynames E, \emptyset, E), that is, EofCE \of C modified by EE and TofCT \of C extended with the type names of EE; this extends the notation C⊕TEC \oplus \TE of the Core.

5.2Interfaces

The interface that a module exposes is determined by the environment EE of its own declarations and by the exposing list of its header. For an environment EE whose qualified environment is empty, Expose(E,explist)\Expose(E, \explist) is defined as follows:

Expose(E,(..)) = EExpose(E,(item1, ⋯, itemn)) = Expose(E,item1)+⋯+Expose(E,itemn)Expose(E,var) = {var↦E(var)} in EnvExpose(E,tycon) = ({}, {tycon↦(θ,VE)}, VEv)Expose(E,tycon (..)) = ({}, {tycon↦(θ,VE)}, VE) \begin{array}{ll} \Expose(E, \kw{(..)}) {\ =\ }& E\\ \Expose(E, \kw{(}\expitem_1\kw{,}\ \cdots\kw{,}\ \expitem_n\kw{)}) {\ =\ }& \Expose(E, \expitem_1) + \cdots\\ & \quad{} + \Expose(E, \expitem_n)\\ \Expose(E, \var) {\ =\ }& \{\var \mapsto E(\var)\}\ \text{in}\ \Env\\ \Expose(E, \tycon) {\ =\ }& (\emptymap,\ \{\tycon \mapsto (\theta, \VE)\},\ \VE_{\isv})\\ \Expose(E, \tycon\,\kw{(..)}) {\ =\ }& (\emptymap,\ \{\tycon \mapsto (\theta, \VE)\},\ \VE) \end{array}

where, in the last two lines, E(tycon)=(θ,VE)E(\tycon) = (\theta, \VE) and VEv={con↦(σ,v) ; VE(con)=(σ,v)}\VE_{\isv} = \{\con \mapsto (\sigma, \isv) \,;\, \VE(\con) = (\sigma, \isv)\}. It is defined when every identifier of an item is bound in EE, 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 tycon\tycon exposes a custom type closed and a record alias with its constructor; and an item tycon (..)\tycon\,\kw{(..)} 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 II is well formed if, for every (θ,VE)∈Ran (TEofI)(\theta, \VE) \in \Ran(\TE \of I) and every con\con with VE(con)=(σ,v)\VE(\con) = (\sigma, \isv), also (VEofI)(con)=(σ,v)(\VE \of I)(\con) = (\sigma, \isv). The rules rely on the following property, which may easily be demonstrated from the rules for type declarations.

If EE is the environment to which the top-level declarations of a module elaborate, and I=Expose(E,explist)I = \Expose(E, \explist) is defined, then II is well formed and I⊆EI \subseteq E 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 modid\modid and sets N,N′⊆LongIdN, N' \subseteq \smLongId, the provider map π⊎modid(N,N′)\pi \smmerge_{\modid} (N, N') is π\pi in which modid\modid replaces the providers of the identifiers in NN and joins the providers of those in N′N': if π′=π⊎modid(N,N′)\pi' = \pi \smmerge_{\modid} (N, N'), then

π′(longid)={{modid}if longid∈N,π(longid)∪{modid}if longid∈N′∖N,π(longid)if longid∈Dom π∖(N∪N′), \pi'(\smlongid) = \begin{cases} \{\modid\} & \text{if } \smlongid \in N,\\ \pi(\smlongid) \cup \{\modid\} & \text{if } \smlongid \in N' \setminus N,\\ \pi(\smlongid) & \text{if } \smlongid \in \Dom\pi \setminus (N \cup N'), \end{cases}

where π(longid)\pi(\smlongid) is taken to be ∅\emptyset when longid∉Dom π\smlongid \notin \Dom\pi. For modid∈Dom ME\modid \in \Dom\ME, let TEmodid=TEofME(modid)\TE_{\modid} = \TE \of \ME(\modid) and VEmodid=VEofME(modid)\VE_{\modid} = \VE \of \ME(\modid), except that for the module List of the initial basis

TEList = TE0+TEofME(List),VEList = VE0+VEofME(List), \begin{array}{ll} \TE_{\kw{List}} {\ =\ }& \TE_0 + \TE \of \ME(\kw{List}),\\ \VE_{\kw{List}} {\ =\ }& \VE_0 + \VE \of \ME(\kw{List}), \end{array}

where E0=({},TE0,VE0)E_0 = (\emptymap, \TE_0, \VE_0) is the environment of the type constructor List and the list constructors [] and :: given in Appendix C. The environment that a provider map π\pi gives in ME\ME is env(ME,π)=(QE,TE,VE)\smenv(\ME, \pi) = (\QE, \TE, \VE), where

TE = {tycon↦TEmodid(tycon) ; π(tycon)={modid}}VE = {id↦VEmodid(id) ; id∈Var∪Con, π(id)={modid}}QE = {modid′↦({},TEmodid′′,VEmodid′′) ; modid′.id∈Dom π for some id} \begin{array}{ll} \TE {\ =\ }& \{\tycon \mapsto \TE_{\modid}(\tycon) \,;\, \pi(\tycon) = \{\modid\}\}\\ \VE {\ =\ }& \{\smid \mapsto \VE_{\modid}(\smid) \,;\, \smid \in \cls{Var} \cup \cls{Con},\ \pi(\smid) = \{\modid\}\}\\ \QE {\ =\ }& \{\modid' \mapsto (\emptymap, \TE_{\modid'}', \VE_{\modid'}') \,;\, \modid'.\smid \in \Dom\pi \text{ for some } \smid\} \end{array}

and, for each such modid′\modid',

TEmodid′′ = {tycon↦TEmodid(tycon) ; π(modid′.tycon)={modid}}VEmodid′′ = {id↦VEmodid(id) ; id∈Var∪Con, π(modid′.id)={modid}}. \begin{array}{ll} \TE_{\modid'}' {\ =\ }& \{\tycon \mapsto \TE_{\modid}(\tycon) \,;\, \pi(\modid'.\tycon) = \{\modid\}\}\\ \VE_{\modid'}' {\ =\ }& \{\smid \mapsto \VE_{\modid}(\smid) \,;\, \smid \in \cls{Var} \cup \cls{Con},\ \pi(\modid'.\smid) = \{\modid\}\}. \end{array}

Thus an ambiguous identifier is bound in no component of env(ME,π)\smenv(\ME, \pi), and a phrase that uses it does not elaborate, while one that does not use it is unaffected. The rules add a module modid\modid to π(longid)\pi(\smlongid) only when the last part of longid\smlongid is bound by TEmodid\TE_{\modid} or VEmodid\VE_{\modid}, so env(ME,π)\smenv(\ME, \pi) is defined for every provider map that arises.

Before any import is read, the type List and the list constructors of E0E_0 are available, provided by the module List:

π0={List↦{List}, []↦{List}, ::↦{List}}, \pi_0 = \{\kw{List} \mapsto \{\kw{List}\},\ \kw{[]} \mapsto \{\kw{List}\},\ \kw{::} \mapsto \{\kw{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 OTSTAT\OTstat of the initial basis, in every module alike (Section 4.3). For a module identifier modid\modid, ops(modid)⊆Dom OTSTAT\smops(\modid) \subseteq \Dom\OTstat is the set of operators that the module modid\modid 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 Imports\cls{Imports}, which we write imports0\imports_0. For any imports\imports, we write imports0 imports\imports_0\ \imports for the import list consisting of the default imports followed by those of imports\imports.

5.4Port Types

The values that may cross a port are those of the types τ\tau for which Port(τ)\Port(\tau) holds. Port\Port is the smallest predicate on types that satisfies the conditions of Figure 17; it holds for no other type. In the figure, Maybe\tMaybe, Array\tArray, Bytes\tBytes and Value\tValue 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 Value\tValue as well.

Port(τ)if τ∈{Int,Float,Bool,if τ∈{String,Bytes,(),Value}Port(τ List)if Port(τ)Port(τ Maybe)if Port(τ)Port(τ Array)if Port(τ)Port((τ1,τ2)Tuple2)if Port(τ1) and Port(τ2)Port((τ1,τ2,τ3)Tuple3)if Port(τ1), Port(τ2)and Port(τ3)Port({ϱ})if Port(ϱ(lab))for every lab∈Dom ϱ \begin{array}{ll} \Port(\tau) {\qquad}& \text{if } \tau \in \{\tInt, \tFloat, \tBool,\\ {\qquad}& \phantom{\text{if } \tau \in \{} \tString, \tBytes, \tunit, \tValue\}\\[2pt] \Port(\tau\,\tList) {\qquad}& \text{if } \Port(\tau)\\[2pt] \Port(\tau\,\tMaybe) {\qquad}& \text{if } \Port(\tau)\\[2pt] \Port(\tau\,\tArray) {\qquad}& \text{if } \Port(\tau)\\[2pt] \Port((\tau_1, \tau_2)\tTuple{2}) {\qquad}& \text{if } \Port(\tau_1) \text{ and } \Port(\tau_2)\\[2pt] \Port((\tau_1, \tau_2, \tau_3)\tTuple{3}) {\qquad}& \text{if } \Port(\tau_1),\ \Port(\tau_2)\\ {\qquad}& \text{and } \Port(\tau_3)\\[2pt] \Port(\rec{\varrho}) {\qquad}& \text{if } \Port(\varrho(\lab))\\ {\qquad}& \text{for every } \lab \in \Dom\varrho \end{array}
Figure 17: Port Types

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.

TypePort?Reason
Int, Float, String, BoolyesJSON numbers, strings and Booleans
Bytesyesbyte sequences, which are not JSON values
()yesJSON null
Json.Encode.Value, Json.Decode.Valueyesany JSON value, never a byte sequence
List tt, Array ttif tt isJSON arrays
Maybe ttif tt isnull or the value
2- and 3-tuplesif each component isJSON arrays
closed recordsif each field isJSON objects
Charnoexcluded in Standard Elm
function typesnono JSON representation
type variables, constrained ones includednono JSON representation
extensible recordsnounknown fields
custom types other than Bool and Maybe: Result, Order, Never, Dict, Set, and every custom type of a programnono canonical encoding
Cmd, Sub, Task, and other abstract types of the basisnono JSON representation
Figure 18: What can and cannot be sent over a port

5.5Inference Rules

As for the Core, the rules of the static semantics of Modules allow sentences of the form

A⊢phrase⇒A′ \jdg{A}{\mathit{phrase}}{A'}

to be inferred, where in this case AA 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 A′A' is a semantic object or a tuple of semantic objects. The sentence is pronounced “phrase\mathit{phrase} elaborates to A′A' in AA”. 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 tyvarseq=tyvar1⋯tyvark\tyvarseq = \tyvar_1 \cdots \tyvar_k that parametrises a phrase (the conbind\conbind of a custom-type binding or the ty\ty of a type-alias binding), we write tyvarseq=ξ(k)\tyvarseq = \tyrowvar^{(k)} when, for each ii, ξi\tyrowvar_i is the variable named by tyvari\tyvar_i (Section 4.1) that is a row variable if tyvari\tyvar_i occurs in the phrase as the extension variable of a record type expression { tyvari | tyrow }\kw{\kwlb}\ \tyvar_i\ \kw{|}\ \tyrow\ \kw{\kwrb}, and a type variable otherwise. For a type constructor tycon\tycon we write con=tycon\con = \tycon when con\con is spelt as tycon\tycon. The partial function labs\smlabs is defined on a type expression ty\ty that is a record type expression { lab1 : ty1, ⋯, labn : tyn }\kw{\kwlb}\ \lab_1\ \kw{:}\ \ty_1\kw{,}\ \cdots\kw{,}\ \lab_n\ \kw{:}\ \ty_n\ \kw{\kwrb} (n≥0n \geq 0), possibly enclosed in parentheses, and gives the sequence of its labels in the order written:

labs(ty)=lab1⋯labn. \smlabs(\ty) = \lab_1 \cdots \lab_n .

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 BB be a basis with tynames (MEofB)⊆TofB\tynames(\ME \of B) \subseteq T \of B, and let B⊢module⇒(T′,ME′,PE′),E\jdg{B}{\modm}{(T', \ME', \PE'), E} be inferred. Then T′∩(TofB)=∅T' \cap (T \of B) = \emptyset, and tynames E\tynames E, tynames ME′\tynames \ME' and tynames PE′\tynames \PE' are subsets of (TofB)∪T′(T \of B) \cup T'.

Moreover, in every instance of rule (57), TE\TE is determined by CC and the phrase, up to the choice of the type names in Ran TE\Ran\TE that are not in TofCT \of C.

ModulesB⊢module⇒B′,E\jdg{B}{\modm}{B', E}
B,π0⊢imports0 imports⇒πC=(TofB, ∅, env(MEofB,π))C⊢topdec⇒E,PEE⊢header⇒(modid,I)modid∉Dom (MEofB)T′=tynames E∖(TofB)B⊢header imports topdec⇒(T′,{modid↦I},PE),E\frac{\begin{array}{c}\jdg{B, \pi_0}{\imports_0\ \imports}{\pi} \premsep C = (T \of B,\ \emptyset,\ \smenv(\ME \of B, \pi)) \\ \jdg{C}{\topdec}{E, \PE} \premsep \jdg{E}{\header}{(\modid, I)} \\ \modid \notin \Dom(\ME \of B) \premsep T' = \tynames E \setminus (T \of B)\end{array}}{\begin{array}{c}\jdg{B}{\header\ \imports\ \topdec}{(T', \{\modid \mapsto I\}, \PE), E}\end{array}}
(46)
Comments:
(46)

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 CC 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.x refers to an import with the qualifier M, if there is one. The header is elaborated last, in the environment EE of the declarations of the module alone; hence a module never exposes what it imports. The set T′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\ME \of B, and modid∉Dom (MEofB)\modid \notin \Dom(\ME \of B), 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 named main: only the main of the main module of a program is constrained, in Chapter 9.

HeadersE⊢header⇒(modid,I)\jdg{E}{\header}{(\modid, I)}
I=Expose(E,explist)E⊢⟨port⟩ module modid exposing explist⇒(modid,I)\frac{\begin{array}{c}I = \Expose(E, \explist)\end{array}}{\begin{array}{c}\jdg{E}{\opt{\kw{port}}\ \kw{module}\ \modid\ \kw{exposing}\ \explist}{(\modid, I)}\end{array}}
(47)
Comments:
(47)

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 Expose\Expose 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.

ImportsB,π⊢imports⇒π′\jdg{B, \pi}{\imports}{\pi'}

In rule (48), modid′′\modid'' is modid′\modid' if the first option is present and modid\modid if it is absent.

(MEofB)(modid)=Iπ1=π⊎modid(∅, modid′′.Dom I)⟨ ⁣⟨modid,I⊢explist⇒(N,N′)⟩ ⁣⟩π2=π1 ⟨ ⁣⟨⊎modid(N,N′)⟩ ⁣⟩B,π2⊢imports⇒π′B,π⊢import modid ⟨as modid′⟩ ⟨ ⁣⟨exposing explist⟩ ⁣⟩ imports⇒π′\frac{\begin{array}{c}(\ME \of B)(\modid) = I \premsep \pi_1 = \pi \smmerge_{\modid} (\emptyset,\ \modid''.\Dom I) \\ \optt{\jdg{\modid, I}{\explist}{(N, N')}} \premsep \pi_2 = \pi_1\ \optt{{}\smmerge_{\modid} (N, N')} \\ \jdg{B, \pi_2}{\imports}{\pi'}\end{array}}{\begin{array}{c}\jdg{B, \pi}{\kw{import}\ \modid\ \opt{\kw{as}\ \modid'}\ \optt{\kw{exposing}\ \explist}\ \imports}{\pi'}\end{array}}
(48)
B,π⊢ ⇒π\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{B, \pi}{\ }{\pi}\end{array}}
(49)
Comments:
(48)

Every identifier that the module exposes becomes available under the qualifier modid′′\modid'', and the identifiers listed by the exposing list become available unqualified. With as, the qualifier is modid′\modid' instead of modid\modid: 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 NN. A module that is not in MEofB\ME \of B cannot be imported.

Exposing Listsmodid,I⊢explist⇒(N,N′)\jdg{\modid, I}{\explist}{(N, N')}
modid,I⊢(..)⇒(∅,Dom I)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{\modid, I}{\kw{(..)}}{(\emptyset, \Dom I)}\end{array}}
(50)
modid,I⊢itemi⇒(Ni,Ni′)(1≤i≤n)modid,I⊢(item1, ⋯, itemn)⇒(N1∪⋯∪Nn, N1′∪⋯∪Nn′)\frac{\begin{array}{c}\jdg{\modid, I}{\expitem_i}{(N_i, N'_i)} \quad (1 \le i \le n)\end{array}}{\begin{array}{c}\jdg{\modid, I}{\kw{(}\expitem_1\kw{,}\ \cdots\kw{,}\ \expitem_n\kw{)}}{(N_1 \cup \cdots \cup N_n,\ N'_1 \cup \cdots \cup N'_n)}\end{array}}
(51)
Comments:
(50)

Everything is joined and nothing replaced, so two imports with exposing (..) of modules that both expose x make x ambiguous. The imports elaborate nevertheless, and only a use of x fails to elaborate. The operators of ops(modid)\smops(\modid) are also exposed, in the sense of Section 3.3, but no operator is bound.

Exposing Itemsmodid,I⊢item⇒(N,N′)\jdg{\modid, I}{\expitem}{(N, N')}
var∈Dom (VEofI)modid,I⊢var⇒(∅,{var})\frac{\begin{array}{c}\var \in \Dom(\VE \of I)\end{array}}{\begin{array}{c}\jdg{\modid, I}{\var}{(\emptyset, \{\var\})}\end{array}}
(52)
op∈ops(modid)modid,I⊢(op)⇒(∅,∅)\frac{\begin{array}{c}\opv \in \smops(\modid)\end{array}}{\begin{array}{c}\jdg{\modid, I}{\kw{(}\opv\kw{)}}{(\emptyset, \emptyset)}\end{array}}
(53)
I(tycon)=(θ,VE)N′={con ; VE(con)=(σ,v) for some σ}modid,I⊢tycon⇒({tycon},N′)\frac{\begin{array}{c}I(\tycon) = (\theta, \VE) \premsep N' = \{\con \,;\, \VE(\con) = (\sigma, \isv) \text{ for some } \sigma\}\end{array}}{\begin{array}{c}\jdg{\modid, I}{\tycon}{(\{\tycon\}, N')}\end{array}}
(54)
I(tycon)=(θ,VE)VE(con)=(σ,c) for some con,σmodid,I⊢tycon (..)⇒({tycon}, Dom VE∩Dom (VEofI))\frac{\begin{array}{c}I(\tycon) = (\theta, \VE) \premsep \VE(\con) = (\sigma, \isc) \text{ for some } \con, \sigma\end{array}}{\begin{array}{c}\jdg{\modid, I}{\tycon\,\kw{(..)}}{(\{\tycon\},\ \Dom\VE \cap \Dom(\VE \of I))}\end{array}}
(55)
Comments:
(52)

A value is joined with what earlier imports provide, so a value exposed by name by two modules is ambiguous.

(53)

The rule only checks that the module declares the operator, and the item binds nothing: the operators exposed to a module are those that may be used in it in infix position (Section 3.3), and the meaning of each is given by OTSTAT\OTstat (rule (4)), whatever the module imports.

(54)

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, N′N' 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), I(Just)I(\kw{Just}) is undefined, and no rule applies.

(55)

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 Dom (VEofI)\Dom(\VE \of I), 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.

Top-level DeclarationsC⊢topdec⇒E,PE\jdg{C}{\topdec}{E, \PE}
C+VE⊢valbind⇒VEVE′=ClosCVEC⊢valbind⇒VE′ in Env, {}\frac{\begin{array}{c}\jdg{C + \VE}{\valbind}{\VE} \premsep \VE' = \Clos_C \VE\end{array}}{\begin{array}{c}\jdg{C}{\valbind}{\VE'\ \text{in}\ \Env,\ \emptymap}\end{array}}
(56)
⟨C⊕TE⊢datbind⇒VE′,TE′∀(t,VE′′)∈Ran TE′, t∉TofC⟩C⊕TE⊢typbindi⇒VEi,TEi(1≤i≤l)TE=⟨TE′+⟩TE1+⋯+TElVE=⟨VE′+⟩VE1+⋯+VEltyvars TE=∅C⊢⟨type datbind ;⟩ type alias typbind1 ; ⋯; type alias typbindl⇒({},TE,VE), {}\frac{\begin{array}{c}\opt{\jdg{C \oplus \TE}{\datbind}{\VE', \TE'} \premsep \forall (t, \VE'') \in \Ran\TE',\ t \notin T \of C} \\ \jdg{C \oplus \TE}{\typbind_i}{\VE_i, \TE_i} \quad (1 \le i \le l) \\ \TE = \opt{\TE' +{}} \TE_1 + \cdots + \TE_l \premsep \VE = \opt{\VE' +{}} \VE_1 + \cdots + \VE_l \\ \tyvars \TE = \emptyset\end{array}}{\begin{array}{c}C \vdash \opt{\kw{type}\ \datbind\ \kw{;}}\ \kw{type alias}\ \typbind_1\ \kw{;}\ \cdots \\ \qquad \kw{;}\ \kw{type alias}\ \typbind_l \Rightarrow (\emptymap, \TE, \VE),\ \emptymap\end{array}}
(57)
U is new for ty in CC+U⊢ty⇒ττ=τ′→α CmdPort(τ′)C⊢port var : ty⇒{var↦(ClosCτ,v)} in Env, {var↦(out,τ′)}\frac{\begin{array}{c}U \text{ is new for } \ty \text{ in } C \premsep \jdg{C + U}{\ty}{\tau} \\ \tau = \tau' \rightarrow \alpha\,\sbtCmd \premsep \Port(\tau')\end{array}}{\begin{array}{c}\jdg{C}{\kw{port}\ \var\ \kw{:}\ \ty}{\{\var \mapsto (\Clos_C \tau, \isv)\}\ \text{in}\ \Env,\ \{\var \mapsto (\smdirout, \tau')\}}\end{array}}
(58)
U is new for ty in CC+U⊢ty⇒ττ=(τ′→α)→α SubPort(τ′)C⊢port var : ty⇒{var↦(ClosCτ,v)} in Env, {var↦(in,τ′)}\frac{\begin{array}{c}U \text{ is new for } \ty \text{ in } C \premsep \jdg{C + U}{\ty}{\tau} \\ \tau = (\tau' \rightarrow \alpha) \rightarrow \alpha\,\sbtSub \premsep \Port(\tau')\end{array}}{\begin{array}{c}\jdg{C}{\kw{port}\ \var\ \kw{:}\ \ty}{\{\var \mapsto (\Clos_C \tau, \isv)\}\ \text{in}\ \Env,\ \{\var \mapsto (\smdirin, \tau')\}}\end{array}}
(59)
C⊢topdec1⇒E1,PE1C⊕E1⊢topdec2⇒E2,PE2the separator is not one of a type-declaration phraseC⊢topdec1 ; topdec2⇒E1+E2, PE1+PE2\frac{\begin{array}{c}\jdg{C}{\topdec_1}{E_1, \PE_1} \premsep \jdg{C \oplus E_1}{\topdec_2}{E_2, \PE_2} \\ \text{the separator is not one of a type-declaration phrase}\end{array}}{\begin{array}{c}\jdg{C}{\topdec_1\ \kw{;}\ \topdec_2}{E_1 + E_2,\ \PE_1 + \PE_2}\end{array}}
(60)
Comments:
(56)

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.

(57)

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 l≥0l \geq 0, and l≥1l \geq 1 when the option is absent, in which case the phrase begins with the first alias, and when l=0l = 0 it is type datbind\kw{type}\ \datbind. Since TE\TE 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 CC 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 TofCT \of C makes the type names of the custom types new. The side condition tyvars TE=∅\tyvars\TE = \emptyset keeps the equation from justifying itself: the closures of rules (61) and (64) are taken in C⊕TEC \oplus \TE, 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 TE\TE 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.

(58)

The variables of the annotation are made explicit, as for an annotated value. The argument of Cmd\sbtCmd 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 Port(τ′)\Port(\tau') excludes type variables, α\alpha is the only variable of τ\tau, and the closure generalises it. The type Cmd\sbtCmd is that of the initial basis; a type of a program spelt Cmd is a different type. A port is a value of status v\isv, so it is used and exposed like any other value; PE\PE 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.

(59)

The variable α\alpha 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.

(60)

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. CC, a declaration of the module replaces an imported binding of the same identifier, or makes an ambiguous one available. The syntactic restrictions ensure that Dom E1∩Dom E2=∅\Dom E_1 \cap \Dom E_2 = \emptyset and Dom PE1∩Dom PE2=∅\Dom\PE_1 \cap \Dom\PE_2 = \emptyset.

Custom-type BindingsC⊢datbind⇒VE,TE\jdg{C}{\datbind}{\VE, \TE}
tyvarseq=ξ(k)U={ξ1,⋯ ,ξk}C+U⊢tyvari⇒τi(1≤i≤k)C+U,τ(k)t⊢conbind⇒VEt∈TyName(k)⟨C⊢datbind′⇒VE′,TE′∀(t′,VE′′)∈Ran TE′, t≠t′⟩C⊢tycon tyvarseq = conbind ⟨and datbind′⟩⇒ClosCVE ⟨+VE′⟩, {tycon↦(t,ClosCVE)} ⟨+TE′⟩\frac{\begin{array}{c}\tyvarseq = \tyrowvar^{(k)} \premsep U = \{\tyrowvar_1, \cdots, \tyrowvar_k\} \\ \jdg{C + U}{\tyvar_i}{\tau_i} \quad (1 \le i \le k) \\ \jdg{C + U, \tau^{(k)}t}{\conbind}{\VE} \premsep t \in \TyName^{(k)} \\ \opt{\jdg{C}{\datbind'}{\VE', \TE'} \premsep \forall (t', \VE'') \in \Ran\TE',\ t \neq t'}\end{array}}{\begin{array}{c}C \vdash \tycon\ \tyvarseq\ \kw{=}\ \conbind\ \opt{\kw{and}\ \datbind'} \Rightarrow \\ \qquad \Clos_C \VE\ \opt{{}+ \VE'},\ \{\tycon \mapsto (t, \Clos_C \VE)\}\ \opt{{}+ \TE'}\end{array}}
(61)
Comments:
(61)

The parameters are made explicit while the constructors elaborate, and are generalised by the closure, since UofC=∅U \of C = \emptyset at the top level. A parameter need not occur in conbind\conbind, so phantom types are allowed. A parameter used as the extension variable of a record type is a row variable; τi\tau_i is then the open record type with no fields of its own, and an argument of tt in that position is a record type. The side condition t≠t′t \neq t' gives the custom types of one group distinct type names; rule (57) makes them new. The syntactic restrictions ensure that Dom VE∩Dom VE′=∅\Dom\VE \cap \Dom\VE' = \emptyset and tycon∉Dom TE′\tycon \notin \Dom\TE'.

Constructor BindingsC,τ⊢conbind⇒VE\jdg{C, \tau}{\conbind}{\VE}
C⊢tyi⇒τi(1≤i≤n)⟨C,τ⊢conbind′⇒VE⟩C,τ⊢con ty1⋯tyn ⟨| conbind′⟩⇒{con↦(τ1→⋯→τn→τ,c)} ⟨+VE⟩\frac{\begin{array}{c}\jdg{C}{\ty_i}{\tau_i} \quad (1 \le i \le n) \premsep \opt{\jdg{C, \tau}{\conbind'}{\VE}}\end{array}}{\begin{array}{c}C, \tau \vdash \con\ \ty_1 \cdots \ty_n\ \opt{\kw{|}\ \conbind'} \Rightarrow \\ \qquad \{\con \mapsto (\tau_1 \rightarrow \cdots \rightarrow \tau_n \rightarrow \tau, \isc)\}\ \opt{{}+ \VE}\end{array}}
(62)
Comments:
(62)

When n=0n = 0 the type of con\con is τ\tau, so a constructor without arguments is a value of the custom type. The syntactic restrictions ensure con∉Dom VE\con \notin \Dom\VE.

Alias BindingsC⊢typbind⇒VE,TE\jdg{C}{\typbind}{\VE, \TE}
tyvarseq=ξ(k)C+{ξ1,⋯ ,ξk}⊢ty⇒τty∉Dom labsC⊢tycon tyvarseq = ty⇒{}, {tycon↦(Λξ(k).τ,{})}\frac{\begin{array}{c}\tyvarseq = \tyrowvar^{(k)} \premsep \jdg{C + \{\tyrowvar_1, \cdots, \tyrowvar_k\}}{\ty}{\tau} \premsep \ty \notin \Dom\smlabs\end{array}}{\begin{array}{c}\jdg{C}{\tycon\ \tyvarseq\ \kw{=}\ \ty}{\emptymap,\ \{\tycon \mapsto (\Lambda\tyrowvar^{(k)}.\tau, \emptymap)\}}\end{array}}
(63)
tyvarseq=ξ(k)C+{ξ1,⋯ ,ξk}⊢ty⇒τlabs(ty)=lab1⋯labnτ={ϱ}ϱ={lab1↦τ1,⋯ ,labn↦τn}con=tyconVE=ClosC{con↦(τ1→⋯→τn→τ,v)}C⊢tycon tyvarseq = ty⇒VE, {tycon↦(Λξ(k).τ,VE)}\frac{\begin{array}{c}\tyvarseq = \tyrowvar^{(k)} \premsep \jdg{C + \{\tyrowvar_1, \cdots, \tyrowvar_k\}}{\ty}{\tau} \\ \smlabs(\ty) = \lab_1 \cdots \lab_n \premsep \tau = \rec{\varrho} \premsep \varrho = \{\lab_1 \mapsto \tau_1, \cdots, \lab_n \mapsto \tau_n\} \\ \con = \tycon \premsep \VE = \Clos_C \{\con \mapsto (\tau_1 \rightarrow \cdots \rightarrow \tau_n \rightarrow \tau, \isv)\}\end{array}}{\begin{array}{c}\jdg{C}{\tycon\ \tyvarseq\ \kw{=}\ \ty}{\VE,\ \{\tycon \mapsto (\Lambda\tyrowvar^{(k)}.\tau, \VE)\}}\end{array}}
(64)
Comments:
(63)

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 ty\ty, so the type function has no unused parameter. Rules (63) and (64) both cover the production; which applies is decided by the form of ty\ty. An alias of an extensible record type, or of another alias, defines no constructor, even when that alias is a record alias.

(64)

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 v\isv, 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 TofBT \of B (rules (61) and (57)). Let DD and D′D' be derivations for a module in a basis BB. Then D′D' is an instance of DD when, for some bijection ρ\rho from the type names that DD generates to those that D′D' generates, D′D' is an instance, in the sense of Section 4.10, of the derivation obtained from DD by replacing each such type name tt by ρ(t)\rho(t); the sets TT of corresponding contexts are then equal. A derivation for a module in BB is principal when every derivation for the module in BB is an instance of it.

Every module that elaborates in a basis BB has a principal derivation in BB, 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 BB 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.

Discussions

Discuss this chapter·Search the discussions on GitHub

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