Skip to content

Commit

Permalink
fix: revertAll must clear auxiliary declarations (#6386)
Browse files Browse the repository at this point in the history
This PR ensures that `revertAll` clears auxiliary declarations when
invoked directly by users.

closes #6263
  • Loading branch information
leodemoura authored Dec 15, 2024
1 parent e08d35c commit a8a160b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/Lean/Meta/Tactic/Grind/RevertAll.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Lean.Meta.Tactic.Util
import Lean.Meta.Tactic.Revert

namespace Lean.Meta.Grind

/--
Revert all free variables at goal `mvarId`.
Reverts all free variables in the goal `mvarId`.
**Remark**: Auxiliary local declarations are cleared.
The `grind` tactic also clears them, but this tactic can be used independently by users.
-/
def _root_.Lean.MVarId.revertAll (mvarId : MVarId) : MetaM MVarId := mvarId.withContext do
mvarId.checkNotAssigned `revertAll
let fvarIds := (← getLCtx).getFVarIds
let fvars ← fvarIds.filterMapM fun fvarId => do
if (← fvarId.getDecl).isAuxDecl then
return none
else
return some (mkFVar fvarId)
let tag ← mvarId.getTag
let mut toRevert := #[]
for fvarId in (← getLCtx).getFVarIds do
unless (← fvarId.getDecl).isAuxDecl do
toRevert := toRevert.push fvarId
mvarId.setKind .natural
let (e, _) ←
try
liftMkBindingM <| MetavarContext.revert fvars mvarId (preserveOrder := true)
finally
mvarId.setKind .syntheticOpaque
let mvar := e.getAppFn
mvar.mvarId!.setTag tag
return mvar.mvarId!
let (_, mvarId) ← mvarId.revert toRevert
(preserveOrder := true)
(clearAuxDeclsInsteadOfRevert := true)
return mvarId

end Lean.Meta.Grind
14 changes: 14 additions & 0 deletions tests/lean/run/6263.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Lean

open Lean.Elab.Tactic

variable (p q : Prop)
theorem foo (h : p ∧ q) : q ∧ p := by
run_tac liftMetaTactic1 (·.revertAll)
guard_target =ₛ ∀ (p q : Prop), p ∧ q → q ∧ p
sorry

theorem bla (h : p ∧ q) : q ∧ p := by
revert p q
guard_target =ₛ ∀ (p q : Prop), p ∧ q → q ∧ p
sorry

0 comments on commit a8a160b

Please sign in to comment.