Zulip Chat Archive
Stream: lean4
Topic: dot notation and open
Eric Wieser (Jun 30 2025 at 14:55):
Is this intended behavior?
def Wat.Prod.thd {α β γ} (x : α × β × γ) : γ := x.2.2
open Wat in
#check (1, 2, 3).thd -- works!
Kyle Miller (Jun 30 2025 at 14:57):
Yes, the idea is supposed to be that you can open some functions for your own library, without polluting another namespace you don't own.
Eric Wieser (Jun 30 2025 at 14:59):
This ends up a little confusing with things like
import Mathlib
open List
#check #v[1].exists_eq_cons
which is trying to invoke List.Vector.exists_eq_cons on _root_.Vector
Kyle Miller (Jun 30 2025 at 15:00):
(lean4#6189 was the change)
Eric Wieser (Jun 30 2025 at 15:00):
For what it's worth, this isn't breaking me, I just was surprised by the feature, and it seems risky in the face of types that share a name in different namespaces.
Kyle Miller (Jun 30 2025 at 15:01):
That's not good... but I think it's also not good practice sharing name components like this, having the same name for a type in different namespaces.
Kyle Miller (Jun 30 2025 at 15:02):
At least in this case it's just surprising it resolves at all, right? Not that you're not getting things that you want to resolve to resolve?
Eric Wieser (Jun 30 2025 at 15:29):
Indeed, I don't have an example where it's harmful, only where it fails confusingly
Mario Carneiro (Jun 30 2025 at 15:54):
Kyle Miller said:
That's not good... but I think it's also not good practice sharing name components like this, having the same name for a type in different namespaces.
This is a new change to 'good practice' if so. To me it seems quite reasonable if you have several different versions of an API which are separated by their namespace
Mario Carneiro (Jun 30 2025 at 15:55):
in fact, it largely defeats the purpose of namespaces, it means we have to be globally unique again and worry about name conflicts caused by this
Eric Wieser (Jun 30 2025 at 16:14):
For what it's worth, C# has this feature via "extension methods", but it's opt in by marking one of the arguments in a special way
Mario Carneiro (Jun 30 2025 at 16:19):
I don't think C# has the ability to turn extension methods on and off again, or does it?
Mario Carneiro (Jun 30 2025 at 16:19):
in rust if you use typeclasses to define extension methods then they are available as long as you have opened the typeclass, so you can turn them off and on again to some extent
Kyle Miller (Jun 30 2025 at 17:01):
Mario Carneiro said:
This is a new change to 'good practice' if so. To me it seems quite reasonable if you have several different versions of an API which are separated by their namespace
It's not a change — I'm expressing a personal opinion.
Let me refine this opinion: if you have multiple namespaces you are working with and you want to open them all, you shouldn't reuse name components. In my experience, it's not good practice, since the name conflicts cause issues. (I personally like to avoid reusing any name components between namespaces that tend to be used in modules together, even if I don't open them, but that's not a strict rule.)
Here we have Vector and List.Vector, with open List. Would you agree that having List.Vector defeats the purpose of being able to open List?
Mario Carneiro (Jun 30 2025 at 19:35):
I agree that you shouldn't use Vector and List.Vector at the same time if you have used open List
Mario Carneiro (Jun 30 2025 at 19:36):
Is it the case that all problematic situations here for dot notation are also problematic in the sense of having an ambiguous name in scope?
Jireh Loreaux (Jun 30 2025 at 22:19):
If I understand correctly, the net effect of this is we could namespace Mathlib and nothing would break (in Mathlib, downstream would then have to open Mathlib a lot). Is that correct? This would obviate the biggest obstacle in my mind to that choice.
Kevin Buzzard (Jun 30 2025 at 22:32):
+1 to namespacing Mathlib BTW. I don't see why we should be arrogant enough to just take the root namespace for our ideas.
Eric Wieser (Jun 30 2025 at 22:42):
I don't think that was really the point of Jireh's comment; we could have Mathlib.Group anyway, it's things like docs#Nat.digits that the re-namespacing of is more controversial.
Niels Voss (Jun 30 2025 at 23:13):
The doc-gen site would display Mathlib before every definition name, though
Kim Morrison (Jun 30 2025 at 23:57):
Good? Or if it's bad, then change doc-gen? Seems a minor problem!
Niels Voss (Jul 01 2025 at 02:11):
Out of curiosity, would this allow two different versions of Mathlib based on the same Lean version to be loaded simultaneously by changing one of the namespace names? (I am not claiming this is a good idea, I'm just wondering if it would be possible)
Niels Voss (Jul 01 2025 at 02:12):
Actually nevermind, there are overlapping tactics and global notations
Yaël Dillies (Jul 01 2025 at 08:10):
Niels Voss said:
The doc-gen site would display Mathlib before every definition name, though
And loogle too. This is already a big issue when
- We can't use scoped notation in loogle
- There are super long namespaces like
CategoryTheory
Eric Wieser (Jul 01 2025 at 11:06):
1: yes we can, open scoped Foo in $your_search
Sebastian Ullrich (Jul 01 2025 at 11:50):
A user-configurable, persistent set of default opens for Loogle seems like a reasonable feature request to me, I would use it for open Lean!
Kenny Lau (Aug 19 2025 at 10:37):
It seems like there's another potentially unintended behaviour:
axiom A : Type
axiom B : Type
namespace A
-- comment me out
open A
axiom B.c (x : B) : Type
variable (x : B)
#check x.c
end A
Kenny Lau (Aug 19 2025 at 10:38):
you have to actually open the namespace to make this bug work, even if you're already in the namespace! (I would have previously assumed that the namespace is automatically opened in the namespace)
Kyle Miller (Aug 19 2025 at 16:02):
@Kenny Lau I was experimenting with this recently in the unfinished lean4#9636 to try to address issue lean4#6135
The current rules for dot notation ignore the current namespace when resolving the name. Roughly, the way name resolution works in general is that it first uses all prefixes of the current namespace to find a name, and if that doesn't work, it tries all the open namespaces. The PR would make it use the namespaces.
The idea of the PR is to make it possible to be in namespace A, then define A.IO.FS.Stream.foo, and then use it with dot notation. And, while making sure that open IO will always open _root_.IO, even if it might also open A.IO because you're somewhere in the A namespace.
Martin Dvořák (Oct 05 2025 at 09:07):
I am happy with Mathlib having the monopoly for defining what Group is.
My personal preference is to not namespace everything.
Kyle Miller (Oct 06 2025 at 14:43):
(Link to an MWE about a possibly related issue: )
Last updated: Dec 20 2025 at 21:32 UTC