Skip to content

Commit bac41b9

Browse files
committed
@ohyeat: replace loop by tail recursion
1 parent 75643c6 commit bac41b9

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

REPL/Main.lean

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,28 @@ def printFlush [ToString α] (s : α) : IO Unit := do
422422
out.flush -- Flush the output
423423

424424
/-- Read-eval-print loop for Lean. -/
425-
unsafe def repl : IO Unit :=
426-
StateT.run' loop {}
427-
where loop : M IO Unit := do
428-
let query ← getLines
429-
if query = "" then
430-
return ()
431-
if query.startsWith "#" || query.startsWith "--" then loop else
432-
IO.println <| toString <| ← match ← parse query with
433-
| .command r => return toJson (← runCommand r)
434-
| .file r => return toJson (← processFile r)
435-
| .proofStep r => return toJson (← runProofStep r)
436-
| .pickleEnvironment r => return toJson (← pickleCommandSnapshot r)
437-
| .unpickleEnvironment r => return toJson (← unpickleCommandSnapshot r)
438-
| .pickleProofSnapshot r => return toJson (← pickleProofSnapshot r)
439-
| .unpickleProofSnapshot r => return toJson (← unpickleProofSnapshot r)
440-
printFlush "\n" -- easier to parse the output if there are blank lines
441-
loop
425+
unsafe def repl : IO Unit := do
426+
let rec loop (s : State) : IO Unit := do
427+
let query ← getLines
428+
if query = "" then
429+
return ()
430+
else if query.startsWith "#" || query.startsWith "--" then
431+
loop s
432+
else
433+
let (result, s') ← StateT.run (do
434+
match ← parse query with
435+
| .command r => pure <| toJson (← runCommand r)
436+
| .file r => pure <| toJson (← processFile r)
437+
| .proofStep r => pure <| toJson (← runProofStep r)
438+
| .pickleEnvironment r => pure <| toJson (← pickleCommandSnapshot r)
439+
| .unpickleEnvironment r => pure <| toJson (← unpickleCommandSnapshot r)
440+
| .pickleProofSnapshot r => pure <| toJson (← pickleProofSnapshot r)
441+
| .unpickleProofSnapshot r => pure <| toJson (← unpickleProofSnapshot r)
442+
) s
443+
IO.println (toString result)
444+
printFlush "\n" -- easier to parse the output if there are blank lines
445+
loop s'
446+
loop {}
442447

443448
/-- Main executable function, run as `lake exe repl`. -/
444449
unsafe def main (_ : List String) : IO Unit := do

0 commit comments

Comments
 (0)