lean-interact 0.1.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.
@@ -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
@@ -0,0 +1,335 @@
1
+ Metadata-Version: 2.4
2
+ Name: lean-interact
3
+ Version: 0.3.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) 2025
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: gitpython>=3.1.44
31
+ Requires-Dist: pexpect>=4.9.0
32
+ Requires-Dist: psutil>=6.1.0
33
+ Requires-Dist: pydantic>=2.11.1
34
+ Requires-Dist: requests>=2.32.3
35
+ Requires-Dist: rich>=13.9.4
36
+ Description-Content-Type: text/markdown
37
+
38
+ # LeanInteract
39
+
40
+ **LeanInteract** is a Python package designed to seamlessly interact with Lean 4 through the [Lean REPL](https://github.com/leanprover-community/repl).
41
+
42
+ ## Key Features
43
+
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).
49
+
50
+ ## Installation and Setup
51
+
52
+ You can install the LeanInteract package using the following command:
53
+
54
+ ```bash
55
+ pip install lean-interact
56
+ ```
57
+
58
+ Requirements:
59
+
60
+ - Python >= 3.10
61
+ - 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.
67
+
68
+ ## Script examples
69
+
70
+ In the `examples` directory, you will find a few scripts demonstrating how to use LeanInteract.
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.
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).
76
+
77
+ ## Usage
78
+
79
+ ### Default Lean version (latest available)
80
+
81
+ ```python
82
+ from lean_interact import LeanREPLConfig, LeanServer, Command
83
+
84
+ config = LeanREPLConfig(verbose=True) # download and build Lean REPL
85
+ server = LeanServer(config) # start Lean REPL
86
+ server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
87
+ ```
88
+
89
+ <details>
90
+ <summary>Output</summary>
91
+
92
+ ```python
93
+ CommandResponse(env=0)
94
+ ```
95
+
96
+ </details>
97
+
98
+ Iterate on the environment state:
99
+
100
+ ```python
101
+ server.run(Command(cmd="example (x : Nat) : x = 5 → x = 5 := by exact ex x", env=0))
102
+ ```
103
+
104
+ <details>
105
+ <summary>Output</summary>
106
+
107
+ ```python
108
+ CommandResponse(env=1)
109
+ ```
110
+
111
+ </details>
112
+
113
+ > [!NOTE]
114
+ > 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
+
116
+ ### Specific Lean version
117
+
118
+ ```python
119
+ config = LeanREPLConfig(lean_version="v4.7.0")
120
+ ```
121
+
122
+ ### Existing Lean projects
123
+
124
+ ```python
125
+ config = LeanREPLConfig(project=LocalProject("path/to/your/project"))
126
+ ```
127
+
128
+ or
129
+
130
+ ```python
131
+ config = LeanREPLConfig(project=GitProject("https://github.com/yangky11/lean4-example"))
132
+ ```
133
+
134
+ You can then use `run` as usual:
135
+
136
+ ```python
137
+ from lean_interact import FileCommand
138
+
139
+ server = LeanServer(config)
140
+ server.run(FileCommand(path="file.lean"))
141
+ ```
142
+
143
+ > [!IMPORTANT]
144
+ > Ensure the project can be *successfully* built with `lake build` before using LeanInteract.
145
+
146
+ ### Temporary project with dependencies
147
+
148
+ ```python
149
+ from lean_interact import TempRequireProject
150
+
151
+ config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject([LeanRequire(
152
+ name="mathlib",
153
+ git="https://github.com/leanprover-community/mathlib4.git",
154
+ rev="v4.7.0"
155
+ )]))
156
+ ```
157
+
158
+ Mathlib being a frequent requirement, a shortcut is available:
159
+
160
+ ```python
161
+ config = LeanREPLConfig(lean_version="v4.7.0", project=TempRequireProject("mathlib"))
162
+ ```
163
+
164
+ You can then use Mathlib as follows:
165
+
166
+ ```python
167
+ server = LeanServer(config)
168
+ server.run(Command(cmd="""import Mathlib
169
+ theorem ex_mathlib (x : ℝ) (y : ℚ) :
170
+ ( Irrational x ) -> Irrational ( x + y ) := sorry"""))
171
+ ```
172
+
173
+ <details>
174
+ <summary>Output</summary>
175
+
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
+ ```
195
+
196
+ </details>
197
+
198
+ > [!NOTE]
199
+ >
200
+ > - Mathlib is a large library and may take some time to download and build.
201
+ > - A separate cache is used for each unique set of dependencies.
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
+ from lean_interact import TemporaryProject
209
+
210
+ config = LeanREPLConfig(lean_version="v4.18.0", project=TemporaryProject("""
211
+ import Lake
212
+ open Lake DSL
213
+
214
+ package "dummy" where
215
+ version := v!"0.1.0"
216
+
217
+ @[default_target]
218
+ lean_exe "dummy" where
219
+ root := `Main
220
+
221
+ require mathlib from git
222
+ "https://github.com/leanprover-community/mathlib4.git" @ "v4.18.0"
223
+ """))
224
+ ```
225
+
226
+ ### Tactic mode (experimental)
227
+
228
+ ```python
229
+ server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := sorry"))
230
+ ```
231
+
232
+ <details>
233
+ <summary>Output</summary>
234
+
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
+ )
253
+ ```
254
+
255
+ </details>
256
+
257
+ You can then iterate on the proof state by executing tactics:
258
+
259
+ ```python
260
+ server.run(ProofStep(tactic="intro h", proof_state=0))
261
+ ```
262
+
263
+ <details>
264
+ <summary>Output</summary>
265
+
266
+ ```python
267
+ ProofStepResponse(goals=['n : Nat\nh : n = 5\n⊢ n = 5'], proof_state=1)
268
+ ```
269
+
270
+ </details>
271
+
272
+ ```python
273
+ server.run(ProofStep(tactic="exact h", proof_state=1))
274
+ ```
275
+
276
+ <details>
277
+ <summary>Output</summary>
278
+
279
+ ```python
280
+ ProofStepResponse(goals=[], proof_state=2)
281
+ ```
282
+
283
+ </details>
284
+
285
+ or by directly running the entire proof:
286
+
287
+ ```python
288
+ server.run(ProofStep(tactic="(\nintro h\nexact h)", proof_state=0))
289
+ ```
290
+
291
+ <details>
292
+ <summary>Output</summary>
293
+
294
+ ```python
295
+ ProofStepResponse(goals=[], proof_state=3)
296
+ ```
297
+
298
+ </details>
299
+
300
+ ## Helper Commands
301
+
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.
306
+
307
+ ## Advanced options
308
+
309
+ ### LeanServer
310
+
311
+ Two versions of Lean servers are available:
312
+
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.
315
+
316
+ > [!TIP]
317
+ >
318
+ > - To run multiple requests in parallel, we recommend using multiprocessing with one global `LeanREPLConfig` instance, and one `AutoLeanServer` instance per process.
319
+ > - Make sure to instantiate `LeanREPLConfig` before starting the processes to avoid conflicts during Lean REPL's download and build.
320
+ > - 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.
321
+
322
+ ### Custom Lean REPL
323
+
324
+ 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).
325
+
326
+ ## Similar tools
327
+
328
+ We recommend checking out these tools:
329
+
330
+ - **[PyPantograph](https://github.com/lenianiva/PyPantograph)**: Based on Pantograph, offering more options for proof interactions than Lean REPL.
331
+ - **[LeanDojo](https://github.com/lean-dojo/LeanDojo)**: Parses Lean projects to create datasets and interact with proof states.
332
+ - **[itp-interface](https://github.com/trishullab/itp-interface)**: A Python interface for interacting and extracting data from Lean 4 and Coq.
333
+ - **[leanclient](https://github.com/oOo0oOo/leanclient)**: Interact with the Lean LSP server.
334
+
335
+ LeanInteract is inspired by **[pylean](https://github.com/zhangir-azerbayev/repl)** and **[lean4_jupyter](https://github.com/utensil/lean4_jupyter)**.