lean-python 0.1.0__py3-none-any.whl
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_python/__init__.py +6 -0
- lean_python/cli.py +50 -0
- lean_python/embed.py +131 -0
- lean_python/io.py +15 -0
- lean_python/io_externs.py +1192 -0
- lean_python/marshal.py +727 -0
- lean_python/models.py +297 -0
- lean_python/runtime.py +1400 -0
- lean_python/sync.py +265 -0
- lean_python/types.py +33 -0
- lean_python-0.1.0.dist-info/METADATA +231 -0
- lean_python-0.1.0.dist-info/RECORD +15 -0
- lean_python-0.1.0.dist-info/WHEEL +4 -0
- lean_python-0.1.0.dist-info/entry_points.txt +2 -0
- lean_python-0.1.0.dist-info/licenses/LICENSE +202 -0
lean_python/sync.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""``lean-py sync``: check the Lean blocks of a package and generate their Python modules."""
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import pathlib
|
|
8
|
+
import re
|
|
9
|
+
import shutil
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
from lean_python import embed, models
|
|
14
|
+
|
|
15
|
+
LAKEFILE = """name = "lean-python-workspace"
|
|
16
|
+
defaultTargets = ["LeanPythonEmbed"]
|
|
17
|
+
|
|
18
|
+
[[lean_lib]]
|
|
19
|
+
name = "LeanPythonEmbed"
|
|
20
|
+
|
|
21
|
+
[[lean_lib]]
|
|
22
|
+
name = "LeanPythonModels"
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
_LAKE_ERROR = re.compile(r"^(error|warning): (?:\./)*(LeanPython(?:Embed|Models)/[^:]+\.lean):(\d+):(\d+): (.*)$")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SyncError(Exception):
|
|
29
|
+
def __init__(self, msg, code=254):
|
|
30
|
+
super().__init__(msg)
|
|
31
|
+
self.code = code
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def project_root(pkg_dir):
|
|
35
|
+
p = pkg_dir.resolve()
|
|
36
|
+
for d in [p, *p.parents]:
|
|
37
|
+
if (d / "pyproject.toml").exists():
|
|
38
|
+
return d
|
|
39
|
+
return p.parent
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def record_blocks(pkg_dir):
|
|
43
|
+
"""Imports every module of the package and returns the blocks their `lean(...)` calls define."""
|
|
44
|
+
pkg_dir = pkg_dir.resolve()
|
|
45
|
+
pkg = pkg_dir.name
|
|
46
|
+
sys.path.insert(0, str(pkg_dir.parent))
|
|
47
|
+
embed._recording = []
|
|
48
|
+
errors = []
|
|
49
|
+
try:
|
|
50
|
+
for path in sorted(pkg_dir.rglob("*.py")):
|
|
51
|
+
rel = path.relative_to(pkg_dir)
|
|
52
|
+
if rel.parts[0] == "_lean" or "__pycache__" in rel.parts or rel.name == "__main__.py":
|
|
53
|
+
continue
|
|
54
|
+
parts = [pkg, *rel.with_suffix("").parts]
|
|
55
|
+
if parts[-1] == "__init__":
|
|
56
|
+
parts = parts[:-1]
|
|
57
|
+
name = ".".join(parts)
|
|
58
|
+
try:
|
|
59
|
+
importlib.import_module(name)
|
|
60
|
+
except Exception as e: # noqa: BLE001
|
|
61
|
+
errors.append(f"{name}: {type(e).__name__}: {e}")
|
|
62
|
+
return list(embed._recording), errors
|
|
63
|
+
finally:
|
|
64
|
+
embed._recording = None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def resolve_use(block, use, by_name):
|
|
68
|
+
q = use if ":" in use else f"{block.module}:{use}"
|
|
69
|
+
b = by_name.get(q)
|
|
70
|
+
if b is None:
|
|
71
|
+
raise SyncError(f"{block.file}:{block.line}: lean block {block.name!r} uses unknown block {use!r}", 252)
|
|
72
|
+
return b
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def block_file(block, by_name):
|
|
76
|
+
imports = list(block.imports)
|
|
77
|
+
opens = []
|
|
78
|
+
if block.models:
|
|
79
|
+
for module in models.modules_source(list(block.models)):
|
|
80
|
+
ns = models.dotted(models.models_namespace(module))
|
|
81
|
+
imports.append(ns)
|
|
82
|
+
opens.append(ns)
|
|
83
|
+
for use in block.uses:
|
|
84
|
+
other = resolve_use(block, use, by_name)
|
|
85
|
+
ns = models.dotted(other.lean_module)
|
|
86
|
+
imports.append(ns)
|
|
87
|
+
opens.append(ns)
|
|
88
|
+
ns = models.dotted(block.lean_module)
|
|
89
|
+
head = "".join(f"import {i}\n" for i in dict.fromkeys(imports))
|
|
90
|
+
head += f"\nnamespace {ns}\n"
|
|
91
|
+
head += "".join(f"open {o}\n" for o in opens)
|
|
92
|
+
head += "\n"
|
|
93
|
+
return head, head + block.source + f"\n\nend {ns}\n"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def source_line(block):
|
|
97
|
+
"""The line of the Python file where the block's source string starts."""
|
|
98
|
+
try:
|
|
99
|
+
tree = ast.parse(pathlib.Path(block.file).read_text())
|
|
100
|
+
except (OSError, SyntaxError):
|
|
101
|
+
return block.line
|
|
102
|
+
for node in ast.walk(tree):
|
|
103
|
+
if isinstance(node, ast.Call) and node.lineno <= block.line <= (node.end_lineno or node.lineno):
|
|
104
|
+
args = [*node.args, *(k.value for k in node.keywords if k.arg == "source")]
|
|
105
|
+
for a in args:
|
|
106
|
+
if isinstance(a, ast.Constant) and a.value == block.source:
|
|
107
|
+
return a.lineno
|
|
108
|
+
return block.line
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def map_error(line, offsets):
|
|
112
|
+
m = _LAKE_ERROR.match(line.strip())
|
|
113
|
+
if not m:
|
|
114
|
+
return None
|
|
115
|
+
sev, path, ln, col, msg = m.groups()
|
|
116
|
+
info = offsets.get(path)
|
|
117
|
+
if info is None:
|
|
118
|
+
return f"{path}:{ln}:{col}: {sev}: {msg}"
|
|
119
|
+
block, head_lines, start = info
|
|
120
|
+
k = int(ln) - head_lines
|
|
121
|
+
if k < 1:
|
|
122
|
+
return f"{block.file}:{block.line}: {sev}: (lean block {block.name}, Lean {ln}:{col}) {msg}"
|
|
123
|
+
return f"{block.file}:{start + k - 1}: {sev}: (lean block {block.name}, Lean {k}:{col}) {msg}"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def lake_messages(output, offsets):
|
|
127
|
+
"""The messages of a Lake build mapped to the Python files, with their continuation lines."""
|
|
128
|
+
out = []
|
|
129
|
+
lines = output.splitlines()
|
|
130
|
+
i = 0
|
|
131
|
+
while i < len(lines):
|
|
132
|
+
mapped = map_error(lines[i], offsets)
|
|
133
|
+
i += 1
|
|
134
|
+
if mapped is None:
|
|
135
|
+
continue
|
|
136
|
+
body = [mapped]
|
|
137
|
+
while (
|
|
138
|
+
i < len(lines)
|
|
139
|
+
and lines[i].strip()
|
|
140
|
+
and map_error(lines[i], offsets) is None
|
|
141
|
+
and (
|
|
142
|
+
lines[i].startswith(" ")
|
|
143
|
+
or not re.match(r"^(✖|✔|⣿|trace:|info:|error:|warning:|Some required|- )", lines[i])
|
|
144
|
+
)
|
|
145
|
+
):
|
|
146
|
+
body.append(lines[i])
|
|
147
|
+
i += 1
|
|
148
|
+
out.append("\n".join(body))
|
|
149
|
+
return out
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def sync_package(pkg_dir, cli=None, out=sys.stderr):
|
|
153
|
+
pkg_dir = pathlib.Path(pkg_dir).resolve()
|
|
154
|
+
cli = cli or os.environ.get("LEAN_PYTHON_CLI") or shutil.which("lean-python")
|
|
155
|
+
if cli is None:
|
|
156
|
+
raise SyncError("the Lean CLI `lean-python` is not on PATH (set LEAN_PYTHON_CLI)", 255)
|
|
157
|
+
gv = subprocess.run([cli, "--generator-version"], check=True, capture_output=True, text=True).stdout.strip()
|
|
158
|
+
if gv != embed.GENERATOR_VERSION:
|
|
159
|
+
raise SyncError(f"{cli} generates version {gv}, this package expects {embed.GENERATOR_VERSION}", 255)
|
|
160
|
+
|
|
161
|
+
blocks, import_errors = record_blocks(pkg_dir)
|
|
162
|
+
for e in import_errors:
|
|
163
|
+
print(f"lean-py: warning: importing {e}", file=out)
|
|
164
|
+
by_name = {}
|
|
165
|
+
for b in blocks:
|
|
166
|
+
if b.qualname in by_name:
|
|
167
|
+
raise SyncError(f"{b.file}:{b.line}: lean block {b.name!r} is defined twice in {b.module}", 252)
|
|
168
|
+
by_name[b.qualname] = b
|
|
169
|
+
|
|
170
|
+
root = project_root(pkg_dir)
|
|
171
|
+
ws = root / ".lean-python" / "workspace"
|
|
172
|
+
for d in ("LeanPythonEmbed", "LeanPythonModels"):
|
|
173
|
+
shutil.rmtree(ws / d, ignore_errors=True)
|
|
174
|
+
ws.mkdir(parents=True, exist_ok=True)
|
|
175
|
+
(ws / "lakefile.toml").write_text(LAKEFILE)
|
|
176
|
+
|
|
177
|
+
model_modules = {}
|
|
178
|
+
for b in blocks:
|
|
179
|
+
if b.models:
|
|
180
|
+
model_modules.update(models.modules_source(list(b.models)))
|
|
181
|
+
offsets = {}
|
|
182
|
+
for module, src in model_modules.items():
|
|
183
|
+
p = ws / pathlib.Path(*models.models_namespace(module)).with_suffix(".lean")
|
|
184
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
185
|
+
p.write_text(src)
|
|
186
|
+
for b in blocks:
|
|
187
|
+
head, text = block_file(b, by_name)
|
|
188
|
+
rel = pathlib.Path(*b.lean_module).with_suffix(".lean")
|
|
189
|
+
p = ws / rel
|
|
190
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
191
|
+
p.write_text(text)
|
|
192
|
+
offsets[str(rel)] = (b, head.count("\n"), source_line(b))
|
|
193
|
+
(ws / "LeanPythonEmbed.lean").write_text(
|
|
194
|
+
"".join(f"import {models.dotted(b.lean_module)}\n" for b in blocks) or "\n"
|
|
195
|
+
)
|
|
196
|
+
(ws / "LeanPythonModels.lean").write_text(
|
|
197
|
+
"".join(f"import {models.dotted(models.models_namespace(m))}\n" for m in model_modules) or "\n"
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
r = subprocess.run(
|
|
201
|
+
["lake", "build", "LeanPythonEmbed", "LeanPythonModels"], cwd=ws, capture_output=True, text=True, check=False
|
|
202
|
+
)
|
|
203
|
+
failed = r.returncode != 0
|
|
204
|
+
messages = lake_messages(r.stdout + r.stderr, offsets)
|
|
205
|
+
for m in messages:
|
|
206
|
+
if failed or ": warning:" in m.split("\n")[0]:
|
|
207
|
+
print(f"lean-py: {m}", file=out)
|
|
208
|
+
if failed:
|
|
209
|
+
if not messages:
|
|
210
|
+
print(r.stdout + r.stderr, file=out)
|
|
211
|
+
raise SyncError("lake build failed", 254)
|
|
212
|
+
|
|
213
|
+
out_dir = pkg_dir / "_lean"
|
|
214
|
+
shutil.rmtree(out_dir, ignore_errors=True)
|
|
215
|
+
out_dir.mkdir()
|
|
216
|
+
jobs = []
|
|
217
|
+
for b in blocks:
|
|
218
|
+
type_modules = [
|
|
219
|
+
{"namespace": ".".join(models.models_namespace(m)), "module": m, "adopt": True}
|
|
220
|
+
for m in (models.modules_source(list(b.models)) if b.models else {})
|
|
221
|
+
]
|
|
222
|
+
for use in b.uses:
|
|
223
|
+
other = resolve_use(b, use, by_name)
|
|
224
|
+
type_modules.append(
|
|
225
|
+
{"namespace": ".".join(other.lean_module), "module": f"{other.top_package}._lean.{other.stem}"}
|
|
226
|
+
)
|
|
227
|
+
jobs.append(
|
|
228
|
+
{
|
|
229
|
+
"module": ".".join(b.lean_module),
|
|
230
|
+
"namespace": ".".join(b.lean_module),
|
|
231
|
+
"output_file": str(out_dir / f"{b.stem}.py"),
|
|
232
|
+
"type_modules": type_modules,
|
|
233
|
+
}
|
|
234
|
+
)
|
|
235
|
+
batch = ws / "batch.json"
|
|
236
|
+
batch.write_text(json.dumps(jobs))
|
|
237
|
+
r = subprocess.run(
|
|
238
|
+
[
|
|
239
|
+
cli,
|
|
240
|
+
"module",
|
|
241
|
+
"generate",
|
|
242
|
+
"--batch",
|
|
243
|
+
str(batch),
|
|
244
|
+
"--search-path",
|
|
245
|
+
str(ws / ".lake/build/lib/lean"),
|
|
246
|
+
"--output",
|
|
247
|
+
"json",
|
|
248
|
+
],
|
|
249
|
+
capture_output=True,
|
|
250
|
+
text=True,
|
|
251
|
+
check=False,
|
|
252
|
+
)
|
|
253
|
+
if r.returncode != 0:
|
|
254
|
+
print(r.stderr + r.stdout, file=out)
|
|
255
|
+
raise SyncError("lean-python module generate failed", r.returncode)
|
|
256
|
+
summaries = json.loads(r.stdout)
|
|
257
|
+
for s in summaries:
|
|
258
|
+
for sk in s.get("skipped", []):
|
|
259
|
+
print(f"lean-py: skipped {sk['name']}: {sk['reason']}", file=out)
|
|
260
|
+
(out_dir / "__init__.py").write_text('"""Python modules generated by `lean-py sync`. Do not edit."""\n')
|
|
261
|
+
registry = {}
|
|
262
|
+
for b in blocks:
|
|
263
|
+
registry[b.qualname] = embed.block_key(b)
|
|
264
|
+
(out_dir / "blocks.json").write_text(json.dumps(registry, indent=2, sort_keys=True) + "\n")
|
|
265
|
+
print(f"lean-py: {len(blocks)} blocks of {pkg_dir.name} checked and generated", file=out)
|
lean_python/types.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Python types of Lean's scalar types, as ``Annotated`` aliases with annotated-types constraints."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
from annotated_types import Ge, Le, MaxLen, MinLen
|
|
6
|
+
|
|
7
|
+
Nat = Annotated[int, Ge(0)]
|
|
8
|
+
UInt8 = Annotated[int, Ge(0), Le(2**8 - 1)]
|
|
9
|
+
UInt16 = Annotated[int, Ge(0), Le(2**16 - 1)]
|
|
10
|
+
UInt32 = Annotated[int, Ge(0), Le(2**32 - 1)]
|
|
11
|
+
UInt64 = Annotated[int, Ge(0), Le(2**64 - 1)]
|
|
12
|
+
USize = UInt64
|
|
13
|
+
Int8 = Annotated[int, Ge(-(2**7)), Le(2**7 - 1)]
|
|
14
|
+
Int16 = Annotated[int, Ge(-(2**15)), Le(2**15 - 1)]
|
|
15
|
+
Int32 = Annotated[int, Ge(-(2**31)), Le(2**31 - 1)]
|
|
16
|
+
Int64 = Annotated[int, Ge(-(2**63)), Le(2**63 - 1)]
|
|
17
|
+
ISize = Int64
|
|
18
|
+
Char = Annotated[str, MinLen(1), MaxLen(1)]
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"Char",
|
|
22
|
+
"ISize",
|
|
23
|
+
"Int8",
|
|
24
|
+
"Int16",
|
|
25
|
+
"Int32",
|
|
26
|
+
"Int64",
|
|
27
|
+
"Nat",
|
|
28
|
+
"UInt8",
|
|
29
|
+
"UInt16",
|
|
30
|
+
"UInt32",
|
|
31
|
+
"UInt64",
|
|
32
|
+
"USize",
|
|
33
|
+
]
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: lean-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Run Lean 4 code as Python: a runtime for code generated from Lean's IR, Lean blocks in Python files, and pydantic models for Lean types
|
|
5
|
+
Project-URL: Repository, https://github.com/conao3/lean-py
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Framework :: Pydantic :: 2
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Requires-Dist: pydantic>=2.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# lean-py
|
|
16
|
+
|
|
17
|
+
Write and prove functions in Lean 4, run them as Python, and pass pydantic models across.
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from pydantic import BaseModel, Field
|
|
21
|
+
from lean_python import lean
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class LineItem(BaseModel):
|
|
25
|
+
sku: str
|
|
26
|
+
qty: int = Field(ge=1)
|
|
27
|
+
unitPrice: int = Field(ge=0)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
pricing = lean("pricing", r"""
|
|
31
|
+
inductive Discount where
|
|
32
|
+
| none
|
|
33
|
+
| percent (p : Nat) (h : p ≤ 100)
|
|
34
|
+
| fixed (amount : Nat)
|
|
35
|
+
|
|
36
|
+
def subtotal (items : List LineItem) : Nat :=
|
|
37
|
+
items.foldl (fun acc i => acc + i.qty * i.unitPrice) 0
|
|
38
|
+
|
|
39
|
+
def total (items : List LineItem) (d : Discount) : Nat :=
|
|
40
|
+
match d with
|
|
41
|
+
| .none => subtotal items
|
|
42
|
+
| .percent p _ => subtotal items - subtotal items * p / 100
|
|
43
|
+
| .fixed a => subtotal items - a
|
|
44
|
+
|
|
45
|
+
theorem total_le (items : List LineItem) (d : Discount) : total items d ≤ subtotal items := by
|
|
46
|
+
unfold total; split <;> omega
|
|
47
|
+
""", models=[LineItem])
|
|
48
|
+
|
|
49
|
+
items = [LineItem(sku="a", qty=2, unitPrice=100)]
|
|
50
|
+
pricing.total(items, pricing.Discount.percent(p=10)) # 180
|
|
51
|
+
pricing.Discount.percent(p=150) # pydantic.ValidationError: p: Input should be less than or equal to 100
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- `lean-py sync` checks the proofs with Lake and compiles the Lean code from Lean's IR to plain
|
|
55
|
+
Python. The generated modules are written to `<package>/_lean/` and ship with your package; running
|
|
56
|
+
them needs only the `lean-python` wheel (and pydantic), not Lean.
|
|
57
|
+
- The generated Python computes what the C backend computes: a diff test runs the same Lean
|
|
58
|
+
programs natively and as Python and compares the output byte for byte. Reference counting is
|
|
59
|
+
kept, so `Array` updates happen in place as in C.
|
|
60
|
+
- Lean structures and inductive types become pydantic models. A proof field (`h : p ≤ 100`) becomes
|
|
61
|
+
a validator that runs a decision procedure compiled from the proposition, so every value that
|
|
62
|
+
reaches Lean satisfies the invariants the proofs rely on; a comparison with a constant also becomes
|
|
63
|
+
an annotated-types constraint (`Le(100)`), checked by pydantic-core and shown in the JSON Schema. The JSON of the models is the JSON of
|
|
64
|
+
Lean's `deriving ToJson, FromJson`.
|
|
65
|
+
- Your own pydantic models can be used from Lean (`models=[...]`). Their field constraints
|
|
66
|
+
(`ge`, `gt`, `le`, `lt`, `min_length`, `max_length`) become proof fields that Lean proofs may assume.
|
|
67
|
+
- The models work wherever pydantic does: `TypeAdapter`, `model_validate_json`, JSON Schema, FastAPI
|
|
68
|
+
request and response types.
|
|
69
|
+
|
|
70
|
+
## Install
|
|
71
|
+
|
|
72
|
+
The Python package and the Lean CLI:
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
uv add lean-python # the runtime, lean(), lean-py sync
|
|
76
|
+
nix profile install github:conao3/lean-py # the Lean CLI `lean-python`
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`lean-py sync` also needs `lake` and the Lean toolchain of `lean-toolchain` (v4.35.0) on `PATH`.
|
|
80
|
+
|
|
81
|
+
## Lean blocks
|
|
82
|
+
|
|
83
|
+
`lean(name, source, *, imports=(), uses=(), models=())` defines a Lean block of the calling module
|
|
84
|
+
(which must belong to a package). The block's definitions are in the namespace
|
|
85
|
+
`LeanPythonEmbed.<module>.<name>`; the returned module exposes them under their names relative to it
|
|
86
|
+
(`Discount.percent`, `total`).
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
lean-py sync --package src/myapp
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
imports every module of the package, writes each block to a Lake workspace in
|
|
93
|
+
`.lean-python/workspace/`, builds it, and generates `src/myapp/_lean/<module>__<name>.py` and
|
|
94
|
+
`src/myapp/_lean/blocks.json`. Lean errors point at the Python file:
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
lean-py: src/myapp/m.py:6: error: (lean block m, Lean 4:28) Tactic `rfl` failed: ...
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Add `.lean-python/` to `.gitignore` and commit `<package>/_lean/`.
|
|
101
|
+
|
|
102
|
+
At run time `lean(...)` compares a hash of the block (its source, imports, uses, the Lean structures
|
|
103
|
+
of its models and the generator version) with `blocks.json` and raises `LeanBlockStale` when the
|
|
104
|
+
block changed since the last sync. With `LEAN_PYTHON_AUTO_SYNC=1` it runs `lean-py sync` instead.
|
|
105
|
+
|
|
106
|
+
- `imports=["Std.Data.HashMap"]`: Lean modules to import.
|
|
107
|
+
- `uses=["geometry"]`: other blocks of the module (or `"pkg.mod:name"`), imported and opened; their
|
|
108
|
+
types are shared, not generated again.
|
|
109
|
+
- `models=[LineItem]`: pydantic models available as Lean structures (below).
|
|
110
|
+
|
|
111
|
+
## Values
|
|
112
|
+
|
|
113
|
+
| Lean | Python |
|
|
114
|
+
|---|---|
|
|
115
|
+
| `Nat` | `int` (`lean_python.types.Nat` = `Annotated[int, Ge(0)]`) |
|
|
116
|
+
| `Int` | `int` |
|
|
117
|
+
| `UInt8` … `UInt64`, `USize`, `Int8` … `Int64` | `int` with the range as `Ge`/`Le` |
|
|
118
|
+
| `Fin n` | `Annotated[int, Ge(0), Lt(n)]` |
|
|
119
|
+
| `Bool` | `bool` |
|
|
120
|
+
| `Char` | `str` of one character |
|
|
121
|
+
| `String` | `str` |
|
|
122
|
+
| `Float`, `Float32` | `float` |
|
|
123
|
+
| `Unit` | `None` |
|
|
124
|
+
| `Option α` | `α \| None` |
|
|
125
|
+
| `List α`, `Array α` | `list[α]` |
|
|
126
|
+
| `α × β` | `tuple[α, β]` |
|
|
127
|
+
| `{x : α // p x}` | `Annotated[α, AfterValidator(p)]` |
|
|
128
|
+
| structure | a frozen, strict pydantic model |
|
|
129
|
+
| inductive type, constructors without arguments | `enum.StrEnum` |
|
|
130
|
+
| other inductive type | a model per constructor, subclasses of the type's class |
|
|
131
|
+
| `IO α` | `α`; an `IO.Error` is raised as `LeanIOError` (an `OSError`) |
|
|
132
|
+
|
|
133
|
+
Arguments are checked while they are converted (`pub.double(-1)` raises `ValidationError` with the
|
|
134
|
+
location of the bad value). Constants are functions without arguments (`answer()`).
|
|
135
|
+
|
|
136
|
+
Constructors and structures take keyword arguments (`Shape.rect(width=1, height=2)`); unnamed
|
|
137
|
+
constructor arguments are `a0`, `a1`, …. Inductive values work with `match`:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
match shape:
|
|
141
|
+
case geometry.Shape.rect(width=w, height=h):
|
|
142
|
+
...
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### JSON
|
|
146
|
+
|
|
147
|
+
The JSON of a generated model is the JSON of Lean's derived `ToJson`: a structure is an object, a
|
|
148
|
+
constructor without arguments is its name (`"dot"`), a constructor with named arguments is
|
|
149
|
+
`{"rect": {"width": 1, "height": 2}}`, with unnamed arguments `{"num": 7}` or `{"pair": [1, "x"]}`.
|
|
150
|
+
`tests/test_json.py` compares the generated models with a native Lean program that prints `toJson`.
|
|
151
|
+
|
|
152
|
+
### Your pydantic models in Lean
|
|
153
|
+
|
|
154
|
+
`models=[LineItem]` writes a Lean structure for each model (and the models its fields use) to the
|
|
155
|
+
namespace `LeanPythonModels.<python module>`, opened in the block:
|
|
156
|
+
|
|
157
|
+
```lean
|
|
158
|
+
structure LineItem where
|
|
159
|
+
sku : String
|
|
160
|
+
qty : Nat
|
|
161
|
+
qty_ge : 1 ≤ qty
|
|
162
|
+
unitPrice : Nat
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`int` with a lower bound of at least 0 becomes `Nat`; the other bounds and `min_length`/`max_length`
|
|
166
|
+
become proof fields. Field types: `int`, `bool`, `str`, `float`, `list[T]`, `tuple[...]`, `T | None`,
|
|
167
|
+
other models, enums (an inductive type `LeanPythonModels.<module>.<Enum>` with a constructor per
|
|
168
|
+
member), `Literal["a", "b"]` (`LeanPythonLiterals.<module>.<Model>_<field>`), and unions of models
|
|
169
|
+
(`LeanPythonUnions.<module>.<Model>_<field>`, a constructor per model holding it as `val`). `multiple_of`, `pattern` and validators are not assumed in Lean; the structure's
|
|
170
|
+
docstring lists them. Generated functions take and return your classes; values that did not go
|
|
171
|
+
through validation (`model_construct`) are checked again on the way to Lean.
|
|
172
|
+
|
|
173
|
+
## The CLI
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
lean-python module generate --module-name <Name> [--roots <Name> ...] [--namespace <Name>]
|
|
177
|
+
[--search-path <dir> ...] [--output-file <path>] [--internal-only] [--output json|text]
|
|
178
|
+
[--type-module <Name>=<module> ...] [--model-module <Name>=<module> ...] [--batch <file>]
|
|
179
|
+
lean-python --version | --generator-version | help
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
`module generate` writes one Python module with the IR of the roots and of everything they call
|
|
183
|
+
(Init and Std included), and public functions and types for the roots. Definitions it cannot expose
|
|
184
|
+
(polymorphic functions, functions over functions) are reported as skipped. Exit codes: 0 success,
|
|
185
|
+
252 invalid parameters, 254 input that cannot be converted, 255 other errors.
|
|
186
|
+
|
|
187
|
+
## Performance
|
|
188
|
+
|
|
189
|
+
`make bench` (CPython 3.14, x86_64; the native column is the C backend):
|
|
190
|
+
|
|
191
|
+
| workload | n | Python (s) | native C (s) |
|
|
192
|
+
|---|---|---|---|
|
|
193
|
+
| `Array.push`, `set!`, sum | 10⁴ | 0.017 | 0.005 |
|
|
194
|
+
| `Array.push`, `set!`, sum | 10⁵ | 0.179 | 0.006 |
|
|
195
|
+
| `Array.push`, `set!`, sum | 10⁶ | 1.796 | 0.017 |
|
|
196
|
+
| `fib` (loop) | 10⁵ | 0.176 | 0.074 |
|
|
197
|
+
| `naiveFib` | 25 | 0.149 | 0.005 |
|
|
198
|
+
| `String.push` | 10⁴ | 0.012 | 0.005 |
|
|
199
|
+
| `String.push` | 10⁵ | 0.227 | 0.006 |
|
|
200
|
+
| `Array.qsort` | 10⁵ | 1.929 | 0.020 |
|
|
201
|
+
|
|
202
|
+
Passing 10⁴ models to a Lean function takes 0.036 s (pydantic's `TypeAdapter(list[Model])` validates
|
|
203
|
+
10⁴ dicts in 0.009 s).
|
|
204
|
+
|
|
205
|
+
## Development
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
nix develop -c make check # treefmt, the diff tests against the C backend, pytest
|
|
209
|
+
nix develop -c make bench
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
- `LeanPython/EmitPy.lean`: IR → Python. `LeanPython/Marshal.lean`, `LeanPython/Generate.lean`: public
|
|
213
|
+
types and functions. `Main.lean`: the CLI.
|
|
214
|
+
- `src/lean_python/runtime.py`, `io_externs.py`: the runtime (`lean_*` are the C names of Lean's
|
|
215
|
+
externs). `marshal.py`, `types.py`: pydantic types. `embed.py`, `sync.py`, `models.py`, `cli.py`:
|
|
216
|
+
`lean(...)` and `lean-py sync`.
|
|
217
|
+
- `LeanPythonTest/`: Lean programs of the tests; `test-diff.sh` runs `Vectors` and `Io` natively and
|
|
218
|
+
as Python.
|
|
219
|
+
|
|
220
|
+
## Limitations
|
|
221
|
+
|
|
222
|
+
- Polymorphic functions, functions taking or returning functions, `Thunk`/`Task`/`IO.Ref`/
|
|
223
|
+
`ByteArray` in public signatures are not exposed (they still run inside Lean code).
|
|
224
|
+
- `Task`s run synchronously.
|
|
225
|
+
- Building a `String` with `String.push` copies the string each time (Python `bytes`), so it grows
|
|
226
|
+
quadratically.
|
|
227
|
+
- Python's own functions are not verified in Lean yet.
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
Apache-2.0
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
lean_python/__init__.py,sha256=w09gaS4QGgFS0pM8Y3aSVMfQm-iHgEG_Sm_RqwKh--I,182
|
|
2
|
+
lean_python/cli.py,sha256=ERwAkhhFJV32BqS3-lDHPYi0UiCbydyEFI0ZlHobmsk,1559
|
|
3
|
+
lean_python/embed.py,sha256=WBEWYN7r3xFbSNLgvRRfn-6qhcR5Q1h1ge8KwQ5yyfE,3996
|
|
4
|
+
lean_python/io.py,sha256=l7LErXI_DhACZf9Jaef3r5Stqh4PdUk-bCNTo91BLE0,356
|
|
5
|
+
lean_python/io_externs.py,sha256=lI7oK7WTMjYV1JxaNzEnscVMunME_5SYoGDz3Qq1GSM,29807
|
|
6
|
+
lean_python/marshal.py,sha256=WIY3yJtnIMvym8_jraY8F1tW8x1iYqRkcS4W_FGWwLM,20778
|
|
7
|
+
lean_python/models.py,sha256=wunIZ9hOG-8H3shdRClLN7pR6Kmofo9zlPcL3Hr0rq0,10940
|
|
8
|
+
lean_python/runtime.py,sha256=Fr3R6x-kZUW2H1_NwbwI_LI97OUmMQ8Q0smnPKyyAAc,28439
|
|
9
|
+
lean_python/sync.py,sha256=JKJj3IWQlrqjTvK_-puPuF1Nt0LMumywUilUJANqhfc,9304
|
|
10
|
+
lean_python/types.py,sha256=qOhcwe1lYGD17OHW_A4LzfGi2bcPovTANTggaJwG-FM,843
|
|
11
|
+
lean_python-0.1.0.dist-info/METADATA,sha256=7IosM2WL2AjIjMM0CYy015gHPmkViqxr_vT075QI1gw,9775
|
|
12
|
+
lean_python-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
13
|
+
lean_python-0.1.0.dist-info/entry_points.txt,sha256=BD86jsJKikYcvsZse4ysbLvvN7TuGogwmiUZZwu7FBo,49
|
|
14
|
+
lean_python-0.1.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
15
|
+
lean_python-0.1.0.dist-info/RECORD,,
|