Liking cljdoc? Tell your friends :D

Python development

leanfran compiles Lean 4 IR to Python (LeanPython/, LeanPython/Cli.lean), exposes Lean types as pydantic models, and embeds Lean in Python (src/leanfran/embed.py, scan.py, LeanPython/Embed.lean). See the backend README for usage.

Paths in this guide are relative to langs/Python/.

Layout

  • LeanPython/EmitPy.lean: the emitter. Collects IR decls from roots and emits Python functions (join points as a while True state machine, reference counting as in C).
  • LeanPython/Marshal.lean: Lean types → descriptors, pydantic models, decision procedures of proof fields, and the bridges of py: externs. LeanPython/Generate.lean: public functions. LeanPython/Cli.lean: the commands of leanfran --lang python.
  • src/leanfran/runtime.py, io_externs.py: runtime. lean_* functions are named after the C symbols of Lean externs; LeanPython/RuntimeExterns.lean lists them (make gen-externs), and the CLI refuses to emit code calling a symbol not in the list.
  • src/leanfran/marshal.py: conversion between Python and runtime values, LeanModel, LeanInductive (Lean's JSON encoding), adopt for the user's models.
  • src/leanfran/embed.py, models.py: lean(...) and the Lean structures of the user's models.
  • src/leanfran/cli.py, scan.py, translation.py: the commands scan and translate of python3 -m leanfran.cli. They print JSON and write no file.
  • src/leanfran/verify.py, translate.py: @model, check_model and the translation of Python functions; src/leanfran/lean/LeanPythonLib/Py.lean holds Python's semantics in Lean.
  • LeanPython/Embed.lean: the Python Backend of Leanfran.Embed (../../Leanfran/Embed/), which runs leanfran --lang python sync|check|status: it starts scan and translate, writes the Lake workspace, runs lake, generates the modules, installs them, and writes leanfran.manifest.json and leanfran.lock.
  • LeanPythonTest/: Lean test programs. Vectors.lean and Io.lean hold the diff-test cases; *Main.lean are the native oracles. Public.lean, Json.lean: the public API and JSON tests.
  • tests/: pytest. test-out/ holds generated code (ignored by git).

The emitter replaces the calls of a spec whose @[csimp] lemma names a py: extern (plan of EmitPy.lean). A bridge has the name of the extern and the parameters of the spec, with the ownership that the callers were compiled for; it converts with the descriptors to_py and to_lean, and calls the copy <spec>._leanfran_spec when lean_py_extern returns None. ModelD finds the user's model at the call, so a generated module imports no module of the user for a bridge. The deciders of proof fields and the attribute spec of a public function use the copies. translation.replaces says which functions replace a spec.

Conventions

  • The root ../../lean-toolchain matches nixpkgs-unstable's lean4.
  • When the CLI reports externs not in the runtime, find the extern in the Lean sources (grep -rn 'extern "<symbol>"' under the toolchain's src/lean), implement it following lean.h (same argument order, erased arguments dropped; never consume a borrowed argument; inc values read out of containers), and run make gen-externs.
  • New runtime behaviour gets a case in LeanPythonTest/Vectors.lean or Io.lean; the diff test must match the C backend byte for byte. New public types get a test in tests/test_public.py, and a JSON case in LeanPythonTest/Json.lean when they derive ToJson.
  • translate.py translates an operation only for types whose Lean operation returns Python's value; it refuses the others. An operation that the translation accepts gets a function in tests/ops/ops/kept.py (test_operations runs the Lean definition as Python and compares it with the function on boundary values), one that it refuses gets a function in refused.py whose docstring is the message, and a boundary value of the random tests gets a function in wrong.py.
  • Raise generatorVersion in LeanPython/Cli.lean and GENERATOR_VERSION in src/leanfran/embed.py together when the generated code changes.

Testing

nix develop ../..#python -c make check   # treefmt, the diff tests against the C backend, pytest, the samples
nix develop ../..#python -c make bench

../../maint/Python/test-diff.sh runs Vectors and Io natively and as Python.

Publishing

The Release and Publish workflows release every backend under one version, as CONTRIBUTING.md describes. ../../maint/Python/bump-version.sh writes the version, and the Publish job publishes the package to PyPI with trusted publishing.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close