把黎曼猜想的 Lean 证明,写成人能读懂的证明
把黎曼猜想的 Lean 证明写成数学家能读的证明文章,以及人人能看懂的证明思路
| 1 | """Offline evidence checks for the Phase 0 map and later blueprint chapters.""" |
| 2 | import hashlib |
| 3 | import os |
| 4 | import re |
| 5 | import sys |
| 6 | from pathlib import Path |
| 7 | from atlas import atlas, chapters, need, obj, read, triage, NAME |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parent |
| 10 | |
| 11 | |
| 12 | def tex_commands(text, command): |
| 13 | # Restricted blueprint dialect; comments end at an unescaped percent sign. |
| 14 | text = re.sub(r'(?<!\\)%[^\n]*', '', text) |
| 15 | starts = list(re.finditer(r'\\' + command + r'\b', text)) |
| 16 | matches = list(re.finditer(r'\\' + command + r'\s*\{([^{}]*)\}', text)) |
| 17 | need(len(starts) == len(matches), f"malformed \\{command} command") |
| 18 | values = [m[1].strip() for m in matches] |
| 19 | need(all(NAME.fullmatch(v) for v in values), f"invalid \\{command} argument") |
| 20 | return values |
| 21 | |
| 22 | |
| 23 | def frontmatter(path): |
| 24 | text = read(path, 64_000).decode('utf-8') |
| 25 | match = re.match(r'\A---\r?\n(.*?)\r?\n---(?:\r?\n|$)', text, re.S) |
| 26 | need(match, f"{path.name}: review needs front matter") |
| 27 | fields = {} |
| 28 | for line in match[1].splitlines(): |
| 29 | need(':' in line, f"{path.name}: use simple key: value front matter") |
| 30 | k, v = line.split(':', 1) |
| 31 | need(k.strip() not in fields and bool(v.strip()), "duplicate/empty review field") |
| 32 | fields[k.strip()] = v.strip() |
| 33 | need(fields.get('verdict') in {'approve', 'changes'}, "verdict must be approve|changes") |
| 34 | need(all(fields.get(k) for k in ('reviewer', 'chapter', 'author', 'tex_sha256', 'expert')), "review identity/revision fields missing") |
| 35 | need(fields['expert'] in {'yes', 'no'}, "expert must be yes|no") |
| 36 | need(fields['reviewer'].casefold() != fields['author'].casefold(), "chapter writer cannot review own work") |
| 37 | need(re.fullmatch('[0-9a-f]{64}', fields['tex_sha256']), "invalid review tex_sha256") |
| 38 | return fields |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | need(len(sys.argv) == 2, 'Usage: python verify.py <route-folder>') |
| 43 | route = Path(sys.argv[1]).resolve() |
| 44 | need(route.is_dir(), 'route folder does not exist') |
| 45 | # Read route map if present; otherwise the pushed repository map, never a download. |
| 46 | input_root = Path(os.environ.get('SS_INPUT', str(ROOT))).resolve() |
| 47 | def locate(name): |
| 48 | return route / name if (route / name).exists() else input_root / name |
| 49 | atlas_folder = route if (route / 'atlas-manifest.json').exists() else input_root |
| 50 | rows = atlas(atlas_folder) |
| 51 | example = route == ROOT / 'examples' |
| 52 | if example: |
| 53 | print('EXAMPLE ONLY: fictional declarations and reviews, not mathematical evidence') |
| 54 | messages = [] |
| 55 | cone_names = None |
| 56 | triage_file = locate('triage.json') |
| 57 | if triage_file.exists(): |
| 58 | need(rows is not None, 'triage needs a local hash-checked atlas') |
| 59 | cone_names, status = triage(rows, obj(triage_file), example) |
| 60 | print(status) |
| 61 | messages.append(status) |
| 62 | map_file = locate('chapters.json') |
| 63 | chapter_map = None |
| 64 | if map_file.exists(): |
| 65 | need(rows is not None and cone_names is not None, 'chapters need atlas and triage.json') |
| 66 | chapter_map = chapters(rows, obj(map_file), cone_names) |
| 67 | messages.append(f'chapters: {len(chapter_map)} chapters, complete partition, acyclic') |
| 68 | print(messages[-1]) |
| 69 | files = sorted(route.glob('*.tex')) |
| 70 | need(len(files) <= 1000, 'too many TeX files') |
| 71 | anchors, labels, uses = {}, set(), set() |
| 72 | for path in files: |
| 73 | text = read(path, 1_000_000).decode('utf-8') |
| 74 | anchors[path.stem] = set(tex_commands(text, 'lean')) |
| 75 | local_labels = tex_commands(text, 'label') |
| 76 | need(len(local_labels) == len(set(local_labels)) and not labels.intersection(local_labels), 'duplicate TeX label') |
| 77 | labels.update(local_labels) |
| 78 | uses.update(tex_commands(text, 'uses')) |
| 79 | need(not uses - labels, 'broken uses labels: ' + ', '.join(sorted(uses - labels))) |
| 80 | if rows is not None: |
| 81 | for chapter, names in anchors.items(): |
| 82 | need(not names - rows.keys(), f'chapter {chapter}: unknown Lean anchors: ' + ', '.join(sorted(names - rows.keys()))) |
| 83 | reviews = list((route / 'reviews').rglob('*.md')) if (route / 'reviews').exists() else [] |
| 84 | need(len(reviews) <= 2000, 'too many reviews') |
| 85 | approved, experts, seen = {}, {}, set() |
| 86 | for path in reviews: |
| 87 | review = frontmatter(path) |
| 88 | chapter, reviewer = review['chapter'], review['reviewer'].casefold() |
| 89 | need(chapter in anchors and chapter_map is not None and chapter in chapter_map, 'review refers to unknown chapter/writer') |
| 90 | need(review['author'].casefold() == chapter_map[chapter]['writer'].casefold(), 'review author differs from chapter writer') |
| 91 | need((chapter, reviewer) not in seen, 'one current review per reviewer/chapter; replace older verdicts') |
| 92 | seen.add((chapter, reviewer)) |
| 93 | # Git may translate LF to CRLF on Windows. Bind reviews to canonical LF bytes. |
| 94 | actual = hashlib.sha256(read(route / f'{chapter}.tex', 1_000_000).replace(b'\r\n', b'\n')).hexdigest() |
| 95 | need(review['tex_sha256'] == actual, 'stale review: TeX hash differs') |
| 96 | if review['verdict'] == 'approve': |
| 97 | approved.setdefault(chapter, set()).add(reviewer) |
| 98 | if review['expert'] == 'yes': |
| 99 | experts.setdefault(chapter, set()).add(reviewer) |
| 100 | for chapter, names in anchors.items(): |
| 101 | if chapter_map is None or rows is None: |
| 102 | print(f'chapter {chapter}: not yet expert-reviewed; coverage unchecked') |
| 103 | continue |
| 104 | need(chapter in chapter_map, f'unknown chapter id {chapter}') |
| 105 | declarations = set(chapter_map[chapter]['declarations']) |
| 106 | missing_interface = set(chapter_map[chapter]['interface']) - names |
| 107 | need(not missing_interface, f'chapter {chapter}: interface anchors missing') |
| 108 | covered = len(names & declarations) |
| 109 | need(covered * 100 >= 95 * len(declarations), f'chapter {chapter}: coverage < 95%') |
| 110 | print(f'chapter {chapter}: coverage {covered}/{len(declarations)}; {len(approved.get(chapter, set()))} approving reviews') |
| 111 | if not experts.get(chapter): |
| 112 | print(f'chapter {chapter}: not yet expert-reviewed') |
| 113 | else: |
| 114 | print(f'chapter {chapter}: expert approval declared; maintainer confirms identity') |
| 115 | print(f'chapter {chapter}: not yet expert-reviewed until identity is confirmed') |
| 116 | messages.append(f'chapter {chapter}: 0 broken anchors, coverage >= 95%') |
| 117 | if rows is None and files: |
| 118 | print('atlas not yet published: anchors unchecked') |
| 119 | print('blueprint: syntax only; coverage unchecked') |
| 120 | elif messages: |
| 121 | print(messages[-1]) |
| 122 | elif files: |
| 123 | print('blueprint: anchors checked; coverage unchecked') |
| 124 | elif rows is not None: |
| 125 | print(f'atlas: {len(rows)} declarations, sha256 ok') |
| 126 | else: |
| 127 | print('Nothing to check yet') |
| 128 | |
| 129 | |
| 130 | if __name__ == '__main__': |
| 131 | try: |
| 132 | main() |
| 133 | except (ValueError, OSError, KeyError, TypeError, AttributeError, UnicodeError, EOFError) as error: |
| 134 | print(f'Invalid blueprint: {error}') |
| 135 | sys.exit(1) |