import Lean open Lean def atlasKind : ConstantInfo → String | .axiomInfo _ => "axiom" | .defnInfo _ => "definition" | .thmInfo _ => "theorem" | .opaqueInfo _ => "opaque" | .quotInfo _ => "quotient" | .inductInfo _ => "inductive" | .ctorInfo _ => "constructor" | .recInfo _ => "recursor" unsafe def main (args : List String) : IO UInt32 := do let [entry, modulesFile, outputFile] := args | throw <| IO.userError "usage: ExtractAtlas.lean ENTRY MODULES.txt OUTPUT.jsonl" initSearchPath (← findSysroot) let modulesText ← IO.FS.readFile modulesFile let modulesText := modulesText.replace "\r" "" let modules : Std.HashSet String := Std.HashSet.ofList (modulesText.splitOn "\n" |>.filter (· != "")) enableInitializersExecution let env ← importModules #[{module := entry.toName}] {} (loadExts := true) let ctx : PPContext := { env := env } let output ← IO.FS.Handle.mk outputFile .write let mut count := 0 let mut missingRanges := 0 for (name, info) in env.constants.toList do let some idx := env.getModuleIdxFor? name | continue let moduleName := env.header.moduleNames[idx]!.toString unless modules.contains moduleName do continue let typeText := (← ctx.runMetaM (Meta.ppExpr info.type)).pretty 120 let ranges ← ctx.runCoreM (findDeclarationRanges? name) let lines := match ranges with | some ranges => ranges.range.endPos.line - ranges.range.pos.line + 1 | none => 0 if ranges.isNone then missingRanges := missingRanges + 1 -- Use Lean's own helper: references in types and values, including opaque -- theorem values, plus its inductive/constructor/recursor metadata policy. let dependencies := info.getUsedConstantsAsSet.toList.map Name.toString |>.mergeSort (· < ·) let row := Json.mkObj [ ("name", toJson name.toString), ("module", toJson moduleName), ("kind", toJson (atlasKind info)), ("type", toJson typeText), ("dependencies", toJson dependencies), ("source_lines", toJson lines)] output.putStrLn row.compress count := count + 1 output.flush IO.eprintln s!"Extracted {count} declarations; {missingRanges} have no recorded declaration range (source_lines = 0)." return 0