Skip to content

Universe normalization is too late to prevent unfolding in defeq check #12102

Open

Description

@kbuzzard

This code

import Lean
universe u v
structure X : Type u where x : Nat
opaque a (_ : Nat) : X.{u} := ⟨0⟩
opaque b (_ _ : X.{u}) : X.{u} := ⟨0⟩
elab "big%" : term =>
  let rec foo : Nat → Nat → Lean.Expr
    | 0, i => Lean.mkApp (Lean.mkConst ``a [.param `u]) (Lean.mkNatLit i)
    | n+1, i => Lean.mkApp2 (Lean.mkConst ``b [.param `u]) (foo n (2*i)) (foo n (2*i+1))
  pure (foo 12 0)
structure A where
opaque foo (_ : X.{u}) : A := ⟨⟩
structure B extends A
set_option maxRecDepth 100000 in
def bar : B := ⟨foo big%⟩

set_option trace.profiler.useHeartbeats true
set_option trace.profiler true

example : B.toA bar.{max u v} = B.toA bar.{max u v} := rfl
example : B.toA bar.{max v u} = B.toA bar.{max u v} := rfl

demonstrates the following phenomenon: when attempting to check definitional equality of two terms which are syntactically identical other than having different but defeq universes (in this case max u v and max v u), the defeq check unfolds the terms. The code constructs a large term so that unfolding is expensive. Profiling results for the last two lines: with syntactically equal universes we have

[Elab.command] [5884.000000] example : B.toA bar.{max u v} = B.toA bar.{max u v} :=
      rfl ▶
[Elab.async] [150.000000] Lean.addDecl ▼
  [Kernel] [97.000000] ✅️ typechecking declarations [_example] 

and with definitionally equal universes we have

[Elab.command] [3677942.000000] example : B.toA bar.{max v u} = B.toA bar.{max u v} :=
      rfl ▶
[Elab.async] [114954.000000] Lean.addDecl ▼
  [Kernel] [114901.000000] ✅️ typechecking declarations [_example] 

indicating that both elaboration and typechecking are affected by this. Potential fixes (which of course may have unwanted side-effects) would be to simply not allow both max u v and max v u to exist, or to normalise universes before structure eta defeq checks. Mathlib uses multiple universes in much of algebra (and for good reason) and also in category theory (also for good reason), but I am unclear about how common it is to see more than one universe in other parts of the Lean ecosystem.

This comes up in practice in Mathlib, when attempting to do commutative algebra; there was a discussion on the Lean Zulip #lean4 > kernel universe normalization issue @ 💬 in Jan 2025, where one solution suggested by the devs was "don't make such big terms" (which is certainly a valid observation). At that time there was no mathlib-free minimisation and an issue was opened against Mathlib leanprover-community/mathlib4#26018 . The conversation was reignited by me in Jan 2026 when I was attempting to debug a declaration which was taking forever to typecheck (again in commutative algebra) and traced it back to this issue; this time we have the mathlib-free minimisation above.

Right now the practical consequences of this are frustrating. There are several independent and totally hacky ways in which an end user can affect Lean's choice of the order of universes in max (and thus affect typechecking speed):

Example 1: alphabetical order of universe names. The Mathlib PR leanprover-community/mathlib4#34088 saves 90G instructions in one declaration by changing the name of a universe from u (before u_1 alphabetically) to v (after u_1 alphabetically).

Example 2: unification. The Mathlib PR leanprover-community/mathlib4#34254 fills in a hole which is trivially fillable by unification, and for reasons unknown to me this causes a universe variable in the middle of a term to change from max u_1 u to max u u_1 and all of a sudden it typechecks 7x quicker.

Example 3: changing when a universe name is declared. There is an example in a mathlib file where changing def KaehlerDifferential.mvPolynomialBasis (σ) : ... to def KaehlerDifferential.mvPolynomialBasis (σ : Type u_1) : ... results in a large change in unification time, even though in the first example σ is ultimately given the universe Type u_1. This seems to be because a universe ordering algorithm kicks in at a point where the name of the universe has not yet been assigned, and produces a different answer to when it is explicitly assigned.

These mathlib PRs are widely regarded as hacks (even by me, and I made them), however they are having a non-negligible effect on the typechecking time of various terms in what is a slow corner of mathlib: I conjecture that the phenomenon outlined in this issue is a primary cause of this slowness.

I feel that it would be a very interesting experiment to make a branch of core where universes are normalised more aggressively, see if the MWE above then compiles quickly, and if so then to profile mathlib with these changes. However this kind of experiment is beyond me.

Activity

  1. changed the title [-]Universe normalization is too late to prevent unfolding[/-] [+]Universe normalization is too late to prevent unfolding in defeq check[/+] on Jan 22, 2026
  2. added a commit that references this issue on Jan 27, 2026
    c0d1025
  3. kim-em commented on Jan 27, 2026

    @kim-em
    Collaborator

    Candidate fix: #12175

    The issue is that the existing optimization in lazy_delta_reduction_step (which checks if two terms are the same definition with defeq universes before unfolding) only triggered for applications (is_app(t_n) && is_app(s_n)), not for plain constants.

    When comparing bar.{max u v} with bar.{max v u}, both are plain constants (not applications), so the optimization was bypassed and the kernel unfolded bar before discovering the universes are definitionally equal.

    The fix extends the optimization to also handle plain constants.

  4. kim-em commented on Jan 27, 2026

    @kim-em
    Collaborator

    Unfortunately, while #12175 does help with Mario's example above, it doesn't help in Kahler/Polynomial.lean (i.e. the slow elaboration that the hacky fix leanprover-community/mathlib4#34088 is trying to address) or in JacobiZariski.lean with leanprover-community/mathlib4#26008 reverted.

    So --- we need new minimizations!

  5. kim-em commented on Jan 27, 2026

    @kim-em
    Collaborator

    #12175 helps quite a bit in the kernel:

    Metric Master PR #12175 PR vs Master
    max u v elab 5,570 5,775 +4%
    max v u elab 3,677,625 3,710,609 +0.9%
    max u v kernel 97 97 0%
    max v u kernel 114,901 129 -99.9%
  6. kim-em commented on Jan 27, 2026

    @kim-em
    Collaborator
    Metric Master #12175 #12186
    max u v elab 5,570 5,775 6,015
    max v u elab 3,677,625 3,710,609 7,373
    max u v kernel 97 97 98
    max v u kernel 114,901 129 114,902
  7. added
    P-medium
    on Feb 4, 2026
  8. added
    P-high
    and removed
    P-medium
    on Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions