Add expression IO stub for constant types

This commit is contained in:
Leni Aniva 2023-05-20 14:04:09 -07:00
parent 65da39440d
commit c4a1ccad13
4 changed files with 50 additions and 18 deletions

View File

@ -2,10 +2,27 @@ import Lean.Data.Json
import Lean.Environment import Lean.Environment
import Pantograph.Commands import Pantograph.Commands
import Pantograph.IO
import Pantograph.Symbols import Pantograph.Symbols
namespace Pantograph namespace Pantograph
/-- Stores state of the REPL -/
structure State where
environments: Array Lean.Environment
-- State monad
abbrev T (m: Type → Type) := StateT State m
abbrev Subroutine α := ExceptT String (T IO) α
def nextId (s: State): Nat := s.environments.size
def State.getEnv (state: State) (id: Nat): Except String Lean.Environment :=
match state.environments.get? id with
| .some env => return env
| .none => throw s!"Invalid environment id {id}"
-- Utilities -- Utilities
def option_expect (o: Option α) (error: String): Except String α := def option_expect (o: Option α) (error: String): Except String α :=
match o with match o with
@ -17,7 +34,6 @@ structure Command where
payload: Lean.Json payload: Lean.Json
deriving Lean.FromJson deriving Lean.FromJson
/-- Parse a command either in `{ "cmd": ..., "payload": ... }` form or `cmd { ... }` form. -/ /-- Parse a command either in `{ "cmd": ..., "payload": ... }` form or `cmd { ... }` form. -/
def parse_command (s: String): Except String Command := do def parse_command (s: String): Except String Command := do
let s := s.trim let s := s.trim
@ -34,14 +50,6 @@ def parse_command (s: String): Except String Command := do
| .none => throw "Command is empty" | .none => throw "Command is empty"
structure State where
environments: Array Lean.Environment
-- State monad
abbrev T (m: Type → Type) := StateT State m
abbrev Subroutine α := ExceptT String (T IO) α
def nextId (s: State): Nat := s.environments.size
open Commands open Commands
@ -60,6 +68,10 @@ unsafe def execute (command: String): ExceptT String (T IO) Lean.Json := do
-- Delete all the environments -- Delete all the environments
let ret ← clear let ret ← clear
return Lean.toJson ret return Lean.toJson ret
| "inspect" =>
let args: Commands.Inspect ← Lean.fromJson? command.payload
let ret ← inspect args
return Lean.toJson ret
| "proof.trace" => | "proof.trace" =>
let args: Commands.ProofTrace ← Lean.fromJson? command.payload let args: Commands.ProofTrace ← Lean.fromJson? command.payload
let ret ← proof_trace args let ret ← proof_trace args
@ -82,19 +94,27 @@ unsafe def execute (command: String): ExceptT String (T IO) Lean.Json := do
filtered_symbols := num_filtered_symbols } filtered_symbols := num_filtered_symbols }
catalog (args: Catalog): Subroutine CatalogResult := do catalog (args: Catalog): Subroutine CatalogResult := do
let state ← get let state ← get
match state.environments.get? args.id with let env ← state.getEnv args.id
| .some env =>
let names := env.constants.fold (init := []) (λ es name info => let names := env.constants.fold (init := []) (λ es name info =>
match to_filtered_symbol name info with match to_filtered_symbol name info with
| .some x => x::es | .some x => x::es
| .none => es) | .none => es)
return { theorems := names } return { theorems := names }
| .none => throw s!"Invalid environment id {args.id}"
clear: Subroutine ClearResult := do clear: Subroutine ClearResult := do
let state ← get let state ← get
for env in state.environments do for env in state.environments do
env.freeRegions env.freeRegions
return { n := state.environments.size } return { n := state.environments.size }
inspect (args: Inspect): Subroutine InspectResult := do
let state ← get
let env ← state.getEnv args.id
let info? := env.find? <| strToName args.symbol
let info ← match info? with
| none => throw s!"Symbol not found: {args.symbol}"
| some info => pure info.toConstantVal
-- Now print the type expression
let format := IO.exprToStr env info.type
return { type := format }
proof_trace (args: ProofTrace): Subroutine ProofTraceResult := do proof_trace (args: ProofTrace): Subroutine ProofTraceResult := do
-- Step 1: Create tactic state -- Step 1: Create tactic state
-- Step 2: Execute tactic -- Step 2: Execute tactic

View File

@ -4,7 +4,7 @@ import Lean.Data.Json
namespace Pantograph.Commands namespace Pantograph.Commands
structure Create where structure Create where
imports : List String imports : List String := []
deriving Lean.FromJson deriving Lean.FromJson
structure CreateResult where structure CreateResult where
id: Nat id: Nat
@ -23,6 +23,14 @@ structure ClearResult where
n: Nat -- Number of environments reset n: Nat -- Number of environments reset
deriving Lean.ToJson deriving Lean.ToJson
structure Inspect where
id: Nat -- Environment id
symbol: String
deriving Lean.FromJson
structure InspectResult where
type: String
deriving Lean.ToJson
structure ProofTrace where structure ProofTrace where
id: Nat -- Environment id id: Nat -- Environment id
deriving Lean.FromJson deriving Lean.FromJson

View File

@ -8,6 +8,9 @@ Expression IO
namespace Pantograph.IO namespace Pantograph.IO
def exprToStr (env: Lean.Environment) (e: Lean.Expr): String :=
let format := Lean.Meta.ppExpr e
"stub"
end Pantograph.IO end Pantograph.IO

View File

@ -27,6 +27,7 @@ result of a command execution. The command can be passed in one of two formats
command { ... } command { ... }
{ "cmd": command, "payload": ... } { "cmd": command, "payload": ... }
``` ```
The list of available commands can be found in `Pantograph/Commands.lean`
Example: (~5k symbols) Example: (~5k symbols)
``` ```