diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index efd60b7..ba3ecd3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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) @@ -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 @@ -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: | @@ -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 diff --git a/LeanPy/Kernel.lean b/LeanPy/Kernel.lean index 5aa34b7..c2547e5 100644 --- a/LeanPy/Kernel.lean +++ b/LeanPy/Kernel.lean @@ -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 "" catch e => return s!"" diff --git a/README.md b/README.md index cfae63f..247d755 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 ``` diff --git a/examples/01_basic/lean/lake-manifest.json b/examples/01_basic/lean/lake-manifest.json index 2640b46..75abf2b 100644 --- a/examples/01_basic/lean/lake-manifest.json +++ b/examples/01_basic/lean/lake-manifest.json @@ -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", diff --git a/examples/01_basic/lean/lean-toolchain b/examples/01_basic/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/01_basic/lean/lean-toolchain +++ b/examples/01_basic/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/02_pantograph_kernel/lean/lake-manifest.json b/examples/02_pantograph_kernel/lean/lake-manifest.json index 32deec3..6a16556 100644 --- a/examples/02_pantograph_kernel/lean/lake-manifest.json +++ b/examples/02_pantograph_kernel/lean/lake-manifest.json @@ -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", diff --git a/examples/02_pantograph_kernel/lean/lean-toolchain b/examples/02_pantograph_kernel/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/02_pantograph_kernel/lean/lean-toolchain +++ b/examples/02_pantograph_kernel/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/03_numpy_typed/lean/lake-manifest.json b/examples/03_numpy_typed/lean/lake-manifest.json index 55986ef..01c47f4 100644 --- a/examples/03_numpy_typed/lean/lake-manifest.json +++ b/examples/03_numpy_typed/lean/lake-manifest.json @@ -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", diff --git a/examples/03_numpy_typed/lean/lean-toolchain b/examples/03_numpy_typed/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/03_numpy_typed/lean/lean-toolchain +++ b/examples/03_numpy_typed/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/04_sympy_tactic/lean/lake-manifest.json b/examples/04_sympy_tactic/lean/lake-manifest.json index 417eaa9..2d8837f 100644 --- a/examples/04_sympy_tactic/lean/lake-manifest.json +++ b/examples/04_sympy_tactic/lean/lake-manifest.json @@ -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", diff --git a/examples/04_sympy_tactic/lean/lean-toolchain b/examples/04_sympy_tactic/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/04_sympy_tactic/lean/lean-toolchain +++ b/examples/04_sympy_tactic/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/05_knuckledragger/lean/lake-manifest.json b/examples/05_knuckledragger/lean/lake-manifest.json index 6377d58..a9dfc5a 100644 --- a/examples/05_knuckledragger/lean/lake-manifest.json +++ b/examples/05_knuckledragger/lean/lake-manifest.json @@ -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", diff --git a/examples/05_knuckledragger/lean/lean-toolchain b/examples/05_knuckledragger/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/05_knuckledragger/lean/lean-toolchain +++ b/examples/05_knuckledragger/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/06_effectful_verifier/lean/lake-manifest.json b/examples/06_effectful_verifier/lean/lake-manifest.json index a1dccf9..7c9fd22 100644 --- a/examples/06_effectful_verifier/lean/lake-manifest.json +++ b/examples/06_effectful_verifier/lean/lake-manifest.json @@ -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", diff --git a/examples/06_effectful_verifier/lean/lean-toolchain b/examples/06_effectful_verifier/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/examples/06_effectful_verifier/lean/lean-toolchain +++ b/examples/06_effectful_verifier/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/examples/kernel/README.md b/examples/kernel/README.md new file mode 100644 index 0000000..b2c98ac --- /dev/null +++ b/examples/kernel/README.md @@ -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. diff --git a/examples/kernel/inspect_project.py b/examples/kernel/inspect_project.py new file mode 100644 index 0000000..315741a --- /dev/null +++ b/examples/kernel/inspect_project.py @@ -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() diff --git a/examples/kernel/lean/KernelBridge.lean b/examples/kernel/lean/KernelBridge.lean new file mode 100644 index 0000000..6b3527e --- /dev/null +++ b/examples/kernel/lean/KernelBridge.lean @@ -0,0 +1,4 @@ +import LeanPy.Kernel +import LeanPy.Export + +#export_python_registry "KernelBridge" diff --git a/examples/kernel/lean/lake-manifest.json b/examples/kernel/lean/lake-manifest.json new file mode 100644 index 0000000..4f0b5a3 --- /dev/null +++ b/examples/kernel/lean/lake-manifest.json @@ -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} diff --git a/examples/kernel/lean/lakefile.toml b/examples/kernel/lean/lakefile.toml new file mode 100644 index 0000000..dbc2488 --- /dev/null +++ b/examples/kernel/lean/lakefile.toml @@ -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"] diff --git a/examples/kernel/lean/lean-toolchain b/examples/kernel/lean/lean-toolchain new file mode 100644 index 0000000..a6aff06 --- /dev/null +++ b/examples/kernel/lean/lean-toolchain @@ -0,0 +1 @@ +leanprover/lean4:v4.35.0 diff --git a/lake-manifest.json b/lake-manifest.json index 196d618..c38c46d 100644 --- a/lake-manifest.json +++ b/lake-manifest.json @@ -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", diff --git a/lakefile.lean b/lakefile.lean index a8de91f..24932d8 100644 --- a/lakefile.lean +++ b/lakefile.lean @@ -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" diff --git a/lean-toolchain b/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/lean-toolchain +++ b/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0 diff --git a/scripts/prepare_lean435.py b/scripts/prepare_lean435.py new file mode 100644 index 0000000..43d9bd0 --- /dev/null +++ b/scripts/prepare_lean435.py @@ -0,0 +1,35 @@ +"""Fetch Lake dependencies and apply the checked Lean 4.35 Regex workaround.""" + +import argparse +import hashlib +import subprocess +from pathlib import Path + +ORIGINAL = "2f2c23ed8bf07339194bf6feec94a97eca44d858ef1ad58afcab752a01039c05" +PATCHED = "eaf853049bd9837d0bb51c2763707942eb6b54cd3f8e64f0a7608a626181a7ae" + + +def prepare(project: Path) -> None: + subprocess.run(["lake", "update"], cwd=project, check=True) + source = project / ".lake/packages/Regex/regex/Regex/Data/SparseSet/Bijection.lean" + data = source.read_bytes() + fingerprint = hashlib.sha256(data).hexdigest() + if fingerprint == PATCHED: + print(f"Regex workaround already applied: {source}") + return + if fingerprint != ORIGINAL: + raise RuntimeError(f"Refusing to patch unexpected Regex source: {source}") + data = data.replace( + b" grind\n have surj", + b" apply Fin.ext\n grind -hom [Fin.ext_iff]\n have surj", + ) + if hashlib.sha256(data).hexdigest() != PATCHED: + raise RuntimeError("Regex patch did not produce the expected source") + source.write_bytes(data) + print(f"Applied Lean 4.35 Regex proof workaround: {source}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("project", nargs="?", type=Path, default=Path.cwd()) + prepare(parser.parse_args().project.resolve()) diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index 9e05be7..b9e5eee 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -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", diff --git a/tests/lean/lean-toolchain b/tests/lean/lean-toolchain index ba8ebf2..a6aff06 100644 --- a/tests/lean/lean-toolchain +++ b/tests/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:v4.34.1 +leanprover/lean4:v4.35.0