Skip to content

induction_eliminator and cases_eliminator only key on the head symbol #4577

Open

Description

@eric-wieser

Prerequisites

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

Description

The @[induction_eliminator] and @[cases_eliminator] attribute cannot be used to register eliminators for two different types with the same head symbol.

Context

This is necessary to register eliminators for quotients of additive and multiplicative groups in Mathlib, where both have head symbol HasQuotient.quotient (leanprover-community/mathlib3#12605)

Steps to Reproduce

  1. Run the following:
inductive Foo : Bool → Type
  | ofTrue (x : Nat) : Foo true
  | ofFalse (x : Nat) : Foo false

-- insert an `id` term to make it easy to check these are actually being used.

@[elab_as_elim]
theorem Foo.true_rec {P : Foo true → Prop} (h : ∀ x, P (id <| .ofTrue x))
    (x : Foo true) : P x := by
  cases x; apply h

@[elab_as_elim]
theorem Foo.false_rec {P : Foo false → Prop} (h : ∀ x, P (id <| .ofFalse x))
    (x : Foo false) : P x := by
  cases x; apply h

example (x : Foo true) : x = x := by
  cases x using Foo.true_rec -- works
  sorry

example (x : Foo false) : x = x := by
  cases x using Foo.false_rec -- works
  sorry

attribute [cases_eliminator] Foo.true_rec Foo.false_rec

example (x : Foo true) : x = x := by
  cases x -- fails, tried to use `Foo.false_rec`
  sorry

example (x : Foo false) : x = x := by
  cases x --works
  sorry

Expected behavior: Both false_rec and true_rec can be registered, and the correct eliminator is chosen based on the full type of x

Actual behavior: The last-registered eliminator wins, causes the penultimate example to fail as the eliminator does not actually match the type of the major premise.

Versions

4.9.0-rc3

Additional Information

[Additional information, configuration or data that might be necessary to reproduce the issue]

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 Jun 27, 2024
  2. added
    P-medium
    on Aug 9, 2024
  3. robertmaxton42 commented on Oct 9, 2025

    @robertmaxton42
    Contributor

    Here's another manifestation of the same issue:

    inductive Zigzag where | even (n : Nat) | odd (n : Nat)
    
    namespace Zigzag
    
    instance : LE Zigzag where
      le 
      | even m, even n | even m, odd n | odd m, odd n => m ≤ n
      | odd m, even n => m < n
    
    theorem le_def (x y : Zigzag) : (x ≤ y) = 
      match x, y with
      | even m, even n | even m, odd n | odd m, odd n => m ≤ n
      | odd m, even n => m < n := rfl
    
    
    @[elab_as_elim, cases_eliminator]
    theorem le_induction {motive : {x y : Zigzag} → x ≤ y → Prop}
        (even_even : ∀ {m n} (h : m ≤ n), motive (le_def (even m) (even n) |>.mpr h))
        (odd_odd : ∀ {m n} (h : m ≤ n), motive (le_def (odd m) (odd n) |>.mpr h))
        (even_odd : ∀ {m n} (h : m ≤ n), motive (le_def (even m) (odd n) |>.mpr h))
        (odd_even : ∀ {m n} (h : m < n), motive (le_def (odd m) (even n) |>.mpr h)) :
        ∀ {x y} (h : x ≤ y), motive h
      | even _, even _, h => even_even h
      | odd _, odd _, h => odd_odd h
      | even _, odd _, h => even_odd h
      | odd _, even _, h => odd_even h
    
    -- Positive example: `cases` should invoke `le_induction` automatically on hypotheses of type `@LE Zigzag _ _`
    example {x y : Zigzag} (h : x ≤ y) : True := by
      cases h <;> trivial -- works
    
    -- Negative example: applying `cases_eliminator` should *not* prevent `cases` from being used on hypotheses of
    -- type `@LE α _ _` for any other `α`
    example {x y : Nat} (h : x ≤ y) : True := by
      cases h <;> trivial
    /- Invalid target:
      h
    has type
      x ≤ y
    but is expected to have type
      ?x ≤ ?y -/
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-mediumbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions