ECMA-262 in Lean

Reading page · generated from spec/spectec/prose/01-model.spectec, 02-value.spectec, 05-model-laws.spectec

The model and its laws

A model (syntax Model record) gives each sort of the specification (mathematical values, Numbers, BigInts, Strings, Symbols, Parse Nodes, Lists, Data Blocks, CharSets, Records, the store) a carrier and its operations, over which specification values (syntax Val) and store cells (syntax Cell) are defined. Every object cites its governing ECMA-262 clause, and the SpecTec laws blocks state what the prose assumes of them: 148 laws, each quoting the sentence it reads.

The model

From spec/spectec/prose/01-model.spectec

A Model gives each sort of the specification a carrier and its operations. The machine is defined over every model; the laws a model must satisfy are a separate statement (Model.Laws). No carrier is fixed here: mathematical values form an ordered field with "is an integer" a predicate; Strings are sequences of code units; Lists, Records and objects live in a store and have identity.

syntax

NumView

01-model.spectec:16

The case view of a Number value (§6.1.6.1, sec-ecmascript-language-types-number-type).

syntax NumView (Math : Type)

a finite non-zero value: its mathematical value

| finite (m : Math)
syntax record

ParseCx

01-model.spectec:31

A parser invocation's context (§5.1.2, sec-parsing): the Annex B sections whose grammar applies (those the host supports, B.1 and B.3, sec-additional-ecmascript-features-for-web-browsers), whether the source text is strict mode code from outside (§11.2.2, sec-strict-mode-code: eval code of a direct eval in strict mode code), whether it is parsed for ParseJSON (§25.5.1), and whether it is source text parsed with the goal symbol Module (B.1.1), or a reparse of tokens such a parse matched.

syntax ParseCx recordderiving [Repr, BEq, Inhabited]
syntax

NormForm

01-model.spectec:40

The Unicode Normalization Forms (UAX #15; §22.1.3.15, sec-string.prototype.normalize).

syntax NormFormderiving [Repr, BEq, DecidableEq, Inhabited]
syntax record

Model

01-model.spectec:50

A model of the spec's sorts and their primitive operations (§5.2, sec-algorithm-conventions; §6, sec-ecmascript-data-types-and-values).

syntax Model record
122 constructors

mathematical values (§5.2.6)

Math : Type;

Number values (§6.1.6.1)

Num : Type;

BigInt values (§6.1.6.2)

Big : Type;

String values: sequences of 16-bit code units (§6.1.4)

Str : Type;

Symbol values: identities (§6.1.5)

Sym : Type;

identities of store cells: objects, Records and Lists

Addr : Type;

Parse Nodes (§5.1.2)

Node : Type;

finite sequences: List contents (§6.2.2)

Seq : Type -> Type;

byte sequences: Data Block contents (§6.2.9)

Bytes : Type;

finite maps from field names: Record contents and internal slots (§6.2.1)

Rec : Type -> Type;

own-property tables: maps from property keys, in creation order (§6.1.7)

Props : Type -> Type;

the store

Heap : Type -> Type;

x<sup>y</sup>: for an integer y, or for x > 0 and any y

pow : Math -> Math -> Math;

the integer e with 10^e ≤ x < 10^(e+1), for x > 0: the place of the first significant digit of x's decimal representation (Model.Laws)

decExp : Math -> Math;

the square root of a non-negative mathematical value

sqrt : Math -> Math;

a §21.3 function at x, on the domain the calling steps guard; opaque: every use is implementation-approximated

fn : MathFn -> Math -> Math;

𝔽(x) for finite x: roundTiesToEven to the nearest Number value (§6.1.6.1)

numOf : Math -> Num;

+∞𝔽 (false) or -∞𝔽 (true)

numInf : Bool -> Num;

+0𝔽 (false) or -0𝔽 (true)

numZero : Bool -> Num;

IEEE 754-2019 addition, roundTiesToEven (§5.2.6: operators on Numbers)

numAdd : Num -> Num -> Num;

IEEE 754-2019 compareQuietLess

numLt : Num -> Num -> Bool;

IEEE 754-2019 compareQuietEqual

numEq : Num -> Num -> Bool;

Number::toString's integers n, k and s for a finite positive Number and a radix from 2 to 36 (§6.1.6.1.20 step 5): s has k digits, 𝔽(s × radix^(n-k)) is the Number, and k is as small as possible (Model.NumLaws)

numDigits : Num -> Math -> Math times Nat times Math;

a String literal of the text

strLit : String -> Str;

the code unit at an index, as a mathematical value

strAt : Str -> Math -> Option(Math);

the String made from n copies of s appended together (n ≥ 0)

strRepeat : Math -> Str -> Str;

the representation of non-negative integer n using radix r (2 ≤ r ≤ 36)

intToRadix : Math -> Math -> Str;

a Symbol's [[Description]]: a String, or none for undefined (§6.1.5)

symDesc : Sym -> Option(Str);

the Symbol made by the kth creation, with the description

symNew : Nat -> Option(Str) -> Sym;

the k-th written occurrence of a nonterminal on the production's right-hand side, absent optional items counted: some (some c) its child, some none an optional item that is absent, none no such occurrence

nodeChild : Node -> NtId -> Nat -> Option(Option(Node));

the nonterminal the node is an instance of

nodeNt : Node -> NtId;

§5.1.2 parser invocation k: a parse of code points with a goal symbol and its parameters, some (some n) the Parse Node, some none no parse, none where the model does not decide (Model.ParseLaws)

parse : ParseCx -> Nat -> Seq(Math) -> NtId -> List(ParamId times Bool) -> Option(Option(Node));

§5.1.5 "P must cover K": the covered Parse Node of parser invocation k: the Parse Node the cover's source text parses to with the goal symbol and its parameters, given from the cover itself; none where the model gives none from the cover, and its source text is parsed (Model.CoverLaws)

cover : ParseCx -> Nat -> Node -> NtId -> List(ParamId times Bool) -> Option(Node) := fun _ _ _ _ _ => none;

the parser invocation a Parse Node resulted from (§5.1.2: never reused across invocations)

nodeStamp : Node -> Nat;

the span of source text it matched, in code points

nodeSpan : Node -> Nat times Nat;

whether two Parse Nodes are the same one

nodeEq : Node -> Node -> Bool;

n copies of a value, for a non-negative integer n

seqRepeat : {α : Type} -> Math -> α -> Seq(α);

the elements, first to last, folded

seqFoldl : {α β : Type} -> (β -> α -> β) -> β -> Seq(α) -> β;

update in place, or add at the end of creation order

propsPut : {α : Type} -> Props(α) -> Str sum Sym -> α -> Props(α);

the simple case folding of a code point: its CaseFolding.txt mapping of status C or S, if any (UAX #44 Simple_Case_Folding)

scf? : Math -> Option(Math);

toLowercase (Unicode §3.13, Default Case Conversion): full lowercase mappings, Final_Sigma in context

toLower : Seq(Math) -> Seq(Math);

toUppercase (Unicode §3.13, Default Case Conversion): full uppercase mappings

toUpper : Seq(Math) -> Seq(Math);

the CharSetElements, each a sequence of code points, whose Unicode Character Database definition includes the property named p with the value named v (names or aliases, as code points; §22.2.2.9); none where p and v name no property value, or one with infinitely many elements (False of a property of strings)

propSet : Seq(Math) -> Seq(Math) -> Option(Seq(Seq(Math)));

the long name, as code points, of the value named v of the Unicode property named p (names or aliases, compared exactly; PropertyValueAliases.txt); none where p and v name none

propValue : Seq(Math) -> Seq(Math) -> Option(Seq(Math));

the code points normalized into the form, as the Unicode Standard's Normalization Forms specify (UAX #15); opaque

normalize : NormForm -> Seq(Math) -> Seq(Math);

a Data Block of n zero bytes (§6.2.9, empty if not a non-negative integer)

bytesZero : Math -> Bytes;
Auxiliary definitions (2): seqList, Model.canonChar
def

seqList

01-model.spectec:259

A List's elements, first to last.

def seqList {M : Model} {α : Type} (sq : M.Seq(α)) : List(α) := M.seqFoldl(fun l x => l ++ [x], [], sq)
def

Model.canonChar

01-model.spectec:263

§22.2.2.7.3 Canonicalize(rer, ch) on a character c, with [[IgnoreCase]] ignoreCase and HasEitherUnicodeFlag u.

def Model.canonChar (M : Model) (ignoreCase u : Bool) (c : M.Math) : M.Math := if !ignoreCase then c else if u then M.scf?(c).getD(c) else let mapped := M.toUpper(M.seqPush(M.seqNil, c)) in if M.eq(M.seqLen(mapped), M.ofNat(1)) then match M.seqGet(mapped, M.ofNat(0)) with  | some(d) => if M.lt(M.ofNat(0xFFFF), d) || !M.lt(c, M.ofNat(128)) && M.lt(d, M.ofNat(128)) then c else d  | none => cend else c

Values, cells and stops

From spec/spectec/prose/02-value.spectec

The values algorithms compute with (Val), a cell of the store (Cell: a Record, a List, an object, an Environment Record), a call of the host as the host-call log records it (HostCall), and why a run stops without a result (Stop).

syntax record

CloSite

02-value.spectec:16

An Abstract Closure literal (§6.2.8, sec-abstract-closure): its algorithm and its index there.

syntax CloSite recordderiving [Repr, BEq, Inhabited]
syntax

Val

02-value.spectec:24

The values algorithms compute with, by sort (§6.1, sec-ecmascript-language-types; §6.2, sec-ecmascript-specification-types).

syntax Valinhabited Val(M) := .absent
33 constructors

an optional parameter not passed; an absent optional child

| absent

a built-in function's parameter not passed: undefined to every use but is present (§18)

| missing
| bool (b : Bool)

a Record or a List, by identity

| ref (a : M.Addr)

an algorithm: an internal method or a function's behaviour

| alg (a : AlgId)

the Parse Node of the terminal t, at position i of n's present right-hand side

| leaf (n : M.Node) (i : Nat) (t : String)

a terminal symbol, by its text

| term (t : String)

a type hole's value: the type, with the algorithm, Parse Node and variables it is written in

| ty (t : TyExpr) (self : AlgId) (node : Option(M.Node)) (env : List(Val))

a condition over bound variables: the condition, the slots it binds, and the algorithm, Parse Node and variables it is written in; applied to values, it holds of them or not

| cond (c : Cond) (xs : List(Slot)) (self : AlgId) (node : Option(M.Node)) (env : List(Val))

a grammar symbol and its parameters (true: +)

| nt (n : NtId) (args : List(ParamId times Bool))

a written production: its GL productions (one per presence of its optional symbols)

| prod (ps : List(ProdId))

a phrase's choice of words

| choice (i : Nat)

an emu-table, as an emu-xref names it

| table (t : TableId)

a variable not yet bound: a read of it stops

| unbound

values held together, without identity: a rule's early errors, before they are a List

| tuple (vs : List(Val))
syntax

Cell

02-value.spectec:88

A store cell: an Object (§6.1.7, sec-object-type), a Record or List (§6.2.2, sec-list-and-record-specification-type), a Data Block (§6.2.9, sec-data-blocks), a RegExp CharSet (§22.2.2, sec-pattern-semantics), or an Environment Record (§9.1, sec-environment-records).

syntax Cell

a List; origin is its first index ("indexed 1 through N")

| list (xs : M.Seq(Val(M))) (origin : Nat)

a Data Block (§6.2.9): a sequence of byte values (integers 0 to 255)

| block (bs : M.Bytes)

a CharSet (§22.2.2.1): a mathematical set of CharSetElements

| charSet (cs : M.CharSet)

internal slots, and own properties: each an attribute Record of the store

| object (slots : M.Rec(Val(M))) (props : M.Props(Val(M)))

an Environment Record: its fields, and its bindings by name, each a binding Record of the store

| env (kind : SortId) (fs : M.Rec(Val(M))) (binds : M.Props(Val(M)))
syntax

HostCall

02-value.spectec:105

A call of the host, as the host-call log records it so that host laws can relate answers across a run (§4.2, sec-hosts-and-implementations; §9.5, sec-jobs; §9.6, sec-agents; §9.9.3, sec-weakref-execution; §9.10, sec-clear-kept-objects).

syntax HostCall

a host-defined operation: its clause, its arguments, the active script or module at the call (§9.4.1), the running execution context's Realm (§9.4), and its answer

| hook (c : ClauseId) (args : List(Val(M))) (active : Val(M)) (realm : Val(M)) (answer : Val(M))

a choice at a primitive the host decides: its arguments and answer

| choice (q : PrimId) (args : List(Val(M))) (answer : Val(M))

the next job the host runs (§9.5): the log position of the call that scheduled it, or none; and the milliseconds elapsed by the host's own clock as it takes it

| next (answer : Option(Nat)) (elapsed : M.Math)

the host ends its jobs with a promise, generic or timeout job scheduled and not taken: the embedding terminates the agent (§9.6)

| terminate

the host-defined preparation steps of the job scheduled at that log position (§9.5)

| prepare (job : Nat)

the host-defined cleanup steps of the job scheduled at that log position (§9.5)

| cleanup (job : Nat)

whether the host performs ClearKeptObjects now (§9.10)

| kept (answer : Bool)

the objects and symbols whose WeakRefs and cells the host emptied at a job boundary (§9.9.3)

| collect (answer : List(Val(M)))
syntax record

ShapeChange

02-value.spectec:130

A change to an object's shape (§14.7.5.9, sec-enumerate-object-properties), as the shape log records it: a property added (added), or a property removed, replaced or its [[Enumerable]] changed, or its [[Prototype]] changed; before is the host's calls before it.

syntax ShapeChange record
syntax

Stop

02-value.spectec:137

Why a run stops without a result (§2, sec-conformance; §5.2, sec-algorithm-conventions; §9.6, sec-agents).

syntax Stopderiving [Repr, BEq, Inhabited]

every agent waits and none will be woken

| blocked

a value of the wrong sort

| sortError

an Assert, or ! on an abrupt completion

| assertFailed

residual prose without an assigned meaning

| residue

the end of an algorithm without Return

| noReturn

an implementation limit of the host

| implLimit

The laws of a model

From spec/spectec/prose/05-model-laws.spectec

What the specification assumes of its sorts, stated over a Model's operations. Indices, lengths and code units are mathematical values that are non-negative integers, written M.ofNat k. The laws about Parse Nodes are Model.NodeLaws, Model.ParseLaws, Model.CoverLaws and Model.ParentLaws.

Auxiliary definitions (2): utf16Units, strLitUnits
def

utf16Units

05-model-laws.spectec:13

§11.1.1 UTF16EncodeCodePoint: the code units of a code point.

def utf16Units (c : Nat) : List(Nat) := if c < 0x10000 then [c] else [0xD800 + (c - 0x10000) / 0x400, 0xDC00 + (c - 0x10000) % 0x400]
def

strLitUnits

05-model-laws.spectec:16

The code units of a text: its code points encoded in UTF-16.

def strLitUnits (t : String) : List(Nat) := t.toList.flatMap(fun c => utf16Units(c.toNat))

The Number type (§6.1.6.1): its value set, the Number value for a real, and IEEE 754-2019's operations on it.

Auxiliary definitions (8): Model.abs, Model.form, Model.finiteNum, Model.candidate, …
def

Model.abs

05-model-laws.spectec:25

|x|

def Model.abs (x : M.Math) : M.Math := if M.lt(x, M.ofNat(0)) then M.neg(x) else x
def

Model.form

05-model-laws.spectec:28

±m × 2^k / 2^1074: a finite non-zero Number value's magnitude form, with e = k − 1074.

def Model.form (neg : Bool) (m k : Nat) : M.Math := let v := M.div(M.mul(M.ofNat(m), M.pow(M.ofNat(2), M.ofNat(k))), M.pow(M.ofNat(2), M.ofNat(1074))) in if neg then M.neg(v) else v
def

Model.finiteNum

05-model-laws.spectec:32

§6.1.6.1: the finite non-zero Number values, normalized (2^52 ≤ m < 2^53, −1074 ≤ e ≤ 971) or denormalized (0 < m < 2^52, e = −1074); even when the significand m is even.

def Model.finiteNum (x : M.Math) (even : Bool) : Prop := exists (neg : Bool) (m k : Nat), x = M.form(neg, m, k) /\ m % 2 == 0 = even /\ (2 ^ 52 <= m /\ m < 2 ^ 53 /\ k <= 2045 \/ 0 < m /\ m < 2 ^ 52 /\ k = 0)
def

Model.candidate

05-model-laws.spectec:36

The candidates of "the Number value for x": the finite values without −0, i.e. the non-zero ones and 0, and ±2^1024 (even); with the parity of each significand.

def Model.candidate (y : M.Math) (even : Bool) : Prop := M.finiteNum(y, even) \/ y = M.ofNat(0) /\ even = true \/ (y = M.pow(M.ofNat(2), M.ofNat(1024)) \/ y = M.neg(M.pow(M.ofNat(2), M.ofNat(1024)))) /\ even = true
def

Model.nearest

05-model-laws.spectec:39

y is "the member of the set closest in value to x", with ties to the even significand.

def Model.nearest (x y : M.Math) : Prop := exists ev, M.candidate(y, ev) /\ forall z ez, M.candidate(z, ez) -> M.lt(M.abs(M.sub(x, z)), M.abs(M.sub(x, y))) = false /\ (M.abs(M.sub(x, z)) = M.abs(M.sub(x, y)) -> z <> y -> ev = true)
def

Model.viewFor

05-model-laws.spectec:43

The view of the Number value for a non-zero chosen member y of x: ±∞ for ±2^1024, ±0 by x's sign for 0, else y.

def Model.viewFor (x y : M.Math) : NumView(M.Math) := if M.eq(y, M.pow(M.ofNat(2), M.ofNat(1024))) then .posInf else if M.eq(y, M.neg(M.pow(M.ofNat(2), M.ofNat(1024)))) then .negInf else if M.eq(y, M.ofNat(0)) then if M.lt(x, M.ofNat(0)) then .negZero else .posZero else .finite(y)
def

Model.realOf

05-model-laws.spectec:46

The real value of a finite Number view (both zeros are 0).

def Model.realOf : NumView(M.Math) -> Option(M.Math)  | .finite(m) => some(m)  | .posZero => some(M.ofNat(0))  | .negZero => some(M.ofNat(0))  | _ => noneend
def

Model.digitsOf

05-model-laws.spectec:55

Number::toString step 5's condition on n, k and s for x at radix r: k ≥ 1, n and s integers, r^(k-1) ≤ s < r^k, and 𝔽(s × r^(n-k)) is x.

def Model.digitsOf (x : M.Num) (r n : M.Math) (k : Nat) (s : M.Math) : Prop := 1 <= k /\ M.isInt(n) = true /\ M.isInt(s) = true /\ M.lt(s, M.pow(r, M.ofNat(k - 1))) = false /\ M.lt(s, M.pow(r, M.ofNat(k))) = true /\ M.numOf(M.mul(s, M.pow(r, M.sub(n, M.ofNat(k))))) = x
laws

Model.NumLaws

05-model-laws.spectec:60

The laws of the Number type: its value set (§6.1.6.1, sec-ecmascript-language-types-number-type), "the Number value for x" as roundTiesToEven, and IEEE 754-2019's operations.

Proved as part of Model.Laws (its field num).

laws Model.NumLaws (M : Model)

12 laws

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

representing the double-precision floating point IEEE 754-2019 binary64 values

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

non-zero finite Numbers are partitioned into two groups

Every finite view is a normalized or denormalized value (finiteNum).

numView_finite
SpecTec
law numView_finite : forall a m, M.numView(a) = .finite(m) -> (exists ev, M.finiteNum(m, ev));

Every normalized or denormalized value is a Number value.

finiteNum_view
SpecTec
law finiteNum_view : forall m ev, M.finiteNum(m, ev) -> (exists a, M.numView(a) = .finite(m));
6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

Choose the member of this set that is closest in value to x. If two values of the set are equally close, then the one with an even significand is chosen

6.1.6.1.1 Number::unaryMinus sec-numeric-types-number-unaryMinus

Return the negation of number; that is, compute a Number with the same magnitude but opposite sign.

Negation flips the sign; NaN stays.

numNeg_view
SpecTec
law numNeg_view : forall a, M.numView(M.numNeg(a)) = match M.numView(a) with    | .nan => .nan    | .posInf => .negInf    | .negInf => .posInf    | .posZero => .negZero    | .negZero => .posZero    | .finite(m) => .finite(M.neg(m))  end;
6.1.6.1.7 Number::add sec-numeric-types-number-add

If x is -0𝔽 and y is -0𝔽, return -0𝔽. 1. Return 𝔽(ℝ(x) + ℝ(y)).

Number::add's steps, a row each.

numAdd_view
SpecTec
law numAdd_view : forall a b, M.numView(M.numAdd(a, b)) = match M.numView(a), M.numView(b) with    | .nan, _ => .nan    | _, .nan => .nan    | .posInf, .negInf => .nan    | .negInf, .posInf => .nan    | .posInf, _ => .posInf    | .negInf, _ => .negInf    | _, .posInf => .posInf    | _, .negInf => .negInf    | .negZero, .negZero => .negZero    | va, vb => match M.realOf(va), M.realOf(vb) with        | some(x), some(y) => M.numView(M.numOf(M.add(x, y)))        | _, _ => .nan      end  end;
6.1.6.1.8 Number::subtract sec-numeric-types-number-subtract

Return Number::add(x, Number::unaryMinus(y)).

6.1.6.1.4 Number::multiply sec-numeric-types-number-multiply

Return 𝔽(ℝ(x) × ℝ(y)).

Number::multiply's steps, a row each.

numMul_view
SpecTec
law numMul_view : forall a b, M.numView(M.numMul(a, b)) = match M.numView(a), M.numView(b) with    | .nan, _ => .nan    | _, .nan => .nan    | .posInf, .posZero => .nan    | .posInf, .negZero => .nan    | .negInf, .posZero => .nan    | .negInf, .negZero => .nan    | .posZero, .posInf => .nan    | .negZero, .posInf => .nan    | .posZero, .negInf => .nan    | .negZero, .negInf => .nan    | va, vb => let neg := (match va with        | .negInf | .negZero => true        | .finite(m) => M.lt(m, M.ofNat(0))        | _ => false      end) != (match vb with        | .negInf | .negZero => true        | .finite(m) => M.lt(m, M.ofNat(0))        | _ => false      end) in match M.realOf(va), M.realOf(vb) with        | some(x), some(y) => if M.eq(M.mul(x, y), M.ofNat(0)) then if neg then .negZero else .posZero else M.numView(M.numOf(M.mul(x, y)))        | _, _ => if neg then .negInf else .posInf      end  end;
6.1.6.1.5 Number::divide sec-numeric-types-number-divide

Return 𝔽(ℝ(x) / ℝ(y)).

Number::divide's steps, a row each.

numDiv_view
SpecTec
law numDiv_view : forall a b, M.numView(M.numDiv(a, b)) = match M.numView(a), M.numView(b) with    | .nan, _ => .nan    | _, .nan => .nan    | .posInf, .posInf => .nan    | .posInf, .negInf => .nan    | .negInf, .posInf => .nan    | .negInf, .negInf => .nan    | .posZero, .posZero => .nan    | .posZero, .negZero => .nan    | .negZero, .posZero => .nan    | .negZero, .negZero => .nan    | va, vb => let neg := (match va with        | .negInf | .negZero => true        | .finite(m) => M.lt(m, M.ofNat(0))        | _ => false      end) != (match vb with        | .negInf | .negZero => true        | .finite(m) => M.lt(m, M.ofNat(0))        | _ => false      end) in match va, vb with        | .posInf, _ | .negInf, _ => if neg then .negInf else .posInf        | _, .posInf | _, .negInf => if neg then .negZero else .posZero        | _, .posZero | _, .negZero => if neg then .negInf else .posInf        | _, _ => match M.realOf(va), M.realOf(vb) with            | some(x), some(y) => if M.eq(M.div(x, y), M.ofNat(0)) then if neg then .negZero else .posZero else M.numView(M.numOf(M.div(x, y)))            | _, _ => .nan          end      end  end;
6.1.6.1.12 Number::lessThan sec-numeric-types-number-lessThan

If ℝ(x) < ℝ(y), return true.

Number::lessThan's steps, undefined read as false.

numLt_def
SpecTec
law numLt_def : forall a b, M.numLt(a, b) = match M.numView(a), M.numView(b) with    | .nan, _ => false    | _, .nan => false    | .negInf, .negInf => false    | .negInf, _ => true    | _, .negInf => false    | .posInf, _ => false    | _, .posInf => true    | va, vb => match M.realOf(va), M.realOf(vb) with        | some(x), some(y) => M.lt(x, y)        | _, _ => false      end  end;
6.1.6.1.13 Number::equal sec-numeric-types-number-equal

If x is +0𝔽 and y is -0𝔽, return true.

Number::equal's steps.

numEq_def
SpecTec
law numEq_def : forall a b, M.numEq(a, b) = match M.numView(a), M.numView(b) with    | .nan, _ => false    | _, .nan => false    | .posInf, .posInf => true    | .negInf, .negInf => true    | va, vb => match M.realOf(va), M.realOf(vb) with        | some(x), some(y) => M.eq(x, y)        | _, _ => false      end  end;
6.1.6.1.20 Number::toString sec-numeric-types-number-tostring

Let n, k, and s be integers such that k ≥ 1, … and k is as small as possible.

For a finite positive x and a radix from 2 to 36, numDigits meets the step's condition (digitsOf), and no n, k and s that meet it have a smaller k.

numDigits_least
SpecTec
law numDigits_least : forall x m (r : Nat), M.numView(x) = .finite(m) -> M.lt(M.ofNat(0), m) = true -> 2 <= r -> r <= 36 -> M.digitsOf(x, M.ofNat(r), M.numDigits(x, M.ofNat(r)).1, M.numDigits(x, M.ofNat(r)).2.1, M.numDigits(x, M.ofNat(r)).2.2) /\ forall n k s, M.digitsOf(x, M.ofNat(r), n, k, s) -> M.numDigits(x, M.ofNat(r)).2.1 <= k;
Auxiliary definitions (3): Model.isPoint, Model.allPoints, Model.isByte
def

Model.isPoint

05-model-laws.spectec:206

§6.1.4: m is a code point's value, an integer in [0, 0x10FFFF].

def Model.isPoint (M : Model) (m : M.Math) : Prop := M.isInt(m) = true /\ M.lt(m, M.ofNat(0)) = false /\ M.lt(M.ofNat(0x10FFFF), m) = false
def

Model.allPoints

05-model-laws.spectec:209

Every element of xs is a code point.

def Model.allPoints (M : Model) (xs : M.Seq(M.Math)) : Prop := forall i x, M.seqGet(xs, i) = some(x) -> M.isPoint(x)
def

Model.isByte

05-model-laws.spectec:212

§6.2.9: b is a byte value, an integer in [0, 255].

def Model.isByte (M : Model) (b : M.Math) : Prop := M.isInt(b) = true /\ M.lt(b, M.ofNat(0)) = false /\ M.lt(b, M.ofNat(256)) = true
laws

Model.ByteLaws

05-model-laws.spectec:215

The laws of Data Block byte sequences (§6.2.9, sec-data-blocks).

Proved as part of Model.Laws (its field bytes).

laws Model.ByteLaws (M : Model)

9 laws

6.2.9 Data Blocks sec-data-blocks

A Data Block value is created with a fixed number of bytes that each have the initial value 0.

Every byte read from a Data Block is an integer from 0 to 255.

bytesGet_byte
SpecTec
law bytesGet_byte : forall bs i b, M.bytesGet(bs, i) = some(b) -> M.isByte(b);
laws

Model.Laws

05-model-laws.spectec:256

The laws a model of the spec's sorts satisfies (§5.2, sec-mathematical-operations; §6, sec-ecmascript-data-types-and-values).

Proved of the engine's model: Engine.Impl.laws, the first part of Implements.model.

laws Model.Laws (M : Model)

116 laws

5.2.6 Mathematical Operations sec-mathematical-operations

When applied to mathematical values, the operators refer to the usual mathematical operations.

Addition is commutative.

add_comm
SpecTec
law add_comm : forall a b, M.add(a, b) = M.add(b, a);

Multiplication is commutative.

mul_comm
SpecTec
law mul_comm : forall a b, M.mul(a, b) = M.mul(b, a);

1 is the multiplicative identity.

mul_one
SpecTec
law mul_one : forall a, M.mul(a, M.ofNat(1)) = a;

Division by a non-zero value inverts multiplication.

div_mul
SpecTec
law div_mul : forall a b, b <> M.ofNat(0) -> M.mul(M.div(a, b), b) = a;
5.2.6 Mathematical Operations sec-mathematical-operations

Mathematical values: Arbitrary real numbers, used as the default numeric type.

5.2.6 Mathematical Operations sec-mathematical-operations

When applied to mathematical values, the operators refer to the usual mathematical operations.

< is irreflexive.

lt_irrefl
SpecTec
law lt_irrefl : forall a, M.lt(a, a) = false;

< is transitive.

lt_trans
SpecTec
law lt_trans : forall a b c, M.lt(a, b) = true -> M.lt(b, c) = true -> M.lt(a, c) = true;
5.2.6 Mathematical Operations sec-mathematical-operations

Mathematical values: Arbitrary real numbers, used as the default numeric type.

< is total, as on the reals.

lt_total
SpecTec
law lt_total : forall a b, M.lt(a, b) = true \/ a = b \/ M.lt(b, a) = true;
5.2.6 Mathematical Operations sec-mathematical-operations

When applied to mathematical values, the operators refer to the usual mathematical operations.

= on mathematical values is equality.

eq_iff
SpecTec
law eq_iff : forall a b, M.eq(a, b) = true <=> a = b;
5.2.6 Mathematical Operations sec-mathematical-operations

The mathematical function floor(x) produces the largest integer (closest to +∞) that is not larger than x.

7.1.4.1.3 RoundMVResult sec-roundmvresult

a digit is significant if it is not zero or there is a non-zero digit to its left and there is a non-zero digit to its right

5.2.6 Mathematical Operations sec-mathematical-operations

When the term integer is used in this specification, it refers to a mathematical value which is in the set of integers, unless otherwise stated.

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

are represented in ECMAScript as a single special NaN ("not a number") value.

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

There are two other special values, called positive Infinity and negative Infinity.

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

Note that there is both a positive zero and a negative zero.

6.1.6.1 The Number Type sec-ecmascript-language-types-number-type

representing the double-precision floating point IEEE 754-2019 binary64 values

The Number type's value set, 𝔽 and the operations (NumLaws).

num
SpecTec
law num : M.NumLaws;
5.2.6 Mathematical Operations sec-mathematical-operations

BigInts: ECMAScript language values representing arbitrary integers in a one-to-one correspondence.

6.1.4 The String Type sec-ecmascript-language-types-string-type

These positions are indexed with non-negative integers. The first element (if any) is at index 0, the next element (if any) at index 1, and so on.

A code unit is at a non-negative integer index below the length and is in [0, 65535].

strAt_some
SpecTec
law strAt_some : forall s i c, M.strAt(s, i) = some(c) -> (exists k, i = M.ofNat(k) /\ M.lt(i, M.strLen(s)) = true) /\ exists u, u < 65536 /\ c = M.ofNat(u);
6.1.4 The String Type sec-ecmascript-language-types-string-type

The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values

6.1.4 The String Type sec-ecmascript-language-types-string-type

denotes the String value whose sequence of code units is the concatenation of the code units (in order) of each of the arguments (in order)

6.1.4 The String Type sec-ecmascript-language-types-string-type

denotes the String value consisting of the consecutive code units of string beginning at index inclusiveStart and ending immediately before index exclusiveEnd

6.1.4 The String Type sec-ecmascript-language-types-string-type

(where each argument is a String value, a code unit, or a sequence of code units)

22.1.3.18 String.prototype.repeat sec-string.prototype.repeat

If n = 0, return the empty String.

22.1.3.18 String.prototype.repeat sec-string.prototype.repeat

Return the String value that is made from n copies of string appended together.

6.1.6.1.20 Number::toString sec-numeric-types-number-tostring

The digits used in the representation of a number using radix r are taken from the first r code units of "0123456789abcdefghijklmnopqrstuvwxyz" in order.

6.1.6.1.20 Number::toString sec-numeric-types-number-tostring

It represents x as a String using a positional numeral system with radix radix.

The representation of numbers with magnitude greater than or equal to 1𝔽 never includes leading zeroes.

5.2.8 Identity sec-identity

Values without identity are equal to other values without identity if all of their innate characteristics are the same

String equality is equality of code-unit sequences.

strEq_iff
SpecTec
law strEq_iff : forall s t, M.strEq(s, t) = true <=> s = t;
6.1.4 The String Type sec-ecmascript-language-types-string-type

each element in the String is treated as a UTF-16 code unit value

6.1.4 The String Type sec-ecmascript-language-types-string-type

The length of a String is the number of elements (i.e., 16-bit values) within it.

6.1.4 The String Type sec-ecmascript-language-types-string-type

These positions are indexed with non-negative integers. The first element (if any) is at index 0, the next element (if any) at index 1, and so on.

6.1.5 The Symbol Type sec-ecmascript-language-types-symbol-type

Each Symbol is unique and immutable.

Symbol equality is identity.

symEq_iff
SpecTec
law symEq_iff : forall a b, M.symEq(a, b) = true <=> a = b;
6.1.5 The Symbol Type sec-ecmascript-language-types-symbol-type

Each Symbol has an immutable [[Description]] internal slot whose value is either a String or undefined.

6.1.5 The Symbol Type sec-ecmascript-language-types-symbol-type

Each Symbol is unique and immutable.

Each creation makes a different Symbol.

symNew_inj
SpecTec
law symNew_inj : forall k d k' d', M.symNew(k, d) = M.symNew(k', d') -> k = k';
5.2.8 Identity sec-identity

each value with identity is unique and therefore only equal to itself.

An object is its identity.

addrEq_iff
SpecTec
law addrEq_iff : forall a b, M.addrEq(a, b) = true <=> a = b;

A Parse Node, a value with identity, is equal only to itself.

nodeEq_iff
SpecTec
law nodeEq_iff : forall a b, M.nodeEq(a, b) = true <=> a = b;
6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

The elements of a list may be randomly accessed using 0-origin indices.

An element is at a non-negative integer index below the length.

seqGet_some
SpecTec
law seqGet_some : forall {α} (xs : M.Seq(α)) i (a : α), M.seqGet(xs, i) = some(a) -> (exists k, i = M.ofNat(k) /\ M.lt(i, M.seqLen(xs)) = true);
6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

Values of the List type are simply ordered sequences of list elements containing the individual values.

A List is its sequence of elements.

seq_ext
SpecTec
law seq_ext : forall {α} (xs ys : M.Seq(α)), (forall i, M.seqGet(xs, i) = M.seqGet(ys, i)) -> xs = ys;
6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

A new empty List can be expressed as « ».

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

Values of the List type are simply ordered sequences of list elements containing the individual values.

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

denotes a new List value whose elements are the concatenation of the elements (in order) of each of the arguments (in order)

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

Values of the List type are simply ordered sequences of list elements containing the individual values.

6.2.9 Data Blocks sec-data-blocks

A Data Block value is created with a fixed number of bytes that each have the initial value 0.

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

When an algorithm iterates over the elements of a List without specifying an order, the order used is the order of the elements in the List.

A fold of « » is its start.

seqFoldl_nil
SpecTec
law seqFoldl_nil : forall {α β} (f : β -> α -> β) (b : β), M.seqFoldl(f, b, M.seqNil(α := α)) = b;
6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

Any fields that are not explicitly listed are considered to be absent.

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

A Record type value consists of one or more named fields.

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

Any fields that are not explicitly listed are considered to be absent.

6.2.2 The List and Record Specification Types sec-list-and-record-specification-type

A Record type value consists of one or more named fields.

6.1.7 The Object Type sec-object-type

The properties of an object are uniquely identified using property keys.

6.1.7 The Object Type sec-object-type

Each own property of an object must have a key that is distinct from the keys of the other own properties of that object.

10.1.11.1 OrdinaryOwnPropertyKeys sec-ordinaryownpropertykeys

in ascending chronological order of property creation

6.1.7 The Object Type sec-object-type

The properties of an object are uniquely identified using property keys.

5.2.8 Identity sec-identity

References to existing values with identity cannot be manifest simply by describing them

5.2.8 Identity sec-identity

each value with identity is unique and therefore only equal to itself.

5.2.8 Identity sec-identity

References to existing values with identity cannot be manifest simply by describing them

5.2.8 Identity sec-identity

Some values with identity are mutable and therefore can have their characteristics (except their identity) changed in-place, causing all holders of the value to observe the new characteristics.

5.2.8 Identity sec-identity

References to existing values with identity cannot be manifest simply by describing them

22.2.2.1 Notation sec-pattern-notation

A CharSet is a mathematical set of CharSetElements.

22.2.2.1 Notation sec-pattern-notation

For convenience, when working with CharSetElements of this kind, an individual character is treated interchangeably with a sequence of one character.

22.2.2.1 Notation sec-pattern-notation

A CharSet is a mathematical set of CharSetElements.

22.2.2.1 Notation sec-pattern-notation

For convenience, when working with CharSetElements of this kind, an individual character is treated interchangeably with a sequence of one character.

Let charSet be the CharSet containing all CharSetElements whose character database definition includes the property p with value “True”.

22.2.2.9.4 AllCharacters sec-allcharacters

Return the CharSet containing all Unicode code points c that do not have a Simple Case Folding mapping (that is, scf(c)=c).

22.2.2.9.3 WordCharacters sec-wordcharacters

Let extraWordChars be the CharSet containing all characters c such that c is not in basicWordChars but Canonicalize(regexpRecord, c) is in basicWordChars.

22.2.2.7.1 CharacterSetMatcher sec-runtime-semantics-charactersetmatcher-abstract-operation

If there exists a CharSetElement in charSet containing exactly one character a such that Canonicalize(regexpRecord, a) is cc, let found be true; else let found be false.

6.2.9 Data Blocks sec-data-blocks

A Data Block value is created with a fixed number of bytes that each have the initial value 0.

Auxiliary definitions (2): Model.stringPropertyNames, Model.codePointsOf
def

Model.stringPropertyNames

05-model-laws.spectec:725

The names of Table 67's binary properties of strings.

def Model.stringPropertyNames : List(String) := ["Basic_Emoji", "Emoji_Keycap_Sequence", "RGI_Emoji_Modifier_Sequence", "RGI_Emoji_Flag_Sequence", "RGI_Emoji_Tag_Sequence", "RGI_Emoji_ZWJ_Sequence", "RGI_Emoji"]
def

Model.codePointsOf

05-model-laws.spectec:728

A name's code points.

def Model.codePointsOf (M : Model) (s : String) : M.Seq(M.Math) := s.toList.foldr(fun c q => M.seqCons(M.ofNat(c.toNat), q), M.seqNil)
laws

Model.UnicodeLaws

05-model-laws.spectec:732

The laws of the Unicode operations (§6.1.4, sec-ecmascript-language-types-string-type; §22.1.3.15, sec-string.prototype.normalize): only what the text relies on.

Proved of the engine's model: Engine.Impl.unicodeLaws, part of Implements.model.

laws Model.UnicodeLaws (M : Model)

7 laws

22.2.2.7.3 Canonicalize sec-runtime-semantics-canonicalize-ch

If the file CaseFolding.txt of the Unicode Character Database provides a simple or common case folding mapping for char, return the result of applying that mapping to char.

22.1.3.28 String.prototype.toLowerCase sec-string.prototype.tolowercase

Let lowerText be toLowercase(sText), according to the Unicode Default Case Conversion algorithm.

22.1.3.30 String.prototype.toUpperCase sec-string.prototype.touppercase

except that the String is mapped using the toUppercase algorithm of the Unicode Default Case Conversion

Let charSet be the CharSet containing all CharSetElements whose character database definition includes the property p with value “True”.

or a binary Unicode property of strings listed in the “ Property name ” column of

22.1.3.15 String.prototype.normalize sec-string.prototype.normalize

Let normal be the String value that is the result of normalizing string into the normalization form named by form as specified in the latest Unicode Standard, Normalization Forms.

Auxiliary definitions (2): Model.InSeq, Model.KidOf
def

Model.InSeq

05-model-laws.spectec:764

c is an element of the List s.

def Model.InSeq {M : Model} {α : Type} (c : α) (s : M.Seq(α)) : Prop := M.seqFoldl(fun P x => P \/ x = c, False, s)
def

Model.KidOf

05-model-laws.spectec:767

n is a child of p: one of p's children is n by identity (nodeEq).

def Model.KidOf {M : Model} (n p : M.Node) : Prop := exists c, Model.InSeq(c, M.nodeKids(p)) /\ M.nodeEq(c, n) = true
relation

Model.Within

05-model-laws.spectec:770

p is r or a Parse Node in the tree below r, at any depth (§5.1.2, sec-parsing).

relation Model.Within {M : Model} : M.Node -> M.Node -> Prop
laws

Model.ParentLaws

05-model-laws.spectec:777

Model.nodeParent's laws (§5.1.2, sec-parsing): an answer is a node at or below r of which n is a child, and there is one wherever such a node is; with them nodeAncestors' and nodeHeight's.

Proved of the engine's model: Engine.Impl.parentLaws, part of Implements.model.

laws Model.ParentLaws (M : Model)

4 laws

5.1.2 Parsing sec-parsing

Moreover, it has zero or more children, one for each symbol on the production's right-hand side: each child is a Parse Node that is an instance of the corresponding symbol.

The parent the model gives of n in the tree rooted at r is a node at or below r of which n is a child.

sound
SpecTec
law sound : forall r n p, M.nodeParent(r, n) = some(p) -> Model.Within(r, p) /\ Model.KidOf(n, p);

The ancestors of n in the tree rooted at r are its parent, then the parent's ancestors; none for a node without a parent.

ancestors
SpecTec
law ancestors : forall r n, M.nodeAncestors(r, n) = match M.nodeParent(r, n) with    | some(p) => p :: M.nodeAncestors(r, p)    | none => []  end;

UTF-16 code points (§6.1.4)

From lean/Ecma/Prims/Strings.lean

The prose notation of §6.1.4 and §11 over Strings, code units and code points. A code unit or a code point is a mathematical value: an integer 0–0xFFFF or 0–0x10FFFF. A String is M.Str; where the text allows "a sequence of code units", a List of code units stands for the String. Source text is a List of code points (§11).

Auxiliary definitions (6): Prims.Strings.inRange, Prims.Strings.isPoint, Prims.Strings.isLead, Prims.Strings.isTrail, …
def

Prims.Strings.inRange

Strings.lean:23

An integer in [0, hi].

def inRange (hi : Nat) (m : M.Math) : Bool :=  M.isInt m && !M.lt m (M.ofNat 0) && !M.lt (M.ofNat hi) m
def

Prims.Strings.isPoint

Strings.lean:30

A code point's value (§6.1.4, sec-ecmascript-language-types-string-type).

def isPoint (m : M.Math) : Bool := inRange 0x10FFFF m
def

Prims.Strings.isLead

Strings.lean:190

Whether a code unit is a leading surrogate (§6.1.4, sec-ecmascript-language-types-string-type).

def isLead (u : M.Math) : Bool := !M.lt u (M.ofNat 0xD800) && !M.lt (M.ofNat 0xDBFF) u
def

Prims.Strings.isTrail

Strings.lean:193

Whether a code unit is a trailing surrogate (§6.1.4, sec-ecmascript-language-types-string-type).

def isTrail (u : M.Math) : Bool := !M.lt u (M.ofNat 0xDC00) && !M.lt (M.ofNat 0xDFFF) u
def

Prims.Strings.pairValue

Strings.lean:196

§11.1.4 (sec-utf16decodesurrogatepair): the code point of a surrogate pair.

def pairValue (c1 c2 : M.Math) : M.Math :=  M.add (M.add (M.mul (M.sub c1 (M.ofNat 0xD800)) (M.ofNat 0x400)) (M.sub c2 (M.ofNat 0xDC00)))    (M.ofNat 0x10000)
def

Prims.Strings.strPoints

Strings.lean:202

§6.1.4 (sec-ecmascript-language-types-string-type): a String's code points: a lead and trail surrogate pair as one, any other code unit as itself.

def strPoints (t : M.Str) : M.Seq M.Math :=  let (acc, pend) := M.seqFoldl (fun (st : M.Seq M.Math × Option M.Math) u => match st with    | (acc, some l) =>      if isTrail u then (M.seqPush acc (pairValue l u), none)      else if isLead u then (M.seqPush acc l, some u)      else (M.seqPush (M.seqPush acc l) u, none)    | (acc, none) => if isLead u then (acc, some u) else (M.seqPush acc u, none))    (M.seqNil, none) (M.strUnits t)  match pend with  | some l => M.seqPush acc l  | none => acc