Skip to content

type theory edge case: projections and sort polymorphism #7637

Open

Description

@cppio

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

Using set_option bootstrap.inductiveCheckResultingUniverse false allows the creation of inductive types in Sort u that support primitive projections to types in Sort u, yet with a recursor that only allows eliminating into Prop. This means that primitive projections are currently not conservative over recursors.

Context

https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/type.20theory.20edge.20case.3A.20projections.20and.20sort.20polymorphism

Steps to Reproduce

set_option bootstrap.inductiveCheckResultingUniverse false in
set_option genSizeOf false in
set_option genInjectivity false in
inductive Wrap (α : Sort u) : Sort u
  | wrap (unwrap : α)

abbrev Wrap.unwrap {α : Sort u} (self : Wrap α) : α := self.1

/--
info: Wrap.rec.{u} {α : Sort u} {motive : Wrap α → Prop} (wrap : ∀ (unwrap : α), motive (Wrap.wrap unwrap)) (t : Wrap α) :
  motive t
-/
#guard_msgs in
#check Wrap.rec

The recursor for Wrap can only eliminate into Prop since the argument unwrap : α to the constructor is not a Prop. However, primitive projection is allowed since Wrap α : Prop only when α : Prop, and projecting a Prop field is valid. This means that the projection Wrap.unwrap cannot be expressed using the recursor Wrap.rec.

Versions

Lean 4.19.0-nightly-2025-03-22

Additional Information

There seem to be two ways to fix this discrepency between the strength of recursors and projections.
One is to disallow projections for such types, weakening projections to match the existing recursors, and the other is to allow subsingleton elimination for such types, strengthening recursors to match the existing projections.
I have implemented the latter at 0194f05, where the subsingleton elimination check in elim_only_at_universe_zero is modified to check that the fields live in Prop for level assignments that result in the inductive type being in Prop. In the example above, this causes Wrap.rec to support non-Prop motives. This change would also allow structures like PProd and PSigma to avoid having max 1 in their sort level, which could potentially simplify metaprogramming code:

set_option bootstrap.inductiveCheckResultingUniverse false in
structure PProd' (α : Sort u) (β : Sort v) : Sort (max u v) where
  fst : α
  snd : β

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 Mar 22, 2025
  2. kmill commented on Mar 22, 2025

    @kmill
    Collaborator

    Quoting @digama0 on Zulip:

    This is going to make lean4lean run into an unprovable goal, so I'd be interested to see this fixed, and I agree that the simplest fix is to ban primitive projections on such types.

  3. added
    P-low
    on Mar 25, 2025
  4. cppio commented on Aug 11, 2025

    @cppio
    ContributorAuthor

    There are a couple of other potential fixes I missed, so I'll list them all here:

    1. Prevent types with overly conservative recursors (like Wrap) from being accepted. Specifically, the kernel should reject non-predicate inductives that do not support large elimination. This fixes the issue since it prevents the recursor from ever being too weak. The inductive types that currently have overly conservative recursors are, aside from this bug, useless, so nothing is lost from this change.
    2. Make the subsingleton elimination check more liberal to allow types like Wrap to have large elimination. Specifically, the checks for the constructor arguments should be done after substituting the levels that make the resulting type live in Prop. This fixes the issue since it strengthens the recursor in the cases where projection is possible. This is the change I previously suggested and implemented in 0194f05. This change has the added benefit of allowing for more general structures like PProd (α : Sort u) (β : Sort v) : Sort (max u v) and PULift.{r, s} (α : Sort s) : Sort (max s r), which could simplify meta code.
    3. Change the typing rules for projections to check whether the recursor supports large elimination instead of the current check of whether the type is a Prop. This fixes the issue by having, by definition, the projections never be stronger than the recursor. This change also has the effect of enabling projections for Acc, since Acc supports large elimination and the constructor contains the index as a field. Given h : @Acc α r a, we would have h.1 : α and h.2 : ∀ y, r y h.1 → Acc r y. Ideally, h.1 would reduce to a, so that h.2 is equivalent to the current Acc.inv h, but that would require a special reduction rule for projections that avoids reducing the proof term in situations like these.
    4. Change the typing rules for projections to check whether the inductive type can ever be in Prop instead of the current check of whether the current type is a Prop. This is a more conservative version of option 3 that avoids the impact of allowing projections for Acc.

    EDIT: For options 3 and 4, there also needs to be a change in the recursor reduction to prevent eta expansion from occurring.

  5. cppio commented on Aug 13, 2025

    @cppio
    ContributorAuthor

    I have just discovered that nested inductives can be used to create stronger recursors:

    set_option genSizeOf false in
    inductive Unwrap (α : Sort max 1 u) : Sort max 1 u
      | unused (wrap : Wrap ((fun _ => α) Unwrap))
    
    noncomputable
    def Wrap.rec' {α : Sort max 1 u} {motive : Wrap α → Sort v} : (wrap : ∀ unwrap, motive ⟨unwrap⟩) → ∀ t, motive t :=
      @Unwrap.rec_1 α (fun _ => PUnit) motive fun _ _ => ⟨⟩

    This behavior is unrelated to projections:

    set_option bootstrap.inductiveCheckResultingUniverse false in
    set_option genSizeOf false in
    set_option genInjectivity false in
    inductive POption (α : Sort u) : Sort u
      | none
      | some (val : α)
    
    /--
    info: POption.rec.{u} {α : Sort u} {motive : POption α → Prop} (none : motive POption.none)
      (some : ∀ (val : α), motive (POption.some val)) (t : POption α) : motive t
    -/
    #guard_msgs in
    #check POption.rec
    
    set_option genSizeOf false in
    inductive Unwrap (α : Sort max 1 u) : Sort max 1 u
      | unused (wrap : POption ((fun _ => α) Unwrap))
    
    noncomputable
    def POption.rec' {α : Sort max 1 u} {motive : POption α → Sort v} : (none : motive none) → (some : ∀ val, motive (some val)) → ∀ t, motive t :=
      @Unwrap.rec_1 α (fun _ => PUnit) motive fun _ _ => ⟨⟩

    I believe these recursors are still sound, but I'm guessing this behavior is unintentional. Option 1 from my previous list of fixes also fixes this issue.

  6. eric-wieser commented on Jun 3, 2026

    @eric-wieser
    Contributor

    There is a related discussion at #lean4 > PSigma and universe levels @ 💬

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