Zulip Chat Archive
Stream: lean4
Topic: Working with ExprDiff
Adam Topaz (Jan 12 2024 at 19:25):
I have some term of ExprDiff
, and I would like to extract the actual subexpressions that are part of the diff. I'm sure we have some functions that accomplish this but I can't seem to find anything useful. Note that ExprDiff
itself contains only the positions of the diffs:
import Lean
import Qq
open Lean Qq
#eval show MetaM Unit from do
let a : Q(Nat) ← mkFreshExprMVarQ q(Nat)
let e1 := q(($a + 1) + 2)
let e2 := q(($a + 4) + 2)
let d ← Lean.Widget.exprDiff e1 e2
for (i,j) in d.changesBefore do
IO.println s!"{i}, {j}"
for (i,j) in d.changesAfter do
IO.println s!"{i}, {j}"
Adam Topaz (Jan 12 2024 at 19:27):
In the example above, i would like to somehow extract that exprs associated to 1
and 4
.
Kyle Miller (Jan 12 2024 at 19:30):
Maybe it's docs#Lean.Meta.viewSubexpr or docs#Lean.Core.viewSubexpr ?
The first instantiates binders, creating a valid local context for the subexpression, and the second doesn't.
Adam Topaz (Jan 12 2024 at 19:30):
Thanks sounds promising! Thanks!
Adam Topaz (Jan 12 2024 at 19:34):
Works exactly as expected! Here is an example in case someone looks for this in the future:
import Lean
import Qq
open Lean Qq
#eval show MetaM Unit from do
let a : Q(Nat) ← mkFreshExprMVarQ q(Nat)
let e1 := q(($a + 1) + 2)
let e2 := q(($a + 4) + 2)
let d ← Lean.Widget.exprDiff e1 e2
for (i,_) in d.changesBefore do
IO.println s!"{← Core.viewSubexpr i e1}"
for (i,_) in d.changesAfter do
IO.println s!"{← Core.viewSubexpr i e2}"
Kyle Miller (Jan 12 2024 at 20:02):
(Note that if the diffs are under binders, then Core.viewSubexpr
might give expressions with bvars)
Last updated: May 02 2025 at 03:31 UTC