Zulip Chat Archive

Stream: lean4

Topic: Is there a tactic similar to “replace with” in Coq?


Qiancheng Fu (Jan 09 2025 at 07:15):

In Coq, the ‘replace m with n’ tactic asks for a proof of ‘m = n’ and replaces all occurrences of ‘m’ in the current goal with ‘n’. Is there a similar tactic in Lean?

Henrik Böving (Jan 09 2025 at 07:15):

You can use subst on an equality proof

Qiancheng Fu (Jan 09 2025 at 07:17):

I will have to manually clear the equality proof from my context after the substitution right?

Joachim Breitner (Jan 09 2025 at 08:18):

conv tactic equals_to should help here.

Ah, but that doesn't replace all occurrences

Joachim Breitner (Jan 09 2025 at 08:19):

Then there is the idiom rw [show m = n by …]

Kevin Buzzard (Jan 09 2025 at 15:04):

Qiancheng Fu said:

I will have to manually clear the equality proof from my context after the substitution right?

Not with subst, although subst only works if one side is a variable.

Qiancheng Fu (Jan 09 2025 at 17:46):

Joachim Breitner said:

Then there is the idiom rw [show m = n by …]

The rw [show m = n by ...] is fairly close to what I want, though when writing the proof inside by ..., the goal doesn't seem to be updated. I've elected to just write my own tactic that implements the behavior of Coq's replace with.

Kyle Miller (Jan 09 2025 at 18:04):

A related idiom is using a named metavariable for the proof, like

example (m n : Nat) : m + 2 = n + 3 := by
  rw [show m = n from ?pf]
  case pf =>
    sorry

Qiancheng Fu (Jan 09 2025 at 18:05):

Ahh, that’s nice!

Miguel Marco (Jan 09 2025 at 21:39):

I usually do that kind of things using

example (m n : Nat) : m + 2 = n + 3 := by
  have hmn : m = n
  · sorry
  rw [hmn]

or if you prefer to do it in a different order:

example (m n : Nat) : m + 2 = n + 3 := by
  suffices hmn : m = n
  · rw [hmn]
   ...
  · sorry

Joachim Breitner (Jan 09 2025 at 22:48):

That’s a natural thing to do, and probably one of the earliest approach users find. What I don’t like about this is that it clutters the infoview with these additional assumptions hmn that I only meant to use once.


Last updated: May 02 2025 at 03:31 UTC