Skip to main content

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-rc1 and v4.18.0-rc1.
    • Ensures compatibility with various Lean projects and machine learning benchmarks.
  • 📦 Temporary Projects: Easily instantiate temporary Lean environments with dependencies.
    • Useful for experimenting and interacting with benchmarks depending on Mathlib like ProofNet# and MiniF2F without manually setting up a Lean project.

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-lean command can install Lean 4 for you after installing LeanInteract.

[!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:

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 LeanREPLConfig might 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 build before 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 manager elan.
  • 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 using run_code, run_file, and run_tactic methods.
  • AutoLeanServer: An experimental subclass of LeanServer automatically recovering from crashes and timeouts. It also monitors memory usage to limit out of memory crashes in multiprocessing contexts. Use the add_to_session_cache attribute 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 LeanREPLConfig instance, and one AutoLeanServer instance per process.
  • Make sure to instantiate LeanREPLConfig before starting the processes to avoid conflicts during Lean REPL's download and build.
  • While AutoLeanServer can 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


Download files

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

Source Distribution

lean_interact-0.2.0.tar.gz (56.1 kB view details)

Uploaded Source

Built Distribution

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

lean_interact-0.2.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

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

Hashes for lean_interact-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1fac66b6f4048c505ae53f6c4c5bc7179536546d8af58345ed4d53efd81d6c63
MD5 c87dbeecf4038ae32ce6f684814fc070
BLAKE2b-256 376ab72ba890300e6ef98c6f9b9fb2edf9f0dfa38017d8024b5262aa84b921ec

See more details on using hashes here.

File details

Details for the file lean_interact-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lean_interact-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2afb0334abe4958bcf0776e8c2d44dc80733060b37168d72e2c70ce8247ad7a
MD5 e3b74af5eb055e8085631da481f6bec2
BLAKE2b-256 15cb5f1b86553c21378b74f4b1da21b75e2aa5180550bb0b6c1077ab2e057e61

See more details on using hashes here.

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