Zulip Chat Archive

Stream: lean4

Topic: norm_cast not seeing coercion?


Arien Malec (Oct 25 2022 at 22:21):

I'm translating @Kevin Buzzard's Complex Number Game to Lean 4, and tripping over issues.

@[ext] structure Complex where
  mk :: (re: Float) (im: Float)

instance : Coe Float Complex where
  coe f := f,0

@[simp, norm_cast] theorem of_float_inj {a b: Float} : (a : Complex) = b  a = b := sorry

gives me

 { re := a, im := 0 } = { re := b, im := 0 }

(BTW: is Real not a thing in Lean 4?)

James Gallicchio (Oct 25 2022 at 22:33):

Noncomputable reals will probably live in mathlib4, which is currently a partial port of lean3 mathlib :)
https://github.com/leanprover-community/mathlib4

Gabriel Ebner (Oct 25 2022 at 22:51):

Ah, I don't think I documented this part!

Gabriel Ebner (Oct 25 2022 at 22:51):

For norm_cast to work correctly, you need to register the coercion.

Gabriel Ebner (Oct 25 2022 at 22:51):

This also enables the notation in the delaborator.

Gabriel Ebner (Oct 25 2022 at 22:53):

The easiest way to do this is to declare a separate function for the coercion and tag it with @[coe]:

Gabriel Ebner (Oct 25 2022 at 22:53):

@[coe] def Float.toComplex (f : Float) : Complex := f, 0
instance : Coe Float Complex where coe := Float.toComplex

Gabriel Ebner (Oct 25 2022 at 22:55):

We're going to have some nice syntax on top of this that will automate the function+instance dance.

Gabriel Ebner (Oct 25 2022 at 22:55):

See also https://github.com/leanprover-community/mathlib4/issues/172


Last updated: Dec 20 2023 at 11:08 UTC