Zulip Chat Archive

Stream: lean4

Topic: Accessing argv[0]


Eric Wieser (Apr 09 2024 at 16:43):

Is it possible to access argv[0] in lean? As I read it, src#Lean.IR.EmitC.emitMainFn only propagates argv[1] onwards to the arguments to def main.

Eric Wieser (Apr 09 2024 at 16:49):

(As for the behavior of lean --run, this could set argv[0] to the path to the lean source file, like python -m does)

Eric Wieser (Apr 09 2024 at 19:37):

If it's too late to change the signature of main (which would be very unfortunate, since it currently diverges from Python, C, and Rust), then I suppose this could be exposed by IO.progName : IO String?

Sebastian Ullrich (Apr 09 2024 at 20:24):

Yes, that may be the most reasonable addition at this point. Do you have a use case where appPath (sp?) is not sufficient?

Eric Wieser (Apr 09 2024 at 20:39):

Yes, in my use case the generated binary is symlinked from multiple locations, and I need to know which symlink my binary was invoked from

Eric Wieser (Apr 09 2024 at 20:40):

(I think appPath fully resolved the symlink when I tried it)

Henrik Böving (Apr 09 2024 at 20:41):

Eric Wieser said:

Yes, in my use case the generated binary is symlinked from multiple locations, and I need to know which symlink my binary was invoked from

Note that this is a very general use case, for example busybox "single binary all UNIX utils" trick works like this. So from a PL perspective we definitely want this feature in some way.

Eric Wieser (Apr 09 2024 at 20:42):

It looks like Haskell's design is close to lean; getArgs does not include argv[0], which is instead in getProgName

Eric Wieser (Apr 09 2024 at 20:42):

So I retract any implication that Lean has made a wrong turn here; this is a missing feature not a design flaw :)

Eric Wieser (Apr 09 2024 at 20:43):

Perhaps another solution is to allow def main (progName : String) (args : List String) : IO _

Eric Wieser (Apr 09 2024 at 20:43):

Though making progName available globally monadically like it is in Haskell would be convenient for me

Eric Wieser (Oct 23 2024 at 14:33):

Filed as lean4#5819

Eric Wieser (Oct 23 2024 at 17:57):

And a working patch at lean4#5820


Last updated: May 02 2025 at 03:31 UTC