lean-interact 0.3.3__tar.gz → 0.4.1__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.
Files changed (21) hide show
  1. {lean_interact-0.3.3 → lean_interact-0.4.1}/.github/workflows/ci.yml +2 -6
  2. {lean_interact-0.3.3 → lean_interact-0.4.1}/PKG-INFO +252 -79
  3. lean_interact-0.4.1/README.md +470 -0
  4. {lean_interact-0.3.3 → lean_interact-0.4.1}/examples/proof_generation_and_autoformalization.py +1 -1
  5. {lean_interact-0.3.3 → lean_interact-0.4.1}/examples/type_check.py +1 -1
  6. {lean_interact-0.3.3 → lean_interact-0.4.1}/pyproject.toml +2 -1
  7. {lean_interact-0.3.3 → lean_interact-0.4.1}/src/lean_interact/config.py +88 -81
  8. {lean_interact-0.3.3 → lean_interact-0.4.1}/src/lean_interact/interface.py +109 -3
  9. {lean_interact-0.3.3 → lean_interact-0.4.1}/src/lean_interact/server.py +45 -34
  10. {lean_interact-0.3.3 → lean_interact-0.4.1}/src/lean_interact/utils.py +1 -1
  11. lean_interact-0.4.1/tests/test_concurrency.py +163 -0
  12. {lean_interact-0.3.3 → lean_interact-0.4.1}/tests/test_server.py +17 -13
  13. {lean_interact-0.3.3 → lean_interact-0.4.1}/uv.lock +12 -1
  14. lean_interact-0.3.3/README.md +0 -298
  15. {lean_interact-0.3.3 → lean_interact-0.4.1}/.devcontainer/devcontainer.json +0 -0
  16. {lean_interact-0.3.3 → lean_interact-0.4.1}/.github/workflows/publish-to-pypi.yml +0 -0
  17. {lean_interact-0.3.3 → lean_interact-0.4.1}/.gitignore +0 -0
  18. {lean_interact-0.3.3 → lean_interact-0.4.1}/LICENSE +0 -0
  19. {lean_interact-0.3.3 → lean_interact-0.4.1}/examples/beq_plus.py +0 -0
  20. {lean_interact-0.3.3 → lean_interact-0.4.1}/src/lean_interact/__init__.py +0 -0
  21. {lean_interact-0.3.3 → lean_interact-0.4.1}/tests/test_utils.py +0 -0
@@ -9,12 +9,6 @@ jobs:
9
9
  - name: Checkout
10
10
  uses: actions/checkout@v3
11
11
 
12
- - name: Install elan
13
- run: |
14
- set -o pipefail
15
- curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y --default-toolchain none
16
- echo "$HOME/.elan/bin" >> $GITHUB_PATH
17
-
18
12
  - name: Set up Python
19
13
  uses: actions/setup-python@v4
20
14
  with:
@@ -24,6 +18,8 @@ jobs:
24
18
  run: |
25
19
  pip install --upgrade pip
26
20
  pip install .
21
+ install-lean
22
+ echo "$HOME/.elan/bin" >> $GITHUB_PATH
27
23
 
28
24
  - name: Run tests
29
25
  run: python -m unittest discover -s ./tests
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lean-interact
3
- Version: 0.3.3
3
+ Version: 0.4.1
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
@@ -27,6 +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: filelock>=3.18.0
30
31
  Requires-Dist: gitpython>=3.1.44
31
32
  Requires-Dist: pexpect>=4.9.0
32
33
  Requires-Dist: psutil>=6.1.0
@@ -37,6 +38,10 @@ Description-Content-Type: text/markdown
37
38
 
38
39
  # LeanInteract
39
40
 
41
+ [![PyPI version](https://badge.fury.io/py/lean-interact.svg)](https://badge.fury.io/py/lean-interact)
42
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
43
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
44
+
40
45
  **LeanInteract** is a Python package designed to seamlessly interact with Lean 4 through the [Lean REPL](https://github.com/leanprover-community/repl).
41
46
 
42
47
  ## Key Features
@@ -44,9 +49,39 @@ Description-Content-Type: text/markdown
44
49
  - **🔗 Interactivity**: Execute Lean code and files directly from Python.
45
50
  - **🚀 Ease of Use**: LeanInteract abstracts the complexities of Lean setup and interaction.
46
51
  - **🔧 Compatibility**: Supports all Lean versions between `v4.7.0-rc1` and `v4.19.0-rc2`.
52
+ - We backport the latest features of Lean REPL to older versions of Lean.
47
53
  - **📦 Temporary Projects**: Easily instantiate temporary Lean environments.
48
54
  - 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).
49
55
 
56
+ ## Table of Contents
57
+
58
+ - [Key Features](#key-features)
59
+ - [Installation and Setup](#installation-and-setup)
60
+ - [Script examples](#script-examples)
61
+ - [Usage](#usage)
62
+ - [Basic example](#basic-example)
63
+ - [Tactic mode](#tactic-mode)
64
+ - [Custom Lean configuration](#custom-lean-configuration)
65
+ - [Specific Lean version](#specific-lean-version)
66
+ - [Existing Lean projects](#existing-lean-projects)
67
+ - [Temporary project with dependencies](#temporary-project-with-dependencies)
68
+ - [Fine-grained temporary project](#fine-grained-temporary-project)
69
+ - [Available Queries](#available-queries)
70
+ - [Command](#command)
71
+ - [FileCommand](#filecommand)
72
+ - [ProofStep](#proofstep)
73
+ - [Environment Pickling](#environment-pickling)
74
+ - [ProofState Pickling](#proofstate-pickling)
75
+ - [Helper Commands](#helper-commands)
76
+ - [Advanced options](#advanced-options)
77
+ - [LeanServer](#leanserver)
78
+ - [Custom Lean REPL](#custom-lean-repl)
79
+ - [Similar tools](#similar-tools)
80
+ - [Troubleshooting](#troubleshooting)
81
+ - [Contributing](#contributing)
82
+ - [Citation](#citation)
83
+ - [License](#license)
84
+
50
85
  ## Installation and Setup
51
86
 
52
87
  You can install the LeanInteract package using the following command:
@@ -59,24 +94,21 @@ Requirements:
59
94
 
60
95
  - Python >= 3.10
61
96
  - git
62
- - [Lean 4](https://leanprover-community.github.io/get_started.html) (or use the `install-lean` command from LeanInteract)
63
-
64
- > [!NOTE]
65
- > This tool is still experimental and has been primarily tested on Linux. Compatibility with macOS is not guaranteed. For Windows, use WSL.
66
- > Please report any issues you encounter.
97
+ - [Lean 4](https://leanprover-community.github.io/get_started.html) (Tip: use the `install-lean` command from LeanInteract)
67
98
 
68
99
  ## Script examples
69
100
 
70
- In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
101
+ In the [`examples`](examples) directory, you will find a few scripts demonstrating how to use LeanInteract.
71
102
 
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.
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.
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).
103
+ - [`proof_generation_and_autoformalization.py`](examples/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.
104
+ - [`beq_plus.py`](examples/beq_plus.py): run the autoformalization [BEq+](https://arxiv.org/abs/2406.07222) metric on the [ProofNetVerif](https://huggingface.co/datasets/PAug/ProofNetVerif) benchmark.
105
+ - [`type_check.py`](examples/type_check.py): optimize type checking using environment states.
76
106
 
77
107
  ## Usage
78
108
 
79
- ### Default Lean version (latest available)
109
+ ### Basic example
110
+
111
+ The following code will use the default Lean version (latest available):
80
112
 
81
113
  ```python
82
114
  from lean_interact import LeanREPLConfig, LeanServer, Command
@@ -90,7 +122,15 @@ server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
90
122
  <summary>Output</summary>
91
123
 
92
124
  ```python
93
- CommandResponse(env=0)
125
+ CommandResponse(
126
+ env=0,
127
+ messages=[
128
+ Message(start_pos=Pos(line=1, column=0),
129
+ end_pos=Pos(line=1, column=42),
130
+ data='Goals accomplished!',
131
+ severity='info')
132
+ ]
133
+ )
94
134
  ```
95
135
 
96
136
  </details>
@@ -105,21 +145,116 @@ server.run(Command(cmd="example (x : Nat) : x = 5 → x = 5 := by exact ex x", e
105
145
  <summary>Output</summary>
106
146
 
107
147
  ```python
108
- CommandResponse(env=1)
148
+ CommandResponse(
149
+ env=1,
150
+ messages=[
151
+ Message(start_pos=Pos(line=1, column=0),
152
+ end_pos=Pos(line=1, column=50),
153
+ data='Goals accomplished!',
154
+ severity='info')
155
+ ]
156
+ )
109
157
  ```
110
158
 
111
159
  </details>
112
160
 
161
+ See [Available Queries](#available-queries) for all available commands.
162
+
113
163
  > [!NOTE]
114
164
  > 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.
115
165
 
116
- ### Specific Lean version
166
+ ### Tactic mode
167
+
168
+ > [!WARNING]
169
+ > This feature is experimental in Lean REPL and may not work as expected: some valid proofs might be incorrectly rejected. Please report any issues you encounter [here](https://github.com/leanprover-community/repl/issues).
170
+
171
+ First, let's run a command to create a theorem with a `sorry` proof:
172
+
173
+ ```python
174
+ server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := sorry"))
175
+ ```
176
+
177
+ <details>
178
+ <summary>Output</summary>
179
+
180
+ ```python
181
+ CommandResponse(
182
+ sorries=[
183
+ Sorry(start_pos=Pos(line=1, column=40),
184
+ end_pos=Pos(line=1, column=45),
185
+ goal='n : Nat\n⊢ n = 5 → n = 5',
186
+ proof_state=0)
187
+ ],
188
+ env=0,
189
+ messages=[
190
+ Message(start_pos=Pos(line=1, column=8),
191
+ end_pos=Pos(line=1, column=10),
192
+ data="declaration uses 'sorry'",
193
+ severity='warning')
194
+ ]
195
+ )
196
+ ```
197
+
198
+ </details>
199
+
200
+ You can then iterate on the proof state by executing tactics:
201
+
202
+ ```python
203
+ from lean_interact import ProofStep
204
+
205
+ server.run(ProofStep(tactic="intro h", proof_state=0))
206
+ ```
207
+
208
+ <details>
209
+ <summary>Output</summary>
210
+
211
+ ```python
212
+ ProofStepResponse(
213
+ proof_state=1,
214
+ goals=['n : Nat\nh : n = 5\n⊢ n = 5'],
215
+ proof_status='Incomplete: open goals remain'
216
+ )
217
+ ```
218
+
219
+ </details>
220
+
221
+ ```python
222
+ server.run(ProofStep(tactic="exact h", proof_state=1))
223
+ ```
224
+
225
+ <details>
226
+ <summary>Output</summary>
227
+
228
+ ```python
229
+ ProofStepResponse(proof_state=2, goals=[], proof_status='Completed')
230
+ ```
231
+
232
+ </details>
233
+
234
+ or by directly running the entire proof:
235
+
236
+ ```python
237
+ server.run(ProofStep(tactic="(\nintro h\nexact h)", proof_state=0))
238
+ ```
239
+
240
+ <details>
241
+ <summary>Output</summary>
242
+
243
+ ```python
244
+ ProofStepResponse(proof_state=3, goals=[], proof_status='Completed')
245
+ ```
246
+
247
+ </details>
248
+
249
+ ### Custom Lean configuration
250
+
251
+ #### Specific Lean version
117
252
 
118
253
  ```python
119
254
  config = LeanREPLConfig(lean_version="v4.7.0")
120
255
  ```
121
256
 
122
- ### Existing Lean projects
257
+ #### Existing Lean projects
123
258
 
124
259
  ```python
125
260
  config = LeanREPLConfig(project=LocalProject("path/to/your/project"))
@@ -143,7 +278,7 @@ server.run(FileCommand(path="file.lean"))
143
278
  > [!IMPORTANT]
144
279
  > Ensure the project can be *successfully* built with `lake build` before using LeanInteract.
145
280
 
146
- ### Temporary project with dependencies
281
+ #### Temporary project with dependencies
147
282
 
148
283
  ```python
149
284
  from lean_interact import TempRequireProject
@@ -175,21 +310,19 @@ theorem ex_mathlib (x : ℝ) (y : ℚ) :
175
310
 
176
311
  ```python
177
312
  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
- )],
313
+ env=0,
185
314
  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
315
+ Sorry(end_pos=Pos(line=3, column=51),
316
+ goal='x : ℝ\ny : ℚ\n⊢ Irrational x → Irrational (x + ↑y)',
317
+ start_pos=Pos(line=3, column=46),
318
+ proof_state=0)
319
+ ],
320
+ messages=[
321
+ Message(end_pos=Pos(line=2, column=18),
322
+ data="declaration uses 'sorry'",
323
+ start_pos=Pos(line=2, column=8),
324
+ severity='warning')
325
+ ]
193
326
  )
194
327
  ```
195
328
 
@@ -200,9 +333,9 @@ CommandResponse(
200
333
  > - Mathlib is a large library and may take some time to download and build.
201
334
  > - A separate cache is used for each unique set of dependencies.
202
335
 
203
- ### Fine-grained temporary project
336
+ #### Fine-grained temporary project
204
337
 
205
- For more control over the temporary project, you can use `TemporaryProject` to specify the content of the lakefile.
338
+ For more control over the temporary project, you can use `TemporaryProject` to specify the content of the lakefile (`.lean` format).
206
339
 
207
340
  ```python
208
341
  from lean_interact import TemporaryProject
@@ -223,79 +356,82 @@ require mathlib from git
223
356
  """))
224
357
  ```
225
358
 
226
- ### Tactic mode (experimental)
359
+ ## Available Queries
227
360
 
228
- ```python
229
- server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := sorry"))
230
- ```
361
+ LeanInteract supports various types of queries to interact with the Lean REPL. We briefly describe them in this section. You can check the file [interface.py](src/lean_interact/interface.py) for more details.
231
362
 
232
- <details>
233
- <summary>Output</summary>
363
+ ### Command
364
+
365
+ Execute Lean code directly:
234
366
 
235
367
  ```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
- )
253
- ```
368
+ from lean_interact import Command
254
369
 
255
- </details>
370
+ # Execute a simple theorem
371
+ response = server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
256
372
 
257
- You can then iterate on the proof state by executing tactics:
373
+ # Execute with options to get tactics information
374
+ response = server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := by simp", all_tactics=True))
258
375
 
259
- ```python
260
- server.run(ProofStep(tactic="intro h", proof_state=0))
376
+ # Continue in the same environment
377
+ response = server.run(Command(cmd="#check ex", env=response.env))
261
378
  ```
262
379
 
263
- <details>
264
- <summary>Output</summary>
380
+ ### FileCommand
381
+
382
+ Process Lean files:
265
383
 
266
384
  ```python
267
- ProofStepResponse(goals=['n : Nat\nh : n = 5\n⊢ n = 5'], proof_state=1)
268
- ```
385
+ from lean_interact import FileCommand
269
386
 
270
- </details>
387
+ # Execute a Lean file
388
+ response = server.run(FileCommand(path="myfile.lean"))
271
389
 
272
- ```python
273
- server.run(ProofStep(tactic="exact h", proof_state=1))
390
+ # With options for more information
391
+ response = server.run(FileCommand(path="myfile.lean", root_goals=True))
274
392
  ```
275
393
 
276
- <details>
277
- <summary>Output</summary>
394
+ ### ProofStep
395
+
396
+ Work with proofs step by step using tactics:
278
397
 
279
398
  ```python
280
- ProofStepResponse(goals=[], proof_state=2)
399
+ from lean_interact import ProofStep
400
+
401
+ # Apply a tactic to a proof state
402
+ response = server.run(ProofStep(proof_state=0, tactic="intro h"))
403
+
404
+ # Apply multiple tactics at once
405
+ response = server.run(ProofStep(proof_state=0, tactic="(\nintro h\nexact h)"))
281
406
  ```
282
407
 
283
- </details>
408
+ ### Environment Pickling
284
409
 
285
- or by directly running the entire proof:
410
+ Save and restore environment states:
286
411
 
287
412
  ```python
288
- server.run(ProofStep(tactic="(\nintro h\nexact h)", proof_state=0))
413
+ from lean_interact import PickleEnvironment, UnpickleEnvironment
414
+
415
+ # Save an environment
416
+ server.run(PickleEnvironment(env=1, pickle_to="env_state.olean"))
417
+
418
+ # Restore an environment
419
+ server.run(UnpickleEnvironment(unpickle_env_from="env_state.olean"))
289
420
  ```
290
421
 
291
- <details>
292
- <summary>Output</summary>
422
+ ### ProofState Pickling
423
+
424
+ Save and restore proof states:
293
425
 
294
426
  ```python
295
- ProofStepResponse(goals=[], proof_state=3)
296
- ```
427
+ from lean_interact import PickleProofState, UnpickleProofState
297
428
 
298
- </details>
429
+ # Save a proof state
430
+ server.run(PickleProofState(proof_state=2, pickle_to="proof_state.olean"))
431
+
432
+ # Restore a proof state
433
+ server.run(UnpickleProofState(unpickle_proof_state_from="proof_state.olean", env=1))
434
+ ```
299
435
 
300
436
  ## Helper Commands
301
437
 
@@ -333,3 +469,40 @@ We recommend checking out these tools:
333
469
  - **[leanclient](https://github.com/oOo0oOo/leanclient)**: Interact with the Lean LSP server.
334
470
 
335
471
  LeanInteract is inspired by **[pylean](https://github.com/zhangir-azerbayev/repl)** and **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**.
472
+
473
+ ## Troubleshooting
474
+
475
+ Common issues and their solutions:
476
+
477
+ 1. **Out of memory errors**: Reduce parallel processing or increase system memory. Alternatively, use `AutoLeanServer` with conservative memory settings
478
+
479
+ 2. **Timeout errors**: Currently, `LeanServer` simply stops the Lean REPL if a command times out. Use `AutoLeanServer` for automatic recovery.
480
+
481
+ 3. **Long waiting times during first run**: This is expected as Lean REPL is being downloaded and built. Additionally, if you are importing Mathlib it will take even more time. Subsequent runs will be much faster.
482
+
483
+ ## Contributing
484
+
485
+ Contributions are welcome! Here's how you can help:
486
+
487
+ 1. Fork the repository
488
+ 2. Create a feature branch: `git checkout -b feature-name`
489
+ 3. Commit your changes: `git commit -am 'Add new feature'`
490
+ 4. Push to the branch: `git push origin feature-name`
491
+ 5. Submit a pull request
492
+
493
+ ## Citation
494
+
495
+ If you use LeanInteract in your research, please cite it as follows:
496
+
497
+ ```bibtex
498
+ @software{leaninteract,
499
+ author = {Poiroux, Auguste and Kuncak, Viktor and Bosselut, Antoine},
500
+ title = {LeanInteract: A Python Interface for Lean 4},
501
+ url = {https://github.com/augustepoiroux/lean-interact},
502
+ year = {2025}
503
+ }
504
+ ```
505
+
506
+ ## License
507
+
508
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.