Skip to content

Commit

Permalink
eval auto on mathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
PratherConid committed Jan 7, 2025
1 parent 25c8f25 commit 2e43ab8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Auto/EvaluateAuto/OS.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abbrev EvalProc := IO.Process.Child ⟨.piped, .piped, .piped⟩

def EvalProc.create (path : String) (args : Array String) : IO EvalProc :=
IO.Process.spawn {stdin := .piped, stdout := .piped, stderr := .piped, cmd := path, args := args}
86 changes: 86 additions & 0 deletions Auto/EvaluateAuto/TestAuto.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Lean
import Auto.EvaluateAuto.OS
import Auto.EvaluateAuto.Result
import Auto.EvaluateAuto.ConstAnalysis
import Auto.EvaluateAuto.EnvAnalysis
Expand All @@ -18,6 +19,7 @@ inductive SolverConfig where
| leanSmt
| smt (solverName : Solver.SMT.SolverName)
| tptp (solverName : Solver.TPTP.SolverName) (path : String)
deriving Repr

instance : ToString SolverConfig where
toString : SolverConfig → String
Expand Down Expand Up @@ -178,4 +180,88 @@ def runAutoOnNamesFile (cfg : EvalAutoConfig) (fname : String) : CoreM Unit := d
let names ← NameArray.load fname
runAutoOnConsts cfg names

structure EvalAutoOnMathlibConfig where
/-- Timeout for Lean code (Lean-auto + native provers) -/
maxHeartbeats : Nat := 65536
/-- Timeout for external provers, i.e. TPTP solvers and SMT solvers -/
timeout : Nat := 10
/-- Solver configuration -/
solverConfig : SolverConfig
/-- Folder for saving the result of the evaluation -/
resultFolder : String
/-- Number of threads to use -/
nthreads : Nat
/-- Batch size -/
batchSize : Nat

def Array.groupBySize (xs : Array α) (size : Nat) : Option (Array (Array α)) :=
if size == 0 then
.none
else
let n := (xs.size + size - 1) / size
let ret := Array.mk <| (List.range n).map (fun i => Array.mk <| (xs.toList.drop (size * i)).take size)
some ret

def evalAutoAtMathlibHumanTheorems (config : EvalAutoOnMathlibConfig) : CoreM Unit := do
if !(← System.FilePath.isDir config.resultFolder) then
IO.FS.createDir config.resultFolder
let evaluateFilesHandle ← IO.FS.Handle.mk (config.resultFolder ++ "/evaluateFiles.txt") .write
let all ← allHumanTheorems
let .some batches := Array.groupBySize all config.batchSize
| throwError "Batch size must be nonzero"
let mut running := #[]
for (batch, idx) in batches.zipWithIndex do
evaluateFilesHandle.putStrLn (toString idx)
evaluateFilesHandle.flush
let evalProc ← EvalProc.create "lake" #["env", "lean", "--stdin"]
let logPath := config.resultFolder ++ toString idx
evalProc.stdin.putStr (evalFile batch logPath)
let (_, evalProc) ← evalProc.takeStdin
running := running.push (idx, evalProc)
while running.size >= config.nthreads do
running ← tryWaitOn evaluateFilesHandle running
while running.size != 0 do
running ← tryWaitOn evaluateFilesHandle running
where
tryWaitOn (evaluateFilesHandle : IO.FS.Handle) (running : Array (Nat × _)) : CoreM (Array (Nat × _)) := do
let mut running' := #[]
for (idx, proc) in running do
let retCode? ← proc.tryWait
match retCode? with
| .some retCode =>
evaluateFilesHandle.putStrLn s!"{idx} : {retCode}"
evaluateFilesHandle.flush
| .none => running' := running'.push (idx, proc)
return running'
evalFile
(thms : Array Name) (logPath : String) : String :=
let lb := "{"
let rb := "}"
let thmsStrs : List String :=
match thms.toList.getLast? with
| .some last =>
thms.toList.dropLast.map (fun n => s!" {repr n},") ++ [s!" {repr last}"]
| .none => []
let lines := [
s!"import Mathlib",
"import Auto.EvaluateAuto.TestAuto",
"import Auto.Tactic",
"open Lean EvalAuto",
"",
"def thms : Std.HashSet Name := Std.HashSet.ofList ["
] ++ thmsStrs ++ [
"]",
"",
"def action : CoreM Unit := do",
" let p ← initSrcSearchPath",
s!" let _ ← runAutoOnConsts",
s!" {lb} maxHeartbeats := {config.maxHeartbeats}, timeout := {config.timeout},",
s!" solverConfig := {repr config.solverConfig},",
s!" logFile := Option.some ({repr (logPath ++ ".log")}), resultFile := Option.some ({repr (logPath ++ ".result")}){rb}",
" thms",
"",
"#eval action"
]
String.intercalate "\n" lines

end EvalAuto
18 changes: 6 additions & 12 deletions Auto/EvaluateAuto/TestTactics.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Lean
import Auto.EvaluateAuto.OS
import Auto.EvaluateAuto.Result
import Auto.EvaluateAuto.ConstAnalysis
import Auto.EvaluateAuto.EnvAnalysis
Expand Down Expand Up @@ -252,16 +253,9 @@ structure EvalTacticOnMathlibConfig where
recorded here and avoided (throw error immediately) during evaluation.
-/
nonterminates : Array (RegisteredTactic × Name)
/--
Number of threads to use
-/
/-- Number of threads to use -/
nthreads : Nat

abbrev EvalProc := IO.Process.Child ⟨.piped, .piped, .piped⟩

def EvalProc.create (path : String) (args : Array String) : IO EvalProc :=
IO.Process.spawn {stdin := .piped, stdout := .piped, stderr := .piped, cmd := path, args := args}

/--
This should be run after `import Mathlib`, and should be run with a `cwd` where
`lake env` creates an environment in which `Mathlib` and `lean-auto` are available
Expand Down Expand Up @@ -290,7 +284,7 @@ def evalTacticsAtMathlibHumanTheorems (config : EvalTacticOnMathlibConfig) : Cor
let evalProc ← EvalProc.create "lake" #["env", "lean", "--stdin"]
let logPath := config.resultFolder ++ extraLogPath
let validThms := (allTally.get? mm).getD #[]
evalProc.stdin.putStr (evalFile mm validThms logPath config.tactics)
evalProc.stdin.putStr (evalFile mm validThms logPath config)
let (_, evalProc) ← evalProc.takeStdin
running := running.push (mm, evalProc)
while running.size >= config.nthreads do
Expand All @@ -310,15 +304,15 @@ where
return running'
evalFile
(mm : Name) (validThms : Array Name)
(logPath : String) (tacs : Array RegisteredTactic) : String :=
(logPath : String) (config : EvalTacticOnMathlibConfig) : String :=
let lb := "{"
let rb := "}"
let thmsStrs : List String :=
match validThms.toList.getLast? with
| .some last =>
validThms.toList.dropLast.map (fun n => s!" {repr n},") ++ [s!" {repr last}"]
| .none => []
let tacsStr := String.intercalate ", " (tacs.map (fun tac => "." ++ toString tac)).toList
let tacsStr := String.intercalate ", " (config.tactics.map (fun tac => "." ++ toString tac)).toList
let lines := [
s!"import {mm}",
"import Auto.EvaluateAuto.TestTactics",
Expand All @@ -332,7 +326,7 @@ where
"def action : CoreM Unit := do",
" let p ← initSrcSearchPath",
s!" let _ ← evalAtModule ({repr mm}) p (fun ci => humanThms.contains ci.name)",
s!" {lb} tactics := #[{tacsStr}],",
s!" {lb} maxHeartbeats := {config.maxHeartbeats}, tactics := #[{tacsStr}],",
s!" logFile := {repr (logPath ++ ".log")}, resultFile := {repr (logPath ++ ".result")},",
s!" nonterminates := #[] {rb}",
"",
Expand Down
2 changes: 1 addition & 1 deletion Auto/Solver/SMT.lean
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ inductive SolverName where
| z3
| cvc4
| cvc5
deriving BEq, Hashable, Inhabited
deriving BEq, Hashable, Inhabited, Repr

instance : ToString SolverName where
toString : SolverName → String
Expand Down
4 changes: 2 additions & 2 deletions Auto/Solver/TPTP.lean
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Solver.TPTP
inductive ZEPortType where
| fo
| lams
deriving BEq, Hashable, Inhabited
deriving BEq, Hashable, Inhabited, Repr

inductive SolverName where
-- Disable TPTP prover
Expand All @@ -53,7 +53,7 @@ inductive SolverName where
-- E prover, higher-order version
| eproverHo
| vampire
deriving BEq, Hashable, Inhabited
deriving BEq, Hashable, Inhabited, Repr

instance : ToString SolverName where
toString : SolverName → String
Expand Down

0 comments on commit 2e43ab8

Please sign in to comment.