Zulip Chat Archive

Stream: new members

Topic: import Mathlib leads to Lean not terminating


Sven Manthe (Sep 21 2023 at 08:24):

I used a Lean file with "import Mathlib", and repeatedly different non-well-typed statements led to Lean using 100% CPU and seemingly not terminating (for at least some minutes). Moreover, removing the relevant line did not stop this Lean process, and adding it back in increased the CPU usage by another 100%. The only way to stop it seemed to be to kill the Lean process externally.
Replacing the import Mathlib by importing only the parts I needed resolved the issue. I could reproduce this behavior both on Manjaro and Linux Mint. @Floris van Doorn suggested I report it here. Here is a minimal working example (the last line causing the problems), but I also have different examples:

import Mathlib

@[ext]
structure ATree (A: Type) where
  carrier: Set (List A)
  isTree: (x: List A)(a: A), x ++ [a]  carrier  x  carrier

instance : CoeFun (ATree A) (Set (List A)) where coe T := T.carrier

Adomas Baliuka (Sep 21 2023 at 08:39):

In my very brief experience of using import Mathlib, very long compilation times were normal but settled down eventually (as long as I didn't modify anything within Mathlib itself and only used it in my own code)

Kevin Buzzard (Sep 21 2023 at 08:40):

import Mathlib is not the problem, that compiles super-quickly in Lean 4 (assuming you've got your project set up correctly with a fully-compiled mathlib). I need to add variable (A : Type) to make this a full MWE but Lean hangs on the instance for me.

Kevin Buzzard (Sep 21 2023 at 08:42):

With just import Mathlib.Data.Set.Basic I get the expected error

application type mismatch
  CoeFun (ATree A) (Set (List A))
argument
  Set (List A)
has type
  Type : Type 1
but is expected to have type
  outParam (ATree A → Sort ?u.427) : Type ?u.427

Eric Wieser (Sep 21 2023 at 08:47):

docs#CoeFun doesn't make any sense here

Eric Wieser (Sep 21 2023 at 08:50):

I think there must be a bad coercion instance somewhere

Kevin Buzzard (Sep 21 2023 at 08:52):

import Mathlib.Order.Heyting.Regular

is the bad import.

Eric Wieser (Sep 21 2023 at 08:52):

How did you find that so quickly?

Kevin Buzzard (Sep 21 2023 at 08:52):

blind luck

Kevin Buzzard (Sep 21 2023 at 08:54):

I copied the entire Mathlib.lean as imports to the file and then bisected. The big problem with this approach is that when you finally get it down to one import, that import file is not the problem because it imports 5 other files which import 25 other files etc etc, so the search has only just begun. So I bisected down to that import, and then replaced it with its only import and the problem went away and I was like "wooah, it must be my birthday or something"

Eric Wieser (Sep 21 2023 at 08:54):

docs#Heyting.Regular.instCoeRegular is the bad instance

Eric Wieser (Sep 21 2023 at 08:55):

In that attribute [-instance] Heyting.Regular.instCoeRegular makes the problem go away

Eric Wieser (Sep 21 2023 at 08:55):

The instance looks harmless though

Kevin Buzzard (Sep 21 2023 at 08:57):

Kevin Buzzard said:

blind luck

Oh, the correct answer is actually "problem happens to be in a leaf file" (which might also explain why it wasn't spotted before)

Floris van Doorn (Sep 21 2023 at 12:20):

Eric Wieser said:

The instance looks harmless though

Shouldn't it be marked as CoeHead or CoeTail or something? It clearly is not harmless...

Mario Carneiro (Sep 21 2023 at 12:27):

yes, it should be a CoeOut

Floris van Doorn (Sep 21 2023 at 12:31):

Even without that instance, it takes 3+ seconds to fail, which is kinda unfortunate. It's quite sad that all coercions take 1-2 seconds to fail, even if they are clearly nonsense, like between Set (List A) =?= sorryAx (ATree A → Sort ?u.535) true T

Floris van Doorn (Sep 21 2023 at 12:33):

The failure takes almost a thousand steps, each costing 0-30ms.

Kevin Buzzard (Sep 21 2023 at 12:34):

Floris you're old enough to remember when attempting to coerce from Int to Nat in Lean 3 would time out; hopefully now, like then, we'll be able to solve these problems.

Floris van Doorn (Sep 21 2023 at 12:38):

#7293

Floris van Doorn (Sep 21 2023 at 12:40):

Kevin Buzzard said:

Floris you're old enough to remember when attempting to coerce from Int to Nat in Lean 3 would time out; hopefully now, like then, we'll be able to solve these problems.

I hope so. This is causing quite a lot of issues.

Floris van Doorn (Sep 21 2023 at 12:41):

And of course we should write Lean 4 linters guarding against bad coercions like the one above.

Eric Wieser (Sep 21 2023 at 14:10):

Is the problem that Lean is trying to coerce to Set (List A), so it tries to find Regular (Set (List A)), which it thinks it can get from Regular (Regular (Set (List A)))...?

Floris van Doorn (Sep 21 2023 at 19:33):

I am quite sure that was exactly what was happening.

Floris van Doorn (Sep 21 2023 at 19:34):

We had linters in Lean 3 against such coercions (although I forgot which of Foo A -> A or A -> Bar A was problematic).

Anne Baanen (Sep 22 2023 at 07:56):

Kevin Buzzard said:

I copied the entire Mathlib.lean as imports to the file and then bisected. The big problem with this approach is that when you finally get it down to one import, that import file is not the problem because it imports 5 other files which import 25 other files etc etc, so the search has only just begun.

Perhaps we should have (a script that generates) a version of Mathlib.lean with the imports topologically sorted. So that A.lean comes after B.lean if A imports B. Might be a useful first step towards a #mwe-ifier.


Last updated: Dec 20 2023 at 11:08 UTC