Up to now we have checked results inside a single file using #eval. Now we will read command-line arguments, print to the screen, and build a native executable.
1. A Pure Greeting Function and an Effectful Printing Function
Because greet takes a name and simply returns a string, we can test our greeting message without touching the screen. The notation s!"Hello, {name}!" interpolates the name into the string. At the same time, main is the entry point that reads command-line arguments and actually prints the message.
Its return type IO UInt32 means that running it performs input/output and finishes by returning a 32-bit exit code. Under do, we list the actions to run in order and end with return 0 to report a successful exit.1
An IO action describes work to be carried out. Here, IO UInt32 describes an action that prints a greeting and hands back an unsigned 32-bit number. The result of greet is just a String: it assembles the words.
Placing the types of the two functions side by side makes it immediately clear which one computes a pure string and which one interacts with the terminal.
If you only want to change the wording of the greeting, you only need to look at greet. Reading command-line arguments and printing are isolated inside main, and the final number is the process exit code rather than part of the greeting.2
Put the code below into an empty Greet.lean file and run lean Greet.lean in that folder! Passing a name to #eval on the last line lets you test the greeting before creating a full project.
def greet (name : String) : String :=
s!"Hello, {name}!"
def main (args : List String) : IO UInt32 := do
let target := args.head?.getD "Lean 4"
IO.println (greet target)
return 0
#eval main ["Makonea"]Output:
Hello, Makonea!
0Compare which step inside main produced each of the two output lines. Here args.head? safely extracts the first list element as an Option String (some "Makonea"), and .getD "Lean 4" falls back to "Lean 4" whenever no argument was given (none).
The first output line, Hello, Makonea!, is printed to the screen by IO.println (greet target), while the second line, 0, is the exit code (UInt32) returned by return 0 and displayed by #eval.
The two lines we wrote top-to-bottom inside the do block are chained internally into a single action as in the formula below. The symbol (written >>= in code, the monadic bind operator) is the connector meaning “run the I/O action on the left first, then feed its result into the input of the function on the right to continue.” And (written return 0 in code) is the action that wraps the number 0 inside IO as the final result without performing any screen output.
Here the result value produced by printing has type Unit: because there is no meaningful payload needed by the next step, the lambda simply receives it and moves on. Writing the block with do lets you read the sequence from top to bottom without manually nesting lambdas at every step.34
Unit has just one value, written () in code. Running IO.println puts text on the screen, with no data to send to the next line. That is why its result type is IO Unit.
2. Creating and Building a Project with Lake
Now we build a standalone executable with **Lake** (Lean Make), Lean’s build and package manager. In a terminal inside your practice folder, run lake new to create a new project and move into its directory.5
lake new hello_lean
cd hello_leanOpen the generated Main.lean file, replace its contents with def greet and def main from Section 1 (omitting the #eval test line at the bottom), and save.
Below, lake build compiles the targets declared in the project. In lake exe hello_lean Makonea, hello_lean is the executable target, and main receives Makonea in its argument list. Main.lean is the source file containing the code that starts that target.
lake build
lake exe hello_lean MakoneaRunning the commands above prints the greeting in your terminal:
Output:
Hello, Makonea!Notice that unlike #eval main ["Makonea"] in Section 1, only the single greeting line appears here and the second line 0 is gone! In a native executable, only strings sent to IO.println are printed to the terminal screen, whereas the 0 returned by return 0 at the end of main is handed directly to the operating system shell as the process exit code indicating that the program finished without errors.
Three files in the project folder control the build:
lean-toolchain: pins the exact Lean compiler version for this project (read automatically byelan).lakefile.toml: declares the package name, external library dependencies (such asmathlib), library targets, and executables. Therootfield under[[lean_exe]]points to theMainmodule containingmain.Main.lean: the source file containing the executable’smainentry point.
Code that we previously tested in-place with #eval can now accept arguments straight from the command line. Try running lake exe hello_lean with no arguments at all: thanks to the fallback value we set in Section 1, it prints Hello, Lean 4!.
Footnotes
- Lean Language Reference 4.36.0. IO. Explains how side-effecting operations such as input and output are represented and executed via
IO. ↩ - Lean Language Reference 4.36.0. IO · Optional Values. Covers the effect boundary marked by
IOand howgetDsupplies a default value. ↩ - Lean Language Reference 4.36.0. Syntax: do Notation. Covers desugaring sequential statements into
bindand producing final values withreturn. ↩ - Sebastian Ullrich and Leonardo de Moura, ICFP 2022. ‘do’ Unchained: Embracing Local Imperativity in a Purely Functional Language. Introduces the design and semantics of Lean 4’s
donotation. ↩ - Lean Language Reference 4.36.0. Lake. Summarizes project commands including
lake new,lake build, andlake exe. ↩