Zulip Chat Archive

Stream: new members

Topic: How to end a scope?


Darij Grinberg (Oct 24 2025 at 19:40):

I can open a scope in Lean using open. How do I close it?

Kenny Lau (Oct 24 2025 at 19:43):

enclose it in a section/namespace

Arthur Paulino (Oct 24 2025 at 19:46):

I think it would be nice to be able to close it within the same section or namespace.

@Darij Grinberg you can also do

open Foo in
def bar ...

Which opens the Foo scope limited to bar

Darij Grinberg (Oct 24 2025 at 19:50):

ah!

Kenny Lau (Oct 24 2025 at 19:59):

i think it's possible, i'm trying to do it rn

Kenny Lau (Oct 24 2025 at 20:08):

import Lean

def Lean.OpenDecl.match (n : Name) : OpenDecl  Bool
  | .simple a _b => a == n
  | .explicit a _b => a == n

namespace Lean.Elab.Command

def Scope.close (n : Name) (sc : Scope) : Scope :=
  { sc with openDecls := sc.openDecls.filter (!OpenDecl.match n ·) }

elab "close " ns:ident : command =>
  modifyScope <| Scope.close ns.getId

end Lean.Elab.Command

section Test

#check Nat.add_mod
#check add_mod -- error

open Nat

#check Nat.add_mod
#check add_mod

close Nat

#check Nat.add_mod
#check add_mod -- error

end Test

Kenny Lau (Oct 24 2025 at 20:09):

the #xy continues...

Darij Grinberg (Oct 24 2025 at 20:32):

wow, I didn't know Lean code could modify itself this easily

Darij Grinberg (Oct 24 2025 at 20:32):

but yeah, i can wrap my scopes in sections, no problem with that

Kenny Lau (Oct 24 2025 at 20:33):

image.png

so that means i can also delete variables... i've wanted that feature for a while

Junyan Xu (Oct 25 2025 at 18:13):

so that means i can also delete variables... i've wanted that feature for a while

Is omit not good enough?

Kenny Lau (Oct 26 2025 at 08:07):

@Junyan Xu see #mathlib4 > RFC: delete variables


Last updated: Dec 20 2025 at 21:32 UTC