Heavy code: Lean, Isabelle, Mizar#
The Robbins theorem has been formalized in several proof assistants. Each formalization is several hundred lines of
code — a decent piece of “heavy” code with proofs in Boolean algebra. The script obce/pobierz.py fetches four of them
(in pinned versions, with SHA-256 checksums checked) together with the Otter and EQP proofs:
File |
System |
Author, year |
Licence |
|---|---|---|---|
|
Lean 4, mathlib |
J. Tan, 2026 |
Apache 2.0 |
|
Lean 3, a mathlib branch |
B. Mehta, 2021 |
Apache 2.0 |
|
Isabelle/HOL, Archive of Formal Proofs |
M. Wampler-Doty, 2010 |
BSD |
|
Mizar, Mizar Mathematical Library |
A. Grabowski, 2001 |
GPL 3 or CC BY-SA 3.0 |
In this part we treat these files as data: we count what they are made of and look for places that the tactics of part 2 could compress.
Four proofs of one theorem#
First the size. We remove comments and descriptions, because they are not part of the proof; we count lines of code and theorems (lemmas, theorems, definitions):
import re
def without_comments(content, system):
"""The code without comments and description blocks."""
if system == "lean":
content = re.sub(r"/-.*?-/", "", content, flags=re.S)
marker = "--"
elif system == "isabelle":
content = re.sub(r"\(\*.*?\*\)", "", content, flags=re.S)
content = re.sub(r"\b(?:text|section|subsection)\s*\\<open>.*?\\<close>", "", content, flags=re.S)
marker = None
else:
marker = "::"
return [w for w in content.splitlines() if w.strip() and not (marker and w.strip().startswith(marker))]
FILES = {
"Lean 4 (mathlib)": ("mathlib4/Robbins.lean", "lean", r"^(?:private )?(?:lemma|theorem|def|instance)\b"),
"Lean 3": ("mathlib3/robbins.lean", "lean", r"^(?:private )?(?:lemma|theorem|def|instance)\b"),
"Isabelle (AFP)": ("afp/Robbins_Conjecture.thy", "isabelle", r"^\s*(?:lemma|theorem|corollary|definition)\b"),
"Mizar (MML)": ("mizar/robbins1.miz", "mizar", r"^\s*(?:theorem|definition)\b"),
}
loc = {}
for name, (file, system, declaration) in FILES.items():
content = (FOREIGN / file).read_text()
loc[name] = "\n".join(without_comments(content, system))
print(f"{name:18} {len(content.splitlines()):5} lines, of these code {len(loc[name].splitlines()):5}, "
f"theorems and definitions {len(re.findall(declaration, content, re.M)):3}")
Lean 4 (mathlib) 406 lines, of these code 311, theorems and definitions 48
Lean 3 458 lines, of these code 394, theorems and definitions 78
Isabelle (AFP) 984 lines, of these code 776, theorems and definitions 82
Mizar (MML) 1920 lines, of these code 1778, theorems and definitions 86
The same theorem (the Isabelle and Lean 4 versions follow Mann’s proof, the Mizar version — the work of McCune, Huntington and Dahn) takes from about three hundred to almost two thousand lines. The difference comes from the tools: what the system can do by itself and what has to be spelled out for it.
Lean 4: expansions written by hand, bookkeeping in ac_rfl#
The author of the mathlib version deliberately does not use the automation grind — in the description of the file
he writes that he wants Mann’s manipulations to stay visible, so he allows only ac_rfl for reordering summands and
lia for comparing numbers. Let us count the tactics:
lean4 = loc["Lean 4 (mathlib)"]
for tactic in ["rw", "nth_rw", "rwa", "simp_rw", "ac_rfl", "conv_lhs", "conv_rhs", "lia", "grind"]:
print(f"{tactic:8} {len(re.findall(rf'\b{tactic}\b', lean4)):3}")
print("expansions ← robbins:", len(re.findall(r"← robbins\b", lean4)))
content4 = (FOREIGN / "mathlib4/Robbins.lean").read_text()
lemmas_44_49 = content4[content4.index("lemma mann_44"):content4.index("def q : α")]
print(" of these in lemmas 44–49:", len(re.findall(r"← robbins\b", lemmas_44_49)))
print("add_comm:", len(re.findall(r"\badd_comm\b", lean4)), " add_assoc:", len(re.findall(r"\badd_assoc\b", lean4)),
" show … by ac_rfl:", len(re.findall(r"show [^\n]*by ac_rfl", lean4)))
rw 74
nth_rw 28
rwa 3
simp_rw 5
ac_rfl 33
conv_lhs 3
conv_rhs 11
lia 7
grind 0
expansions ← robbins: 25
of these in lemmas 44–49: 7
add_comm: 38 add_assoc: 28 show … by ac_rfl: 7
Thirty-three times ac_rfl — this is the bookkeeping of associativity and commutativity, like our ac. Twenty-five
times ← robbins with arguments given by hand — these are the creative expansions that the tactic auto found in
part 2. For lemmas 44–49 (seven of these expansions) auto reproduces them by itself; in the others the space of
possibilities is too large and a hint pays off. On top of that come several dozen manual reorderings: add_comm and
add_assoc inside rw and show … by ac_rfl before a rewrite — this is a job for the tactic rw_ac.
Isabelle: chains of reorderings and metis#
Wampler-Doty’s version is written in the Isar language: a proof is a chain of equations also … have … finally, and
every step is justified by the automation metis or simp. Some steps are pure reorderings of summands:
isabelle = loc["Isabelle (AFP)"]
print("metis calls:", len(re.findall(r"\bmetis\b", isabelle)))
print("also steps: ", len(re.findall(r"\balso\b", isabelle)))
only_ac = re.findall(r"also from (?:sup_assoc|sup_comm) have", isabelle)
print("also steps justified only by associativity or commutativity:", len(only_ac))
metis calls: 110
also steps: 78
also steps justified only by associativity or commutativity: 30
Here is a fragment of such a chain (© M. Wampler-Doty, Archive of Formal Proofs, BSD licence; \<squnion> is the sum,
- the complement, \<dots> — the right side of the previous step):
lines = (FOREIGN / "afp/Robbins_Conjecture.thy").read_text().splitlines()
start = next(i for i, w in enumerate(lines) if "also from sup_assoc have" in w)
print("\n".join(w[:110] for w in lines[start:start + 6]))
also from sup_assoc have
"\<dots> = -(-(-x) \<squnion> -(-x)) \<squnion>
(-(-(-x) \<squnion> -(-(-x))) \<squnion> -(-(-x) \<squnion> -x)) \<squnion>
-(-(-(-x)) \<squnion> -x)"
by simp
also from sup_comm have
Every also from sup_assoc have rewrites one bracket — exactly what our tactic ac does in one step of a script.
Thirty such steps in chains of equations would disappear if the system had a tactic closing “equal up to A1 and A2”
(Isabelle has such tools — simplification with AC rules — but the author did not use them here).
On the other hand metis is the most radical solution: an automated prover (resolution) called as a tactic. It
searches for a proof of a step from the given facts by itself, and turns the result into a proof checked by Isabelle’s
kernel. It is the same idea as our tactic auto, only much stronger. The script is short, but one cannot see why
the step is true — and when the facts change, metis may stop finding the proof.
Mizar: everything explicit#
Mizar has no tactics in Lean’s sense. A proof is a text in which every equational step .= has a justification
by …, and the checker verifies that the step follows from the given facts by simple reasoning. Associativity and
commutativity therefore have to be cited every time: in Mizar these are the definitions LATTICES:def 5
(associativity of the sum) and LATTICES:def 4 (commutativity).
mizar = (FOREIGN / "mizar/robbins1.miz").read_text()
equation_steps = sum(1 for w in mizar.splitlines() if w.strip().startswith(".="))
only_ac_lines = 0
for w in mizar.splitlines():
m = re.search(r"\bby\s+([^;]*?)\s*;?\s*$", w)
if m and {x.strip() for x in m.group(1).split(",") if x.strip()} <= {"LATTICES:def 4", "LATTICES:def 5"}:
only_ac_lines += 1
print("steps .= :", equation_steps)
print("lines justified only by associativity or commutativity:", only_ac_lines)
start = next(i for i, w in enumerate(mizar.splitlines()) if ".= ABC + (D + E + (F + GH)) by LATTICES:def 5" in w)
print("\n".join(mizar.splitlines()[start - 2:start + 5]))
steps .= : 319
lines justified only by associativity or commutativity: 81
then (a *' b) + (a *' c) + (a *' (b + c))` = ABC + (D + E + F + GH) by
LATTICES:def 5
.= ABC + (D + E + (F + GH)) by LATTICES:def 5
.= ABC + (D + E) + (F + GH) by LATTICES:def 5
.= ABC + D + E + (F + GH) by LATTICES:def 5
.= ABC + D + (E + (F + GH)) by LATTICES:def 5
.= ABC + D + (E + (F + G + H)) by LATTICES:def 5
(Fragment of theorem 4.32 of robbins1.miz; © Association of Mizar Users, A. Grabowski, licence GPL 3 or CC BY-SA
3.0.) Five steps in a row regroup the same sum of eight summands. In Mizar one compresses differently: instead of
tactics there are abbreviations set A = … (visible above: ABC, GH) and auxiliary lemmas. In return the proof can
be read without running anything — that is why it is the longest.
Machine proofs#
Proofs found by programs are measured in inference steps. Let us count them in the downloaded files, together with the proof by Robert Veroff (2002) — one continuous derivation of Huntington’s equation in Otter:
def otter_length(file):
return int(re.search(r"Length of proof is (\d+)", (FOREIGN / file).read_text()).group(1))
def eqp_steps(file):
lines = re.findall(r"^\d+ \(wt=\d+\) \[(.*?)\] (.*)\.$", (FOREIGN / file).read_text(), re.M)
return sum(1 for justification, eq_text in lines if justification and not eq_text.startswith("-("))
machine = {
"EQP, condition 1 (m5-50)": eqp_steps("mccune/eqp-lemma3.proof.txt"),
"EQP, Huntington's equation directly (m5-50)": eqp_steps("mccune/eqp-hunt.proof.txt"),
"Otter, condition 1": otter_length("mccune/otter-lemma3.proof.txt"),
"Otter (Veroff 2002), Huntington's equation directly": otter_length("veroff/r2h.pf"),
}
for verdict, n in machine.items():
print(f"{verdict:50} {n:4} steps")
EQP, condition 1 (m5-50) 12 steps
EQP, Huntington's equation directly (m5-50) 86 steps
Otter, condition 1 40 steps
Otter (Veroff 2002), Huntington's equation directly 142 steps
Again: EQP has associativity and commutativity built in, Otter does not — and the Otter proof is longer, although it proves the same.
Other theorems about Boolean algebras have long machine proofs too. In January 2025 Stephen Wolfram described (Who Can Understand the Proof? A Window on Formalized Mathematics) a proof of his single axiom for Boolean algebra with the NAND operation, found by a program: 307 steps, of which the derivation of commutativity alone takes 104. Of the 101 lemmas of this proof, 47 come from substitution and 54 from “bisubstitution” (critical pairs) — and none of them has a name that would tell a human anything. It is the opposite end from mathlib: a complete proof, but without a structure one can understand.
What would compress it#
Let us collect what the numbers showed:
Repeated pattern |
Where |
How many |
The tactic that swallows it |
|---|---|---|---|
reordering summands |
Isabelle ( |
30, 81, 28 of 41 |
|
reordering before |
Lean ( |
38, 28, 7 |
|
creative expansion with the Robbins equation |
Lean ( |
25 |
|
the same proof for different \(k\) |
Lean ( |
lemma 36 |
a loop (unrolled induction) |
long witness terms |
all |
\(e\) has 329 symbols |
abbreviations: |
The conclusion is more general than the Robbins problem. A good formal proof is a compressed proof: repeated
fragments have been turned into lemmas, bookkeeping into tactics, and long terms into abbreviations. When compression
is missing (Mizar, Otter), the proof is long, but every step can be seen. When automation does all the work (metis),
the script is short, but one cannot see why a step holds. And a machine proof without named lemmas (Wolfram) can be long
and incomprehensible at the same time. The art is to compress the bookkeeping and to leave the ideas in sight.
References and places worth a visit#
J. Tan,
Archive/Robbins.lean(mathlib, 2026) and B. Mehta, the Lean 3 version in a mathlib branch (2021).M. Wampler-Doty, A Complete Proof of the Robbins Conjecture, Archive of Formal Proofs (2010).
A. Grabowski, Robbins Algebras vs. Boolean Algebras, Formalized Mathematics 9(4), 681–690 (2001); in the MML as ROBBINS1.
R. Veroff, a page on the Robbins problem with a derivation of Huntington’s equation in Otter.
S. Wolfram, Who Can Understand the Proof? A Window on Formalized Mathematics (9 January 2025).
F. Wiedijk, The De Bruijn Factor (TPHOLs 2000) — how to measure how many times longer a formal proof is than the mathematical text (it comes out at about four).