2024-05-28 17:25:22 -07:00
|
|
|
|
import Lean.Data.HashMap
|
|
|
|
|
import Pantograph.Compile
|
2024-09-06 21:07:12 -07:00
|
|
|
|
import Pantograph.Condensed
|
2024-05-28 17:25:22 -07:00
|
|
|
|
import Pantograph.Environment
|
2023-10-15 12:31:22 -07:00
|
|
|
|
import Pantograph.Goal
|
2024-05-28 17:25:22 -07:00
|
|
|
|
import Pantograph.Library
|
2023-10-15 17:15:23 -07:00
|
|
|
|
import Pantograph.Protocol
|
|
|
|
|
import Pantograph.Serial
|
2023-08-13 21:19:06 -07:00
|
|
|
|
|
|
|
|
|
namespace Pantograph
|
|
|
|
|
|
|
|
|
|
structure Context where
|
2023-08-14 17:07:53 -07:00
|
|
|
|
imports: List String
|
2023-08-13 21:19:06 -07:00
|
|
|
|
|
|
|
|
|
/-- Stores state of the REPL -/
|
|
|
|
|
structure State where
|
2023-10-15 17:15:23 -07:00
|
|
|
|
options: Protocol.Options := {}
|
2023-11-07 12:09:54 -08:00
|
|
|
|
nextId: Nat := 0
|
|
|
|
|
goalStates: Lean.HashMap Nat GoalState := Lean.HashMap.empty
|
2023-08-13 21:19:06 -07:00
|
|
|
|
|
2023-10-15 17:15:23 -07:00
|
|
|
|
/-- Main state monad for executing commands -/
|
2023-12-12 18:39:02 -08:00
|
|
|
|
abbrev MainM := ReaderT Context (StateT State Lean.CoreM)
|
2024-09-06 21:30:11 -07:00
|
|
|
|
|
2023-10-15 17:15:23 -07:00
|
|
|
|
-- HACK: For some reason writing `CommandM α := MainM (Except ... α)` disables
|
|
|
|
|
-- certain monadic features in `MainM`
|
|
|
|
|
abbrev CR α := Except Protocol.InteractionError α
|
2023-08-13 21:19:06 -07:00
|
|
|
|
|
2024-09-06 21:07:12 -07:00
|
|
|
|
def runMetaInMainM { α } (metaM: Lean.MetaM α): MainM α :=
|
|
|
|
|
metaM.run'
|
|
|
|
|
def runTermElabInMainM { α } (termElabM: Lean.Elab.TermElabM α) : MainM α :=
|
|
|
|
|
termElabM.run' (ctx := Condensed.elabContext) |>.run'
|
|
|
|
|
|
2023-10-15 17:15:23 -07:00
|
|
|
|
def execute (command: Protocol.Command): MainM Lean.Json := do
|
2023-08-15 15:40:54 -07:00
|
|
|
|
let run { α β: Type } [Lean.FromJson α] [Lean.ToJson β] (comm: α → MainM (CR β)): MainM Lean.Json :=
|
2023-08-13 21:19:06 -07:00
|
|
|
|
match Lean.fromJson? command.payload with
|
2023-08-15 15:40:54 -07:00
|
|
|
|
| .ok args => do
|
|
|
|
|
match (← comm args) with
|
|
|
|
|
| .ok result => return Lean.toJson result
|
|
|
|
|
| .error ierror => return Lean.toJson ierror
|
2023-08-24 22:51:40 -07:00
|
|
|
|
| .error error => return Lean.toJson $ errorCommand s!"Unable to parse json: {error}"
|
2023-08-15 15:40:54 -07:00
|
|
|
|
match command.cmd with
|
2023-11-06 12:20:08 -08:00
|
|
|
|
| "reset" => run reset
|
|
|
|
|
| "stat" => run stat
|
|
|
|
|
| "expr.echo" => run expr_echo
|
2023-12-12 18:56:25 -08:00
|
|
|
|
| "env.catalog" => run env_catalog
|
|
|
|
|
| "env.inspect" => run env_inspect
|
2023-12-13 19:35:32 -08:00
|
|
|
|
| "env.add" => run env_add
|
2023-11-06 12:20:08 -08:00
|
|
|
|
| "options.set" => run options_set
|
|
|
|
|
| "options.print" => run options_print
|
|
|
|
|
| "goal.start" => run goal_start
|
|
|
|
|
| "goal.tactic" => run goal_tactic
|
|
|
|
|
| "goal.continue" => run goal_continue
|
|
|
|
|
| "goal.delete" => run goal_delete
|
2023-10-27 15:41:12 -07:00
|
|
|
|
| "goal.print" => run goal_print
|
2024-05-31 16:35:46 -07:00
|
|
|
|
| "compile.unit" => run compile_unit
|
2023-08-13 21:19:06 -07:00
|
|
|
|
| cmd =>
|
2023-10-15 17:15:23 -07:00
|
|
|
|
let error: Protocol.InteractionError :=
|
2023-08-24 22:51:40 -07:00
|
|
|
|
errorCommand s!"Unknown command {cmd}"
|
2023-08-13 21:19:06 -07:00
|
|
|
|
return Lean.toJson error
|
|
|
|
|
where
|
2024-03-10 08:13:10 -07:00
|
|
|
|
errorCommand := errorI "command"
|
|
|
|
|
errorIndex := errorI "index"
|
2023-08-14 17:07:53 -07:00
|
|
|
|
-- Command Functions
|
2023-10-15 17:15:23 -07:00
|
|
|
|
reset (_: Protocol.Reset): MainM (CR Protocol.StatResult) := do
|
2023-08-14 17:07:53 -07:00
|
|
|
|
let state ← get
|
2023-08-27 19:58:52 -07:00
|
|
|
|
let nGoals := state.goalStates.size
|
2023-11-07 13:07:50 -08:00
|
|
|
|
set { state with nextId := 0, goalStates := Lean.HashMap.empty }
|
2023-08-27 19:58:52 -07:00
|
|
|
|
return .ok { nGoals }
|
2023-10-15 17:15:23 -07:00
|
|
|
|
stat (_: Protocol.Stat): MainM (CR Protocol.StatResult) := do
|
2023-08-27 19:58:52 -07:00
|
|
|
|
let state ← get
|
|
|
|
|
let nGoals := state.goalStates.size
|
|
|
|
|
return .ok { nGoals }
|
2023-12-15 10:37:55 -08:00
|
|
|
|
env_catalog (args: Protocol.EnvCatalog): MainM (CR Protocol.EnvCatalogResult) := do
|
2024-03-09 16:50:36 -08:00
|
|
|
|
let result ← Environment.catalog args
|
|
|
|
|
return .ok result
|
2023-12-12 18:56:25 -08:00
|
|
|
|
env_inspect (args: Protocol.EnvInspect): MainM (CR Protocol.EnvInspectResult) := do
|
2023-08-14 17:07:53 -07:00
|
|
|
|
let state ← get
|
2023-12-15 10:37:55 -08:00
|
|
|
|
Environment.inspect args state.options
|
2023-12-13 19:35:32 -08:00
|
|
|
|
env_add (args: Protocol.EnvAdd): MainM (CR Protocol.EnvAddResult) := do
|
2023-12-15 10:37:55 -08:00
|
|
|
|
Environment.addDecl args
|
2023-10-15 17:15:23 -07:00
|
|
|
|
expr_echo (args: Protocol.ExprEcho): MainM (CR Protocol.ExprEchoResult) := do
|
2023-08-14 21:43:40 -07:00
|
|
|
|
let state ← get
|
2024-06-11 12:44:42 -07:00
|
|
|
|
exprEcho args.expr (expectedType? := args.type?) (levels := args.levels.getD #[]) (options := state.options)
|
2023-10-15 17:15:23 -07:00
|
|
|
|
options_set (args: Protocol.OptionsSet): MainM (CR Protocol.OptionsSetResult) := do
|
2023-08-16 19:25:32 -07:00
|
|
|
|
let state ← get
|
|
|
|
|
let options := state.options
|
|
|
|
|
set { state with
|
|
|
|
|
options := {
|
|
|
|
|
-- FIXME: This should be replaced with something more elegant
|
|
|
|
|
printJsonPretty := args.printJsonPretty?.getD options.printJsonPretty,
|
|
|
|
|
printExprPretty := args.printExprPretty?.getD options.printExprPretty,
|
|
|
|
|
printExprAST := args.printExprAST?.getD options.printExprAST,
|
2024-04-12 22:39:47 -07:00
|
|
|
|
printDependentMVars := args.printDependentMVars?.getD options.printDependentMVars,
|
2023-10-26 22:47:42 -07:00
|
|
|
|
noRepeat := args.noRepeat?.getD options.noRepeat,
|
2023-08-16 19:25:32 -07:00
|
|
|
|
printAuxDecls := args.printAuxDecls?.getD options.printAuxDecls,
|
|
|
|
|
printImplementationDetailHyps := args.printImplementationDetailHyps?.getD options.printImplementationDetailHyps
|
2024-09-06 21:07:12 -07:00
|
|
|
|
automaticMode := args.automaticMode?.getD options.automaticMode,
|
2023-08-16 19:25:32 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return .ok { }
|
2024-05-04 23:36:42 -07:00
|
|
|
|
options_print (_: Protocol.OptionsPrint): MainM (CR Protocol.Options) := do
|
2023-08-16 19:25:32 -07:00
|
|
|
|
return .ok (← get).options
|
2023-10-15 17:15:23 -07:00
|
|
|
|
goal_start (args: Protocol.GoalStart): MainM (CR Protocol.GoalStartResult) := do
|
2023-08-13 21:19:06 -07:00
|
|
|
|
let state ← get
|
|
|
|
|
let env ← Lean.MonadEnv.getEnv
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let expr?: Except _ GoalState ← runTermElabInMainM (match args.expr, args.copyFrom with
|
2024-06-11 12:44:42 -07:00
|
|
|
|
| .some expr, .none => goalStartExpr expr (args.levels.getD #[])
|
2023-08-13 21:19:06 -07:00
|
|
|
|
| .none, .some copyFrom =>
|
2023-11-06 10:45:11 -08:00
|
|
|
|
(match env.find? <| copyFrom.toName with
|
2023-08-13 21:19:06 -07:00
|
|
|
|
| .none => return .error <| errorIndex s!"Symbol not found: {copyFrom}"
|
2023-12-08 17:31:25 -08:00
|
|
|
|
| .some cInfo => return .ok (← GoalState.create cInfo.type))
|
2023-10-27 15:32:59 -07:00
|
|
|
|
| _, _ =>
|
|
|
|
|
return .error <| errorI "arguments" "Exactly one of {expr, copyFrom} must be supplied")
|
2023-08-13 21:19:06 -07:00
|
|
|
|
match expr? with
|
2023-08-15 15:40:54 -07:00
|
|
|
|
| .error error => return .error error
|
2023-12-08 17:31:25 -08:00
|
|
|
|
| .ok goalState =>
|
2023-11-07 12:09:54 -08:00
|
|
|
|
let stateId := state.nextId
|
|
|
|
|
set { state with
|
|
|
|
|
goalStates := state.goalStates.insert stateId goalState,
|
|
|
|
|
nextId := state.nextId + 1
|
|
|
|
|
}
|
2023-11-06 11:51:31 -08:00
|
|
|
|
return .ok { stateId, root := goalState.root.name.toString }
|
2023-10-15 17:15:23 -07:00
|
|
|
|
goal_tactic (args: Protocol.GoalTactic): MainM (CR Protocol.GoalTacticResult) := do
|
2023-08-13 21:19:06 -07:00
|
|
|
|
let state ← get
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let .some goalState := state.goalStates.find? args.stateId |
|
|
|
|
|
return .error $ errorIndex s!"Invalid state index {args.stateId}"
|
|
|
|
|
let .some goal := goalState.goals.get? args.goalId |
|
|
|
|
|
return .error $ errorIndex s!"Invalid goal index {args.goalId}"
|
|
|
|
|
let nextGoalState?: Except _ TacticResult ← runTermElabInMainM do
|
|
|
|
|
match args.tactic?, args.expr?, args.have?, args.calc?, args.conv? with
|
|
|
|
|
| .some tactic, .none, .none, .none, .none => do
|
|
|
|
|
pure <| Except.ok <| ← goalState.tryTactic goal tactic
|
|
|
|
|
| .none, .some expr, .none, .none, .none => do
|
|
|
|
|
pure <| Except.ok <| ← goalState.tryAssign goal expr
|
|
|
|
|
| .none, .none, .some type, .none, .none => do
|
|
|
|
|
let binderName := args.binderName?.getD ""
|
|
|
|
|
pure <| Except.ok <| ← goalState.tryHave goal binderName type
|
|
|
|
|
| .none, .none, .none, .some pred, .none => do
|
|
|
|
|
pure <| Except.ok <| ← goalState.tryCalc goal pred
|
|
|
|
|
| .none, .none, .none, .none, .some true => do
|
|
|
|
|
pure <| Except.ok <| ← goalState.conv goal
|
|
|
|
|
| .none, .none, .none, .none, .some false => do
|
|
|
|
|
pure <| Except.ok <| ← goalState.convExit
|
|
|
|
|
| _, _, _, _, _ =>
|
|
|
|
|
let error := errorI "arguments" "Exactly one of {tactic, expr, have, calc, conv} must be supplied"
|
|
|
|
|
pure $ Except.error $ error
|
|
|
|
|
match nextGoalState? with
|
|
|
|
|
| .error error => return .error error
|
2024-09-06 21:30:11 -07:00
|
|
|
|
| .ok (.success nextGoalState) => do
|
|
|
|
|
let nextGoalState ← match state.options.automaticMode, args.conv? with
|
|
|
|
|
| true, .none => do
|
|
|
|
|
let .ok result := nextGoalState.resume goalState.goals | throwError "Resuming known goals"
|
|
|
|
|
pure result
|
|
|
|
|
| true, .some true => pure nextGoalState
|
|
|
|
|
| true, .some false => do
|
|
|
|
|
let .some (_, _, dormantGoals) := goalState.convMVar? | throwError "If conv exit succeeded this should not fail"
|
|
|
|
|
let .ok result := nextGoalState.resume dormantGoals | throwError "Resuming known goals"
|
|
|
|
|
pure result
|
|
|
|
|
| false, _ => pure nextGoalState
|
2024-09-06 21:07:12 -07:00
|
|
|
|
let nextStateId := state.nextId
|
|
|
|
|
set { state with
|
|
|
|
|
goalStates := state.goalStates.insert state.nextId nextGoalState,
|
|
|
|
|
nextId := state.nextId + 1,
|
|
|
|
|
}
|
|
|
|
|
let goals ← nextGoalState.serializeGoals (parent := .some goalState) (options := state.options) |>.run'
|
|
|
|
|
return .ok {
|
|
|
|
|
nextStateId? := .some nextStateId,
|
|
|
|
|
goals? := .some goals,
|
|
|
|
|
}
|
|
|
|
|
| .ok (.parseError message) =>
|
|
|
|
|
return .ok { parseError? := .some message }
|
|
|
|
|
| .ok (.invalidAction message) =>
|
|
|
|
|
return .error $ errorI "invalid" message
|
|
|
|
|
| .ok (.failure messages) =>
|
|
|
|
|
return .ok { tacticErrors? := .some messages }
|
2023-11-04 15:51:09 -07:00
|
|
|
|
goal_continue (args: Protocol.GoalContinue): MainM (CR Protocol.GoalContinueResult) := do
|
|
|
|
|
let state ← get
|
2023-11-07 12:09:54 -08:00
|
|
|
|
match state.goalStates.find? args.target with
|
2023-11-04 15:51:09 -07:00
|
|
|
|
| .none => return .error $ errorIndex s!"Invalid state index {args.target}"
|
|
|
|
|
| .some target => do
|
|
|
|
|
let nextState? ← match args.branch?, args.goals? with
|
|
|
|
|
| .some branchId, .none => do
|
2023-11-07 12:09:54 -08:00
|
|
|
|
match state.goalStates.find? branchId with
|
2023-11-04 15:51:09 -07:00
|
|
|
|
| .none => return .error $ errorIndex s!"Invalid state index {branchId}"
|
2024-03-31 16:11:41 -07:00
|
|
|
|
| .some branch => pure $ goalContinue target branch
|
2023-11-04 15:51:09 -07:00
|
|
|
|
| .none, .some goals =>
|
2024-03-10 08:13:10 -07:00
|
|
|
|
pure $ goalResume target goals
|
2023-11-04 15:51:09 -07:00
|
|
|
|
| _, _ => return .error <| errorI "arguments" "Exactly one of {branch, goals} must be supplied"
|
|
|
|
|
match nextState? with
|
2023-11-09 22:24:17 -08:00
|
|
|
|
| .error error => return .error <| errorI "structure" error
|
2023-11-04 15:51:09 -07:00
|
|
|
|
| .ok nextGoalState =>
|
2023-11-07 12:09:54 -08:00
|
|
|
|
let nextStateId := state.nextId
|
|
|
|
|
set { state with
|
|
|
|
|
goalStates := state.goalStates.insert nextStateId nextGoalState,
|
|
|
|
|
nextId := state.nextId + 1
|
|
|
|
|
}
|
2024-03-10 08:13:10 -07:00
|
|
|
|
let goals ← goalSerialize nextGoalState (options := state.options)
|
2023-11-04 15:51:09 -07:00
|
|
|
|
return .ok {
|
2023-11-09 22:24:17 -08:00
|
|
|
|
nextStateId,
|
|
|
|
|
goals,
|
2023-11-04 15:51:09 -07:00
|
|
|
|
}
|
2023-10-15 17:15:23 -07:00
|
|
|
|
goal_delete (args: Protocol.GoalDelete): MainM (CR Protocol.GoalDeleteResult) := do
|
2023-08-30 19:16:33 -07:00
|
|
|
|
let state ← get
|
2023-11-07 12:09:54 -08:00
|
|
|
|
let goalStates := args.stateIds.foldl (λ map id => map.erase id) state.goalStates
|
2023-08-30 19:16:33 -07:00
|
|
|
|
set { state with goalStates }
|
|
|
|
|
return .ok {}
|
2023-10-27 15:41:12 -07:00
|
|
|
|
goal_print (args: Protocol.GoalPrint): MainM (CR Protocol.GoalPrintResult) := do
|
|
|
|
|
let state ← get
|
2023-11-07 12:09:54 -08:00
|
|
|
|
match state.goalStates.find? args.stateId with
|
2023-10-27 15:41:12 -07:00
|
|
|
|
| .none => return .error $ errorIndex s!"Invalid state index {args.stateId}"
|
2023-12-12 18:39:02 -08:00
|
|
|
|
| .some goalState => runMetaM <| do
|
2024-03-14 16:34:01 -07:00
|
|
|
|
return .ok (← goalPrint goalState state.options)
|
2024-05-31 16:35:46 -07:00
|
|
|
|
compile_unit (args: Protocol.CompileUnit): MainM (CR Protocol.CompileUnitResult) := do
|
2024-05-28 17:25:22 -07:00
|
|
|
|
let module := args.module.toName
|
|
|
|
|
try
|
2024-05-31 16:35:46 -07:00
|
|
|
|
let steps ← Compile.processSource module
|
|
|
|
|
let units? := if args.compilationUnits then
|
|
|
|
|
.some $ steps.map λ step => (step.src.startPos.byteIdx, step.src.stopPos.byteIdx)
|
|
|
|
|
else
|
|
|
|
|
.none
|
|
|
|
|
let invocations? ← if args.invocations then
|
|
|
|
|
pure $ .some (← Compile.collectTacticsFromCompilation steps)
|
|
|
|
|
else
|
|
|
|
|
pure .none
|
|
|
|
|
return .ok { units?, invocations? }
|
2024-05-28 17:25:22 -07:00
|
|
|
|
catch e =>
|
|
|
|
|
return .error $ errorI "compile" (← e.toMessageData.toString)
|
2023-08-13 21:19:06 -07:00
|
|
|
|
|
|
|
|
|
end Pantograph
|