Elm Exactly

Preface

Until now the meaning of an Elm program has been settled in practice by what one implementation does with it: the Elm compiler for JavaScript, together with the JavaScript kernel of its core library. The language is described in prose, in a guide and in the documentation of its libraries, and that description serves a programmer learning the language well. It serves less well three other readers. An implementer must decide, for every construction, what the program means, and cannot always tell from the existing implementation whether a behaviour is the language’s or the accident of a host. A programmer who wants to show that two programs are equivalent, or that a function never fails, needs a meaning precise enough to argue from. And two parties who must agree on what a given program does need a text that both accept as the authority.

This book is a formal definition of both the grammar and the meaning of Elm, version 0.19.1. The occasion for it is a second implementation, eco, which compiles Elm to native code and has its own runtime. Two implementations on different hosts give different answers wherever an answer was a consequence of the host rather than of the language: the range of integers, what a character is, what happens when an integer is divided by zero. Once there are two, each such question must be answered once, independently of both, and precisely enough that either implementation can be checked against the answer. We call the language so defined Standard Elm.

Standard Elm

Elm is a functional language for writing interactive programs that run under the control of a host, first of all a web browser. It is pure: a phrase denotes a value, and evaluating it changes nothing, since the language has no assignable variables, no mutable data and no input or output. It is strict: the argument of a function is evaluated before the function is applied. It is statically typed, with types inferred and polymorphism introduced at definitions. In its semantics it is extremely close to ML: values, closures, custom types with constructors, pattern matching and polymorphic type inference behave as they do there, and where the two languages agree this definition gives Elm the meaning ML has. Five features distinguish Elm from ML and shape the definition. First, Elm has no exceptions and pursues the aim that a program which has been accepted does not fail when it runs. No phrase raises or handles an exception, and the definition gives every phrase that has elaborated either a value or none. A phrase has no value only when its evaluation does not terminate, when it needs a top-level value while that value is itself being evaluated, or when it reaches Debug.todo, the one escape the language provides. Arithmetic is accordingly total: where an operation has no mathematically correct result, the definition gives a defined answer in its place, so that 3 // 0 is 0. The basic arithmetic and comparison operations are defined in this book rather than left to a library. Second, records are row-polymorphic: a function may demand some fields of a record and accept any record that has them, and a record type may end in a row variable standing for the fields not named. Third, a type variable may be constrained to a class (number, comparable, appendable or compappend) and then stands only for the few predefined types on which arithmetic, ordering or appending is defined. The classes are fixed: a program cannot add types to them. They take the place of ML’s overloaded identifiers and equality types, and equality itself is defined at every type. Fourth, the module system is deliberately simple. A module is a sequence of top-level declarations and makes some of them available to other modules by name; there are no signatures, functors or sharing. Fifth, effects are managed. A program never performs an effect. It computes values that describe effects (commands, subscriptions and tasks) and hands them to the platform that runs it; the platform performs them and returns their outcomes to the program as messages, one at a time. Commands, subscriptions and tasks are defined as types of the basis rather than as syntax, but they are integral to the language and have a chapter of their own. A program communicates with its host through ports, which it declares; port is a reserved word, port declarations belong to the language, and the definition states exactly which values can and cannot be sent over a port. Standard Elm leaves out what Elm reserves to its own core packages: effect modules, declarations of new infix operators and references to kernel code. It also leaves out embedded shader blocks, which belong to a graphics extension rather than to the language.

Language Definition

The words of a programming language (type, case, port, module) name things that are not phrases: types, values, functions, interfaces, commands. These things are the semantic objects of the language, and they, not its syntax, carry its character; when programmers think about an Elm program they think in terms of them. For Standard Elm the semantic objects include types with rows and classes, type schemes, environments and module interfaces on the one hand, and values, closures, records, tasks, commands and the boundary values (JSON values, with byte sequences) that cross a port on the other. A definition of the language must therefore be a definition in terms of these objects. They must be described in a notation independent of Elm, since to describe them in another programming language would only move the question to that language; ordinary mathematics serves. And they must be given enough theory (substitution, instantiation, closure, the properties on which the rules rely) for the rules to make sense, as a mathematician develops the theory of the objects a subject is about.

With the objects in hand, the definition must say what meaning each phrase of the language has. Here the keystone of our method is that a phrase never has a meaning in isolation. It always has a meaning against a background, which is itself a semantic object: a distillation of what earlier phrases have contributed, such as the types and values of the names in scope and the interfaces of the modules imported. The meaning of a phrase depends on its background as much as on the phrase. The basic assertion of the definition is therefore a sentence of the form

B⊢P⇒M, \jdg{B}{P}{M},

read “against the background BB, the phrase PP has the meaning MM”. The formal purpose of this definition is exactly to decree which sentences of this form hold, and no more. It does so by rules, each of which allows a sentence about a compound phrase to be inferred from sentences about its constituent phrases, so that the meaning of a phrase is determined by the meanings of its parts. The rules are numbered through the whole book, and each group of them is followed by Comments on the points that are not obvious.

The meaning of an Elm phrase has two parts: what can be known of it before it is evaluated, namely whether it is well formed and what its type is, and the value it evaluates to. We give the two separately. The static semantics infers types and interfaces, and the dynamic semantics infers values; each is an instance of the keystone, with its own backgrounds and its own meanings. The factoring is genuine. A value never has to be presented together with its type, and the dynamic semantics consults no type except where a port must know the type of what it sends or receives. A phrase that does not elaborate has no meaning at all, and the dynamic semantics need not consider it.

This Definition

This book follows The Definition of Standard ML (Revised) closely, in its structure, its notation and its style: a grid of phases against levels, a bare language with derived forms, semantic objects defined in figures, numbered inference rules with Comments, and an initial basis in appendices. Where Elm agrees with ML we have followed the treatment of that book, so that a reader who knows it will find most of the static semantics of the Core, and much of its dynamic semantics, in familiar form. The additions are the row variables and classes of the static semantics, the much smaller static semantics of modules, a chapter on tasks, commands, subscriptions and ports, and a chapter on programs which defines how the platform runs a program as a labelled transition system whose observable behaviour is its traffic on ports. Appendix F compares the two definitions construct by construct.

Two conventions of the Definition of Standard ML are absent here. There, every sentence of the dynamic semantics carries a state, for references, and the state is threaded through the premises of every rule, so that the order of the premises is the order of evaluation; and for every rule there are further implicit rules by which an exception packet raised in a premise becomes the result of the conclusion. Standard Elm has neither state nor exceptions, so neither convention is needed. Its dynamic sentences carry no state, the order of premises carries no meaning, and no rule produces a packet. The body of the definition does not settle everything. Where implementations legitimately differ (the range of integers, what a character is, the treatment of floating-point special values, the order in which the commands of a batch are performed), the body states only what every implementation must satisfy, through abstract parameters, and Appendix E records the choices of Elm on JavaScript and of eco, with their consequences for programs. Elsewhere the two implementations disagreed, or agreed on a behaviour that followed from their hosts rather than from the language, and the definition had to decide. Each such decision is a ruling, taken in the spirit of the language: no run-time errors, total arithmetic, and a meaning that does not depend on how a program is optimised. Some rulings declare existing behaviour non-conforming. On Elm on JavaScript, modBy 0 x stops the program and comparing two functions with == stops it too, where the definition gives 0 and a Boolean; on eco, a negative power of 1 or -1 at Int is 0 where the definition gives 1, or 1 or -1 by the parity of the exponent; on both, a value containing a NaN may be equal to itself, where the definition says that it is not. Appendix E lists every such point with the ruling that settles it.

How this Definition was Produced

This definition was written with the assistance of an AI system, Claude (model Claude Opus 5.5, made by Anthropic), working within a staged, documented workflow. Under the author’s direction it surveyed the sources and test suites of the two implementations, Elm 0.19.1 and eco, recording each fact with a citation to the code or test that establishes it; it proposed a ruling, with its reasons, wherever the implementations disagree or are silent; it drafted the chapters and appendices; it audited the drafts, in independent passes, for consistency between chapters; and it encoded the dynamic semantics of the Core, of modules, of effects and of programs in the proof assistant Lean 4, namely every rule of Chapters 6, 7 and 8, the dynamic rules and the transitions of programs of Chapter 9, and the arithmetic, equality, order, Boolean, conversion, elementary-function, append and character part of APPLY\APPLY, with an evaluator proved sound and complete, and a scheduler proved sound, with respect to those rules, and checked against the expected results of 702 of the test programs that accompany this book, which a program translates into the encoding from their source, each as the program of modules it is; it also encoded the static semantics, every rule of Chapters 4 and 5 and the static rules of Chapter 9, with the derived forms of Appendix A, the syntactic restrictions that can be stated on a syntax tree, and Appendix C, with a checker of derivations proved sound and a type inference proved sound and principal, by which 702 of the test programs are shown legal and 287 of the 315 that must be refused are shown not legal (the parser the encoding starts from rejects the others, or reads them in a form that has lost the error); and for each of the 702 translated programs, Lean computes from its source, through that inference, number specialisation and type erasure, the program the translator writes, and shows that this specialisation is the only one. The author set the directions the definition follows, reviewed every ruling, and is responsible for the whole; the AI system is not an author. The survey, the record of rulings, the audit reports and the mechanisation are kept with the sources of this book.

Acknowledgements

Elm was designed by Evan Czaplicki, and the language defined here is his. The form of this book is that of The Definition of Standard ML by Robin Milner, Mads Tofte, Robert Harper and David MacQueen; we are indebted to them for a method which has served a second language with little change.

Rupert Smith
November 2026

Discussions

Discuss this chapter·Search the discussions on GitHub

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