Skip to main content

Bidirectional Lean4-Python transpiler

Project description

LP² (lp2) — Lean ↔ Python, squared

LP² = LP² = LP × LP = Lean ↔ Python, squared for bidirectional. Translates in both directions: Python→Lean and Lean→Python.

Python Ruff

Translate Python functions with type hints into Lean 4 definitions, and Lean 4 definitions into Python functions.

Install

pip install lp2

Usage

CLI

# Python → Lean4
lp2 py2lean my_file.py
lp2 py2lean --stdin < my_file.py

# Lean4 → Python
lp2 lean2py my_file.lean
lp2 lean2py --stdin < my_file.lean

Python API

from lp2 import py_to_lean, lean_to_py, convert_str, convert_file

lean_code = py_to_lean("def add(x: int, y: int) -> int:\n    return x + y\n")
py_code = lean_to_py("def add (x y : Int) : Int := x + y\n")

# Convenience
result = convert_str(source, "py2lean")
result = convert_file("input.py", "py2lean")

Examples

Python → Lean4:

def fib(n: int) -> int:
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)
def fib (n : Int) : Int :=
  if n  1 then n else fib (n - 1) + fib (n - 2)

Lean4 → Python:

def isZero (n : Int) : Bool := match n with
  | 0 => true
  | _ => false
def isZero(n: int) -> bool:
    match n:
        case 0:
            return True
        case _:
            return False

Development

pip install -e ".[test]"
pytest
ruff format src/ tests/
prospector --with-tool ruff --with-tool mypy src/

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lp2-0.1.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lp2-0.1.0-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

File details

Details for the file lp2-0.1.0.tar.gz.

File metadata

  • Download URL: lp2-0.1.0.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lp2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f2dbda90a1fd0e51d5aebf80eef9e02a70f8ac7d4ed380956611105e2fce8253
MD5 74a4099ef2ea5309432faa312b853cdf
BLAKE2b-256 007c37e3d50a32bed1051d36dd3d7d434e0bfcfabc8564fcae0764fec25b3069

See more details on using hashes here.

Provenance

The following attestation bundles were made for lp2-0.1.0.tar.gz:

Publisher: pypi-publish.yml on daedalus/lp2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lp2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lp2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lp2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4fb14b5861f6a94300d6d4e3cb15bd75e6e92ea66fccd9133fc03ef52267e8d
MD5 8a7020aec666afe4ec253b0d8ae47acc
BLAKE2b-256 f65d62426aadc1b1c911bb7a0730021a0b43d55073379d0a07c86ec03467016c

See more details on using hashes here.

Provenance

The following attestation bundles were made for lp2-0.1.0-py3-none-any.whl:

Publisher: pypi-publish.yml on daedalus/lp2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page