Zulip Chat Archive

Stream: general

Topic: typeclass inference of `add_comm_monoid` vs `add_comm_group`


Eric Wieser (Jul 21 2020 at 10:34):

Anne Baanen said:

There should be no issues making arguments to functions more general, since any consumers must declare these arguments themselves (unless they share a variables statement with your definition.)

This seems to fall apart once the requirements are pi-instances - see this small PR for an example

Anne Baanen (Jul 21 2020 at 11:01):

As far as I can tell, you can replace the first typeclass argument (λ i, by apply_instance) in your example with infer_instance in the third argument and it will still work.

Anne Baanen (Jul 21 2020 at 11:05):

The error comes from diamond inheritance in comm_group -> {comm_monoid, group} -> monoid, and the unifier is not smart enough to unify the comm_monoid.to_monoid (comm_group.to_monoid ?) that it expects with the instance group.to_monoid (comm_group.to_group __inst_3) that it has inferred.

Eric Wieser (Jul 21 2020 at 11:06):

Can priority be used to avoid needing the unifier?

Anne Baanen (Jul 21 2020 at 11:09):

Possibly. I'm not an expert in dealing with typeclass diamonds, though.

Anne Baanen (Jul 21 2020 at 11:19):

The priority was tweaked in the opposite direction in #2848:

I have tweaked a few instance priorities to make sure that things don't go too bad: there are often additional steps in typeclass inference, going from ring to semiring and from add_comm_group to add_comm_monoid, so I have increased their priority (putting it strictly between 100 and 1000), and adjusted some other priorities to get more canonical instance paths, fixing some preexisting weirdnesses (notably in multilinear maps).

Anne Baanen (Jul 21 2020 at 11:34):

This is breaking my head. If we add a line to direct_sum:

local attribute [instance, priority 1] add_comm_group.to_add_group

def direct_sum : Type* := Π i, β i

then the following works in direct_sum_module:

instance : semimodule R (direct_sum ι M) := @dfinsupp.to_semimodule _ M _ _ _ _

or:

instance : semimodule R (direct_sum ι M) := @dfinsupp.to_semimodule _ _ _ _ infer_instance _

But any other variations give a type error.

Eric Wieser (Jul 21 2020 at 13:03):

I don't have an understanding as to why, but relaxing a few more constraints seems to have set the linked PR on a possibly successful build... Guess I'll wait and see.

Eric Wieser (Jul 21 2020 at 14:02):

Yep, got it working #3490 is now passing. There's some really ugly _ introductions that had to happen to make it work.

Oliver Nash (Jul 16 2021 at 17:20):

I'm having trouble inferring an add_comm_group class on a direct sum of submodules. Here's a MWE:

import linear_algebra.direct_sum_module

open_locale direct_sum

variables (ι R M : Type*) [ring R] [add_comm_group M] [module R M] (I : ι  submodule R M)

 -- succeeds
def foo : add_comm_monoid ( i, I i) := infer_instance

 -- fails
def bar : add_comm_group ( i, I i) := infer_instance

 -- fails, error seems to make sense; `coe_sort` doesn't have a pi rule I guess
def baz : add_comm_group ( i, I i) := direct_sum.add_comm_group I

-- succeeds
def qux : add_comm_group ( i, I i) := direct_sum.add_comm_group (λ i, (I i))

Oliver Nash (Jul 16 2021 at 17:21):

My problem is essentially the failure of bar and I'm wondering what is the right solution. One solution would be to register qux as an instance but I'd guess this is fixing a symptom rather than a root cause.

Oliver Nash (Jul 16 2021 at 17:22):

Any advice from experts would be most gratefully received!

Eric Wieser (Jul 16 2021 at 20:06):

This is a known problem, and I think the workaround is to just add an almost duplicate instance like I've tried in #8221

Eric Wieser (Jul 16 2021 at 20:07):

@Kevin Buzzard made a minimal repro for lean4 and they fixed it there, but I'm kinda suspicious the fix only fixes a small subset of this type of problem given the description of how the fix works.

Eric Wieser (Jul 16 2021 at 20:09):

Unfortunately I can't tell if #8221 is actively a bad idea, or if we just have too many proofs on the borderline of timing out

Oliver Nash (Jul 17 2021 at 09:39):

Interesting thanks. I guess I'll just add the instance I need in a similar spirit to #8221 and move on with my life.

Oliver Nash (Jul 17 2021 at 09:39):

I'll also link to this conversation https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Typeclass.20resolution.20under.20binders for the sake of anyone coming here in the future.

Kevin Buzzard (Jul 17 2021 at 10:10):

I think it would be a really interesting and important project to try and manually make these typeclasses in Lean 4, to see what the devs think. There's no reason we can't start experimenting with the new typeclass inference now -- and it's what Leo wants us to do. I've already made a bunch of the algebra hierarchy in mathlib4 and there are UK undergraduates (both current and former) already actively writing mathlib 4 code.

Kevin Buzzard (Jul 17 2021 at 10:12):

It's no secret that Leo sometimes has a difficult relationship with mathematicians, but he has spent the last few of his life working furiously on what he hopes is his masterpiece and he would really like us to do small experiments with it. This sounds like a perfect small experiment to me.

Kevin Buzzard (Jul 17 2021 at 10:14):

I should also say that there's a schemes branch where I'm extremely slowly manually porting the definition of a scheme to lean 4. Let's make schemes in Lean again! It's one of my hobbies! Feel free to help anyone

Kevin Buzzard (Jul 17 2021 at 10:15):

@Cyril Cohen are you interested in any of these projects?

Kevin Buzzard (Jul 17 2021 at 10:15):

@Chris Hughes ?

Kevin Buzzard (Jul 17 2021 at 10:16):

This sounds like both good fun and cutting edge type theory

Oliver Nash (Jul 17 2021 at 10:19):

I'm in favour of this. I have no idea how hard / easy it would be to make these typeclass in Lean 4 but I'd be up for trying towards the end of next week if someone else doesn't beat me to it.


Last updated: Dec 20 2023 at 11:08 UTC