Skip to content

Universe level normalization has associativity of max backwards from parsing associativity #5695

Open

Description

@kmill

Description

Given universe level max u v w, it gets normalized as max (max u v) w.

Steps to Reproduce

In the following, the structure command derives the type automatically and normalizes the level.

structure Foo.{u,v,w} (α : Type (max u v w)) where
  val : α

#check Foo
/-
Foo.{u, v, w} (α : Type (max u v w)) : Type (max (max u v) w)
-/

Expected behavior: The universe level should be max u (max v w) (i.e., max u v w)

Actual behavior: The universe level is max (max u v) w

Versions

4.12.0 1d8555f

Additional Information

One way or the other, max u v w normalized should pretty print as max u v w.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Activity

  1. added
    bug
    on Oct 14, 2024
  2. added
    P-low
    on Oct 18, 2024
  3. kmill commented on Nov 19, 2024

    @kmill
    CollaboratorAuthor

    Related, Mario noted in #2689 (before I changed the PR description):

    PS: Level.normalize also differs from level::normalize: the lean function left associates max expressions and the C++ function right associates. I suspect this could also lead to different behaviors in edge cases, although I don't yet have an example. Is it worth making them agree here?

  4. nomeata commented on Jan 28, 2025

    @nomeata
    Collaborator

    Still low priority, but maybe changing the elaborator to associate the other way is a simple fix that will make users happy here. If someone feels like giving it a shot to see if it goes through without notable breakage, it’ll likely get merged.

  5. digama0 commented on Jan 28, 2025

    @digama0
    Collaborator

    Alternatively you could change the pretty printer to completely ignore max associativity and just flatten things regardless of whether it is left or right associated. Lean is very good at not making the association matter in practice so this seems like a good way to just avoid it being something to call attention to.

  6. sgouezel commented on Jan 28, 2025

    @sgouezel
    Contributor

    It looks like the association matters a lot for kernel typechecking performance (see the Zulip thread, e.g. https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/JacobiZariski.20is.20slow.2E/near/496348891), so hiding that under the rug just by fixing the pretty-printing is probably not a good idea!

  7. sgouezel commented on Jan 28, 2025

    @sgouezel
    Contributor

    (And given the performance issue, I'd raise the priority, also :-)

  8. kbuzzard commented on Jan 30, 2025

    @kbuzzard
    Contributor

    Yes, it might (or might not) be the case that the phenomenon being discussed in this issue is behind timeouts in algebraic geometry in mathlib (which we'll need for FLT, but not urgently). For example the hacky mathlib PR leanprover-community/mathlib4#21129 changes the names of universes so that they are in a different alphabetical order, in order to resolve timeouts and get a big speedup on a file. Example: if some universes are called a,b,c then a file (involving other universes u,v) compiles, but if a,b,c are renamed to x,y,z then the file does not compile because of timeouts, see here for instance. Universe normalization depends on alphabetical order, and changing alphabetical order enables the end user to manipulate the normalization algorithm; this has become important in algebraic geometry formalization because we need to work around the phenomenon explained here in #lean4 on Zulip where the kernel takes a very long time with a rfl which the elaborator was fine with. The conjecture is that the kernel only found itself in this situation in the first place (in the actual algebraic geometry file) because of this issue. NB I don't think this is to do with algebraic geometry per se, I think it might just be to do with situations where there are many different universe variables in complex terms.

    I don't know whether the solution to this issue is as simple as "fix the line mentioned here".

    It would be possible to make a mathlib-free minimisation of the example in the Zulip chat but the naive approach would be a few hours' work and the resulting minimisation would be of the form "copy out a huge chunk of the algebra hierarchy and then do something which is two lines long with it".

  9. removed
    P-low
    on Jan 30, 2025
  10. added
    P-low
    on Feb 11, 2025
  11. nomeata commented on Apr 19, 2025

    @nomeata
    Collaborator

    Not wanting to necessarily stir up a new discussion of a P-low issue, but still worth recording this here:

    There is the extra complication that imax isn’t even associative:

    imax (imax 2 0) 1 = imax 0 1 = 1 ≠ 2 = imax 2 1 = imax 2 (imax 0 1)
    

    and at least as long we have the syntax imax a b c it really has to mean imax a (imax b c), the other associativity would be very confusing.

    So for imax the parsing and pretty-printing associativity is necessarily right-associative.

    Then it seems reasonable for max to use the same. This could be a stronger reason than consistency with the normalization procedure.

    So maybe there is no bug here, but just a unavoidable consequence of other reasonable decisions.

  12. nomeata commented on Jun 25, 2025

    @nomeata
    Collaborator

    Stumbled over this again. Did anyone ever try to make the Lean normalize function right-associate the max’es, to match the kernel’s and the parsing associativity?

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-lowbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions