Merge sort of records in time on a multitape Turing machine
ProvedIntMul.TM.merge_sortSorting records on tapes. There are a single deterministic multitape Turing machine and a constant with the following property. Let and let be records, each consisting of a key and a payload . On input
of length , the machine halts within steps and outputs the same records in the same format, stably sorted by the numerical value of the key (records with equal keys keep their input order).
This is the label-and-sort primitive used by Harvey and van der Hoeven for data rearrangements (e.g. the Agarwal–Cooley isomorphism in Proposition 5.4).
Formalization Note The output is specified as the encoding of Mathlib's stable List.mergeSort by ; since all keys have the same length, equal values mean equal keys. The input word determines the record list (records are separated by single s, keys and payloads alternate), so the required output is well defined. Machine model: IntMul_MultitapeModel with arbitrary inputs from IntMul_MultitapeWords.
import Mathlib import Definitions.Def_IntMul_MultitapeModel import Definitions.Def_IntMul_MultitapeWords
namespace IntMul.TM
theorem merge_sort :
∃ M : MultitapeTM, ∃ c : ℝ, 0 < c ∧
∀ (ℓ q : ℕ) (recs : List (List Bool × List Bool)),
(∀ r ∈ recs, r.1.length = ℓ ∧ r.2.length = q) →
let enc : List (List Bool × List Bool) → List (Option Bool) := fun rs =>
joinSep (rs.flatMap fun r => [bits r.1, bits r.2])
∃ t : ℕ,
(t : ℝ) ≤ c * (((enc recs).length + 1 : ℕ) : ℝ) * ((Nat.clog 2 recs.length + 1 : ℕ) : ℝ) ∧
M.HaltsWithOutputW (enc recs) t
(enc (recs.mergeSort fun a b => decide (val a.1 ≤ val b.1))) := by sorry
end IntMul.TMRead-back
What the Lean code literally says, in plain math · claude-opus-5-5
Theorem IntMul.TM.merge_sort. There exist a deterministic multitape Turing machine and a real constant (chosen after ; independent of everything below) such that the following holds for all and every finite list of records (), each record a pair of bit strings (key , payload ), under the hypothesis that every record has and .
Define the encoding of a record list as the word over
(the strings joined by single , no leading/trailing ; the empty list gives the empty word). Let for ( for ). Then there is with
such that , started on the word , is after exactly steps in with output tape , where is the list produced by the library's merge sort applied to with the comparison " iff ". Here reads a bit string as binary, leftmost bit most significant; is the integer ceiling-log with the convention . This merge sort is stable: is the permutation of sorted nondecreasingly by key value in which records with equal key value keep their input order (and, since all keys have the same length , equal value means identical key string).
Started on a word over means: state , tape holds (letters as ), every other tape holds , all heads at cell . Machine model. A machine has a finite alphabet containing pairwise distinct symbols (blank), (start marker), plus possibly further symbols; a finite state set with ; a fixed number of tapes with cells (tape = read-only input tape, tape = output tape, others work tapes); a transition function acting simultaneously on all tapes (write one symbol and move per tape), which never erases or writes except by rewriting it in place, never moves left from , and leaves the configuration fixed in . "Halts with output at step " means: after exactly steps from the initial configuration the state is and the entire output tape is (cells hold , every later cell blank); work tapes, input tape and head positions are unconstrained. Since is a fixed point, "for some " is the same as "halts within steps".
Quantifiers and edge cases. and are universally quantified before ; when is empty the hypothesis holds for every and the requirement is: on the empty input, halt within steps with empty output (output tape ). (empty keys) and/or (empty payloads) are allowed: e.g. with all keys are equal and the output must equal the input; with and the input is . The input word determines (splitting at every gives exactly the fields for , the empty word only arises from ), so the required output is a well-defined function of the input and the statement is not unsatisfiable on that ground. The machine is not told . The logarithmic factor counts records , not input length; one constant serves all (no threshold). Words not of the form with uniform field lengths are unconstrained.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.