leanprover / leanprover/lean4

"omit" doesn't omit typeclass assumptions

Open
#5,595 2 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal AddComm example against the reported Lean nightly version and compare the expected and actual elaboration. Investigate the behavior of omit and the elaborator's handling of inherited typeclass instances; done means the example uses only the AddComm R instance, or the issue's proposed alternative is clearly resolved.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.