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
| 1 | """Analyze the kernel-constant graph exported by DeclarationAudit.lean.""" |
| 2 | import collections |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | |
| 6 | HERE = Path(__file__).resolve().parent |
| 7 | EXTERNAL = (HERE.parents[2] / '.lake/packages/goldbach').resolve() |
| 8 | graph = json.loads((HERE / 'declaration-graph.json').read_text()) |
| 9 | nodes = {r['name']: r for r in graph['declarations']} |
| 10 | project_modules = { |
| 11 | r['module'] for r in nodes.values() |
| 12 | if (EXTERNAL / (r['module'].replace('.', '/') + '.lean')).is_file() |
| 13 | } |
| 14 | |
| 15 | |
| 16 | def refs(n): |
| 17 | r = nodes[n] |
| 18 | return r['type_refs'] + r['value_refs'] + r['constructor_refs'] |
| 19 | |
| 20 | |
| 21 | def closure(roots): |
| 22 | parents = {n: None for n in roots} |
| 23 | todo = collections.deque(roots) |
| 24 | while todo: |
| 25 | n = todo.popleft() |
| 26 | for target in refs(n): |
| 27 | if target not in parents: |
| 28 | assert target in nodes, target |
| 29 | parents[target] = n |
| 30 | todo.append(target) |
| 31 | return parents |
| 32 | |
| 33 | |
| 34 | def path_to(n, parents): |
| 35 | path = [n] |
| 36 | while parents[n] is not None: |
| 37 | n = parents[n] |
| 38 | path.append(n) |
| 39 | return path[::-1] |
| 40 | |
| 41 | |
| 42 | scope_roots = { |
| 43 | **{n: ['ConditionalGoldbach.' + n] for n in |
| 44 | ['theorem_1_1', 'theorem_1_2', 'primePair_consequence']}, |
| 45 | 'reviewed_102': json.loads((HERE / 'reviewed-names.json').read_text()), |
| 46 | 'public_library': [n for n in graph['local_roots'] if not nodes[n]['private']], |
| 47 | 'all_local_declarations': graph['local_roots'], |
| 48 | } |
| 49 | summary = {} |
| 50 | for scope, roots in scope_roots.items(): |
| 51 | parents = closure(roots) |
| 52 | external = sorted(n for n in parents if nodes[n]['module'] in project_modules) |
| 53 | by_module = collections.defaultdict(list) |
| 54 | for n in external: |
| 55 | by_module[nodes[n]['module']].append(n) |
| 56 | boundaries = [] |
| 57 | for source in sorted(parents): |
| 58 | if not nodes[source]['module'].startswith('ConditionalGoldbach'): |
| 59 | continue |
| 60 | for edge_kind in ['type_refs', 'value_refs', 'constructor_refs']: |
| 61 | for target in nodes[source][edge_kind]: |
| 62 | if nodes[target]['module'] in project_modules: |
| 63 | boundaries.append({ |
| 64 | 'source': source, |
| 65 | 'source_module': nodes[source]['module'], |
| 66 | 'edge_kind': edge_kind, |
| 67 | 'target': target, |
| 68 | 'target_module': nodes[target]['module'], |
| 69 | }) |
| 70 | result = { |
| 71 | 'root_count': len(roots), |
| 72 | 'reachable_declaration_count': len(parents), |
| 73 | 'external_project_declaration_count': len(external), |
| 74 | 'external_project_module_count': len(by_module), |
| 75 | 'external_project_modules': dict(sorted(by_module.items())), |
| 76 | 'external_direct_boundary_declaration_count': len({e['target'] for e in boundaries}), |
| 77 | 'external_direct_boundary_edges': boundaries, |
| 78 | 'axioms': sorted(n for n in parents if nodes[n]['kind'] == 'axiom'), |
| 79 | 'proof_witness_paths': {n: path_to(n, parents) for n in external}, |
| 80 | } |
| 81 | (HERE / f'{scope}.json').write_text(json.dumps(result, indent=2, ensure_ascii=False) + '\n') |
| 82 | summary[scope] = {k: v for k, v in result.items() if k not in |
| 83 | ['proof_witness_paths', 'external_project_modules', 'external_direct_boundary_edges']} |
| 84 | summary[scope]['external_project_module_counts'] = {m: len(ns) for m, ns in sorted(by_module.items())} |
| 85 | with (HERE / f'{scope}-external-declarations.tsv').open('w') as f: |
| 86 | f.write('module\tname\tkind\tprivate\n') |
| 87 | for n in external: |
| 88 | r = nodes[n] |
| 89 | f.write(f"{r['module']}\t{n}\t{r['kind']}\t{r['private']}\n") |
| 90 | |
| 91 | (HERE / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False) + '\n') |
| 92 | print(json.dumps(summary, indent=2, ensure_ascii=False)) |
