Add support for the language requested by the contributor to leanfran.
Read AGENTS.md, CONTRIBUTING.md and the existing backend development guides first.
For a backend that translates Lean IR, follow the existing emitters and
host-language runtimes. Match Lean's runtime semantics and extern conventions.
Give the backend a Lake project under langs/<LANGUAGE>/ for its tests and oracles, and
compare its public calls with the native Lean oracle. Put its command line in the module
Lean<X>.Cli: a command table of Leanfran.Cli with module generate, which
Leanfran.Module.command runs with the emit function of a Leanfran.Module.Language value,
and obligations check and theorems check, which Leanfran.Check.command and
Leanfran.Check.theoremsCommand run with a policy. Add its library to
the root lakefile.toml and to the leanfran package of flake.nix, and its --lang value to
the root Main.lean and to maint/test-cli.sh. Write
the expected command-line output of the language in maint/cli-expect/<language>.txt (the format is in
maint/cli-expect/lib.sh). Add host-language tests, runnable examples
and documentation covering setup, usage and supported features.
To embed Lean blocks in host-language sources, join leanfran --lang <language> sync,
check and status. Leanfran.Embed (Leanfran/Embed/) runs them for every language: it
writes the Lake workspace in .leanfran/<language>/workspace, builds the blocks, maps Lean
errors to the host source, generates and installs the files, and writes
leanfran.manifest.json and leanfran.lock. The backend supplies two things:
Leanfran.Embed.Backend value in Lean<X>/Embed.lean. It names the language and the
package file (such as go.mod) that marks the project root without leanfran.toml, launches
the language package, and gives the
workspace lakefile.toml, the Lean support modules and the name of the library Lean<X>Csimp (see below). Lean<X>.Cli adds
Leanfran.Embed.commands of this value to its command table, and languages of
Leanfran/Config.lean lists the keys of its [embed.<language>] table and its Lean libraries.
Generated files start with the line of Leanfran.Header.text: sync removes and overwrites the
files of the manifest and those whose line names a module of these libraries.scan and translate of the language package. Both take no arguments and
read one JSON object with the project root as root from standard input. scan also
receives the table [embed.<language>] of leanfran.toml as config (an empty object
without one) and the [[require]] tables as require, reads the project's sources and
prints the blocks, their source positions and hashes, and the functions to verify as one
JSON object. The hash of a block is of its content. The build, the loading program and the random tests compare
this content with the key in the generated code and stop when it differs. Neither has the generator version, since a user
cannot sync the generated code of a dependency: status reports code of another version as stale. The data of scan is passed on to translate and to the fresh function of the Backend,
which decides whether the state of a function in the lock still holds. A block whose Lean imports the translation of a function has
"needs_models": true, and may list the ids of the functions that it needs as "functions":
the block is stale when one of them changes, or any function without the list. translate also receives that result as scan
and the generation summaries as blocks, and prints the Lean translation of the
functions: the modules with the ids of their functions, the aggregates that the proofs import, and an error for each function that it
cannot translate. Lean rejects a module for all its functions, so a module has one function, or parts (text, and the
ids of the functions that need the part, with those that call it) between its text and its tail: a part that Lean
rejects is left out with the parts of its functions. translate catches every error of one function, and leaves out those that call it. maint/test-isolation.sh checks this. A block that the package adds to those of the sources may have "theorems": false: check judges no theorems of it. sync builds, generates and installs the blocks without needs_models first,
then runs translate and does the same with the others and with the blocks that use them;
blocks has only the blocks of the first stage, and the lakefile must build them without
the modules of the second stage. A sync that the language starts while it compiles or loads the project, with
LEANFRAN_AUTO_SYNC=1, gets LEANFRAN_SYNC_MODELS=0 when translate would start that again: it ends after the first
stage and leaves the others stale. check then judges the theorems of every block with
theorems check: the workspace must not turn warnings into errors, since a sorry of a block is a
warning of sync, and the runtime must define lean_sorry. Both run in the project root, write no files outside .leanfran/<language>/, compare
LEANFRAN_VERSION, LEANFRAN_GENERATOR_VERSION and LEANFRAN_LEAN_VERSION with the
package's own constants, and exit with 0, with 252 for arguments or an input that they
cannot read, with 254 for an error in the project
(<file>:<line>:<column>: error: <message> on standard error), or with 255 for an error in
the environment. Other messages start with leanfran:. An unreadable input is
leanfran: the input of <command> is not JSON: <reason> or
leanfran: the input of <command> must be an object with the string "root".Generated code can call a function of the language that proves to compute a spec of a block. translate returns
"csimp": [{"label", "spec", "extern"}] with a function that may replace its spec: the obligation that must be
proved, the Lean name of the spec, and the string of @[extern], <scheme>:<target>. The generated code must be
able to call the function, its arguments and result must convert in both directions, and a function with requires
has none. Policy.replaces of the backend's Lean<X>/Cli.lean can judge these conditions in Lean instead, as Clojure
does for the conversions of the types of the spec. translate does not compare the types of the function and the spec:
obligations check (sameValues of Leanfran/Check.lean) reads them from the obligation ∀ xs, model xs = spec xs,
whose right side is in a constructor of Policy.ok for a model that may fail. check records those of the same types
whose model does not refer to any candidate replacement in the lock, with the module of the spec. The
obligation must cover all spec arguments in order and the types must match after unfolding reducible
definitions; the only result exception is an Int model for a Nat spec. When several functions qualify,
the smallest function id wins. Then
Leanfran/Embed/Csimp.lean writes the modules Lean<X>Csimp.<module of the spec> (an @[extern] definition and an
rfl @[csimp] theorem for each spec) for the functions that status prints as proved. Batch.run passes them as
--import; the lakefile builds the library by default, and languages of Leanfran/Config.lean lists it. The
manifest has the hash of the modules that a block depends on as the support of the block.
The emitter applies the lemmas with Leanfran/Csimp.lean: replacements gives the specs whose replacement is an
extern of the scheme, substDecl replaces their calls in the IR, and reaches and specDecls give the copies that keep the
Lean bodies. A bridge of a replaced spec converts the arguments, calls the function and converts the result. It calls the
copy when a value is outside the range of the function's types. The random tests call the copies of the specs. A spec
of other types than the function leaves the build and the other tests as they are: its test fails with the spec failed
for an argument that the spec does not take, and mismatch for a result of another type.
C has no emitter of its own: LeanC/Embed/Csimp.lean replaces the calls in the C files that Lake built, and the Lean
bodies are in a second library.
The Go backend is the example: langs/Go/LeanGo/Embed.lean holds its Backend, and
langs/Go/cmd/leanfran-go/ holds scan and translate. maint/test-embed.sh runs the
shared flow with the fake package tests/embed/fake.sh; the scan and translate results
that the script writes for it show the JSON that the two commands exchange with the compiler.
maint/test-theorems.sh checks a block with unproved theorems in a new project of each language,
maint/test-freshness.sh when the generated code of a block is out of date, and maint/test-csimp.sh the functions
that replace their specs, and functions of other types than their specs.
At build, load or property-test time, reject missing or stale blocks and tell the user
to run leanfran --lang <language> sync. Only opt-in LEANFRAN_AUTO_SYNC=1 can
run sync automatically; do not reenter translation from a host load.
Use LEANFRAN_CLI to locate the compiler, and leave lock-based proof gates to check
and status. Judge theorems in each block through the common checker, including proofs
inside definitions; an unproved theorem is not a reason to omit the block's generated code.
Property tests run 200 accepted inputs by default, seed 1, with LEANFRAN_RUNS and
LEANFRAN_SEED overrides. Include boundary values, reject inputs whose precondition
fails, and stop after 100 discards per requested run. Compare the host result to the
unreplaced Lean specification, then evaluate postconditions in their written order.
Retain the initial run number and shrink the input without changing the failure's label
or reason. Print failed: <reason> as the result of a failed host call, and continue
testing the remaining functions.
A backend that uses Lean's native emitter and runtime should exercise the API and build integration it supplies. See the C development guide for checks covering wrappers, initialization, ownership and linking.
Give the backend's Makefile the targets build, test, test-generate, check-samples,
check-verify, check-cli, check, format, format-check and gen-externs; a target
without work in this backend has no recipe. check runs the checks and format-check.
Each samples/Makefile exposes test to run the samples and verify to run
leanfran --lang <language> check --require proved in the verified projects and use
git diff --exit-code to check their committed outputs, manifests and locks.
maint/test-make-targets.sh checks both sets of target names. List the expected command lines of
leanfran --lang <language> and of the language package in
maint/cli-expect/<language>.txt, and add the language to maint/test-cli.sh,
maint/test-host.sh (check-cli) and maint/test-generate.sh (test-generate).
Write docs/<language>-guide.md with the sections Install, Quick start, Lean blocks,
Types, Generated code, Commands, Configuration, Verifying functions and
Limits, in this order. Samples go under Quick start, performance and the runtime under
Generated code, and the verification section has Marking a function,
Obligations and proof status and Random tests. Explain the proof gate, replacement lifecycle,
the original Lean body, and the host types that cannot be replaced. langs/<LANGUAGE>/README.md has Install,
First Lean block and Next steps, and docs/<language>-development.md has Layout,
Conventions, Testing and Publishing. Use the words "Lean block", "generated code",
"model" (the Lean translation of a function), "obligation", "workspace"
(.leanfran/<language>/workspace) and "project file" (leanfran.toml) in them. Describe the
[embed.<language>] table in the settings of docs/generate-guide.md.
Integrate the backend into the shared Nix environment, root Makefile and CI. Add
the backend and its development guide to CONTRIBUTING.md and update the
repository layout in AGENTS.md.
List the language in the root README: the registry badge in the header, the
language and documentation tables, and the install notes in Get started.
Run the relevant build, test and formatting checks, and report their results
along with any remaining limitations for review.
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 |