Zulip Chat Archive

Stream: general

Topic: Error in lean3


Adalynn (Apr 05 2023 at 15:33):

I know that you're typically not supposed to be have prelude at the top of files, but it's for a personal (read: a crank) project to see how close I can get Lean 3 notation to standard mathematics. So, now to the actual question: when trying to define has_zero and has_one to get natural numbers to work, it seems to give me an error about how it can't force them to be nats. I can't seem to find the error message anywhere in the C++ code for one, so I'm just... wondering what's going on?

Alex J. Best (Apr 05 2023 at 15:37):

Can you make a #mwe?

Adalynn (Apr 05 2023 at 15:51):

Will do, just in the middle of class right now so I don't have the MWE on hand.

Adalynn (Apr 05 2023 at 16:34):

prelude
universe u
class has_zero(α: Type u) := (zero: α)

constant T: Type
constant t: T
noncomputable instance: has_zero T := has_zero.mk t

#check 0

and the error is "invalid numeral, failed to force numeral to be a nat"

Adalynn (Apr 05 2023 at 16:35):

It still breaks even if I try something like 0 + t when I add in notation-stuff for + as well

Johan Commelin (Apr 05 2023 at 16:57):

The default type for numerals in Lean 3 is aka nat. But I don't know how this plays with prelude.

Johan Commelin (Apr 05 2023 at 16:58):

Is nat defined after prelude? Do you need to provide an instance of has_zero nat?

Adalynn (Apr 05 2023 at 17:15):

prelude-mode doesn't have nat, I've checked. So the default is the unicode symbol? I'll have to try that later, along with a has_zero nat.

Johan Commelin (Apr 05 2023 at 17:24):

@Adalynn No, you don't need the unicode symbol. Maybe just assert the existence of nat, via

constant nat
axiom nat.has_zero : has_zero nat

Johan Commelin (Apr 05 2023 at 17:24):

Or define the actual inductive type, etc...

Adalynn (Apr 05 2023 at 17:32):

Yeah, that also doesn't work... Need to actually have an instance, which causes it to work. Thanks for the advice!

Notification Bot (Apr 05 2023 at 17:56):

Adalynn has marked this topic as resolved.

Alex J. Best (Apr 05 2023 at 18:33):

I'm still a bit confused by your question, the following works fine for me

prelude
universe u
class has_zero(α: Type u) := (zero: α)

class has_add (α: Type u) := (add: α  α  α)
notation a ` + ` b := has_add.add a b
constant T: Type
constant t: T
noncomputable instance: has_zero T := has_zero.mk t

noncomputable instance: has_add T := sorry

#check 0 + t
#check (0 : T)

Johan Commelin (Apr 05 2023 at 18:35):

@Alex J. Best But does #check 0 (without a type) also work for you?

Alex J. Best (Apr 05 2023 at 18:35):

No, but I wouldn't expect it to

Alex J. Best (Apr 05 2023 at 18:37):

Maybe I just don't understand what the goal is enough but "It still breaks even if I try something like 0 + t when I add in notation-stuff for + as well" was unclear to me, so I'm just showing that if you set things up right that works fine. and 0 is interpreted as something of type T

Adalynn (Apr 05 2023 at 19:04):

OH right, It's because I had in my file, add was defined via class has_add(α: Type u)(β: Type v)(γ: out_param(Type w)) := (add: α → β → γ) to allow for coset notation

Alex J. Best (Apr 05 2023 at 19:10):

Right if you have a heterogeneous addition operation you would need to specify the type of 0as (0 : T) if you wanted it to be in T otherwise lean would assume its a nat indeed (even if nat isn't defined)

Adalynn (Apr 05 2023 at 19:44):

Yeah, I thought initially it wasn't the homogeneity of the has_add but the homogeneity of the arguments (i.e. I thought it would work if I had an instance as has_add T T T) which was why I was so confused, thanks.

Notification Bot (Apr 06 2023 at 22:02):

Adalynn has marked this topic as unresolved.

Adalynn (Apr 06 2023 at 22:04):

prelude
...
instance Class_subset_Class: has_subset Class Class :=
    λC: Class, λD: Class, (x: Set)[xC  xD]⟩
  noncomputable instance Class_subset_Set: has_subset Class Set :=
    λC: Class, λS: Set, (x: Set)[xC  xS]⟩
  noncomputable instance Set_subset_Class: has_subset Set Class :=
    λS: Set, λC: Class, (x: Set)[xS  xC]⟩
  noncomputable instance Set_subset_Set: has_subset Set Set :=
    λS: Set, λT: Set, (x: Set)[xS  xT]⟩

constants A B: Class
constants S T: Set
#check A  B

For some reason this also breaks, with a heterogeneous $\subseteq$ defined in a different file...

this is probably why people don't use prelude often

Adalynn (Apr 06 2023 at 22:10):

the same thing happened when I tried overloading the heterogenous equals with Set = Class and Class = Set, it for some reason clashed with the homogenous equals instance in a different file

What's going on? Does the lean 3 compiler just like to force homogeneity?

Kyle Miller (Apr 06 2023 at 22:13):

For what it's worth, here's where the nat error in the C++ is reported https://github.com/leanprover-community/lean/blob/f2b2beefe2e9ab200d23e1c99dc20879eb32e9fa/src/frontends/lean/elaborator.cpp#L3618

Adalynn (Apr 06 2023 at 22:21):

I fixed the nat error already, by using a workaround... but I'm now stuck on a similar error with typeclasses

Adalynn (Apr 06 2023 at 22:42):

Oh, I fixed it, I had needless out_params everywhere


Last updated: Dec 20 2023 at 11:08 UTC