Zulip Chat Archive

Stream: lean4

Topic: open is confused


Kenny Lau (Jul 14 2025 at 21:32):

I've found a very interesting behaviour, not sure if it's intended or not:

theorem Nat.foo : 37 = 51 := sorry

namespace Foo

theorem Nat.my_test : 39 = 21 := sorry

open Nat in
theorem bar : 37 = 29 := by
  rw [foo] -- unknown identifier 'foo'

end Foo

Here Lean complains that it has no idea what foo means, until you comment out Nat.my_test above it!

Kenny Lau (Jul 14 2025 at 21:33):

presumably Lean saw the additional declaration Foo.Nat.my_test and decided that open Nat means open Foo.Nat?

Kenny Lau (Jul 14 2025 at 21:33):

(I suppose the intended solution is to replace Nat.my_test with instead _root_.Nat.my_test

Kyle Miller (Jul 14 2025 at 21:34):

Odd, open Nat is supposed to open every Nat that can be opened, if I remember correctly.

Aaron Liu (Jul 14 2025 at 21:35):

weird

Aaron Liu (Jul 14 2025 at 21:35):

is Nat resolved in the current namespace?

Aaron Liu (Jul 14 2025 at 21:36):

this properly opens Nat

theorem Nat.foo : 37 = 51 := sorry

namespace Foo

theorem Nat.my_test : 39 = 21 := sorry

open _root_.Nat in
theorem bar : 37 = 29 := by
  rw [foo] -- unknown identifier 'foo'

end Foo

Kyle Miller (Jul 14 2025 at 21:36):

Yes, it seems to be resolved. If you do open Nat in #where you can see open Foo.Nat.

Kyle Miller (Jul 14 2025 at 21:39):

I guess the deal is that resolveNamespace returns just a single resolution here.

import Lean

theorem Nat.foo : 37 = 51 := sorry

namespace Foo

theorem Nat.my_test : 39 = 21 := sorry

#eval Lean.resolveNamespace (Lean.mkIdent `Nat)
-- [`Foo.Nat]

Kyle Miller (Jul 14 2025 at 21:45):

docs#Lean.ResolveName.resolveNamespaceUsingScope? only looks for a single resolution, using the largest prefix of the current namespace.

(This sort of problem that reusing namespace components can cause.)

Jovan Gerbscheid (Jul 14 2025 at 21:56):

I think this kind of resolution is correct when it comes to identifiers. Because priority is given to those that can be obtained using the namespaces and without the opens. (so that e.g. in namespace CategoryTheory, Functor is unambiguous). But when opening namespaces, this seems wrong.

Michael Stoll (Jul 15 2025 at 09:16):

See also #lean4 > open not opening a namespace @ 💬


Last updated: Dec 20 2025 at 21:32 UTC