Zulip Chat Archive

Stream: new members

Topic: Having trouble converting Nat to generic type


Colin Jones ⚛️ (May 13 2024 at 17:33):

Hi, I've been having issues with this particular line of code:

import Mathlib

variable (n m : )

def Y2 [OfNat α m]: α := (m : α)

Does anyone know why this doesn't work?

Damiano Testa (May 13 2024 at 17:57):

Does it work without saying that α is a Type?

Kyle Miller (May 13 2024 at 17:57):

OfNat is meant to be used to give literal numbers an interpretation, and it's not meant to be a general coercion.

You can do

def Y2 [Coe Nat α] : α := (m : α)

or use some algebraic typeclass that provides such a coercion and do for example

def Y2 [Ring α] : α := (m : α)

Colin Jones ⚛️ (May 15 2024 at 18:30):

Does the Coe Nat stand for coercion?

Colin Jones ⚛️ (May 15 2024 at 18:44):

Thank you!


Last updated: May 02 2025 at 03:31 UTC