Zulip Chat Archive

Stream: lean4

Topic: Control namespaces


Andrej Bauer (May 09 2023 at 20:42):

Is there an option (such as autoImplicit) that means "arbitrary namespaces which are likely typos should not be silently accepted"? I get bitten by stuff like this:

structure STree := ...

def Stree.map := ...  -- this should be an error, I mispelled Stree

Even this works -- and it's not a good idea:

def once.upon.a.time.there.was.a.kingdom := 42

Mario Carneiro (May 09 2023 at 20:43):

unfortunately not, because namespaces are never really "declared" in lean, their existence is inferred from first use, kind of like using mkdir -p when writing a declaration

Andrej Bauer (May 09 2023 at 20:44):

Oh my.

Mario Carneiro (May 09 2023 at 20:45):

they are actually a thing that can be declared in the sense that open STree works and open Stree doesn't (until after your typo'd declaration), but there is no def namespace command

Mario Carneiro (May 09 2023 at 20:48):

A linter might be able to catch your example, but this simple syntactic fact about implicit namespace creation in idiomatic code prevents catching all cases without a biggish refactoring of lean and the ecosystem

Chris Bailey (May 09 2023 at 21:03):

Andrej Bauer said:

I get bitten by stuff like this:

structure STree := ...

def Stree.map := ...  -- this should be an error, I mispelled Stree

Fwiw the way people usually seem to do this (which also gets rid of the class of errors you're running into) is:

structure STree := ...

namespace STree

def map := ..
def fold := ..

end STree

Andrej Bauer (May 09 2023 at 21:10):

That's a good idea, thanks. BTW, I have learned through pain that open is evil.

Adam Topaz (May 09 2023 at 21:13):

Would you consider open Foo in ... less evil?

Kyle Miller (May 09 2023 at 21:23):

(I once cut my hand with a knife while carving and learned that knives are evil too :wink:)

Kyle Miller (May 09 2023 at 21:28):

Though open is definitely similar to Python's from Foo import *, which I pretty much never use. It's able to do some type-based disambiguation at least, which Python doesn't give you, and Lean gives you static checking. Usually in Lean I open namespaces (within a section/namespace) if I'm working with something that could have been in another namespace, but for whatever reason it's better to be in a new namespace.

Kyle Miller (May 09 2023 at 21:31):

One painful thing is that there are a couple of key namespaces with the same function name, like there's both docs4#Lean.Elab.Term.elabTerm and docs4#Lean.Elab.Tactic.elabTerm with essentially the same type signature if you're in the TacticM monad due to lifting.

Andrej Bauer (May 09 2023 at 21:34):

If ... is less than a screenful of code, yes.

Chris Bailey (May 09 2023 at 22:14):

Andrej Bauer said:

That's a good idea, thanks. BTW, I have learned through pain that open is evil.

What's the beef with open? I feel like most languages have a similar mechanism.

If you're using VScode, the current namespce and section information for your cursor will show up in the breadcrumbs line, which is at the top/under any tabs. You can get nice results with open commands by using them within sections (thanks Mario) :

section
open STree
/- closes STree -/
end

section Named
open STree
/- closes STree -/
end Named

Last updated: Dec 20 2023 at 11:08 UTC