Zulip Chat Archive

Stream: lean4

Topic: Why does `IO.currentFile` not exist?


Michiel Huttener (Jul 17 2025 at 13:56):

We have IO.currentDir which returns the name of the current directory, but there seems to be no (native) way to let Lean return the current file name.

Julian Berman (Jul 17 2025 at 14:17):

That'd be a slightly funny kind of "false friend" naming for such a thing to me, as the two concepts are not terribly related (one is a thing which is related to any process on a machine, the other is some programming-language-specific concept which isn't always well defined). I agree that I did expect to find such a function somewhere just now, all the languages I know of have one, but amusingly it seems Haskell seems maybe to not have one either unless you're using template haskell?

Arthur Paulino (Jul 17 2025 at 15:11):

Isn't it IO.appPath?

Paul Lezeau (Jul 17 2025 at 15:13):

If you have acceess to the CoreM monad you can access Lean.Core.Context.fileName

Kyle Miller (Jul 17 2025 at 15:22):

It makes senes for this to be in a "meta" monad rather than IO since the currently-processed file is a compile-time concern, not a runtime concern. (Assuming Paul's interpretation that "current file name" means the current Lean file is correct.)

Michiel Huttener (Jul 17 2025 at 15:48):

Aha I see! Thanks for the clarification.

For completeness, a MWE that shows the differences. (Fun to see in the web editor as well!)

import Lean

open Lean IO

 -- IO
#eval currentDir
#eval appDir
#eval appPath

-- very meta
#eval getFileName

Michiel Huttener (Jul 19 2025 at 11:02):

Oh wait, the getFileName above is Lean.MonadLog.getFileName and has nothing to do with Lean.Core.Context.fileName, and I don't see how one can get the context in CoreM.
Nevermind: there is an instance

Eric Wieser (Jul 19 2025 at 15:14):

Arthur Paulino said:

Isn't it IO.appPath?

Arguably it's argv[0], but Lean only passes argv[1:] to def main (lean4#5819)

Markus Himmel (Jul 20 2025 at 08:48):

If you want argv[0] and have a recent Lean you can use docs#Std.Internal.IO.Process.getExecutablePath (it will move out of Internal later this year). I don't know whether this ever returns something different from docs#IO.appPath.

Eric Wieser (Jul 20 2025 at 17:48):

I guess the test is what exec -a secret_name ./lake/build/bin/foo does, which appPath didn't previous respect


Last updated: Dec 20 2025 at 21:32 UTC