The dynamic semantics of the Core defines the values that phrases evaluate to, the environments that declarations and patterns produce, and the closures by which functions are represented. Its sentences infer that a phrase evaluates to a value or an environment in a given environment, or that a pattern, matched against a value, gives an environment or fails. Evaluation in Standard Elm has no state and no exceptions: sentences carry no states, and no rule produces an exception packet (Section 6.7). Every phrase evaluated here is assumed to have elaborated in the static semantics of Chapter 4.
6.1Reduced Syntax¶
Types play no part in evaluation, so for the dynamic semantics the Core syntax is reduced as follows:
The type annotation “” is omitted from every value binding, which thus has the form or .
The Core phrase classes Ty and TyRow are omitted.
6.2Number Specialisation¶
Tested by: LetNumberCaseTuple3Test, LetNumberContainerDualTest, LetNumberIndirectDualTest, LetNumberListCaseTest, NumberDestructFieldTest, StandardA93MutualNumberDefaultTest, StandardA99RecordPatternAsTest, StandardA99RecordPatternBothTest, StandardA99RecordPatternDependentTest, StandardA99RecordPatternFloatTest, StandardA99RecordPatternFunctionTest, StandardA99RecordPatternInTopCopyTest, StandardA99RecordPatternInTupleTest, StandardA99RecordPatternIntTest, StandardA99RecordPatternLambdaTest, StandardA99RecordPatternSharedVariableTest, StandardA99RecordPatternTwoVariablesTest
Erasing types would leave the value of an integer constant undetermined when its type is Float or a type variable of class . The dynamic semantics of every level is therefore given for the elaborated program after number specialisation, a translation of the whole program, which we define here, where the dynamic semantics begins. It starts from the principal derivation of the program (Section 4.10), with the following default.
Defaulting of number. In the principal derivation, each type variable of class that is bound neither by a closure (rules (19), (56) and (61)), nor by the quantifier of an annotated binding (rule (20)), nor as a parameter of a type declaration (rules (61), (63) and (64)), is taken to be : is substituted for it throughout the derivation, which remains a derivation for the program, since . Every constant of such a type is an integer constant, since a floating-point constant has the type itself, and every integer constant may have the type . A variable of class bound in any of these ways is not affected: one bound by a closure or an annotation is quantified in the type scheme of a value, and the copies below assign it; a parameter of a type declaration is bound by a type function, whose applications substitute for it. Since closure never generalises an explicit variable in scope (Section 4.8), an explicit variable is bound by the annotation that introduces it, and so is never defaulted, even where a nested binding uses it: with f : number -> number and f x = let g y = y + x in g 1, the number of x is bound by the annotation of f, not by the closure of g. The default changes no type that the program forces, and so no type a program elaborates with: it decides only types the principal derivation leaves open.
Then each declaration of values, of a let or at the top level of a module, whose bindings have type schemes that quantify type variables of class is replaced by the following copies of it:
the copy that assigns to each of those variables, the default copy, which binds the variables of the declaration under their own names; and
one copy for each other assignment of or to those variables at which the program uses the declaration, the copies binding distinct new variables.
A copy renames the variables the declaration binds, the variables of its pattern rows among them (Section 2.10), and never a label: the copy at of the destructuring binding { h } = { h = \x -> x - 3 }, whose equivalent form is { h = h } = …, is { h = } = …, which binds a new variable to the field h of the same record. A copy is not written in a program, so the syntactic restrictions do not judge it; it keeps every property they give a program, except that the variable of a pattern row need not be spelt as its label, which the restriction on pattern rows requires only of a program. Each occurrence of a variable the declaration binds is made to refer to the copy for the instance at which that occurrence was elaborated. Every binding of a declaration quantifies all the variables of class of the declaration, those its own type does not mention included (Section 4.8), so an instance assigns a type to each of them; a variable that nothing at the occurrence fixes is , as a variable left free in the program is by the default above. A declaration whose bindings quantify no type variable of class is not copied, and is its own default copy. Since the default copy is made whether or not the program uses the declaration at that instance, every declaration has a copy: in particular every top-level value of a module, including main and every value that no phrase uses, has a copy and is evaluated when its module is evaluated. The default copy of the declaration of main is the one that Chapter 9 names by , at the instance of the scheme of main. Copies are made from the outside in, so that a variable of class that a declaration does not quantify is already fixed by the copy of the declaration that encloses it, and a variable of class left free in the program has been taken to be by the default above. With check h = if h == 0 then h else
Debug.todo "h" and x = check (2 ^ -1) used only as x // 2, the copy of x at , where 2 ^ -1 is (Appendix D), is ; but the default copy, at , where it is , has no value, so the program has none. A variable of class that the type of x does not mention, such as that of 2 ^
-1 in if 2 ^ -1 == 0 then 1 else 2, is not a variable of the declaration: no closure generalises it, and it is in every copy. After specialisation every integer constant has the type or . The specialisation is finite: contains only and (Section 4.4), so a declaration with variables of class has at most copies, even when a binding is used recursively at an instance other than its own.
Where the type of an occurrence of an integer constant matters below, it is its type after specialisation.
6.3Simple Objects¶
The dynamic semantics builds its objects from the identifier classes of Chapter 2 and the simple object classes of Figure 19.
| special values | ||||
| basic values | ||||
| constructor names | ||||
| failure |
SVal is the class of special values: the integers of the Int range , the floating-point numbers (among them the two infinities and NaN), the characters, and the strings, which are finite sequences of characters. The range and the representation of characters are implementation parameters, given in Appendix E. Each occurrence of a special constant denotes a special value, written . An integer constant spells an integer , and its value depends on the type of the occurrence after number specialisation (Section 6.2): at , , where maps an integer into by the same normalisation as Int arithmetic (Section E.1); at , is the floating-point number nearest to , rounded as Appendix D prescribes. A floating-point constant denotes the real number it spells, rounded to the nearest floating-point number; a character constant denotes a character, and a string constant a sequence of characters, as explained in Chapter 2.
BasVal is the class of basic values: the names of the operations of the initial basis, listed in Appendix D. Each basic value has an arity , the number of arguments it takes before it acts (Section 6.5).
The class Con of value constructors serves also as the class of constructor names. We take Con to contain, besides the constructors spelt by identifiers, the two list constructor names [] and ::, which no identifier spells. Each constructor has an arity , the number of arguments it takes: for a constructor of a custom type, the number of types that follow it in its declaration (Chapter 3); and . Constructors of different custom types may be spelt alike and have different arities; always means the arity of in the custom type of the value at hand, which elaboration determines. Since only phrases that elaborate are evaluated, a constructor value is only ever compared with the constructors of its own custom type, whose names are distinct, so no confusion arises.
FAIL is the outcome of matching a value against a pattern that does not fit it. It is not a value: it serves only within the rules, to record that a match has not succeeded.
6.4Compound Objects¶
The compound objects of the dynamic semantics are shown in Figure 20. The classes Val, Closure, Env, QualEnv and ValEnv are defined together; they are the smallest classes closed under the constructions of the figure.
The value is the unit value. A member of BasApp is a basic value applied to the arguments it has received so far, and always has ; we identify with , so that . A member of ConVal is a constructor value, and always has : when it is complete, and when it is a constructor awaiting further arguments. A constructor name with is itself a constructor value. We write for the constructor value ; the lists are thus the constructor values and with a list. Records are finite maps from labels to values; the empty record is . A tuple has two or three components. The classes EffVal of effect values (task, command, subscription, process-identifier and program values) and JVal of JSON values are defined in Chapter 8; this chapter uses only the fact that their members are values. Projection, injection and modification are as defined for the static semantics (Chapter 4); the convention that a decorated metavariable ranges over the class of the undecorated one is stated in Chapter 1. The unions in Figure 20 are disjoint unions, and the classes defined there are disjoint from one another; the injections into Val are left implicit. An environment is modified by a value environment as , meaning . The names , , Env and the like denote dynamic objects here and static objects in Chapter 4; the two semantics are presented apart, so the reuse is harmless.
A long identifier is looked up in an environment as follows:
and likewise for constructors. The identifiers that the equivalent forms of derived forms introduce (Appendix A) are looked up in the same way. Because only an import of the module Basics provides the qualifier Basics (Section 3.7), Basics.True, Basics.False and Basics.negate always denote the values of the initial dynamic basis (Appendix D); the constructor :: is bound in the initial environment of that appendix, from which every module is evaluated. Operators are not looked up in , and are not in the domain of a value environment. Since no program declares an operator, every operator is one of the initial basis, and its value is the same in every environment: the value that the dynamic operator table of Appendix D (Figure 46) gives it. For :: this value is the list constructor ::.
6.5Basic Values¶
The meaning of the basic values is represented by a function which, for a basic value of arity and values , gives a finite set of values:
is defined in Appendix D (Section D.4), which also gives the arity of every basic value. Equality and the order relations of the initial basis are basic values, and their meaning on structured values is defined there, not in this chapter. gives a set, rather than a value, so that the definition is exact about the two operations of the initial basis that do not determine one result. For every basic value the set has exactly one member, with two exceptions. First, when an equality test reaches two functions its result is an unspecified Bool, so that the set may hold both True and False; evaluation is then non-deterministic. Second, for the basic value bound to Debug.todo, is empty, so that an application of Debug.todo has no result. Every other basic value gives a result for every argument: arithmetic gives a defined value even where no mathematically correct one exists, and Debug.log returns its second argument, any output it makes lying outside the definition. Thus no evaluation ends in a run-time error: a phrase either has a value or has none.
6.6Function Closures¶
A function closure is applied to a value by matching against and evaluating in the resulting environment. The environment is not but , where the domain of the fourth component holds the variables treated recursively, and
is defined by:
;
if , then ;
if , then
.
Thus gives each closure in the bindings of afresh, so that a recursive application made during the evaluation of finds them; the recursion is unwound one step at a time, and every semantic object stays finite. is used in two places in the rules: for a recursive declaration, and for the application of a closure.
A declaration is recursive when one of its bindings refers to a variable it binds. For a value binding , is the set of variables that occur free in the right-hand sides of the bindings of , with “free” and “right-hand side” as defined in Section A.1; the variables bound by are the domain of the value environment it evaluates to. By the restrictions of Section 2.10, every binding of a recursive declaration has the form .
6.7Inference Rules¶
The rules infer sentences of the forms
where is an environment, is a value against which the phrase is matched (, a record, for pattern rows), and is the result: a value, a record, a value environment, or FAIL (a pattern row gives a value environment, never FAIL). The first form is pronounced “ evaluates to in ”, the second “, matched against , gives in ”, and the third “ applied to gives ”. The hypotheses of a rule that are not sentences are its side conditions. The convention for options is the same as for the Core static semantics: in each instance of a rule, the phrases in first options are either all present or all absent. Sentences have no states, and there are no exception packets: the Core has neither assignable references nor exceptions. Hence no state convention and no exception convention is needed, and every rule is given in full. In particular the order of the premises of a rule carries no meaning. A rule says which results the parts of a phrase must have, and the conclusion holds only when all of them do; since nothing but the result is observable, an implementation may evaluate the parts of a phrase in any order. Only the parts that some premise names are evaluated: the body of a lambda is evaluated when the closure is applied, and a case evaluates the body of the chosen match rule only.
We allow compound variables, built from the variables of Figures 19 and 20 with “/”, to range over unions of classes: ranges over and over . We extend modification to failure:
A phrase has no result in when no sentence with it on the left of can be inferred. This is so when its evaluation does not terminate, and when it needs an application of Debug.todo. The rules place no bound on the resources evaluation uses; an implementation that stops for lack of resources is outside the definition.
The argument is evaluated whether or not the function uses it: (\_ -> 1) (Debug.todo "x") has no value.
The fourth component of the closure is empty because a lambda introduces no recursively defined values; rule (91) supplies it for a recursive declaration. The closure holds the environment of this evaluation, so closures made in successive calls of a function hold the values of their own calls. Two evaluations of one lambda give closures that no rule distinguishes from one another by identity.
There is no rule in which , matched against , gives FAIL: the static semantics requires the pattern of a lambda to be exhaustive (Chapter 4). Were it to fail, no rule would apply.
No side condition is needed: in a program that elaborates a constructor value is applied only while it awaits arguments. Thus Just applied to 1 gives the complete constructor value Just 1.
The result is any member of the set (Section 6.5). When the set has two members the rule does not determine the result: this happens only when an equality test reaches two functions, as in (\x -> x) == (\x -> x), which may evaluate to True or to False. When the set is empty, as for Debug.todo, the rule never applies, and the application has no result.
No rule applies the unit value, a special value, a complete constructor value, a record, a tuple or an effect value: in a program that elaborates the function of an application is always a closure, a constructor value awaiting arguments, or a basic value awaiting arguments. The rules of this group are the only definition of application; Chapters 8 and 9 use the sentence to apply function values outside any phrase.
The value being matched stands to the left of the turnstile. The match rules are tried in the order written, and the first whose pattern matches gives the result; the match rules after it are not evaluated, even when their patterns would also match. This order is part of the meaning, and is unaffected by the freedom of evaluation order (Section 6.7), since a rule for a later match rule requires every earlier one to give FAIL.
The body is not evaluated, so the result is FAIL even when has no value.
Rules (90) and (91) cover the one production, distinguished by whether the declaration is recursive (Section 6.6). The right-hand sides are evaluated in , without the variables the declaration binds; in a recursive declaration every right-hand side is a lambda, and gives each of its closures the bindings of the whole declaration. is not applied to a declaration that is not recursive, since a closure it binds may have been made elsewhere, in an environment where a variable spelt as one that the declaration binds denotes something else. A let declaration needs only because every binding of a recursive let declaration is a function (Section 2.10); top-level declarations, whose recursive members need not be functions, are treated in Chapter 7.
A constant in a pattern is an integer, character or string constant, never a floating-point one, so the test is identity of special values. An integer constant matches the normalised integer it denotes.
The pattern row binds its variable to the field of ; in a row written as a label alone the variable is spelt as the label (Appendix A). The side condition makes the rule apply exactly when has the field; by elaboration it always holds, since a record of the type the pattern requires has every field the pattern row names. Hence a pattern row cannot fail, and its result is a value environment, never FAIL, as rule (104) expects.
Here , and by elaboration . All the argument patterns are matched, and the result is FAIL when any of them fails. Since matching has no effect and always has a result, the order in which the arguments are matched is immaterial.
By the syntactic restrictions, is not bound by , so the modification adds a binding and replaces none.
The value of an integer constant depends on the type of its occurrence after number specialisation (Section 6.2). Thus, with
x : Floatdefined byx = 2, the constant denotes a floating-point number andx ^-1evaluates to0.5; and withinc n = n + 1, the applicationinc 0.5uses the default copy ofinc, at Float, in which1denotes a floating-point number, and evaluates to1.5, whileinc (7 // 2)uses its copy at Int and evaluates to the Int value 4.As in the static semantics, long variables are looked up in the environment. The identifier status of the static semantics plays no part.
A constructor of a custom type is bound to the constructor value con, awaiting all its arguments; the value bound to the constructor of a record type alias is given in Chapter 7.
The value of an operator does not depend on E, so a variable that a program declares cannot capture an operator: a top-level
addof a module leaves(+)unchanged. The operator values(&&)and(||)denote functions of the initial basis, which receive both arguments as values: in(&&) False ethe expressioneis evaluated. Only the infixed formse1 && e2ande1 || e2, which are derived forms (Appendix A), evaluate their second operand conditionally.By elaboration, Domr′⊆Domr, so the result has the fields of r, with the values of the fields named in exprow replaced. The record bound to var is unaffected, there being no state.
Every binding of dec is evaluated, whether or not exp uses it:
let _ = Debug.todo "x" in 1has no value. Aletbinds a single group of bindings (Appendix A), so the evaluation of the groups of aletas written is ordered by the nesting of the equivalent form, and within a group the order does not matter.