lean-interact 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/python
3
+ {
4
+ "name": "Python 3",
5
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6
+ "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
7
+ "features": {
8
+ "ghcr.io/va-h/devcontainers-features/uv:1": {
9
+ "shellautocompletion": true,
10
+ "version": "latest"
11
+ }
12
+ },
13
+
14
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
15
+ // "forwardPorts": [],
16
+
17
+ // Use 'postCreateCommand' to run commands after the container is created.
18
+ "postCreateCommand": "uv pip install -e . && uv sync --dev && uv run install-lean",
19
+
20
+ // Configure tool-specific properties.
21
+ // "customizations": {},
22
+
23
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
24
+ "remoteUser": "root"
25
+ }
@@ -12,8 +12,7 @@ jobs:
12
12
  - name: Install elan
13
13
  run: |
14
14
  set -o pipefail
15
- curl -sSfL https://github.com/leanprover/elan/releases/download/v3.1.1/elan-x86_64-unknown-linux-gnu.tar.gz | tar xz
16
- ./elan-init -y --default-toolchain none
15
+ curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y --default-toolchain none
17
16
  echo "$HOME/.elan/bin" >> $GITHUB_PATH
18
17
 
19
18
  - name: Set up Python
@@ -0,0 +1,21 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ pypi:
10
+ name: Publish to PyPI
11
+ runs-on: ubuntu-latest
12
+ environment:
13
+ name: pypi
14
+ url: https://pypi.org/p/lean-interact
15
+ permissions:
16
+ id-token: write
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: astral-sh/setup-uv@v3
20
+ - run: uv build
21
+ - run: uv publish --trusted-publishing always
@@ -1,13 +1,14 @@
1
+ # Lean
1
2
  build
2
3
  lake-packages/*
3
4
  lakefile.olean
4
5
  .lake
5
- test/Mathlib/.lake
6
- test/*.olean
7
- test/*.olean.tmp
6
+ src/cache
8
7
  cache
9
- sandbox*
10
8
 
11
9
  # Python
12
10
  .venv
13
11
  __pycache__
12
+
13
+ sandbox*
14
+ .vscode
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 LeanDojo Team
3
+ Copyright (c) 2025
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lean-interact
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: LeanInteract is a Python package that allows you to interact with the Lean theorem prover.
5
5
  Author-email: Auguste Poiroux <auguste.poiroux@epfl.ch>
6
6
  License: MIT License
7
7
 
8
- Copyright (c) 2023 LeanDojo Team
8
+ Copyright (c) 2025
9
9
 
10
10
  Permission is hereby granted, free of charge, to any person obtaining a copy
11
11
  of this software and associated documentation files (the "Software"), to deal
@@ -27,7 +27,7 @@ License: MIT License
27
27
  License-File: LICENSE
28
28
  Keywords: Lean,REPL,autoformalization,theorem proving
29
29
  Requires-Python: >=3.10
30
- Requires-Dist: packaging>=24.2
30
+ Requires-Dist: gitpython>=3.1.44
31
31
  Requires-Dist: pexpect>=4.9.0
32
32
  Requires-Dist: psutil>=6.1.0
33
33
  Requires-Dist: requests>=2.32.3
@@ -38,57 +38,46 @@ Description-Content-Type: text/markdown
38
38
 
39
39
  **LeanInteract** is a Python package designed to seamlessly interact with Lean 4 through the [Lean REPL](https://github.com/leanprover-community/repl).
40
40
 
41
- > [!NOTE]
42
- > This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. Windows is not supported at the moment. Please report any issues you encounter.
43
-
44
41
  ## Key Features
45
42
 
43
+ - **🔗 Interactivity**: Execute Lean code and files directly from Python, and iterate on environment states.
46
44
  - **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction, enabling quick experimentation.
47
- - **🔗 Interactivity**: Execute Lean code, files, tactics, and (partial) proofs directly from Python. Easily iterate through environment and proof states.
48
- - **🔧 Compatibility**: Supports Lean versions from `v4.7.0-rc1` to `v4.16.0-rc2`.
45
+ - Automatically downloads and builds Lean REPL versions for you. Versions are cached for fast reuse.
46
+ - **🔧 Compatibility**: Supports all Lean versions between `v4.7.0-rc1` and `v4.18.0-rc1`.
49
47
  - Ensures compatibility with various Lean projects and machine learning benchmarks.
50
- - Need older versions? Open an issue [here](https://github.com/augustepoiroux/repl).
51
- - **🔄 Up-to-date**: LeanInteract is a lightweight wrapper around [Lean REPL](https://github.com/leanprover-community/repl), ensuring it stays current with the latest Lean versions and features.
52
- - **📥 Automatic Setup**: Automatically downloads and builds Lean REPL versions for you. Versions are cached for fast reuse.
53
48
  - **📦 Temporary Projects**: Easily instantiate temporary Lean environments with dependencies.
54
- - Useful for experimenting and interacting with benchmarks like [ProofNet](https://github.com/zhangir-azerbayev/ProofNet) and [MiniF2F](https://github.com/yangky11/miniF2F-lean4) without manual setup.
55
-
56
- ## Similar tools
57
-
58
- We recommend checking out these tools:
49
+ - Useful for experimenting and interacting with benchmarks depending on [Mathlib](https://github.com/leanprover-community/mathlib4) like [ProofNet#](https://huggingface.co/datasets/PAug/ProofNetSharp) and [MiniF2F](https://github.com/yangky11/miniF2F-lean4) without manually setting up a Lean project.
59
50
 
60
- - **[PyPantograph](https://github.com/lenianiva/PyPantograph)**: Based on Pantograph, offering more options for proof interactions than Lean REPL.
61
- - **[LeanDojo](https://github.com/lean-dojo/LeanDojo)**: Parses Lean projects to create datasets and interact with theorems to prove them.
62
- - **[leanclient](https://github.com/oOo0oOo/leanclient)**: Interact with the Lean LSP server.
63
-
64
- LeanInteract is inspired by:
51
+ ## Installation and Setup
65
52
 
66
- - **[pylean](https://github.com/zhangir-azerbayev/repl)**
67
- - **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**
53
+ You can install the LeanInteract package using the following command:
68
54
 
69
- ## Installation and Setup
55
+ ```bash
56
+ pip install lean-interact
57
+ ```
70
58
 
71
59
  Requirements:
72
60
 
73
61
  - Python >= 3.10
74
62
  - git
75
63
  - [Lean 4](https://leanprover-community.github.io/get_started.html)
64
+ - Tip: the `install-lean` command can install Lean 4 for you after installing LeanInteract.
76
65
 
77
- If the requirements are met, install the LeanInteract package:
78
-
79
- ```bash
80
- pip install lean-interact
81
- ```
66
+ > [!NOTE]
67
+ > This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. For Windows, use WSL.
68
+ > Please report any issues you encounter.
82
69
 
83
70
  ## Script examples
84
71
 
85
- In the `examples` directory, you will find various scripts demonstrating how to use LeanInteract:
72
+ In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract. We recommend [uv](https://github.com/astral-sh/uv) to run these scripts (`uv run <script>.py`).
86
73
 
87
- - `minif2f_deepseek_prover_v1_5.py`: use [DeepSeek-Prover-V1.5](https://arxiv.org/abs/2408.08152) to prove theorems from the [MiniF2F](https://github.com/yangky11/miniF2F-lean4) benchmark.
88
- - `proofnetsharp_type_check.py`: type check theorems from the [ProofNet#](https://github.com/zhangir-azerbayev/ProofNet) benchmark.
89
- - `proofnetsharp_false_theorems.py`: find theorems that can be proven false from the [ProofNet#](https://github.com/zhangir-azerbayev/ProofNet) benchmark.
90
- - `proofnet_metric_beq_plus.py`: run a simple [BEq](https://openreview.net/forum?id=hUb2At2DsQ) checker on the ProofNet-Metric benchmark.
91
- - `improving_autoformalization_using_type_checking.py`: an implementation of the sampling method used in [Improving Autoformalization using Type Checking](https://arxiv.org/abs/2406.07222).
74
+ - `beq_plus.py`: run the autoformalization [BEq+](https://arxiv.org/abs/2406.07222) metric on the [ProofNetVerif](https://huggingface.co/datasets/PAug/ProofNetVerif) benchmark.
75
+ - `type_check.py`: optimize type checking of formal statements using environment states.
76
+
77
+ Soon to be added:
78
+
79
+ - `proof_generation_and_autoformalization.py`: use [DeepSeek-Prover-V1.5](https://arxiv.org/abs/2408.08152), [Goedel-Prover](https://goedel-lm.github.io/), and other models to prove theorems from the [MiniF2F](https://github.com/yangky11/miniF2F-lean4) and [ProofNet#](https://huggingface.co/datasets/PAug/ProofNetSharp) benchmarks.
80
+ - `statement_autoformalization_sampling.py`: an implementation of the sampling-based statement autoformalization method used in [Improving Autoformalization using Type Checking](https://arxiv.org/abs/2406.07222).
92
81
 
93
82
  ## Usage
94
83
 
@@ -97,7 +86,7 @@ In the `examples` directory, you will find various scripts demonstrating how to
97
86
  ```python
98
87
  from lean_interact import LeanREPLConfig, LeanServer
99
88
 
100
- config = LeanREPLConfig() # download and build Lean REPL
89
+ config = LeanREPLConfig(verbose=True) # download and build Lean REPL
101
90
  server = LeanServer(config) # start Lean REPL
102
91
  server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
103
92
  ```
@@ -119,50 +108,7 @@ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
119
108
 
120
109
  </details>
121
110
 
122
- You can then iterate on the proof state by executing tactics:
123
-
124
- ```python
125
- server.run_tactic("intro h", proof_state=0)
126
- ```
127
-
128
- <details>
129
- <summary>Output</summary>
130
-
131
- ```json
132
- {"proofState": 1, "goals": ["n : Nat\nh : n = 5\n⊢ n = 5"]}
133
- ```
134
-
135
- </details>
136
-
137
- ```python
138
- server.run_tactic("exact h", proof_state=1)
139
- ```
140
-
141
- <details>
142
- <summary>Output</summary>
143
-
144
- ```json
145
- {"proofState": 2, "goals": []}
146
- ```
147
-
148
- </details>
149
-
150
- Or by running the entire proof:
151
-
152
- ```python
153
- server.run_proof("intro h\nexact h", proof_state=0)
154
- ```
155
-
156
- <details>
157
- <summary>Output</summary>
158
-
159
- ```json
160
- {"proofState": 3, "goals": []}
161
- ```
162
-
163
- </details>
164
-
165
- You can also iterate on the environment:
111
+ Iterate on the environment state:
166
112
 
167
113
  ```python
168
114
  server.run_code("theorem ex2 (x : Nat) : x = 5 → x = 5 := by\n exact ex x", env=0)
@@ -180,19 +126,25 @@ server.run_code("theorem ex2 (x : Nat) : x = 5 → x = 5 := by\n exact ex x", e
180
126
  > [!NOTE]
181
127
  > 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.
182
128
 
183
- ### Using a specific Lean version
129
+ ### Specific Lean version
184
130
 
185
131
  ```python
186
132
  config = LeanREPLConfig(lean_version="v4.7.0")
187
133
  ```
188
134
 
189
- ### Using an existing Lean project directory
135
+ ### Existing Lean projects
136
+
137
+ ```python
138
+ config = LeanREPLConfig(project=LocalProject("path/to/your/project"))
139
+ ```
140
+
141
+ or
190
142
 
191
143
  ```python
192
- config = LeanREPLConfig(project_dir="path/to/your/project")
144
+ config = LeanREPLConfig(project=GitProject("https://github.com/yangky11/lean4-example"))
193
145
  ```
194
146
 
195
- You can then use `run_code`, `run_tactic` and `run_file` as usual:
147
+ You can then use `run_code` and `run_file` as usual:
196
148
 
197
149
  ```python
198
150
  server = LeanServer(config)
@@ -200,28 +152,28 @@ server.run_file("file.lean")
200
152
  ```
201
153
 
202
154
  > [!IMPORTANT]
203
- > Ensure the project in `project_dir` has been *successfully* built with `lake build` before using the REPL.
155
+ > Ensure the project can be *successfully* built with `lake build` before using LeanInteract.
204
156
 
205
- ### Using a temporary project with dependencies
157
+ ### Temporary project with dependencies
206
158
 
207
159
  ```python
208
- config = LeanREPLConfig(lean_version="v4.7.0", require=[LeanRequire(
160
+ config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject([LeanRequire(
209
161
  name="mathlib",
210
162
  git="https://github.com/leanprover-community/mathlib4.git",
211
163
  rev="v4.7.0"
212
- )])
164
+ )]))
213
165
  ```
214
166
 
215
167
  Mathlib being a frequent requirement, a shortcut is available:
216
168
 
217
169
  ```python
218
- config = LeanREPLConfig(lean_version="v4.7.0", require="mathlib")
170
+ config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject("mathlib"))
219
171
  ```
220
172
 
221
- You can then use Mathlib:
173
+ You can then use Mathlib as follows:
222
174
 
223
175
  ```python
224
- server = LeanServer(config_readme_mathlib)
176
+ server = LeanServer(config)
225
177
  server.run_code("""import Mathlib
226
178
  theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x + y ) := sorry""")
227
179
  ```
@@ -246,9 +198,100 @@ theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x +
246
198
  > [!NOTE]
247
199
  >
248
200
  > - Mathlib is a large library and may take some time to download and build.
249
- > - Mathlib, and other libraries, are not available for all Lean versions. An error will be raised if the Lean version you are using does not support Mathlib. You can always specify a different version with the `lean_version` parameter if you know a compatible version.
250
201
  > - A separate cache is used for each unique set of dependencies.
251
202
 
203
+ ### Fine-grained temporary project
204
+
205
+ For more control over the temporary project, you can use `TemporaryProject` to specify the content of the lakefile.
206
+
207
+ ```python
208
+ config = LeanREPLConfig(lean_version="v4.18.0-rc1", project=TemporaryProject("""
209
+ import Lake
210
+ open Lake DSL
211
+
212
+ package "dummy" where
213
+ version := v!"0.1.0"
214
+
215
+ @[default_target]
216
+ lean_exe "dummy" where
217
+ root := `Main
218
+
219
+ require mathlib from git
220
+ "https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0-rc1"
221
+ """))
222
+ ```
223
+
224
+ ### Tactic and proof modes (experimental)
225
+
226
+ ```python
227
+ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
228
+ ```
229
+
230
+ <details>
231
+ <summary>Output</summary>
232
+
233
+ ```json
234
+ {"sorries": [{"proofState": 0,
235
+ "pos": {"line": 1, "column": 40},
236
+ "goal": "n : Nat\n⊢ n = 5 → n = 5",
237
+ "endPos": {"line": 1, "column": 45}}],
238
+ "messages": [{"severity": "warning",
239
+ "pos": {"line": 1, "column": 8},
240
+ "endPos": {"line": 1, "column": 10},
241
+ "data": "declaration uses 'sorry'"}],
242
+ "env": 0}
243
+ ```
244
+
245
+ </details>
246
+
247
+ You can then iterate on the proof state by executing tactics:
248
+
249
+ ```python
250
+ server.run_tactic("intro h", proof_state=0)
251
+ ```
252
+
253
+ <details>
254
+ <summary>Output</summary>
255
+
256
+ ```json
257
+ {"proofState": 1, "goals": ["n : Nat\nh : n = 5\n⊢ n = 5"]}
258
+ ```
259
+
260
+ </details>
261
+
262
+ ```python
263
+ server.run_tactic("exact h", proof_state=1)
264
+ ```
265
+
266
+ <details>
267
+ <summary>Output</summary>
268
+
269
+ ```json
270
+ {"proofState": 2, "goals": []}
271
+ ```
272
+
273
+ </details>
274
+
275
+ or by running the entire/partial proofs:
276
+
277
+ ```python
278
+ server.run_proof("intro h\nexact h", proof_state=0)
279
+ ```
280
+
281
+ <details>
282
+ <summary>Output</summary>
283
+
284
+ ```json
285
+ {"proofState": 3, "goals": []}
286
+ ```
287
+
288
+ </details>
289
+
290
+ ## Helper Commands
291
+
292
+ - `install_lean`: Installs Lean 4 version manager `elan`.
293
+ - `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.
294
+
252
295
  ## Advanced options
253
296
 
254
297
  ### LeanServer
@@ -256,18 +299,25 @@ theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x +
256
299
  Two versions of Lean servers are available:
257
300
 
258
301
  - **`LeanServer`**: A wrapper around Lean REPL. Interact with it using `run_code`, `run_file`, and `run_tactic` methods.
259
- - **`AutoLeanServer`**: An experimental subclass of `LeanServer` monitoring memory usage to limit *out of memory* crashes in multiprocessing contexts. Additionally, since Lean REPL retains all environment and proof states, `AutoLeanServer` regularly clears the REPL's memory to prevent crashes. Use the `add_to_session_cache` attribute in various methods to prevent selected environment/proof states to be cleared.
302
+ - **`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.
260
303
 
261
304
  > [!TIP]
262
305
  >
263
- > - To run multiple requests in parallel, we recommend using multiprocessing with one `AutoLeanServer` instance per process.
306
+ > - To run multiple requests in parallel, we recommend using multiprocessing with one global `LeanREPLConfig` instance, and one `AutoLeanServer` instance per process.
264
307
  > - Make sure to instantiate `LeanREPLConfig` before starting the processes to avoid conflicts during Lean REPL's download and build.
265
308
  > - 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.
266
309
 
267
- ### Helper Methods and Variables
310
+ ### Custom Lean REPL
268
311
 
269
- - `clear_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.
312
+ 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](mailto:auguste.poiroux@epfl.ch).
270
313
 
271
- ### Custom Lean REPL
314
+ ## Similar tools
315
+
316
+ We recommend checking out these tools:
317
+
318
+ - **[PyPantograph](https://github.com/lenianiva/PyPantograph)**: Based on Pantograph, offering more options for proof interactions than Lean REPL.
319
+ - **[LeanDojo](https://github.com/lean-dojo/LeanDojo)**: Parses Lean projects to create datasets and interact with proof states.
320
+ - **[itp-interface](https://github.com/trishullab/itp-interface)**: A Python interface for interacting and extracting data from Lean 4 and Coq.
321
+ - **[leanclient](https://github.com/oOo0oOo/leanclient)**: Interact with the Lean LSP server.
272
322
 
273
- 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 format to <https://github.com/augustepoiroux/repl>. For assistance, feel free to contact [us](mailto:auguste.poiroux@epfl.ch).
323
+ LeanInteract is inspired by **[pylean](https://github.com/zhangir-azerbayev/repl)** and **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**.