goldbach-spectral-bridge
Li and Liu's GRH + S ⇒ Goldbach in Lean, checked against the paper by Comparator: clean it up together, and settle S
P-4: a GUIDE.md link and statement checker #53
Merged
+57 −0 routes/p4-guide-checker/README.md added View file
| @@ -0,0 +1,57 @@ | ||
| 1 | +--- | |
| 2 | +issue: 48 | |
| 3 | +status: done | |
| 4 | +--- | |
| 5 | +# p4-guide-checker | |
| 6 | + | |
| 7 | +P-4 (#48): a Python checker for `GUIDE.md`, with no Lean and no build. | |
| 8 | + | |
| 9 | +## Run | |
| 10 | + | |
| 11 | +From the repository root: | |
| 12 | + | |
| 13 | +```bash | |
| 14 | +python3 routes/p4-guide-checker/check_guide.py | |
| 15 | +``` | |
| 16 | + | |
| 17 | +It prints one `OK path#Lnn name` line for every local link, then a summary. Use | |
| 18 | +`--summary` for only the summary. It exits `0` when every check passes and `1` with the | |
| 19 | +list of unresolved links otherwise. | |
| 20 | + | |
| 21 | +## What it checks | |
| 22 | + | |
| 23 | +- Every Markdown link of the form `[`name`](path#Lnn)` in `GUIDE.md`: file `path` exists, | |
| 24 | + line `nn` is inside it, and that line begins the declaration `name` (an `@[...]` | |
| 25 | + attribute line immediately above is allowed). | |
| 26 | +- Every `statement_NNN_*` cited in `GUIDE.md` is declared in | |
| 27 | + `verification/independent-audit/comparator-project/TexAuditComparator/Challenge.lean`. | |
| 28 | +- Every three-cell table row that has links: the statement name in the third cell ends in the | |
| 29 | + Lean declaration name in the second cell. | |
| 30 | + | |
| 31 | +`--root` can point at a copy of the repository; the default is the root two directories | |
| 32 | +above the script. | |
| 33 | + | |
| 34 | +## On `main` at `6ec7289` | |
| 35 | + | |
| 36 | +```text | |
| 37 | +SUMMARY links=293 resolved=293 statements=103 declared=103 table_rows=103 failures=0 | |
| 38 | +OK GUIDE.md checks passed | |
| 39 | +``` | |
| 40 | + | |
| 41 | +The full per-link run printed 501 lines: 293 resolved-link lines, 103 statement-declaration | |
| 42 | +lines, 103 table-row lines, and the two summary lines. Exit status `0`. | |
| 43 | + | |
| 44 | +A negative fixture (one link pointed at a comment line instead of the declaration) printed | |
| 45 | + | |
| 46 | +```text | |
| 47 | +FAIL Dummy.lean#L1: expected declaration 'foo', got None (line 1 does not begin a declaration: '-- line one is not a declaration') | |
| 48 | +SUMMARY links=2 resolved=1 statements=1 declared=1 table_rows=1 failures=1 | |
| 49 | +FAIL 1 problem(s) | |
| 50 | +``` | |
| 51 | + | |
| 52 | +and exited `1`, so the failure path is live, not just the happy path. | |
| 53 | + | |
| 54 | +## What it does not show | |
| 55 | + | |
| 56 | +That `GUIDE.md`'s pages, clauses or row labels match the paper: that is P-3 (#47) and V-5 | |
| 57 | +(#11). | |
+196 −0 routes/p4-guide-checker/check_guide.py added View file
| @@ -0,0 +1,196 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Check that GUIDE.md's local `#L` links point at the declarations they name. | |
| 3 | + | |
| 4 | +The checker is deliberately small and has no Lean dependency. It reads | |
| 5 | +``GUIDE.md`` at the repository root, resolves every Markdown link of the form | |
| 6 | +``[`name`](path#Lnn)`` against ``path`` line ``nn``, and checks three things: | |
| 7 | + | |
| 8 | +* the line begins the declaration ``name`` (an attribute line immediately above | |
| 9 | + is allowed); | |
| 10 | +* every ``statement_NNN_*`` cited anywhere in the guide is declared in | |
| 11 | + ``Challenge.lean``; | |
| 12 | +* in every three-cell table row, the statement name in the third cell ends in | |
| 13 | + the Lean declaration name in the second cell. | |
| 14 | + | |
| 15 | +Run from the repository root: | |
| 16 | + | |
| 17 | + python3 routes/p4-guide-checker/check_guide.py | |
| 18 | + | |
| 19 | +Exit status is 0 only if all checks pass. | |
| 20 | +""" | |
| 21 | + | |
| 22 | +from __future__ import annotations | |
| 23 | + | |
| 24 | +import argparse | |
| 25 | +import re | |
| 26 | +import sys | |
| 27 | +import urllib.parse | |
| 28 | +from pathlib import Path | |
| 29 | + | |
| 30 | +LINK_RE = re.compile(r"\[`([^`]+)`\]\(([^)#]+)#L(\d+)(?:-L\d+)?\)") | |
| 31 | +STATEMENT_RE = re.compile(r"statement_\d{3}_[A-Za-z0-9_']+") | |
| 32 | +DECL_RE = re.compile( | |
| 33 | + r"^(?:@\[[^\]]*\]\s*)?" | |
| 34 | + r"(?:(?:private|protected|noncomputable|partial|unsafe|scoped|local|public|irreducible)\s+)*" | |
| 35 | + r"(theorem|lemma|def|abbrev|structure|class|inductive|instance|opaque|axiom)\s+" | |
| 36 | + r"([^\s:{([]+)" | |
| 37 | +) | |
| 38 | +CHALLENGE_PATH = Path( | |
| 39 | + "verification/independent-audit/comparator-project/TexAuditComparator/Challenge.lean" | |
| 40 | +) | |
| 41 | + | |
| 42 | + | |
| 43 | +def declaration_name(lines: list[str], line_no: int) -> tuple[str | None, str]: | |
| 44 | + """Return (declaration name, detail) for a 1-based line number. | |
| 45 | + | |
| 46 | + If the cited line is an attribute, skip consecutive attribute lines and | |
| 47 | + read the declaration they annotate. ``detail`` is the text used in the | |
| 48 | + report when no declaration is found. | |
| 49 | + """ | |
| 50 | + if not 1 <= line_no <= len(lines): | |
| 51 | + return None, f"line {line_no} is outside the file ({len(lines)} lines)" | |
| 52 | + | |
| 53 | + index = line_no - 1 | |
| 54 | + line = lines[index].strip() | |
| 55 | + while line.startswith("@[") and index + 1 < len(lines): | |
| 56 | + index += 1 | |
| 57 | + line = lines[index].strip() | |
| 58 | + | |
| 59 | + match = DECL_RE.match(line) | |
| 60 | + if not match: | |
| 61 | + return None, f"line {line_no} does not begin a declaration: {line[:100]!r}" | |
| 62 | + return match.group(2), f"line {line_no} begins {line[:100]!r}" | |
| 63 | + | |
| 64 | + | |
| 65 | +def fail(message: str, failures: list[str]) -> None: | |
| 66 | + failures.append(message) | |
| 67 | + print(f"FAIL {message}") | |
| 68 | + | |
| 69 | + | |
| 70 | +def main() -> int: | |
| 71 | + parser = argparse.ArgumentParser(description=__doc__) | |
| 72 | + parser.add_argument( | |
| 73 | + "--root", | |
| 74 | + type=Path, | |
| 75 | + default=Path(__file__).resolve().parents[2], | |
| 76 | + help="repository root (default: two directories above this script)", | |
| 77 | + ) | |
| 78 | + parser.add_argument( | |
| 79 | + "--summary", | |
| 80 | + action="store_true", | |
| 81 | + help="print only the summary, not one line per link", | |
| 82 | + ) | |
| 83 | + args = parser.parse_args() | |
| 84 | + | |
| 85 | + root = args.root.resolve() | |
| 86 | + guide_path = root / "GUIDE.md" | |
| 87 | + challenge_path = root / CHALLENGE_PATH | |
| 88 | + failures: list[str] = [] | |
| 89 | + | |
| 90 | + if not guide_path.is_file(): | |
| 91 | + print(f"FAIL missing {guide_path}", file=sys.stderr) | |
| 92 | + return 2 | |
| 93 | + if not challenge_path.is_file(): | |
| 94 | + print(f"FAIL missing {challenge_path}", file=sys.stderr) | |
| 95 | + return 2 | |
| 96 | + | |
| 97 | + guide = guide_path.read_text(encoding="utf-8") | |
| 98 | + guide_lines = guide.splitlines() | |
| 99 | + challenge_lines = challenge_path.read_text(encoding="utf-8").splitlines() | |
| 100 | + | |
| 101 | + links = LINK_RE.findall(guide) | |
| 102 | + if not links: | |
| 103 | + print("FAIL GUIDE.md contains no local #L links", file=sys.stderr) | |
| 104 | + return 2 | |
| 105 | + | |
| 106 | + resolved = 0 | |
| 107 | + for name, path_text, line_text in links: | |
| 108 | + line_no = int(line_text) | |
| 109 | + path_text = urllib.parse.unquote(path_text) | |
| 110 | + target = (root / path_text).resolve() | |
| 111 | + try: | |
| 112 | + target.relative_to(root) | |
| 113 | + except ValueError: | |
| 114 | + fail(f"{path_text}#L{line_no}: path escapes the repository", failures) | |
| 115 | + continue | |
| 116 | + | |
| 117 | + if not target.is_file(): | |
| 118 | + fail(f"{path_text}#L{line_no}: file does not exist", failures) | |
| 119 | + continue | |
| 120 | + | |
| 121 | + lines = target.read_text(encoding="utf-8").splitlines() | |
| 122 | + got, detail = declaration_name(lines, line_no) | |
| 123 | + if got != name: | |
| 124 | + fail( | |
| 125 | + f"{path_text}#L{line_no}: expected declaration {name!r}, got {got!r} ({detail})", | |
| 126 | + failures, | |
| 127 | + ) | |
| 128 | + continue | |
| 129 | + | |
| 130 | + resolved += 1 | |
| 131 | + if not args.summary: | |
| 132 | + print(f"OK {path_text}#L{line_no} {name}") | |
| 133 | + | |
| 134 | + # Every statement_NNN_* the guide cites must be declared in Challenge.lean. | |
| 135 | + cited_statements = set(STATEMENT_RE.findall(guide)) | |
| 136 | + declared_statements = { | |
| 137 | + name | |
| 138 | + for line in challenge_lines | |
| 139 | + for name, _detail in [declaration_name([line], 1)] | |
| 140 | + if name and name.startswith("statement_") | |
| 141 | + } | |
| 142 | + missing_statements = sorted(cited_statements - declared_statements) | |
| 143 | + for name in missing_statements: | |
| 144 | + fail(f"{name}: cited in GUIDE.md but not declared in {CHALLENGE_PATH}", failures) | |
| 145 | + if not args.summary: | |
| 146 | + for name in sorted(cited_statements): | |
| 147 | + print(f"OK {CHALLENGE_PATH} declares {name}") | |
| 148 | + | |
| 149 | + # Every three-cell table row with links: statement name ends in the | |
| 150 | + # declaration name in the second cell. | |
| 151 | + table_rows = 0 | |
| 152 | + for line_no, line in enumerate(guide_lines, 1): | |
| 153 | + if not line.startswith("|") or not LINK_RE.search(line): | |
| 154 | + continue | |
| 155 | + cells = [cell.strip() for cell in line.strip().strip("|").split("|")] | |
| 156 | + if len(cells) != 3: | |
| 157 | + fail(f"GUIDE.md:{line_no}: table row has {len(cells)} cells, expected 3", failures) | |
| 158 | + continue | |
| 159 | + declaration_links = LINK_RE.findall(cells[1]) | |
| 160 | + statement_links = LINK_RE.findall(cells[2]) | |
| 161 | + if len(declaration_links) != 1 or len(statement_links) != 1: | |
| 162 | + fail( | |
| 163 | + f"GUIDE.md:{line_no}: expected one link in each of the last two cells, " | |
| 164 | + f"got {len(declaration_links)} and {len(statement_links)}", | |
| 165 | + failures, | |
| 166 | + ) | |
| 167 | + continue | |
| 168 | + | |
| 169 | + declaration = declaration_links[0][0] | |
| 170 | + statement = statement_links[0][0] | |
| 171 | + table_rows += 1 | |
| 172 | + if not statement.startswith("statement_") or not statement.endswith(declaration): | |
| 173 | + fail( | |
| 174 | + f"GUIDE.md:{line_no}: statement {statement!r} does not end in " | |
| 175 | + f"declaration {declaration!r}", | |
| 176 | + failures, | |
| 177 | + ) | |
| 178 | + elif not args.summary: | |
| 179 | + print(f"OK GUIDE.md:{line_no} {statement} ends in {declaration}") | |
| 180 | + | |
| 181 | + print( | |
| 182 | + f"SUMMARY links={len(links)} resolved={resolved} " | |
| 183 | + f"statements={len(cited_statements)} declared={len(cited_statements) - len(missing_statements)} " | |
| 184 | + f"table_rows={table_rows} failures={len(failures)}" | |
| 185 | + ) | |
| 186 | + if failures: | |
| 187 | + print(f"FAIL {len(failures)} problem(s):", file=sys.stderr) | |
| 188 | + for message in failures: | |
| 189 | + print(f" {message}", file=sys.stderr) | |
| 190 | + return 1 | |
| 191 | + print("OK GUIDE.md checks passed") | |
| 192 | + return 0 | |
| 193 | + | |
| 194 | + | |
| 195 | +if __name__ == "__main__": | |
| 196 | + raise SystemExit(main()) | |
