Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: round-trip failure for daggered names with default To/FromJson #19

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions demo/Demo.lean
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ theorem test (n : Nat) : n * 1 = n := by
rw [← ih]
simp
%end

%example proofWithInstance
-- Test that proof states containing daggered names can round-trip
def test2 [ToString α] (x : α) : Decidable (toString x = "") := by
constructor; sorry
%end
9 changes: 0 additions & 9 deletions src/examples/SubVerso/Examples.lean
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,3 @@ def wxyz (n : Nat) := 1 + 3 + n
#check wxyz
def xyz (n : Nat) := 1 + %ex{test2}{3 + n}
%end


-- %example test
-- #eval 5
-- %end

-- %dump test
-- %dump test3.test2
-- %dump test3
24 changes: 24 additions & 0 deletions src/highlighting/SubVerso/Highlighting/Highlighted.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ namespace SubVerso.Highlighting
deriving instance Repr for Std.Format.FlattenBehavior
deriving instance Repr for Std.Format

-- Workaround for the fact that the default From/ToJson instances for Name
-- don't always round-trip

private def altNameJson (n : Name) : Json := Json.arr (splitName #[] n)
where
splitName acc
| .anonymous => acc
| .num x n => splitName acc x |>.push n
| .str x s => splitName acc x |>.push s

private def altNameUnJson (json : Json) : Except String Name := do
let arr ← json.getArr?
let mut n := .anonymous
for v in arr do
match v with
| (s : String) => n := n.str s
| (i : Nat) => n := n.num i
| other => .error s!"Expected a string or number as a name component, got '{other}'"
pure n

private local instance : ToJson Name := ⟨altNameJson⟩
private local instance : FromJson Name := ⟨altNameUnJson⟩


inductive Token.Kind where
| /-- `occurrence` is a unique identifier that unites the various keyword tokens from a given production -/
keyword (name : Option Name) (occurrence : Option String) (docs : Option String)
Expand Down
Loading