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 | import ConditionalGoldbach |
| 2 | import Lean |
| 3 | |
| 4 | open Lean Elab Command |
| 5 | |
| 6 | set_option maxRecDepth 1000000 |
| 7 | set_option maxHeartbeats 0 |
| 8 | |
| 9 | private def origin (env : Environment) (modules : Array Name) (n : Name) : Name := |
| 10 | match env.getModuleIdxFor? n with |
| 11 | | some i => modules[i]! |
| 12 | | none => env.mainModule |
| 13 | |
| 14 | private def kind (i : ConstantInfo) : String := |
| 15 | match i with |
| 16 | | .axiomInfo _ => "axiom" |
| 17 | | .thmInfo _ => "theorem" |
| 18 | | .defnInfo _ => "definition" |
| 19 | | .opaqueInfo _ => "opaque" |
| 20 | | .ctorInfo _ => "constructor" |
| 21 | | .recInfo _ => "recursor" |
| 22 | | .inductInfo _ => "inductive" |
| 23 | | .quotInfo _ => "quotient" |
| 24 | |
| 25 | /-- Follow the entry module's compiled imports, excluding imports used only by this audit. -/ |
| 26 | private def importClosure (env : Environment) (root : Name) : Except String (Array Name) := do |
| 27 | let mut todo := #[root] |
| 28 | let mut seen : NameSet := {} |
| 29 | while !todo.isEmpty do |
| 30 | let name := todo.back! |
| 31 | todo := todo.pop |
| 32 | if seen.contains name then continue |
| 33 | let some idx := env.getModuleIdx? name | throw s!"Missing imported module {name}" |
| 34 | seen := seen.insert name |
| 35 | todo := todo ++ env.header.moduleData[idx]!.imports.map (·.module) |
| 36 | return env.header.moduleNames.filter seen.contains |
| 37 | |
| 38 | run_cmd do |
| 39 | let env := (← getEnv).setExporting false |
| 40 | let modules := env.header.moduleNames |
| 41 | let productionModules ← ofExcept <| importClosure env `ConditionalGoldbach |
| 42 | let localNames := env.constants.toList.toArray.filterMap fun (n, _) => |
| 43 | if "ConditionalGoldbach".isPrefixOf (origin env modules n).toString then some n else none |
| 44 | let mut todo := localNames |
| 45 | let mut seen : NameSet := {} |
| 46 | let mut rows : Array Json := #[] |
| 47 | let mut missing : Array String := #[] |
| 48 | while !todo.isEmpty do |
| 49 | let n := todo.back! |
| 50 | todo := todo.pop |
| 51 | if seen.contains n then continue |
| 52 | seen := seen.insert n |
| 53 | let some info := env.find? n | missing := missing.push n.toString; continue |
| 54 | let typeRefs := info.type.getUsedConstants |
| 55 | let valueRefs := (info.value? (allowOpaque := true)).map Expr.getUsedConstants |>.getD #[] |
| 56 | let ctorRefs := match info with |
| 57 | | .inductInfo v => v.ctors.toArray |
| 58 | | _ => #[] |
| 59 | todo := todo ++ typeRefs ++ valueRefs ++ ctorRefs |
| 60 | rows := rows.push <| Json.mkObj [ |
| 61 | ("name", toJson n.toString), |
| 62 | ("module", toJson (origin env modules n).toString), |
| 63 | ("kind", toJson (kind info)), |
| 64 | ("private", toJson (isPrivateName n)), |
| 65 | ("type_refs", toJson (typeRefs.map Name.toString)), |
| 66 | ("value_refs", toJson (valueRefs.map Name.toString)), |
| 67 | ("constructor_refs", toJson (ctorRefs.map Name.toString))] |
| 68 | let out := Json.mkObj [ |
| 69 | ("method", toJson "Transitive constant dependencies through declaration types and opaque-inclusive values; inductive constructors are included as in Lean.collectAxioms. Origins use Environment.getModuleIdxFor?."), |
| 70 | ("imported_modules", toJson (productionModules.map Name.toString)), |
| 71 | ("local_roots", toJson (localNames.map Name.toString)), |
| 72 | ("missing", toJson missing), |
| 73 | ("declarations", Json.arr rows)] |
| 74 | liftIO <| IO.FS.writeFile "verification/publication/declaration-audit/declaration-graph.json" out.compress |
| 75 | liftIO <| IO.FS.writeFile "verification/publication/declaration-audit/imported-modules.json" |
| 76 | (toJson (productionModules.map Name.toString)).pretty |
| 77 | logInfo m!"Audited {localNames.size} local declarations and {rows.size} reachable declarations; {missing.size} missing." |
