Index of theproofnetwork/open-problems@860a535/scripts/Index.lean
qedmarket’s copy of theproofnetwork/open-problems@860a535, 30 Nov 2026original source ↗
import Leanimport FormalConjecturesUtil.Attributes.Basic /-!# The index's raw facts lake exe index <file with one module name per line> [<file with theorem names>] > raw.json Reads the compiled modules (build them first) and prints, for every theorem tagged`@[category research open]` in them and every theorem named in the second file: its module, declaration range, docstring, AMS subjects, question kind,formal-proof links, and what the official Lean verifier would make of it as a challenge: - `sorry`: something the statement rests on (a definition, or a lemma a definition uses) has `sorry`, so no submission can avoid `sorryAx`;- `axioms`: other axioms it rests on, beyond `propext`, `Quot.sound` and `Classical.choice`;- `private`: it rests on a private declaration of its own module, which a submission can't name;- `classical`: whether it rests on `Classical.choice` (so it can't be a constructive challenge). `scripts/build_index.py` turns this into `index.json`.-/ open Lean ProblemAttributes structure Facts where usesSorry : Bool := false axioms : NameSet := {} deriving Inhabited def allowed : List Name := [``propext, ``Quot.sound, ``Classical.choice] /-- What a constant rests on, through its type and its value (a theorem's proof included), memoized. -/partial def facts (env : Environment) (n : Name) : StateM (Std.HashMap Name Facts) Facts := do if let some f := (← get)[n]? then return f let some ci := env.find? n | return {} let mut f : Facts := {} if n == ``sorryAx then f := { f with usesSorry := true } if ci matches .axiomInfo _ then f := { f with axioms := f.axioms.insert n } modify (·.insert n f) -- no cycles among declarations, but be safe let used := ci.type.getUsedConstants ++ ((ci.value? (allowOpaque := true)).map (·.getUsedConstants)).getD #[] for m in used do let g ← facts env m f := { usesSorry := f.usesSorry || g.usesSorry, axioms := g.axioms.foldl NameSet.insert f.axioms } modify (·.insert n f) return f /-- Private declarations of module `mod` that `n`'s type, or the definitions it uses, rest on. -/partial def privates (env : Environment) (mod : Name) (n : Name) (seen : NameSet) : NameSet × NameSet := if seen.contains n then (seen, {}) else let seen := seen.insert n match env.getModuleIdxFor? n, env.find? n with | some i, some ci => if env.header.moduleNames[i.toNat]! != mod then (seen, {}) else let used := ci.type.getUsedConstants ++ ((ci.value? (allowOpaque := true)).map (·.getUsedConstants)).getD #[] used.foldl (init := (seen, if isPrivateName n then ({} : NameSet).insert n else {})) fun (seen, acc) m => let (seen, p) := privates env mod m seen; (seen, p.foldl NameSet.insert acc) | _, _ => (seen, {}) def statementFacts (env : Environment) (n : Name) : StateM (Std.HashMap Name Facts) Facts := do let some ci := env.find? n | return {} let mut f : Facts := {} for m in ci.type.getUsedConstants do let g ← facts env m f := { usesSorry := f.usesSorry || g.usesSorry, axioms := g.axioms.foldl NameSet.insert f.axioms } return f /-- Module roots of this repository and its support package: their definitions are looked into, everythingelse (Mathlib, Lean, Batteries, …) is reported as used and not descended into. -/def ourRoots : List Name := [`FormalConjectures, `FormalConjecturesUtil, `FormalConjecturesForMathlib, `Prove2Me] def isOurs (env : Environment) (n : Name) : Bool := match env.getModuleIdxFor? n with | some i => ourRoots.contains (env.header.moduleNames[i.toNat]!.getRoot) | none => false -- declared in no imported module: nothing to look into /-- The constants outside this repository that `n` rests on, through the types of everything and the values ofthis repository's own definitions (not the proofs of its lemmas), memoized. For tags: what a statement is about. -/partial def externals (env : Environment) (n : Name) : StateM (Std.HashMap Name NameSet) NameSet := do if let some s := (← get)[n]? then return s modify (·.insert n {}) -- guards against cycles let some ci := env.find? n | return {} let used := ci.type.getUsedConstants ++ (if ci matches .defnInfo _ | .opaqueInfo _ then ((ci.value? (allowOpaque := true)).map (·.getUsedConstants)).getD #[] else #[]) let mut s : NameSet := {} for m in used do if m == n then continue if isOurs env m then s := (← externals env m).foldl NameSet.insert s else s := s.insert m modify (·.insert n s) return s def kindName : FormalProofKind → String | .formalConjecturesProof => "formal_conjectures" | .lean4 => "lean4" | .otherSystem => "other_system" unsafe def main (args : List String) : IO Unit := do let (list, extra) ← match args with | [l] => pure (l, none) | [l, e] => pure (l, some e) | _ => throw <| IO.userError "usage: index <file with module names> [<file with more theorem names>]" let mods := (← IO.FS.lines list).filter (!·.isEmpty) |>.map String.toName -- Theorems without a category tag (Prove2Me's) to index as well. let more ← match extra with | some e => pure <| (← IO.FS.lines e).filter (!·.isEmpty) |>.map String.toName | none => pure #[] initSearchPath (← findSysroot) Lean.searchPathRef.modify (· ++ [System.FilePath.mk ".lake/build/lib/lean"]) enableInitializersExecution let env ← importModules (mods.map ({ module := · })) {} (trustLevel := 1024) (loadExts := true) let ctx : Core.Context := { fileName := "", fileMap := default } let (out, _) ← (do let env ← getEnv let tags ← getTags let subjects ← getSubjectTags let proofs ← getFormalProofTags let wanted := Std.HashSet.ofArray mods let mut cache : Std.HashMap Name Facts := {} let mut extCache : Std.HashMap Name NameSet := {} let mut rows : Array Json := #[] let candidates := (tags.filter (·.category == .research .open)).map (fun t => (t.declName, t.informal)) ++ more.map (·, "") for (n, informal) in candidates do let some i := env.getModuleIdxFor? n | continue let mod := env.header.moduleNames[i.toNat]! unless wanted.contains mod do continue unless (env.find? n) matches some (.thmInfo _) do continue let (f, c) := (statementFacts env n).run cache cache := c let ps := (privates env mod n {}).2 let (ext, c2) := (externals env n).run extCache extCache := c2 let range ← findDeclarationRanges? n let doc ← findDocString? env n let subj := subjects.filter (·.declName == n) |>.toList.flatMap (·.subjects) |>.filterMap (·.toNat?) let fp := proofs.filter (·.declName == n) |>.toList.map fun t => Json.mkObj [("kind", kindName t.proofKind), ("link", t.proofLink)] let question := (← getQuestionKind? n).map toString rows := rows.push <| Json.mkObj [ ("theorem", n.toString (escape := false)), ("module", mod.toString), ("line", toJson (range.map (·.range.pos.line))), ("endLine", toJson (range.map (·.range.endPos.line))), ("docstring", toJson doc), ("informal", informal), ("subjects", toJson subj), ("question", toJson question), ("formalProofs", Json.arr fp.toArray), ("sorry", f.usesSorry), ("axioms", toJson (f.axioms.toList.filter (!allowed.contains ·) |>.map (·.toString))), ("classical", f.axioms.contains ``Classical.choice), ("private", toJson (ps.toList.map (·.toString))), ("uses", toJson ((ext.toList.filter (!·.isInternal)).map (·.toString (escape := false)) |>.toArray.qsort (· < ·)))] let docs := mods.filterMap fun m => (getModuleDoc? env m).map fun ds => (m.toString, toJson ("\n\n".intercalate (ds.toList.map (·.doc)))) return Json.mkObj [("theorems", Json.arr rows), ("moduleDocs", Json.mkObj docs.toList)] : CoreM Json).toIO ctx { env } IO.println out.compress