Zulip Chat Archive

Stream: mathlib4

Topic: nth_rw


Kenny Lau (Jul 06 2025 at 02:52):

import Mathlib.Tactic.NthRewrite

theorem id_apply_mul {a b : Nat} : id (a * b) = a * b := rfl

example : (2 * 3) + (5 * 6) = 4 := by nth_rw 1 2 [ id_apply_mul]
-- ⊢ id (2 * 3) + 5 * 6 = 4

example : (2 * 3) + (2 * 3) = 4 := by nth_rw 1 2 [ id_apply_mul]
-- ⊢ id (2 * 3) + id (2 * 3) = 4

Is this intentional? That nth_rw for more than one n only works on the exact previous match of the pattern

Aaron Liu (Jul 06 2025 at 02:54):

Yes

Kenny Lau (Jul 06 2025 at 02:54):

(Note that it doesn't help if I write it a second time inside the same call)

Kenny Lau (Jul 06 2025 at 02:54):

what should I write if I want to make it id (2 * 3) + id (5 * 6)?

Aaron Liu (Jul 06 2025 at 02:54):

You have to rewrite twice then

Aaron Liu (Jul 06 2025 at 02:55):

Kenny Lau said:

(Note that it doesn't help if I write it a second time inside the same call)

What do you mean by this?

Kenny Lau (Jul 06 2025 at 02:55):

import Mathlib.Tactic.NthRewrite

theorem id_apply_mul {a b : Nat} : id (a * b) = a * b := rfl

example : (2 * 3) + (5 * 6) = 4 := by nth_rw 1 2 [ id_apply_mul,  id_apply_mul]
-- ⊢ id (id (2 * 3)) + 5 * 6 = 4

Kenny Lau (Jul 06 2025 at 02:55):

I understand that I need to write nth_rw twice

Aaron Liu (Jul 06 2025 at 02:55):

In any case, I'd recommend using conv mode over nth_rw

Aaron Liu (Jul 06 2025 at 02:56):

Kenny Lau said:

import Mathlib.Tactic.NthRewrite

theorem id_apply_mul {a b : Nat} : id (a * b) = a * b := rfl

example : (2 * 3) + (5 * 6) = 4 := by nth_rw 1 2 [ id_apply_mul,  id_apply_mul]
-- ⊢ id (id (2 * 3)) + 5 * 6 = 4

This matches 2 * 3 twice independently

Kenny Lau (Jul 06 2025 at 02:56):

Aaron Liu said:

In any case, I'd recommend using conv mode over nth_rw

why?

Aaron Liu (Jul 06 2025 at 02:56):

It feels better to me? I'm not sure.

Kenny Lau (Jul 06 2025 at 02:57):

import Mathlib.Tactic.NthRewrite

theorem id_apply_mul {a b : Nat} : id (a * b) = a * b := rfl

example : (2 * 3) + (5 * 6) = 4 := by
  conv => rw [ id_apply_mul]; enter [1,2]; rw [ id_apply_mul]
  -- ⊢ id (2 * 3) + id (5 * 6) = 4

Kenny Lau (Jul 06 2025 at 02:57):

so you would prefer this?

Aaron Liu (Jul 06 2025 at 02:57):

What I'd actually do in this situation would be to make {a b : Nat} explicit in id_apply_mul

Kenny Lau (Jul 06 2025 at 02:58):

good call, that actually helped!

Kenny Lau (Jul 06 2025 at 02:58):

i forgot about matching patterns


Last updated: Dec 20 2025 at 21:32 UTC