Elm Exactly
A Standard Definition of Elm?Draft version 0.4 / Appendix F

FElm and Standard ML

This appendix compares Standard Elm, as this document defines it, with Standard ML as defined in The Definition of Standard ML (Revised) by Milner, Tofte, Harper and MacQueen (MIT Press, 1997). It defines nothing; each entry refers to the place in this document where the construct is defined.

F.1What Is the Same

The method of this definition is that of the Definition of Standard ML. The language is presented as a grid of phases (syntax, static semantics, dynamic semantics) against levels; each semantics section defines its semantic objects before its rules; the rules infer sentences of the form background, phrase, meaning, one rule group per phrase class, numbered through the whole book and followed by Comments; and the rules are given for a bare language, into which every derived form is translated. Much of the Core coincides as well: type names generated by type declarations, type functions, type schemes and their instances, closure of a type with respect to a context, contexts and environments, function closures whose recursion is expressed by the operation Rec \Rec, matches whose failure is the object FAIL\FAIL, value constructors with their identifier status, and evaluation by value. The differences lie in the effects the dynamic semantics must account for, in the types of records, tuples and overloaded operations, in the modules, and in the syntax, and are listed below.

F.2Differences by Construct

The differences are shown construct by construct in Figures 56 (evaluation), 57 and 58 (types), 59 (modules, programs and effects) and 60 (syntax). In the figures, “Ch.”, “Sec.”, “Fig.” and “App.” refer to chapters, sections, figures and appendices of this document.

FeatureStandard MLElm (this definition)Where defined here
Exceptionsexception declarations, raise and handle; exception names and packets; an exception convention for the dynamic rules.None: no exception names, no packets, no exception convention. Evaluation of a phrase either gives a value or gives none; absence and failure are values of the types Maybe and Result of the initial basis.Ch. 6, Sec. 6.7; Sec. C.4
Incomplete matchesAllowed; a match that fails raises Match, a value binding that fails raises Bind.Not allowed: every match, lambda pattern and destructuring binding must be exhaustive, and every match rule must be needed, at the types of the principal derivation.Sec. 4.11; Sec. 4.10
StateReferences (ref, :=, !), addresses and states; a state convention, under which the order of premises is the order of evaluation.None: sentences carry no state, and no state convention is needed. The order of premises has no meaning; the order of evaluation is observable only through Debug, and is left to the implementation.Sec. 6.7; Sec. E.6
Arithmetic failureOverflow is raised when a result leaves the range of int; Div when a divisor is zero.Total: every Int result is normalised into the implementation’s range, and an operation with no correct result gives a defined one; thus 3 // 0 is 0.Ch. D; Sec. E.1
EqualityEquality types: type variables ''a, an equality attribute on type names; = at a function type does not elaborate.No equality types: == elaborates at every type. When it reaches two functions its result is an unspecified Bool, so evaluation may be non-deterministic.Sec. 4.4; Sec. D.4; Sec. E.4
Figure 56: Standard ML and Elm: Evaluation
FeatureStandard MLElm (this definition)Where defined here
OverloadingA side mechanism (Appendix E of that definition): overloaded identifiers such as + and <, and overloaded special constants, resolved by context, with int as default.Part of the type system: type variables constrained to the classes number\knum, comparable\kcomp, appendable\kapp and compappend\kcapp, generalised and instantiated like others. A number\knum variable that the principal derivation leaves undetermined is taken to be Float before evaluation.Sec. 4.4; Sec. 4.10; Sec. 6.2
RecordsRecord types are closed. The selector #lab\lab and the record pattern wildcard ... require the record type to be determined by the context. No record update.Record types may be extensible, by a row variable ({ r | x : Int }). Field selection, the field accessor .lab\lab and record patterns apply to every record with the fields named. Record update { r | x = e } keeps the type of the record. A pattern row lab\lab stands for lab = var\lab\ \kw{=}\ \var, as in SML.Sec. 4.2; Sec. 4.9; App. A
TuplesA tuple is a record with labels 1, …, nn (n≥2n \geq 2); the unit is the empty record.Tuples of two and three components have type names of their own, and are not records; the unit is a type of its own.Fig. 5; Sec. 4.2
Datatypesdatatype with withtype, datatype replication, abstype; datatypes may be declared locally.Custom types are declared only at the top level of a module. All custom types of a module form one group, and they and the type aliases of the module may mention one another; there is no withtype, replication or abstype. A record alias also declares a constructor.Fig. 10; Sec. A.2; Sec. 5.5
Figure 57: Standard ML and Elm: Types
FeatureStandard MLElm (this definition)Where defined here
Value polymorphismA value restriction: only a non-expansive expression has its type generalised.No value restriction: closure applies to every binding, whatever its expression.Sec. 4.8; Sec. 4.9
Explicit type variablesBound at a value declaration, explicitly (val 'a) or implicitly at the outermost value declaration in which they occur. Type constraints exp\expm : ty\ty on any expression or pattern.No binder: the type variables of an annotation scope over the annotations nested in its definition, and may not escape it. Annotations are given only for definitions and ports.Sec. 4.7
Figure 58: Standard ML and Elm: Polymorphism and Type Variables
FeatureStandard MLElm (this definition)Where defined here
ModulesStructures, signatures and functors; sharing; transparent and opaque signature matching, with realisation and enrichment.Flat modules: a module is a header, imports and top-level declarations, and exports top-level names by an exposing list; a custom type is exposed with or without its constructors. No signatures, functors or sharing; the import relation between modules has no cycle.Ch. 3; Ch. 5, Sec. 5.2; Ch. 7
ProgramsAn interactive top level: a sequence of top-level declarations, each elaborated and evaluated in the basis left by those before it; a declaration that fails is rejected and the session continues.A program is a main module and the modules it imports, elaborated as a whole; its value main is executed by a platform, as a labelled transition system whose observable behaviour is its traffic on ports.Ch. 9, Sec. 9.4
EffectsAt the level of the language, only state and exceptions; input and output are left to library functions with side effects.Evaluation has no effect. Tasks, commands and subscriptions are values, built by functions of the basis, which the platform carries out; their results return to the program as messages.Ch. 8, Sec. 8.4, Sec. 8.5
PortsNone.A port declaration in a port module gives a command for sending values out or a subscription for receiving them. Only values of port types may cross a port, encoded as JSON values in which byte sequences may stand.Sec. 5.4; Sec. 8.7
Figure 59: Standard ML and Elm: Modules, Programs and Effects
FeatureStandard MLElm (this definition)Where defined here
Infixed operatorsinfix, infixr and nonfix declarations in programs, scoped like other declarations; op removes infix status.The fixities are fixed by the initial basis and by library modules; no program declares an operator. Some operators are non-associative. The operator value (op\opv) uses an operator as a function.Sec. 2.7, Fig. 3
LayoutInsensitive to layout; phrases are delimited by reserved words such as end and by ;.Sensitive to indentation: match rules, the definitions of a let and the declarations of a module are separated by layout, and ; and and are abstract syntax.Sec. 2.6; Sec. 3.4
Recursionval rec and fun with and group recursive bindings as written; each declaration is in scope only after it.The definitions of a let and the declarations of a module stand in any order and are grouped, by derived forms, into components of the “refers to” relation in dependency order. In a let, recursion passes only through definitions with parameters.Sec. A.1; Sec. A.2; Sec. 2.10
Functionsfn takes a match; fun defines a function by several clauses.A lambda expression takes atomic patterns and has one body; a function binding has one clause; several cases are written with case.Fig. 5; App. A
Figure 60: Standard ML and Elm: Syntax

Standard ML constructs without a counterpart here are the sequential expression, while, local, open, abstype and the declarations of exceptions; the nearest to open is the exposing list of an import (Section 3.5). Elm constructs without a counterpart in Standard ML are record update and extensible record types, the field accessor, the constrained classes of type variables, and ports.

Discussions

Discuss this chapter·Search the discussions on GitHub

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