Zulip Chat Archive

Stream: Is there code for X?

Topic: Int to Fin


Ka Wing Li (Jan 06 2026 at 09:29):

It must lie in mathlib somewhere, but I cannot find a conversion of Int to Fin?

Ruben Van de Velde (Jan 06 2026 at 09:36):

I'm not sure that it exists

Johan Commelin (Jan 06 2026 at 09:52):

@Ka Wing Li Are you perhaps interested in going from Int to ZMod n?

Eric Wieser (Jan 06 2026 at 10:55):

open scoped Fin.CommRing will do it if you do want Fin

Ka Wing Li (Jan 07 2026 at 02:01):

I have come up with

import Mathlib

def Int.toFin (z : Int) (n : Nat) (hn : n  0) : Fin n :=
  z.natMod n, Int.natMod_lt hn

#eval (4 : Fin 10) + (- 8 : Int) |>.toFin 10 (by trivial)

Ka Wing Li (Jan 07 2026 at 02:01):

Yet not so satisfying

Kim Morrison (Jan 07 2026 at 03:34):

You could supply an auto_param, i.e. := by lia for hn.

Ka Wing Li (Jan 07 2026 at 03:38):

oh nice, that does the trick

Ka Wing Li (Jan 07 2026 at 03:38):

Thanks!

Eric Wieser (Jan 07 2026 at 08:30):

Eric Wieser said:

open scoped Fin.CommRing will do it if you do want Fin

Just to be explicit, once you do this the function you want is docs#Int.cast, but this is inserted automatically as a coercion.


Last updated: Feb 28 2026 at 14:05 UTC