Zulip Chat Archive

Stream: new members

Topic: open scoped


Kevin Buzzard (May 02 2024 at 11:40):

From Mathlib.Algebra.Quaternion:

The following notation is available with `open Quaternion` or `open scoped Quaternion`.

* `[R, c₁, c₂]` : `QuaternionAlgebra R c₁ c₂`
* `[R]` : quaternions over `R`.

What's the difference between open and open scoped?

Damiano Testa (May 02 2024 at 11:52):

I think that open scoped does a subset of what open does: it "opens" what is in

scoped[AddMonoidAlgebra] notation:9000 R:max "[" A "]" => AddMonoidAlgebra R A

but does not also give you access to the AddMonoidAlgebra. lemmas without the AddMonoidAlgebra-prefix.

Damiano Testa (May 02 2024 at 11:53):

import Mathlib

open scoped Polynomial

#check Nat[X]
#check C  -- fails

open Polynomial

#check Nat[X]
#check C

Damiano Testa (May 02 2024 at 11:54):

The notation Nat[X] works the first time, since it is scoped, but C is not picked up as Polynomial.C.

Kevin Buzzard (May 02 2024 at 11:57):

Oh -- is it like lean 3 open locale?

Damiano Testa (May 02 2024 at 11:57):

Ah, very likely yes! Maybe not literally, but probably close enough.

Kevin Buzzard (May 02 2024 at 11:58):

Lean 4 open = Lean 3 open + open locale (i.e. the namespace plus the notation), and Lean 4 open scoped is just the notation but not the namespace?

Damiano Testa (May 02 2024 at 11:58):

Yes, at least this is my mental model. With some flexibility about possible edge cases. I think that also instances can be scoped.

Michael Stoll (May 02 2024 at 12:00):

Yes, they can.

Damiano Testa (May 02 2024 at 12:01):

I think that open_locale also allowed instances anyway, so probably the analogy with open_locale is quite accurate.

Floris van Doorn (May 02 2024 at 21:21):

Yes, open scoped is indeed the analogue of open_locale in Lean 3. Except in Lean 3 it was a hack, and Lean 4 supports it natively.

Floris van Doorn (May 02 2024 at 21:22):

And in both versions you could scope/localize any attribute (including instances) and notation.

Floris van Doorn (May 02 2024 at 21:22):

In Lean 4 you can also scope things like macros, pretty printers, parsers, and various other things.

Floris van Doorn (May 02 2024 at 21:23):

e.g. we now have notation ∑ (x ∈ s) (y ∈ s), f x y and we could imagine writing a pretty printer for that. But because it obscures what is going on, that maybe should be a scoped pretty printer

Eric Wieser (May 02 2024 at 22:03):

One thing that's missing here is an open notscoped Foo that matches the behavior of the lean 3 open foo. (for Nat, it would put factorial in the local environment, but not the ! notation)

Eric Wieser (May 02 2024 at 22:04):

Maybe it could be spelt open Nat (*), which matches open Nat (factorial)

𝚠𝚘𝚓𝚌𝚒𝚎𝚌𝚑 𝚗𝚊𝚠𝚛𝚘𝚌𝚔𝚒 (May 02 2024 at 22:11):

Eric Wieser said:

One thing that's missing here is an open notscoped Foo that matches the behavior of the lean 3 open foo. (for Nat, it would put factorial in the local environment, but not the ! notation)

For this reason I have been writing

namespace Foo
namespace Notation
...
end Notation
...
end Foo

so that open Foo does not necessarily bring in the notation.


Last updated: May 02 2025 at 03:31 UTC