Skip to content

Dot notation resolution has counterintuitive behavior with diamond multiple inheritance  #3467

Closed

Description

@Shamrock-Frost

Description

Consider the following two snippets of code:

structure A where
  countA : Nat

structure B where
  countB : Nat

namespace A

def getTwiceCountA (a : A) := a.countA * 2

end A

namespace B

def getTwiceCountB (b : B) := b.countB * 2

end B

structure C extends A, B

def getCounts (c : C) :=
  c.countA + c.countB

def getTwiceCounts (c : C) :=
  c.getTwiceCountA + c.getTwiceCountB

and

structure X where
  base : Nat

structure A extends X where
  countA : Nat

structure B extends X where
  countB : Nat

namespace A

def getTwiceCountA (a : A) := a.countA * 2

end A

namespace B

def getTwiceCountB (b : B) := b.countB * 2

end B

structure C extends A, B

def getCounts (c : C) :=
  c.countA + c.countB

def getTwiceCounts (c : C) :=
  c.getTwiceCountA + c.getTwiceCountB

The first snippet works as expected, while the second fails with a red underline under c.getTwiceCountB and the error message

invalid field 'getTwiceCountB', the environment does not contain 'C.getTwiceCountB'
  c
has type
  C

This was confusing to me as an end user, and I would prefer if the second example worked.

Context

I was performing an experimental refactor of mathlib's MonoidalFunctor typeclass so that it inherits from both LaxMonoidalFunctor and (a new class) ColaxMonoidalFunctor (both extend from the Functor class in the category theory library). When writing a method for this new definition of MonoidalFunctor I got an error when trying to use dot notation for something in the ColaxMonoidalFunctor namespace on a term of type MonoidalFunctor. I posted asking about this on zulip. I believe this behavior is a result of how Lean.getParentStructures is implemented. In a case of diamond shaped inheritance like the A, B, C, X example, the structure C has a field toA but C.toB is a derived method instead of a field itself, and so Lean.getParentStructures won't see any fields where isSubobjectField? is some which correspond to B.

Steps to Reproduce

  1. Make a diamond shaped inheritance chain and try to access via dot-notation a non-field member of the namespace of an intermediate structure (other than the first structure after extends).

Expected behavior: The name resolves to the member of the super-structure namespace.

Actual behavior: It does not.

Versions

Lean: 4.6.0-rc1
OS: Windows 11

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 Feb 22, 2024
  2. added
    P-medium
    on Sep 13, 2024
  3. added a commit that references this issue on Oct 31, 2024
    465ed8a
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