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

9Programs

This chapter defines what a program is and what it means. A program is a main module together with the modules it imports. It is elaborated and evaluated module by module, in a basis that pairs a static basis (Chapter 5) with a dynamic basis (Chapter 7); elaboration finds the type of main in the main module and the ports of the program, and evaluation finds the value of main. That value is a program value (Chapter 8), and the program runs on the platform (Chapter 1), which supplies the primitives and managers beyond those of tasks and ports. The running of a program is defined as a labelled transition system, whose configurations hold the current model, the messages awaiting update, the processes, the current subscriptions and the values awaiting delivery on outgoing ports. The sentences of the chapter infer that a program elaborates to a type and a port environment, that it evaluates to a value, that a program value started with flags begins in a configuration, and that a configuration moves to another, with or without an event. The meaning of a program is the set of sequences of events at its boundary that it can produce.

9.1Syntax of Programs

The phrase class Program of programs is defined as follows:

program\program::=module ⟨program⟩\modm\ \opt{\program}

A program is thus a non-empty sequence of modules; its last module is its main module. A module module\modm imports a module modid\modid if an import of module\modm, written or default (Figure 11), names modid\modid. Programs are subject to the following restrictions, in addition to the restrictions of Section 3.7 on each module.

  • No two modules of a program have the same module identifier, and no module of a program has the identifier of a module of the initial basis (Appendix C).

    program-modules-distinct
  • Each module of a program imports only modules of the initial basis and modules that stand before it in the program.

    program-import-order
  • Every module of a program other than the main module is imported, directly or through other modules, by the main module.

    program-modules-reachable
  • The main module has a top-level value declaration that binds main. That declaration has the form main ⟨: ty⟩ = exp\kw{main}\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm, binding main alone, and main does not occur free in exp\expm, so that main does not refer to itself, directly or through a delayed occurrence.

    program-main

Thus the modules of a program are the main module and every module it imports directly or indirectly; the import relation among them has no cycle; and every module stands after the modules it imports. A program as written is the finite set of these modules. Any sequence of that set that meets the restrictions is a program in the sense of the grammar; the main module comes last in every such sequence, and all such sequences have the same meaning (Section 9.3).

9.2Elaboration of Programs

Until now, the name Basis and the metavariable BB have stood for a static basis in Chapter 5 and for a dynamic basis in Chapter 7; those chapters never needed both at once. In this chapter the class Basis of Chapter 5 is called StaticBasis\pgStaticBasis, with metavariable BSTAT\pgBstat, and that of Chapter 7 is called DynamicBasis\pgDynamicBasis, with metavariable BDYN\pgBdyn; a basis is a pair of the two:

B or (BSTAT,BDYN)∈Basis=StaticBasis×DynamicBasis. B\ \text{or}\ (\pgBstat,\pgBdyn)\in\Basis=\pgStaticBasis\times\pgDynamicBasis .

The initial basis B0B_0 is the pair of the initial static basis of Appendix C and the initial dynamic basis of Appendix D. Modification of static bases is component by component, the set component by union, as for contexts (Section 4.3):

(T,ME,PE)+(T′,ME′,PE′)=(T∪T′, ME+ME′, PE+PE′); (T,\ME,\PE)+(T',\ME',\PE')=(T\cup T',\ \ME+\ME',\ \PE+\PE') ;

a dynamic basis is a module environment, modified as a finite map.

The rules of this section and the next allow sentences of the forms

BSTAT⊢STATprogram⇒(τ,PE),BDYN⊢DYNprogram⇒v,B⊢program⇒(τ,PE,v) \begin{array}{c} \jstat{\pgBstat}{\program}{(\tau,\PE)},\qquad \jdyn{\pgBdyn}{\program}{v},\\[0.5ex] \jdg{B}{\program}{(\tau,\PE,v)} \end{array}

to be inferred, where BSTAT\pgBstat is a static basis, BDYN\pgBdyn a dynamic basis, BB a basis, τ\tau a type, PE\PE a port environment (Section 5.1) and vv a value. We write ⊢STAT\vdash_{\mathrm{STAT}} for the elaboration of programs, ⊢DYN\vdash_{\mathrm{DYN}} for their evaluation, and ⊢\vdash alone for the sentence that combines the two. The first sentence is pronounced “program\program elaborates to τ\tau and PE\PE in BSTAT\pgBstat”: τ\tau is the type of main in the main module, and PE\PE holds the ports of the program. The second is pronounced “program\program evaluates to vv in BDYN\pgBdyn”: vv is the value of main. The third is pronounced “program\program elaborates to τ\tau and PE\PE, and evaluates to vv, in BB”. Premises also use the sentences about modules of Chapters 5 and 7, in their forms there; other hypotheses are side conditions.

The type of main is the instance of its type scheme that takes Float\tFloat for each bound type variable of class number\knum. It is the type of the default copy of the declaration of main, the copy that number specialisation makes of every top-level declaration, taking Float\tFloat for each of its variables of class number\knum (Section 6.2). For a type scheme σ=∀ξ(k).τ\sigma=\forall\tyrowvar^{(k)}.\tau, let α1,…,αl\alpha_1,\ldots,\alpha_l be those of the bound variables ξ1,…,ξk\tyrowvar_1,\ldots,\tyrowvar_k that are type variables of class number\knum; then

Dflt σ=τ{Float,…,Float/α1,…,αl}. \pgDflt\sigma=\tau\{\tFloat,\ldots,\tFloat/\alpha_1,\ldots,\alpha_l\} .

The substitution respects classes, since Float∈Admnumber\tFloat\in\Adm{\knum}. In the rules below, Program\sbtProgram is the type name of Platform.Program, of arity 3, and Node\sbtNode that of VirtualDom.Node, of arity 1, of which Html is an alias (Appendix C); Managerplat\effMgrPlat is the set of the names of the managers of the platform (Section 8.1). In each rule, the premise BSTAT⊢module⇒BSTAT′,E\jdg{\pgBstat}{\modm}{\pgBstat',E} is inferred by a principal derivation for the module (Section 5.6), so that EE, and the scheme σ\sigma of main in it, are principal.

Elaboration of ProgramsBSTAT⊢STATprogram⇒(τ,PE)\jstat{\pgBstat}{\program}{(\tau,\PE)}
BSTAT⊢module⇒BSTAT′,EDom (PEofBSTAT′)∩(Dom (PEofBSTAT)∪Managerplat)=∅BSTAT+BSTAT′⊢STATprogram⇒(τ,PE)BSTAT⊢STATmodule program⇒(τ,PE)\frac{\begin{array}{c}\jdg{\pgBstat}{\modm}{\pgBstat',E}\\ \Dom(\PE\of\pgBstat')\cap\big(\Dom(\PE\of\pgBstat)\cup\effMgrPlat\big)=\emptyset\\ \jstat{\pgBstat+\pgBstat'}{\program}{(\tau,\PE)}\end{array}}{\begin{array}{c}\jstat{\pgBstat}{\modm\ \program}{(\tau,\PE)}\end{array}}
(172)
BSTAT⊢module⇒BSTAT′,EDom (PEofBSTAT′)∩(Dom (PEofBSTAT)∪Managerplat)=∅E(main)=(σ,v)σ=∀ξ(k).(τ1,τ2,τ3)ProgramPort(τ1)BSTAT⊢STATmodule⇒(Dflt σ, PEof(BSTAT+BSTAT′))\frac{\begin{array}{c}\jdg{\pgBstat}{\modm}{\pgBstat',E}\\ \Dom(\PE\of\pgBstat')\cap\big(\Dom(\PE\of\pgBstat)\cup\effMgrPlat\big)=\emptyset\\ E(\kw{main})=(\sigma,\isv)\premsep \sigma=\forall\tyrowvar^{(k)}.(\tau_1,\tau_2,\tau_3)\sbtProgram\premsep \Port(\tau_1)\end{array}}{\begin{array}{c}\jstat{\pgBstat}{\modm}{\big(\pgDflt\sigma,\ \PE\of(\pgBstat+\pgBstat')\big)}\end{array}}
(173)
BSTAT⊢module⇒BSTAT′,EDom (PEofBSTAT′)∩(Dom (PEofBSTAT)∪Managerplat)=∅E(main)=(σ,v)Dflt σ=τ NodeBSTAT⊢STATmodule⇒(τ Node, PEof(BSTAT+BSTAT′))\frac{\begin{array}{c}\jdg{\pgBstat}{\modm}{\pgBstat',E}\\ \Dom(\PE\of\pgBstat')\cap\big(\Dom(\PE\of\pgBstat)\cup\effMgrPlat\big)=\emptyset\\ E(\kw{main})=(\sigma,\isv)\premsep \pgDflt\sigma=\tau\,\sbtNode\end{array}}{\begin{array}{c}\jstat{\pgBstat}{\modm}{\big(\tau\,\sbtNode,\ \PE\of(\pgBstat+\pgBstat')\big)}\end{array}}
(174)
Comments:
(172)

Each module is elaborated in the static basis that the modules before it have built, so it finds there the interface of every module it imports, and the type names it generates are new (rule (46)). The side condition makes port names global to the program: no two ports of a program share a name, even in different modules, and no port has the name of a manager of the platform; a program with such a clash does not elaborate.

(173)

Rules (173) and (174) cover the one production, since main may have either of two forms of type. Only the main of the main module is constrained; a main in another module is an ordinary value. The scheme of main is taken from EE, the environment of all the declarations of the module, not from its interface, so main need not be exposed. A bound variable of class number\knum in the scheme of main is Float\tFloat in its type: with init = \() -> (0, Cmd.none) and no other constraint on the model, the model type is Float\tFloat, and the value of main is that of its default copy, at Float\tFloat (rule (176)). The flags type τ1\tau_1 is a port type, and is taken from the scheme before the default, so it contains no type variable. Thus main : Program (Int -> Int) Model Msg and main : Program flags Model Msg, with flags a type variable, do not elaborate, and nor does an unannotated main whose init is \n -> (n + 1, Cmd.none), whose flags type is a variable of class number\knum. The unit type is a port type, and as flags it is treated specially (Section 9.4). Since Bytes is a port type, flags may be, or contain, values of type Bytes. The model type τ2\tau_2 and the message type τ3\tau_3 are unconstrained. Some implementations admit further types of flags, which are not port types (Appendix E).

(174)

A main of type Html msg or Svg msg has a type τ Node\tau\,\sbtNode, since both are aliases. No function type is of either form, so a main with parameters does not elaborate.

Since the derivations for the modules of a program are principal, a flags type that the program leaves undetermined is a type variable in the scheme of main, which is not a port type: an unannotated main = Platform.worker { init = \_ -> (0, Cmd.none), … }, whose init ignores its flags, does not elaborate, although a derivation that took the flags to be ()\tunit would satisfy the rule. There is no rule for a program one of whose modules does not elaborate. Such a program has no meaning, and no part of it is evaluated (Section 9.3).

9.3Evaluation of Programs

The modules of a program are evaluated in the order in which they are elaborated, each in the dynamic basis that the modules before it have built. The syntax evaluated is the reduced syntax of Section 7.1, which records the direction and payload type that elaboration finds for each port, after number specialisation (Section 6.2), which makes the default copy of every top-level declaration besides the copies its uses need; when the declaration quantifies no variable of class number\knum, the default copy is the declaration itself. In rule (176), VE(main)\VE(\kw{main}) is the value bound by the default copy of the declaration of main, whose type is Dflt σ\pgDflt\sigma for the scheme σ\sigma of main, the type found by rule (173) or (174).

Evaluation of ProgramsBDYN⊢DYNprogram⇒v\jdyn{\pgBdyn}{\program}{v}
BDYN⊢module⇒BDYN′,VEBDYN+BDYN′⊢DYNprogram⇒vBDYN⊢DYNmodule program⇒v\frac{\begin{array}{c}\jdg{\pgBdyn}{\modm}{\pgBdyn',\VE}\premsep \jdyn{\pgBdyn+\pgBdyn'}{\program}{v}\end{array}}{\begin{array}{c}\jdyn{\pgBdyn}{\modm\ \program}{v}\end{array}}
(175)
BDYN⊢module⇒BDYN′,VEv=VE(main)BDYN⊢DYNmodule⇒v\frac{\begin{array}{c}\jdg{\pgBdyn}{\modm}{\pgBdyn',\VE}\premsep v=\VE(\kw{main})\end{array}}{\begin{array}{c}\jdyn{\pgBdyn}{\modm}{v}\end{array}}
(176)
Comments:
(175)

A module is evaluated once, after every module it imports, and its top-level values are evaluated then, whether or not they are used: every top-level declaration has its default copy, even when nothing uses it. A main in a module other than the main module is evaluated in the same way, once, as an ordinary value.

(176)

The value of main is taken from VE\VE, the value environment of all the top-level declarations of the module (rule (113)), not from the basis BDYN′\pgBdyn', which holds only what the module exposes; so main need not be exposed. No module imports the main module, and BDYN′\pgBdyn' is not used. If a top-level value of any module has no value (it reaches Debug.todo, or forces a delayed reference to itself), no sentence about the program can be inferred, and the program has no meaning.

ProgramsB⊢program⇒(τ,PE,v)\jdg{B}{\program}{(\tau,\PE,v)}
BSTATofB⊢STATprogram⇒(τ,PE)BDYNofB⊢DYNprogram⇒vB⊢program⇒(τ,PE,v)\frac{\begin{array}{c}\jstat{\pgBstat\of B}{\program}{(\tau,\PE)}\premsep \jdyn{\pgBdyn\of B}{\program}{v}\end{array}}{\begin{array}{c}\jdg{B}{\program}{(\tau,\PE,v)}\end{array}}
(177)
Comments:
(177)

A program is elaborated as a whole before any part of it is evaluated: if one module fails to elaborate, the static premise fails, and nothing is evaluated, not even the modules before it. Elaboration may choose the new type names of a module freely (rule (46)), but neither PE\PE, whose types are built from type names of the initial basis alone, nor vv depends on the choice.

A program program\program has a meaning when B0⊢program⇒(τ,PE,v)\jdg{B_0}{\program}{(\tau,\PE,v)} for some τ\tau, PE\PE and vv. Since a program is a set of modules, the following property, which may easily be demonstrated, is needed for this to make sense.

Let program\program and program′\program' be sequences of the same modules, both meeting the restrictions of Section 9.1. Then the sentence B0⊢program⇒(τ,PE,v)\jdg{B_0}{\program}{(\tau,\PE,v)} can be inferred if and only if the sentence B0⊢program′⇒(τ′,PE,v)\jdg{B_0}{\program'}{(\tau',\PE,v)} can, where τ′\tau' is τ\tau with the type names that the modules generate renamed.

9.4Execution

Let B0⊢program⇒(τ,PE,v)\jdg{B_0}{\program}{(\tau,\PE,v)}. When τ\tau is τ′ Node\tau'\,\sbtNode, the program is a view, whose meaning is given in Section 9.6. Otherwise τ=(τ1,τ2,τ3)Program\tau=(\tau_1,\tau_2,\tau_3)\sbtProgram, where τ1\tau_1 is the flags type, and vv is a program value prog r\effprog\,r (Section 8.5). This section defines how a program value runs. Throughout, PE\PE is the port environment of the program, and the record rr is fixed: the rules apply its fields init, update and subscriptions, written r(init)r(\kw{init}) and so on.

A program runs on a platform. Besides the primitives Primplat\effPrimPlat, their outcomes and the managers Managerplat\effMgrPlat (Chapter 8), the platform provides a set Eventplat\pgEventPlat of platform events, and its platform steps, defined in Section 9.4.3. All of these are parameters of the platform; the definition fixes the manager of tasks, the ports, and the program loop.

9.4.1Configurations and Events

For a set AA, A∗=⋃n≥0AnA^{*}=\bigcup_{n\geq 0}A^n is the set of finite sequences of members of AA. The empty sequence is nil\pgnil, and x⋅yx\cdot y is the concatenation of the sequences xx and yy; a member of AA is also a sequence of length one, and x1⋯xnx_1\cdots x_n is x1⋅…⋅xnx_1\cdot\ldots\cdot x_n. A finite multiset of members of AA is a finite map from AA to the positive integers, giving the multiplicity of each member; multisets are written as in Section 8.2. The objects of execution are shown in Figure 26.

s or ds\ \text{or}\ d∈\inTime\pgTime={x∈R∣x≥0}= \{x\in\mathbb{R}\mid x\geq 0\}
KK∈\inConfig\Config=Time×Val×Val∗×ProcMap= \pgTime\times\Val\times\Val^{*}\times\pgProcMap
=×SubVal×OutQueue×Pending\phantom{{}={}}\times\SubVal\times\pgOutQueue\times\pgPending
PP∈\inProcMap\pgProcMap=ProcId→finProc= \ProcId\finmap\pgProc
∈\inProc\pgProc=(TaskVal×Time×(CmdVal∪{spawned}))= \big(\TaskVal\times\pgTime\times(\CmdVal\cup\{\pgspawned\})\big)
=∪{ended}\phantom{{}={}}\cup\{\pgended\}
QQ∈\inOutQueue\pgOutQueue=Var→finBVal∗= \cls{Var}\finmap\BVal^{*}
HH∈\inPending\pgPending=CmdVal→fin{1,2,…}= \CmdVal\finmap\{1,2,\ldots\}
ℓ\ell∈\inEvent\Event=({in}×Var×BVal)= (\{\pgEvIn\}\times\cls{Var}\times\BVal)
=∪({out}×Var×BVal)∪Action\phantom{{}={}}\cup(\{\pgEvOut\}\times\cls{Var}\times\BVal)\cup\effAction
=∪({wait}×Time)∪Eventplat\phantom{{}={}}\cup(\{\pgwait\}\times\pgTime)\cup\pgEventPlat
Figure 26: Semantic Objects of Execution

A configuration K=(s,v,M,P,sb,Q,H)K=(s,v,M,P,\sbv,Q,H) has the following components.

  • ss is the present time, in milliseconds since the program started.

  • vv is the model, the value that the latest update, or init, produced.

  • MM is the message queue, the messages awaiting update.

  • PP holds every process started so far. If P(pid)=(tk,s′,o)P(\pid)=(\tk,s',\pgorig), the process named by pid\pid is live, is reducing tk\tk, and took its latest step (or started) at time s′s'; its origin o\pgorig is the leaf command whose task it performs, or spawned\pgspawned if it was started by spawn\effspawn. If P(pid)=endedP(\pid)=\pgended, the process has ended or been killed.

  • sb\sbv is the current subscription value, the value of subscriptions applied to the model.

  • Q(p)Q(\effp) is the sequence of boundary values awaiting delivery on the outgoing port p\effp.

  • HH holds the leaf commands handed to the managers of the platform and not yet acted on.

The event in(p,j)\pgEvIn(\effp,j) is the host’s sending of jj on the incoming port p\effp; out(p,j)\pgEvOut(\effp,j) is the program’s delivery of jj on the outgoing port p\effp; an action a\effa (Section 8.4) is a primitive performed by a process; wait d\pgwait\,d, with d>0d>0, is the passing of dd milliseconds; and a platform event is whatever the platform makes observable of its managers.

For a manager m\effm and a value vv, the leaf commands (and leaf subscriptions) of m\effm with argument vv are

Leaf(m,v)={map(vn,⋯map(v1,leaf(m,v))⋯ )∣n≥0}. \pgLeaf(\effm,v)=\{\effmap(v_n,\cdots\effmap(v_1,\effleaf(\effm,v))\cdots) \mid n\geq 0\} .

The outgoing queues at the start of a program are

Qinit={p↦nil ; PE(p)=(out,τ) for some τ}. \pgQinit=\{\effp\mapsto\pgnil\,;\,\PE(\effp)=(\smdirout,\tau)\ \text{for some}\ \tau\} .

For a type τ\tau with Port(τ)\Port(\tau) and a boundary value jj, the flags value of jj at τ\tau is

Flagsτ(j)={()if τ=(),Decτ(j)otherwise, \pgFlags{\tau}(j)=\begin{cases} () & \text{if}\ \tau=\tunit,\\ \Dec{\tau}(j) & \text{otherwise}, \end{cases}

which is undefined when Decτ(j)\Dec{\tau}(j) is.

9.4.2Dispatch and Start

The rules of this section and the next allow sentences of the forms

s,P,Q,H⊢L⇒(P′,Q′,H′),PE,τ1,j⊢prog r⇒K,K→⟨ℓ⟩K′ \jdg{s,P,Q,H}{L}{(P',Q',H')},\qquad \jdg{\PE,\tau_1,j}{\effprog\,r}{K},\qquad K\step{\opt{\ell}}K'

to be inferred. In the first, LL is a sequence of leaf commands, and the sentence is pronounced “dispatching LL at time ss turns PP, QQ, HH into P′P', Q′Q', H′H'”. The second is pronounced “prog r\effprog\,r, started with the flags jj, begins in KK”. The third is pronounced “KK moves to K′K'”, and, when the event ℓ\ell is present, “with ℓ\ell”; a transition without an event is silent. Premises also use the sentences of Chapter 8 and the sentence ⊢v1 v2⇒v\japp{v_1}{v_2}{v} of the application of values (Section 6.7); other hypotheses are side conditions. The convention for options is the same as for the Core static semantics. There are no states and no exception packets: the configuration is the whole of what changes, and every rule is given in full.

A command is dispatched by dispatching a sequence that holds its leaf commands, each as many times as Flat\effFlat gives it, in any order. Which order is no part of the meaning, by the effect order convention of Section 8.5.

Dispatchs,P,Q,H⊢L⇒(P′,Q′,H′)\jdg{s,P,Q,H}{L}{(P',Q',H')}
s,P,Q,H⊢nil⇒(P,Q,H)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{s,P,Q,H}{\pgnil}{(P,Q,H)}\end{array}}
(178)
c∈Leaf(task,tk)pid∉Dom Ps, P+{pid↦(tk,s,c)}, Q,H⊢L⇒(P′,Q′,H′)s,P,Q,H⊢c⋅L⇒(P′,Q′,H′)\frac{\begin{array}{c}\effc\in\pgLeaf(\effmtask,\tk)\premsep\pid\notin\Dom P\\ \jdg{s,\ P+\{\pid\mapsto(\tk,s,\effc)\},\ Q,H}{L}{(P',Q',H')}\end{array}}{\begin{array}{c}\jdg{s,P,Q,H}{\effc\cdot L}{(P',Q',H')}\end{array}}
(179)
c∈Leaf(out p,j)s,P, Q+{p↦Q(p)⋅j}, H⊢L⇒(P′,Q′,H′)s,P,Q,H⊢c⋅L⇒(P′,Q′,H′)\frac{\begin{array}{c}\effc\in\pgLeaf(\effmout\,\effp,j)\\ \jdg{s,P,\ Q+\{\effp\mapsto Q(\effp)\cdot j\},\ H}{L}{(P',Q',H')}\end{array}}{\begin{array}{c}\jdg{s,P,Q,H}{\effc\cdot L}{(P',Q',H')}\end{array}}
(180)
c∈Leaf(m,v)m∈Managerplats,P,Q, H⊎{ ⁣∣ c ∣ ⁣}⊢L⇒(P′,Q′,H′)s,P,Q,H⊢c⋅L⇒(P′,Q′,H′)\frac{\begin{array}{c}\effc\in\pgLeaf(\effm,v)\premsep\effm\in\effMgrPlat\\ \jdg{s,P,Q,\ H\uplus\effmset{\effc}}{L}{(P',Q',H')}\end{array}}{\begin{array}{c}\jdg{s,P,Q,H}{\effc\cdot L}{(P',Q',H')}\end{array}}
(181)
Comments:
(179)

Each task leaf starts a process of its own, with an identifier that names no process started before, live or ended; a leaf that occurs twice in a command starts two processes, and performs its task twice. The process keeps the leaf as its origin, since the message it gives, when its task succeeds, comes from the leaf (rule (188)).

(180)

The boundary value was computed when the command was made (Section 8.7); dispatch only appends it to the queue of its port. Since Q(p)Q(\effp) is defined only for the outgoing ports of the program, p\effp is one of them. The values of one command for one port join the queue in the order of LL, which is unspecified: with Cmd.batch [send 1, send 2], either value may be delivered first.

(181)

What the managers of the platform do with the leaves handed to them is given by the platform steps (Section 9.4.3). No rule dispatches a leaf of any other manager: a leaf of an incoming port is a subscription, never a command, and there are no effect modules in Standard Elm.

Starting a ProgramPE,τ1,j⊢prog r⇒K\jdg{\PE,\tau_1,j}{\effprog\,r}{K}
Flagsτ1(j)=v1⊢r(init) v1⇒(v,cm)Flat(cm)={ ⁣∣ c1,…,cn ∣ ⁣}0, {}, Qinit, {}⊢c1⋯cn⇒(P,Q,H)⊢r(subscriptions) v⇒sbPE,τ1,j⊢prog r⇒(0,v,nil,P,sb,Q,H)\frac{\begin{array}{c}\pgFlags{\tau_1}(j)=v_1\premsep\japp{r(\kw{init})}{v_1}{(v,\cm)}\\ \effFlat(\cm)=\effmset{\effc_1,\ldots,\effc_n}\premsep \jdg{0,\ \emptymap,\ \pgQinit,\ \emptymap}{\effc_1\cdots\effc_n}{(P,Q,H)}\\ \japp{r(\kw{subscriptions})}{v}{\sbv}\end{array}}{\begin{array}{c}\jdg{\PE,\tau_1,j}{\effprog\,r}{(0,v,\pgnil,P,\sbv,Q,H)}\end{array}}
(182)
Comments:
(182)

The host starts a program with a boundary value jj as its flags; when the host gives no flags, jj is null\effnull. With flags of the unit type every boundary value is accepted, whereas a port of the unit type accepts only null\effnull. When Flagsτ1(j)\pgFlags{\tau_1}(j) is undefined, as for j=1.5j=1.5 with flags type Int, no rule applies, there is no configuration in which the program begins, and the program does not start; how an implementation reports this is outside the definition (Appendix E). The commands of init are dispatched, and the subscriptions computed from the initial model, before any message is handled. The empty map {}\emptymap serves here as the empty process map and the empty multiset.

9.4.3Transitions

A platform step K→⟨ℓ⟩platK′K\pgplatstep{\opt{\ell}}K' is given by the platform. Its event ℓ\ell, if present, is a platform event. A platform step changes nothing but the components MM and HH of KK: it removes leaf commands from HH, and it appends to MM messages that, as the platform defines, are given by the leaf commands it removes or by the members of Flat(sb)\effFlat(\sbv) whose manager is in Managerplat\effMgrPlat (Section 8.8). Whether a platform step exists may depend on the present time ss.

TransitionsK→⟨ℓ⟩K′K\step{\opt{\ell}}K'
⊢r(update) v1⇒v2⊢v2 v⇒(v′,cm)Flat(cm)={ ⁣∣ c1,…,cn ∣ ⁣}s,P,Q,H⊢c1⋯cn⇒(P′,Q′,H′)⊢r(subscriptions) v′⇒sb′(s,v,v1⋅M,P,sb,Q,H)→(s,v′,M,P′,sb′,Q′,H′)\frac{\begin{array}{c}\japp{r(\kw{update})}{v_1}{v_2}\premsep\japp{v_2}{v}{(v',\cm)}\\ \effFlat(\cm)=\effmset{\effc_1,\ldots,\effc_n}\premsep \jdg{s,P,Q,H}{\effc_1\cdots\effc_n}{(P',Q',H')}\\ \japp{r(\kw{subscriptions})}{v'}{\sbv'}\end{array}}{\begin{array}{c}(s,v,v_1\cdot M,P,\sbv,Q,H)\step{}(s,v',M,P',\sbv',Q',H')\end{array}}
(183)
P(pid)=(tk,s′,o)tk→⟨a⟩tk′⟨a=π(v1⋯vk)⇓tk′′π∈Primplat⟩(s,v,M,P,sb,Q,H)→⟨a⟩(s,v,M,P+{pid↦(tk′,s,o)},sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\tk,s',\pgorig)\premsep\tk\effred{\opt{\effa}}\tk'\\ \opt{\effa=\effdone{\pi(v_1\cdots v_k)}{\tk''}\premsep\pi\in\effPrimPlat}\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\opt{\effa}} (s,v,M,P+\{\pid\mapsto(\tk',s,\pgorig)\},\sbv,Q,H)\end{array}}
(184)
P(pid)=(tk,s′,o)tk→atk′a=sleep(v′)⇓tk′′if v′∈Fltfin then s≥s′+max⁡(0,v′)(s,v,M,P,sb,Q,H)→a(s,v,M,P+{pid↦(tk′,s,o)},sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\tk,s',\pgorig)\premsep\tk\effred{\effa}\tk'\premsep \effa=\effdone{\effsleep(v')}{\tk''}\\ \text{if}\ v'\in\effFltFin\ \text{then}\ s\geq s'+\max(0,v')\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\effa} (s,v,M,P+\{\pid\mapsto(\tk',s,\pgorig)\},\sbv,Q,H)\end{array}}
(185)
P(pid)=(tk,s′,o)tk→atk′a=spawn(tk0)⇓succeed pid′pid′∉Dom PP′=P+{pid↦(tk′,s,o), pid′↦(tk0,s,spawned)}(s,v,M,P,sb,Q,H)→a(s,v,M,P′,sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\tk,s',\pgorig)\premsep\tk\effred{\effa}\tk'\premsep \effa=\effdone{\effspawn(\tk_0)}{\effsucceed\,\pid'}\\ \pid'\notin\Dom P\premsep P'=P+\{\pid\mapsto(\tk',s,\pgorig),\ \pid'\mapsto(\tk_0,s,\pgspawned)\}\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\effa}(s,v,M,P',\sbv,Q,H)\end{array}}
(186)
P(pid)=(tk,s′,o)tk→atk′a=kill(pid′)⇓tk′′P′=(P+{pid′↦ended})+{pid↦(tk′,s,o)}(s,v,M,P,sb,Q,H)→a(s,v,M,P′,sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\tk,s',\pgorig)\premsep\tk\effred{\effa}\tk'\premsep \effa=\effdone{\effkill(\pid')}{\tk''}\\ P'=(P+\{\pid'\mapsto\pgended\})+\{\pid\mapsto(\tk',s,\pgorig)\}\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\effa}(s,v,M,P',\sbv,Q,H)\end{array}}
(187)
P(pid)=(succeed v′,s′,c)⊢c◃v′⇒v′′(s,v,M,P,sb,Q,H)→(s,v,M⋅v′′,P+{pid↦ended},sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\effsucceed\,v',s',\effc)\premsep\effrecv{\effc}{v'}{v''}\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{}(s,v,M\cdot v'',P+\{\pid\mapsto\pgended\},\sbv,Q,H)\end{array}}
(188)
P(pid)=(tk,s′,o)tk=fail v′ or (tk=succeed v′ and o=spawned)(s,v,M,P,sb,Q,H)→(s,v,M,P+{pid↦ended},sb,Q,H)\frac{\begin{array}{c}P(\pid)=(\tk,s',\pgorig)\\ \tk=\efffail\,v'\ \text{or}\ (\tk=\effsucceed\,v'\ \text{and}\ \pgorig=\pgspawned)\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{}(s,v,M,P+\{\pid\mapsto\pgended\},\sbv,Q,H)\end{array}}
(189)
PE(p)=(in,τ)Decτ(j)=v′Flat(sb)={ ⁣∣ c1,…,cn ∣ ⁣}for 1≤i≤n:  ⊢ci◃(p,j)⇒Mi,or Mi=nil and ⊢ci◃(p,j)⇒v′′ for no v′′(s,v,M,P,sb,Q,H)→in(p,j)(s,v,M⋅M1⋯Mn,P,sb,Q,H)\frac{\begin{array}{c}\PE(\effp)=(\smdirin,\tau)\premsep\Dec{\tau}(j)=v'\premsep \effFlat(\sbv)=\effmset{\effc_1,\ldots,\effc_n}\\ \text{for}\ 1\leq i\leq n:\ \ \effrecv{\effc_i}{(\effp,j)}{M_i},\\ \text{or}\ M_i=\pgnil\ \text{and}\ \effrecv{\effc_i}{(\effp,j)}{v''}\ \text{for no}\ v''\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\pgEvIn(\effp,j)} (s,v,M\cdot M_1\cdots M_n,P,\sbv,Q,H)\end{array}}
(190)
PE(p)=(in,τ)Decτ(j) is undefinedK→in(p,j)K\frac{\begin{array}{c}\PE(\effp)=(\smdirin,\tau)\premsep\Dec{\tau}(j)\ \text{is undefined}\end{array}}{\begin{array}{c}K\step{\pgEvIn(\effp,j)}K\end{array}}
(191)
Q(p)=j⋅J(s,v,M,P,sb,Q,H)→out(p,j)(s,v,M,P,sb,Q+{p↦J},H)\frac{\begin{array}{c}Q(\effp)=j\cdot J\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\pgEvOut(\effp,j)}(s,v,M,P,\sbv,Q+\{\effp\mapsto J\},H)\end{array}}
(192)
d>0(s,v,M,P,sb,Q,H)→wait d(s+d,v,M,P,sb,Q,H)\frac{\begin{array}{c}d>0\end{array}}{\begin{array}{c}(s,v,M,P,\sbv,Q,H)\step{\pgwait\,d}(s+d,v,M,P,\sbv,Q,H)\end{array}}
(193)
K→⟨ℓ⟩platK′K→⟨ℓ⟩K′\frac{\begin{array}{c}K\pgplatstep{\opt{\ell}}K'\end{array}}{\begin{array}{c}K\step{\opt{\ell}}K'\end{array}}
(194)
Comments:
(183)

This is the only rule that takes a message from the queue or changes the model. The message at the head of the queue is applied, with the model, to update; the commands of the result are dispatched, and the subscriptions recomputed from the new model, whether or not it has changed, all in the one step. If the application of update has no result (it reaches Debug.todo), the rule never applies again, and no further message is handled, though processes, ports and the platform go on taking steps.

(184)

A process takes one reduction of its task at a time, and the steps of different processes, and every other transition, may come between any two of them. A process may thus be suspended at any andThen\effandThen; a program may rely on nothing more. Rules (184) to (187) record the present time as the time of the process’s latest step. An action of a primitive of the platform acts on the world as the platform defines.

(185)

The process reached prim(sleep,v′)\effprim(\effsleep,v') by its step at time s′s', so the action takes place no sooner than max⁡(0,v′)\max(0,v') milliseconds later; for an infinite or NaN delay, no condition is imposed. The definition sets no time by which the action must take place.

(186)

The new process names a process never started before, and its origin is spawned\pgspawned: its outcome goes to no other process, and it gives no message (rule (189)).

(187)

The killed process takes no further step, and if it was performing the task of a leaf it gives no message. Killing a process that has ended changes nothing. When a process kills itself, the second modification overrides the first, so it is not ended and goes on with tk′\tk'.

(188)

A process whose origin is a leaf command ends, when its task succeeds, by adding the message of its leaf (rule (169)) to the end of the queue. When the functions of the map\effmaps around the leaf give no result, the process never ends.

(189)

A process whose task fails ends, whatever its origin, and nothing else happens: there is no error, and no message.

(190)

An incoming value is an event of its own, so it is never handled in the middle of another step; its messages join the end of the queue, and are handled by later updates. Each leaf subscription on p\effp gives one message, through the functions of the map\effmaps around it; a leaf on another port, or of another manager, gives none, and so does a leaf whose functions give no result. The order of the messages of one value follows the enumeration of Flat(sb)\effFlat(\sbv), and is unspecified. A value accepted while no subscription is current gives no message.

(191)

A boundary value that does not decode at the type of the port is rejected: the program never sees it, and is unchanged. Thus with port recv : (String -> msg) -> Sub msg, the value 1337 is rejected. For every incoming port and every boundary value one of rules (190) and (191) applies, so the host may send at any time. A name that is not an incoming port of the program has no transition: sending on it is outside the definition.

(192)

The values of each port are delivered in the order in which they joined its queue, so a value sent by one update is delivered before a value that a later update sends on the same port. Delivery happens after the step that queued the value, at a time the definition does not fix, and in no fixed order across ports. The event records only that the program has delivered jj; whether the host has a subscriber, and what it does with jj, are outside the definition.

(193)

Time passes only in this rule, and every other transition takes no time. Since this rule applies to every configuration, the definition requires no step to happen by any time: only the lower bound of rule (185), and what the platform imposes on its own steps, relate the steps of a program to time.

(194)

The managers of the platform, such as those of Time or Http, act through these steps, within the limits stated above. A platform with no managers has no platform steps.

The rules give the following guarantees, and no others.

Messages are handled one at a time, by rule (183), in the order in which they joined the message queue. Each update applies update to the model that the previous update produced, or to the model of init for the first. The commands of an update are dispatched in the step of that update, so the effects of an earlier update are dispatched before those of a later one. Apart from these, and from the order of the values of each outgoing port, the steps of distinct processes, the arrival of incoming values and platform steps, the delivery of outgoing values, and the update steps interleave in every way the rules allow.

9.5Observable Behaviour

Let B0⊢program⇒(τ,PE,prog r)\jdg{B_0}{\program}{(\tau,\PE,\effprog\,r)} with τ=(τ1,τ2,τ3)Program\tau=(\tau_1,\tau_2,\tau_3)\sbtProgram, and let jj be a boundary value. An execution of program\program with flags jj is a finite sequence of transitions

K0→⟨ℓ1⟩K1→⟨ℓ2⟩⋯→⟨ℓn⟩Kn(n≥0), K_0\step{\opt{\ell_1}}K_1\step{\opt{\ell_2}}\cdots\step{\opt{\ell_n}}K_n \qquad(n\geq 0),

where PE,τ1,j⊢prog r⇒K0\jdg{\PE,\tau_1,j}{\effprog\,r}{K_0}. The observable events are those of the forms in(p,j′)\pgEvIn(\effp,j'), out(p,j′)\pgEvOut(\effp,j') and wait d\pgwait\,d, the platform events, and the actions π(v1⋯vk)⇓tk\effdone{\pi(v_1\cdots v_k)}{\tk} with π∈Primplat\pi\in\effPrimPlat. The trace of an execution is the sequence of its observable events, in order; silent transitions and the actions of sleep\effsleep, spawn\effspawn and kill\effkill, whose whole effect lies within the configuration, are omitted. The set of traces

Tr(program,j) \pgTr(\program,j)

is the set of the traces of the executions of program\program with flags jj, and the meaning of program\program is the function that maps each jj to Tr(program,j)\pgTr(\program,j). When Tr(program,j)\pgTr(\program,j) is empty, the program does not start with flags jj; otherwise it holds the empty trace and every prefix of each of its traces.

An implementation conforms to the definition if, for every program that has a meaning and every boundary value jj, every trace that the implementation can produce when it runs the program with flags jj is a member of Tr(program,j)\pgTr(\program,j), and the implementation does not start the program when Tr(program,j)\pgTr(\program,j) is empty. Two conforming implementations may produce different traces of one program, as where the order of the commands of a batch, or the interleaving of processes, is left open, and both conform. Conformance concerns what is produced, not that anything is: the definition does not require a program to make progress.

9.6Views and Other Programs

Let B0⊢program⇒(τ′ Node,PE,v)\jdg{B_0}{\program}{(\tau'\,\sbtNode,\PE,v)}. The program is a view: its meaning is the value vv of type Html, which the host renders. It has no configuration and no transitions: it handles no message, and has no traffic on its ports, whatever ports its modules declare. How the host renders vv is outside the definition; an implementation that only evaluates the program conforms. Every top-level value of every module is evaluated, so a Debug.log in the definition of main is evaluated once.

A platform may provide functions other than Platform.worker that make program values, such as the functions sandbox, element, document and application of the module Browser. They are not part of the initial basis, and their records have further fields, such as view (Section 8.5). A program whose main is made by one of them runs as in Section 9.4, except that the platform defines how init is applied (for application, to the flags, a URL and a navigation key), and that among its platform steps are steps that render the value of view applied to the present model, as a platform event, after each update, and steps that append to the message queue the messages that the host sends from the rendered view. A platform that does not provide these functions, or does not run the programs they make, is recorded in Appendix E.

Discussions

Discuss this chapter·Search the discussions on GitHub

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