In Lean 4, we have IO.currentDir which returns the cwd. Is it possible to set the cwd though?
How to change current working directory in Lean 4?
64 Views Asked by Tony Beta Lambda At
1
In Lean 4, we have IO.currentDir which returns the cwd. Is it possible to set the cwd though?
Short answer: No
Long answer: Yes, but probably not in a nice way that you would ever want to use in practice.
If you dig into the IO source, you will see
IO.currentDiris defined as an opaque reference to the underlyinglean_io_current_dir. If we look atlean_io_current_dirwe see it just queries the C functiongetcwd.getcwdhas no problem getting a new path as long as we can tell the OS to set it somehow. Doing this in Lean4 is the hard part since we need to somehow callchdiron ourselves, and to my knowledge the Lean developers have not given us a nice way to do this. However, something like this can be implemented in a hacky system dependent way. The following is an example of a hacky Linux solution based on gdb I've gotten to work. This solution is based off this post.The solution works by using gdb to attach to the main lean process via its PID and forcing it to run the
chdirsyscall.