Appearance
A sort means a stable merge sort under a consistent comparator
Status: accepted · Date: 2026-11-11
Tolean gives xs.toSorted((a, b) => e) and [...xs].sort((a, b) => e) a meaning only when the comparator is consistent for the elements that it sorts. Then the result is List.mergeSort of the elements, where a comes after b only when the comparator gives a positive number for (a, b). The rule is in tolean/TypeScript/Spec/Eval.lean (sortValues).
What the spec leaves open
ECMA-262 does not name a sort algorithm. SortIndexedProperties (§23.1.3.30.1) asks only for an order of the elements in which no element comes after one that the comparator says is greater. Since ES2019 it also asks that elements that the comparator calls equal keep their order, so the sort is stable. When the comparator is not consistent, for example when it gives NaN or says that a < b and b < a, the spec leaves the order to the engine.
A consistent comparator makes "a does not come after b" a total preorder. Then exactly one order of the elements meets both conditions, and List.mergeSort gives it. Spec/Lemmas/Sort.lean proves the two conditions for that order with Lean's lemmas about List.mergeSort. So the interpreter gives the order that every engine gives.
Consistency is checked on the values
The interpreter calls the comparator on every pair of elements and checks that it is consistent for them. When it is not, the interpreter gives no value, as it does for any program that leaves the subset. The check reads only the elements, so a comparator such as (a, b) => a.t - b.t on number keys has a meaning for every array whose keys are finite. An array with a NaN key or an infinite key has none. The spec checks each element against itself too, and Infinity - Infinity is NaN, so one infinite key is enough.
The output of the translator runs the same comparator in Lean, as JSNumber.sortWith. The IR evaluator checks consistency in the same way as the interpreter, so the proof that the translator is sound needs no fact about the comparator.
The translator accepts comparators that it can prove consistent
The printed Lean sorts without the check, because a Lean function cannot give no value. So the translation computes what JavaScript computes only when the comparator is consistent. The translator therefore accepts only comparators of a few shapes, whose consistency Tolean proves once:
- a comparison of keys, such as
(a.k < b.k) ? -1 : (a.k > b.k) ? 1 : 0, where the key is abigintor astring, which are totally ordered; - the same comparison with a later comparison in place of the
0, which compares by a second key when the first keys are equal; - the same comparison on
numberkeys, which is consistent only when no key isNaN; a.t - b.tonnumberkeys, which is consistent only when every key is finite.
A certificate for a file that sorts by number keys states that condition as a precondition. The sign of -1 and 1 as numbers comes from SafeIntLaws, so a certificate for a file that sorts takes the laws of the number model as hypotheses.
The certifier implements the precondition for a direct comparison or difference sort of a list parameter, in either direction. Several keys may mix numbers, strings and bigints. Each numeric key has its own condition, which direct forwarding calls pass to their callers. NumberOrderLaws supplies the number facts, and sortValues_sortWith_on needs consistency only for that input. Other shapes remain in the pure TypeScript plan.
What we rejected
- Sort in place on any array.
xs.sort(cmp)changesxs, and the caller can holdxs. The subset has no way to show that change, so it sorts only an array literal, which nothing else holds. - Print a sort by key, such as
List.mergeSort xs (fun a b => a.k ≤ b.k). That output reads better. But it means what the comparator means only under the laws of the number model, and the proof that the translator is sound assumes no law. The output keeps the comparator, and a lemma turns it into a sort by key for a proof that wants one.
This page is docs/decisions/2026-11-11-sort.md at commit 5f1d031.
