[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"JTy49LolEC":3},"# LeanElpi\n\n**λProlog inside Lean 4.** Write tactics, commands and proof search as logic programs, share\ntypes and functions with Lean in both directions, and get proofs the Lean kernel checks.\n\nLeanElpi is a clean-room, native Lean 4 implementation of the [ELPI](https://github.com/LPCIC/elpi)\ndialect of λProlog, the language behind Coq-Elpi. It embeds in Lean as a metaprogramming tool (commands, tactics,\n`MetaM` builtins), works as a pure library for ordinary Lean programs, ships a\nstandalone `lean-elpi` binary, and adds an opt-in LF layer with kernel-checked proof\nterms.\n\n**Status:** version 0.3: the engine, constraints, Lean integration, the LF layer and the Lean/ELPI type bridge are implemented. Documentation site and book: https://volland.github.io/lean-elpi/ (sources in `site/`, built into `docs/` with `python3 site/build.py`).\n\n## Use it in your project\n\nAdd LeanElpi as a Lake dependency (Lean 4.35.0, no Mathlib needed):\n\n```lean\n-- lakefile.lean\nrequire «lean-elpi» from git \"https://github.com/Volland/lean-elpi\" @ \"v0.3.3\"\n```\n\nor in `lakefile.toml`:\n\n```toml\n[[require]]\nname = \"lean-elpi\"\ngit = \"https://github.com/Volland/lean-elpi\"\nrev = \"v0.3.3\"\n```\n\nThen `import Elpi`:\n\n```lean\nimport Elpi\nopen Elpi.Cmd\n\n-- a Lean type, shared with ELPI\ninductive Tree (α : Type) where\n  | leaf\n  | node (l : Tree α) (a : α) (r : Tree α)\n  deriving Elpi\n\n-- a Lean function, callable from ELPI\ndef twice (n : Nat) : Nat := 2 * n\nelpi_extern twice\n\nelpi_program demo\n#elpi demo lp%{ twice 21 X }%     -- X = 42\n```\n\n## From Python, Node.js and Rust\n\nOne native library serves three bindings: load a program once, extend it, query it many times.\n\n```bash\npip install leanelpi        # Python\nnpm install @lean-elpi/leanelpi   # Node.js\ncargo add leanelpi          # Rust (needs the native library, see the guide)\n```\n\n```python\nfrom leanelpi import Program\nProgram(\"pred n o:int.\\nn 1.\\nn 2.\").solve(\"n X\")   # two solutions: X = 1, X = 2\n```\n\nmacOS and Linux, no Lean installation needed. Guide: [From Python, Node.js and Rust](https://volland.github.io/lean-elpi/book/bindings/); release steps: [PUBLISHING.md](PUBLISHING.md).\n\n## What it does\n\n- **ELPI engine in Lean**: typed clauses, `pi`/`=>`, higher-order pattern unification, modes, constraints and CHR, a command-line tool, and a lazy library API.\n- **Lean integration**: `{{ … }}` quotations, tactics and commands written in λProlog, `lean.*` builtins over `MetaM`, a typed rule DSL.\n- **Type bridge** (new in 0.2): `deriving Elpi` for parametric and recursive types, `elpi_data` (ELPI types to Lean inductives), `elpi_extern` (typed Lean functions as ELPI predicates), `lean.env.indt` / `lean.add_inductive` (read and create inductive types from ELPI).\n- **Bindings**: Python, Node.js and Rust packages over a C interface, with the Lean runtime inside one shared library.\n- **LF layer**: dependently typed signatures, recorded proof terms, export to Lean inductives, kernel-checked certificates.\n\n## Performance and compatibility\n\nMeasured against ELPI 1.18 on ELPI's own performance programs (`tests/bench`), all 24 of them run on release 0.3.3 (11 were rejected by 0.3.2). It is about 2.4 times slower by geometric mean over the 20 programs where ELPI takes more than 0.1 s, 6.2 times at worst, and faster than ELPI on `cbv`, on start-up and on the smallest programs; release 0.3.2 was 2.3 times slower on the programs it could read, and 0.3.0 was 31 times. It passes 52 of the 114 upstream ELPI tests that apply and 38 of 40 corpus programs. Memory is 50 to 200 MB, except `map` on lists (1.4 GB: nothing reclaims variables in a long run). Details, the known failures and how to reproduce: the blog posts [compatibility and performance](https://volland.github.io/lean-elpi/blog/compatibility-and-performance/) [why it was slow](https://volland.github.io/lean-elpi/blog/why-leanelpi-was-slow/) and [every performance program runs](https://volland.github.io/lean-elpi/blog/every-performance-program-runs/) and [what we took from ELPI's runtime](https://volland.github.io/lean-elpi/blog/what-we-took-from-elpi/).\n\nSee [CHANGELOG.md](CHANGELOG.md) for releases and [CONTRIBUTING.md](CONTRIBUTING.md) to help.\n\n## Where to read\n\n| You want | Read |\n|---|---|\n| The big picture | `tg.md/overview.md`, then `tg.md/papers/system.md` |\n| Why things are the way they are | `tg.md/decisions.md`, `tg.md/alternatives.md` |\n| Module layout and UML | `tg.md/architecture/` |\n| Exact requirements | `openspec/changes/*/specs/` |\n| The plan per milestone | `openspec/changes/\u003Cchange>/{proposal,design,tasks}.md` |\n\nThe docs form a typed knowledge graph read with the `tg` CLI:\n\n```bash\ntg search \"pattern unification\"\ntg section \"decisions#Persistent store\"\ntg trace --gaps          # what is not implemented or verified yet\ntg check && tg validate  # links, structure, typed edges\n```\n\n## Build\n\n```bash\nlake build                        # library + lean-elpi executable (Lean 4.35.0)\ntests/conformance/run.sh record   # store ELPI 1.18 oracle answers (needs opam elpi)\ntests/conformance/run.sh          # compare lean-elpi against the oracle\n```\n\n## License\n\nApache-2.0, clean-room: no upstream ELPI (LGPL-2.1) code is ported or vendored.\n",1793400790979]