leanprover / leanprover/lean4

Inconsistent name resolution with generalized field notation in opened namespaces

Open
#13,261 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.