#!/usr/bin/env python3 """Compile root sources, the 010a--010u chain, and 011 composition sources. Requires Lean 4.33.0 on PATH. This script deliberately fails rather than claiming a compiler receipt when Lean is unavailable. """ from pathlib import Path import os import re import shutil import subprocess import sys BASE = Path("proofs") TARGET = "SutureSignature011b" CORE_TARGETS = ("GaugeMatterTrunk011f", "QuantumStateBoundary011g", "ObservableExposure011h", "GlobalCoverage011i") SMOOTH_TARGETS = ("SmoothSuture011c", "SutureCovariance011c", "SutureRegular011c", "SutureOpenBranch011c", "SutureActualMinor011c", "SutureLDU011c", "SutureLDUCertificate011c", "SutureLDUWitness011c", "SutureLDUEntries011c", "SutureClassical011d", "SutureS02Unconditional011e", "RootToPhysicsManifest011j") LEGACY_COMPILER = Path("scripts/compile_lean_010u.py") DEPENDENCY = re.compile(r"^\s*import\s+proofs\.([A-Za-z0-9_]+)\s*$", re.M) def closure(name: str, visiting=None, completed=None, ordered=None): visiting = set() if visiting is None else visiting completed = set() if completed is None else completed ordered = [] if ordered is None else ordered if name in completed: return ordered if name in visiting: raise ValueError(f"cyclic local import: {name}") path = BASE / f"{name}.lean" if not path.is_file(): raise ValueError(f"missing import: {path}") visiting.add(name) for dependency in DEPENDENCY.findall(path.read_text(encoding="utf-8")): closure(dependency, visiting, completed, ordered) visiting.remove(name) completed.add(name) ordered.append(name) return ordered def main() -> int: ordered = closure(TARGET) for core_target in CORE_TARGETS: for name in closure(core_target): if name not in ordered: ordered.append(name) print("011 composition import closure (" + str(len(ordered)) + "): " + ", ".join(ordered), flush=True) if "--dry-run" in sys.argv: legacy = subprocess.run([ sys.executable, str(LEGACY_COMPILER), "proofs.UniversalClosure010u", "--dry-run", ]) if legacy.returncode: return legacy.returncode return 0 if shutil.which("lean") is None: print("LEAN UNAVAILABLE: no compilation receipt claimed", file=sys.stderr) return 127 if shutil.which("lake") is None: print("LAKE UNAVAILABLE: no smooth-witness receipt claimed", file=sys.stderr) return 127 Path("build/proofs").mkdir(parents=True, exist_ok=True) # The 011g import closure includes YMGapRefusal010r, whose plain-name # import of YMDiracGapAudit010q is built by the legacy compiler. Run the # complete pinned 010 replay first so this remains valid on a cold tree. legacy = subprocess.run([sys.executable, str(LEGACY_COMPILER)]) if legacy.returncode: return legacy.returncode environment = os.environ.copy() environment["LEAN_PATH"] = "build:proofs" log = Path("build/011-lean.log") with log.open("w", encoding="utf-8") as output: for name in ordered: command = ["lean", "--root=.", "-o", f"build/proofs/{name}.olean", f"proofs/{name}.lean"] result = subprocess.run(command, text=True, capture_output=True, env=environment) body = result.stdout + result.stderr output.write(body) print("Lean", name, "PASS" if result.returncode == 0 else "FAIL", flush=True) if result.returncode: print(body, file=sys.stderr) return result.returncode environment["LEAN_PATH"] = "build:proofs" + ( ":" + environment["LEAN_PATH"] if environment.get("LEAN_PATH") else "" ) for smooth_target in SMOOTH_TARGETS: smooth = subprocess.run( ["lake", "env", "lean", "--root=.", "-o", f"build/proofs/{smooth_target}.olean", f"proofs/{smooth_target}.lean"], text=True, capture_output=True, env=environment, ) smooth_body = smooth.stdout + smooth.stderr with log.open("a", encoding="utf-8") as output: output.write(smooth_body) print("Lean", smooth_target, "PASS" if smooth.returncode == 0 else "FAIL", flush=True) if smooth.returncode: print(smooth_body, file=sys.stderr) return smooth.returncode print("010a--010u + 011a--011j compilation PASSED. Log:", log, flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())