Appearance
Using Tolean in a project
This page shows how to lay out a project that uses Tolean, for each language that Tolean supports. The rule is the same everywhere. Each generated Lean file lives in the same package as the source file it came from, next to the claims and proofs about it. One Lake package at the project root builds all of them, and CI checks three things on every change.
The files for one source file
Each source file that you want to verify gets three Lean files beside it:
| File | Who writes it | What it holds |
|---|---|---|
Raise.lean | Tolean | The translated definitions. CI regenerates it, so never edit it. |
RaiseSpec.lean | You | The claims, as def … : Prop in the namespace Spec. Reviewers read this file. |
RaiseProof.lean | You | The proofs, as theorems in the namespace Proof, and #audit_claims Spec Proof. |
The generated file is committed, so a reviewer sees the Lean change in the same pull request as the source change.
What CI checks
CI runs three steps, and any one of them can fail the build:
- It runs
tolean translate --check, which fails when a generated Lean file is not current. This catches source edits that were made without regenerating. - It runs
lake build --wfail. This checks every proof, and#audit_claimsfails the build if a claim has no proof. - It fails if any proof uses
sorry,admit,axiomornative_decide.
An example claim and proof
The example below uses this TypeScript function. A Go project uses the same three files.
ts
export interface Gauge {
readonly level: bigint;
readonly max: bigint;
}
export function raise(g: Gauge, step: bigint): Gauge {
if (g.level + step > g.max) {
return { level: g.max, max: g.max };
}
return { level: g.level + step, max: g.max };
}Tolean writes Raise.lean:
lean
-- Generated by tolean 0.3.1 for ES2024 from sensor/raise.ts. Do not edit.
namespace sensor.Raise
structure Gauge where
level : Int
max : Int
def raise (g : Gauge) (step : Int) : Gauge :=
if (g.level + step) > g.max then
({ level := g.max, max := g.max } : Gauge)
else
({ level := (g.level + step), max := g.max } : Gauge)
end sensor.RaiseYou write the claim in RaiseSpec.lean:
lean
import sensor.Raise
namespace Spec
open sensor.Raise
/-- `raise` never lifts the level above the maximum. -/
def raise_stays_under_max : Prop :=
∀ (g : Gauge) (step : Int), g.level ≤ g.max → (raise g step).level ≤ (raise g step).max
end SpecAnd you write the proof in RaiseProof.lean:
lean
import tolean.Audit
import sensor.RaiseSpec
namespace Proof
open sensor.Raise
theorem raise_stays_under_max : Spec.raise_stays_under_max := by
intro g step h
unfold raise
split
· exact Int.le_refl _
· show g.level + step ≤ g.max; omega
end Proof
#audit_claims Spec ProofTypeScript
Install Tolean as a development dependency. The npm package contains the translator and the Lean runtime that generated code imports.
myapp/
package.json # devDependency: tolean
lakefile.toml
lean-toolchain # the same Lean version as tolean
src/sensor/
raise.ts
Raise.lean # generated
RaiseSpec.lean
RaiseProof.leanThe lakefile.toml at the project root points at the runtime inside node_modules, and it lists each package directory that holds Lean files:
toml
name = "myapp"
defaultTargets = ["sensor"]
[[require]]
name = "tolean"
path = "node_modules/tolean/lean"
[[lean_lib]]
name = "sensor"
srcDir = "src"
globs = ["sensor.+"]Keep the library in defaultTargets. If you leave it out, lake build finishes successfully without building anything.
To regenerate the Lean files, run Tolean from the directory that srcDir names, so that each namespace matches its module path. Pass the project config inline with --config, or put it in a tolean.json file in that directory:
sh
cd src && lake -d .. exe tolean-cli translate --config '{"typescript":{"sources":["sensor/*.ts"]}}'Always run Tolean through a project config. The single-file commands are for Tolean's own tests, and their arguments can change between versions.
Go
The Go translator works one package at a time. By default it starts from the package's exported functions, and the roots setting can name others. We have checked that Lake and the Go toolchain accept this layout: Lake builds the Lean files from inside the Go package directory, and go build and go vet ignore them.
mymod/
go.mod
tolean.json # lists the Go sources to translate
lakefile.toml # requires tolean at a pinned tag
lean-toolchain
sensor/
raise.go
Sensor.lean # generated, for the whole package
SensorSpec.lean
SensorProof.leanThe lakefile.toml uses the project root as srcDir, because Go packages sit directly under it:
toml
name = "mymod"
defaultTargets = ["sensor"]
[[require]]
name = "tolean"
git = "https://github.com/shihangw/tolean"
rev = "v0.3.1"
[[lean_lib]]
name = "sensor"
srcDir = "."
globs = ["sensor.+"]The tolean.json file at the project root lists the packages to translate:
json
{ "go": { "sources": ["sensor/*.go"] } }Run lake exe tolean-cli translate from the project root to regenerate all the Lean files. Run it from the root, because the namespace follows the path from there. Tolean translates a whole package into one file, named after its directory, so the package in sensor/ gives sensor/Sensor.lean with the namespace sensor.Sensor. The glob must match every .go file of a package, because a root function can reach declarations in any of them. The go line in go.mod will play the same role as the ECMAScript target does for TypeScript: it sets the language version that the translation assumes.
How Tolean reaches Go projects
A Go project gets Tolean through Lake, not through the Go toolchain. The project needs Lean anyway, because CI runs lake build to check the proofs. The [[require]] block that brings in the Lean runtime also brings in the translator, because both live in the same Tolean repository. Lake builds the translator from source the first time, and lake exe tolean-cli runs it after that. Lake runs it in the current directory, so the paths in the config are relative to where you run it.
This gives us one pinned version, in one place. The translator and the runtime always come from the same tag, so they cannot drift apart. There are no release binaries to build for each platform and no download step to secure.
The cost is that a developer who regenerates Lean files must have Lean installed, through elan or the Tolean Nix flake. The first build of the translator also takes a few minutes, so CI should cache the .lake directory. If Go developers who never touch proofs find that too heavy, we can add prebuilt binaries and a small Go wrapper module later, without changing this layout.
This page is docs/project-layout.md at commit 5f1d031.
