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.
| process identifiers | ||||
| primitives | ||||
| port names | ||||
| managers of the platform | ||||
| JSON numbers | ||||
| byte sequences |
A process identifier names a process (Section 8.4); ProcId is an infinite set. The class Prim of primitives is
where , and are the primitives of the module Process, and 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 has an arity ; the arity of each of , and is 1. A port name is the variable of a port declaration (Chapter 3). Port names are global to a program and distinct from one another (Section 3.7). , 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. is the set of JSON numbers: the rational numbers with and integers, that is, the numbers a JSON text can spell. Every integer of and every finite floating-point number is a member of ; we omit the injections from these numbers into . The injection takes both floating-point zeros, and , to the JSON number : a JSON number has no sign apart from its value. We write for the set of strings of SVal, for the set of floating-point numbers of SVal, and for its finite members, so that holds the two infinities and NaN. For , is the floating-point number nearest , rounded as the value of a floating-point constant is (Section 6.3); is the float , not . is the set of byte sequences: the finite sequences of integers from to . We write , with and for each , for the byte sequence of in order. A byte sequence is not a JSON number, a string or an array: is disjoint from , from 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.
We write the members of these classes as follows:
Subscriptions are written as commands are, and an action is the triple . We write for , the batch with no components, in CmdVal and in SubVal. In a primitive task , always .
A boundary value is , , , a number, a string, a byte sequence, an array, which is a member of , 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 . A byte sequence is not a JSON value. The metavariable ranges over BVal; where only a JSON value is meant we write . 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 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 we write for the list value , and when . 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 , and is the union of multisets, which adds multiplicities.
8.3Inference Rules¶
The semantic rules of this chapter allow sentences of the forms
to be inferred. The first is pronounced “ reduces to ”, and, when the label is present, “performing ”; a reduction without a label is silent. The second is pronounced “ encodes at as ”, and the third “ decodes at as ”; there is a type with (Section 5.4). In the fourth, is a command or subscription value with no in it (a leaf command, Section 8.5), the input is a value or a pair of a port name and a boundary value, and the sentence is pronounced “, receiving , gives the message ”. Premises also use the sentence 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 , 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 to range over .
and are defined by the rules alone: is the boundary value for which can be inferred, and is undefined when there is none, and likewise for (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 has succeeded with , and has failed with ; these two are the outcomes. The task performs and, if it succeeds with , goes on with the task to which applied to evaluates; is the same for failure. A primitive task performs the primitive on .
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 , whose outcome is a member of the set
of possible outcomes, and which may act on the world outside the process. For the primitives of Process,
and their actions mean the following.
The action , where , takes place no sooner than milliseconds after the process reached , when ; when is an infinity or NaN, when it takes place is not defined.
starts a new process, named by , reducing . The identifier 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.
ends the process named by 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 , the set , 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.
The following properties follow from the rules.
An outcome has no reduction. If is not an outcome, then either every reduction of is silent, and all have the same right-hand side up to the choice of a member of a set given by ; or every reduction of is labelled by an action , for one and the same and ; or 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 is one of the following.
The task manager . Its leaves are commands; it performs in a new process, and when succeeds with , the leaf gives a message from .
The manager of an outgoing port . Its leaves , with , are commands; each sends to the host, and gives no message.
The manager of an incoming port of payload type . Its leaves are subscriptions, where is a function value; while the subscription is current, each boundary value the host sends on that decodes at gives a message through (Section 8.8).
A manager of the platform, in . 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 determines the payload type of every manager of a program. A batch groups commands, and applies the function value to every message that gives.
A command or subscription value with no in it has the form with , and is called a leaf command (or leaf subscription); its manager is . The flattening of a command value is the finite multiset of leaf commands defined by
where a leaf command that occurs several times in gives as many members of the multiset on the right of the second equation. The flattening of a subscription value is defined by the same equations. In particular .
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 is the meaning of a Program: 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 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 | ||
Task.succeed | 1 | |
Task.fail | 1 | |
Task.andThen | 2 | |
Task.onError | 2 | |
Task.command | 1 | |
Process.sleep | 1 | |
Process.spawn | 1 | |
Process.kill | 1 | |
Cmd.batch | 1 | |
Cmd.map | 2 | |
Sub.batch | 1 | |
Sub.map | 2 | |
Platform.worker | 1 |
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 , the value of batch []. The texts of Task.perform and Task.attempt apply command to a constructor value , 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 evaluates to , where is the value of Task.map , 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 sent on an outgoing port of payload type becomes the boundary value , and a boundary value arriving at an incoming port of payload type becomes the value , if that is defined. The payload types are the types with (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 of Appendix D (Section D.4.10), each an Int value from to .
The integer itself is the JSON number. Whether a host can hold every integer of exactly is not a matter for the definition (Appendix E).
JSON has no number for NaN or for an infinity. Such a float is encoded as , which a Float port does not decode, while a Maybe Float port decodes it as Nothing.
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 on the left is an Int value and on the right the same integer.
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 : in Maybe (Maybe Int), both Nothing and Just Nothing encode as , and so do Nothing and Just () in Maybe ().
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.
The fields become the keys of an object, which has no order, so the order of fields is no part of what is sent.
A value of type Json.Encode.Value is already a JSON value and is sent as it is. The side condition holds of every value of that type, so no byte sequence is ever sent through Json.Encode.Value.
A JSON number is an Int exactly when it is an integer within : 1.0 and -0 decode as 1 and 0, while 1.5, and an integer outside , do not decode. Since is a parameter of the implementation, so is the set of numbers a port of type Int accepts (Appendix E).
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 is the number , and decodes as the float , not . No boundary value decodes as NaN.
A byte sequence, and nothing else, decodes at Bytes: is undefined for every JSON value , so a string or an array of numbers sent to a Bytes port is rejected at the boundary.
At an incoming port, the unit type accepts and nothing else. The flags of a program are decoded otherwise when their type is the unit type (Chapter 9).
The side condition makes 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 that does not decode at does not decode at Maybe either.
An array longer than the tuple is accepted, and its further elements are ignored; a shorter array does not decode.
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.
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 type | Port? | Sent as (Enc) | Accepted (Dec) |
Int | yes | the number | a number that is an integer in |
Float | yes | the number; for NaN and the infinities | any number, rounded |
Bool | yes | or | or only |
String | yes | the string | a string only |
Bytes | yes | a byte sequence | a byte sequence only |
() | yes | only (flags: Chapter 9) | |
Maybe | if is | for Nothing; as for Just | as Nothing; anything else as , under Just |
List | if is | an array | an array whose elements decode as |
Array | if is | an array | an array whose elements decode as |
(, ), (, , ) | if each is | an array of 2 or 3 elements | an array at least as long; further elements ignored |
closed record { : , … } | if each is | an object with exactly the fields as keys | an object with at least those keys; further keys ignored |
Json.Encode.Value | yes | itself | any JSON value; no byte sequence |
Char | no | ||
| a function type | no | ||
| a type variable | no | ||
| an extensible record | no | ||
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 |
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 with , and are partial functions: each value has at most one encoding, and each boundary value at most one decoding, at . Every value that elaboration admits as a payload of type has an encoding at . If , and has no component that is NaN, an infinity or a negative zero, and no component whose argument encodes as , then , where is with each array replaced by the value of
Array.fromListapplied 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 of payload type , that is the basic value , and for an incoming port the basic value . The class BasVal of Chapter 6 includes these values, defined here, for every port name and every payload type. Their meaning is
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 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 s around the leaf.
The input is the value with which the process performing 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.
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 is undefined, no rule applies, and the leaf gives no message.
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 s 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 that arrives at an incoming port of payload type is accepted if is defined, and otherwise rejected. A rejected value is refused at the boundary: the program never sees it, and it is unaffected. When is accepted and is the current subscription value of the program, every member of for which gives the message , once for each time it occurs in . 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.
The task tk to which v applied to 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.The continuation v is skipped: a failure passes over every andThen until an onError handles it, as rule (135) does. Rule (136) is the dual for success.
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, and the definition guarantees no more. In particular, a program cannot rely on a process running without interruption until it reaches a primitive.
This is the only rule with a label, and it does not determine its result: the outcome is any member of Out(π,v1⋯vk). For spawn, 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.