Skip to main content

leanelpi (Python)

Run ELPI (λProlog) programs on the LeanElpi engine from Python. Needs libleanelpi; build it with scripts/build-ffi.sh and point LEANELPI_LIB at it, or leave it in dist/.

from leanelpi import Program

p = Program("pred n o:int.\nn 1.\nn 2.")
[s["X"] for s in p.solve("n X")]   # ['1', '2']
[s.terms["X"] for s in p.solve("n X")]   # [1, 2]

Solution.bindings (and solution["X"]) hold the values as ELPI prints them; Solution.terms holds them as Python values: int, float, str, a list for an ELPI list, Const("anna"), App("pt", (1, "x")), Var(name) for an unbound variable and Opaque(printed) for anything else (λ-terms).

Host predicates (0.4)

declare makes a predicate whose calls Python answers during solve_host, so rules can ask an LLM or a tool and still decide the conclusion themselves:

from leanelpi import Program, Const, ElpiError

world = Program("""
kind person type.
type anna, marco person.
pred knows i:person, i:person.
knows anna marco.
pred same_person i:string, o:person.
pred believable i:string, i:string.
believable A B :- same_person A PA, same_person B PB, knows PA PB.
""").declare("pred same_person i:string, o:person.")

def same_person(name):                        # an LLM or a tool would go here
    return [Const(c) for c in {"Anya": ["anna"], "Marko": ["marco"]}.get(name, [])]

world.solve_host('believable "Anya" "Marko"', {"same_person": same_person})   # one solution
world.first_host('same_person "Anya" P', {"same_person": same_person}).terms   # {'P': Const('anna')}
world.solve_host('believable "Anya" "Marko"', {"same_person": lambda n: [Const("maria")]})
# ElpiError: host predicate 'same_person': unknown constant 'maria'
  • Program.declare(signature, name=None) returns a new program (the old one is unchanged). The program may already declare the predicate, so its rules can call it, but must not have clauses for it. Querying it with plain solve raises ElpiError.
  • Program.solve_host(query, host, *, limit=None, fuel=None) returns the solutions; solve_host_all returns a HostRun(solutions, exhausted, fuel_used) and first_host the first Solution or None.
  • host is a dict {name: function}, where the function receives the i: arguments as Python values, or one function host(name, args) receiving every argument (unbound ones as Var). It returns an iterable of rows, one tuple of o: values per answer, in order (a bare value is a one-output row; wrap a list output as ([1, 2],)). An empty iterable or None means the call fails; True means it succeeds once (a predicate with no outputs). Return int, float, str, bool (tt/ff), Const, App or a list.
  • An exception in the host stops the run and raises ElpiError with the exception as __cause__; nothing unwinds through C. KeyboardInterrupt and SystemExit are re-raised.
  • The engine rejects a constant the program does not know. It does not type-check host answers, so the binding rejects values that cannot have the declared type (a str for o:person, a constant declared as another kind) and turns an int for o:float into a float.
  • Calls are synchronous, on the thread that called solve_host; a host may call the engine again, and several threads may run solve_host at once. ElpiError.out_of_fuel tells a spent fuel= budget from other errors.

See the guide.

Metadata

Release files for leanelpi 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for leanelpi 0.4.1
File Actions
leanelpi-0.4.1-py3-none-manylinux_2_35_x86_64.whl Python 3 none Linux glibc 2.35+ x86-64 Details for leanelpi-0.4.1-py3-none-manylinux_2_35_x86_64.whl
leanelpi-0.4.1-py3-none-manylinux_2_35_aarch64.whl Python 3 none Linux glibc 2.35+ ARM64 Details for leanelpi-0.4.1-py3-none-manylinux_2_35_aarch64.whl
leanelpi-0.4.1-py3-none-macosx_11_0_x86_64.whl Python 3 none macOS 11.0+ x86-64 Details for leanelpi-0.4.1-py3-none-macosx_11_0_x86_64.whl
leanelpi-0.4.1-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for leanelpi-0.4.1-py3-none-macosx_11_0_arm64.whl

Total release size: 211.8 MB

Release files / leanelpi-0.4.1-py3-none-manylinux_2_35_x86_64.whl

Download URL leanelpi-0.4.1-py3-none-manylinux_2_35_x86_64.whl
Size 57.8 MB
Tags Linux glibc 2.35+ x86-64 Python 3
SHA-256 checksum
How to use checksums
852db5b51c8069be7e20d2de1d38a098e1ae309cb43b7efd84a43ee009dce556
BLAKE2b-256 checksum
How to use checksums
247188029284c2e9ff090d3c9f44928e815d977cdbbee4ceb2ca706dfc2d539d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Release files / leanelpi-0.4.1-py3-none-manylinux_2_35_aarch64.whl

Download URL leanelpi-0.4.1-py3-none-manylinux_2_35_aarch64.whl
Size 59.8 MB
Tags Linux glibc 2.35+ ARM64 Python 3
SHA-256 checksum
How to use checksums
d4507f45a74ff684d5da25a22df127f66e5cab4aba1d619a9e7111a43e89b052
BLAKE2b-256 checksum
How to use checksums
9f12d5f68684b5a4a1021ec01a35bbfb3dbbcdfac9dc0b2d98bc18e23eaeb44c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Release files / leanelpi-0.4.1-py3-none-macosx_11_0_x86_64.whl

Download URL leanelpi-0.4.1-py3-none-macosx_11_0_x86_64.whl
Size 49.1 MB
Tags Python 3 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
f72382c50d2644a1e280bd46609ade07e778af2c7eed6c59d07d8ef0c6a90ae7
BLAKE2b-256 checksum
How to use checksums
d12c69f1e4cd6d24cae8a5f36cea9cb22da046d1945657aeeaac9ef31e3d1713
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Release files / leanelpi-0.4.1-py3-none-macosx_11_0_arm64.whl

Download URL leanelpi-0.4.1-py3-none-macosx_11_0_arm64.whl
Size 45.0 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fe8b3f3ec80a5e86627a474a4a0585f1b74bd0574bd9429120a33b5f5d3bab75
BLAKE2b-256 checksum
How to use checksums
1bbb5eca87c1d04f24eecad21136113c14802e9980bc86bca5a159f66c80e8c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

Security for leanelpi 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for leanelpi-0.4.1-py3-none-manylinux_2_35_x86_64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 31, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page