REPL interaction and data extraction for Lean 4.
Project description
Installation
Clone the repository including submodules:
git clone --recurse-submodules https://github.com/LeanTreeAnonymized/LeanTree.git
Ensure Lean 4.19 is installed via elan.
lake --version
At the moment, LeanTree only supports Lean 4.19.
Make sure pip is installed.
Then, run:
make install
Alternatively, use Poetry explicitly:
pip install poetry
poetry install
For running tests or experiments, refer to the Development section.
Basic Usage
Note: You must create a Lean project to use Lean. See Lean project guide for more information.
Start by creating or loading a Lean project:
from leantree import LeanProject
project = LeanProject.create("path/to/project")
# or load an existing one:
project = LeanProject("path/to/project")
# or decide automatically:
project = LeanProject("path/to/project", create=True)
The created project includes Mathlib by default.
If no path is provided in LeanProject.create, the project will be created in or loaded from the leantree_project subdirectory of the current directory.
Starting a Proof
Using the environment, you can initialize a proof search by supplying a theorem with one or more sorry keywords.
with project.environment() as env:
env.send_command("import Mathlib\nopen BigOperators Real Nat Topology Rat")
branch = env.proof_from_sorry("theorem succ_less_double_succ (n : Nat) : n > 0 → n < 2 * n := by sorry")
zero, succ = branch.apply_tactic("cases n")
print("Factorized proof states after `cases n`:")
print(zero.state)
print(succ.state)
assert not zero.is_solved
...
Async API
async with project.environment() as env:
await env.send_command_async("import Mathlib\nopen BigOperators Real Nat Topology Rat")
branch = await env.proof_from_sorry_async("theorem succ_less_double_succ (n : Nat) : n > 0 → n < 2 * n := by sorry")
zero, succ = await branch.apply_tactic_async("cases n")
...
Data Extraction
Using the project, you can parse a Lean file and build all proof trees. Then, you can use the environment to start a proof for each tactic block in the file.
file = project.load_file("Example.lean")
# Pretty-print all proof trees.
for thm in file.theorems:
print(thm.load_source() + "\n")
for by_block in thm.by_blocks:
print(by_block.tree.pretty_print())
print("-" * 100)
# Start proofs for each tactic block.
for thm, branch in env.file_proofs(file):
if isinstance(branch, Exception):
print(f"Could not start theorem '{thm}' due to exception: {branch}")
...
Dataset Generation
You can easily generate a dataset containing all Lean files in a directory. For production-ready dataset generation, see the Datasets section
import glob
import json
with open("dataset.json", "w") as f:
for path in glob.glob('**/*.lean', recursive=True):
file = project.load_file(path)
f.write(json.dumps(file.serialize()) + "\n")
Save/Restore Environment State
The environment state can be saved to disk and restored later.
with project.environment() as env:
env.send_command("import Mathlib\nopen BigOperators Real Nat Topology Rat")
env.pickle("env.pkl")
with project.environment() as env:
env.unpickle("env.pkl")
branch = env.proof_from_sorry("theorem succ_less_double_succ (n : Nat) : n > 0 → n < 2 * n := by sorry")
zero, succ = branch.apply_tactic("cases n")
...
Datasets
Assuming you have already create a Lean project in leantree_project, you can recreate the whole Mathlib dataset by running:
python dataset/tree_dataset.py generate --project_path leantree_project --source_files mathlib/Mathlib
Development
Install all development and experiments dependencies:
make install-dev
Running Tests
To run tests, first create a Lean 4.19 project called leantree_project in the LeanTree directory.
You can use LeanTree for that - in the leantree directory, run the following Python code:
from leantree import LeanProject
project = LeanProject.create()
After the project is created, run:
make test
Debugging Tips
When working with a Lean environment, you can use env.take_control() to debug the underlying Lean REPL.
This method connects your stdin/stdout to the REPL's stdin/stdout.
Related Tools
For a detailed comparison, refer to the LeanTree paper.
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 leantree-1.0.0.tar.gz.
File metadata
- Download URL: leantree-1.0.0.tar.gz
- Upload date:
- Size: 40.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-62-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce219545775e2f22557951eaf061ef1e7ec5ef107b5e6e541413415c9ab543cc
|
|
| MD5 |
c518b2c73be99a884c36795efcc7cba1
|
|
| BLAKE2b-256 |
38622b54c37dd242acce812e34e7d3743b1329947f2c96be8690b6bcddb97dff
|
File details
Details for the file leantree-1.0.0-cp312-cp312-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: leantree-1.0.0-cp312-cp312-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 48.7 kB
- Tags: CPython 3.12, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-62-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb3884e7f13a7c12e4a95d22d165518edfe99686d9d2fa83dafb5e3e569dc35a
|
|
| MD5 |
fe0e0c4bfe23b8e04eb448ca26ac6022
|
|
| BLAKE2b-256 |
8314480d37d0e0013a70d912b66a72b711aef78f8b42858b2c8ff3cf724076c2
|