From One File to a Lake Project

Every program in this book is one file that you run with lean --run. That command checks the file and then interprets it. For a program with several files, or one that needs to start fast, use Lake. Lake is the Lean build tool, and elan installs it with Lean.

Create a project:

1 lake new hello
2 cd hello

With Lean 4.35.0 this creates the following files, plus a README and a CI workflow:

1 Hello.lean
2 Hello/Basic.lean
3 Main.lean
4 lakefile.toml
5 lean-toolchain

Modules and Files

A Lean module name follows its path: Hello/Basic.lean is module Hello.Basic, and Main.lean at the root is module Main. That is why the executable entry uses root = "Main". Renaming a file changes its module name, and imports must use the new name.

lake-manifest.json records the resolved package versions and commits. Commit it with the project so another machine builds the same dependencies. lake update refreshes it after you change a requirement.

lean-toolchain records the Lean version. elan reads it and uses that version for the project. lakefile.toml lists what to build:

 1 name = "hello"
 2 version = "0.1.0"
 3 defaultTargets = ["hello"]
 4 
 5 [[lean_lib]]
 6 name = "Hello"
 7 
 8 [[lean_exe]]
 9 name = "hello"
10 root = "Main"

Build the project and run the program:

1 $ lake build
2 $ lake exe hello
3 Hello, world!

lake build compiles Lean to C and then to a native program at .lake/build/bin/hello. To turn a program from this book into a project, replace the contents of Main.lean with the file. The import lines stay the same because they name modules of the standard library.

Adding an Executable

lakefile.toml takes one entry per program. Add a file Second.lean at the project root and a matching entry:

1 [[lean_exe]]
2 name = "second"
3 root = "Second"

lake build then writes .lake/build/bin/second. A library entry ([[lean_lib]]) builds modules for other projects to import and has no main. Add a dependency with a require entry and lake update; for mathlib, name the package and the leanprover-community scope.

A Project for This Book

The book’s source_code/lakefile.toml is one project with fourteen executables and no library. Each entry has only a name, because the file at the root has the same name: Bayes.lean, BM25.lean, and so on. To copy that layout, put the fourteen files in one directory with lean-toolchain, add one [[lean_exe]] per file, and run lake build. Each file keeps its own main, so Lake writes one binary per entry under .lake/build/bin/.

Troubleshooting

If lake build uses the wrong Lean version, compare lean-toolchain with elan show. A stale build after an edit usually means the .lake cache; lake clean removes it. lake build reports errors with the file and line, as lean does. A compiled binary starts faster than lean --run, at the cost of a C compile after each change. A network program spends most of its time waiting for the service, so compiling helps little there.

The next chapter looks at the compiled program itself: what Lake produces, how to ship it, and how to call C code from Lean.