#!/usr/bin/env python3 # Copyright (c) 2026 Anthropic, PBC. All rights reserved. # Released under Apache 2.0 license as described in the file LICENSE. # SPDX-License-Identifier: Apache-2.0 """Write the files that are derived from the files Challenge/T.lean. scripts/generate.py [--check] For each topic T it writes comparator-t.json (t is T in lower case), which tells Comparator to compare the theorems of Challenge/T.lean with the theorems of the same names that Solution/T.lean imports. It also writes scripts/PrintAxioms.lean, which prints the axioms behind each of these theorems. --check writes nothing and fails if a file on disk differs from what would be written. A statement that is missing from a list would silently not be compared. To guard against such accidents the script accepts only Challenge files of a simple form, described at the function theorem_names. It also fails if a trusted file imports more than this: EndStatement.lean nothing, PaperStatements.lean Mathlib and EndStatement, Challenge/T.lean the trusted file T; see the function trusted_code, which says what form of text it accepts. The script reads Lean text without Lean's parser. So it guards against accidents, and does not prove that a file has the form described. It is not a defence against a trusted file written to deceive: these files are meant to be read. It does not look at lakefile.toml, lake-manifest.json and lean-toolchain, which have to be trusted as well. """ import json import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent AXIOMS = ["propext", "Quot.sound", "Classical.choice"] HEADER = "".join(Path(__file__).read_text(encoding="utf-8").splitlines(keepends=True)[1:4]).replace("# ", "") IMPORT = re.compile(r"(?:public )?import (\w+(?:\.\w+)*)") PLAIN_LINE = re.compile(rf"module|{IMPORT.pattern}|public section|open( [A-Z][\w.]*)+( in)?|(namespace|section|end)( \w+)?") THEOREM = re.compile(r"theorem ([\w']+)[\s:({\[⦃].*:=\s*(by\s+)?sorry", re.S) DECLARES = re.compile(r"(? 1] for rel, text in files.items(): if not check: (ROOT / rel).write_text(text, encoding="utf-8") elif not (ROOT / rel).exists() or (ROOT / rel).read_text(encoding="utf-8") != text: problems.append(f"{rel} is not what scripts/generate.py would write; run scripts/generate.py") for p in problems: print("error: " + p, file=sys.stderr) print(f"{len(topics)} topics, {len(everything)} statements") return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())