lean-interact 0.2.0__tar.gz → 0.3.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.2.0 → lean_interact-0.3.0}/PKG-INFO +86 -74
- {lean_interact-0.2.0 → lean_interact-0.3.0}/README.md +84 -73
- {lean_interact-0.2.0 → lean_interact-0.3.0}/examples/beq_plus.py +117 -55
- lean_interact-0.3.0/examples/proof_generation_and_autoformalization.py +384 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/examples/type_check.py +28 -17
- {lean_interact-0.2.0 → lean_interact-0.3.0}/pyproject.toml +2 -1
- lean_interact-0.3.0/src/lean_interact/__init__.py +35 -0
- lean_interact-0.3.0/src/lean_interact/config.py +341 -0
- lean_interact-0.3.0/src/lean_interact/interface.py +172 -0
- lean_interact-0.3.0/src/lean_interact/server.py +469 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/src/lean_interact/utils.py +28 -31
- lean_interact-0.3.0/tests/test_server.py +625 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/uv.lock +126 -1
- lean_interact-0.2.0/src/lean_interact/__init__.py +0 -21
- lean_interact-0.2.0/src/lean_interact/server.py +0 -902
- lean_interact-0.2.0/tests/test_server.py +0 -429
- {lean_interact-0.2.0 → lean_interact-0.3.0}/.devcontainer/devcontainer.json +0 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/.github/workflows/ci.yml +0 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/.github/workflows/publish-to-pypi.yml +0 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/.gitignore +0 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/LICENSE +0 -0
- {lean_interact-0.2.0 → lean_interact-0.3.0}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lean-interact
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.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
|
|
@@ -30,6 +30,7 @@ Requires-Python: >=3.10
|
|
|
30
30
|
Requires-Dist: gitpython>=3.1.44
|
|
31
31
|
Requires-Dist: pexpect>=4.9.0
|
|
32
32
|
Requires-Dist: psutil>=6.1.0
|
|
33
|
+
Requires-Dist: pydantic>=2.11.1
|
|
33
34
|
Requires-Dist: requests>=2.32.3
|
|
34
35
|
Requires-Dist: rich>=13.9.4
|
|
35
36
|
Description-Content-Type: text/markdown
|
|
@@ -40,13 +41,11 @@ Description-Content-Type: text/markdown
|
|
|
40
41
|
|
|
41
42
|
## Key Features
|
|
42
43
|
|
|
43
|
-
- **🔗 Interactivity**: Execute Lean code and files directly from Python
|
|
44
|
-
- **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
-
|
|
48
|
-
- **📦 Temporary Projects**: Easily instantiate temporary Lean environments with dependencies.
|
|
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.
|
|
44
|
+
- **🔗 Interactivity**: Execute Lean code and files directly from Python.
|
|
45
|
+
- **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction.
|
|
46
|
+
- **🔧 Compatibility**: Supports all Lean versions between `v4.7.0-rc1` and `v4.18.0`.
|
|
47
|
+
- **📦 Temporary Projects**: Easily instantiate temporary Lean environments.
|
|
48
|
+
- Useful for experimenting 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).
|
|
50
49
|
|
|
51
50
|
## Installation and Setup
|
|
52
51
|
|
|
@@ -60,8 +59,7 @@ Requirements:
|
|
|
60
59
|
|
|
61
60
|
- Python >= 3.10
|
|
62
61
|
- git
|
|
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.
|
|
62
|
+
- [Lean 4](https://leanprover-community.github.io/get_started.html) (or use the `install-lean` command from LeanInteract)
|
|
65
63
|
|
|
66
64
|
> [!NOTE]
|
|
67
65
|
> This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. For Windows, use WSL.
|
|
@@ -69,41 +67,30 @@ Requirements:
|
|
|
69
67
|
|
|
70
68
|
## Script examples
|
|
71
69
|
|
|
72
|
-
In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
|
|
70
|
+
In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
|
|
73
71
|
|
|
72
|
+
- `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 on [MiniF2F](https://github.com/yangky11/miniF2F-lean4) and [ProofNet#](https://huggingface.co/datasets/PAug/ProofNetSharp) benchmarks.
|
|
74
73
|
- `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
|
|
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).
|
|
74
|
+
- `type_check.py`: optimize type checking using environment states.
|
|
75
|
+
- `statement_autoformalization_sampling.py`: sampling-based method used in [Improving Autoformalization using Type Checking](https://arxiv.org/abs/2406.07222).
|
|
81
76
|
|
|
82
77
|
## Usage
|
|
83
78
|
|
|
84
79
|
### Default Lean version (latest available)
|
|
85
80
|
|
|
86
81
|
```python
|
|
87
|
-
from lean_interact import LeanREPLConfig, LeanServer
|
|
82
|
+
from lean_interact import LeanREPLConfig, LeanServer, Command
|
|
88
83
|
|
|
89
84
|
config = LeanREPLConfig(verbose=True) # download and build Lean REPL
|
|
90
85
|
server = LeanServer(config) # start Lean REPL
|
|
91
|
-
server.
|
|
86
|
+
server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
|
|
92
87
|
```
|
|
93
88
|
|
|
94
89
|
<details>
|
|
95
90
|
<summary>Output</summary>
|
|
96
91
|
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
"pos": {"line": 1, "column": 40},
|
|
100
|
-
"goal": "n : Nat\n⊢ n = 5 → n = 5",
|
|
101
|
-
"endPos": {"line": 1, "column": 45}}],
|
|
102
|
-
"messages": [{"severity": "warning",
|
|
103
|
-
"pos": {"line": 1, "column": 8},
|
|
104
|
-
"endPos": {"line": 1, "column": 10},
|
|
105
|
-
"data": "declaration uses 'sorry'"}],
|
|
106
|
-
"env": 0}
|
|
92
|
+
```python
|
|
93
|
+
CommandResponse(env=0)
|
|
107
94
|
```
|
|
108
95
|
|
|
109
96
|
</details>
|
|
@@ -111,14 +98,14 @@ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
|
111
98
|
Iterate on the environment state:
|
|
112
99
|
|
|
113
100
|
```python
|
|
114
|
-
server.
|
|
101
|
+
server.run(Command(cmd="example (x : Nat) : x = 5 → x = 5 := by exact ex x", env=0))
|
|
115
102
|
```
|
|
116
103
|
|
|
117
104
|
<details>
|
|
118
105
|
<summary>Output</summary>
|
|
119
106
|
|
|
120
|
-
```
|
|
121
|
-
|
|
107
|
+
```python
|
|
108
|
+
CommandResponse(env=1)
|
|
122
109
|
```
|
|
123
110
|
|
|
124
111
|
</details>
|
|
@@ -144,11 +131,13 @@ or
|
|
|
144
131
|
config = LeanREPLConfig(project=GitProject("https://github.com/yangky11/lean4-example"))
|
|
145
132
|
```
|
|
146
133
|
|
|
147
|
-
You can then use `
|
|
134
|
+
You can then use `run` as usual:
|
|
148
135
|
|
|
149
136
|
```python
|
|
137
|
+
from lean_interact import FileCommand
|
|
138
|
+
|
|
150
139
|
server = LeanServer(config)
|
|
151
|
-
server.
|
|
140
|
+
server.run(FileCommand(path="file.lean"))
|
|
152
141
|
```
|
|
153
142
|
|
|
154
143
|
> [!IMPORTANT]
|
|
@@ -157,6 +146,8 @@ server.run_file("file.lean")
|
|
|
157
146
|
### Temporary project with dependencies
|
|
158
147
|
|
|
159
148
|
```python
|
|
149
|
+
from lean_interact import TempRequireProject
|
|
150
|
+
|
|
160
151
|
config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject([LeanRequire(
|
|
161
152
|
name="mathlib",
|
|
162
153
|
git="https://github.com/leanprover-community/mathlib4.git",
|
|
@@ -174,23 +165,32 @@ You can then use Mathlib as follows:
|
|
|
174
165
|
|
|
175
166
|
```python
|
|
176
167
|
server = LeanServer(config)
|
|
177
|
-
server.
|
|
178
|
-
theorem ex_mathlib (x : ℝ) (y : ℚ)
|
|
168
|
+
server.run(Command(cmd="""import Mathlib
|
|
169
|
+
theorem ex_mathlib (x : ℝ) (y : ℚ) :
|
|
170
|
+
( Irrational x ) -> Irrational ( x + y ) := sorry"""))
|
|
179
171
|
```
|
|
180
172
|
|
|
181
173
|
<details>
|
|
182
174
|
<summary>Output</summary>
|
|
183
175
|
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
176
|
+
```python
|
|
177
|
+
CommandResponse(
|
|
178
|
+
messages=[
|
|
179
|
+
Message(
|
|
180
|
+
start_pos=Pos(line=2, column=8),
|
|
181
|
+
end_pos=Pos(line=2, column=18),
|
|
182
|
+
data="declaration uses 'sorry'",
|
|
183
|
+
severity='warning'
|
|
184
|
+
)],
|
|
185
|
+
sorries=[
|
|
186
|
+
Sorry(
|
|
187
|
+
start_pos=Pos(line=3, column=46),
|
|
188
|
+
end_pos=Pos(line=3, column=51),
|
|
189
|
+
goal='x : ℝ\ny : ℚ\n⊢ Irrational x → Irrational (x + ↑y)',
|
|
190
|
+
proof_state=0
|
|
191
|
+
)],
|
|
192
|
+
env=0
|
|
193
|
+
)
|
|
194
194
|
```
|
|
195
195
|
|
|
196
196
|
</details>
|
|
@@ -205,7 +205,9 @@ theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x +
|
|
|
205
205
|
For more control over the temporary project, you can use `TemporaryProject` to specify the content of the lakefile.
|
|
206
206
|
|
|
207
207
|
```python
|
|
208
|
-
|
|
208
|
+
from lean_interact import TemporaryProject
|
|
209
|
+
|
|
210
|
+
config = LeanREPLConfig(lean_version="v4.18.0", project=TemporaryProject("""
|
|
209
211
|
import Lake
|
|
210
212
|
open Lake DSL
|
|
211
213
|
|
|
@@ -217,29 +219,37 @@ lean_exe "dummy" where
|
|
|
217
219
|
root := `Main
|
|
218
220
|
|
|
219
221
|
require mathlib from git
|
|
220
|
-
"https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0
|
|
222
|
+
"https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0"
|
|
221
223
|
"""))
|
|
222
224
|
```
|
|
223
225
|
|
|
224
|
-
### Tactic
|
|
226
|
+
### Tactic mode (experimental)
|
|
225
227
|
|
|
226
228
|
```python
|
|
227
|
-
server.
|
|
229
|
+
server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := sorry"))
|
|
228
230
|
```
|
|
229
231
|
|
|
230
232
|
<details>
|
|
231
233
|
<summary>Output</summary>
|
|
232
234
|
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
235
|
+
```python
|
|
236
|
+
CommandResponse(
|
|
237
|
+
messages=[
|
|
238
|
+
Message(
|
|
239
|
+
start_pos=Pos(line=1, column=8),
|
|
240
|
+
end_pos=Pos(line=1, column=10),
|
|
241
|
+
data="declaration uses 'sorry'",
|
|
242
|
+
severity='warning'
|
|
243
|
+
)],
|
|
244
|
+
sorries=[
|
|
245
|
+
Sorry(
|
|
246
|
+
start_pos=Pos(line=1, column=40),
|
|
247
|
+
end_pos=Pos(line=1, column=45),
|
|
248
|
+
goal='n : Nat\n⊢ n = 5 → n = 5',
|
|
249
|
+
proof_state=0
|
|
250
|
+
)],
|
|
251
|
+
env=0
|
|
252
|
+
)
|
|
243
253
|
```
|
|
244
254
|
|
|
245
255
|
</details>
|
|
@@ -247,50 +257,52 @@ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
|
247
257
|
You can then iterate on the proof state by executing tactics:
|
|
248
258
|
|
|
249
259
|
```python
|
|
250
|
-
server.
|
|
260
|
+
server.run(ProofStep(tactic="intro h", proof_state=0))
|
|
251
261
|
```
|
|
252
262
|
|
|
253
263
|
<details>
|
|
254
264
|
<summary>Output</summary>
|
|
255
265
|
|
|
256
|
-
```
|
|
257
|
-
|
|
266
|
+
```python
|
|
267
|
+
ProofStepResponse(goals=['n : Nat\nh : n = 5\n⊢ n = 5'], proof_state=1)
|
|
258
268
|
```
|
|
259
269
|
|
|
260
270
|
</details>
|
|
261
271
|
|
|
262
272
|
```python
|
|
263
|
-
server.
|
|
273
|
+
server.run(ProofStep(tactic="exact h", proof_state=1))
|
|
264
274
|
```
|
|
265
275
|
|
|
266
276
|
<details>
|
|
267
277
|
<summary>Output</summary>
|
|
268
278
|
|
|
269
|
-
```
|
|
270
|
-
|
|
279
|
+
```python
|
|
280
|
+
ProofStepResponse(goals=[], proof_state=2)
|
|
271
281
|
```
|
|
272
282
|
|
|
273
283
|
</details>
|
|
274
284
|
|
|
275
|
-
or by running the entire
|
|
285
|
+
or by directly running the entire proof:
|
|
276
286
|
|
|
277
287
|
```python
|
|
278
|
-
server.
|
|
288
|
+
server.run(ProofStep(tactic="(\nintro h\nexact h)", proof_state=0))
|
|
279
289
|
```
|
|
280
290
|
|
|
281
291
|
<details>
|
|
282
292
|
<summary>Output</summary>
|
|
283
293
|
|
|
284
|
-
```
|
|
285
|
-
|
|
294
|
+
```python
|
|
295
|
+
ProofStepResponse(goals=[], proof_state=3)
|
|
286
296
|
```
|
|
287
297
|
|
|
288
298
|
</details>
|
|
289
299
|
|
|
290
300
|
## Helper Commands
|
|
291
301
|
|
|
292
|
-
|
|
293
|
-
|
|
302
|
+
The following commands are installed with LeanInteract:
|
|
303
|
+
|
|
304
|
+
- `install-lean`: Installs Lean 4 version manager `elan`.
|
|
305
|
+
- `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
306
|
|
|
295
307
|
## Advanced options
|
|
296
308
|
|
|
@@ -298,8 +310,8 @@ server.run_proof("intro h\nexact h", proof_state=0)
|
|
|
298
310
|
|
|
299
311
|
Two versions of Lean servers are available:
|
|
300
312
|
|
|
301
|
-
- **`LeanServer`**: A wrapper around Lean REPL. Interact with it using
|
|
302
|
-
- **`AutoLeanServer`**: An experimental subclass of `LeanServer` automatically recovering from crashes and timeouts. It also monitors memory usage to limit *out of memory*
|
|
313
|
+
- **`LeanServer`**: A wrapper around Lean REPL. Interact with it using the `run` method.
|
|
314
|
+
- **`AutoLeanServer`**: An experimental subclass of `LeanServer` automatically recovering from some crashes and timeouts. It also monitors memory usage to limit *out of memory* issues in multiprocessing contexts. Use the `add_to_session_cache` attribute available in the `run` method to prevent selected environment/proof states to be cleared.
|
|
303
315
|
|
|
304
316
|
> [!TIP]
|
|
305
317
|
>
|
|
@@ -4,13 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
## Key Features
|
|
6
6
|
|
|
7
|
-
- **🔗 Interactivity**: Execute Lean code and files directly from Python
|
|
8
|
-
- **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction
|
|
9
|
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
- **📦 Temporary Projects**: Easily instantiate temporary Lean environments with dependencies.
|
|
13
|
-
- 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.
|
|
7
|
+
- **🔗 Interactivity**: Execute Lean code and files directly from Python.
|
|
8
|
+
- **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction.
|
|
9
|
+
- **🔧 Compatibility**: Supports all Lean versions between `v4.7.0-rc1` and `v4.18.0`.
|
|
10
|
+
- **📦 Temporary Projects**: Easily instantiate temporary Lean environments.
|
|
11
|
+
- Useful for experimenting 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).
|
|
14
12
|
|
|
15
13
|
## Installation and Setup
|
|
16
14
|
|
|
@@ -24,8 +22,7 @@ Requirements:
|
|
|
24
22
|
|
|
25
23
|
- Python >= 3.10
|
|
26
24
|
- git
|
|
27
|
-
- [Lean 4](https://leanprover-community.github.io/get_started.html)
|
|
28
|
-
- Tip: the `install-lean` command can install Lean 4 for you after installing LeanInteract.
|
|
25
|
+
- [Lean 4](https://leanprover-community.github.io/get_started.html) (or use the `install-lean` command from LeanInteract)
|
|
29
26
|
|
|
30
27
|
> [!NOTE]
|
|
31
28
|
> This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. For Windows, use WSL.
|
|
@@ -33,41 +30,30 @@ Requirements:
|
|
|
33
30
|
|
|
34
31
|
## Script examples
|
|
35
32
|
|
|
36
|
-
In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
|
|
33
|
+
In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
|
|
37
34
|
|
|
35
|
+
- `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 on [MiniF2F](https://github.com/yangky11/miniF2F-lean4) and [ProofNet#](https://huggingface.co/datasets/PAug/ProofNetSharp) benchmarks.
|
|
38
36
|
- `beq_plus.py`: run the autoformalization [BEq+](https://arxiv.org/abs/2406.07222) metric on the [ProofNetVerif](https://huggingface.co/datasets/PAug/ProofNetVerif) benchmark.
|
|
39
|
-
- `type_check.py`: optimize type checking
|
|
40
|
-
|
|
41
|
-
Soon to be added:
|
|
42
|
-
|
|
43
|
-
- `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.
|
|
44
|
-
- `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).
|
|
37
|
+
- `type_check.py`: optimize type checking using environment states.
|
|
38
|
+
- `statement_autoformalization_sampling.py`: sampling-based method used in [Improving Autoformalization using Type Checking](https://arxiv.org/abs/2406.07222).
|
|
45
39
|
|
|
46
40
|
## Usage
|
|
47
41
|
|
|
48
42
|
### Default Lean version (latest available)
|
|
49
43
|
|
|
50
44
|
```python
|
|
51
|
-
from lean_interact import LeanREPLConfig, LeanServer
|
|
45
|
+
from lean_interact import LeanREPLConfig, LeanServer, Command
|
|
52
46
|
|
|
53
47
|
config = LeanREPLConfig(verbose=True) # download and build Lean REPL
|
|
54
48
|
server = LeanServer(config) # start Lean REPL
|
|
55
|
-
server.
|
|
49
|
+
server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
|
|
56
50
|
```
|
|
57
51
|
|
|
58
52
|
<details>
|
|
59
53
|
<summary>Output</summary>
|
|
60
54
|
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
"pos": {"line": 1, "column": 40},
|
|
64
|
-
"goal": "n : Nat\n⊢ n = 5 → n = 5",
|
|
65
|
-
"endPos": {"line": 1, "column": 45}}],
|
|
66
|
-
"messages": [{"severity": "warning",
|
|
67
|
-
"pos": {"line": 1, "column": 8},
|
|
68
|
-
"endPos": {"line": 1, "column": 10},
|
|
69
|
-
"data": "declaration uses 'sorry'"}],
|
|
70
|
-
"env": 0}
|
|
55
|
+
```python
|
|
56
|
+
CommandResponse(env=0)
|
|
71
57
|
```
|
|
72
58
|
|
|
73
59
|
</details>
|
|
@@ -75,14 +61,14 @@ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
|
75
61
|
Iterate on the environment state:
|
|
76
62
|
|
|
77
63
|
```python
|
|
78
|
-
server.
|
|
64
|
+
server.run(Command(cmd="example (x : Nat) : x = 5 → x = 5 := by exact ex x", env=0))
|
|
79
65
|
```
|
|
80
66
|
|
|
81
67
|
<details>
|
|
82
68
|
<summary>Output</summary>
|
|
83
69
|
|
|
84
|
-
```
|
|
85
|
-
|
|
70
|
+
```python
|
|
71
|
+
CommandResponse(env=1)
|
|
86
72
|
```
|
|
87
73
|
|
|
88
74
|
</details>
|
|
@@ -108,11 +94,13 @@ or
|
|
|
108
94
|
config = LeanREPLConfig(project=GitProject("https://github.com/yangky11/lean4-example"))
|
|
109
95
|
```
|
|
110
96
|
|
|
111
|
-
You can then use `
|
|
97
|
+
You can then use `run` as usual:
|
|
112
98
|
|
|
113
99
|
```python
|
|
100
|
+
from lean_interact import FileCommand
|
|
101
|
+
|
|
114
102
|
server = LeanServer(config)
|
|
115
|
-
server.
|
|
103
|
+
server.run(FileCommand(path="file.lean"))
|
|
116
104
|
```
|
|
117
105
|
|
|
118
106
|
> [!IMPORTANT]
|
|
@@ -121,6 +109,8 @@ server.run_file("file.lean")
|
|
|
121
109
|
### Temporary project with dependencies
|
|
122
110
|
|
|
123
111
|
```python
|
|
112
|
+
from lean_interact import TempRequireProject
|
|
113
|
+
|
|
124
114
|
config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject([LeanRequire(
|
|
125
115
|
name="mathlib",
|
|
126
116
|
git="https://github.com/leanprover-community/mathlib4.git",
|
|
@@ -138,23 +128,32 @@ You can then use Mathlib as follows:
|
|
|
138
128
|
|
|
139
129
|
```python
|
|
140
130
|
server = LeanServer(config)
|
|
141
|
-
server.
|
|
142
|
-
theorem ex_mathlib (x : ℝ) (y : ℚ)
|
|
131
|
+
server.run(Command(cmd="""import Mathlib
|
|
132
|
+
theorem ex_mathlib (x : ℝ) (y : ℚ) :
|
|
133
|
+
( Irrational x ) -> Irrational ( x + y ) := sorry"""))
|
|
143
134
|
```
|
|
144
135
|
|
|
145
136
|
<details>
|
|
146
137
|
<summary>Output</summary>
|
|
147
138
|
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
139
|
+
```python
|
|
140
|
+
CommandResponse(
|
|
141
|
+
messages=[
|
|
142
|
+
Message(
|
|
143
|
+
start_pos=Pos(line=2, column=8),
|
|
144
|
+
end_pos=Pos(line=2, column=18),
|
|
145
|
+
data="declaration uses 'sorry'",
|
|
146
|
+
severity='warning'
|
|
147
|
+
)],
|
|
148
|
+
sorries=[
|
|
149
|
+
Sorry(
|
|
150
|
+
start_pos=Pos(line=3, column=46),
|
|
151
|
+
end_pos=Pos(line=3, column=51),
|
|
152
|
+
goal='x : ℝ\ny : ℚ\n⊢ Irrational x → Irrational (x + ↑y)',
|
|
153
|
+
proof_state=0
|
|
154
|
+
)],
|
|
155
|
+
env=0
|
|
156
|
+
)
|
|
158
157
|
```
|
|
159
158
|
|
|
160
159
|
</details>
|
|
@@ -169,7 +168,9 @@ theorem ex_mathlib (x : ℝ) (y : ℚ) :\n ( Irrational x ) -> Irrational ( x +
|
|
|
169
168
|
For more control over the temporary project, you can use `TemporaryProject` to specify the content of the lakefile.
|
|
170
169
|
|
|
171
170
|
```python
|
|
172
|
-
|
|
171
|
+
from lean_interact import TemporaryProject
|
|
172
|
+
|
|
173
|
+
config = LeanREPLConfig(lean_version="v4.18.0", project=TemporaryProject("""
|
|
173
174
|
import Lake
|
|
174
175
|
open Lake DSL
|
|
175
176
|
|
|
@@ -181,29 +182,37 @@ lean_exe "dummy" where
|
|
|
181
182
|
root := `Main
|
|
182
183
|
|
|
183
184
|
require mathlib from git
|
|
184
|
-
"https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0
|
|
185
|
+
"https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0"
|
|
185
186
|
"""))
|
|
186
187
|
```
|
|
187
188
|
|
|
188
|
-
### Tactic
|
|
189
|
+
### Tactic mode (experimental)
|
|
189
190
|
|
|
190
191
|
```python
|
|
191
|
-
server.
|
|
192
|
+
server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := sorry"))
|
|
192
193
|
```
|
|
193
194
|
|
|
194
195
|
<details>
|
|
195
196
|
<summary>Output</summary>
|
|
196
197
|
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
198
|
+
```python
|
|
199
|
+
CommandResponse(
|
|
200
|
+
messages=[
|
|
201
|
+
Message(
|
|
202
|
+
start_pos=Pos(line=1, column=8),
|
|
203
|
+
end_pos=Pos(line=1, column=10),
|
|
204
|
+
data="declaration uses 'sorry'",
|
|
205
|
+
severity='warning'
|
|
206
|
+
)],
|
|
207
|
+
sorries=[
|
|
208
|
+
Sorry(
|
|
209
|
+
start_pos=Pos(line=1, column=40),
|
|
210
|
+
end_pos=Pos(line=1, column=45),
|
|
211
|
+
goal='n : Nat\n⊢ n = 5 → n = 5',
|
|
212
|
+
proof_state=0
|
|
213
|
+
)],
|
|
214
|
+
env=0
|
|
215
|
+
)
|
|
207
216
|
```
|
|
208
217
|
|
|
209
218
|
</details>
|
|
@@ -211,50 +220,52 @@ server.run_code("theorem ex (n : Nat) : n = 5 → n = 5 := sorry")
|
|
|
211
220
|
You can then iterate on the proof state by executing tactics:
|
|
212
221
|
|
|
213
222
|
```python
|
|
214
|
-
server.
|
|
223
|
+
server.run(ProofStep(tactic="intro h", proof_state=0))
|
|
215
224
|
```
|
|
216
225
|
|
|
217
226
|
<details>
|
|
218
227
|
<summary>Output</summary>
|
|
219
228
|
|
|
220
|
-
```
|
|
221
|
-
|
|
229
|
+
```python
|
|
230
|
+
ProofStepResponse(goals=['n : Nat\nh : n = 5\n⊢ n = 5'], proof_state=1)
|
|
222
231
|
```
|
|
223
232
|
|
|
224
233
|
</details>
|
|
225
234
|
|
|
226
235
|
```python
|
|
227
|
-
server.
|
|
236
|
+
server.run(ProofStep(tactic="exact h", proof_state=1))
|
|
228
237
|
```
|
|
229
238
|
|
|
230
239
|
<details>
|
|
231
240
|
<summary>Output</summary>
|
|
232
241
|
|
|
233
|
-
```
|
|
234
|
-
|
|
242
|
+
```python
|
|
243
|
+
ProofStepResponse(goals=[], proof_state=2)
|
|
235
244
|
```
|
|
236
245
|
|
|
237
246
|
</details>
|
|
238
247
|
|
|
239
|
-
or by running the entire
|
|
248
|
+
or by directly running the entire proof:
|
|
240
249
|
|
|
241
250
|
```python
|
|
242
|
-
server.
|
|
251
|
+
server.run(ProofStep(tactic="(\nintro h\nexact h)", proof_state=0))
|
|
243
252
|
```
|
|
244
253
|
|
|
245
254
|
<details>
|
|
246
255
|
<summary>Output</summary>
|
|
247
256
|
|
|
248
|
-
```
|
|
249
|
-
|
|
257
|
+
```python
|
|
258
|
+
ProofStepResponse(goals=[], proof_state=3)
|
|
250
259
|
```
|
|
251
260
|
|
|
252
261
|
</details>
|
|
253
262
|
|
|
254
263
|
## Helper Commands
|
|
255
264
|
|
|
256
|
-
|
|
257
|
-
|
|
265
|
+
The following commands are installed with LeanInteract:
|
|
266
|
+
|
|
267
|
+
- `install-lean`: Installs Lean 4 version manager `elan`.
|
|
268
|
+
- `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.
|
|
258
269
|
|
|
259
270
|
## Advanced options
|
|
260
271
|
|
|
@@ -262,8 +273,8 @@ server.run_proof("intro h\nexact h", proof_state=0)
|
|
|
262
273
|
|
|
263
274
|
Two versions of Lean servers are available:
|
|
264
275
|
|
|
265
|
-
- **`LeanServer`**: A wrapper around Lean REPL. Interact with it using
|
|
266
|
-
- **`AutoLeanServer`**: An experimental subclass of `LeanServer` automatically recovering from crashes and timeouts. It also monitors memory usage to limit *out of memory*
|
|
276
|
+
- **`LeanServer`**: A wrapper around Lean REPL. Interact with it using the `run` method.
|
|
277
|
+
- **`AutoLeanServer`**: An experimental subclass of `LeanServer` automatically recovering from some crashes and timeouts. It also monitors memory usage to limit *out of memory* issues in multiprocessing contexts. Use the `add_to_session_cache` attribute available in the `run` method to prevent selected environment/proof states to be cleared.
|
|
267
278
|
|
|
268
279
|
> [!TIP]
|
|
269
280
|
>
|