leanfran.tomlleanfran generate compiles the modules of your Lake packages to every language you list in leanfran.toml. One command replaces a leanfran --lang <language> module generate command per module: it builds the packages, binds the types that the generated modules share, and writes the files.
leanfran generate [--dir <dir>] [--output json|text]
--dir is the directory of leanfran.toml (default: the working directory). The paths in the file are relative to it. lake comes from $LAKE, then PATH.
Each [[generate]] table compiles modules of one Lake package to one language and one directory.
[[generate]]
lang = "rust"
lake-dir = "lean"
output-dir = "src/gen"
path = "crate::generated"
modules = ["Shop.Cart", "Shop.Order"]
[generate.bind]
"Shop.Money" = "::money::generated::shop_money"
[[generate]]
lang = "python"
lake-dir = "lean"
output-dir = "python/shop/gen"
path = "shop.gen"
modules = [
{ name = "Shop.Cart", as = "cart" },
{ name = "Shop.Order", as = "order", bind = { "Shop.Money" = "money.gen.shop_money" } },
]
| Key | Required | Meaning |
|---|---|---|
lang | yes | rust, python, go, typescript, php, clojure, or c |
lake-dir | yes | The Lake package that holds the modules |
output-dir | yes | The directory of the generated code |
path | Rust, Python, Go, PHP, and Clojure | The Rust module path, Python package, Go import path, PHP namespace, or Clojure namespace prefix of output-dir |
modules | yes | The modules to compile: a module name, a glob, or a table |
bind | no | Types defined outside the table: a Lean namespace and the path of its types in the language. Clojure and C tables reject it |
An entry of modules is one of:
"Shop.Cart";"Shop.*" is Shop and every module below it, "Shop.+" is every module below it. A glob matches the modules that lake build built in the package;{ name = "Shop.Cart", as = "cart", bind = { ... } }. as names the generated code, and bind applies to this module only.| Language | Default name of a module | Files | Path of the module's types |
|---|---|---|---|
| Rust | The module name in snake case, Shop.Cart → shop_cart | <output-dir>/<name>.rs and <output-dir>/mod.rs, which declares every module of the table | <path>::<name> |
| Python | The same as Rust | <output-dir>/<name>.py | <path>.<name> |
| Go | The last component in lower case, Shop.Cart → cart | The package <name> in <output-dir>/<name>/ | <path>/<name> |
| TypeScript | Module components separated by slashes, Shop.Cart → Shop/Cart | <output-dir>/<name>.ts | A relative import between the generated files, including the .ts extension |
| PHP | The last component, Shop.Cart → Cart | PSR-4 classes in <output-dir>/<name>/ | <path>\<name> |
| Clojure | Module components in kebab case, separated by dots, Shop.LineItem → shop.line-item | The namespace <path>.<name> in <output-dir>, shop.gen.shop.line-item → <output-dir>/shop/gen/shop/line_item.clj | None: values are maps |
| C | The last component in snake case, Shop.LineItem → line_item, used as the C prefix | <name>.h, <name>.c, and the other files of leanfran --lang c module generate in <output-dir>/<name>/ | None: each module defines its own types |
For PHP, map the path namespace to output-dir in Composer's PSR-4 configuration. Use a TOML literal string such as path = 'App\Lean' for the namespace. The module name in the table, after applying as, determines the subdirectory, namespace suffix, and facade name. Supply an as value with a valid PHP identifier when the default name cannot be used.
For Clojure, output-dir is a source directory on the classpath, and each module becomes the same file as leanfran --lang clojure module generate --module-name <module> --clojure-namespace <path>.<name> writes. as replaces the name after path with dot-separated segments that start with a letter and contain letters, digits, -, and _. A Clojure namespace passes structures as maps, so it does not define types and does not need bind.
For C, each module becomes the same files as leanfran --lang c module generate --module-name <module> --prefix <name> --output-dir <output-dir>/<name> writes. Build its library in the Lake package with leanfran --lang c build --output-dir <output-dir>/<name>, as described in the C guide. as replaces the prefix with an ASCII identifier that starts with a letter, contains letters, digits, and _, and is neither lf nor leanfran. The names of a table differ in more than letter case.
A C module does not share types with the other modules of its table. Its header defines a C type with the module's prefix for each Lean type its functions use, so Shapes.Point is area_shapes_point in the area module and report_shapes_point in the report module. To pass a value from one module to another, encode it with the first module's <type>_to_json and decode it with the second module's <type>_from_json. A program can link the libraries of several modules of a package, as long as their prefixes differ and it links each Lean module once: two prefixes of the same Lean module define the same Lean initializer. Initialization of several libraries in one program follows Call the generated API.
leanfran.toml also holds the settings of sync, check and status. leanfran generate runs only the [[generate]] tables. Keep the output-dir of each [[generate]] table out of the sources patterns of [embed.c], because sync scans every file that they match.
The project file leanfran.toml has the settings of every language in the project.
[check]
require = "proved"
[[require]]
name = "mathlib"
scope = "leanprover-community"
[lake]
packages-dir = "../lake-packages"
[embed.python]
paths = ["src/myapp"]
[embed.typescript]
paths = ["src"]
tag-key = "ctor"
option = "null"
[embed.php]
paths = ["src"]
[embed.go]
paths = ["."]
[embed.c]
sources = ["src/*.c"]
lean-files = []
cflags = []
output-dir = "leanfran"
[embed.rust]
| Key | Default | Meaning |
|---|---|---|
[check] require | none | "proved" makes check and status work as with --require proved |
[[require]] | none | A Lake dependency with the keys of Lake's [[require]], for each language's workspace. name is required, and a relative path starts at the project root |
[lake] packages-dir | none | The directory of the Lake packages of the workspaces |
paths of [embed.python] | The package src/<name> or <name> of the project name in pyproject.toml | The packages |
paths of [embed.typescript] | ["src"] | The directories to scan |
paths of [embed.php] | The autoload.psr-4 directories of composer.json | The directories to scan |
paths of [embed.go] | ["."] | The directories to scan |
paths of [embed.clojure] | ["src"] | The directories to scan |
tag-key, option of [embed.typescript] | "ctor", "null" | The form of the generated TypeScript |
sources of [embed.c] | ["**/*.c", "**/*.h"] | The patterns of the C files to scan. The output directory and .git, .lake and .leanfran are left out |
lean-files, cflags, output-dir of [embed.c] | [], [], "leanfran" | Lean files added as blocks, compiler flags, and the output directory |
paths of [embed.rust] | ["."] | The directories of the crates |
Every key is optional. C needs the file. An unknown key, a value of another type and a missing name of [[require]] are errors with exit code 254, such as leanfran.toml:3:1: error: unknown key 'x'.
sync runs lake update in the workspace when the [[require]] tables or packages-dir change. When a package is, or depends on, Mathlib, it then runs lake exe cache get, which downloads the build cache of Mathlib; this needs curl, a C compiler and the leantar of the toolchain that elan installs, and a failure exits with 255. With MATHLIB_NO_CACHE_ON_UPDATE=1, sync does not run it.
The content of a package with a path is a part of the state of every block and function: after a change of one of its .lean files or of its lakefile.toml, status prints stale. This leaves out directories whose name starts with a dot and directories with a lakefile of their own, such as a package that the package requires with a path.
The packages go to .leanfran/<language>/workspace/.lake/packages, for each language. packages-dir of [lake] puts them in one directory, relative to the project root, that languages and projects share. Projects that share a directory must select the same revisions of the packages. packages-dir is not a part of the state of the blocks.
The project root is the nearest directory with leanfran.toml, from --dir (default: the working directory) upwards. Without one, it is the nearest directory with the package file of the language: Cargo.toml, pyproject.toml, package.json, deps.edn, composer.json or go.mod. A directory with neither exits with 254. leanfran.manifest.json, leanfran.lock, .leanfran/ and lean/Proofs/ are in the project root; Rust's .leanfran/rust/cache/ is in each crate.
A leanfran.toml in a directory above a project makes that directory the root. Give an independent project below it, such as a test fixture, its own leanfran.toml.
leanfran sync, leanfran check and leanfran status without --lang run the command for each [embed.<language>] table in the order of the file. They run the next language after a failure and exit with the largest code. Without a leanfran.toml or an [embed.<language>] table they exit with 252. Every language has its own section in leanfran.manifest.json and leanfran.lock, and a command for one language keeps the bytes of the others.
A proof with sorry in a Lean block gives a warning of sync, and the code is generated. check judges the theorems of every block and the proofs inside its definitions, and prints a row for each declaration that is not proved, before the rows of the functions:
app.facts:facts [assumed]: axioms
app.facts:facts [unfinished]: sorry
| Status | Meaning |
|---|---|
sorry | The declaration has a sorry, or is a theorem that rests on one |
axioms | It rests on an axiom other than propext, Quot.sound and Classical.choice (native_decide adds one) |
failed | The kernel rejects the theorem or a declaration that it uses |
stale | status only, as <block> [theorems]: stale: the block, a block that it uses, or a setting changed after the last check |
A definition is listed when its own value has a sorry or an axiom, or a proof that rests on one; a definition that calls it is not. An example is not a declaration, so it only gives the warning.
check writes the judged blocks and the rows to blocks and theorems of the language in leanfran.lock, and status prints them without running Lean. A failed or a stale row exits with 1, and with --require proved or [check] require = "proved" every row does. status --require proved also prints stale for the blocks of a project without a lock; without it, such a project has no rows.
leanfran --lang <language> theorems check --modules <Name> ... judges Lean modules of the search path.
check judges each function apart from the others. A function is unextracted when the translation does not support its code, when its marks are wrong, when Lean rejects its translation (a wrong type, a recursion without a proof that it ends, a limit such as maxHeartbeats), or when a tool that the translation runs stops at it. A function that calls an unextracted function is unextracted too. The other functions keep their results, and check exits with 1. Each unextracted function has a line with its reason on standard error:
src/calc.ts:24:10: error: unextracted src/calc.ts:power: the operator ** is outside the subset
src/calc.ts:28:8: error: unextracted src/calc.ts:cube: calls src/calc.ts:power, which is unextracted
src/calc.ts:33:8: error: unextracted src/calc.ts:label: Lean rejects the translation: Type mismatch
A function is also unextracted when a Lean block, or another module of the project that its translation imports, changes how Lean reads the translation:
instance : HMul UInt32 UInt32 UInt32, that the translation uses;macro_rules | `($a - $b) => …;notation:65 a " + " b => …), with an identifier or a term, or with a token such as [[ that starts with a bracket. A notation of your own starts with a token of its own, such as ⊞;Coe Nat Int or Coe Bool Prop.Macros for tactics change proofs only, so they count for none of this.
In a proof file with errors, the proofs without errors count, and a proof with an error, or one that uses such a declaration, is failed. When Lean cannot read the imports of a file, or the file starts with module, all its proofs are failed.
When the source itself does not parse, compile or load, check judges no function: it prints <file>:<line>:<column>: error: <message>, exits with 254 and leaves leanfran.lock as it is.
A function and its spec have the same types when the Lean types of the model are those of the spec, up to abbrev. Functions of other types build, and check, status and the random test report them:
| Types | check and status | Random test |
|---|---|---|
Lean converts them with a coercion (a fixed-width integer for a spec of Int, Option α for Option (Option α)) | The state of the proof. A proved function does not replace its spec | Fails when the language cannot pass the argument to the spec, or when the result is not a value of the spec's type. In Python, TypeScript, PHP and Clojure, numbers that are equal are the same value |
Lean does not convert them (a number for a spec of String, another number of arguments) | unextracted, exit code 1 | Fails |
The test of an argument that the spec does not take prints <function> [<label>]: the spec failed with the error, and that of a result of another type <function> [refines]: mismatch. The tests of other functions run:
app::wrong::text [refines]: the spec failed (seed 1, run 0)
input: x=0
error: argument 1 has type u32, the spec expects String
app::wrong::words [refines]: mismatch (seed 1, run 0)
input: x=0
expected: "0", got: 0
A generated Clojure function does not check its arguments, so a spec that computes a value for an argument of another type returns it.
status --output json prints the rows as one JSON object on standard output, with the exit code of --output text, which is the default:
{"blocks":[{"file":"src/app/_lean/arith.py","name":"app:arith","status":"missing"}],"functions":[{"id":"app:square","label":"refines","status":"proved"}],"theorems":[{"block":"app:arith","name":"unfinished","status":"sorry"}]}
A row of blocks has the status stale (with "changed":true when the content of the block differs, and without it when only a setting or a version does), missing (with the missing file) or removed. A row of theorems has a status of the theorems; a stale row has no name. A row of functions has a status of the lock, stale or removed; a removed function has no label. Without --lang, the object has one such object for each language, as in {"go":{"blocks":[],"functions":[],"theorems":[]}}; a language whose command fails has none.
The proofs of all languages are in lean/Proofs/. The check of a language does not build these files:
LeanPythonModel or LeanRustGen), directly or through other proofs;[[require]].A [[generate]] table shares one definition of each supported Lean type, except in Clojure and C. The module that declares a structure or inductive type without parameters generates its definition, even when the module has no public functions. A concrete type with parameters, such as Packet Status, goes in the first module that needs it. A type declared in a module outside the table follows the same rule.
The compiler generates modules in Lean's import order and uses the path and name of the definition already generated for each type. A module can use types from another module with a parent or child namespace. The module where Lean declares a type determines its ownership, regardless of the type's namespace.
Use bind to refer to types defined elsewhere, including types generated by another table. When a type matches a binding, the binding takes priority over the definition in the table. The longest matching namespace wins, and a module's bind overrides the table's binding for the same namespace.
TypeScript resolves relative bind specifiers from the generated file that imports them. Use module-level bind when files in different directories need different relative specifiers.
The summary lists the modules of each Lake package and language in their configured order after glob expansion. --output text prints one line per module, and leanfran: skipped <name>: <reason> on standard error for each definition or type without generated code. --output json prints one array for all languages, in the form that module generate --output json prints: module, files, functions, externs, skipped, data, and error for a module that failed.
0 success; 252 a missing leanfran.toml, one without [[generate]], or invalid parameters; 254 an error in the project, such as an invalid leanfran.toml (leanfran.toml:<line>:<column>: error: <message>), a missing lake-dir, a failed lake build, or a module that cannot be converted; 255 other errors, such as a missing lake or a Lake package that uses another Lean version. The modules after a failed one are still generated, and the command exits with the largest code of its languages.
Can you improve this documentation? These fine people already did:
conao3 & Naoya YamashitaEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |