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:
| ::= |
A program is thus a non-empty sequence of modules; its last module is its main module. A module imports a module if an import of , written or default (Figure 11), names . 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-distinctEach module of a program imports only modules of the initial basis and modules that stand before it in the program.
program-import-orderEvery module of a program other than the main module is imported, directly or through other modules, by the main module.
program-modules-reachableThe main module has a top-level value declaration that binds
program-mainmain. That declaration has the form , bindingmainalone, andmaindoes not occur free in , so thatmaindoes not refer to itself, directly or through a delayed occurrence.
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 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 , with metavariable , and that of Chapter 7 is called , with metavariable ; a basis is a pair of the two:
The initial basis 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):
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
to be inferred, where is a static basis, a dynamic basis, a basis, a type, a port environment (Section 5.1) and a value. We write for the elaboration of programs, for their evaluation, and alone for the sentence that combines the two. The first sentence is pronounced “ elaborates to and in ”: is the type of main in the main module, and holds the ports of the program. The second is pronounced “ evaluates to in ”: is the value of main. The third is pronounced “ elaborates to and , and evaluates to , in ”. 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 for each bound type variable of class . 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 for each of its variables of class (Section 6.2). For a type scheme , let be those of the bound variables that are type variables of class ; then
The substitution respects classes, since . In the rules below, is the type name of Platform.Program, of arity 3, and that of VirtualDom.Node, of arity 1, of which Html is an alias (Appendix C); is the set of the names of the managers of the platform (Section 8.1). In each rule, the premise is inferred by a principal derivation for the module (Section 5.6), so that , and the scheme of main in it, are principal.
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 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 , the default copy is the declaration itself. In rule (176), is the value bound by the default copy of the declaration of main, whose type is for the scheme of main, the type found by rule (173) or (174).
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.
The value of main is taken from , the value environment of all the top-level declarations of the module (rule (113)), not from the basis , which holds only what the module exposes; so main need not be exposed. No module imports the main module, and 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.
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 , whose types are built from type names of the initial basis alone, nor depends on the choice.
A program has a meaning when for some , and . Since a program is a set of modules, the following property, which may easily be demonstrated, is needed for this to make sense.
Let and be sequences of the same modules, both meeting the restrictions of Section 9.1. Then the sentence can be inferred if and only if the sentence can, where is with the type names that the modules generate renamed.
9.4Execution¶
Let . When is , the program is a view, whose meaning is given in Section 9.6. Otherwise , where is the flags type, and is a program value (Section 8.5). This section defines how a program value runs. Throughout, is the port environment of the program, and the record is fixed: the rules apply its fields init, update and subscriptions, written and so on.
A program runs on a platform. Besides the primitives , their outcomes and the managers (Chapter 8), the platform provides a set 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 , is the set of finite sequences of members of . The empty sequence is , and is the concatenation of the sequences and ; a member of is also a sequence of length one, and is . A finite multiset of members of is a finite map from 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.
A configuration has the following components.
is the present time, in milliseconds since the program started.
is the model, the value that the latest
update, orinit, produced.is the message queue, the messages awaiting
update.holds every process started so far. If , the process named by is live, is reducing , and took its latest step (or started) at time ; its origin is the leaf command whose task it performs, or if it was started by . If , the process has ended or been killed.
is the current subscription value, the value of
subscriptionsapplied to the model.is the sequence of boundary values awaiting delivery on the outgoing port .
holds the leaf commands handed to the managers of the platform and not yet acted on.
The event is the host’s sending of on the incoming port ; is the program’s delivery of on the outgoing port ; an action (Section 8.4) is a primitive performed by a process; , with , is the passing of milliseconds; and a platform event is whatever the platform makes observable of its managers.
For a manager and a value , the leaf commands (and leaf subscriptions) of with argument are
The outgoing queues at the start of a program are
For a type with and a boundary value , the flags value of at is
which is undefined when is.
9.4.2Dispatch and Start¶
The rules of this section and the next allow sentences of the forms
to be inferred. In the first, is a sequence of leaf commands, and the sentence is pronounced “dispatching at time turns , , into , , ”. The second is pronounced “, started with the flags , begins in ”. The third is pronounced “ moves to ”, and, when the event is present, “with ”; a transition without an event is silent. Premises also use the sentences of Chapter 8 and the sentence 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 gives it, in any order. Which order is no part of the meaning, by the effect order convention of Section 8.5.
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)).
The boundary value was computed when the command was made (Section 8.7); dispatch only appends it to the queue of its port. Since is defined only for the outgoing ports of the program, is one of them. The values of one command for one port join the queue in the order of , which is unspecified: with Cmd.batch [send 1, send 2], either value may be delivered first.
The host starts a program with a boundary value as its flags; when the host gives no flags, is . With flags of the unit type every boundary value is accepted, whereas a port of the unit type accepts only . When is undefined, as for 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 serves here as the empty process map and the empty multiset.
9.4.3Transitions¶
A platform step is given by the platform. Its event , if present, is a platform event. A platform step changes nothing but the components and of : it removes leaf commands from , and it appends to messages that, as the platform defines, are given by the leaf commands it removes or by the members of whose manager is in (Section 8.8). Whether a platform step exists may depend on the present time .
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.
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 ; 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.
The process reached by its step at time , so the action takes place no sooner than milliseconds later; for an infinite or NaN delay, no condition is imposed. The definition sets no time by which the action must take place.
The new process names a process never started before, and its origin is : its outcome goes to no other process, and it gives no message (rule (189)).
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 .
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 s around the leaf give no result, the process never ends.
A process whose task fails ends, whatever its origin, and nothing else happens: there is no error, and no message.
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 gives one message, through the functions of the s 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 , and is unspecified. A value accepted while no subscription is current gives no message.
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.
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 ; whether the host has a subscriber, and what it does with , are outside the definition.
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.
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
updateto the model that the previous update produced, or to the model ofinitfor 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 with , and let be a boundary value. An execution of with flags is a finite sequence of transitions
where . The observable events are those of the forms , and , the platform events, and the actions with . The trace of an execution is the sequence of its observable events, in order; silent transitions and the actions of , and , whose whole effect lies within the configuration, are omitted. The set of traces
is the set of the traces of the executions of with flags , and the meaning of is the function that maps each to . When is empty, the program does not start with flags ; 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 , every trace that the implementation can produce when it runs the program with flags is a member of , and the implementation does not start the program when 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 . The program is a view: its meaning is the value 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 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.
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.
Rules (173) and (174) cover the one production, since
mainmay have either of two forms of type. Only themainof the main module is constrained; amainin another module is an ordinary value. The scheme ofmainis taken from E, the environment of all the declarations of the module, not from its interface, somainneed not be exposed. A bound variable of class number in the scheme ofmainis Float in its type: withinit = \() -> (0, Cmd.none)and no other constraint on the model, the model type is Float, and the value ofmainis that of its default copy, at Float (rule (176)). The flags type τ1 is a port type, and is taken from the scheme before the default, so it contains no type variable. Thusmain : Program (Int -> Int) Model Msgandmain : Program flags Model Msg, withflagsa type variable, do not elaborate, and nor does an unannotatedmainwhoseinitis\n -> (n + 1, Cmd.none), whose flags type is a variable of class number. The unit type is a port type, and as flags it is treated specially (Section 9.4). SinceBytesis a port type, flags may be, or contain, values of typeBytes. The model type τ2 and the message type τ3 are unconstrained. Some implementations admit further types of flags, which are not port types (Appendix E).A
mainof typeHtml msgorSvg msghas a type τNode, since both are aliases. No function type is of either form, so amainwith parameters does not elaborate.