14. Projects, modules, and library navigation
A collection of checked files becomes a maintainable project when its build, imports, and toolchain are explicit. Lake supplies that structure. A project normally contains lean-toolchain, a Lake configuration, source modules, and a dependency manifest when dependencies have been resolved.
Create a package
In a new directory, with the intended Elan toolchain selected, run:
lake init workbook
lake build
Inspect the generated files before changing them. Lake supports TOML and Lean configuration files. You can copy the capstone configuration as a starting point.
Commit lean-toolchain, the Lake configuration, source files, and lake-manifest.json when it records resolved dependencies. Exclude .lake/, which contains build products and downloaded packages.
Use lake env lean File.lean to check a file in the package’s dependency environment. Use lake build for library and executable targets. Use the package build to verify that all required targets compile together.
Module names follow file paths
A file at Workbook/Core.lean is imported as Workbook.Core. An umbrella module Workbook.lean can import selected public modules. Organise code by responsibility: pure domain operations in one module, their interface theorems alongside them or in a proof module, and input/output in the executable root.
Imports load modules, and namespaces organise declaration names. Opening a namespace lets you refer to its declarations without their full qualification.
namespace Ledger
def credit (balance amount : Nat) : Nat := balance + amount
end Ledger
#check Ledger.credit
section
variable (initial : Nat)
def addReserve (amount : Nat) : Nat := initial + amount
end
#check addReserve
A section scopes local variables and settings within the current namespace. Lean includes a section variable in a declaration when the declaration uses it. Here addReserve receives the initial amount as an argument.
Find declarations by their types
Start from the goal’s main operation. For a list append theorem, use editor completion after List. to find related lemmas and jump to their definitions. Use #check to see whether a candidate’s type fits the goal.
#print shows a declaration in more detail, and #check @name exposes its implicit arguments. To inspect the elaborated details of a single command, prefix it with set_option pp.all true in.
For mathematical work, use library documentation and theorem search, then verify the discovered declaration under your installed dependency version. Search tools identify candidates. Check each candidate’s type and imports in your project.
Dependency discipline
Pin dependency revisions to make builds reproducible. Match the project’s Lean version to the versions expected by its dependencies, then use native cache files built for that toolchain.
Upgrade one layer at a time: choose the dependency revision, adopt its supported toolchain, rebuild, and address renamed declarations or changed APIs. If a proof fails after an upgrade, check whether the simplification rules have changed.
Exercises
- Move a pure function and its theorem into
Workbook/Core.lean, then import them from another file. - Put the declarations in a namespace and call them with fully qualified names.
- Explain why
open Workbookcannot replaceimport Workbook.Core.