Zulip Chat Archive

Stream: lean4

Topic: simproc for Fin


Bhavik Mehta (Nov 11 2024 at 13:33):

Do we have a simproc or simp lemma for

example : ((3 : Fin 4) : Nat) = 3 := by
  simp

or related explicit calculations? It seems to me like Fin.isValue is meant to do something like this, but currently (even in Mathlib) I can't see any automatic way of simplifying the lhs without already writing down the rhs.

Eric Wieser (Nov 11 2024 at 13:42):

rw [Fin.coe_ofNat_eq_mod] works here (docs#Fin.coe_ofNat_eq_mod)

Eric Wieser (Nov 11 2024 at 13:42):

rewrite [Fin.coe_ofNat_eq_mod] is a little less magic

Eric Wieser (Nov 11 2024 at 13:42):

Maybe this should be simp?

Bhavik Mehta (Nov 11 2024 at 13:43):

I just came across that lemma too, but it's marked simp. Why does simp not succeed using it?

Bhavik Mehta (Nov 11 2024 at 13:44):

Before finding that lemma I also tried making

@[simp] lemma coe_ofNat_of_lt {a b : } [NeZero b] (h : a < b) :
    ((OfNat.ofNat (no_index a) : Fin b) : ) = a := Nat.mod_eq_of_lt h

which also doesn't work with simp in this example

Eric Wieser (Nov 11 2024 at 13:44):

I would guess this is all related to the no_index trouble around ofNat (edit: lean4#3684)

Eric Wieser (Nov 11 2024 at 13:48):

Oh, it's a typo, no_index OfNat.ofNat n should be no_index (OfNat.ofNat n)

@[simp]
theorem coe_ofNat_eq_mod (m n : ) [NeZero m] :
    ((no_index (OfNat.ofNat n) : Fin m) : ) = OfNat.ofNat n % m :=
  rfl

Eric Wieser (Nov 11 2024 at 13:48):

I always get the precedence of no_index wrong

Eric Wieser (Nov 11 2024 at 13:56):

#18872

Bhavik Mehta (Nov 11 2024 at 13:59):

There's two other instances of the pattern no_index OfNat.ofNat n in mathlib, do you want to change those in this PR too?

Bhavik Mehta (Nov 11 2024 at 14:04):

image.png
(for your convenience!)

Yakov Pechersky (Nov 11 2024 at 14:58):

We had a norm_fin tactic in mathlib3 that might be in scope for this. And, probably not what you wanted for your use site (you said you don't want to write the rhs), but rfl proves your original example.

Bhavik Mehta (Nov 11 2024 at 15:03):

Yeah, rfl working is good but I'd really like other simp and rw lemmas to apply afterwards, so rfl isn't really sufficient here unfortunately

Eric Wieser (Nov 13 2024 at 23:21):

Eric Wieser said:

#18872

I ended up removing @[simp] from this lemma because it's not very confluent. I think your version would be safer.


Last updated: May 02 2025 at 03:31 UTC