Repository navigation
Description
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.getTwiceCountBand
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.getTwiceCountBThe 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
- 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
feat: resolve generalized field notation using all parents (#5770)