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

6Dynamic Semantics for the Core

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 “: ty\kw{:}\ \ty” is omitted from every value binding, which thus has the form var = exp ⟨and valbind⟩\var\ \kw{=}\ \expm\ \opt{\kw{and}\ \valbind} or atpat = exp\atpat\ \kw{=}\ \expm.

  • 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 number\knum. 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 number\knum that is bound neither by a closure (rules (19), (56) and (61)), nor by the quantifier ∀U′\forall U' of an annotated binding (rule (20)), nor as a parameter of a type declaration (rules (61), (63) and (64)), is taken to be Float\tFloat: Float\tFloat is substituted for it throughout the derivation, which remains a derivation for the program, since Float∈Admnumber\tFloat \in \Adm{\knum}. Every constant of such a type is an integer constant, since a floating-point constant has the type Float\tFloat itself, and every integer constant may have the type Float\tFloat. A variable of class number\knum 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 number\knum is replaced by the following copies of it:

  • the copy that assigns Float\tFloat 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 Int\tInt or Float\tFloat 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 Int\tInt of the destructuring binding { h } = { h = \x -> x - 3 }, whose equivalent form is { h = h } = …, is { h = h′\mathtt{h}' } = …, which binds a new variable h′\mathtt{h}' 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 number\knum 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 Float\tFloat, as a variable left free in the program is by the default above. A declaration whose bindings quantify no type variable of class number\knum 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 Dflt \pgDflt, at the instance Dflt σ\pgDflt\sigma of the scheme σ\sigma of main. Copies are made from the outside in, so that a variable of class number\knum that a declaration does not quantify is already fixed by the copy of the declaration that encloses it, and a variable of class number\knum left free in the program has been taken to be Float\tFloat 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 Int\tInt, where 2 ^ -1 is 00 (Appendix D), is 00; but the default copy, at Float\tFloat, where it is 0.50.5, has no value, so the program has none. A variable of class number\knum 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 Float\tFloat in every copy. After specialisation every integer constant has the type Int\tInt or Float\tFloat. The specialisation is finite: Admnumber\Adm{\knum} contains only Int\tInt and Float\tFloat (Section 4.4), so a declaration with kk variables of class number\knum has at most 2k2^k 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.

sv\sv∈\inSVal\SValspecial values
bb∈\inBasVal\BasValbasic values
con\con∈\inCon\cls{Con}constructor names
{FAIL}\FAILclsfailure
Figure 19: Simple Semantic Objects

SVal is the class of special values: the integers of the Int range IntRange\IntRange, the floating-point numbers (among them the two infinities and NaN), the characters, and the strings, which are finite sequences of characters. The range IntRange\IntRange and the representation of characters are implementation parameters, given in Appendix E. Each occurrence of a special constant scon\scon denotes a special value, written val(scon)\valof(\scon). An integer constant spells an integer nn, and its value depends on the type of the occurrence after number specialisation (Section 6.2): at Int\tInt, val(scon)=norm(n)\valof(\scon)=\norm(n), where norm\norm maps an integer into IntRange\IntRange by the same normalisation as Int arithmetic (Section E.1); at Float\tFloat, val(scon)\valof(\scon) is the floating-point number nearest to nn, 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 bb has an arity arity(b)≥1\arity(b)\geq 1, 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 con\con has an arity arity(con)≥0\arity(\con)\geq 0, 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); arity([])=0\arity(\kw{[]})=0 and arity(::)=2\arity(\kw{::})=2. Constructors of different custom types may be spelt alike and have different arities; arity(con)\arity(\con) always means the arity of con\con 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.

vv∈\inVal\Val={()}∪SVal∪BasApp∪ConVal∪Record= \{()\}\cup\SVal\cup\BasApp\cup\ConVal\cup\Record
=∪Tuple∪Closure∪EffVal∪JVal\phantom{{}={}}\cup\Tuple\cup\Closure\cup\EffVal\cup\JVal
(b,v1⋯vk)(b, v_1\cdots v_k)∈\inBasApp\BasApp=⋃k≥0BasVal×Valk= \textstyle\bigcup_{k\geq 0}\BasVal\times\Val^k
con v1⋯vk\con\ v_1\cdots v_k∈\inConVal\ConVal=⋃k≥0Con×Valk= \textstyle\bigcup_{k\geq 0}\cls{Con}\times\Val^k
rr∈\inRecord\Record=Lab→finVal= \cls{Lab}\finmap\Val
(v1,v2⟨,v3⟩)(v_1,v_2\opt{,v_3})∈\inTuple\Tuple=Val2∪Val3= \Val^2\cup\Val^3
(atpat,exp,E,VE)(\atpat,\expm,E,\VE)∈\inClosure\Closure=AtPat×Exp×Env×ValEnv= \cls{AtPat}\times\cls{Exp}\times\Env\times\ValEnv
(QE,VE) or E(\QE,\VE)\ \text{or}\ E∈\inEnv\Env=QualEnv×ValEnv= \QualEnv\times\ValEnv
QE\QE∈\inQualEnv\QualEnv=ModId→finEnv= \cls{ModId}\finmap\Env
VE\VE∈\inValEnv\ValEnv=(Var∪Con)→finVal= (\cls{Var}\cup\cls{Con})\finmap\Val
Figure 20: Compound Semantic Objects

The value ()() is the unit value. A member (b,v1⋯vk)(b, v_1\cdots v_k) of BasApp is a basic value applied to the arguments it has received so far, and always has k<arity(b)k<\arity(b); we identify (b)(b) with bb, so that BasVal⊆BasApp\BasVal\subseteq\BasApp. A member con v1⋯vk\con\ v_1\cdots v_k of ConVal is a constructor value, and always has k≤arity(con)k\leq\arity(\con): when k=arity(con)k=\arity(\con) it is complete, and when k<arity(con)k<\arity(\con) it is a constructor awaiting further arguments. A constructor name con\con with k=0k=0 is itself a constructor value. We write v1::v2v_1\mathbin{\kw{::}}v_2 for the constructor value :: v1 v2\kw{::}\ v_1\ v_2; the lists are thus the constructor values []\kw{[]} and v1::v2v_1\mathbin{\kw{::}}v_2 with v2v_2 a list. Records are finite maps from labels to values; the empty record is {}\emptymap. 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 EE is modified by a value environment as E+VEE+\VE, meaning E+({},VE)E+(\emptymap,\VE). The names EE, VE\VE, 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 E=(QE,VE)E=(\QE,\VE) as follows:

E(var)=VE(var),E(modid.var)=(QE(modid))(var), E(\var)=\VE(\var),\qquad E(\modid\kw{.}\var)=(\QE(\modid))(\var),

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 E0E_0 of that appendix, from which every module is evaluated. Operators are not looked up in EE, 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 OTDYN(op)\OTdyn(\opv) 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 APPLY\APPLY which, for a basic value bb of arity nn and values v1,…,vnv_1,\ldots,v_n, gives a finite set of values:

APPLY(b,v1⋯vn)∈Fin Val. \APPLY(b, v_1\cdots v_n)\in\Fin\Val .

APPLY\APPLY 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. APPLY\APPLY 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 bb bound to Debug.todo, APPLY(b,v)\APPLY(b,v) 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 (atpat,exp,E,VE)(\atpat,\expm,E,\VE) is applied to a value vv by matching vv against atpat\atpat and evaluating exp\expm in the resulting environment. The environment is not E+VEE+\VE but E+Rec VEE+\Rec\VE, where the domain of the fourth component VE\VE holds the variables treated recursively, and

Rec :ValEnv→ValEnv \Rec : \ValEnv\to\ValEnv

is defined by:

  • Dom (Rec VE)=Dom VE\Dom(\Rec\VE)=\Dom\VE;

  • if VE(var)∉Closure\VE(\var)\notin\Closure, then (Rec VE)(var)=VE(var)(\Rec\VE)(\var)=\VE(\var);

  • if VE(var)=(atpat,exp,E,VE′)\VE(\var)=(\atpat,\expm,E,\VE'), then
    (Rec VE)(var)=(atpat,exp,E,VE)(\Rec\VE)(\var)=(\atpat,\expm,E,\VE).

Thus Rec VE\Rec\VE gives each closure in Ran VE\Ran\VE the bindings of VE\VE afresh, so that a recursive application made during the evaluation of exp\expm finds them; the recursion is unwound one step at a time, and every semantic object stays finite. Rec \Rec 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 valbind\valbind, FV(valbind)\dcoreFV(\valbind) is the set of variables that occur free in the right-hand sides of the bindings of valbind\valbind, with “free” and “right-hand side” as defined in Section A.1; the variables bound by valbind\valbind 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 var = \ atpat -> exp\var\ \kw{=}\ \kw{\textbackslash}\,\atpat\ \kw{->}\ \expm.

6.7Inference Rules

The rules infer sentences of the forms

E⊢phrase⇒A,E,v⊢phrase⇒A,⊢v1 v2⇒v \jdg{E}{\mathit{phrase}}{A},\qquad \jdgv{E}{v}{\mathit{phrase}}{A},\qquad \japp{v_1}{v_2}{v}

where EE is an environment, vv is a value against which the phrase is matched (rr, a record, for pattern rows), and AA 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 “phrase\mathit{phrase} evaluates to AA in EE”, the second “phrase\mathit{phrase}, matched against vv, gives AA in EE”, and the third “v1v_1 applied to v2v_2 gives vv”. 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 ⟨ ⟩\opt{\ } 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: v/FAILv/\FAIL ranges over Val∪{FAIL}\Val\cup\FAILcls and VE/FAIL\VE/\FAIL over ValEnv∪{FAIL}\ValEnv\cup\FAILcls. We extend modification to failure:

VE+FAIL  =  FAIL+VE  =  FAIL+FAIL  =  FAIL. \VE+\FAIL \;=\; \FAIL+\VE \;=\; \FAIL+\FAIL \;=\; \FAIL .

A phrase has no result in EE when no sentence with it on the left of ⇒\Rightarrow 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.

Atomic ExpressionsE⊢atexp⇒v\jdg{E}{\atexp}{v}
E⊢scon⇒val(scon)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{E}{\scon}{\valof(\scon)}\end{array}}
(65)
E(longvar)=vE⊢longvar⇒v\frac{\begin{array}{c}E(\longvar)=v\end{array}}{\begin{array}{c}\jdg{E}{\longvar}{v}\end{array}}
(66)
E(longcon)=vE⊢longcon⇒v\frac{\begin{array}{c}E(\longcon)=v\end{array}}{\begin{array}{c}\jdg{E}{\longcon}{v}\end{array}}
(67)
E⊢(op)⇒OTDYN(op)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{E}{\kw{(}\opv\kw{)}}{\OTdyn(\opv)}\end{array}}
(68)
E⊢()⇒()\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{E}{\kw{()}}{()}\end{array}}
(69)
E⊢exp1⇒v1E⊢exp2⇒v2⟨E⊢exp3⇒v3⟩E⊢(exp1, exp2 ⟨, exp3⟩)⇒(v1,v2⟨,v3⟩)\frac{\begin{array}{c}\jdg{E}{\expm_1}{v_1}\premsep\jdg{E}{\expm_2}{v_2}\premsep \opt{\jdg{E}{\expm_3}{v_3}}\end{array}}{\begin{array}{c}\jdg{E}{\kw{(}\expm_1\kw{,}\ \expm_2\ \opt{\kw{,}\ \expm_3}\kw{)}} {(v_1,v_2\opt{,v_3})}\end{array}}
(70)
E⊢[ ]⇒[]\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{E}{\kw{[}\ \kw{]}}{\kw{[]}}\end{array}}
(71)
⟨E⊢exprow⇒r⟩E⊢{ ⟨exprow⟩ }⇒{}⟨+ r⟩\frac{\begin{array}{c}\opt{\jdg{E}{\exprow}{r}}\end{array}}{\begin{array}{c}\jdg{E}{\kw{\kwlb}\ \opt{\exprow}\ \kw{\kwrb}}{\emptymap\opt{+\,r}}\end{array}}
(72)
E(var)=rE⊢exprow⇒r′E⊢{ var | exprow }⇒r+r′\frac{\begin{array}{c}E(\var)=r\premsep\jdg{E}{\exprow}{r'}\end{array}}{\begin{array}{c}\jdg{E}{\kw{\kwlb}\ \var\ \kw{|}\ \exprow\ \kw{\kwrb}}{r+r'}\end{array}}
(73)
E⊢exp⇒vE⊢(exp)⇒v\frac{\begin{array}{c}\jdg{E}{\expm}{v}\end{array}}{\begin{array}{c}\jdg{E}{\kw{(}\expm\kw{)}}{v}\end{array}}
(74)
E⊢dec⇒VEE+VE⊢exp⇒vE⊢let dec in exp⇒v\frac{\begin{array}{c}\jdg{E}{\dec}{\VE}\premsep\jdg{E+\VE}{\expm}{v}\end{array}}{\begin{array}{c}\jdg{E}{\kw{let}\ \dec\ \kw{in}\ \expm}{v}\end{array}}
(75)
Comments:
(65)

The value of an integer constant depends on the type of its occurrence after number specialisation (Section 6.2). Thus, with x : Float defined by x = 2, the constant denotes a floating-point number and x ^ -1 evaluates to 0.5; and with inc n = n + 1, the application inc 0.5 uses the default copy of inc, at Float\tFloat, in which 1 denotes a floating-point number, and evaluates to 1.5, while inc (7 // 2) uses its copy at Int\tInt and evaluates to the Int value 44.

(66)

As in the static semantics, long variables are looked up in the environment. The identifier status of the static semantics plays no part.

(67)

A constructor of a custom type is bound to the constructor value con\con, awaiting all its arguments; the value bound to the constructor of a record type alias is given in Chapter 7.

(68)

The value of an operator does not depend on EE, so a variable that a program declares cannot capture an operator: a top-level add of a module leaves (+) unchanged. The operator values (&&) and (||) denote functions of the initial basis, which receive both arguments as values: in (&&) False e the expression e is evaluated. Only the infixed forms e1 && e2 and e1 || e2, which are derived forms (Appendix A), evaluate their second operand conditionally.

(73)

By elaboration, Dom r′⊆Dom r\Dom r'\subseteq\Dom r, so the result has the fields of rr, with the values of the fields named in exprow\exprow replaced. The record bound to var\var is unaffected, there being no state.

(75)

Every binding of dec\dec is evaluated, whether or not exp\expm uses it: let _ = Debug.todo "x" in 1 has no value. A let binds a single group of bindings (Appendix A), so the evaluation of the groups of a let as written is ordered by the nesting of the equivalent form, and within a group the order does not matter.

Expression RowsE⊢exprow⇒r\jdg{E}{\exprow}{r}
E⊢exp⇒v⟨E⊢exprow⇒r⟩E⊢lab = exp ⟨, exprow⟩⇒{lab↦v}⟨+ r⟩\frac{\begin{array}{c}\jdg{E}{\expm}{v}\premsep\opt{\jdg{E}{\exprow}{r}}\end{array}}{\begin{array}{c}\jdg{E}{\lab\ \kw{=}\ \expm\ \opt{\kw{,}\ \exprow}}{\{\lab\mapsto v\}\opt{+\,r}}\end{array}}
(76)
Comments:
(76)

The fields are evaluated in no specified order, neither as written nor by label (Section 6.7).

ExpressionsE⊢exp⇒v\jdg{E}{\expm}{v}
E⊢atexp⇒vE⊢atexp⇒v\frac{\begin{array}{c}\jdg{E}{\atexp}{v}\end{array}}{\begin{array}{c}\jdg{E}{\atexp}{v}\end{array}}
(77)
E⊢exp⇒v1E⊢atexp⇒v2⊢v1 v2⇒vE⊢exp atexp⇒v\frac{\begin{array}{c}\jdg{E}{\expm}{v_1}\premsep\jdg{E}{\atexp}{v_2}\premsep\japp{v_1}{v_2}{v}\end{array}}{\begin{array}{c}\jdg{E}{\expm\ \atexp}{v}\end{array}}
(78)
E⊢\ atpat -> exp⇒(atpat,exp,E,{})\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{E}{\kw{\textbackslash}\,\atpat\ \kw{->}\ \expm}{(\atpat,\expm,E,\emptymap)}\end{array}}
(79)
E⊢exp⇒vE,v⊢match⇒v′E⊢case exp of match⇒v′\frac{\begin{array}{c}\jdg{E}{\expm}{v}\premsep\jdgv{E}{v}{\match}{v'}\end{array}}{\begin{array}{c}\jdg{E}{\kw{case}\ \expm\ \kw{of}\ \match}{v'}\end{array}}
(80)
Comments:
(78)

The argument is evaluated whether or not the function uses it: (\_ -> 1) (Debug.todo "x") has no value.

(79)

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 EE 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.

(80)

There is no rule in which match\match gives FAIL: the static semantics requires every match of a case to be exhaustive (Chapter 4), so for a value of the right type some match rule applies.

Application of Values⊢v1 v2⇒v\japp{v_1}{v_2}{v}
E+Rec VE,v⊢atpat⇒VE′E+Rec VE+VE′⊢exp⇒v′⊢(atpat,exp,E,VE) v⇒v′\frac{\begin{array}{c}\jdgv{E+\Rec\VE}{v}{\atpat}{\VE'}\premsep \jdg{E+\Rec\VE+\VE'}{\expm}{v'}\end{array}}{\begin{array}{c}\japp{(\atpat,\expm,E,\VE)}{v}{v'}\end{array}}
(81)
⊢(con v1⋯vk) v⇒con v1⋯vk v\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\japp{(\con\ v_1\cdots v_k)}{v}{\con\ v_1\cdots v_k\ v}\end{array}}
(82)
k+1<arity(b)⊢(b,v1⋯vk) v⇒(b,v1⋯vk v)\frac{\begin{array}{c}k+1<\arity(b)\end{array}}{\begin{array}{c}\japp{(b,v_1\cdots v_k)}{v}{(b,v_1\cdots v_k\,v)}\end{array}}
(83)
k+1=arity(b)v′∈APPLY(b,v1⋯vk v)⊢(b,v1⋯vk) v⇒v′\frac{\begin{array}{c}k+1=\arity(b)\premsep v'\in\APPLY(b,v_1\cdots v_k\,v)\end{array}}{\begin{array}{c}\japp{(b,v_1\cdots v_k)}{v}{v'}\end{array}}
(84)
Comments:
(81)

There is no rule in which atpat\atpat, matched against vv, gives FAIL: the static semantics requires the pattern of a lambda to be exhaustive (Chapter 4). Were it to fail, no rule would apply.

(82)

No side condition k<arity(con)k<\arity(\con) 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.

(84)

The result is any member of the set APPLY(b,v1⋯vk v)\APPLY(b,v_1\cdots v_k\,v) (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 ⊢v1 v2⇒v\japp{v_1}{v_2}{v} to apply function values outside any phrase.

MatchesE,v⊢match⇒v′/FAIL\jdgv{E}{v}{\match}{v'/\FAIL}
E,v⊢mrule⇒v′E,v⊢mrule ⟨; match⟩⇒v′\frac{\begin{array}{c}\jdgv{E}{v}{\mrule}{v'}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\mrule\ \opt{\kw{;}\ \match}}{v'}\end{array}}
(85)
E,v⊢mrule⇒FAILE,v⊢mrule⇒FAIL\frac{\begin{array}{c}\jdgv{E}{v}{\mrule}{\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\mrule}{\FAIL}\end{array}}
(86)
E,v⊢mrule⇒FAILE,v⊢match⇒v′/FAILE,v⊢mrule ; match⇒v′/FAIL\frac{\begin{array}{c}\jdgv{E}{v}{\mrule}{\FAIL}\premsep\jdgv{E}{v}{\match}{v'/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\mrule\ \kw{;}\ \match}{v'/\FAIL}\end{array}}
(87)
Comments:
(85)

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.

Match RulesE,v⊢mrule⇒v′/FAIL\jdgv{E}{v}{\mrule}{v'/\FAIL}
E,v⊢pat⇒VEE+VE⊢exp⇒v′E,v⊢pat -> exp⇒v′\frac{\begin{array}{c}\jdgv{E}{v}{\pat}{\VE}\premsep\jdg{E+\VE}{\expm}{v'}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat\ \kw{->}\ \expm}{v'}\end{array}}
(88)
E,v⊢pat⇒FAILE,v⊢pat -> exp⇒FAIL\frac{\begin{array}{c}\jdgv{E}{v}{\pat}{\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat\ \kw{->}\ \expm}{\FAIL}\end{array}}
(89)
Comments:
(89)

The body exp\expm is not evaluated, so the result is FAIL even when exp\expm has no value.

DeclarationsE⊢dec⇒VE\jdg{E}{\dec}{\VE}
E⊢valbind⇒VEDom VE∩FV(valbind)=∅E⊢valbind⇒VE\frac{\begin{array}{c}\jdg{E}{\valbind}{\VE}\premsep\Dom\VE\cap\dcoreFV(\valbind)=\emptyset\end{array}}{\begin{array}{c}\jdg{E}{\valbind}{\VE}\end{array}}
(90)
E⊢valbind⇒VEDom VE∩FV(valbind)≠∅E⊢valbind⇒Rec VE\frac{\begin{array}{c}\jdg{E}{\valbind}{\VE}\premsep\Dom\VE\cap\dcoreFV(\valbind)\neq\emptyset\end{array}}{\begin{array}{c}\jdg{E}{\valbind}{\Rec\VE}\end{array}}
(91)
Comments:
(90)

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 EE, without the variables the declaration binds; in a recursive declaration every right-hand side is a lambda, and Rec \Rec gives each of its closures the bindings of the whole declaration. Rec \Rec 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 Rec \Rec 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.

Value BindingsE⊢valbind⇒VE\jdg{E}{\valbind}{\VE}
E⊢exp⇒v⟨E⊢valbind⇒VE⟩E⊢var = exp ⟨and valbind⟩⇒{var↦v}⟨+ VE⟩\frac{\begin{array}{c}\jdg{E}{\expm}{v}\premsep\opt{\jdg{E}{\valbind}{\VE}}\end{array}}{\begin{array}{c}\jdg{E}{\var\ \kw{=}\ \expm\ \opt{\kw{and}\ \valbind}}{\{\var\mapsto v\}\opt{+\,\VE}}\end{array}}
(92)
E⊢exp⇒vE,v⊢atpat⇒VEE⊢atpat = exp⇒VE\frac{\begin{array}{c}\jdg{E}{\expm}{v}\premsep\jdgv{E}{v}{\atpat}{\VE}\end{array}}{\begin{array}{c}\jdg{E}{\atpat\ \kw{=}\ \expm}{\VE}\end{array}}
(93)
Comments:
(93)

There is no rule in which atpat\atpat, matched against vv, gives FAIL: the static semantics requires the pattern of a value binding to be exhaustive (Chapter 4).

Atomic PatternsE,v⊢atpat⇒VE/FAIL\jdgv{E}{v}{\atpat}{\VE/\FAIL}
E,v⊢_⇒{}\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{\_}}{\emptymap}\end{array}}
(94)
v=val(scon)E,v⊢scon⇒{}\frac{\begin{array}{c}v=\valof(\scon)\end{array}}{\begin{array}{c}\jdgv{E}{v}{\scon}{\emptymap}\end{array}}
(95)
v≠val(scon)E,v⊢scon⇒FAIL\frac{\begin{array}{c}v\neq\valof(\scon)\end{array}}{\begin{array}{c}\jdgv{E}{v}{\scon}{\FAIL}\end{array}}
(96)
E,v⊢var⇒{var↦v}\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\var}{\{\var\mapsto v\}}\end{array}}
(97)
E(longcon)=conv=conE,v⊢longcon⇒{}\frac{\begin{array}{c}E(\longcon)=\con\premsep v=\con\end{array}}{\begin{array}{c}\jdgv{E}{v}{\longcon}{\emptymap}\end{array}}
(98)
E(longcon)=conv≠conE,v⊢longcon⇒FAIL\frac{\begin{array}{c}E(\longcon)=\con\premsep v\neq\con\end{array}}{\begin{array}{c}\jdgv{E}{v}{\longcon}{\FAIL}\end{array}}
(99)
E,()⊢()⇒{}\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdgv{E}{()}{\kw{()}}{\emptymap}\end{array}}
(100)
v=(v1,v2⟨,v3⟩)E,v1⊢pat1⇒VE1/FAILE,v2⊢pat2⇒VE2/FAIL⟨E,v3⊢pat3⇒VE3/FAIL⟩E,v⊢(pat1, pat2 ⟨, pat3⟩)⇒VE1/FAIL+VE2/FAIL⟨+ VE3/FAIL⟩\frac{\begin{array}{c}v=(v_1,v_2\opt{,v_3})\premsep \jdgv{E}{v_1}{\pat_1}{\VE_1/\FAIL}\\ \jdgv{E}{v_2}{\pat_2}{\VE_2/\FAIL}\premsep \opt{\jdgv{E}{v_3}{\pat_3}{\VE_3/\FAIL}}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{(}\pat_1\kw{,}\ \pat_2\ \opt{\kw{,}\ \pat_3}\kw{)}} {\VE_1/\FAIL+\VE_2/\FAIL\opt{+\,\VE_3/\FAIL}}\end{array}}
(101)
v=[]E,v⊢[ ]⇒{}\frac{\begin{array}{c}v=\kw{[]}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{[}\ \kw{]}}{\emptymap}\end{array}}
(102)
v≠[]E,v⊢[ ]⇒FAIL\frac{\begin{array}{c}v\neq\kw{[]}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{[}\ \kw{]}}{\FAIL}\end{array}}
(103)
v=r⟨E,r⊢patrow⇒VE⟩E,v⊢{ ⟨patrow⟩ }⇒{}⟨+ VE⟩\frac{\begin{array}{c}v=r\premsep\opt{\jdgv{E}{r}{\patrow}{\VE}}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{\kwlb}\ \opt{\patrow}\ \kw{\kwrb}}{\emptymap\opt{+\,\VE}}\end{array}}
(104)
E,v⊢pat⇒VE/FAILE,v⊢(pat)⇒VE/FAIL\frac{\begin{array}{c}\jdgv{E}{v}{\pat}{\VE/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\kw{(}\pat\kw{)}}{\VE/\FAIL}\end{array}}
(105)
Comments:
(95)

A constant in a pattern is an integer, character or string constant, never a floating-point one, so the test v=val(scon)v=\valof(\scon) is identity of special values. An integer constant matches the normalised integer it denotes.

(97)

A variable pattern always binds: constructors and variables are spelt differently (Chapter 2), so no identifier status is consulted.

(100)

For rules (100), (101) and (104): in a program that elaborates the value matched has the form the pattern requires, so no rule for these patterns gives FAIL on its own account; a tuple fails only when a component pattern fails. A record pattern matches a record with any further fields.

Pattern RowsE,r⊢patrow⇒VE\jdgv{E}{r}{\patrow}{\VE}
lab∈Dom r⟨E,r⊢patrow⇒VE⟩E,r⊢lab = var ⟨, patrow⟩⇒{var↦r(lab)}⟨+ VE⟩\frac{\begin{array}{c}\lab\in\Dom r\premsep\opt{\jdgv{E}{r}{\patrow}{\VE}}\end{array}}{\begin{array}{c}\jdgv{E}{r}{\lab\ \kw{=}\ \var\ \opt{\kw{,}\ \patrow}}{\{\var\mapsto r(\lab)\}\opt{+\,\VE}}\end{array}}
(106)
Comments:
(106)

The pattern row binds its variable to the field lab\lab of rr; in a row written as a label alone the variable is spelt as the label (Appendix A). The side condition lab∈Dom r\lab\in\Dom r makes the rule apply exactly when rr 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.

PatternsE,v⊢pat⇒VE/FAIL\jdgv{E}{v}{\pat}{\VE/\FAIL}
E,v⊢atpat⇒VE/FAILE,v⊢atpat⇒VE/FAIL\frac{\begin{array}{c}\jdgv{E}{v}{\atpat}{\VE/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\atpat}{\VE/\FAIL}\end{array}}
(107)
E(longcon)=conv=con v1⋯vnE,v1⊢atpat1⇒VE1/FAIL  ⋯  E,vn⊢atpatn⇒VEn/FAILE,v⊢longcon atpat1⋯atpatn⇒VE1/FAIL+⋯+VEn/FAIL\frac{\begin{array}{c}E(\longcon)=\con\premsep v=\con\ v_1\cdots v_n\\ \jdgv{E}{v_1}{\atpat_1}{\VE_1/\FAIL}\ \ \cdots\ \ \jdgv{E}{v_n}{\atpat_n}{\VE_n/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\longcon\ \atpat_1\cdots\atpat_n} {\VE_1/\FAIL+\cdots+\VE_n/\FAIL}\end{array}}
(108)
E(longcon)=conv∉{con}×ValnE,v⊢longcon atpat1⋯atpatn⇒FAIL\frac{\begin{array}{c}E(\longcon)=\con\premsep v\notin\{\con\}\times\Val^n\end{array}}{\begin{array}{c}\jdgv{E}{v}{\longcon\ \atpat_1\cdots\atpat_n}{\FAIL}\end{array}}
(109)
v=v1::v2E,v1⊢pat1⇒VE1/FAILE,v2⊢pat2⇒VE2/FAILE,v⊢pat1 :: pat2⇒VE1/FAIL+VE2/FAIL\frac{\begin{array}{c}v=v_1\mathbin{\kw{::}}v_2\\ \jdgv{E}{v_1}{\pat_1}{\VE_1/\FAIL}\premsep \jdgv{E}{v_2}{\pat_2}{\VE_2/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat_1\ \kw{::}\ \pat_2}{\VE_1/\FAIL+\VE_2/\FAIL}\end{array}}
(110)
v=[]E,v⊢pat1 :: pat2⇒FAIL\frac{\begin{array}{c}v=\kw{[]}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat_1\ \kw{::}\ \pat_2}{\FAIL}\end{array}}
(111)
E,v⊢pat⇒VE/FAILE,v⊢pat as var⇒VE/FAIL+{var↦v}\frac{\begin{array}{c}\jdgv{E}{v}{\pat}{\VE/\FAIL}\end{array}}{\begin{array}{c}\jdgv{E}{v}{\pat\ \kw{as}\ \var}{\VE/\FAIL+\{\var\mapsto v\}}\end{array}}
(112)
Comments:
(108)

Here n≥1n\geq 1, and by elaboration n=arity(con)n=\arity(\con). 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.

(109)

Rules (96), (99), (103), (109) and (111) are the only rules that give FAIL without a premise giving FAIL; every other FAIL comes from one of them.

(112)

By the syntactic restrictions, var\var is not bound by pat\pat, so the modification adds a binding and replaces none.

Discussions

Discuss this chapter·Search the discussions on GitHub

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