Zulip Chat Archive

Stream: new members

Topic: A command that prints the current namespace?


Eduardo Ochs (May 18 2024 at 06:20):

Hi all,

how do I write a command that prints the current namespace? I guess that I would need elab, CoreM, logInfo, and getCurrNamespace, but I don't know how to put these pieces together...

Thanks in advance!

Yaël Dillies (May 18 2024 at 06:20):

Have you looked at how #where is implemented?

Eduardo Ochs (May 18 2024 at 06:29):

Is it from Lean itself or from mathlib? I don't have mathlib here now, and my usual ways of grepping the Lean sources don't show it...

Yaël Dillies (May 18 2024 at 06:30):

Hmm, don't actually know

Damiano Testa (May 18 2024 at 06:34):

I think that the namespace is stored in the Scope. You may be able to access it from CommandElabM.

Damiano Testa (May 18 2024 at 06:46):

Actually docs#Lean.Macro.getCurrNamespace may do just that for you.

Eduardo Ochs (May 18 2024 at 06:53):

Sorry for my newbieness, but how do I convert its output to a string? Try:

namespace Foo
#check Lean.Macro.getCurrNamespace
#print Lean.Macro.getCurrNamespace
#eval  Lean.Macro.getCurrNamespace
end Foo

The #eval fails...

Damiano Testa (May 18 2024 at 07:08):

Try something like #eval do logInfo (← Lean.Macro.getCurrNamespace) (untested).

Damiano Testa (May 18 2024 at 07:18):

import Lean

namespace Lean
run_cmd logInfo ( getCurrNamespace)

(tested!)

Markus Schmaus (May 18 2024 at 07:29):

Cool! To make this work in different namespaces as well, I just needed to use the Lean..

import Lean

namespace Foo
run_cmd Lean.logInfo ( Lean.getCurrNamespace)

Kyle Miller (May 18 2024 at 15:12):

If you add the open Lean command you don't need to add the Lean. prefix.


Last updated: May 02 2025 at 03:31 UTC