-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSON representation of InfoTree in output (#31)
- Loading branch information
Showing
7 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import REPL.Frontend | ||
import REPL.InfoTree | ||
import REPL.Lean.InfoTree | ||
import REPL.JSON | ||
import REPL.Main | ||
import REPL.Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import REPL.Lean.InfoTree | ||
import REPL.Lean.ContextInfo | ||
|
||
/-! | ||
# Exporting an `InfoTree` as Json | ||
-/ | ||
|
||
namespace Lean.Elab | ||
|
||
structure InfoTreeNode (α : Type) where | ||
kind : String | ||
node : Option α | ||
children : List Json | ||
deriving ToJson | ||
|
||
deriving instance ToJson for Lean.Position | ||
|
||
structure Syntax.Range where | ||
synthetic : Bool | ||
start : Lean.Position | ||
finish : Lean.Position | ||
deriving ToJson | ||
|
||
structure Syntax.Json where | ||
pp : Option String | ||
-- raw : String | ||
range : Range | ||
deriving ToJson | ||
|
||
def _root_.Lean.Syntax.toRange (stx : Syntax) (ctx : ContextInfo) : Syntax.Range := | ||
let pos := stx.getPos?.getD 0 | ||
let endPos := stx.getTailPos?.getD pos | ||
{ start := ctx.fileMap.toPosition pos | ||
finish := ctx.fileMap.toPosition endPos | ||
synthetic := match stx.getHeadInfo with | ||
| .original .. => false | ||
| _ => true } | ||
|
||
def _root_.Lean.Syntax.toJson (stx : Syntax) (ctx : ContextInfo) (lctx : LocalContext) : IO Syntax.Json := do | ||
return { | ||
pp := match (← ctx.ppSyntax lctx stx).pretty with | ||
| "failed to pretty print term (use 'set_option pp.rawOnError true' for raw representation)" => none | ||
| pp => some pp | ||
-- raw := toString stx | ||
range := stx.toRange ctx } | ||
|
||
structure TacticInfo.Json where | ||
name : Option Name | ||
stx : Syntax.Json | ||
goalsBefore : List String | ||
goalsAfter : List String | ||
deriving ToJson | ||
|
||
-- Note: this is not responsible for converting the children to Json. | ||
def TacticInfo.toJson (i : TacticInfo) (ctx : ContextInfo) : IO TacticInfo.Json := do | ||
return { | ||
name := i.name? | ||
stx := | ||
{ pp := Format.pretty (← i.pp ctx), | ||
-- raw := toString i.info.stx, | ||
range := i.stx.toRange ctx }, | ||
goalsBefore := (← i.goalState ctx).map Format.pretty, | ||
goalsAfter := (← i.goalStateAfter ctx).map Format.pretty } | ||
|
||
structure CommandInfo.Json where | ||
elaborator : Option Name | ||
stx : Syntax.Json | ||
deriving ToJson | ||
|
||
def CommandInfo.toJson (info : CommandInfo) (ctx : ContextInfo) : IO CommandInfo.Json := do | ||
return { | ||
elaborator := match info.elaborator with | .anonymous => none | n => some n, | ||
stx := ← info.stx.toJson ctx {} } | ||
|
||
structure TermInfo.Json where | ||
elaborator : Option Name | ||
stx : Syntax.Json | ||
expectedType? : Option String | ||
expr : String | ||
isBinder : Bool | ||
deriving ToJson | ||
|
||
def TermInfo.toJson (info : TermInfo) (ctx : ContextInfo) : IO TermInfo.Json := do | ||
return { | ||
elaborator := match info.elaborator with | .anonymous => none | n => some n, | ||
stx := ← info.stx.toJson ctx info.lctx, | ||
expectedType? := ← info.expectedType?.mapM fun ty => do | ||
pure (← ctx.ppExpr info.lctx ty).pretty | ||
expr := (← ctx.ppExpr info.lctx info.expr).pretty | ||
isBinder := info.isBinder } | ||
|
||
structure InfoTree.HoleJson where | ||
goalState : String | ||
deriving ToJson | ||
|
||
partial def InfoTree.toJson (t : InfoTree) (ctx? : Option ContextInfo) : IO Json := do | ||
match t with | ||
| .context i t => t.toJson i | ||
| .node info children => | ||
if let some ctx := ctx? then | ||
let node : Option Json ← match info with | ||
| .ofTermInfo info => some <$> (do pure <| Lean.toJson (← info.toJson ctx)) | ||
| .ofCommandInfo info => some <$> (do pure <| Lean.toJson (← info.toJson ctx)) | ||
| .ofTacticInfo info => some <$> (do pure <| Lean.toJson (← info.toJson ctx)) | ||
| _ => pure none | ||
return Lean.toJson (InfoTreeNode.mk info.kind node (← children.toList.mapM fun t' => t'.toJson ctx)) | ||
else throw <| IO.userError "No `ContextInfo` available." | ||
| .hole mvarId => | ||
if let some ctx := ctx? then | ||
return Lean.toJson (InfoTree.HoleJson.mk (← ctx.runMetaM {} (do Meta.ppGoal mvarId)).pretty) | ||
else throw <| IO.userError "No `ContextInfo` available." | ||
|
||
end Lean.Elab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{"messages": | ||
[{"severity": "warning", | ||
"pos": {"line": 1, "column": 7}, | ||
"endPos": {"line": 1, "column": 8}, | ||
"data": "unused variable `h` [linter.unusedVariables]"}], | ||
"infotree": | ||
[{"node": | ||
{"stx": | ||
{"range": | ||
{"synthetic": false, | ||
"start": {"line": 1, "column": 28}, | ||
"finish": {"line": 1, "column": 39}}, | ||
"pp": "constructor"}, | ||
"name": "Lean.Parser.Tactic.constructor", | ||
"goalsBefore": ["h : Int\n⊢ Nat"], | ||
"goalsAfter": []}, | ||
"kind": "TacticInfo", | ||
"children": []}], | ||
"env": 0} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{"cmd": "def f (h : Int) : Nat := by constructor", "infotree": "substantive"} | ||
|