Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Merge sort of mmm records in time O(Nlog⁡m)O(N\log m)O(Nlogm) on a multitape Turing machine

Proved
IntMul.TM.merge_sort

by avi · Oct 9, 2026 · Mathlib 0df444a (Lean v4.33.1)

complexity-theorysortingturing-machines

Sorting records on tapes. There are a single deterministic multitape Turing machine MMM and a constant c>0c>0c>0 with the following property. Let ℓ,q≥0\ell,q\ge0ℓ,q≥0 and let (k1,p1),…,(km,pm)(k_1,p_1),\dots,(k_m,p_m)(k1​,p1​),…,(km​,pm​) be records, each consisting of a key ki∈{0,1}ℓk_i\in\{0,1\}^\ellki​∈{0,1}ℓ and a payload pi∈{0,1}qp_i\in\{0,1\}^qpi​∈{0,1}q. On input

k1#p1#k2#p2#⋯#km#pmk_1\#p_1\#k_2\#p_2\#\cdots\#k_m\#p_mk1​#p1​#k2​#p2​#⋯#km​#pm​

of length NNN, the machine halts within c (N+1)(⌈log⁡2m⌉+1)c\,(N+1)(\lceil\log_2m\rceil+1)c(N+1)(⌈log2​m⌉+1) 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 val⁡(k)\operatorname{val}(k)val(k); 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.

Preamble
import Mathlib
import Definitions.Def_IntMul_MultitapeModel
import Definitions.Def_IntMul_MultitapeWords
Formal statement
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.TM
Source
D. E. Knuth, The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed., Addison-Wesley 1998, §5.4 (external sorting / merging on tapes); used as 'merge sort in time O(tp log t) [31]' in Harvey–van der Hoeven, Ann. of Math. 193 (2021), proof of Proposition 4.7(ii), p. 33, and Proposition 5.4, step (2), p. 41.
Read-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 MMM and a real constant c>0c > 0c>0 (chosen after MMM; independent of everything below) such that the following holds for all ℓ,q∈N\ell, q \in \mathbb Nℓ,q∈N and every finite list of records R=(r1,…,rm)R = (r_1, \dots, r_m)R=(r1​,…,rm​) (m≥0m \ge 0m≥0), each record a pair ri=(ai,bi)r_i = (a_i, b_i)ri​=(ai​,bi​) of bit strings (key aia_iai​, payload bib_ibi​), under the hypothesis that every record has ∣ai∣=ℓ|a_i| = \ell∣ai​∣=ℓ and ∣bi∣=q|b_i| = q∣bi​∣=q.

Define the encoding of a record list as the word over {0,1,#}\{0,1,\#\}{0,1,#}

enc(r1,…,rm)=a1#b1#a2#b2#⋯#am#bm\mathrm{enc}(r_1,\dots,r_m) = a_1 \# b_1 \# a_2 \# b_2 \# \cdots \# a_m \# b_menc(r1​,…,rm​)=a1​#b1​#a2​#b2​#⋯#am​#bm​

(the 2m2m2m strings a1,b1,…,am,bma_1, b_1, \dots, a_m, b_ma1​,b1​,…,am​,bm​ joined by single #\##, no leading/trailing #\##; the empty list gives the empty word). Let N=∣enc(R)∣=m(ℓ+q)+2m−1N = |\mathrm{enc}(R)| = m(\ell + q) + 2m - 1N=∣enc(R)∣=m(ℓ+q)+2m−1 for m≥1m \ge 1m≥1 (N=0N = 0N=0 for m=0m = 0m=0). Then there is t∈Nt \in \mathbb Nt∈N with

t≤c (N+1) (⌈log⁡2m⌉+1)t \le c\,(N + 1)\,\bigl(\lceil \log_2 m \rceil + 1\bigr)t≤c(N+1)(⌈log2​m⌉+1)

such that MMM, started on the word enc(R)\mathrm{enc}(R)enc(R), is after exactly ttt steps in qhaltq_{\mathrm{halt}}qhalt​ with output tape ▹ enc(R′) □□⋯\triangleright\, \mathrm{enc}(R')\, \square\square\cdots▹enc(R′)□□⋯, where R′R'R′ is the list produced by the library's merge sort applied to RRR with the comparison "r⪯sr \preceq sr⪯s iff val(key of r)≤val(key of s)\mathrm{val}(\text{key of } r) \le \mathrm{val}(\text{key of } s)val(key of r)≤val(key of s)". Here val\mathrm{val}val reads a bit string as binary, leftmost bit most significant; ⌈log⁡2m⌉\lceil \log_2 m\rceil⌈log2​m⌉ is the integer ceiling-log with the convention ⌈log⁡20⌉=⌈log⁡21⌉=0\lceil\log_2 0\rceil = \lceil \log_2 1\rceil = 0⌈log2​0⌉=⌈log2​1⌉=0. This merge sort is stable: R′R'R′ is the permutation of RRR sorted nondecreasingly by key value in which records with equal key value keep their input order (and, since all keys have the same length ℓ\ellℓ, equal value means identical key string).

Started on a word www over {0,1,#}\{0,1,\#\}{0,1,#} means: state qstartq_{\mathrm{start}}qstart​, tape 000 holds ▹ w □□⋯\triangleright\, w\, \square\square\cdots▹w□□⋯ (letters as 0/1/#\mathtt 0/\mathtt 1/\#0/1/#), every other tape holds ▹□□⋯\triangleright\square\square\cdots▹□□⋯, all heads at cell 000. Machine model. A machine MMM has a finite alphabet containing pairwise distinct symbols □\square□ (blank), ▹\triangleright▹ (start marker), 0,1,#\mathtt 0, \mathtt 1, \#0,1,# plus possibly further symbols; a finite state set with qstart≠qhaltq_{\mathrm{start}} \ne q_{\mathrm{halt}}qstart​=qhalt​; a fixed number kM≥2k_M \ge 2kM​≥2 of tapes with cells 0,1,2,…0,1,2,\dots0,1,2,… (tape 000 = read-only input tape, tape 111 = output tape, others work tapes); a transition function acting simultaneously on all tapes (write one symbol and move ←,−,→\leftarrow,-,\rightarrow←,−,→ per tape), which never erases or writes ▹\triangleright▹ except by rewriting it in place, never moves left from ▹\triangleright▹, and leaves the configuration fixed in qhaltq_{\mathrm{halt}}qhalt​. "Halts with output vvv at step ttt" means: after exactly ttt steps from the initial configuration the state is qhaltq_{\mathrm{halt}}qhalt​ and the entire output tape is ▹ v □□⋯\triangleright\, v\, \square\square\cdots▹v□□⋯ (cells 1..∣v∣1..|v|1..∣v∣ hold vvv, every later cell blank); work tapes, input tape and head positions are unconstrained. Since qhaltq_{\mathrm{halt}}qhalt​ is a fixed point, "for some t≤Tt \le Tt≤T" is the same as "halts within TTT steps".

Quantifiers and edge cases. ℓ\ellℓ and qqq are universally quantified before RRR; when RRR is empty the hypothesis holds for every ℓ,q\ell, qℓ,q and the requirement is: on the empty input, halt within ccc steps with empty output (output tape ▹□□⋯\triangleright\square\square\cdots▹□□⋯). ℓ=0\ell = 0ℓ=0 (empty keys) and/or q=0q = 0q=0 (empty payloads) are allowed: e.g. with ℓ=0\ell = 0ℓ=0 all keys are equal and the output must equal the input; with ℓ=q=0\ell = q = 0ℓ=q=0 and m≥1m \ge 1m≥1 the input is #2m−1\#^{2m-1}#2m−1. The input word determines RRR (splitting at every #\## gives exactly the 2m2m2m fields for m≥1m \ge 1m≥1, the empty word only arises from m=0m = 0m=0), 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 ℓ,q,m\ell, q, mℓ,q,m. The logarithmic factor counts records mmm, not input length; one constant ccc serves all ℓ,q,m\ell, q, mℓ,q,m (no threshold). Words not of the form enc(R)\mathrm{enc}(R)enc(R) with uniform field lengths are unconstrained.

Human review
  • Endorsed by Shuze Chen · Oct 9, 2026

    Confirmed by the moderator at approval.

  • Endorsed by avi · Oct 9, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me