Inconsistent name resolution with generalized field notation in opened namespaces
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 9.2k
- Forks
- 990
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 175
Description
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
There seems to be some inconsistencies regarding the usage of generalized field notation in namespaces.
Opening a namespace allows the usage of generalized field notation as if the definition was made at the top level:
namespace A
def Nat.f : Nat → Nat := Function.const _ 1
/--
info: 1
-/
#guard_msgs in
open A in
#eval (1 : Nat).f
end A
With recursive definitions this does not work, however. For instance, consider
namespace A
open A
/--
error: Invalid field `f`: The environment does not contain `Nat.f`, so it is not possible to project the field `f` from an expression
n
of type `Nat`
-/
#guard_msgs in
def Nat.f : Nat → Nat
| 0 => 0
| n + 1 => n.f + 1
end A
Context
On Zulip there was some discussion regarding generalized field notation for scoped definitions and during experiment I came across this limitation. Relevant Zulip topic: #new members > generalized field notation vs namespaces
Steps to Reproduce
namespace A
open A
def Nat.aux : Nat → Nat := Function.const _ 1
def Nat.fNonrecursive (x : Nat) : Nat := x.aux
def Nat.fRecursive : Nat → Nat
| 0 => 0
| n + 1 => n.fRecursive + 1
end A
Expected behavior: Both fNonrecursive and fRecursive compile without or error or none of them do.
Actual behavior: fNonrecursive compiles without error and the fRecursive gives the following error
/--
error: Invalid field `fRecursive`: The environment does not contain `Nat.fRecursive`, so it is not possible to project the field `fRecursive` from an expression
n
of type `Nat`
--/
Versions
/--
info: Lean 4.29.0
Target: x86_64-unknown-linux-gnu
-/
#guard_msgs in
#version
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproducer in the issue, comparing the nonrecursive and recursive definitions under namespace A and open A. Trace generalized field notation and name resolution for n.fNonrecursive versus n.fRecursive; done means both forms consistently compile or consistently report an error, with the reproducer's expected behavior covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100