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

8Tasks, Commands, Subscriptions and Ports

This chapter defines the effect values of Standard Elm: the tasks, which describe work that may wait, fail and start other processes; the commands and subscriptions, which a program hands to the platform to have tasks performed and values exchanged with its host (the platform and the host are as in Chapter 1); and the program values. It defines the boundary values that cross a port, which are JSON values in which byte sequences may stand, and the encoding and decoding of the values of every type that a port admits, which settles exactly what can and cannot be sent over a port. Its sentences infer that a task reduces to another task, silently or by performing a primitive action; that a value encodes as a boundary value, or a boundary value decodes as a value, at a type; and that a leaf of a command or subscription, receiving an input, gives a message. These objects are defined here; the running of a program, which creates and schedules processes and handles their messages one at a time, is defined by Chapter 9 in terms of them. Every phrase whose value is used here is assumed to have elaborated (Chapter 5).

8.1Simple Objects

The simple objects of this chapter are shown in Figure 22; the classes of Chapter 6 are used as there.

pid\pid∈\inProcId\ProcIdprocess identifiers
π\pi∈\inPrim\effPrimprimitives
p or var\effp\ \text{or}\ \var∈\inVar\cls{Var}port names
∈\inManagerplat\effMgrPlatmanagers of the platform
∈\inJNum\effJNumJSON numbers
∈\inByt\effBytbyte sequences
Figure 22: Simple Semantic Objects of the Effects

A process identifier names a process (Section 8.4); ProcId is an infinite set. The class Prim of primitives is

Prim={sleep,spawn,kill}∪Primplat, \effPrim=\{\effsleep,\effspawn,\effkill\}\cup\effPrimPlat ,

where sleep\effsleep, spawn\effspawn and kill\effkill are the primitives of the module Process, and Primplat\effPrimPlat is a set of further primitives (reading a clock, an HTTP request, and so on) which is a parameter of the platform on which a program runs. Each primitive π\pi has an arity arity(π)≥1\arity(\pi)\geq 1; the arity of each of sleep\effsleep, spawn\effspawn and kill\effkill is 1. A port name p\effp is the variable of a port declaration (Chapter 3). Port names are global to a program and distinct from one another (Section 3.7). Managerplat\effMgrPlat, a parameter of the platform, is a set of names of managers, other than the managers of tasks and ports defined in Section 8.5, and disjoint from the port names. JNum\effJNum is the set of JSON numbers: the rational numbers m×10em\times 10^{e} with mm and ee integers, that is, the numbers a JSON text can spell. Every integer of IntRange\IntRange and every finite floating-point number is a member of JNum\effJNum; we omit the injections from these numbers into JNum\effJNum. The injection takes both floating-point zeros, 00 and −0-0, to the JSON number 00: a JSON number has no sign apart from its value. We write Str\effStr for the set of strings of SVal, Flt\effFlt for the set of floating-point numbers of SVal, and Fltfin⊆Flt\effFltFin\subseteq\effFlt for its finite members, so that Flt∖Fltfin\effFlt\setminus\effFltFin holds the two infinities and NaN. For j∈JNumj\in\effJNum, float(j)\efffloat(j) is the floating-point number nearest jj, rounded as the value of a floating-point constant is (Section 6.3); float(0)\efffloat(0) is the float 00, not −0-0. Byt\effByt is the set of byte sequences: the finite sequences of integers from 00 to 255255. We write bytes(o1⋯ok)\effbytes(o_1\cdots o_k), with k≥0k\geq 0 and 0≤oi≤2550\leq o_i\leq 255 for each ii, for the byte sequence of o1,…,oko_1,\ldots,o_k in order. A byte sequence is not a JSON number, a string or an array: Byt\effByt is disjoint from JNum\effJNum, from Str\effStr and from every other class of this chapter.

8.2Compound Objects

The compound objects of this chapter are shown in Figure 23. The classes EffVal and JVal, which Chapter 6 includes in Val (Figure 20), are defined here. TaskVal, CmdVal, SubVal and JVal are defined together with the classes of Figure 20; all are the smallest classes closed under the constructions of both figures. The class BVal of boundary values, which includes JVal, is defined here too; its other members are not in Val.

ev\ev∈\inEffVal\EffVal=TaskVal∪CmdVal∪SubVal∪ProcId∪ProgVal= \TaskVal\cup\CmdVal\cup\SubVal\cup\ProcId\cup\ProgVal
tk\tk∈\inTaskVal\TaskVal=({succeed,fail}×Val)= (\{\effsucceed,\efffail\}\times\Val)
=∪({andThen,onError}×Val×TaskVal)\phantom{{}={}}\cup(\{\effandThen,\effonError\}\times\Val\times\TaskVal)
=∪⋃k≥1({prim}×Prim×Valk)\phantom{{}={}}\cup\textstyle\bigcup_{k\geq 1}(\{\effprim\}\times\effPrim\times\Val^k)
cm\cm∈\inCmdVal\CmdVal=⋃n≥0({batch}×CmdValn)= \textstyle\bigcup_{n\geq 0}(\{\effbatch\}\times\CmdVal^n)
=∪({map}×Val×CmdVal)\phantom{{}={}}\cup(\{\effmap\}\times\Val\times\CmdVal)
=∪({leaf}×Manager×(Val∪BVal))\phantom{{}={}}\cup(\{\effleaf\}\times\effMgr\times(\Val\cup\BVal))
sb\sbv∈\inSubVal\SubVal=⋃n≥0({batch}×SubValn)= \textstyle\bigcup_{n\geq 0}(\{\effbatch\}\times\SubVal^n)
=∪({map}×Val×SubVal)\phantom{{}={}}\cup(\{\effmap\}\times\Val\times\SubVal)
=∪({leaf}×Manager×Val)\phantom{{}={}}\cup(\{\effleaf\}\times\effMgr\times\Val)
m\effm∈\inManager\effMgr={task}∪({out}×Var)= \{\effmtask\}\cup(\{\effmout\}\times\cls{Var})
=∪({in}×Var×Type)∪Managerplat\phantom{{}={}}\cup(\{\effmin\}\times\cls{Var}\times\Type)\cup\effMgrPlat
a\effa∈\inAction\effAction=⋃k≥1Prim×Valk×TaskVal= \textstyle\bigcup_{k\geq 1}\effPrim\times\Val^k\times\TaskVal
pv\pv∈\inProgVal\ProgVal={prog}×Record= \{\effprog\}\times\Record
jj∈\inBVal\BVal={null,true,false}∪JNum∪Str∪Byt= \{\effnull,\efftrue,\efffalse\}\cup\effJNum\cup\effStr\cup\effByt
=∪⋃n≥0BValn∪(Str→finBVal)\phantom{{}={}}\cup\textstyle\bigcup_{n\geq 0}\BVal^n\cup(\effStr\finmap\BVal)
∈\inJVal\JVal={null,true,false}∪JNum∪Str= \{\effnull,\efftrue,\efffalse\}\cup\effJNum\cup\effStr
=∪⋃n≥0JValn∪(Str→finJVal)\phantom{{}={}}\cup\textstyle\bigcup_{n\geq 0}\JVal^n\cup(\effStr\finmap\JVal)
Figure 23: Compound Semantic Objects of the Effects

We write the members of these classes as follows:

tasks:succeed v,fail v,andThen(v,tk),onError(v,tk),prim(π,v1⋯vk);commands:batch(cm1⋯cmn),map(v,cm),leaf(m,v);managers:task,out p,in(p,τ);actions:π(v1⋯vk)⇓tk;program values:prog r. \begin{array}{ll} \text{tasks:} & \effsucceed\,v,\quad \efffail\,v,\quad \effandThen(v,\tk),\\ & \effonError(v,\tk),\quad \effprim(\pi,v_1\cdots v_k);\\ \text{commands:} & \effbatch(\cm_1\cdots\cm_n),\quad \effmap(v,\cm),\quad \effleaf(\effm,v);\\ \text{managers:} & \effmtask,\quad \effmout\,\effp,\quad \effmin(\effp,\tau);\\ \text{actions:} & \effdone{\pi(v_1\cdots v_k)}{\tk};\\ \text{program values:} & \effprog\,r. \end{array}

Subscriptions are written as commands are, and an action π(v1⋯vk)⇓tk\effdone{\pi(v_1\cdots v_k)}{\tk} is the triple (π,v1⋯vk,tk)(\pi,v_1\cdots v_k,\tk). We write none\effnone for batch( )\effbatch(\,), the batch with no components, in CmdVal and in SubVal. In a primitive task prim(π,v1⋯vk)\effprim(\pi,v_1\cdots v_k), always k=arity(π)k=\arity(\pi).

A boundary value is null\effnull, true\efftrue, false\efffalse, a number, a string, a byte sequence, an array, which is a member [j1,…,jn]\effJarr{j_1,\ldots,j_n} of BValn\BVal^n, or an object, which is a finite map from strings to boundary values. An object has no order on its keys. A boundary value is thus a JSON value in which a byte sequence may stand wherever a value may. A JSON value is a boundary value in which no byte sequence occurs; JVal is the class of JSON values, and JVal⊆BVal\JVal\subseteq\BVal. A byte sequence is not a JSON value. The metavariable jj ranges over BVal; where only a JSON value is meant we write j∈JValj\in\JVal. The values of the type Json.Encode.Value (also called Json.Decode.Value) are exactly the JSON values, the members of JVal, so no byte sequence occurs in one. As with the other classes, JVal is disjoint from the rest of Val, and we omit the injection: a JSON string and the String value with the same characters are different values, and null\effnull is not the unit value ()(). A boundary value that is not a JSON value is not a value of the language; such a boundary value occurs only at a port, and in the leaves of outgoing ports, which CmdVal admits.

For values v1,…,vnv_1,\ldots,v_n we write [v1,…,vn][v_1,\ldots,v_n] for the list value v1::⋯::vn::[]v_1\mathbin{\kw{::}}\cdots\mathbin{\kw{::}}v_n\mathbin{\kw{::}}\kw{[]}, and [ ][\,] when n=0n=0. We write the value to which the initial dynamic basis (Appendix D) binds a long identifier of the basis by that identifier in typewriter, as in Task.succeed; the basic values Array.fromList and Array.toList of Appendix D (Section D.4.9), which no identifier of the basis spells, are written likewise. Finite multisets are written { ⁣∣ c1,…,cn ∣ ⁣}\effmset{\effc_1,\ldots,\effc_n}, and ⊎\uplus is the union of multisets, which adds multiplicities.

8.3Inference Rules

The semantic rules of this chapter allow sentences of the forms

tk→⟨a⟩tk′,Encτ(v)=j,Decτ(j)=v,⊢c◃x⇒v \tk\effred{\opt{\effa}}\tk',\qquad \Enc{\tau}(v)=j,\qquad \Dec{\tau}(j)=v,\qquad \effrecv{\effc}{\effx}{v}

to be inferred. The first is pronounced “tk\tk reduces to tk′\tk'”, and, when the label a\effa is present, “performing a\effa”; a reduction without a label is silent. The second is pronounced “vv encodes at τ\tau as jj”, and the third “jj decodes at τ\tau as vv”; there τ\tau is a type with Port(τ)\Port(\tau) (Section 5.4). In the fourth, c\effc is a command or subscription value with no batch\effbatch in it (a leaf command, Section 8.5), the input x\effx is a value or a pair (p,j)(\effp,j) of a port name and a boundary value, and the sentence is pronounced “c\effc, receiving x\effx, gives the message vv”. Premises also use the sentence ⊢v1 v2⇒v\japp{v_1}{v_2}{v} of the application of values (Section 6.7). Some hypotheses in rules are not of these forms; they are side conditions.

The convention for options is the same as for the Core static semantics. In particular, in a task reduction whose label is written ⟨a⟩\opt{\effa}, the label is present on every arrow of the rule or absent from every one. As in the Core (Section 6.7), sentences have no states and there are no exception packets, so every rule is given in full. We allow the compound variable cm/sb\cm/\sbv to range over CmdVal∪SubVal\CmdVal\cup\SubVal.

Encτ\Enc{\tau} and Decτ\Dec{\tau} are defined by the rules alone: Encτ(v)\Enc{\tau}(v) is the boundary value jj for which Encτ(v)=j\Enc{\tau}(v)=j can be inferred, and is undefined when there is none, and likewise for Decτ(j)\Dec{\tau}(j) (Section 8.7).

8.4Tasks

A task value describes work; it does nothing until a process performs it, and a task value performed twice does its work twice. The task succeed v\effsucceed\,v has succeeded with vv, and fail v\efffail\,v has failed with vv; these two are the outcomes. The task andThen(v,tk)\effandThen(v,\tk) performs tk\tk and, if it succeeds with v′v', goes on with the task to which vv applied to v′v' evaluates; onError(v,tk)\effonError(v,\tk) is the same for failure. A primitive task prim(π,v1⋯vk)\effprim(\pi,v_1\cdots v_k) performs the primitive π\pi on v1⋯vkv_1\cdots v_k.

A process is a process identifier together with the task that it is reducing. A process reduces its task by the rules below, one step at a time, until the task is an outcome; then the process has ended. A silent step concerns that process alone. A labelled step performs a primitive action π(v1⋯vk)⇓tk\effdone{\pi(v_1\cdots v_k)}{\tk}, whose outcome tk\tk is a member of the set

Out(π,v1⋯vk) \effOut(\pi,v_1\cdots v_k)

of possible outcomes, and which may act on the world outside the process. For the primitives of Process,

Out(sleep,v)={succeed ()},Out(spawn,tk)={succeed pid∣pid∈ProcId},Out(kill,pid)={succeed ()}, \begin{array}{rcl} \effOut(\effsleep,v) &=& \{\effsucceed\,()\},\\ \effOut(\effspawn,\tk) &=& \{\effsucceed\,\pid \mid \pid\in\ProcId\},\\ \effOut(\effkill,\pid) &=& \{\effsucceed\,()\}, \end{array}

and their actions mean the following.

  • The action sleep(v)⇓succeed ()\effdone{\effsleep(v)}{\effsucceed\,()}, where v∈Fltv\in\effFlt, takes place no sooner than max⁡(0,v)\max(0,v) milliseconds after the process reached prim(sleep,v)\effprim(\effsleep,v), when v∈Fltfinv\in\effFltFin; when vv is an infinity or NaN, when it takes place is not defined.

  • spawn(tk)⇓succeed pid\effdone{\effspawn(\tk)}{\effsucceed\,\pid} starts a new process, named by pid\pid, reducing tk\tk. The identifier pid\pid names no process that has been started before. The new process is independent of the one that started it: its outcome is delivered to no other process, and when it fails it ends, and nothing else happens.

  • kill(pid)⇓succeed ()\effdone{\effkill(\pid)}{\effsucceed\,()} ends the process named by pid\pid at once, so that it takes no further step, when that process has not ended and is not the process performing the action. Otherwise the action has no effect.

For a primitive π∈Primplat\pi\in\effPrimPlat, the set Out(π,v1⋯vk)\effOut(\pi,v_1\cdots v_k), which may hold failures, and the meaning of its actions are given by the platform. Chapter 9 defines the processes of a running program, and it is there that the conditions above on when an action may take place, and on what it does to other processes, are enforced.

Task Reductiontk→⟨a⟩tk′\tk\effred{\opt{\effa}}\tk'
⊢v v′⇒tkandThen(v,succeed v′)→tk\frac{\begin{array}{c}\japp{v}{v'}{\tk}\end{array}}{\begin{array}{c}\effandThen(v,\effsucceed\,v')\effred{}\tk\end{array}}
(133)
andThen(v,fail v′)→fail v′\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\effandThen(v,\efffail\,v')\effred{}\efffail\,v'\end{array}}
(134)
⊢v v′⇒tkonError(v,fail v′)→tk\frac{\begin{array}{c}\japp{v}{v'}{\tk}\end{array}}{\begin{array}{c}\effonError(v,\efffail\,v')\effred{}\tk\end{array}}
(135)
onError(v,succeed v′)→succeed v′\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\effonError(v,\effsucceed\,v')\effred{}\effsucceed\,v'\end{array}}
(136)
tk→⟨a⟩tk′andThen(v,tk)→⟨a⟩andThen(v,tk′)\frac{\begin{array}{c}\tk\effred{\opt{\effa}}\tk'\end{array}}{\begin{array}{c}\effandThen(v,\tk)\effred{\opt{\effa}}\effandThen(v,\tk')\end{array}}
(137)
tk→⟨a⟩tk′onError(v,tk)→⟨a⟩onError(v,tk′)\frac{\begin{array}{c}\tk\effred{\opt{\effa}}\tk'\end{array}}{\begin{array}{c}\effonError(v,\tk)\effred{\opt{\effa}}\effonError(v,\tk')\end{array}}
(138)
tk∈Out(π,v1⋯vk)prim(π,v1⋯vk)→π(v1⋯vk)⇓tktk\frac{\begin{array}{c}\tk\in\effOut(\pi,v_1\cdots v_k)\end{array}}{\begin{array}{c}\effprim(\pi,v_1\cdots v_k)\effred{\effdone{\pi(v_1\cdots v_k)}{\tk}}\tk\end{array}}
(139)
Comments:
(133)

The task tk\tk to which vv applied to v′v' evaluates is not reduced by this step; it is the task the process performs next. If the application has no result (for instance, it reaches Debug.todo), the task has no reduction, and the process never ends.

(134)

The continuation vv is skipped: a failure passes over every andThen\effandThen until an onError\effonError handles it, as rule (135) does. Rule (136) is the dual for success.

(137)

Rules (137) and (138) carry both silent and labelled steps out of the task that is performed first. Every step of a process is a separate reduction, and another process may take steps between any two of them: a process may be suspended at any andThen\effandThen, and the definition guarantees no more. In particular, a program cannot rely on a process running without interruption until it reaches a primitive.

(139)

This is the only rule with a label, and it does not determine its result: the outcome is any member of Out(π,v1⋯vk)\effOut(\pi,v_1\cdots v_k). For spawn\effspawn, the process identifier is unconstrained here; Chapter 9 chooses one that names no earlier process. For the primitives of the platform, the outcome is decided by the platform, which may also take time to decide it, as for sleep\effsleep.

The following properties follow from the rules.

An outcome has no reduction. If tk\tk is not an outcome, then either every reduction of tk\tk is silent, and all have the same right-hand side up to the choice of a member of a set given by APPLY\APPLY; or every reduction of tk\tk is labelled by an action π(v1⋯vk)⇓tk′\effdone{\pi(v_1\cdots v_k)}{\tk'}, for one and the same π\pi and v1⋯vkv_1\cdots v_k; or tk\tk has no reduction, which happens only when an application in a premise of rule (133) or (135) has no result.

8.5Commands and Subscriptions

A command value, or a subscription value, is a finite tree whose leaves are requests to managers. A manager m\effm is one of the following.

  • The task manager task\effmtask. Its leaves leaf(task,tk)\effleaf(\effmtask,\tk) are commands; it performs tk\tk in a new process, and when tk\tk succeeds with vv, the leaf gives a message from vv.

  • The manager out p\effmout\,\effp of an outgoing port p\effp. Its leaves leaf(out p,j)\effleaf(\effmout\,\effp,j), with j∈BValj\in\BVal, are commands; each sends jj to the host, and gives no message.

  • The manager in(p,τ)\effmin(\effp,\tau) of an incoming port p\effp of payload type τ\tau. Its leaves leaf(in(p,τ),v)\effleaf(\effmin(\effp,\tau),v) are subscriptions, where vv is a function value; while the subscription is current, each boundary value the host sends on p\effp that decodes at τ\tau gives a message through vv (Section 8.8).

  • A manager of the platform, in Managerplat\effMgrPlat. The values its leaves hold, whether they are commands or subscriptions, and the messages they give are defined by the platform.

Since port names are distinct (rule (172)), a port name p\effp determines the payload type τ\tau of every manager in(p,τ)\effmin(\effp,\tau) of a program. A batch groups commands, and map(v,cm)\effmap(v,\cm) applies the function value vv to every message that cm\cm gives.

A command or subscription value with no batch\effbatch in it has the form map(vn,⋯map(v1,leaf(m,v))⋯ )\effmap(v_n,\cdots\effmap(v_1,\effleaf(\effm,v))\cdots) with n≥0n\geq 0, and is called a leaf command (or leaf subscription); its manager is m\effm. The flattening Flat(cm)\effFlat(\cm) of a command value cm\cm is the finite multiset of leaf commands defined by

Flat(batch(cm1⋯cmn))=Flat(cm1)⊎⋯⊎Flat(cmn),Flat(map(v,cm))={ ⁣∣ map(v,c)∣c∈Flat(cm) ∣ ⁣},Flat(leaf(m,v))={ ⁣∣ leaf(m,v) ∣ ⁣}, \begin{array}{rcl} \effFlat(\effbatch(\cm_1\cdots\cm_n)) &=& \effFlat(\cm_1)\uplus\cdots\uplus\effFlat(\cm_n),\\ \effFlat(\effmap(v,\cm)) &=& \effmset{\effmap(v,\effc)\mid \effc\in\effFlat(\cm)},\\ \effFlat(\effleaf(\effm,v)) &=& \effmset{\effleaf(\effm,v)}, \end{array}

where a leaf command that occurs several times in Flat(cm)\effFlat(\cm) gives as many members of the multiset on the right of the second equation. The flattening Flat(sb)\effFlat(\sbv) of a subscription value is defined by the same equations. In particular Flat(none)=Flat(batch(none none))={ ⁣∣   ∣ ⁣}\effFlat(\effnone)=\effFlat(\effbatch(\effnone\ \effnone))=\effmset{\,}.

We have adopted the following convention, the effect order convention. A command determines, through its flattening, which leaf commands it holds and how many times each, but no order among them. Wherever the leaf commands of commands are performed (Chapter 9), the leaf commands of one command for one manager are performed in no specified order, and the leaf commands for different managers are performed in no specified order relative to one another. The same holds for subscriptions. The orders that implementations choose are recorded in Appendix E.

A program value prog r\effprog\,r is the meaning of a Program: rr is the record of the functions that make it up, with at least the fields init, update and subscriptions. The function Platform.worker makes a program value from its record (Figure 24). A platform may provide further functions whose results are program values, whose records have further fields, such as view; what those fields mean is defined by the platform. Chapter 9 defines how a program value runs.

8.6The Effect Values of the Basis

Figure 24 gives the arity of each basic value of the initial basis that makes an effect value, and the result of APPLY\APPLY on it (Section 6.5). Each set has exactly one member. One of these basic values, Task.command, is bound to no identifier of the basis; it is applied only by the texts of Task.perform and Task.attempt, as described below (Appendix D).

basic value bbarity(b)\arity(b)APPLY(b,v1⋯vn)\APPLY(b,v_1\cdots v_n)
Task.succeed1APPLY(b,v)={succeed v}\APPLY(b,v)=\{\effsucceed\,v\}
Task.fail1APPLY(b,v)={fail v}\APPLY(b,v)=\{\efffail\,v\}
Task.andThen2APPLY(b,v,tk)={andThen(v,tk)}\APPLY(b,v,\tk)=\{\effandThen(v,\tk)\}
Task.onError2APPLY(b,v,tk)={onError(v,tk)}\APPLY(b,v,\tk)=\{\effonError(v,\tk)\}
Task.command1APPLY(b,Perform tk)={leaf(task,tk)}\APPLY(b,\kw{Perform}\ \tk)=\{\effleaf(\effmtask,\tk)\}
Process.sleep1APPLY(b,v)={prim(sleep,v)}\APPLY(b,v)=\{\effprim(\effsleep,v)\}
Process.spawn1APPLY(b,tk)={prim(spawn,tk)}\APPLY(b,\tk)=\{\effprim(\effspawn,\tk)\}
Process.kill1APPLY(b,pid)={prim(kill,pid)}\APPLY(b,\pid)=\{\effprim(\effkill,\pid)\}
Cmd.batch1APPLY(b,[cm1,…,cmn])={batch(cm1⋯cmn)}\APPLY(b,[\cm_1,\ldots,\cm_n])=\{\effbatch(\cm_1\cdots\cm_n)\}
Cmd.map2APPLY(b,v,cm)={map(v,cm)}\APPLY(b,v,\cm)=\{\effmap(v,\cm)\}
Sub.batch1APPLY(b,[sb1,…,sbn])={batch(sb1⋯sbn)}\APPLY(b,[\sbv_1,\ldots,\sbv_n])=\{\effbatch(\sbv_1\cdots\sbv_n)\}
Sub.map2APPLY(b,v,sb)={map(v,sb)}\APPLY(b,v,\sbv)=\{\effmap(v,\sbv)\}
Platform.worker1APPLY(b,r)={prog r}\APPLY(b,r)=\{\effprog\,r\}
Figure 24: APPLY on Effect Values

The other functions of these modules that the initial basis provides are written in Elm in elm/core, and each is defined by its text (Section D.3). They are Task.map, Task.map2 to Task.map5, Task.sequence, Task.mapError, Task.perform, Task.attempt, Cmd.none and Sub.none. Thus Cmd.none and Sub.none are none\effnone, the value of batch []. The texts of Task.perform and Task.attempt apply command to a constructor value Perform tk\kw{Perform}\ \tk, where Perform is the constructor of the type MyCmd of the module Task, which that module does not expose. Effect modules are outside Standard Elm, so these two names are given directly: the texts of Task are evaluated in an environment that binds Perform to the constructor Perform, of arity 1, and command to the basic value Task.command of the figure, and neither is bound to an identifier of the basis, as Appendix D states (Section D.3). Hence Task.perform vv tk\tk evaluates to leaf(task,tk′)\effleaf(\effmtask,\tk'), where tk′\tk' is the value of Task.map vv tk\tk, a task that cannot fail and whose success value is the message. Commands of outgoing ports and subscriptions of incoming ports are made by the values of the ports (Section 8.7).

8.7Ports

A value crosses a port as a boundary value: the port boundary is defined over the boundary values of BVal, which are the JSON values of JVal together with those in which byte sequences occur, and host values that are not boundary values are outside the definition. How a host represents a byte sequence is outside the definition as well (Appendix E). A value vv sent on an outgoing port of payload type τ\tau becomes the boundary value Encτ(v)\Enc{\tau}(v), and a boundary value jj arriving at an incoming port of payload type τ\tau becomes the value Decτ(j)\Dec{\tau}(j), if that is defined. The payload types are the types τ\tau with Port(τ)\Port(\tau) (Section 5.4); aliases have been expanded, and a payload type has no type variables. Labels are identified with the strings of their characters, so that a record type’s labels are the keys of the objects that encode its records. The values of the type Bytes are the constructor values binary [o1,…,ok]\dbbin\,[o_1,\ldots,o_k] of Appendix D (Section D.4.10), each oio_i an Int value from 00 to 255255.

EncodingEncτ(v)=j\Enc{\tau}(v)=j
EncInt(sv)=sv\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tInt}(\sv)=\sv\end{array}}
(140)
sv∈FltfinEncFloat(sv)=sv\frac{\begin{array}{c}\sv\in\effFltFin\end{array}}{\begin{array}{c}\Enc{\tFloat}(\sv)=\sv\end{array}}
(141)
sv∈Flt∖FltfinEncFloat(sv)=null\frac{\begin{array}{c}\sv\in\effFlt\setminus\effFltFin\end{array}}{\begin{array}{c}\Enc{\tFloat}(\sv)=\effnull\end{array}}
(142)
EncBool(True)=true\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tBool}(\kw{True})=\efftrue\end{array}}
(143)
EncBool(False)=false\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tBool}(\kw{False})=\efffalse\end{array}}
(144)
EncString(sv)=sv\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tString}(\sv)=\sv\end{array}}
(145)
EncBytes(binary [o1,…,ok])=bytes(o1⋯ok)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tBytes}(\dbbin\,[o_1,\ldots,o_k])=\effbytes(o_1\cdots o_k)\end{array}}
(146)
Enc()(())=null\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tunit}(())=\effnull\end{array}}
(147)
Encτ Maybe(Nothing)=null\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Enc{\tau\,\tMaybe}(\kw{Nothing})=\effnull\end{array}}
(148)
Encτ(v)=jEncτ Maybe(Just v)=j\frac{\begin{array}{c}\Enc{\tau}(v)=j\end{array}}{\begin{array}{c}\Enc{\tau\,\tMaybe}(\kw{Just}\ v)=j\end{array}}
(149)
Encτ(v1)=j1  ⋯  Encτ(vn)=jnEncτ List([v1,…,vn])=[j1,…,jn]\frac{\begin{array}{c}\Enc{\tau}(v_1)=j_1\ \ \cdots\ \ \Enc{\tau}(v_n)=j_n\end{array}}{\begin{array}{c}\Enc{\tau\,\tList}([v_1,\ldots,v_n])=\effJarr{j_1,\ldots,j_n}\end{array}}
(150)
⊢Array.toList v⇒v′Encτ List(v′)=jEncτ Array(v)=j\frac{\begin{array}{c}\japp{\kw{Array.toList}}{v}{v'}\premsep\Enc{\tau\,\tList}(v')=j\end{array}}{\begin{array}{c}\Enc{\tau\,\tArray}(v)=j\end{array}}
(151)
Encτ1(v1)=j1Encτ2(v2)=j2⟨Encτ3(v3)=j3⟩k=2⟨+1⟩Enc(τ1,τ2⟨,τ3⟩) Tuplek((v1,v2⟨,v3⟩))=[j1,j2⟨,j3⟩]\frac{\begin{array}{c}\Enc{\tau_1}(v_1)=j_1\premsep\Enc{\tau_2}(v_2)=j_2\premsep \opt{\Enc{\tau_3}(v_3)=j_3}\premsep k=2\opt{{}+1}\end{array}}{\begin{array}{c}\Enc{(\tau_1,\tau_2\opt{,\tau_3})\,\tTuple{k}}((v_1,v_2\opt{,v_3})) =\effJarr{j_1,j_2\opt{,j_3}}\end{array}}
(152)
Dom r=Dom ϱj∈Str→finBValDom j=Dom ϱEncϱ(lab)(r(lab))=j(lab) for each lab∈Dom ϱEnc{ϱ}(r)=j\frac{\begin{array}{c}\Dom r=\Dom\varrho\premsep j\in\effStr\finmap\BVal\premsep\Dom j=\Dom\varrho\\ \Enc{\varrho(\lab)}(r(\lab))=j(\lab)\ \text{for each}\ \lab\in\Dom\varrho\end{array}}{\begin{array}{c}\Enc{\rec{\varrho}}(r)=j\end{array}}
(153)
j∈JValEncValue(j)=j\frac{\begin{array}{c}j\in\JVal\end{array}}{\begin{array}{c}\Enc{\tValue}(j)=j\end{array}}
(154)
Comments:
(140)

The integer itself is the JSON number. Whether a host can hold every integer of IntRange\IntRange exactly is not a matter for the definition (Appendix E).

(141)

The sign of a zero is not kept: −0-0 encodes as the JSON number 00 (Section 8.1).

(142)

JSON has no number for NaN or for an infinity. Such a float is encoded as null\effnull, which a Float port does not decode, while a Maybe Float port decodes it as Nothing.

(146)

A value of type Bytes is sent as its byte sequence, which is not a JSON value: not a string, and not an array of numbers. Here each oio_i on the left is an Int value and on the right the same integer.

(149)

A Just has no encoding of its own. Hence the encoding of a Maybe value cannot tell Nothing from a Just whose argument encodes as null\effnull: in Maybe (Maybe Int), both Nothing and Just Nothing encode as null\effnull, and so do Nothing and Just () in Maybe ().

(151)

An array is encoded as the list of its elements. Array.toList is the basic value of Section D.4.9, bound to no identifier of the basis.

(153)

The fields become the keys of an object, which has no order, so the order of fields is no part of what is sent.

(154)

A value of type Json.Encode.Value is already a JSON value and is sent as it is. The side condition j∈JValj\in\JVal holds of every value of that type, so no byte sequence is ever sent through Json.Encode.Value.

DecodingDecτ(j)=v\Dec{\tau}(j)=v
j∈IntRangeDecInt(j)=j\frac{\begin{array}{c}j\in\IntRange\end{array}}{\begin{array}{c}\Dec{\tInt}(j)=j\end{array}}
(155)
j∈JNumDecFloat(j)=float(j)\frac{\begin{array}{c}j\in\effJNum\end{array}}{\begin{array}{c}\Dec{\tFloat}(j)=\efffloat(j)\end{array}}
(156)
DecBool(true)=True\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Dec{\tBool}(\efftrue)=\kw{True}\end{array}}
(157)
DecBool(false)=False\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Dec{\tBool}(\efffalse)=\kw{False}\end{array}}
(158)
j∈StrDecString(j)=j\frac{\begin{array}{c}j\in\effStr\end{array}}{\begin{array}{c}\Dec{\tString}(j)=j\end{array}}
(159)
DecBytes(bytes(o1⋯ok))=binary [o1,…,ok]\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Dec{\tBytes}(\effbytes(o_1\cdots o_k))=\dbbin\,[o_1,\ldots,o_k]\end{array}}
(160)
Dec()(null)=()\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Dec{\tunit}(\effnull)=()\end{array}}
(161)
Decτ Maybe(null)=Nothing\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\Dec{\tau\,\tMaybe}(\effnull)=\kw{Nothing}\end{array}}
(162)
j≠nullDecτ(j)=vDecτ Maybe(j)=Just v\frac{\begin{array}{c}j\neq\effnull\premsep\Dec{\tau}(j)=v\end{array}}{\begin{array}{c}\Dec{\tau\,\tMaybe}(j)=\kw{Just}\ v\end{array}}
(163)
Decτ(j1)=v1  ⋯  Decτ(jn)=vnDecτ List([j1,…,jn])=[v1,…,vn]\frac{\begin{array}{c}\Dec{\tau}(j_1)=v_1\ \ \cdots\ \ \Dec{\tau}(j_n)=v_n\end{array}}{\begin{array}{c}\Dec{\tau\,\tList}(\effJarr{j_1,\ldots,j_n})=[v_1,\ldots,v_n]\end{array}}
(164)
Decτ List(j)=v⊢Array.fromList v⇒v′Decτ Array(j)=v′\frac{\begin{array}{c}\Dec{\tau\,\tList}(j)=v\premsep\japp{\kw{Array.fromList}}{v}{v'}\end{array}}{\begin{array}{c}\Dec{\tau\,\tArray}(j)=v'\end{array}}
(165)
n≥kk=2⟨+1⟩Decτ1(j1)=v1Decτ2(j2)=v2⟨Decτ3(j3)=v3⟩Dec(τ1,τ2⟨,τ3⟩) Tuplek([j1,…,jn])=(v1,v2⟨,v3⟩)\frac{\begin{array}{c}n\geq k\premsep k=2\opt{{}+1}\\ \Dec{\tau_1}(j_1)=v_1\premsep\Dec{\tau_2}(j_2)=v_2\premsep \opt{\Dec{\tau_3}(j_3)=v_3}\end{array}}{\begin{array}{c}\Dec{(\tau_1,\tau_2\opt{,\tau_3})\,\tTuple{k}}(\effJarr{j_1,\ldots,j_n}) =(v_1,v_2\opt{,v_3})\end{array}}
(166)
j∈Str→finBValDom ϱ⊆Dom jDom r=Dom ϱDecϱ(lab)(j(lab))=r(lab) for each lab∈Dom ϱDec{ϱ}(j)=r\frac{\begin{array}{c}j\in\effStr\finmap\BVal\premsep\Dom\varrho\subseteq\Dom j\premsep \Dom r=\Dom\varrho\\ \Dec{\varrho(\lab)}(j(\lab))=r(\lab)\ \text{for each}\ \lab\in\Dom\varrho\end{array}}{\begin{array}{c}\Dec{\rec{\varrho}}(j)=r\end{array}}
(167)
j∈JValDecValue(j)=j\frac{\begin{array}{c}j\in\JVal\end{array}}{\begin{array}{c}\Dec{\tValue}(j)=j\end{array}}
(168)
Comments:
(155)

A JSON number is an Int exactly when it is an integer within IntRange\IntRange: 1.0 and -0 decode as 1 and 0, while 1.5, and an integer outside IntRange\IntRange, do not decode. Since IntRange\IntRange is a parameter of the implementation, so is the set of numbers a port of type Int accepts (Appendix E).

(156)

Every JSON number decodes, integers among them, as the nearest floating-point number. A number too large for a finite floating-point number decodes as the infinity of its sign, by the rounding of floating-point constants. An incoming −0-0 is the number 00, and decodes as the float 00, not −0-0. No boundary value decodes as NaN.

(160)

A byte sequence, and nothing else, decodes at Bytes: DecBytes(j)\Dec{\tBytes}(j) is undefined for every JSON value jj, so a string or an array of numbers sent to a Bytes port is rejected at the boundary.

(161)

At an incoming port, the unit type accepts null\effnull and nothing else. The flags of a program are decoded otherwise when their type is the unit type (Chapter 9).

(163)

The side condition j≠nullj\neq\effnull makes null\effnull always decode as Nothing, at once, by rule (162): in Maybe (Maybe Int) it decodes as the outer Nothing, and Just Nothing is never the result of decoding. A value other than null\effnull that does not decode at τ\tau does not decode at Maybe τ\tau either.

(166)

An array longer than the tuple is accepted, and its further elements are ignored; a shorter array does not decode.

(167)

An object with further keys is accepted, and they are ignored; an object that lacks the key of a field does not decode. Which missing field an implementation reports, and how, is outside the definition.

(168)

Every JSON value decodes at Json.Encode.Value. A boundary value in which a byte sequence occurs is not a JSON value, and does not decode there: it is rejected.

Elm typePort?Sent as (Enc)Accepted (Dec)
Intyesthe numbera number that is an integer in IntRange\IntRange
Floatyesthe number; null\effnull for NaN and the infinitiesany number, rounded
Boolyestrue\efftrue or false\efffalsetrue\efftrue or false\efffalse only
Stringyesthe stringa string only
Bytesyesa byte sequencea byte sequence only
()yesnull\effnullnull\effnull only (flags: Chapter 9)
Maybe ty\tyif ty\ty isnull\effnull for Nothing; vv as ty\ty for Just vvnull\effnull as Nothing; anything else as ty\ty, under Just
List ty\tyif ty\ty isan arrayan array whose elements decode as ty\ty
Array ty\tyif ty\ty isan arrayan array whose elements decode as ty\ty
(ty1\ty_1, ty2\ty_2), (ty1\ty_1, ty2\ty_2, ty3\ty_3)if each tyi\ty_i isan array of 2 or 3 elementsan array at least as long; further elements ignored
closed record { lab1\lab_1 : ty1\ty_1, … }if each tyi\ty_i isan object with exactly the fields as keysan object with at least those keys; further keys ignored
Json.Encode.Valueyesitselfany JSON value; no byte sequence
Charno
a function typeno
a type variableno
an extensible recordno
any other custom type (Result, Order, Dict, Set, and those of the program)no
any other abstract type (Cmd, Sub, Task, Process.Id, Program)no
Figure 25: What Can and Cannot Be Sent over a Port

There is no rule for any other pair of a type and a boundary value: a string does not decode as an Int, nor a number as a Bool, nor an object as a tuple, nor a byte sequence as a String or a list. The rules give the following properties.

For every τ\tau with Port(τ)\Port(\tau), Encτ\Enc{\tau} and Decτ\Dec{\tau} are partial functions: each value has at most one encoding, and each boundary value at most one decoding, at τ\tau. Every value that elaboration admits as a payload of type τ\tau has an encoding at τ\tau. If Encτ(v)=j\Enc{\tau}(v)=j, and vv has no component that is NaN, an infinity or a negative zero, and no component Just v′\kw{Just}\ v' whose argument encodes as null\effnull, then Decτ(j)=v′′\Dec{\tau}(j)=v'', where v′′v'' is vv with each array replaced by the value of Array.fromList applied to the list of its elements.

The value of a port is a basic value of arity 1 that the dynamic semantics of modules binds to the port name (Chapter 7). For an outgoing port p\effp of payload type τ\tau, that is the basic value outport(p,τ)\effoutport(\effp,\tau), and for an incoming port the basic value inport(p,τ)\effinport(\effp,\tau). The class BasVal of Chapter 6 includes these values, defined here, for every port name and every payload type. Their meaning is

APPLY(outport(p,τ),v)={leaf(out p,j)∣Encτ(v)=j},APPLY(inport(p,τ),v)={leaf(in(p,τ),v)}. \begin{array}{rcl} \APPLY(\effoutport(\effp,\tau),v) &=& \{\effleaf(\effmout\,\effp,j)\mid\Enc{\tau}(v)=j\},\\ \APPLY(\effinport(\effp,\tau),v) &=& \{\effleaf(\effmin(\effp,\tau),v)\}. \end{array}

By the property above, the first set has exactly one member for every value an outgoing port is applied to: a value is encoded when the command is made. A value sent to an incoming port is decoded when it arrives (Section 8.8).

Figure 25 collects what can and cannot be sent over a port, for each form of type, in agreement with the predicate Port\Port of Section 5.4. A type that is not a payload type makes a port declaration fail to elaborate; no such port exists at run time.

A type alias stands for the type it expands to. Bool and Maybe are custom types, and the only custom types a port admits.

8.8Messages

A leaf command gives messages from the inputs its manager receives: a task leaf from the success value of the process that performs its task, an incoming port leaf from the boundary values that arrive at its port. The rules below define the message given, through the functions of the map\effmaps around the leaf.

Messages⊢c◃x⇒v\effrecv{\effc}{\effx}{v}
⊢leaf(task,tk)◃v⇒v\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\effrecv{\effleaf(\effmtask,\tk)}{v}{v}\end{array}}
(169)
Decτ(j)=v′⊢v v′⇒v′′⊢leaf(in(p,τ),v)◃(p,j)⇒v′′\frac{\begin{array}{c}\Dec{\tau}(j)=v'\premsep\japp{v}{v'}{v''}\end{array}}{\begin{array}{c}\effrecv{\effleaf(\effmin(\effp,\tau),v)}{(\effp,j)}{v''}\end{array}}
(170)
⊢cm/sb◃x⇒v′⊢v v′⇒v′′⊢map(v,cm/sb)◃x⇒v′′\frac{\begin{array}{c}\effrecv{\cm/\sbv}{\effx}{v'}\premsep\japp{v}{v'}{v''}\end{array}}{\begin{array}{c}\effrecv{\effmap(v,\cm/\sbv)}{\effx}{v''}\end{array}}
(171)
Comments:
(169)

The input vv is the value with which the process performing tk\tk succeeds. The task of a leaf made by Task.perform or Task.attempt cannot fail. For Task.perform it is the task given, mapped, whose error type is Never, of which there is no value. For Task.attempt it is the task given with Task.onError applied, which turns a failure into a success whose value is the message made from Err. Either way the task that Perform carries has the error type Never (Section C.2). So every such process that ends gives a message.

(170)

The port name of the input must be that of the manager, so a boundary value arriving at one port gives no message through a leaf of another. When Decτ(j)\Dec{\tau}(j) is undefined, no rule applies, and the leaf gives no message.

(171)

The innermost function is applied first. There is no rule for a leaf of an outgoing port: such a leaf gives no message, and the functions of the map\effmaps around it are never applied. The messages that the leaves of the managers of the platform give are defined by the platform.

A boundary value jj that arrives at an incoming port p\effp of payload type τ\tau is accepted if Decτ(j)\Dec{\tau}(j) is defined, and otherwise rejected. A rejected value is refused at the boundary: the program never sees it, and it is unaffected. When jj is accepted and sb\sbv is the current subscription value of the program, every member c\effc of Flat(sb)\effFlat(\sbv) for which ⊢c◃(p,j)⇒v\effrecv{\effc}{(\effp,j)}{v} gives the message vv, once for each time it occurs in Flat(sb)\effFlat(\sbv). Whether a value is accepted depends on its port alone, not on the current subscriptions; an accepted value that no current subscription receives gives no message. When the messages are handled, and in which order, is defined in Chapter 9.

Discussions

Discuss this chapter·Search the discussions on GitHub

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