Skip to content

"omit" doesn't omit typeclass assumptions #5595

Open

Description

@loefflerd

Prerequisites

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

Description

Once a typeclass variable has been declared, it's impossible to get rid of it. The omit keyword doesn't actually omit it from following declarations; it only cancels the effect of include so it isn't always included, but it is still "optionally" included.

This makes it very difficult to "refine" a typeclass variable from a general typeclass to a more specific one that extends it. Instead one ends up with multiple, unrelated instances of the base class on the same type, which can lead to craziness (of a sort that is extremely confusing for novices in particular).

Context

This is a de-mathlibized version of an issue that arose in a discussion about typeclass variables in mathlib on zulip here: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Multiple.20overlapping.20typeclasses

Steps to Reproduce

MWE:

class AddComm (R : Type) extends Add R where
  add_comm : ∀ a b : R, a + b = b + a

variable (R : Type) [Add R]

omit [Add R] in
variable [AddComm R] in

example (a b : R) : a + b = b + a := by
  exact AddComm.add_comm a b -- fails, wrong "+"

Expected behavior:

It would be nice if the omit declaration actually omitted the Add R typeclass from the example, so the statement was elaborated with only the AddComm R instance in scope. (Alternatively, rather than changing omit we could have a new keyword omit! or forget.)

Actual behavior:

The elaborator has two instances of Add on R to choose from – the one from the explicitly declared Add R variable, and the one that's the base class of the AddComm R variable – and the one it chooses is the one we tried to omit. Hence we get a type mismatch error

type mismatch
  AddComm.add_comm a b
has type
  @HAdd.hAdd R R R (@instHAdd R AddComm.toAdd) a b = b + a : Prop
but is expected to have type
  @HAdd.hAdd R R R (@instHAdd R inst✝¹) a b = b + a : Prop

where inst✝¹ is the Add R instance.

Versions

Tested with live.lean-lang.org, version "4.12.0-nightly-2024-10-01"

Activity

  1. added
    bug
    on Oct 2, 2024
  2. added
    P-medium
    on Oct 25, 2024
  3. fpvandoorn commented on Oct 23, 2025

    @fpvandoorn
    Contributor

    Note: with theorem the error message is different (but the issue is the same).

    class AddComm (R : Type) extends Add R where
      add_comm : ∀ a b : R, a + b = b + a
    
    variable (R : Type) [Add R]
    
    omit [Add R] in
    variable [AddComm R] in
    
    -- error: cannot omit referenced section variable `inst✝¹`
    theorem foo (a b : R) : a + b = b + a := by 
      exact AddComm.add_comm a b
  4. wwylele commented on Apr 22, 2026

    @wwylele

    I'd like to contribute an example that I believe is caused by the same issue, but is more hidden as it doesn't immediately show any error/warning.

    class A (T : Type) : Prop where
    class B (T : Type) : Prop where
    class C (T : Type) : Prop where
    
    instance (priority := 100) A_of_B (T : Type) [B T] : A T where
    instance (priority := 50) A_of_C (T : Type) [C T] : A T where
    
    structure S (T : Type) where
      stuff : Nat
      h : A T
    
    variable (T : Type) [B T]
    
    omit [B T] in def foo [C T] : S T where
      stuff := 42
      h := inferInstance
    
    #check foo -- shows `foo (T : Type) [B T] [C T] : S T`, which unexpectedly includes both [B T] and [C T]
    

    (Tested on v4.29.0)


    Edit: actually #9597 has an almost identical example

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