Liking cljdoc? Tell your friends :D

Python development

leanfran compiles Lean 4 IR to Python (LeanPython/, Main.lean), exposes Lean types as pydantic models, and embeds Lean in Python (src/leanfran/embed.py, sync.py). 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. LeanPython/Generate.lean: public functions. Main.lean: the leanfran-python CLI.
  • 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, sync.py, models.py, cli.py: lean(...) and leanfran sync.
  • 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).

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.
  • Raise generatorVersion in Main.lean and GENERATOR_VERSION in src/leanfran/embed.py together when the generated code changes.

Verification

nix develop ../..#python --command make check

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