Zulip Chat Archive

Stream: general

Topic: dangerous instance


Kenny Lau (Jul 31 2020 at 18:44):

import ring_theory.algebra_tower

instance is_algebra_tower.of_ring_hom {R A B : Type*}
  [comm_semiring R] [comm_semiring A] [comm_semiring B]
  [algebra R A] [algebra R B] (f : A [R] B) :
  @is_algebra_tower R A B _ _ _ _ (ring_hom.to_algebra f) _ :=
by { letI := (f : A →+* B).to_algebra,
exact is_algebra_tower.of_algebra_map_eq (λ x, (f.commutes x).symm) }

#lint

/-
/- Checking 1 declarations (plus 1 automatically generated ones) in the current file -/

/- The `instance_priority` linter reports: -/
/- DANGEROUS INSTANCE PRIORITIES.
The following instances always apply, and therefore should have a priority < 1000.
If you don't know what priority to choose, use priority 100.

If this is an automatically generated instance (using the keywords `class` and `extends`),
see note [lower instance priority] and see note [default priority] for instructions to change the priority: -/
#print is_algebra_tower.of_ring_hom /- set priority below 1000 -/
-/

Kenny Lau (Jul 31 2020 at 18:44):

why is this a dangerous instance?

Kenny Lau (Jul 31 2020 at 18:45):

given R-algebras A and B, and an R-alg hom f : A -> B, this is asserting that R, A, B form an algebra tower

Kenny Lau (Jul 31 2020 at 18:45):

where the structure of A-algebra on B is given by f

Kenny Lau (Jul 31 2020 at 18:58):

@Johan Commelin

Reid Barton (Jul 31 2020 at 19:01):

Johan is away for the next two weeks

Kenny Lau (Jul 31 2020 at 19:04):

oh

Kenny Lau (Jul 31 2020 at 19:04):

would you know why linter didn't like my instance?

Reid Barton (Jul 31 2020 at 19:05):

is (ring_hom.to_algebra f) appearing as a [] argument?

Reid Barton (Jul 31 2020 at 19:07):

that might be how the linter decides whether the instance matches on any variables

Reid Barton (Jul 31 2020 at 19:07):

only check () variables

Reid Barton (Jul 31 2020 at 19:08):

I'm just speculating though.

Kenny Lau (Jul 31 2020 at 19:09):

so is that a bug?

Reid Barton (Jul 31 2020 at 19:10):

Probably not, but it doesn't necessarily mean your instance is bad

Sebastien Gouezel (Jul 31 2020 at 19:56):

The linter does not say it is a bad instance, only that the priority should be lowered to 100 because this is an instance that always applies.

Kenny Lau (Jul 31 2020 at 20:22):

@Sebastien Gouezel but it doesn't always apply right

Kenny Lau (Jul 31 2020 at 20:22):

it's specifically for ring_hom.to_algebra

Yury G. Kudryashov (Jul 31 2020 at 20:43):

I think @Gabriel Ebner knows whether this instance is actually bad for lean.

Mario Carneiro (Aug 01 2020 at 00:03):

What are all the stub arguments in the instance?

Mario Carneiro (Aug 01 2020 at 00:06):

Looking at the linter source, it looks like @Reid Barton is correct. Any variables appearing in instance arguments are ignored for the purpose of determining if an instance "always applies"

Mario Carneiro (Aug 01 2020 at 00:09):

all arguments to is_algebra_tower are instance arguments except R A B

Mario Carneiro (Aug 01 2020 at 00:11):

I think it should be fine either way. You could either decrease the priority like it suggests, or @[nolint instance_priority]

Mario Carneiro (Aug 01 2020 at 00:12):

Although it is worth considering what you want to happen if this instance is competing with an instance for e.g. is_algebra_tower A (B x C) D

Yury G. Kudryashov (Aug 01 2020 at 02:51):

The question is whether Lean quickly decides that it can't apply this instance if [algebra] structure on B is not given by ring_hom.to_algebra _.

Yury G. Kudryashov (Aug 01 2020 at 02:51):

If yes, then this is a false positive of the linter.

Kenny Lau (Aug 01 2020 at 05:15):

Mario Carneiro said:

Although it is worth considering what you want to happen if this instance is competing with an instance for e.g. is_algebra_tower A (B x C) D

the point is that it will not, because then f would have to have type B \times C \ra[A] D

Floris van Doorn (Aug 01 2020 at 07:02):

Have you tried using this instance? Does Lean even apply it?

I'm worried that a typical case in which type-class inference for is_algebra_tower has to fire is when Lean knows the first 3 (explicit) arguments and doesn't know the last 6 (instance) arguments, so it needs to find an instance for @is_algebra_tower R A B ?m1 ?m2 ?m3 ?m4 ?m5 ?m6. In that case, this instance will happily unify, by setting ?m5 := ring_hom.to_algebra ?f. However, there is no way that ?f can be figured out by Lean.

I guess if Lean has to figure out something of the form @is_algebra_tower R A B ?m1 ?m2 ?m3 ?m4 (ring_hom.to_algebra f) ?m6, then this instance is useful, but I don't know when this actually happens (Lean infers type-class arguments from right to left, so how did it already know one of the type-class arguments?


Last updated: Dec 20 2023 at 11:08 UTC