Navier–Stokes: the Lean blow-up proof, cleaned up together
OpenAI's 2026 Lean proofs of blowup for forced Navier–Stokes and Euler, rerun by others, not refereed, no ruling from Clay; cleaned up together: more readable, faster to build

| 1 | """The dead-code landing on shoal/navier-stokes, from Treant's 12-03 deletions and the declaration graph of the site's main. |
| 2 | What goes = what Treant deleted, less what the project's keep rule holds back (the paper's results, what cites the paper, |
| 3 | all they refer to in terms, and what their text names). Order = waves; in a wave each family of files deletes what has |
| 4 | no user left outside its own pull request. |
| 5 | python3 -I planner.py <decls.jsonl> <site checkout> <fork clone> <out dir> |
| 6 | Writes <out>/plan.json (units with their declarations and line ranges) and prints the counts.""" |
| 7 | import bisect, collections, json, os, re, subprocess, sys |
| 8 | decls, site, fork, out = sys.argv[1:5] |
| 9 | def git(repo, *a): return subprocess.run(["git", "-C", repo, *a], capture_output=True, text=True, check=True).stdout |
| 10 | BASE, S03 = "origin/treant/baseline", "origin/treant/03-dead_theorems" |
| 11 | cons = [json.loads(l) for l in open(decls, encoding="utf-8")] |
| 12 | f_of = lambda m: m.replace(".", "/") + ".lean" |
| 13 | ranged = collections.defaultdict(list) |
| 14 | for c in cons: |
| 15 | if c["r"]: ranged[c["m"]].append((c["r"][0], c["r"][1], c["n"])) |
| 16 | group, head = {}, {} |
| 17 | for m, rs in ranged.items(): |
| 18 | rs.sort(key=lambda r: (r[0], -r[1])); idx = [] |
| 19 | for a, b, n in rs: |
| 20 | if idx and a <= idx[-1][1]: idx[-1] = (idx[-1][0], max(idx[-1][1], b)) |
| 21 | else: idx.append((a, b)); head[(m, a)] = n |
| 22 | starts = [g[0] for g in idx] |
| 23 | for a, b, n in rs: |
| 24 | g = idx[bisect.bisect_right(starts, a) - 1]; group[n] = (m, g[0], g[1]) |
| 25 | for c in cons: |
| 26 | if c["n"] not in group: |
| 27 | p = c["n"] |
| 28 | while "." in p: |
| 29 | p = p.rsplit(".", 1)[0] |
| 30 | if p in group and group[p][0] == c["m"]: group[c["n"]] = group[p]; break |
| 31 | G = set(group.values()); name_of = {g: head[(g[0], g[1])] for g in G} |
| 32 | raw = {c["n"]: c["u"] for c in cons} |
| 33 | sys.setrecursionlimit(100000) |
| 34 | def through(n, seen): |
| 35 | o = set() |
| 36 | for u in raw.get(n, ()): |
| 37 | g = group.get(u) |
| 38 | if g: o.add(g) |
| 39 | elif u not in seen: seen.add(u); o |= through(u, seen) |
| 40 | return o |
| 41 | uses = collections.defaultdict(set) |
| 42 | for c in cons: |
| 43 | a = group.get(c["n"]) |
| 44 | if a: uses[a] |= through(c["n"], set()) - {a} |
| 45 | users = collections.defaultdict(set) |
| 46 | for a, bs in uses.items(): |
| 47 | for b in bs: users[b].add(a) |
| 48 | src = {} |
| 49 | def text(m): |
| 50 | if m not in src: src[m] = open(f"{site}/{f_of(m)}", encoding="utf-8").read().split("\n") |
| 51 | return src[m] |
| 52 | body = lambda g: "\n".join(text(g[0])[g[1] - 1:g[2]]) |
| 53 | lines = lambda s: sum(g[2] - g[1] + 1 for g in s) |
| 54 | |
| 55 | # Treant's dead set on the site's main: by line where the file is the same, by the declaration's first lines where it is not |
| 56 | site_blobs = {l.split("\t")[1]: l.split()[2] for l in git(site, "ls-tree", "-r", "HEAD").splitlines()} |
| 57 | base_blobs = {l.split("\t")[1]: l.split()[2] for l in git(fork, "ls-tree", "-r", BASE).splitlines()} |
| 58 | s03_files = set(git(fork, "ls-tree", "-r", "--name-only", S03).splitlines()) |
| 59 | gone = {f for f in base_blobs if f.endswith(".lean") and f not in s03_files} |
| 60 | deleted, added, cur = collections.defaultdict(set), collections.defaultdict(set), None |
| 61 | for line in git(fork, "diff", "-U0", "--no-renames", "--diff-filter=M", BASE, S03, "--", "*.lean").splitlines(): |
| 62 | if line.startswith("--- a/"): cur = line[6:] |
| 63 | elif line.startswith("@@") and cur: |
| 64 | m = re.match(r"@@ -(\d+)(?:,(\d+))? ", line); a, n = int(m.group(1)), int(m.group(2) or 1); deleted[cur].update(range(a, a + n)) |
| 65 | elif line.startswith("+") and not line.startswith("+++") and cur: added[cur].add(line[1:]) |
| 66 | differ = {f for f in site_blobs if f.endswith(".lean") and site_blobs[f] != base_blobs.get(f)} |
| 67 | s03_text = {} |
| 68 | def kept_by_treant(f): |
| 69 | if f not in s03_text: s03_text[f] = git(fork, "show", f"{S03}:{f}") if f in s03_files else "" |
| 70 | return s03_text[f] |
| 71 | all_s03 = None |
| 72 | def first(g): # the declaration's first line with a name on it, as text |
| 73 | for l in text(g[0])[g[1] - 1:g[2]]: |
| 74 | if re.search(r"\b(theorem|lemma|def|abbrev|structure|instance|inductive|class)\b", l): return l.strip() |
| 75 | return None |
| 76 | tdead = set() |
| 77 | for g in G: |
| 78 | f = f_of(g[0]) |
| 79 | if f in gone: tdead.add(g) |
| 80 | elif f not in differ: |
| 81 | if f in deleted and all(l in deleted[f] for l in range(g[1], g[2] + 1)) and (first(g) or "") not in {a.strip() for a in added[f]}: tdead.add(g) |
| 82 | else: # moved by #30/#31: dead if Treant's tree has its first line nowhere in the file it came from |
| 83 | h = first(g) |
| 84 | origin = [x for x in ("NavierStokes/CorrectionStep.lean", f) if x in s03_files or x in base_blobs] |
| 85 | if h and all(h not in kept_by_treant(x) for x in origin) and any(h in git(fork, "show", f"{BASE}:{x}") for x in origin if x in base_blobs): tdead.add(g) |
| 86 | print(f"source declarations {len(G)}; Treant's deletions found on the site's main: {len(tdead)} ({lines(tdead)} lines), {sum(f_of(g[0]) in gone for g in tdead)} of them in the {sum(f in site_blobs for f in gone)} whole files that go, {sum(f_of(g[0]) in differ for g in tdead)} in files main has changed") |
| 87 | |
| 88 | # the keep rule |
| 89 | doc = {} |
| 90 | for c in cons: |
| 91 | g = group.get(c["n"]) |
| 92 | if g and c["d"] and name_of[g] == c["n"]: doc[g] = c["d"] |
| 93 | cites = re.compile(r"\b(Lemma|Theorem|Proposition|Corollary|Section|Equation|Definition)\s+\(?\d|§\s*\d|\(\d+\.\d+\)") |
| 94 | paper_mods = {"NavierStokes.PeriodicPaperTheorem", "NavierStokes.LocalAngularGrowth", "NavierStokes.LocalPaperHeat", "NavierStokes.PaperAdditionalResults", "NavierStokes.R3.Theorem", "NavierStokes.LocalPaperTheorem"} |
| 95 | roots = {g for g in G if g[0] in paper_mods or cites.search(doc.get(g, ""))} |
| 96 | imports = {} |
| 97 | def sees(m): |
| 98 | """The modules of this project that module m imports, itself included.""" |
| 99 | if m not in imports: |
| 100 | imports[m] = {m} |
| 101 | for l in text(m)[:400]: |
| 102 | w = re.match(r"\s*(?:public\s+|private\s+|meta\s+)*import\s+([\w.]+)", l) |
| 103 | if w and w.group(1) in outer_mods: imports[m] |= sees(w.group(1)) |
| 104 | return imports[m] |
| 105 | outer_mods = {g[0] for g in G} |
| 106 | plain = lambda n: re.sub(r"^_private\..*?\.0\.", "", n) |
| 107 | short = collections.defaultdict(set) # last name component -> Treant-dead declarations that carry it |
| 108 | for g in tdead: |
| 109 | if f_of(g[0]) not in gone: short[plain(name_of[g]).rsplit(".", 1)[-1]].add(g) # a file nothing imports goes whole, whatever its names |
| 110 | tok = re.compile(r"[A-Za-z_][A-Za-z0-9_'!?₀-₉]*(?:\.[A-Za-z_][A-Za-z0-9_'!?₀-₉]*)*") |
| 111 | def named_in(g): |
| 112 | """Treant-dead declarations that the text of g names: by a name theirs ends with, in a file g's file can see.""" |
| 113 | out_, vis = set(), sees(g[0]) |
| 114 | for t in set(tok.findall(body(g))): |
| 115 | for h in short.get(t.rsplit(".", 1)[-1], ()): |
| 116 | if h != g and h[0] in vis and (h[0] != g[0] or h[1] < g[1]) and ("." + plain(name_of[h])).endswith("." + t): out_.add(h) |
| 117 | return out_ |
| 118 | held, st = set(), [g for g in roots] |
| 119 | seen = set(st) |
| 120 | while st: |
| 121 | g = st.pop() |
| 122 | if g in tdead: held.add(g) |
| 123 | nxt = set(uses[g]) |
| 124 | if g in tdead: nxt |= named_in(g) # a declaration brought back must still read: what its text names comes back too |
| 125 | for b in nxt: |
| 126 | if b not in seen: seen.add(b); st.append(b) |
| 127 | held = {g for g in held if f_of(g[0]) not in gone} |
| 128 | go = tdead - held |
| 129 | # whatever a declaration that stays refers to stays too (a deletion the diff showed but Treant's tree did not make) |
| 130 | back, changed = 0, True |
| 131 | while changed: |
| 132 | changed = False |
| 133 | for g in list(go): |
| 134 | if any(u not in go for u in users[g]): go.discard(g); held.add(g); back += 1; changed = True |
| 135 | print(f"brought back because a declaration that stays refers to them: {back}") |
| 136 | bad = [g for g in G - go if uses[g] & go] |
| 137 | loose = 0 |
| 138 | for g in go: |
| 139 | t, i = text(g[0]), g[1] - 2 |
| 140 | while i >= 0 and not t[i].strip(): i -= 1 |
| 141 | if i >= 0 and (t[i].rstrip().endswith("-/") or t[i].lstrip().startswith("@[")): loose += 1 |
| 142 | print(f"declarations that go whose docstring or attribute line lies above their range: {loose}") |
| 143 | print(f"held back by the keep rule: {len(held)} declarations ({lines(held)} lines, {100 * lines(held) / lines(tdead):.1f}% of Treant's deleted lines) in {len({g[0] for g in held})} files; {sum(bool(cites.search(doc.get(g, ''))) for g in held)} of them cite the paper themselves") |
| 144 | print(f"what goes: {len(go)} declarations, {lines(go)} lines; a kept declaration that still refers to one of them: {len(bad)} {[name_of[g] for g in bad[:3]]}") |
| 145 | |
| 146 | # waves: in a wave a family deletes every declaration whose users are gone already or go in the same pull request |
| 147 | def fam(m): |
| 148 | parts = m.split("."); d = "/".join(parts[:-1]) |
| 149 | if m.startswith("NavierStokes.R3."): return "NavierStokes/R3" |
| 150 | w = re.match(r"([A-Z][a-z0-9]+)", parts[-1]); return d + "/" + (w.group(1) if w else parts[-1]) + "*" |
| 151 | named = 0 |
| 152 | for g in go: |
| 153 | if f_of(g[0]) in gone: continue |
| 154 | for h in named_in(g): |
| 155 | if h in go and g not in users[h]: users[h].add(g); named += 1 |
| 156 | print(f"order: {named} more pairs where one declaration that goes is only named in the text of another") |
| 157 | whole = {g for g in go if f_of(g[0]) in gone} # whole files: the first step of S-1, its own pull requests |
| 158 | rest, done, waves = go - whole, set(whole), [] |
| 159 | while rest: |
| 160 | wave = {} |
| 161 | byfam = collections.defaultdict(set) |
| 162 | for g in rest: byfam[fam(g[0])].add(g) |
| 163 | for F, cand in byfam.items(): |
| 164 | s = set(cand); changed = True |
| 165 | while changed: |
| 166 | changed = False |
| 167 | for g in list(s): |
| 168 | if any(u in go and u not in done and u not in s for u in users[g]): s.discard(g); changed = True |
| 169 | if s: wave[F] = s |
| 170 | if not wave: break |
| 171 | waves.append(wave) |
| 172 | for s in wave.values(): done |= s; rest -= s |
| 173 | print(f"whole files: {len({g[0] for g in whole})} files in {len({fam(g[0]) for g in whole})} families; then {len(waves)} waves, pull requests a wave: {[len(w) for w in waves]} = {sum(len(w) for w in waves)}; left over (a cycle across families): {len(rest)}") |
| 174 | cum = 0 |
| 175 | for i, w in enumerate(waves, 1): |
| 176 | n = sum(len(s) for s in w.values()); cum += n |
| 177 | if i <= 12: print(f" wave {i}: {len(w)} families, {n} declarations, {lines({g for s in w.values() for g in s})} lines, {100 * cum / max(1, len(go - whole)):.0f}% done") |
| 178 | def top(g): |
| 179 | """The first line to cut for g: its range, and the docstring or attribute lines that sit right above it.""" |
| 180 | t, a = text(g[0]), g[1] |
| 181 | while a >= 2: |
| 182 | prev = t[a - 2].strip() |
| 183 | if prev.startswith("@[") and prev.endswith("]"): a -= 1 |
| 184 | elif prev.endswith("-/"): |
| 185 | i = a - 2 |
| 186 | while i >= 0 and "/-" not in t[i]: i -= 1 |
| 187 | if i < 0 or not t[i].lstrip().startswith("/--"): break |
| 188 | a = i + 1 |
| 189 | elif re.search(r"\bin\s*$", prev): # `omit [...] in`, `include h in`, `open X in`: a command's prefix goes with it |
| 190 | i = a - 2 |
| 191 | while i >= 0 and a - 2 - i < 8 and not PREFIX.match(t[i]): i -= 1 |
| 192 | if i < 0 or not PREFIX.match(t[i]): break |
| 193 | a = i + 1 |
| 194 | else: break |
| 195 | return a |
| 196 | PREFIX = re.compile(r"(omit|include|open|set_option|variable|attribute|local|universe|unseal|seal)\b") |
| 197 | os.makedirs(out, exist_ok=True) |
| 198 | plan = {"whole_files": sorted(f for f in gone if f in site_blobs), "held": sorted([name_of[g], g[0]] for g in held), |
| 199 | "waves": [{F: sorted([g[0], top(g), g[2], name_of[g]] for g in s) for F, s in sorted(w.items())} for w in waves], |
| 200 | "left": sorted([name_of[g], g[0]] for g in rest)} |
| 201 | json.dump(plan, open(f"{out}/plan.json", "w")) |
| 202 | print("written", f"{out}/plan.json") |