Zulip Chat Archive

Stream: general

Topic: protected attribute


Reid Barton (Sep 24 2020 at 17:05):

I just noticed that both protected def ... and @[protected] def ... seem to be valid. Is there any difference?

Reid Barton (Sep 24 2020 at 17:06):

To partially answer my question, there definitely is some difference

Simon Hudon (Sep 24 2020 at 17:19):

What difference do you see?

Reid Barton (Sep 24 2020 at 17:22):

In the same section, it looks like @[protected] has no effect. It's also possible it has no effect whatsoever.

Simon Hudon (Sep 24 2020 at 17:26):

Here is a comparison:

namespace my_ns

protected def foo := 3

#check my_ns.foo
#check foo -- error

@[protected] def bar := 3

#check my_ns.bar
#check bar -- not an error

end my_ns


#check my_ns.foo
#check foo -- error

#check my_ns.bar
#check bar -- error

open my_ns

#check my_ns.foo
#check foo -- error

#check my_ns.bar
#check bar -- error

Simon Hudon (Sep 24 2020 at 17:26):

You're right, in the same namespace, it does not take effect yet

Simon Hudon (Sep 24 2020 at 17:26):

But if you close and open the namespace again:

namespace my_ns

#check my_ns.foo
#check foo -- error

#check my_ns.bar
#check bar -- error

end my_ns

Reid Barton (Sep 24 2020 at 17:30):

Thanks, this confirms what I thought I was seeing. Now the question is--is this intentional? It does seem sort of useful...

Simon Hudon (Sep 24 2020 at 17:31):

It's not intentional, I didn't even know there was a difference. I'm not sure why it's there

Simon Hudon (Sep 24 2020 at 17:31):

How do you make use of it?

Reid Barton (Sep 24 2020 at 17:32):

Accidentally

Simon Hudon (Sep 24 2020 at 17:33):

That's an interesting technique :)

Reid Barton (Sep 24 2020 at 17:35):

I'm returning to nat.pow and I marked a lemma @[protected] in the original nat.pow changes--but I'm sure I didn't intentionally choose it over protected.

Reid Barton (Sep 24 2020 at 17:35):

Now I am marking more lemmas as protected and in the process of doing so I noticed this difference because the lemmas are used later on in the same section.

Reid Barton (Sep 24 2020 at 17:35):

In the end I intend to delete all the lemmas anyways so it doesn't matter.

Simon Hudon (Sep 24 2020 at 17:41):

That's interesting. The curious thing is that, if protected and @[protected] were to behave the same, you could achieve the same effect by adding attribute [protected] foo_bar when you're ready to protect it

Kyle Miller (Sep 24 2020 at 17:41):

I think this is an implementation detail of protected. When you enter a namespace or open a namespace, it seems to go through all the symbols that don't have the protected attribute and adds the prefix-free version to the list of currently accessible symbols. When you do def normally, it seems to both create the symbol and add it to the list of currently accessible symbols, but if it's protected def it adds the protected attribute but does not do the step where it adds it to the list of currently accessible symbols. If you do @[protected] def, then it's def but then adds the protected attribute afterwards.

Something I found surprising about open is how it is an action and not a declaration:

namespace foo
def x := 2
end foo

open foo

namespace foo
def y := 2
end foo

#check x -- ok
#check y -- error

Kyle Miller (Sep 24 2020 at 17:42):

It seems namespace management was inspired by some Scheme's symbol namespace management, at least to some degree. (Common Lisp has symbol packages that can refer to other symbol packages to permanently "open" them.)

Reid Barton (Sep 24 2020 at 17:44):

I think this behavior of open is changing in Lean 4

Simon Hudon (Sep 24 2020 at 17:55):

How so?

Reid Barton (Sep 24 2020 at 18:15):

my understanding is that open will apply also to definitions which didn't exist at the time of the open, like y in the above example

Simon Hudon (Sep 24 2020 at 18:23):

Yes, that's true, I heard that too. That's certainly an improvement


Last updated: Dec 20 2023 at 11:08 UTC