Zulip Chat Archive

Stream: mathlib4

Topic: Deprecation and time out


Antoine Chambert-Loir (Oct 04 2024 at 09:51):

The recent PR #14168 (June 26, 2024) deprecated a number of AlgHom.map_* lemmas on the basis that they are redundant, and asks to replace them with the standard map_* lemma. Unfortunately, this leads to time out problems, in mathlib (as explained in the documentation of the PR), and in the project with @María Inés de Frutos Fernández .

Antoine Chambert-Loir (Oct 04 2024 at 09:52):

Here is a mwe:

import Mathlib.RingTheory.TensorProduct.Basic

open TensorProduct

variable {R A : Type*} [CommRing R] [CommRing A]

lemma foo [Algebra A R] (S : Type*) [CommRing S] [Algebra A S] {S₀ : Subalgebra A S}
    {T₀ : Subalgebra A R} (x y : T₀ [A] S₀) :
    (Algebra.TensorProduct.map T₀.val S₀.val) x + (Algebra.TensorProduct.map T₀.val S₀.val) y =
    (Algebra.TensorProduct.map T₀.val S₀.val) (x + y) := by
  rw [AlgHom.map_add] -- But `rw [map_add]` times out.

María Inés de Frutos Fernández (Oct 04 2024 at 09:56):

The proof exact Eq.symm (map_add (Algebra.TensorProduct.map T₀.val S₀.val) x y) also works in my laptop, but it is slow.
(In the online editor it also times out).

Riccardo Brasca (Oct 04 2024 at 10:10):

I also noticed the same thing.

Ruben Van de Velde (Oct 04 2024 at 10:15):

Ugh. Does it still work with rw if you explicitly pass the hom?

María Inés de Frutos Fernández (Oct 04 2024 at 10:16):

No, it times out as well.

Kevin Buzzard (Oct 04 2024 at 20:43):

  [synthInstance] [1.237288] ✅️ NonUnitalNonAssocSemiring ({ x // x  T₀ } [A] { x // x  S₀ }) 

is at least part of the problem.

Kevin Buzzard (Oct 04 2024 at 20:47):

Random hacks like attribute [-instance] Subalgebra.smulCommClass_right in fix the timeout (this is just looking at things in the instance trace which are making things go the long way around)

Johan Commelin (Oct 07 2024 at 14:57):

We should try to mwe this further. @Antoine Chambert-Loir I have recorded your MWE as the following issue: 

map_add times out, and AlgHom.map_* is deprecated #17507

Antoine Chambert-Loir (Oct 07 2024 at 19:50):

Thanks a lot. But I don't know what I can do for it.
(Actually, it would be useful, and even reasonable, that “advanced” users of Lean start learning seriously this kind of stuff, but for the moment I feel lost there.)

Kevin Buzzard (Oct 07 2024 at 20:42):

My experience with trying to minimise these things is that the actual problem might be that mathlib is big, so when you try to minimise, things then get much quicker.

Antoine Chambert-Loir (Oct 07 2024 at 21:12):

What I mean is that I have no clue about the internal process ran by the compiler. I only have an abstract understanding (if at all) of all the terms that are uttered here and there — unification, typeclass inference, etc. And the game of setting some option (but which one, how can one remember?) to try to find out remains a mysterious one.

Johan Commelin (Oct 08 2024 at 04:25):

@Antoine Chambert-Loir I didn't mean to oblige you into more work. Actually, there's ongoing work to help automate further minimization of such problems. So we should wait a bit, but maybe this isn't a skill that people should invest in for the future.

Kevin Buzzard (Oct 08 2024 at 06:37):

I commented on the GitHub issue by the way. I think it's pretty clear what the problem is, but the only solution I can think of is a massive refactor of mathlib changing the way the order hierarchy interacts with the algebra hierarchy.

I'll repeat my thoughts here: the issue is that whenever faced with a goal of the form "↥X acts on Y" where X is a subthing (eg a subalgebra) of R, but coerced to a type, then lean can apply a fact from typeclass inference saying "it suffices to prove that R acts on Y". This fact turns true statements into false ones in these problematic cases (R is too big to act on Y) and then typeclass inference attempts to use the order hierarchy to find the action and takes a long time to fail because it's so big.

Kevin Buzzard (Oct 08 2024 at 06:39):

The trace makes dismal reading because any attempt to solve this unsolvable goal using order theory is necessarily completely fruitless because there is no order in the question. The easiest fix would just be to not import any order stuff but this is not feasible in practice

Kevin Buzzard (Oct 08 2024 at 07:34):

Possibly related comment here.

Michael Stoll (Oct 08 2024 at 14:08):

There is the approach I tried in #12778, but @Yaël Dillies vetoed it on the grounds that it is not the right way of doing things. Alas, nothing seems to have come of the plans to do it the right way:tm: ...

Daniel Weber (Oct 08 2024 at 14:28):

Kevin Buzzard said:

[...]

I'll repeat my thoughts here: the issue is that whenever faced with a goal of the form "↥X acts on Y" where X is a subthing (eg a subalgebra) of R, but coerced to a type, then lean can apply a fact from typeclass inference saying "it suffices to prove that R acts on Y". [...]

Would it perhaps be possible to give that instance low priority (or the other instance high priority), or would that cause more harm than good?

Kevin Buzzard (Oct 08 2024 at 19:18):

This is pretty much what Michael did in #12778 and the results looked really good!

Kevin Buzzard (Oct 08 2024 at 19:22):

annoyingly, I can't remember how to change the priority of an instance to low. The troublesome instances here are docs#Subalgebra.smulCommClass_right, docs#IsScalarTower.subalgebra' and docs#Subalgebra.isScalarTower_mid

I bet that decreasing the prio of these instances will make the example above work fine.

Antoine Chambert-Loir (Oct 08 2024 at 19:29):

Could we remove these instances in a given file ?

Michael Stoll (Oct 08 2024 at 19:30):

Maybe something like attribute [local instance 50] ...?

Michael Stoll (Oct 08 2024 at 19:32):

import Mathlib.RingTheory.TensorProduct.Basic

open TensorProduct

attribute [local instance 50] Subalgebra.smulCommClass_right
  IsScalarTower.subalgebra' Subalgebra.isScalarTower_mid

variable {R A : Type*} [CommRing R] [CommRing A]

count_heartbeats in -- 32416
lemma foo [Algebra A R] (S : Type*) [CommRing S] [Algebra A S] {S₀ : Subalgebra A S}
    {T₀ : Subalgebra A R} (x y : T₀ [A] S₀) :
    (Algebra.TensorProduct.map T₀.val S₀.val) x + (Algebra.TensorProduct.map T₀.val S₀.val) y =
    (Algebra.TensorProduct.map T₀.val S₀.val) (x + y) := by
  rw [map_add]

Michael Stoll (Oct 08 2024 at 19:34):

The tipping point is prio 999 vs 1000.

Kevin Buzzard (Oct 08 2024 at 19:45):

I always imagine that prio should be involved (like when you're declaring the instance) and then my search fails me.

Kevin Buzzard (Oct 08 2024 at 19:46):

@Yaël Dillies do you have an objection to globally lowering the prio of these instances?

Yaël Dillies (Oct 08 2024 at 19:51):

I see the results of the benchmark, but tweaking priorities only makes working TC searches faster. Failing ones will still search the full tree

Kevin Buzzard (Oct 08 2024 at 19:52):

Right, but this is making less of them fail!

Yaël Dillies (Oct 08 2024 at 19:52):

I think we can tweak the priorities, but the better thing would be to fully cut off the hierarchies

Yaël Dillies (Oct 08 2024 at 19:54):

And I've been working recently on making less lemmas rely on bundled algebra+order classes. I believe that's a requirement to a greater overhaul.

Yaël Dillies (Oct 08 2024 at 19:54):

Anyway, you can tweak the priorities in the meantime

Yuyang Zhao (Oct 08 2024 at 22:35):

Kevin Buzzard said:

Possibly related comment here.

It's even worse there, I'll make a mwe.

Michael Stoll (Oct 09 2024 at 07:24):

I've tried to update #12778. It passes CI, but benching it gives an error.

Yuyang Zhao (Oct 09 2024 at 07:45):

The speedcenter is still broken.

Michael Stoll (Oct 13 2024 at 08:35):

After merging master again, the speed center works agaon on #12778, and it shows a gain of 9.7% in typeclass inference. There are some files that get slower, but this should be fixable with a bit more tinkering...

Yaël Dillies (Oct 13 2024 at 08:36):

It's funny how the files that got slower are mostly mine :thinking: Maybe because I mostly do algebraic order stuff?

Michael Stoll (Oct 13 2024 at 14:46):

I think there has been some refactoring, so probably there are some files or declarations that now need the open scoped AlgebraOrderInstances, but didn't before, and there are most likely some new files, too. If I have time, I'll look at that (but you @Yaël Dillies or anybody else is also welcome to do so).

Yuyang Zhao (Nov 07 2024 at 20:33):

I've recently come back to the FunLike hierarchy slowness. (Before adjusting the priorities) #17675 has a 10x speedup for some other examples, but not for the examples in this topic. I realized we have docs#RingHomClass.toLinearMapClassNNRat and docs#RingHomClass.toLinearMapClassRat, but the scalar type parameter of docs#LinearMapClass is outParam. This may have caused some failed attempts and slow unification.

I'm not sure it's the critical issue of performance, but this looks wrong...

Yuyang Zhao (Nov 10 2024 at 03:27):

If we want these two instances, should we refactor LinearMapClass? cc @Yaël Dillies who added them.

Yury G. Kudryashov (Nov 10 2024 at 05:14):

Why do we want these instances, esp. with RingHomClass assumptions?

Yury G. Kudryashov (Nov 10 2024 at 05:15):

docs#map_rat_smul and docs#map_nnrat_smul have much weaker assumptions

Yaël Dillies (Nov 10 2024 at 09:30):

Hmm, I don't remember why I needed those instances off the top of my head. I would have to dig through APAP

Yaël Dillies (Nov 10 2024 at 09:31):

Yury G. Kudryashov said:

docs#map_rat_smul and docs#map_nnrat_smul have much weaker assumptions

Yes, but they don't return a linear map, which makes them somewhat useless in linear map-full applications

Yaël Dillies (Nov 10 2024 at 09:31):

Something that will definitely work for now is to make them non-instances

Yury G. Kudryashov (Nov 10 2024 at 15:06):

You can use docs#AddMonoidHom.toRatLinearMap

Yury G. Kudryashov (Nov 10 2024 at 15:06):

(which can be generalized to AddMonoidHomClass)


Last updated: May 02 2025 at 03:31 UTC