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

4Static Semantics for the Core

This chapter defines the semantic objects of the static semantics of the Core and the rules by which Core phrases elaborate. The simple objects are type variables, each with a class, row variables, type names and identifier status descriptors. From them we build types, record types, type functions, type schemes, environments and contexts, and on these we define substitution, instantiation and closure. The rules infer sentences C⊢phrase⇒A\jdg{C}{\mathit{phrase}}{A}, in which CC is a context and AA is a type, a row, a value environment, or a value environment paired with a type. A Core phrase is legal when it satisfies the syntactic restrictions of Chapter 2, elaborates by the rules of Section 4.9, and its principal derivation (Section 4.10) meets the further restrictions of Section 4.11.

4.1Simple Objects

All semantic objects of the static semantics are built from identifiers (Chapter 2), labels among them, and four further kinds of simple object: type variables, row variables, type names and identifier status descriptors, together with the classes that type variables carry. Type names are the values taken by type constructors; they are distinct from type variables and from type constructors. The simple object classes, and the metavariables ranging over them, are shown in Figure 12. We include TyVar\TyVar in the figure to show that α\alpha ranges over it.

α or tyvar\alpha\ \text{or}\ \tyvar∈\inTyVar\TyVartype variables
ϵ\epsilon∈\inRowVar\RowVarrow variables
tt∈\inTyName\TyNametype names
κ\kappa∈\inClass\Classclasses
is\is∈\inIdStatus={c,v}\IdStatus = \{\isc, \isv\}identifier status descriptors
Figure 12: Simple Semantic Objects

The classes are

Class={any,number,comparable,appendable,compappend}. \Class = \{\kany, \knum, \kcomp, \kapp, \kcapp\}.

Each α∈TyVar\alpha \in \TyVar has a class class(α)∈Class\clsof(\alpha) \in \Class, and there are infinitely many type variables of each class. A row variable has no class. Each t∈TyNamet \in \TyName has an arity k≥0k \ge 0; we write TyName(k)\TyName^{(k)} for the type names of arity kk.

A type variable tyvar\tyvar of the syntax names one type variable, also written tyvar\tyvar, whose class is fixed by the beginning of its name: number\knum if the name begins with number, comparable\kcomp if it begins with comparable, appendable\kapp if it begins with appendable, compappend\kcapp if it begins with compappend, and any\kany otherwise. When that class is any\kany, tyvar\tyvar also names one row variable; a type variable of the syntax of any other class names no row variable. We write tyvar=α\tyvar = \alpha and tyvar=ϵ\tyvar = \epsilon when α\alpha, respectively ϵ\epsilon, is the variable named by tyvar\tyvar. No other type or row variable is named by a type variable of the syntax.

A label and a value variable may be spelt alike. For a label lab\lab we write var=lab\var = \lab when var\var is spelt as lab\lab.

The rules mention a few type names directly. They are shown in Figure 13; they belong to the initial basis (Appendix C), and a rule that names one of them means that type name, whatever a program binds to the type constructor of the same spelling.

Int, Float\tInt,\ \tFloat∈\inTyName(0)\TyName^{(0)}integers, floats
Char, String\tChar,\ \tString∈\inTyName(0)\TyName^{(0)}characters, strings
()\tunit∈\inTyName(0)\TyName^{(0)}unit
List\tList∈\inTyName(1)\TyName^{(1)}lists
Tuple2\tTuple{2}∈\inTyName(2)\TyName^{(2)}pairs
Tuple3\tTuple{3}∈\inTyName(3)\TyName^{(3)}triples
Figure 13: Built-in Type Names

4.2Compound Objects

When AA and BB are sets, Fin A\Fin A denotes the set of finite subsets of AA, and A→finBA \finmap B denotes the set of finite maps (partial functions with finite domain) from AA to BB. The domain and range of a finite map ff are denoted Dom f\Dom f and Ran f\Ran f. A finite map is written explicitly as {a1↦b1,⋯ ,ak↦bk}\{a_1 \mapsto b_1, \cdots, a_k \mapsto b_k\}, k≥0k \ge 0; in particular the empty map is {}\emptymap. We write {x↦e ; ϕ}\{x \mapsto e \,;\, \phi\} for the finite map ff whose domain is the set of xx satisfying the condition ϕ\phi and whose value on that domain is f(x)=ef(x) = e. A finite map is regarded as the set of its pairs, so f⊆gf \subseteq g means that Dom f⊆Dom g\Dom f \subseteq \Dom g and that gg agrees with ff on Dom f\Dom f.

When ff and gg are finite maps, the map f+gf + g, called ff modified by gg, is the finite map with domain Dom f∪Dom g\Dom f \cup \Dom g and values

(f+g)(a)=if a∈Dom g then g(a) else f(a). (f + g)(a) = \text{if } a \in \Dom g \text{ then } g(a) \text{ else } f(a).

The compound objects of the static semantics of the Core are shown in Figure 14. We take ∪\cup to mean disjoint union over semantic object classes, and we understand all the defined object classes to be disjoint. The classes Type\Type, Row\Row, RecType\RecType, FunType\FunType and ConsType\ConsType are the smallest sets satisfying their equations; hence every type is finite, and no type occurs within itself. A sequence (τ1,⋯ ,τk)(\tau_1, \cdots, \tau_k) of types is also written τ(k)\tau^{(k)}, and a sequence (ξ1,⋯ ,ξk)(\tyrowvar_1, \cdots, \tyrowvar_k) of type and row variables ξ(k)\tyrowvar^{(k)}; the metavariable ξ\tyrowvar ranges over TyVar∪RowVar\TyVar \cup \RowVar. In a context, T∈Fin (TyName)T \in \Fin(\TyName) and U∈Fin (TyVar∪RowVar)U \in \Fin(\TyVar \cup \RowVar).

τ\tau∈\inType\Type=TyVar∪RecType∪FunType∪ConsType= \TyVar \cup \RecType \cup \FunType \cup \ConsType
ϱ\varrho∈\inRow\Row=Lab→finType= \cls{Lab} \finmap \Type
{ϱ} or {ϱ∣ϵ}\rec{\varrho}\ \text{or}\ \orec{\varrho}{\epsilon}∈\inRecType\RecType=Row×(RowVar∪{∅})= \Row \times (\RowVar \cup \{\emptyrow\})
τ→τ′\tau \rightarrow \tau'∈\inFunType\FunType=Type×Type= \Type \times \Type
∈\inConsType\ConsType=⋃k≥0ConsType(k)= \textstyle\bigcup_{k \ge 0} \ConsType^{(k)}
τ(k)t\tau^{(k)} t∈\inConsType(k)\ConsType^{(k)}=Typek×TyName(k)= \Type^k \times \TyName^{(k)}
θ or Λξ(k).τ\theta\ \text{or}\ \Lambda\tyrowvar^{(k)}.\tau∈\inTypeFcn\TypeFcn=⋃k≥0(TyVar∪RowVar)k×Type= \textstyle\bigcup_{k \ge 0} (\TyVar \cup \RowVar)^k \times \Type
σ or ∀ξ(k).τ\sigma\ \text{or}\ \forall\tyrowvar^{(k)}.\tau∈\inTypeScheme\TypeScheme=⋃k≥0(TyVar∪RowVar)k×Type= \textstyle\bigcup_{k \ge 0} (\TyVar \cup \RowVar)^k \times \Type
(θ,VE)(\theta, \VE)∈\inTyStr\TyStr=TypeFcn×ValEnv= \TypeFcn \times \ValEnv
TE\TE∈\inTyEnv\TyEnv=TyCon→finTyStr= \cls{TyCon} \finmap \TyStr
VE\VE∈\inValEnv\ValEnv=(Var∪Con)→finTypeScheme×IdStatus= (\cls{Var} \cup \cls{Con}) \finmap \TypeScheme \times \IdStatus
QE\QE∈\inQualEnv\QualEnv=ModId→finEnv= \cls{ModId} \finmap \Env
E or QE,TE,VEE\ \text{or}\ \QE, \TE, \VE∈\inEnv\Env=QualEnv×TyEnv×ValEnv= \QualEnv \times \TyEnv \times \ValEnv
C or T,U,EC\ \text{or}\ T, U, E∈\inContext\Context=Fin (TyName)×Fin (TyVar∪RowVar)= \Fin(\TyName) \times \Fin(\TyVar \cup \RowVar)
×Env\quad{}\times \Env
Figure 14: Compound Semantic Objects

A record type is a pair of a row and either a row variable or ∅\emptyrow. The record type (ϱ,∅)(\varrho, \emptyrow), written {ϱ}\rec{\varrho}, is closed: its fields are exactly those of ϱ\varrho. The record type (ϱ,ϵ)(\varrho, \epsilon), written {ϱ∣ϵ}\orec{\varrho}{\epsilon}, is open: its fields are those of ϱ\varrho together with those that ϵ\epsilon stands for, none of which has a label in Dom ϱ\Dom\varrho (Section 4.5). The unit type is the constructed type ()()()\tunit, and the type of kk-tuples with components τ1,⋯ ,τk\tau_1, \cdots, \tau_k is (τ1,⋯ ,τk)Tuplek(\tau_1, \cdots, \tau_k)\tTuple{k}. When tt has arity 0 we write tt for the type ()t()t; thus Int\tInt, ()\tunit and so on are types.

Note that Λ\Lambda and ∀\forall bind type and row variables. For any semantic object AA, tynames A\tynames A and tyvars A\tyvars A denote respectively the set of type names and the set of type and row variables occurring free in AA. For a context we take tyvars C=tyvars (EofC)∪(UofC)\tyvars C = \tyvars(E \of C) \cup (U \of C). For a type expression ty\ty, tyvars ty\tyvars \ty denotes the set of type variables of the syntax occurring in ty\ty.

A value environment maps value variables and value constructors to a pair of a type scheme and an identifier status. If VE(var)=(σ,is)\VE(\var) = (\sigma, \is) or VE(con)=(σ,is)\VE(\con) = (\sigma, \is), we say that var\var, respectively con\con, has status is\is in VE\VE. Value variables have status v\isv. The constructors of custom types have status c\isc, and only they may occur in patterns; the constructor that a record type alias defines has status v\isv (Chapter 5). We take Con\cls{Con} to contain, besides the constructors spelt by identifiers, the two list constructor names [] and ::, which no identifier spells; the initial basis binds them with status c\isc to ∀α.α List\forall\alpha.\alpha\,\tList and ∀α.α→α List→α List\forall\alpha.\alpha \rightarrow \alpha\,\tList \rightarrow \alpha\,\tList, where class(α)=any\clsof(\alpha) = \kany (Appendix C).

A qualified environment maps each qualifier to the environment of the names it qualifies; every environment in the range of a qualified environment has the empty qualified environment. In a context C=T,U,EC = T, U, E, TT is the set of type names generated so far, and UU is the set of explicit type and row variables in scope (Section 4.7).

4.3Projection, Injection and Modification

Projection: We often select a component of a tuple of objects, and we rely on metavariable names to say which component. For instance “VEofE\VE \of E” means the value environment component of EE, and, for a record type τ\tau, “ϱofτ\varrho \of \tau” means its row.

When a tuple contains a finite map we apply the tuple to an argument, relying on the syntactic class of the argument to select the map. Thus C(var)C(\var) means (VEof(EofC))(var)(\VE \of (E \of C))(\var), and likewise C(con)C(\con); C(tycon)C(\tycon) means (TEof(EofC))(tycon)(\TE \of (E \of C))(\tycon). A long identifier is looked up through the qualified environment: if longvar=modid.var\longvar = \modid.\var then E(longvar)E(\longvar) means (VEof(QEofE)(modid))(var)(\VE \of (\QE \of E)(\modid))(\var), and likewise for longcon\longcon and longtycon\longtycon; an unqualified longvar\longvar is a var\var. C(longvar)C(\longvar) means (EofC)(longvar)(E \of C)(\longvar). 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 bindings of the initial basis (Appendix C); the constructor :: is bound in the initial environment E0E_0 of that appendix, from which every module is elaborated. Operators are not in the domain of an environment, and C(op)C(\opv) is not defined. Since no program declares an operator, the operators are fixed by the initial basis: Appendix C gives the static operator table

OTSTAT:Op→finTypeScheme×IdStatus, \OTstat : \cls{Op} \finmap \TypeScheme \times \IdStatus ,

which maps each operator of Figure 3 to the type scheme and status of the identifier that the operator stands for; in particular OTSTAT(::)\OTstat(\kw{::}) is the type scheme of the list constructor ::, with status c\isc. The operators of library modules outside the initial basis are outside the definition. Injection: Components may be injected into tuple classes; for example, “VE\VE in Env\Env” means the environment ({},{},VE)(\emptymap, \emptymap, \VE).

Modification: The modification of one map by another has been defined above. It is extended to environments component by component, E+E′E + E', and empty components are left implicit: E+VEE + \VE means E+({},{},VE)E + (\emptymap, \emptymap, \VE). For set components modification means union, so that C+(T,U,VE)C + (T, U, \VE) means

((TofC)∪T, (UofC)∪U, (EofC)+VE), \big((T \of C) \cup T,\ (U \of C) \cup U,\ (E \of C) + \VE\big),

and C+VEC + \VE and C+UC + U leave the other components unchanged. Finally, C⊕TEC \oplus \TE means C+(tynames TE,TE)C + (\tynames \TE, \TE): the type environment is added and TT is extended with its type names.

4.4Classes and Admissible Types

The classes are ordered by ≤\leq, the least partial order on Class\Class such that

number≤comparable,compappend≤comparable,compappend≤appendable,κ≤any for every κ∈Class. \begin{array}{c} \knum \leq \kcomp, \qquad \kcapp \leq \kcomp,\\ \kcapp \leq \kapp, \qquad \kappa \leq \kany\ \text{for every}\ \kappa \in \Class. \end{array}

For each class κ\kappa, the set Admκ⊆Type\Adm{\kappa} \subseteq \Type of types admissible for κ\kappa is defined in Figure 15; Admcomparable\Adm{\kcomp} is the smallest set satisfying its equation. A type variable of class κ\kappa may stand only for a type in Admκ\Adm{\kappa} (Section 4.5). A type variable belongs to Admκ\Adm{\kappa} exactly when its class is at most κ\kappa; no record type and no function type is admissible for any class but any\kany.

Admany = TypeAdmnumber = {Int,Float}∪{α ; class(α)=number}Admcomparable = {Int,Float,Char,String} ∪ {α ; class(α)≤comparable} ∪ {τ List ; τ∈Admcomparable} ∪ {(τ1,τ2)Tuple2 ; τ1,τ2∈Admcomparable} ∪ {(τ1,τ2,τ3)Tuple3 ; τ1,τ2,τ3∈Admcomparable}Admappendable = {String}∪{τ List ; τ∈Type} ∪ {α ; class(α)≤appendable}Admcompappend = {String}∪{τ List ; τ∈Admcomparable} ∪ {α ; class(α)=compappend} \begin{array}{rcl} \Adm{\kany} {\ }& = {\ }& \Type\\[2pt] \Adm{\knum} {\ }& = {\ }& \{\tInt, \tFloat\} \cup \{\alpha \,;\, \clsof(\alpha) = \knum\}\\[2pt] \Adm{\kcomp} {\ }& = {\ }& \{\tInt, \tFloat, \tChar, \tString\}\\ {\ }& \cup {\ }& \{\alpha \,;\, \clsof(\alpha) \leq \kcomp\}\\ {\ }& \cup {\ }& \{\tau\,\tList \,;\, \tau \in \Adm{\kcomp}\}\\ {\ }& \cup {\ }& \{(\tau_1, \tau_2)\tTuple{2} \,;\, \tau_1, \tau_2 \in \Adm{\kcomp}\}\\ {\ }& \cup {\ }& \{(\tau_1, \tau_2, \tau_3)\tTuple{3} \,;\, \tau_1, \tau_2, \tau_3 \in \Adm{\kcomp}\}\\[2pt] \Adm{\kapp} {\ }& = {\ }& \{\tString\} \cup \{\tau\,\tList \,;\, \tau \in \Type\}\\ {\ }& \cup {\ }& \{\alpha \,;\, \clsof(\alpha) \leq \kapp\}\\[2pt] \Adm{\kcapp} {\ }& = {\ }& \{\tString\} \cup \{\tau\,\tList \,;\, \tau \in \Adm{\kcomp}\}\\ {\ }& \cup {\ }& \{\alpha \,;\, \clsof(\alpha) = \kcapp\} \end{array}
Figure 15: Admissible Types

There is no class of types that admit equality. The initial basis gives the equality operators the type scheme ∀α.α→α→Bool\forall\alpha.\alpha \rightarrow \alpha \rightarrow \tBool with class(α)=any\clsof(\alpha) = \kany (Appendix C), so equality elaborates at every type, function types included.

4.5Types and Type Functions

Tested by: CompilerIssue2182ExtensibleFunctionAliasCrashTest

Substitution. Let ξ(k)\tyrowvar^{(k)} be distinct type and row variables, and τ(k)\tau^{(k)} types such that τi∈RecType\tau_i \in \RecType whenever ξi∈RowVar\tyrowvar_i \in \RowVar. For a type τ\tau, the type τ{τ(k)/ξ(k)}\tau\{\tau^{(k)} / \tyrowvar^{(k)}\} is the result of replacing each ξi\tyrowvar_i by τi\tau_i throughout τ\tau. Writing τ{⋅}\tau\{\cdot\} for τ{τ(k)/ξ(k)}\tau\{\tau^{(k)} / \tyrowvar^{(k)}\}, and ϱ{⋅}\varrho\{\cdot\} for {lab↦ϱ(lab){⋅} ; lab∈Dom ϱ}\{\lab \mapsto \varrho(\lab)\{\cdot\} \,;\, \lab \in \Dom\varrho\}, it is defined by

α{⋅} = τiif α=ξiα{⋅} = αif α∉{ξ1,⋯ ,ξk}{ϱ}{⋅} = {ϱ{⋅}}{ϱ∣ϵ}{⋅} = {ϱ{⋅}+ϱ′}if ϵ=ξi and τi={ϱ′}{ϱ∣ϵ}{⋅} = {ϱ{⋅}+ϱ′∣ϵ′}if ϵ=ξi and τi={ϱ′∣ϵ′}{ϱ∣ϵ}{⋅} = {ϱ{⋅}∣ϵ}if ϵ∉{ξ1,⋯ ,ξk}(τ→τ′){⋅} = τ{⋅}→τ′{⋅}(τ1′,⋯ ,τl′)t{⋅} = (τ1′{⋅},⋯ ,τl′{⋅})t \begin{array}{rll} \alpha\{\cdot\} {\ =\ }& \tau_i {\qquad}& \text{if}\ \alpha = \tyrowvar_i\\ \alpha\{\cdot\} {\ =\ }& \alpha {\qquad}& \text{if}\ \alpha \notin \{\tyrowvar_1, \cdots, \tyrowvar_k\}\\ \rec{\varrho}\{\cdot\} {\ =\ }& \rec{\varrho\{\cdot\}} {\qquad}&\\ \orec{\varrho}{\epsilon}\{\cdot\} {\ =\ }& \rec{\varrho\{\cdot\} + \varrho'} {\qquad}& \text{if}\ \epsilon = \tyrowvar_i\ \text{and}\ \tau_i = \rec{\varrho'}\\ \orec{\varrho}{\epsilon}\{\cdot\} {\ =\ }& \orec{\varrho\{\cdot\} + \varrho'}{\epsilon'} {\qquad}& \text{if}\ \epsilon = \tyrowvar_i\ \text{and}\ \tau_i = \orec{\varrho'}{\epsilon'}\\ \orec{\varrho}{\epsilon}\{\cdot\} {\ =\ }& \orec{\varrho\{\cdot\}}{\epsilon} {\qquad}& \text{if}\ \epsilon \notin \{\tyrowvar_1, \cdots, \tyrowvar_k\}\\ (\tau \rightarrow \tau')\{\cdot\} {\ =\ }& \tau\{\cdot\} \rightarrow \tau'\{\cdot\} {\qquad}&\\ (\tau'_1, \cdots, \tau'_l)t\{\cdot\} {\ =\ }& (\tau'_1\{\cdot\}, \cdots, \tau'_l\{\cdot\})t {\qquad}& \end{array}

The result is defined only if, wherever a row variable is replaced, Dom ϱ∩Dom ϱ′=∅\Dom\varrho \cap \Dom\varrho' = \emptyset. Thus a row variable can never give a record type a second field with a label it already has. The substitution respects classes if τi∈Admclass(ξi)\tau_i \in \Adm{\clsof(\tyrowvar_i)} for each ξi∈TyVar\tyrowvar_i \in \TyVar. Substitution is extended to rows, as above, and to other objects in the obvious way, renaming bound variables where necessary.

The rules rely on the following property, which may easily be demonstrated.

If κ≤κ′\kappa \leq \kappa' then Admκ⊆Admκ′\Adm{\kappa} \subseteq \Adm{\kappa'}. If τ∈Admκ\tau \in \Adm{\kappa}, and τ{τ(k)/ξ(k)}\tau\{\tau^{(k)}/\tyrowvar^{(k)}\} is defined and respects classes, then τ{τ(k)/ξ(k)}∈Admκ\tau\{\tau^{(k)}/\tyrowvar^{(k)}\} \in \Adm{\kappa}.

Type functions. A type function θ=Λξ(k).τ\theta = \Lambda\tyrowvar^{(k)}.\tau has arity kk; the bound variables must be distinct. Two type functions are equal if they differ only in their choice of bound variables, a type variable being renamed to a type variable and a row variable to a row variable. The class of a bound type variable of a type function has no significance. If tt has arity kk, then we write tt for the type function Λα(k).α(k)t\Lambda\alpha^{(k)}.\alpha^{(k)}t, where the αi\alpha_i are distinct type variables; thus TyName⊆TypeFcn\TyName \subseteq \TypeFcn.

The application of θ=Λξ(k).τ\theta = \Lambda\tyrowvar^{(k)}.\tau to types τ(k)\tau^{(k)} is

θ(τ(k))=τ{τ(k)/ξ(k)}, \theta(\tau^{(k)}) = \tau\{\tau^{(k)}/\tyrowvar^{(k)}\},

defined when the substitution is defined; it need not respect classes. In particular a parameter of θ\theta that is a row variable accepts only a record type.

4.6Type Schemes

A type scheme σ=∀ξ(k).τ\sigma = \forall\tyrowvar^{(k)}.\tau generalises a type τ′\tau', written σ≻τ′\sigma \succ \tau', if τ′=τ{τ(k)/ξ(k)}\tau' = \tau\{\tau^{(k)}/\tyrowvar^{(k)}\} for some τ(k)\tau^{(k)} for which the substitution is defined and respects classes; τ′\tau' is then an instance of σ\sigma. If σ′=∀ξ′(l).τ′\sigma' = \forall\tyrowvar'^{(l)}.\tau', then σ\sigma generalises σ′\sigma', written σ≻σ′\sigma \succ \sigma', if σ≻τ′\sigma \succ \tau' and no ξj′\tyrowvar'_j occurs free in σ\sigma. By the property of Section 4.5, if σ≻σ′\sigma \succ \sigma' and σ′≻τ′′\sigma' \succ \tau'' then σ≻τ′′\sigma \succ \tau''.

Two type schemes are equal if they can be obtained from each other by renaming and reordering bound variables, and by deleting bound variables that do not occur in the body, other than the variables of class number\knum that closure adds to the scheme of a binding of a group (Section 4.8), which are kept. of the same class and a row variable by a row variable. We consider a type τ\tau to be a type scheme, identifying it with ∀().τ\forall().\tau. For a finite set UU of type and row variables, ∀U.τ\forall U.\tau means ∀ξ(k).τ\forall\tyrowvar^{(k)}.\tau where ξ(k)\tyrowvar^{(k)} is any enumeration of UU.

With each special constant scon\scon we associate a type scheme type(scon)\typeof(\scon): it is ∀α.α\forall\alpha.\alpha, where class(α)=number\clsof(\alpha) = \knum, if scon\scon is an integer constant, and the type Float\tFloat, Char\tChar or String\tString if scon\scon is a floating point, character or string constant. Thus the instances of type(scon)\typeof(\scon) for an integer constant are the types in Admnumber\Adm{\knum}.

4.7Scope of Explicit Type Variables

The type variables written in a type annotation are explicit: within the annotated definition each stands for one type or row variable that the definition may not instantiate. Explicit variables in scope are recorded in UofCU \of C.

For a set UU of type and row variables, names U\names U is the set of type variables tyvar\tyvar of the syntax such that tyvar=ξ\tyvar = \tyrowvar for some ξ∈U\tyrowvar \in U. The set UU is unambiguous if no tyvar\tyvar names two members of UU. We consider only contexts CC in which UofCU \of C is unambiguous; the rules preserve this.

Let ty\ty be the type expression of an annotation elaborated in context CC. A set U′U' of type and row variables is new for ty\ty in CC if

names U′=(tyvars ty)∖names (UofC) and(UofC)∪U′ is unambiguous. \begin{array}{l} \names U' = (\tyvars \ty) \setminus \names(U \of C)\ \text{and}\\ (U \of C) \cup U'\ \text{is unambiguous}. \end{array}

Thus U′U' holds, for each type variable of ty\ty that is not already explicit in CC, the type variable or the row variable that it names: the row variable when ty\ty uses it as the extension of a record type ({ r | … }), and the type variable otherwise. The rules for type expressions accept only variables of UofCU \of C, and both kinds could elaborate a name written alone (rules (22) and (23)), so the kind is fixed here: in f : a -> a the name a is a type variable, and in f : { r | x : Int } -> r the name r is a row variable. The members of U′U' are added to UU while the annotation and the body of its definition elaborate, so that an annotation nested within the body that writes one of these names means the same variable. They are bound in the type scheme of the annotated variable, and they must not occur free in the context of the definition: an explicit variable does not escape its annotation.

4.8Closure

Tested by: StandardA93MutualNumberDefaultTest

Let AA be a semantic object and σ=∀ξ(k).τ\sigma = \forall\tyrowvar^{(k)}.\tau a type scheme. Then ClosAσ\Clos_A \sigma, the closure of σ\sigma with respect to AA, is the type scheme ∀ξ(k)ξ′(l).τ\forall\tyrowvar^{(k)}\tyrowvar'^{(l)}.\tau, where ξ′(l)\tyrowvar'^{(l)} enumerates tyvars σ∖tyvars A\tyvars\sigma \setminus \tyvars A. In particular, for a type τ\tau, ClosAτ=∀ξ(l).τ\Clos_A \tau = \forall\tyrowvar^{(l)}.\tau where ξ(l)\tyrowvar^{(l)} enumerates tyvars τ∖tyvars A\tyvars\tau \setminus \tyvars A. For a value environment, ClosAVE\Clos_A \VE is the value environment obtained from VE\VE by replacing each pair (σ,is)(\sigma, \is) in its range by (ClosAσ,is)(\Clos_A \sigma, \is).

Commonly AA is a context CC. Since UofC⊆tyvars CU \of C \subseteq \tyvars C, closure never generalises an explicit variable in scope. Closure applies to every group of value bindings, pattern bindings included, whatever the form of their expressions.

A group of value bindings is closed as one. When rules (19) and (56) take ClosCVE\Clos_C \VE of the value environment VE\VE of a group, the scheme σ\sigma of each binding is closed also over the type variables of class number\knum in tyvars VE∖(tyvars σ∪tyvars C)\tyvars\VE \setminus (\tyvars\sigma \cup \tyvars C): those of the other bindings of the group that its own type does not mention. Since such a variable does not occur in the binding’s type, it changes no instance of the scheme. It matters only to number specialisation (Section 6.2): an occurrence of the binding gives it a type that nothing fixes, which is then Float\tFloat.

4.9Inference Rules

Each rule of the semantics allows inferences among sentences of the form

A⊢phrase⇒A′ \jdg{A}{\mathit{phrase}}{A'}

where AA is usually a context, phrase\mathit{phrase} is a phrase of the Core, and A′A' is a semantic object, usually a type, a row, a value environment, or a pair of a value environment and a type. It is pronounced “phrase\mathit{phrase} elaborates to A′A' in (context) AA”. Some rules have extra hypotheses not of this form; they are called side conditions.

In the presentation of the rules, phrases within single angle brackets ⟨ ⟩\opt{\ } are called first options, and those within double angle brackets ⟨ ⁣⟨ ⟩ ⁣⟩\optt{\ } are called second options. To reduce the number of rules, we have adopted the following convention:

In each instance of a rule, the first options must be either all present or all absent; similarly the second options must be either all present or all absent.

No Core phrase generates type names, so TofCT \of C is the same in every sentence of a derivation for a Core phrase; it is used by the static semantics of Modules (Chapter 5). A phrase elaborates to A′A' when some derivation of the sentence exists; where the premises of a rule leave a type in its conclusion undetermined, the derivation may choose any type that satisfies them. The relation ⊢\vdash is overloaded for all syntactic classes.

Atomic ExpressionsC⊢atexp⇒τ\jdg{C}{\atexp}{\tau}
type(scon)≻τC⊢scon⇒τ\frac{\begin{array}{c}\typeof(\scon) \succ \tau\end{array}}{\begin{array}{c}\jdg{C}{\scon}{\tau}\end{array}}
(1)
C(longvar)=(σ,is)σ≻τC⊢longvar⇒τ\frac{\begin{array}{c}C(\longvar) = (\sigma, \is) \premsep \sigma \succ \tau\end{array}}{\begin{array}{c}\jdg{C}{\longvar}{\tau}\end{array}}
(2)
C(longcon)=(σ,is)σ≻τC⊢longcon⇒τ\frac{\begin{array}{c}C(\longcon) = (\sigma, \is) \premsep \sigma \succ \tau\end{array}}{\begin{array}{c}\jdg{C}{\longcon}{\tau}\end{array}}
(3)
OTSTAT(op)=(σ,is)σ≻τC⊢(op)⇒τ\frac{\begin{array}{c}\OTstat(\opv) = (\sigma, \is) \premsep \sigma \succ \tau\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\opv\kw{)}}{\tau}\end{array}}
(4)
C⊢()⇒()\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{()}}{\tunit}\end{array}}
(5)
C⊢expi⇒τi(1≤i≤k)C⊢( exp1, ⋯, expk )⇒(τ1,⋯ ,τk)Tuplek\frac{\begin{array}{c}\jdg{C}{\expm_i}{\tau_i} \quad (1 \le i \le k)\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\expm_1\kw{,}\ \cdots\kw{,}\ \expm_k\,\kw{)}}{(\tau_1, \cdots, \tau_k)\tTuple{k}}\end{array}}
(6)
C⊢[ ]⇒τ List\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{[}\ \kw{]}}{\tau\,\tList}\end{array}}
(7)
⟨C⊢exprow⇒ϱ⟩C⊢{ ⟨exprow⟩ }⇒{{}⟨+ϱ⟩}\frac{\begin{array}{c}\opt{\jdg{C}{\exprow}{\varrho}}\end{array}}{\begin{array}{c}\jdg{C}{\kw{\kwlb}\,\opt{\exprow}\,\kw{\kwrb}}{\rec{\emptymap\opt{{}+\varrho}}}\end{array}}
(8)
C⊢var⇒ττ∈RecTypeC⊢exprow⇒ϱϱ⊆(ϱofτ)C⊢{ var | exprow }⇒τ\frac{\begin{array}{c}\jdg{C}{\var}{\tau} \premsep \tau \in \RecType \premsep \jdg{C}{\exprow}{\varrho} \premsep \varrho \subseteq (\varrho \of \tau)\end{array}}{\begin{array}{c}\jdg{C}{\kw{\kwlb}\ \var\ \kw{|}\ \exprow\ \kw{\kwrb}}{\tau}\end{array}}
(9)
C⊢dec⇒VEC+VE⊢exp⇒τC⊢let dec in exp⇒τ\frac{\begin{array}{c}\jdg{C}{\dec}{\VE} \premsep \jdg{C + \VE}{\expm}{\tau}\end{array}}{\begin{array}{c}\jdg{C}{\kw{let}\ \dec\ \kw{in}\ \expm}{\tau}\end{array}}
(10)
C⊢exp⇒τC⊢( exp )⇒τ\frac{\begin{array}{c}\jdg{C}{\expm}{\tau}\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\expm\,\kw{)}}{\tau}\end{array}}
(11)
Comments:
(1)

For an integer constant, τ\tau may be any type in Admnumber\Adm{\knum}: Int\tInt, Float\tFloat or a type variable of class number\knum. A floating point, character or string constant has exactly its type. The type that counts for evaluation is the one the principal derivation gives (Section 4.10); where it is a variable that nothing binds, number specialisation takes it to be Float\tFloat (Section 6.2).

(2)

The instantiation of type schemes allows different occurrences of one longvar\longvar to take different types. The identifier status is not used in this rule, nor in rules (3) and (4); in particular the constructor of a record type alias, of status v\isv, elaborates by rule (3) as any constructor does.

(4)

Infixed applications of operators are derived forms of this phrase (Appendix A), so this rule gives the type of every application of an operator. The context CC is absent from the premise, so an operator has the same type scheme in every module, whatever the module declares or imports: a declaration of add in a program does not change the meaning of +.

(6)

By the grammar kk is 2 or 3.

(7)

Note that τ\tau does not occur in any premise; thus [ ] has type τ List\tau\,\tList for every τ\tau.

(8)

A record expression has a closed record type: its fields are exactly those written.

(9)

The side conditions require the type of var\var to be a record type that has every updated field, each at the type of its new value. The result has the type of var\var, so an update neither adds a field nor changes the type of one: if r\kw{r} has type {{x↦Int}}\rec{\{\kw{x} \mapsto \tInt\}} then { r | x = "s" } does not elaborate. The type of var\var may be open, so a function may update a field of any record that has it.

(10)

The variables bound by dec\dec are in scope in exp\expm at the type schemes that rule (19) gives them, so exp\expm may use them polymorphically.

Expression RowsC⊢exprow⇒ϱ\jdg{C}{\exprow}{\varrho}
C⊢exp⇒τ⟨C⊢exprow⇒ϱ⟩C⊢lab = exp ⟨, exprow⟩⇒{lab↦τ}⟨+ϱ⟩\frac{\begin{array}{c}\jdg{C}{\expm}{\tau} \premsep \opt{\jdg{C}{\exprow}{\varrho}}\end{array}}{\begin{array}{c}\jdg{C}{\lab\ \kw{=}\ \expm\ \opt{\kw{,}\ \exprow}}{\{\lab \mapsto \tau\}\opt{{}+\varrho}}\end{array}}
(12)
Comments:
(12)

The syntactic restrictions ensure lab∉Dom ϱ\lab \notin \Dom\varrho.

ExpressionsC⊢exp⇒τ\jdg{C}{\expm}{\tau}
C⊢atexp⇒τC⊢atexp⇒τ\frac{\begin{array}{c}\jdg{C}{\atexp}{\tau}\end{array}}{\begin{array}{c}\jdg{C}{\atexp}{\tau}\end{array}}
(13)
C⊢exp⇒τ′→τC⊢atexp⇒τ′C⊢exp atexp⇒τ\frac{\begin{array}{c}\jdg{C}{\expm}{\tau' \rightarrow \tau} \premsep \jdg{C}{\atexp}{\tau'}\end{array}}{\begin{array}{c}\jdg{C}{\expm\ \atexp}{\tau}\end{array}}
(14)
C⊢atpat⇒(VE,τ)C+VE⊢exp⇒τ′C⊢\ atpat -> exp⇒τ→τ′\frac{\begin{array}{c}\jdg{C}{\atpat}{(\VE, \tau)} \premsep \jdg{C + \VE}{\expm}{\tau'}\end{array}}{\begin{array}{c}\jdg{C}{\kw{\textbackslash}\ \atpat\ \kw{->}\ \expm}{\tau \rightarrow \tau'}\end{array}}
(15)
C⊢exp⇒τC⊢match⇒τ→τ′C⊢case exp of match⇒τ′\frac{\begin{array}{c}\jdg{C}{\expm}{\tau} \premsep \jdg{C}{\match}{\tau \rightarrow \tau'}\end{array}}{\begin{array}{c}\jdg{C}{\kw{case}\ \expm\ \kw{of}\ \match}{\tau'}\end{array}}
(16)
Comments:
(13)

The relational symbol ⊢\vdash is overloaded for all syntactic classes (here atomic expressions and expressions).

(15)

The variables bound by atpat\atpat receive types, not type schemes, so a parameter has one type throughout exp\expm. Restriction 2 of Section 4.11 requires atpat\atpat to be exhaustive.

MatchesC⊢match⇒τ\jdg{C}{\match}{\tau}
C⊢mrule⇒τ⟨C⊢match⇒τ⟩C⊢mrule ⟨; match⟩⇒τ\frac{\begin{array}{c}\jdg{C}{\mrule}{\tau} \premsep \opt{\jdg{C}{\match}{\tau}}\end{array}}{\begin{array}{c}\jdg{C}{\mrule\ \opt{\kw{;}\ \match}}{\tau}\end{array}}
(17)
Match RulesC⊢mrule⇒τ\jdg{C}{\mrule}{\tau}
C⊢pat⇒(VE,τ)C+VE⊢exp⇒τ′C⊢pat -> exp⇒τ→τ′\frac{\begin{array}{c}\jdg{C}{\pat}{(\VE, \tau)} \premsep \jdg{C + \VE}{\expm}{\tau'}\end{array}}{\begin{array}{c}\jdg{C}{\pat\ \kw{->}\ \expm}{\tau \rightarrow \tau'}\end{array}}
(18)
Comments:
(18)

This rule lets new free type variables enter the context: the types of the variables of pat\pat are chosen in elaborating pat\pat, and they may have to agree with the explicit variables of annotations within exp\expm.

DeclarationsC⊢dec⇒VE\jdg{C}{\dec}{\VE}
C+VE⊢valbind⇒VEVE′=ClosCVEC⊢valbind⇒VE′\frac{\begin{array}{c}\jdg{C + \VE}{\valbind}{\VE} \premsep \VE' = \Clos_C \VE\end{array}}{\begin{array}{c}\jdg{C}{\valbind}{\VE'}\end{array}}
(19)
Comments:
(19)

A declaration is one group of mutually dependent bindings; a let with several definitions is a derived form that divides them into groups (Appendix A). Modifying CC by VE\VE on the left puts every member of the group in scope in every member’s expression. Since VE\VE is the same on both sides, an unannotated member has one type throughout the group: it is monomorphic within the group, and is generalised only by the closure, for the body of the let. An annotated member enters VE\VE with the type scheme of its annotation, so it may be used polymorphically within the group, in its own body included.

The closure applies to every member whatever its expression, so there is no value restriction: in let n = 5 in ( n // 2, n / 2 ) the variable n is used at Int\tInt and at Float\tFloat. The closure leaves the scheme of an annotated member unchanged, since its free variables are explicit variables of CC.

Value BindingsC⊢valbind⇒VE\jdg{C}{\valbind}{\VE}
⟨U′ is new for ty in CC+U′⊢ty⇒τU′∩tyvars C=∅⟩C⟨+U′⟩⊢exp⇒τ⟨ ⁣⟨C⊢valbind⇒VE⟩ ⁣⟩C⊢var ⟨: ty⟩ = exp ⟨ ⁣⟨and valbind⟩ ⁣⟩⇒{var↦(⟨∀U′.⟩τ,v)} ⟨ ⁣⟨+VE⟩ ⁣⟩\frac{\begin{array}{c}\opt{U' \text{ is new for } \ty \text{ in } C \premsep \jdg{C + U'}{\ty}{\tau} \premsep U' \cap \tyvars C = \emptyset} \\ \jdg{C\opt{{}+U'}}{\expm}{\tau} \premsep \optt{\jdg{C}{\valbind}{\VE}}\end{array}}{\begin{array}{c}\jdg{C}{\var\ \opt{\kw{:}\ \ty}\ \kw{=}\ \expm\ \optt{\kw{and}\ \valbind}}{\{\var \mapsto (\opt{\forall U'.}\tau, \isv)\}\ \optt{{}+\VE}}\end{array}}
(20)
C⊢atpat⇒(VE,τ)C⊢exp⇒τC⊢atpat = exp⇒VE\frac{\begin{array}{c}\jdg{C}{\atpat}{(\VE, \tau)} \premsep \jdg{C}{\expm}{\tau}\end{array}}{\begin{array}{c}\jdg{C}{\atpat\ \kw{=}\ \expm}{\VE}\end{array}}
(21)
Comments:
(20)

When the second option is present, var∉Dom VE\var \notin \Dom\VE by the syntactic restrictions. With an annotation, U′U' holds the variables of ty\ty not already explicit in CC (Section 4.7). They are in UU while ty\ty and exp\expm elaborate, so an annotation nested in exp\expm that writes one of their names means the same variable. Since τ\tau is fixed by ty\ty, exp\expm must have exactly that type, with the variables of U′U' uninstantiated: an annotation may be less general than the type exp\expm could have, but never more general. Thus f : a -> a with f x = x + 1 does not elaborate, nor does g : number -> number with g x = x // 2.

The condition U′∩tyvars C=∅U' \cap \tyvars C = \emptyset prevents an explicit variable from escaping its annotation. In f x = let g : a -> a ; g y = x in g x, the body of g has type a -> a only if x has the explicit type a, which is then free in the context of g; so the expression does not elaborate.

(21)

A pattern binding is a group by itself. The variables of atpat\atpat receive types here and are generalised by rule (19). That rule also puts them in scope in exp\expm, but the syntactic restrictions forbid exp\expm to use them, since no pattern binding lies on a cycle of definitions. A binding var = exp\var\ \kw{=}\ \expm is read by rule (20).

Type ExpressionsC⊢ty⇒τ\jdg{C}{\ty}{\tau}
tyvar=αα∈UofCC⊢tyvar⇒α\frac{\begin{array}{c}\tyvar = \alpha \premsep \alpha \in U \of C\end{array}}{\begin{array}{c}\jdg{C}{\tyvar}{\alpha}\end{array}}
(22)
tyvar=ϵϵ∈UofCC⊢tyvar⇒{{}∣ϵ}\frac{\begin{array}{c}\tyvar = \epsilon \premsep \epsilon \in U \of C\end{array}}{\begin{array}{c}\jdg{C}{\tyvar}{\orec{\emptymap}{\epsilon}}\end{array}}
(23)
⟨C⊢tyrow⇒ϱ⟩C⊢{ ⟨tyrow⟩ }⇒{{}⟨+ϱ⟩}\frac{\begin{array}{c}\opt{\jdg{C}{\tyrow}{\varrho}}\end{array}}{\begin{array}{c}\jdg{C}{\kw{\kwlb}\,\opt{\tyrow}\,\kw{\kwrb}}{\rec{\emptymap\opt{{}+\varrho}}}\end{array}}
(24)
tyvar=ϵϵ∈UofCC⊢tyrow⇒ϱC⊢{ tyvar | tyrow }⇒{ϱ∣ϵ}\frac{\begin{array}{c}\tyvar = \epsilon \premsep \epsilon \in U \of C \premsep \jdg{C}{\tyrow}{\varrho}\end{array}}{\begin{array}{c}\jdg{C}{\kw{\kwlb}\ \tyvar\ \kw{|}\ \tyrow\ \kw{\kwrb}}{\orec{\varrho}{\epsilon}}\end{array}}
(25)
C⊢()⇒()\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{()}}{\tunit}\end{array}}
(26)
C⊢tyi⇒τi(1≤i≤k)C⊢( ty1, ⋯, tyk )⇒(τ1,⋯ ,τk)Tuplek\frac{\begin{array}{c}\jdg{C}{\ty_i}{\tau_i} \quad (1 \le i \le k)\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\ty_1\kw{,}\ \cdots\kw{,}\ \ty_k\,\kw{)}}{(\tau_1, \cdots, \tau_k)\tTuple{k}}\end{array}}
(27)
C(longtycon)=(θ,VE)C⊢tyi⇒τi(1≤i≤k)C⊢longtycon ty1⋯tyk⇒θ(τ1,⋯ ,τk)\frac{\begin{array}{c}C(\longtycon) = (\theta, \VE) \premsep \jdg{C}{\ty_i}{\tau_i} \quad (1 \le i \le k)\end{array}}{\begin{array}{c}\jdg{C}{\longtycon\ \ty_1 \cdots \ty_k}{\theta(\tau_1, \cdots, \tau_k)}\end{array}}
(28)
C⊢ty⇒τC⊢ty′⇒τ′C⊢ty -> ty′⇒τ→τ′\frac{\begin{array}{c}\jdg{C}{\ty}{\tau} \premsep \jdg{C}{\ty'}{\tau'}\end{array}}{\begin{array}{c}\jdg{C}{\ty\ \kw{->}\ \ty'}{\tau \rightarrow \tau'}\end{array}}
(29)
C⊢ty⇒τC⊢( ty )⇒τ\frac{\begin{array}{c}\jdg{C}{\ty}{\tau}\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\ty\,\kw{)}}{\tau}\end{array}}
(30)
Comments:
(22)

Rules (22) and (23) both cover the phrase tyvar\tyvar. Since UofCU \of C is unambiguous, at most one of them applies, according to whether the variable of UofCU \of C named by tyvar\tyvar is a type variable or a row variable. A type variable that is not explicit in CC does not elaborate: every type variable of an annotation is made explicit by rule (20), and Chapter 5 makes the parameters of type declarations explicit in the same way.

(23)

A row variable written alone denotes the open record type with no fields of its own: the record of the fields that ϵ\epsilon stands for.

(25)

Only a type variable of class any\kany names a row variable, so { number | x : Int } does not elaborate.

(27)

By the grammar kk is 2 or 3.

(28)

For θ(τ1,⋯ ,τk)\theta(\tau_1, \cdots, \tau_k) to be defined, θ\theta must have arity kk, so a type constructor is never partially applied. Application of a type function performs the substitution, so a type alias denotes its expansion, and an alias with a row parameter applies only to a record type.

Type-expression RowsC⊢tyrow⇒ϱ\jdg{C}{\tyrow}{\varrho}
C⊢ty⇒τ⟨C⊢tyrow⇒ϱ⟩C⊢lab : ty ⟨, tyrow⟩⇒{lab↦τ}⟨+ϱ⟩\frac{\begin{array}{c}\jdg{C}{\ty}{\tau} \premsep \opt{\jdg{C}{\tyrow}{\varrho}}\end{array}}{\begin{array}{c}\jdg{C}{\lab\ \kw{:}\ \ty\ \opt{\kw{,}\ \tyrow}}{\{\lab \mapsto \tau\}\opt{{}+\varrho}}\end{array}}
(31)
Comments:
(31)

The syntactic restrictions ensure lab∉Dom ϱ\lab \notin \Dom\varrho.

Atomic PatternsC⊢atpat⇒(VE,τ)\jdg{C}{\atpat}{(\VE, \tau)}
C⊢_⇒({},τ)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{\_}}{(\emptymap, \tau)}\end{array}}
(32)
type(scon)≻ττ∈{Int,Char,String}C⊢scon⇒({},τ)\frac{\begin{array}{c}\typeof(\scon) \succ \tau \premsep \tau \in \{\tInt, \tChar, \tString\}\end{array}}{\begin{array}{c}\jdg{C}{\scon}{(\emptymap, \tau)}\end{array}}
(33)
C⊢var⇒({var↦(τ,v)},τ)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\var}{(\{\var \mapsto (\tau, \isv)\}, \tau)}\end{array}}
(34)
C(longcon)=(σ,c)σ≻τ(k)tC⊢longcon⇒({},τ(k)t)\frac{\begin{array}{c}C(\longcon) = (\sigma, \isc) \premsep \sigma \succ \tau^{(k)}t\end{array}}{\begin{array}{c}\jdg{C}{\longcon}{(\emptymap, \tau^{(k)}t)}\end{array}}
(35)
C⊢()⇒({},())\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{()}}{(\emptymap, \tunit)}\end{array}}
(36)
C⊢pati⇒(VEi,τi)(1≤i≤k)C⊢( pat1, ⋯, patk )⇒(VE1+⋯+VEk, (τ1,⋯ ,τk)Tuplek)\frac{\begin{array}{c}\jdg{C}{\pat_i}{(\VE_i, \tau_i)} \quad (1 \le i \le k)\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\pat_1\kw{,}\ \cdots\kw{,}\ \pat_k\,\kw{)}}{(\VE_1 + \cdots + \VE_k,\ (\tau_1, \cdots, \tau_k)\tTuple{k})}\end{array}}
(37)
C⊢[ ]⇒({},τ List)\frac{\begin{array}{c}\end{array}}{\begin{array}{c}\jdg{C}{\kw{[}\ \kw{]}}{(\emptymap, \tau\,\tList)}\end{array}}
(38)
⟨C⊢patrow⇒(VE,ϱ)⟩τ∈RecType{}⟨+ϱ⟩⊆(ϱofτ)C⊢{ ⟨patrow⟩ }⇒({}⟨+VE⟩, τ)\frac{\begin{array}{c}\opt{\jdg{C}{\patrow}{(\VE, \varrho)}} \premsep \tau \in \RecType \premsep \emptymap\opt{{}+\varrho} \subseteq (\varrho \of \tau)\end{array}}{\begin{array}{c}\jdg{C}{\kw{\kwlb}\,\opt{\patrow}\,\kw{\kwrb}}{(\emptymap\opt{{}+\VE},\ \tau)}\end{array}}
(39)
C⊢pat⇒(VE,τ)C⊢( pat )⇒(VE,τ)\frac{\begin{array}{c}\jdg{C}{\pat}{(\VE, \tau)}\end{array}}{\begin{array}{c}\jdg{C}{\kw{(}\,\pat\,\kw{)}}{(\VE, \tau)}\end{array}}
(40)
Comments:
(33)

The side conditions give an integer constant the type Int\tInt in a pattern, never Float\tFloat or a type variable of class number\knum; a character or string constant has its own type. Thus case 1.5 of 1 -> 0 ; _ -> 1 does not elaborate. Floating point constants are not patterns, by the grammar.

(34)

The variable receives a type, not a type scheme. Constructors and variables are spelt differently, so no status test is needed here.

(35)

Only status c\isc is accepted, so the constructor of a record type alias, of status v\isv, cannot occur in a pattern. The instance must be a constructed type, so this rule accepts only a constructor without arguments; a constructor with arguments is covered by rule (43).

(37)

By the grammar kk is 2 or 3, and by the syntactic restrictions the domains of the VEi\VE_i are disjoint.

(38)

As in rule (7), τ\tau is arbitrary.

(39)

The record type τ\tau may have fields besides those the pattern names, and it may be open: a record pattern matches every record with the fields it names.

Pattern RowsC⊢patrow⇒(VE,ϱ)\jdg{C}{\patrow}{(\VE, \varrho)}
⟨C⊢patrow⇒(VE,ϱ)⟩C⊢lab = var ⟨, patrow⟩⇒({var↦(τ,v)}⟨+VE⟩, {lab↦τ}⟨+ϱ⟩)\frac{\begin{array}{c}\opt{\jdg{C}{\patrow}{(\VE, \varrho)}}\end{array}}{\begin{array}{c}\jdg{C}{\lab\ \kw{=}\ \var\ \opt{\kw{,}\ \patrow}}{(\{\var \mapsto (\tau, \isv)\}\opt{{}+\VE},\ \{\lab \mapsto \tau\}\opt{{}+\varrho})}\end{array}}
(41)
Comments:
(41)

A field of a record pattern binds the variable of its row to the type of the field. In a program the row is written as a label alone, whose equivalent form lab = var\lab\ \kw{=}\ \var has var=lab\var = \lab (Appendix A); a copy made by number specialisation gives var\var a new name and keeps lab\lab (Section 6.2). The syntactic restrictions ensure lab∉Dom ϱ\lab \notin \Dom\varrho. That var∉Dom VE\var \notin \Dom\VE follows, in a program, from the restriction that a binding site binds no variable twice, and in a copy from the distinct new names it gives.

PatternsC⊢pat⇒(VE,τ)\jdg{C}{\pat}{(\VE, \tau)}
C⊢atpat⇒(VE,τ)C⊢atpat⇒(VE,τ)\frac{\begin{array}{c}\jdg{C}{\atpat}{(\VE, \tau)}\end{array}}{\begin{array}{c}\jdg{C}{\atpat}{(\VE, \tau)}\end{array}}
(42)
C(longcon)=(σ,c)σ≻τ1→⋯→τn→τ′(k)tC⊢atpati⇒(VEi,τi)(1≤i≤n)C⊢longcon atpat1⋯atpatn⇒(VE1+⋯+VEn, τ′(k)t)\frac{\begin{array}{c}C(\longcon) = (\sigma, \isc) \premsep \sigma \succ \tau_1 \rightarrow \cdots \rightarrow \tau_n \rightarrow \tau'^{(k)}t \\ \jdg{C}{\atpat_i}{(\VE_i, \tau_i)} \quad (1 \le i \le n)\end{array}}{\begin{array}{c}\jdg{C}{\longcon\ \atpat_1 \cdots \atpat_n}{(\VE_1 + \cdots + \VE_n,\ \tau'^{(k)}t)}\end{array}}
(43)
C⊢pat1⇒(VE1,τ)C⊢pat2⇒(VE2,τ List)C⊢pat1 :: pat2⇒(VE1+VE2, τ List)\frac{\begin{array}{c}\jdg{C}{\pat_1}{(\VE_1, \tau)} \premsep \jdg{C}{\pat_2}{(\VE_2, \tau\,\tList)}\end{array}}{\begin{array}{c}\jdg{C}{\pat_1\ \kw{::}\ \pat_2}{(\VE_1 + \VE_2,\ \tau\,\tList)}\end{array}}
(44)
C⊢pat⇒(VE,τ)C⊢pat as var⇒(VE+{var↦(τ,v)}, τ)\frac{\begin{array}{c}\jdg{C}{\pat}{(\VE, \tau)}\end{array}}{\begin{array}{c}\jdg{C}{\pat\ \kw{as}\ \var}{(\VE + \{\var \mapsto (\tau, \isv)\},\ \tau)}\end{array}}
(45)
Comments:
(43)

The instance must be a constructed type after exactly nn arguments, so a constructor in a pattern is applied to as many patterns as it takes. Thus a constructed pattern with too few or too many arguments does not elaborate. The syntactic restrictions ensure that the domains of the VEi\VE_i are disjoint.

(44)

The type name is List\tList of the initial basis, whatever the program binds to List.

(45)

The syntactic restrictions ensure var∉Dom VE\var \notin \Dom\VE.

4.10Principal Derivations

A phrase may elaborate in more than one way. An integer constant may take any type in Admnumber\Adm{\knum} (rule (1)), and an occurrence of a variable any instance of its type scheme (rule (2)), so two derivations for one phrase may give its integer constants and its occurrences different types. Whether a phrase elaborates does not depend on the choice, but evaluation does: 2 ^ -1 is 00 at Int\tInt and 0.50.5 at Float\tFloat. The dynamic semantics is therefore given for one derivation, the principal one, which gives no type that the phrase does not force; the further restrictions (Section 4.11) are judged on it too, although for them the choice makes no difference. In a derivation for a phrase, each node is a sentence about an occurrence of a phrase within it; the nodes of two derivations for the same phrase correspond when they are about the same occurrence. Let DD and D′D' be derivations for the same phrase in the same context C0C_0, the type and row variables of DD that are not free in C0C_0 having been renamed apart from those of D′D'. Then D′D' is an instance of DD when there is a substitution S={τ(k)/ξ(k)}S = \{\tau^{(k)}/\tyrowvar^{(k)}\} (Section 4.5) that respects classes, with no ξi\tyrowvar_i free in C0C_0, defined on every type of DD, such that at each pair of corresponding nodes, with contexts CC in DD and C′C' in D′D',

  • SS takes each variable in UofCU \of C to a variable, and UofC′U \of C' is the set of the variables so obtained;

  • each type in the sentence of D′D' is SS applied to the corresponding type in the sentence of DD; and

  • each type scheme σ′\sigma' in the sentence of D′D' is no more general than SσS\sigma, for the corresponding type scheme σ\sigma of DD: every type τ\tau with σ′≻τ\sigma' \succ \tau has Sσ≻τS\sigma \succ \tau.

Since SS is defined on every type of DD, it never gives a record type a second field with a label it already has; no Core phrase generates type names, so TofC=TofC′T \of C = T \of C'. The third clause allows a derivation to give a binding a less general scheme than another derivation gives it, as when y = 2 ^ 64 * 4 is given the type Int\tInt where another derivation gives it the scheme ∀α.α\forall\alpha.\alpha with α\alpha of class number\knum. A derivation DD for a phrase in a context is principal when every derivation for the phrase in that context is an instance of DD. Chapter 5 extends the definition to modules (Section 5.6).

Every Core phrase that elaborates in a context C0C_0 has a principal derivation in C0C_0, and any two of its principal derivations are instances of each other, so that they differ only by a renaming of type and row variables not free in C0C_0.

This is the principal type property of Damas and Milner, extended to classes, rows and annotations. Unification that respects classes has a most general solution whenever it has a solution, since two classes have a greatest common subclass or none: number\knum and comparable\kcomp give number\knum, appendable\kapp and comparable\kcomp give compappend\kcapp, and number\knum and appendable\kapp none (Section 4.4). The labels of a row are distinct (Section 4.5), the explicit variables of an annotation are fixed throughout its scope (Section 4.7), and a group of value bindings is monomorphic within itself (rule (19)), so the inference algorithm of Damas and Milner, extended in these ways, finds a principal derivation. The proof is not given here. Only the variables of class number\knum of a principal derivation affect evaluation: the value of an integer constant, and the result of a number operation of the initial basis, depend on whether a type is Int\tInt or Float\tFloat, while no value depends on how a variable of another class, or of none, is instantiated. Number specialisation (Section 6.2) starts from the principal derivation and fixes its variables of class number\knum.

4.11Further Restrictions

There are a few restrictions on programs which are better expressed apart from the inference rules. They are judged on the principal derivation (Section 4.10): the type τ\tau with which the patterns of a match elaborate, and the type of a pattern, are those that derivation gives. A phrase that elaborates by the rules but whose principal derivation breaks one of them is not legal; it is treated as a phrase that does not elaborate. In fact every derivation for the phrase gives the same answer. A pattern that the principal derivation elaborates at a type variable, at the top of a match or within another pattern, is a wildcard, a variable, or one of these with as or parentheses, since every other pattern (a special constant, a constructor, (), a tuple, [ ], :: or a record pattern) elaborates only at a type that is not a variable; and such a pattern covers every inhabitant. Another derivation may replace the variable by any type, which has inhabitants (below), and the same patterns cover them. The first two restrictions speak of the inhabitants of a type, which are phrases, possibly infinite, built from constructors and special constants, and of the patterns that cover them. The constructors of a type name tt are those that the declaration of the custom type with type name tt binds with status c\isc (Chapter 5), or, for a type name of the initial basis, those that Appendix C gives it; the constructors of List\tList are [] and ::. The inhabitants of a type τ\tau are given by the first of the following clauses that applies:

  • if τ\tau is Int\tInt, Char\tChar or String\tString, each special constant scon\scon such that type(scon)≻τ\typeof(\scon) \succ \tau;

  • if τ=()\tau = \tunit, the phrase ();

  • if τ=(τ1,⋯ ,τk)Tuplek\tau = (\tau_1, \cdots, \tau_k)\tTuple{k}, the phrases (h1, ⋯, hk)\kw{(}h_1\kw{,}\allowbreak\ \cdots\kw{,}\allowbreak\ h_k\kw{)}, where each hih_i is an inhabitant of τi\tau_i;

  • if τ=τ′(k)t\tau = \tau'^{(k)}t and tt has constructors, the phrases con h1⋯hn\con\ h_1 \cdots h_n (written h1 :: h2h_1\ \kw{::}\ h_2 when con\con is ::), where con\con is a constructor of tt whose type scheme σ\sigma satisfies σ≻τ1→⋯→τn→τ\sigma \succ \tau_1 \rightarrow \cdots \rightarrow \tau_n \rightarrow \tau, and each hih_i is an inhabitant of τi\tau_i;

  • otherwise (a function type, a record type, a type variable, or a type whose type name has no constructors), the single phrase _.

The clauses are read coinductively: an inhabitant may be infinite, provided each of its parts is given by a clause. So type V = V V has the one inhabitant V (V (V …)), and Never, whose one constructor JustOneMore takes a Never (Appendix C), has the one inhabitant JustOneMore (JustOneMore …). Every type has an inhabitant, and so every constructor of a type gives inhabitants of it, at every instance. Inhabitants are not values: no value has type Never. An inhabitant describes what a pattern may be asked to match, which depends only on the constructors of a type. Thus with r of type Result Never Int, the second rule of case r of Ok n -> n ; Err e -> never e covers Err (JustOneMore …) and is not redundant, while with m of type Maybe Never, case m of Nothing -> 0 is not exhaustive. Where every type that a match involves has a finite inhabitant, the infinite inhabitants change neither restriction, since a pattern examines only a finite part of an inhabitant. The wildcard _, a variable and a record pattern cover every inhabitant. A special constant covers each special constant that denotes the same character or string or, for integer constants, has the same value as an Int\tInt (Chapter 2), however the two are written. A pattern longcon atpat1⋯atpatn\longcon\ \atpat_1 \cdots \atpat_n (n≥0n \geq 0) covers con h1⋯hn\con\ h_1 \cdots h_n when longcon\longcon is con\con or modid.con\modid.\con and each atpati\atpat_i covers hih_i; likewise [ ] covers [], and pat1 :: pat2\pat_1\ \kw{::}\ \pat_2 covers h1 :: h2h_1\ \kw{::}\ h_2 when pat1\pat_1 covers h1h_1 and pat2\pat_2 covers h2h_2. The pattern () covers (), a tuple pattern covers a tuple component by component, and pat as var\pat\ \kw{as}\ \var and (pat)\kw{(}\pat\kw{)} cover what pat\pat covers. The restrictions are:

  1. Irredundancy. In a match pat1 -> exp1 ; ⋯ ; patn -> expn\pat_1\ \kw{->}\ \expm_1\ \kw{;}\ \cdots\ \kw{;}\ \pat_n\ \kw{->}\ \expm_n whose patterns elaborate with type τ\tau, each patj\pat_j must cover some inhabitant of τ\tau that no pati\pat_i with i<ji < j covers.

    core-irredundancy
  2. Exhaustiveness. In a match as above, every inhabitant of τ\tau must be covered by some pati\pat_i. Likewise, in a pattern binding atpat = exp\atpat\ \kw{=}\ \expm and in a lambda \ atpat -> exp\kw{\textbackslash}\ \atpat\ \kw{->}\ \expm, the pattern atpat\atpat must cover every inhabitant of its type. The restrictions are inherited by derived forms: in a function definition var atpat1⋯atpatn = exp\var\ \atpat_1 \cdots \atpat_n\ \kw{=}\ \expm, each atpati\atpat_i must be exhaustive by itself. In deciding exhaustiveness, no set of special constants covers every inhabitant of Int\tInt, Char\tChar or String\tString, whatever the range of Int\tInt (Appendix E).

    core-exhaustiveness

Discussions

Discuss this chapter·Search the discussions on GitHub

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