forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
rename_bvar
tactic (leanprover#342)
Co-authored-by: Jannis Limperg <jannis@limperg.de>
- Loading branch information
1 parent
260313c
commit 3a2b8a9
Showing
6 changed files
with
134 additions
and
30 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
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,46 @@ | ||
/- | ||
Copyright (c) 2019 Patrick Massot. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Arthur Paulino, Patrick Massot | ||
-/ | ||
|
||
import Lean | ||
import Mathlib.Util.Tactic | ||
|
||
namespace Mathlib.Tactic | ||
|
||
open Lean Meta Parser Elab Tactic | ||
|
||
/-- Renames a bound variable in a hypothesis. -/ | ||
def renameBVarHyp (mvarId : MVarId) (fvarId : FVarId) (old new : Name) : | ||
MetaM Unit := | ||
modifyLocalDecl mvarId fvarId fun ldecl => | ||
ldecl.setType $ ldecl.type.renameBVar old new | ||
|
||
/-- Renames a bound variable in the target. -/ | ||
def renameBVarTarget (mvarId : MVarId) (old new : Name) : MetaM Unit := | ||
modifyTarget mvarId fun e => e.renameBVar old new | ||
|
||
/-- | ||
* `rename_bvar old new` renames all bound variables named `old` to `new` in the target. | ||
* `rename_bvar old new at h` does the same in hypothesis `h`. | ||
```lean | ||
example (P : ℕ → ℕ → Prop) (h : ∀ n, ∃ m, P n m) : ∀ l, ∃ m, P l m := | ||
begin | ||
rename_bvar n q at h, -- h is now ∀ (q : ℕ), ∃ (m : ℕ), P q m, | ||
rename_bvar m n, -- target is now ∀ (l : ℕ), ∃ (n : ℕ), P k n, | ||
exact h -- Lean does not care about those bound variable names | ||
end | ||
``` | ||
Note: name clashes are resolved automatically. | ||
-/ | ||
elab "rename_bvar " old:ident " → " new:ident loc?:(ppSpace location)? : tactic => do | ||
let mvarId ← getMainGoal | ||
match loc? with | ||
| none => renameBVarTarget mvarId old.getId new.getId | ||
| some loc => | ||
withLocation (expandLocation loc) | ||
(fun fvarId => renameBVarHyp mvarId fvarId old.getId new.getId) | ||
(renameBVarTarget mvarId old.getId new.getId) | ||
fun _ => throwError "unexpected location syntax" |
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