lean-interact 0.1.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.
- lean_interact-0.1.0/.github/workflows/ci.yml +30 -0
- lean_interact-0.1.0/.gitignore +13 -0
- lean_interact-0.1.0/.vscode/settings.json +6 -0
- lean_interact-0.1.0/LICENSE +21 -0
- lean_interact-0.1.0/PKG-INFO +273 -0
- lean_interact-0.1.0/README.md +237 -0
- lean_interact-0.1.0/TODO.md +16 -0
- lean_interact-0.1.0/pyproject.toml +25 -0
- lean_interact-0.1.0/src/lean_interact/__init__.py +12 -0
- lean_interact-0.1.0/src/lean_interact/server.py +842 -0
- lean_interact-0.1.0/src/lean_interact/utils.py +63 -0
- lean_interact-0.1.0/tests/test_server.py +414 -0
- lean_interact-0.1.0/uv.lock +700 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
steps:
|
|
9
|
+
- name: Checkout
|
|
10
|
+
uses: actions/checkout@v3
|
|
11
|
+
|
|
12
|
+
- name: Install elan
|
|
13
|
+
run: |
|
|
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
|
|
17
|
+
echo "$HOME/.elan/bin" >> $GITHUB_PATH
|
|
18
|
+
|
|
19
|
+
- name: Set up Python
|
|
20
|
+
uses: actions/setup-python@v4
|
|
21
|
+
with:
|
|
22
|
+
python-version: '3.10'
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: |
|
|
26
|
+
pip install --upgrade pip
|
|
27
|
+
pip install .
|
|
28
|
+
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: python -m unittest discover -s ./tests
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 LeanDojo Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lean-interact
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LeanInteract is a Python package that allows you to interact with the Lean theorem prover.
|
|
5
|
+
Author-email: Auguste Poiroux <auguste.poiroux@epfl.ch>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2023 LeanDojo Team
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Keywords: Lean,REPL,autoformalization,theorem proving
|
|
29
|
+
Requires-Python: >=3.10
|
|
30
|
+
Requires-Dist: packaging>=24.2
|
|
31
|
+
Requires-Dist: pexpect>=4.9.0
|
|
32
|
+
Requires-Dist: psutil>=6.1.0
|
|
33
|
+
Requires-Dist: requests>=2.32.3
|
|
34
|
+
Requires-Dist: rich>=13.9.4
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# LeanInteract
|
|
38
|
+
|
|
39
|
+
**LeanInteract** is a Python package designed to seamlessly interact with Lean 4 through the [Lean REPL](https://github.com/leanprover-community/repl).
|
|
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
|
+
## Key Features
|
|
45
|
+
|
|
46
|
+
- **🚀 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`.
|
|
49
|
+
- 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
|
+
- **📦 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:
|
|
59
|
+
|
|
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:
|
|
65
|
+
|
|
66
|
+
- **[pylean](https://github.com/zhangir-azerbayev/repl)**
|
|
67
|
+
- **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**
|
|
68
|
+
|
|
69
|
+
## Installation and Setup
|
|
70
|
+
|
|
71
|
+
Requirements:
|
|
72
|
+
|
|
73
|
+
- Python >= 3.10
|
|
74
|
+
- git
|
|
75
|
+
- [Lean 4](https://leanprover-community.github.io/get_started.html)
|
|
76
|
+
|
|
77
|
+
If the requirements are met, install the LeanInteract package:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install lean-interact
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Script examples
|
|
84
|
+
|
|
85
|
+
In the `examples` directory, you will find various scripts demonstrating how to use LeanInteract:
|
|
86
|
+
|
|
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).
|
|
92
|
+
|
|
93
|
+
## Usage
|
|
94
|
+
|
|
95
|
+
### Default Lean version (latest available)
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from lean_interact import LeanREPLConfig, LeanServer
|
|
99
|
+
|
|
100
|
+
config = LeanREPLConfig() # download and build Lean REPL
|
|
101
|
+
server = LeanServer(config) # start Lean REPL
|
|
102
|
+
server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
<details>
|
|
106
|
+
<summary>Output</summary>
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
{"sorries": [{"proofState": 0,
|
|
110
|
+
"pos": {"line": 1, "column": 40},
|
|
111
|
+
"goal": "n : Nat\n⊢ n = 5 → n = 5",
|
|
112
|
+
"endPos": {"line": 1, "column": 45}}],
|
|
113
|
+
"messages": [{"severity": "warning",
|
|
114
|
+
"pos": {"line": 1, "column": 8},
|
|
115
|
+
"endPos": {"line": 1, "column": 10},
|
|
116
|
+
"data": "declaration uses 'sorry'"}],
|
|
117
|
+
"env": 0}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
</details>
|
|
121
|
+
|
|
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:
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
server.run_code("theorem ex2 (x : Nat) : x = 5 → x = 5 := by\n exact ex x", env=0)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
<details>
|
|
172
|
+
<summary>Output</summary>
|
|
173
|
+
|
|
174
|
+
```json
|
|
175
|
+
{"env": 1}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
</details>
|
|
179
|
+
|
|
180
|
+
> [!NOTE]
|
|
181
|
+
> 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
|
+
|
|
183
|
+
### Using a specific Lean version
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
config = LeanREPLConfig(lean_version="v4.7.0")
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Using an existing Lean project directory
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
config = LeanREPLConfig(project_dir="path/to/your/project")
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
You can then use `run_code`, `run_tactic` and `run_file` as usual:
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
server = LeanServer(config)
|
|
199
|
+
server.run_file("file.lean")
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
> [!IMPORTANT]
|
|
203
|
+
> Ensure the project in `project_dir` has been *successfully* built with `lake build` before using the REPL.
|
|
204
|
+
|
|
205
|
+
### Using a temporary project with dependencies
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
config = LeanREPLConfig(lean_version="v4.7.0", require=[LeanRequire(
|
|
209
|
+
name="mathlib",
|
|
210
|
+
git="https://github.com/leanprover-community/mathlib4.git",
|
|
211
|
+
rev="v4.7.0"
|
|
212
|
+
)])
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Mathlib being a frequent requirement, a shortcut is available:
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
config = LeanREPLConfig(lean_version="v4.7.0", require="mathlib")
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
You can then use Mathlib:
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
server = LeanServer(config_readme_mathlib)
|
|
225
|
+
server.run_code("""import Mathlib
|
|
226
|
+
theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x + y ) := sorry""")
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
<details>
|
|
230
|
+
<summary>Output</summary>
|
|
231
|
+
|
|
232
|
+
```json
|
|
233
|
+
{"sorries": [{"proofState": 0,
|
|
234
|
+
"pos": {"line": 4, "column": 26},
|
|
235
|
+
"goal": "x : ℝ\ny : ℚ\n⊢ Irrational (x + ↑y)",
|
|
236
|
+
"endPos": {"line": 4, "column": 31}}],
|
|
237
|
+
"messages": [{"severity": "warning",
|
|
238
|
+
"pos": {"line": 3, "column": 8},
|
|
239
|
+
"endPos": {"line": 3, "column": 18},
|
|
240
|
+
"data": "declaration uses 'sorry'"}],
|
|
241
|
+
"env": 0}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
</details>
|
|
245
|
+
|
|
246
|
+
> [!NOTE]
|
|
247
|
+
>
|
|
248
|
+
> - 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
|
+
> - A separate cache is used for each unique set of dependencies.
|
|
251
|
+
|
|
252
|
+
## Advanced options
|
|
253
|
+
|
|
254
|
+
### LeanServer
|
|
255
|
+
|
|
256
|
+
Two versions of Lean servers are available:
|
|
257
|
+
|
|
258
|
+
- **`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.
|
|
260
|
+
|
|
261
|
+
> [!TIP]
|
|
262
|
+
>
|
|
263
|
+
> - To run multiple requests in parallel, we recommend using multiprocessing with one `AutoLeanServer` instance per process.
|
|
264
|
+
> - Make sure to instantiate `LeanREPLConfig` before starting the processes to avoid conflicts during Lean REPL's download and build.
|
|
265
|
+
> - 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
|
+
|
|
267
|
+
### Helper Methods and Variables
|
|
268
|
+
|
|
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.
|
|
270
|
+
|
|
271
|
+
### Custom Lean REPL
|
|
272
|
+
|
|
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).
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# LeanInteract
|
|
2
|
+
|
|
3
|
+
**LeanInteract** is a Python package designed to seamlessly interact with Lean 4 through the [Lean REPL](https://github.com/leanprover-community/repl).
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> 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.
|
|
7
|
+
|
|
8
|
+
## Key Features
|
|
9
|
+
|
|
10
|
+
- **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction, enabling quick experimentation.
|
|
11
|
+
- **🔗 Interactivity**: Execute Lean code, files, tactics, and (partial) proofs directly from Python. Easily iterate through environment and proof states.
|
|
12
|
+
- **🔧 Compatibility**: Supports Lean versions from `v4.7.0-rc1` to `v4.16.0-rc2`.
|
|
13
|
+
- Ensures compatibility with various Lean projects and machine learning benchmarks.
|
|
14
|
+
- Need older versions? Open an issue [here](https://github.com/augustepoiroux/repl).
|
|
15
|
+
- **🔄 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.
|
|
16
|
+
- **📥 Automatic Setup**: Automatically downloads and builds Lean REPL versions for you. Versions are cached for fast reuse.
|
|
17
|
+
- **📦 Temporary Projects**: Easily instantiate temporary Lean environments with dependencies.
|
|
18
|
+
- 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.
|
|
19
|
+
|
|
20
|
+
## Similar tools
|
|
21
|
+
|
|
22
|
+
We recommend checking out these tools:
|
|
23
|
+
|
|
24
|
+
- **[PyPantograph](https://github.com/lenianiva/PyPantograph)**: Based on Pantograph, offering more options for proof interactions than Lean REPL.
|
|
25
|
+
- **[LeanDojo](https://github.com/lean-dojo/LeanDojo)**: Parses Lean projects to create datasets and interact with theorems to prove them.
|
|
26
|
+
- **[leanclient](https://github.com/oOo0oOo/leanclient)**: Interact with the Lean LSP server.
|
|
27
|
+
|
|
28
|
+
LeanInteract is inspired by:
|
|
29
|
+
|
|
30
|
+
- **[pylean](https://github.com/zhangir-azerbayev/repl)**
|
|
31
|
+
- **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**
|
|
32
|
+
|
|
33
|
+
## Installation and Setup
|
|
34
|
+
|
|
35
|
+
Requirements:
|
|
36
|
+
|
|
37
|
+
- Python >= 3.10
|
|
38
|
+
- git
|
|
39
|
+
- [Lean 4](https://leanprover-community.github.io/get_started.html)
|
|
40
|
+
|
|
41
|
+
If the requirements are met, install the LeanInteract package:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install lean-interact
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Script examples
|
|
48
|
+
|
|
49
|
+
In the `examples` directory, you will find various scripts demonstrating how to use LeanInteract:
|
|
50
|
+
|
|
51
|
+
- `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.
|
|
52
|
+
- `proofnetsharp_type_check.py`: type check theorems from the [ProofNet#](https://github.com/zhangir-azerbayev/ProofNet) benchmark.
|
|
53
|
+
- `proofnetsharp_false_theorems.py`: find theorems that can be proven false from the [ProofNet#](https://github.com/zhangir-azerbayev/ProofNet) benchmark.
|
|
54
|
+
- `proofnet_metric_beq_plus.py`: run a simple [BEq](https://openreview.net/forum?id=hUb2At2DsQ) checker on the ProofNet-Metric benchmark.
|
|
55
|
+
- `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).
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
### Default Lean version (latest available)
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from lean_interact import LeanREPLConfig, LeanServer
|
|
63
|
+
|
|
64
|
+
config = LeanREPLConfig() # download and build Lean REPL
|
|
65
|
+
server = LeanServer(config) # start Lean REPL
|
|
66
|
+
server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
<details>
|
|
70
|
+
<summary>Output</summary>
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{"sorries": [{"proofState": 0,
|
|
74
|
+
"pos": {"line": 1, "column": 40},
|
|
75
|
+
"goal": "n : Nat\n⊢ n = 5 → n = 5",
|
|
76
|
+
"endPos": {"line": 1, "column": 45}}],
|
|
77
|
+
"messages": [{"severity": "warning",
|
|
78
|
+
"pos": {"line": 1, "column": 8},
|
|
79
|
+
"endPos": {"line": 1, "column": 10},
|
|
80
|
+
"data": "declaration uses 'sorry'"}],
|
|
81
|
+
"env": 0}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
</details>
|
|
85
|
+
|
|
86
|
+
You can then iterate on the proof state by executing tactics:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
server.run_tactic("intro h", proof_state=0)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
<details>
|
|
93
|
+
<summary>Output</summary>
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{"proofState": 1, "goals": ["n : Nat\nh : n = 5\n⊢ n = 5"]}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
</details>
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
server.run_tactic("exact h", proof_state=1)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
<details>
|
|
106
|
+
<summary>Output</summary>
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
{"proofState": 2, "goals": []}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
</details>
|
|
113
|
+
|
|
114
|
+
Or by running the entire proof:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
server.run_proof("intro h\nexact h", proof_state=0)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
<details>
|
|
121
|
+
<summary>Output</summary>
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{"proofState": 3, "goals": []}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
</details>
|
|
128
|
+
|
|
129
|
+
You can also iterate on the environment:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
server.run_code("theorem ex2 (x : Nat) : x = 5 → x = 5 := by\n exact ex x", env=0)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
<details>
|
|
136
|
+
<summary>Output</summary>
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{"env": 1}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
</details>
|
|
143
|
+
|
|
144
|
+
> [!NOTE]
|
|
145
|
+
> 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.
|
|
146
|
+
|
|
147
|
+
### Using a specific Lean version
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
config = LeanREPLConfig(lean_version="v4.7.0")
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Using an existing Lean project directory
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
config = LeanREPLConfig(project_dir="path/to/your/project")
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
You can then use `run_code`, `run_tactic` and `run_file` as usual:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
server = LeanServer(config)
|
|
163
|
+
server.run_file("file.lean")
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
> [!IMPORTANT]
|
|
167
|
+
> Ensure the project in `project_dir` has been *successfully* built with `lake build` before using the REPL.
|
|
168
|
+
|
|
169
|
+
### Using a temporary project with dependencies
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
config = LeanREPLConfig(lean_version="v4.7.0", require=[LeanRequire(
|
|
173
|
+
name="mathlib",
|
|
174
|
+
git="https://github.com/leanprover-community/mathlib4.git",
|
|
175
|
+
rev="v4.7.0"
|
|
176
|
+
)])
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Mathlib being a frequent requirement, a shortcut is available:
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
config = LeanREPLConfig(lean_version="v4.7.0", require="mathlib")
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
You can then use Mathlib:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
server = LeanServer(config_readme_mathlib)
|
|
189
|
+
server.run_code("""import Mathlib
|
|
190
|
+
theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x + y ) := sorry""")
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
<details>
|
|
194
|
+
<summary>Output</summary>
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
{"sorries": [{"proofState": 0,
|
|
198
|
+
"pos": {"line": 4, "column": 26},
|
|
199
|
+
"goal": "x : ℝ\ny : ℚ\n⊢ Irrational (x + ↑y)",
|
|
200
|
+
"endPos": {"line": 4, "column": 31}}],
|
|
201
|
+
"messages": [{"severity": "warning",
|
|
202
|
+
"pos": {"line": 3, "column": 8},
|
|
203
|
+
"endPos": {"line": 3, "column": 18},
|
|
204
|
+
"data": "declaration uses 'sorry'"}],
|
|
205
|
+
"env": 0}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
</details>
|
|
209
|
+
|
|
210
|
+
> [!NOTE]
|
|
211
|
+
>
|
|
212
|
+
> - Mathlib is a large library and may take some time to download and build.
|
|
213
|
+
> - 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.
|
|
214
|
+
> - A separate cache is used for each unique set of dependencies.
|
|
215
|
+
|
|
216
|
+
## Advanced options
|
|
217
|
+
|
|
218
|
+
### LeanServer
|
|
219
|
+
|
|
220
|
+
Two versions of Lean servers are available:
|
|
221
|
+
|
|
222
|
+
- **`LeanServer`**: A wrapper around Lean REPL. Interact with it using `run_code`, `run_file`, and `run_tactic` methods.
|
|
223
|
+
- **`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.
|
|
224
|
+
|
|
225
|
+
> [!TIP]
|
|
226
|
+
>
|
|
227
|
+
> - To run multiple requests in parallel, we recommend using multiprocessing with one `AutoLeanServer` instance per process.
|
|
228
|
+
> - Make sure to instantiate `LeanREPLConfig` before starting the processes to avoid conflicts during Lean REPL's download and build.
|
|
229
|
+
> - 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.
|
|
230
|
+
|
|
231
|
+
### Helper Methods and Variables
|
|
232
|
+
|
|
233
|
+
- `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.
|
|
234
|
+
|
|
235
|
+
### Custom Lean REPL
|
|
236
|
+
|
|
237
|
+
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).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# TODO
|
|
2
|
+
|
|
3
|
+
- (hard) Handle timeout more properly, i.e. directly from within the Lean REPL
|
|
4
|
+
- Pre-compile binaries so that users don't have to compile them themselves
|
|
5
|
+
- Add example scripts
|
|
6
|
+
- Make sure that AutoLeanServer doesn't restart between running a command and storing it to the session cache
|
|
7
|
+
- Modify Lean REPL to not record environment/proof states
|
|
8
|
+
- Improve session cache performance
|
|
9
|
+
- Currently it saves and reload every states independently, which might be slower thn replaying everything in the right dependency order
|
|
10
|
+
- Before I was replaying from Python the commands keeping the sequential order. Maybe I should do that again
|
|
11
|
+
- Lean REPL doesn't validate proofs, so empty goal list does not mean the proof is valid
|
|
12
|
+
- Lean REPL: add option to get the initial proof state of each declaration in a code
|
|
13
|
+
- Add tests:
|
|
14
|
+
- Check that we can't prove false
|
|
15
|
+
- Check that pickling / unpickling doesn't change declaration type (def => noncomputable def)
|
|
16
|
+
- Capture interaction with REPL in dataclasses
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "lean-interact"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "LeanInteract is a Python package that allows you to interact with the Lean theorem prover."
|
|
5
|
+
keywords = ["Lean", "theorem proving", "autoformalization", "REPL"]
|
|
6
|
+
license = { file = "LICENSE" }
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
authors = [{ name = "Auguste Poiroux", email = "auguste.poiroux@epfl.ch" }]
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"packaging>=24.2",
|
|
12
|
+
"pexpect>=4.9.0",
|
|
13
|
+
"psutil>=6.1.0",
|
|
14
|
+
"requests>=2.32.3",
|
|
15
|
+
"rich>=13.9.4"
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["hatchling"]
|
|
20
|
+
build-backend = "hatchling.build"
|
|
21
|
+
|
|
22
|
+
[dependency-groups]
|
|
23
|
+
dev = [
|
|
24
|
+
"ipykernel>=6.29.5"
|
|
25
|
+
]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
|
|
3
|
+
from lean_interact.server import AutoLeanServer, LeanREPLConfig, LeanRequire, LeanServer
|
|
4
|
+
from lean_interact.utils import clear_cache
|
|
5
|
+
|
|
6
|
+
__all__ = ["LeanREPLConfig", "LeanServer", "AutoLeanServer", "LeanRequire", "clear_cache"]
|
|
7
|
+
|
|
8
|
+
# check if lake is installed
|
|
9
|
+
if shutil.which("lake") is None:
|
|
10
|
+
raise RuntimeError(
|
|
11
|
+
"Lean 4 build system (`lake`) is not installed. You can find installation instructions here: https://leanprover-community.github.io/get_started.html"
|
|
12
|
+
)
|