Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

def _root_... creates literal _root_ namespace #490

Closed
Kha opened this issue May 26, 2021 · 7 comments
Closed

def _root_... creates literal _root_ namespace #490

Kha opened this issue May 26, 2021 · 7 comments
Labels
enhancement New feature or request lean4_release Must be addressed before first official release

Comments

@Kha
Copy link
Member

Kha commented May 26, 2021

namespace Foo
def _root_.bar := 1
end Foo
#check bar
#check Foo._root_.bar

Expected behavior: for the first #check to work

Actual behavior: the second #check unexpectedly works

@leodemoura
Copy link
Member

We should not create a _root_ namespace, and this part is a bug and should be fixed.
This issue also proposes a new feature, the ability to add a declaration to the "root" namespace without closing the current one. I view this feature as a low priority and don't want to think about it right now. For example, what can this feature break? Do we have to change how we process attributes?

@Kha
Copy link
Member Author

Kha commented May 26, 2021

Yeah, not accepting the second check is good enough for now

@leodemoura
Copy link
Member

@gebner Is this feature important for the mathlib port?

@leodemoura leodemoura added the enhancement New feature or request label Jun 1, 2022
@digama0
Copy link
Collaborator

digama0 commented Jun 2, 2022

Yes, this feature was implemented in lean 3 and gets some use. It's not incredibly common but there isn't any obvious way for synport to drop in an alternative without significantly changing the surrounding syntax (closing all the namespaces, adding the definition, reopening the namespace and adding all the variables and local things back...)

@gebner
Copy link
Member

gebner commented Jun 2, 2022

It's not incredibly common

To give concrete numbers, it occurs 651 times in mathlib.

We might also need it to override instance names in the port: leanprover-community/mathport#106

@leodemoura
Copy link
Member

leodemoura commented Jun 2, 2022

@digama0 @gebner Thanks for the feedback.

Could you please confirm whether the following is the behavior you are expecting?

  • Suppose we have open and export active commands in the current namespace/scope. I am assuming they will continue in effect while elaborating def _root_.foo. Correct? The question includes name resolution and scoped attributes. For example
def Bla.z := 1

namespace Foo
open Bla
def x := 10

def _root_.y := x + z -- `x` here is referring to `Foo.x` and `z` to `Bla.z`
  • I am assuming it is an error to use scoped attribute with this feature. Example: @[scoped simp] def _root_.foo ... is not accepted.

Any other gotchas?

@digama0
Copy link
Collaborator

digama0 commented Jun 3, 2022

These are to some extent unexplored questions since the way lean 4 handles namespaced definitions, as though there was a namespace Foo around the definition, is new.

Here's a way that I think can lead to coherent answers to these questions: Imagine that we can write namespace _root_ which acts just like a regular namespace (it is still nesting "inside" namespace Foo and would be closed with end _root_), where the only difference from a regular namespace is that instead of definitions getting the name Foo._root_.y the *._root_ part is removed.

With that model, the name resolution would act as though the open and exports are still in scope, but root-scoped definitions and Bar scoped definitions (in the def _root_.Bar.y case) would take precedence. In your example, x would refer to Foo.x and z would refer to Bla.z like you said.

The behavior of @[scoped attr] should be the same as for @[scoped attr] on a def Bar.foo. I had to check lean to see what it does, and it looks like it makes the attribute active in the namespace Foo, not Bar. Consistency would then suggest to make @[scoped attr] scope the attribute in the namespace Foo (and the current section) even if the definition itself is at _root_.y.

@leodemoura leodemoura added the lean4_release Must be addressed before first official release label Jun 3, 2022
leodemoura added a commit that referenced this issue Jun 13, 2022
Pushed the wrong file in previous commit.

see #490
ChrisHughes24 pushed a commit to ChrisHughes24/lean4 that referenced this issue Dec 2, 2022
With this, the files `init.core` and `init.logic` are synchronized, although we aren't currently tracking files from `init` in [port-status](https://github.com/leanprover-community/mathlib/wiki/mathlib4-port-status).

Co-authored-by: Scott Morrison <[email protected]>
ChrisHughes24 pushed a commit to ChrisHughes24/lean4 that referenced this issue Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lean4_release Must be addressed before first official release
Projects
None yet
Development

No branches or pull requests

4 participants