LeanInteract is a Python package that allows you to interact with the Lean theorem prover.
Project description
LeanInteract
LeanInteract is a Python package designed to seamlessly interact with Lean 4 through the Lean REPL.
Key Features
- 🔗 Interactivity: Execute Lean code and files directly from Python, and iterate on environment states.
- 🚀 Ease of Use: LeanInteract abstracts the complexities of Lean setup and interaction, enabling quick experimentation.
- Automatically downloads and builds Lean REPL versions for you. Versions are cached for fast reuse.
- 🔧 Compatibility: Supports all Lean versions between
v4.7.0-rc1andv4.18.0-rc1.- Ensures compatibility with various Lean projects and machine learning benchmarks.
- 📦 Temporary Projects: Easily instantiate temporary Lean environments with dependencies.
Installation and Setup
You can install the LeanInteract package using the following command:
pip install lean-interact
Requirements:
- Python >= 3.10
- git
- Lean 4
- Tip: the
install-leancommand can install Lean 4 for you after installing LeanInteract.
- Tip: the
[!NOTE] This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. For Windows, use WSL. Please report any issues you encounter.
Script examples
In the examples directory, you will find a few scripts demonstrating how to use LeanInteract. We recommend uv to run these scripts (uv run <script>.py).
beq_plus.py: run the autoformalization BEq+ metric on the ProofNetVerif benchmark.type_check.py: optimize type checking of formal statements using environment states.
Soon to be added:
proof_generation_and_autoformalization.py: use DeepSeek-Prover-V1.5, Goedel-Prover, and other models to prove theorems from the MiniF2F and ProofNet# benchmarks.statement_autoformalization_sampling.py: an implementation of the sampling-based statement autoformalization method used in Improving Autoformalization using Type Checking.
Usage
Default Lean version (latest available)
from lean_interact import LeanREPLConfig, LeanServer
config = LeanREPLConfig(verbose=True) # download and build Lean REPL
server = LeanServer(config) # start Lean REPL
server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
Output
{"sorries": [{"proofState": 0,
"pos": {"line": 1, "column": 40},
"goal": "n : Nat\n⊢ n = 5 → n = 5",
"endPos": {"line": 1, "column": 45}}],
"messages": [{"severity": "warning",
"pos": {"line": 1, "column": 8},
"endPos": {"line": 1, "column": 10},
"data": "declaration uses 'sorry'"}],
"env": 0}
Iterate on the environment state:
server.run_code("theorem ex2 (x : Nat) : x = 5 → x = 5 := by\n exact ex x", env=0)
Output
{"env": 1}
[!NOTE] The initial invocation of
LeanREPLConfigmight take some time as it downloads and builds Lean REPL. Future executions with identical parameters will be significantly quicker due to caching.
Specific Lean version
config = LeanREPLConfig(lean_version="v4.7.0")
Existing Lean projects
config = LeanREPLConfig(project=LocalProject("path/to/your/project"))
or
config = LeanREPLConfig(project=GitProject("https://github.com/yangky11/lean4-example"))
You can then use run_code and run_file as usual:
server = LeanServer(config)
server.run_file("file.lean")
[!IMPORTANT] Ensure the project can be successfully built with
lake buildbefore using LeanInteract.
Temporary project with dependencies
config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject([LeanRequire(
name="mathlib",
git="https://github.com/leanprover-community/mathlib4.git",
rev="v4.7.0"
)]))
Mathlib being a frequent requirement, a shortcut is available:
config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject("mathlib"))
You can then use Mathlib as follows:
server = LeanServer(config)
server.run_code("""import Mathlib
theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x + y ) := sorry""")
Output
{"sorries": [{"proofState": 0,
"pos": {"line": 4, "column": 26},
"goal": "x : ℝ\ny : ℚ\n⊢ Irrational (x + ↑y)",
"endPos": {"line": 4, "column": 31}}],
"messages": [{"severity": "warning",
"pos": {"line": 3, "column": 8},
"endPos": {"line": 3, "column": 18},
"data": "declaration uses 'sorry'"}],
"env": 0}
[!NOTE]
- Mathlib is a large library and may take some time to download and build.
- A separate cache is used for each unique set of dependencies.
Fine-grained temporary project
For more control over the temporary project, you can use TemporaryProject to specify the content of the lakefile.
config = LeanREPLConfig(lean_version="v4.18.0-rc1", project=TemporaryProject("""
import Lake
open Lake DSL
package "dummy" where
version := v!"0.1.0"
@[default_target]
lean_exe "dummy" where
root := `Main
require mathlib from git
"https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0-rc1"
"""))
Tactic and proof modes (experimental)
server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
Output
{"sorries": [{"proofState": 0,
"pos": {"line": 1, "column": 40},
"goal": "n : Nat\n⊢ n = 5 → n = 5",
"endPos": {"line": 1, "column": 45}}],
"messages": [{"severity": "warning",
"pos": {"line": 1, "column": 8},
"endPos": {"line": 1, "column": 10},
"data": "declaration uses 'sorry'"}],
"env": 0}
You can then iterate on the proof state by executing tactics:
server.run_tactic("intro h", proof_state=0)
Output
{"proofState": 1, "goals": ["n : Nat\nh : n = 5\n⊢ n = 5"]}
server.run_tactic("exact h", proof_state=1)
Output
{"proofState": 2, "goals": []}
or by running the entire/partial proofs:
server.run_proof("intro h\nexact h", proof_state=0)
Output
{"proofState": 3, "goals": []}
Helper Commands
install_lean: Installs Lean 4 version managerelan.clear_lean_cache: Removes all Lean REPL versions and temporary projects in the package cache. This can help resolve some issues. If it does, please open an issue.
Advanced options
LeanServer
Two versions of Lean servers are available:
LeanServer: A wrapper around Lean REPL. Interact with it usingrun_code,run_file, andrun_tacticmethods.AutoLeanServer: An experimental subclass ofLeanServerautomatically recovering from crashes and timeouts. It also monitors memory usage to limit out of memory crashes in multiprocessing contexts. Use theadd_to_session_cacheattribute available in various methods to prevent selected environment/proof states to be cleared.
[!TIP]
- To run multiple requests in parallel, we recommend using multiprocessing with one global
LeanREPLConfiginstance, and oneAutoLeanServerinstance per process.- Make sure to instantiate
LeanREPLConfigbefore starting the processes to avoid conflicts during Lean REPL's download and build.- While
AutoLeanServercan help prevent crashes, it is not a complete solution. If you encounter crashes, consider reducing the number of parallel processes or increasing the memory available to your system.
Custom Lean REPL
To use a forked Lean REPL project, specify the git repository using the repl_git parameter in the LeanREPLConfig. Your fork should have a similar versioning format to https://github.com/augustepoiroux/repl (i.e. having a branch with commits for each Lean version). For assistance, feel free to contact us.
Similar tools
We recommend checking out these tools:
- PyPantograph: Based on Pantograph, offering more options for proof interactions than Lean REPL.
- LeanDojo: Parses Lean projects to create datasets and interact with proof states.
- itp-interface: A Python interface for interacting and extracting data from Lean 4 and Coq.
- leanclient: Interact with the Lean LSP server.
LeanInteract is inspired by pylean and lean4_jupyter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lean_interact-0.2.0.tar.gz.
File metadata
- Download URL: lean_interact-0.2.0.tar.gz
- Upload date:
- Size: 56.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fac66b6f4048c505ae53f6c4c5bc7179536546d8af58345ed4d53efd81d6c63
|
|
| MD5 |
c87dbeecf4038ae32ce6f684814fc070
|
|
| BLAKE2b-256 |
376ab72ba890300e6ef98c6f9b9fb2edf9f0dfa38017d8024b5262aa84b921ec
|
File details
Details for the file lean_interact-0.2.0-py3-none-any.whl.
File metadata
- Download URL: lean_interact-0.2.0-py3-none-any.whl
- Upload date:
- Size: 18.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2afb0334abe4958bcf0776e8c2d44dc80733060b37168d72e2c70ce8247ad7a
|
|
| MD5 |
e3b74af5eb055e8085631da481f6bec2
|
|
| BLAKE2b-256 |
15cb5f1b86553c21378b74f4b1da21b75e2aa5180550bb0b6c1077ab2e057e61
|