Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ jobs:
run: uv sync --dev

- name: Install demo dependencies (sympy, numpy)
run: uv pip install sympy numpy
run: uv pip install sympy numpy "patchelf; sys_platform == 'linux'"

- name: Build root Lake project
run: lake build
run: |
python3 scripts/prepare_lean435.py
lake build

- name: Build TestLib (test fixture)
run: lake build TestLib:shared
run: |
python3 ../../scripts/prepare_lean435.py
lake build TestLib:shared
working-directory: tests/lean

- name: Inspect tests/lean build output (diagnostic)
Expand Down Expand Up @@ -99,10 +103,12 @@ jobs:
uses: astral-sh/setup-uv@v5
- run: uv python install 3.12
- run: uv sync --dev
- run: uv pip install sympy numpy
- run: uv pip install sympy numpy "patchelf; sys_platform == 'linux'"
- name: Build
run: |
python3 scripts/prepare_lean435.py
lake build
python3 scripts/prepare_lean435.py tests/lean
(cd tests/lean && lake build)
- name: leaks(1) check
run: tests/leaks_check.sh
Expand All @@ -129,10 +135,12 @@ jobs:
uses: astral-sh/setup-uv@v5
- run: uv python install 3.12
- run: uv sync --dev
- run: uv pip install sympy numpy
- run: uv pip install sympy numpy "patchelf; sys_platform == 'linux'"
- name: Build
run: |
python3 scripts/prepare_lean435.py
lake build
python3 scripts/prepare_lean435.py tests/lean
(cd tests/lean && lake build)
- name: Run tests under ASAN (LD_PRELOAD)
run: |
Expand Down Expand Up @@ -173,10 +181,12 @@ jobs:
uses: astral-sh/setup-uv@v5
- run: uv python install 3.12
- run: uv sync --dev
- run: uv pip install sympy numpy
- run: uv pip install sympy numpy "patchelf; sys_platform == 'linux'"
- name: Build
run: |
python3 scripts/prepare_lean435.py
lake build
python3 scripts/prepare_lean435.py tests/lean
(cd tests/lean && lake build)
- name: valgrind check
run: tests/leaks_check.sh
Expand Down
8 changes: 5 additions & 3 deletions LeanPy/Kernel.lean
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,11 @@ def decideProp (src : String) : IO String := runCore do
let expr ← Lean.instantiateMVars expr
let dty ← Meta.mkAppM ``Decidable #[expr]
let dec ← Meta.synthInstance dty
let r ← Meta.whnf dec
if r.isAppOf ``Decidable.isTrue then return "true"
else if r.isAppOf ``Decidable.isFalse then return "false"
-- Lean 4.35 represents Decidable as a structure containing a Bool.
-- isTrue/isFalse are abbreviations, not constructor heads.
let r ← Meta.whnf (mkApp2 (mkConst ``Decidable.decide) expr dec)
if r.isConstOf ``Bool.true then return "true"
else if r.isConstOf ``Bool.false then return "false"
else return "<undecided>"
catch e =>
return s!"<elab: {← e.toMessageData.toString}>"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Effortless interop between **Lean 4** and **Python**, in both directions.

This branch targets Lean 4.35.0. Before each Lake project's first build, run
`python3 scripts/prepare_lean435.py /path/to/project` from this checkout.
It applies a checked proof-only workaround to the pinned Regex dependency;
plain `lake build` currently fails without it. See the
[kernel bridge example](examples/kernel/README.md) for importing an existing
Mathlib project's compiled proofs from Python.

* **Lean -> Python.** Annotate any Lean definition with `@[python "name"]`
and call it from Python with automatic type marshalling.
`derive_python` exposes inductives and structures as Python constructors.
Expand Down Expand Up @@ -441,7 +448,9 @@ Each is a self-contained Lake + uv project.

```bash
uv sync --dev
python3 scripts/prepare_lean435.py
lake build
python3 scripts/prepare_lean435.py tests/lean
cd tests/lean && lake build TestLib:shared && cd ../..
uv run pytest tests -v
```
Expand Down
4 changes: 2 additions & 2 deletions examples/01_basic/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/01_basic/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions examples/02_pantograph_kernel/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/02_pantograph_kernel/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions examples/03_numpy_typed/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/03_numpy_typed/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions examples/04_sympy_tactic/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/04_sympy_tactic/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions examples/05_knuckledragger/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/05_knuckledragger/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions examples/06_effectful_verifier/lean/lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion examples/06_effectful_verifier/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
37 changes: 37 additions & 0 deletions examples/kernel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Inspect an existing Lean project

This bridge loads compiled `.olean` modules through `Kernel`. It does not link
the inspected project's native libraries or `Mathlib:static`. Build the project
first, using Lean 4.35.0. Declaration lookup and proof tactics then operate on
its imported environment. Native evaluation can require additional libraries.

From the lean.py repository:

```sh
python3 scripts/prepare_lean435.py
lake build
python3 scripts/prepare_lean435.py examples/kernel/lean
(cd examples/kernel/lean && lake build KernelBridge:shared)
uv sync --dev
uv run python examples/kernel/inspect_project.py /path/to/project \
--module MyProject.Proofs \
--declaration MyProject.my_theorem
```

The helper resolves each Lake search-path entry against the inspected project.
The bridge imports `LeanPy.Kernel` and `LeanPy.Export`. It does not import the
`LeanPy` umbrella or request a Regex archive, although building the existing
LeanPy static library still builds its Regex dependency.

## Temporary dependency workaround

Regex `v4.34.0-rc1` triggers a Lean 4.35 `grind` panic in
`Regex.Data.SparseSet.Bijection.surj_of_inj`. Upstream has no Lean 4.35 tag.
`prepare_lean435.py` runs `lake update`, verifies the exact source SHA-256,
and replaces one tactic with `apply Fin.ext; grind -hom [Fin.ext_iff]`.
The theorem statement is unchanged. Lean checks the resulting proof.

Run the helper for each Lake project before building it. It is idempotent and
refuses unexpected source. A plain first `lake build` without this preparation
still fails in Regex. Remove this workaround when a compatible upstream
revision is available.
41 changes: 41 additions & 0 deletions examples/kernel/inspect_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Inspect compiled Lean declarations without linking the project's libraries."""

import argparse
import json
import os
import subprocess
from pathlib import Path

from lean_py import LeanLibrary
from lean_py.kernel import Kernel


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("project", type=Path)
parser.add_argument("--module", action="append", required=True)
parser.add_argument("--declaration", action="append", default=[])
args = parser.parse_args()
project = args.project.resolve()
paths = subprocess.check_output(
["lake", "env", "printenv", "LEAN_PATH"], cwd=project, text=True
).strip()
search_path = os.pathsep.join(
str((project / entry).resolve()) for entry in paths.split(os.pathsep) if entry
)
bridge = Path(__file__).parent / "lean"
kernel = Kernel(LeanLibrary.from_lake(bridge, "KernelBridge"))
kernel.init_search(search_path)
kernel.load(args.module)
declarations = []
for name in args.declaration:
if not kernel.decl_exists(name):
raise ValueError(f"Declaration not found: {name}")
declarations.append(
{"name": name, "type": kernel.decl_type(name), "axioms": kernel.decl_axioms(name)}
)
print(json.dumps({"loaded": kernel.is_loaded(), "declarations": declarations}, indent=2))


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions examples/kernel/lean/KernelBridge.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import LeanPy.Kernel
import LeanPy.Export

#export_python_registry "KernelBridge"
44 changes: 44 additions & 0 deletions examples/kernel/lean/lake-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{"version": "1.3.0",
"packagesDir": ".lake/packages",
"packages":
[{"type": "path",
"scope": "",
"name": "LeanPy",
"manifestFile": "lake-manifest.json",
"inherited": false,
"dir": "../../..",
"copy": false,
"configFile": "lakefile.lean"},
{"url": "https://github.com/pandaman64/lean-regex.git",
"type": "git",
"subDir": "regex",
"scope": "",
"rev": "30b7188eeecd7268d0f9816f97eb6304083c1bfb",
"name": "Regex",
"manifestFile": "lake-manifest.json",
"inputRev": "v4.34.0-rc1",
"inherited": true,
"configFile": "lakefile.toml"},
{"url": "https://github.com/leanprover/Pantograph.git",
"type": "git",
"subDir": null,
"scope": "",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
"type": "git",
"subDir": null,
"scope": "",
"rev": "3e23a4ad2e91eaf07845cecad157b7ffbb437aed",
"name": "LSpec",
"manifestFile": "lake-manifest.json",
"inputRev": "3e23a4ad2e91eaf07845cecad157b7ffbb437aed",
"inherited": true,
"configFile": "lakefile.toml"}],
"name": "KernelBridge",
"lakeDir": ".lake",
"fixedToolchain": false}
12 changes: 12 additions & 0 deletions examples/kernel/lean/lakefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = "KernelBridge"
defaultTargets = ["KernelBridge"]

[[require]]
name = "LeanPy"
path = "../../.."

[[lean_lib]]
name = "KernelBridge"
moreLinkObjs = ["LeanPy/LeanPy:static", "LeanPy/leanPyNative:static", "Pantograph/Pantograph:static"]
precompileModules = true
defaultFacets = ["shared"]
1 change: 1 addition & 0 deletions examples/kernel/lean/lean-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
leanprover/lean4:v4.35.0
4 changes: 2 additions & 2 deletions lake-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"type": "git",
"subDir": null,
"scope": "",
"rev": "92d4818a4b343d7be293731e03359a19e8082626",
"rev": "0789692d65e365c027d171074bf747f1d40328d0",
"name": "Pantograph",
"manifestFile": "lake-manifest.json",
"inputRev": "dev",
"inputRev": "0789692d65e365c027d171074bf747f1d40328d0",
"inherited": false,
"configFile": "lakefile.lean"},
{"url": "https://github.com/argumentcomputer/LSpec.git",
Expand Down
2 changes: 1 addition & 1 deletion lakefile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package LeanPy where
version := v!"0.1.0"

require Pantograph from git
"https://github.com/leanprover/Pantograph.git" @ "dev"
"https://github.com/leanprover/Pantograph.git" @ "0789692d65e365c027d171074bf747f1d40328d0"

require Regex from git
"https://github.com/pandaman64/lean-regex.git" @ "v4.34.0-rc1" / "regex"
Expand Down
2 changes: 1 addition & 1 deletion lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.34.1
leanprover/lean4:v4.35.0
Loading
Loading