leanfran 0.6.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.
leanfran/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ """leanfran: Lean 4 code compiled to Python."""
2
+
3
+ from leanfran.embed import LeanBlockStale, lean
4
+ from leanfran.types import Nat
5
+
6
+ __all__ = ["LeanBlockStale", "Nat", "lean"]
leanfran/check.py ADDED
@@ -0,0 +1,292 @@
1
+ """``leanfran check`` and ``leanfran status``: the proofs of the ``@model`` functions of a package.
2
+
3
+ ``check`` syncs the package's Lean blocks, translates every ``@model`` function into
4
+ ``LeanPythonModel.<module>`` with one obligation per specification (``<function>.obligation.<label>``),
5
+ builds them with the proofs in ``<project>/lean/Proofs/`` (theorems ``<function>.proof.<label>``),
6
+ and asks the Lean CLI for the state of each obligation: proved, sorry, axioms, missing,
7
+ mismatch. The result and a hash of each function's source go to ``<project>/leanfran.lock``.
8
+ ``status`` reads the lock without Lean and marks a function stale when its source changed.
9
+ """
10
+
11
+ import ast
12
+ import hashlib
13
+ import importlib
14
+ import inspect
15
+ import json
16
+ import pathlib
17
+ import shutil
18
+ import subprocess
19
+ import sys
20
+ import textwrap
21
+
22
+ from leanfran import models, sync, translate, verify
23
+
24
+ LAKEFILE_EXTRA = """
25
+ [[lean_lib]]
26
+ name = "LeanPythonLib"
27
+
28
+ [[lean_lib]]
29
+ name = "LeanPythonModel"
30
+
31
+ [[lean_lib]]
32
+ name = "Proofs"
33
+ globs = ["Proofs.+"]
34
+ """
35
+
36
+ STATUSES = ("proved", "sorry", "axioms", "failed", "missing", "mismatch", "unextracted", "stale")
37
+
38
+
39
+ def model_module(module):
40
+ return ["LeanPythonModel", *module.split(".")]
41
+
42
+
43
+ def function_name(fn):
44
+ return models.dotted([*model_module(fn.__module__), fn.__qualname__])
45
+
46
+
47
+ def source_hash(fn):
48
+ src = textwrap.dedent(inspect.getsource(fn))
49
+ return hashlib.sha256(ast.dump(ast.parse(src)).encode()).hexdigest()
50
+
51
+
52
+ def lean_name_of(spec_fn, label, fn):
53
+ name = getattr(spec_fn, "__lean_name__", None)
54
+ if name is None:
55
+ raise sync.SyncError(
56
+ f"{fn.__qualname__}: the {label} specification is not a function of a lean(...) block", 252
57
+ )
58
+ return name, getattr(spec_fn, "__lean_module__", None)
59
+
60
+
61
+ def obligations(spec, tfn):
62
+ """The Lean statements of the obligations of one function, by label."""
63
+ fn = spec.fn
64
+ binders = []
65
+ args = []
66
+ for name, ann in tfn.params:
67
+ t = translate.py_type(ann, fn)
68
+ ty = "Nat" if translate.is_nat(ann) else translate.lean_type(t)
69
+ binders.append(f"({translate.lean_ident(name)} : {ty})")
70
+ args.append(translate.lean_ident(name))
71
+ call = f"({tfn.lean_name} {' '.join(args)})"
72
+ a = " ".join(args)
73
+ imports = set()
74
+ pre = ""
75
+ if spec.requires is not None:
76
+ r, m = lean_name_of(spec.requires, "requires", fn)
77
+ imports.add(m)
78
+ pre = f"{r} {a} = true → "
79
+ out = {}
80
+
81
+ def wrap(body):
82
+ return f"∀ {' '.join(binders)}, {pre}{body}" if binders else f"{pre}{body}"
83
+
84
+ if spec.refines is not None:
85
+ s, m = lean_name_of(spec.refines, "refines", fn)
86
+ imports.add(m)
87
+ ret = getattr(spec.refines, "__annotations__", {}).get("return")
88
+ expected = f"(({s} {a} : Nat) : Int)" if ret == "Nat" else f"({s} {a})"
89
+ out["refines"] = wrap(f"{call} = Except.ok {expected}" if tfn.fails else f"{call} = {expected}")
90
+ for label, e in spec.ensures.items():
91
+ en, m = lean_name_of(e, label, fn)
92
+ imports.add(m)
93
+ out[label] = wrap(f"∀ r, {call} = Except.ok r → {en} {a} r = true" if tfn.fails else f"{en} {a} {call} = true")
94
+ if tfn.ret_nat:
95
+ out["returns_nat"] = wrap(f"∀ r, {call} = Except.ok r → 0 ≤ r" if tfn.fails else f"0 ≤ {call}")
96
+ return out, {i for i in imports if i}
97
+
98
+
99
+ def reimport(pkg):
100
+ for name in [m for m in sys.modules if m == pkg or m.startswith(pkg + ".")]:
101
+ del sys.modules[name]
102
+ importlib.invalidate_caches()
103
+
104
+
105
+ def check_package(pkg_dir, cli=None, out=sys.stderr):
106
+ pkg_dir = pathlib.Path(pkg_dir).resolve()
107
+ cli = cli or shutil.which("leanfran-python")
108
+ sync.sync_package(pkg_dir, cli=cli, out=out)
109
+ root = sync.project_root(pkg_dir)
110
+ ws = root / ".leanfran" / "workspace"
111
+
112
+ reimport(pkg_dir.name)
113
+ verify.REGISTRY.clear()
114
+ for path in sorted(pkg_dir.rglob("*.py")):
115
+ rel = path.relative_to(pkg_dir)
116
+ if rel.parts[0] == "_lean" or rel.name == "__main__.py":
117
+ continue
118
+ parts = [pkg_dir.name, *rel.with_suffix("").parts]
119
+ if parts[-1] == "__init__":
120
+ parts = parts[:-1]
121
+ importlib.import_module(".".join(parts))
122
+ specs = list(verify.REGISTRY)
123
+
124
+ lib = ws / "LeanPythonLib"
125
+ shutil.rmtree(lib, ignore_errors=True)
126
+ shutil.copytree(pathlib.Path(__file__).parent / "lean" / "LeanPythonLib", lib)
127
+ (ws / "LeanPythonLib.lean").write_text("import LeanPythonLib.Py\n")
128
+ shutil.rmtree(ws / "LeanPythonModel", ignore_errors=True)
129
+ shutil.rmtree(ws / "Proofs", ignore_errors=True)
130
+ proofs_src = root / "lean" / "Proofs"
131
+ if proofs_src.is_dir():
132
+ shutil.copytree(proofs_src, ws / "Proofs")
133
+ else:
134
+ (ws / "Proofs").mkdir()
135
+ (ws / "Proofs" / "Empty.lean").write_text("")
136
+ lakefile = (ws / "lakefile.toml").read_text()
137
+ (ws / "lakefile.toml").write_text(lakefile + LAKEFILE_EXTRA)
138
+
139
+ by_module = {}
140
+ for s in specs:
141
+ by_module.setdefault(s.fn.__module__, []).append(s)
142
+ model_modules = []
143
+ obligation_refs = []
144
+ for module, ss in by_module.items():
145
+ tfns = [translate.Fn(s.fn, function_name(s.fn)) for s in ss]
146
+ tr = translate.Translator(tfns)
147
+ model_classes = set()
148
+ for t in tfns:
149
+ for _, ann in t.params:
150
+ _collect_models(ann, model_classes)
151
+ _collect_models(t.hints["return"], model_classes)
152
+ imports = {"LeanPythonLib.Py"}
153
+ imports |= (
154
+ {models.dotted(models.models_namespace(m)) for m in models.modules_source(list(model_classes))}
155
+ if model_classes
156
+ else set()
157
+ )
158
+ defs = []
159
+ for s, t in zip(ss, tfns, strict=True):
160
+ try:
161
+ defs.append(tr.definition(t))
162
+ except translate.TranslateError as e:
163
+ raise sync.SyncError(str(e), 254) from None
164
+ obls, extra = obligations(s, t)
165
+ imports |= extra
166
+ for label, prop in obls.items():
167
+ defs.append(f"abbrev {t.lean_name}.obligation.{label} : Prop :=\n {prop}")
168
+ obligation_refs.append((s, label))
169
+ missing_models = [m for m in models.modules_source(list(model_classes))] if model_classes else []
170
+ for m in missing_models:
171
+ p = ws / pathlib.Path(*models.models_namespace(m)).with_suffix(".lean")
172
+ if not p.exists():
173
+ p.parent.mkdir(parents=True, exist_ok=True)
174
+ p.write_text(models.modules_source(list(model_classes))[m])
175
+ path = ws / pathlib.Path(*model_module(module)).with_suffix(".lean")
176
+ path.parent.mkdir(parents=True, exist_ok=True)
177
+ head = "".join(f"import {i}\n" for i in sorted(imports))
178
+ path.write_text(head + "\n" + "\n\n".join(defs) + "\n")
179
+ model_modules.append(".".join(model_module(module)))
180
+ (ws / "LeanPythonModel.lean").write_text(
181
+ "".join(f"import {models.dotted(m.split('.'))}\n" for m in model_modules) or "\n"
182
+ )
183
+ (ws / "LeanPythonModels.lean").write_text(
184
+ "".join(
185
+ f"import {models.dotted(p.relative_to(ws).with_suffix('').parts)}\n"
186
+ for p in sorted((ws / "LeanPythonModels").rglob("*.lean"))
187
+ )
188
+ if (ws / "LeanPythonModels").is_dir()
189
+ else "\n"
190
+ )
191
+ proof_modules = [".".join(p.relative_to(ws).with_suffix("").parts) for p in sorted((ws / "Proofs").rglob("*.lean"))]
192
+ (ws / "Proofs.lean").write_text("".join(f"import {models.dotted(m.split('.'))}\n" for m in proof_modules))
193
+
194
+ r = subprocess.run(
195
+ ["lake", "build", "LeanPythonLib", "LeanPythonModels", "LeanPythonEmbed", "LeanPythonModel"],
196
+ cwd=ws,
197
+ capture_output=True,
198
+ text=True,
199
+ check=False,
200
+ )
201
+ if r.returncode != 0:
202
+ print(r.stdout + r.stderr, file=out)
203
+ raise sync.SyncError("lake build of the translated functions failed", 254)
204
+ failed_files = {}
205
+ built = []
206
+ for m in proof_modules:
207
+ r = subprocess.run(["lake", "build", m], cwd=ws, capture_output=True, text=True, check=False)
208
+ if r.returncode == 0:
209
+ built.append(m)
210
+ continue
211
+ src = (ws / pathlib.Path(*m.split("."))).with_suffix(".lean")
212
+ failed_files[m] = src.read_text()
213
+ for line in (r.stdout + r.stderr).splitlines():
214
+ if line.startswith(("error:", " ", "⊢", "case ")) or ": error" in line:
215
+ print(f"leanfran: {line}", file=out)
216
+ proof_modules = built
217
+
218
+ obl_file = ws / "obligations.json"
219
+ obl_file.write_text(
220
+ json.dumps([{"model": _plain(function_name(s.fn)), "label": label} for s, label in obligation_refs])
221
+ )
222
+ r = subprocess.run(
223
+ [
224
+ cli,
225
+ "obligations",
226
+ "check",
227
+ "--search-path",
228
+ str(ws / ".lake/build/lib/lean"),
229
+ "--obligations",
230
+ str(obl_file),
231
+ "--modules",
232
+ *model_modules,
233
+ *proof_modules,
234
+ ],
235
+ capture_output=True,
236
+ text=True,
237
+ check=False,
238
+ )
239
+ if r.returncode != 0:
240
+ print(r.stderr, file=out)
241
+ raise sync.SyncError("leanfran-python obligations check failed", r.returncode)
242
+ results = json.loads(r.stdout)
243
+ for (s, label), res in zip(obligation_refs, results, strict=True):
244
+ theorem = f"{s.fn.__qualname__}.proof.{label}"
245
+ if res["status"] == "missing" and any(theorem in text for text in failed_files.values()):
246
+ res["status"] = "failed"
247
+ lock = {}
248
+ for (s, label), res in zip(obligation_refs, results, strict=True):
249
+ entry = lock.setdefault(s.qualname, {"hash": source_hash(s.fn), "lean": function_name(s.fn), "obligations": {}})
250
+ entry["obligations"][label] = res["status"]
251
+ (root / "leanfran.lock").write_text(json.dumps(lock, indent=2, sort_keys=True) + "\n")
252
+ return lock, results
253
+
254
+
255
+ def _plain(name):
256
+ return name.replace("«", "").replace("»", "")
257
+
258
+
259
+ def _collect_models(ann, acc):
260
+ import typing
261
+
262
+ from pydantic import BaseModel
263
+
264
+ while typing.get_origin(ann) is typing.Annotated:
265
+ ann = typing.get_args(ann)[0]
266
+ if isinstance(ann, type) and issubclass(ann, BaseModel):
267
+ acc.add(ann)
268
+ return
269
+ for a in typing.get_args(ann):
270
+ _collect_models(a, acc)
271
+
272
+
273
+ def status(pkg_dir):
274
+ """The lock's states, with ``stale`` for functions whose source changed (no Lean needed)."""
275
+ pkg_dir = pathlib.Path(pkg_dir).resolve()
276
+ root = sync.project_root(pkg_dir)
277
+ lock_path = root / "leanfran.lock"
278
+ lock = json.loads(lock_path.read_text()) if lock_path.exists() else {}
279
+ sys.path.insert(0, str(pkg_dir.parent))
280
+ verify.REGISTRY.clear()
281
+ sync.record_blocks(pkg_dir)
282
+ rows = []
283
+ for s in verify.REGISTRY:
284
+ entry = lock.get(s.qualname)
285
+ labels = list(s.specs())
286
+ if entry is None:
287
+ rows.extend((s.qualname, label, "stale") for label in labels if label != "requires")
288
+ continue
289
+ stale = entry["hash"] != source_hash(s.fn)
290
+ for label, st in entry["obligations"].items():
291
+ rows.append((s.qualname, label, "stale" if stale else st))
292
+ return rows
leanfran/cli.py ADDED
@@ -0,0 +1,73 @@
1
+ """``leanfran``: the command line of the Python package.
2
+
3
+ usage: leanfran sync --package <dir> [--leanfran-python <path>]
4
+ """
5
+
6
+ import argparse
7
+ import os
8
+ import sys
9
+
10
+ USAGE = """usage: leanfran <command> [parameters]
11
+
12
+ commands:
13
+ sync Check the Lean blocks of a package and generate their Python modules
14
+ check Translate the @model functions to Lean and judge the proofs of their obligations
15
+ status Show the states recorded by `check`, marking functions changed since then (no Lean)
16
+
17
+ leanfran sync
18
+ --package <dir> The Python package (the directory with __init__.py) (required)
19
+ --leanfran-python <path> The Lean CLI (default: $LEANFRAN_PYTHON_CLI, then `leanfran-python` on PATH)
20
+
21
+ leanfran check
22
+ --package <dir>, --leanfran-python <path> as for sync
23
+ --require proved Exit with 254 unless every obligation is proved
24
+
25
+ leanfran status
26
+ --package <dir> The Python package (required)
27
+
28
+ exit codes: 0 success, 252 invalid parameters, 254 input that cannot be converted, 255 other errors
29
+ """
30
+
31
+
32
+ def main(argv=None):
33
+ argv = sys.argv[1:] if argv is None else argv
34
+ if not argv or argv[0] in ("help", "--help", "-h"):
35
+ print(USAGE, end="")
36
+ return 0
37
+ command = argv[0]
38
+ if command not in ("sync", "check", "status"):
39
+ print(f"leanfran: unknown command {command!r} (see `leanfran help`)", file=sys.stderr)
40
+ return 252
41
+ p = argparse.ArgumentParser(prog=f"leanfran {command}", add_help=False, exit_on_error=False)
42
+ p.add_argument("--package", required=True)
43
+ p.add_argument("--leanfran-python")
44
+ p.add_argument("--require", choices=["proved"])
45
+ try:
46
+ args = p.parse_args(argv[1:])
47
+ except (argparse.ArgumentError, SystemExit) as e:
48
+ print(f"leanfran: {e}", file=sys.stderr)
49
+ return 252
50
+ from leanfran import check, sync
51
+
52
+ cli = args.leanfran_python or os.environ.get("LEANFRAN_PYTHON_CLI")
53
+ try:
54
+ if command == "sync":
55
+ sync.sync_package(args.package, cli=cli)
56
+ return 0
57
+ if command == "check":
58
+ lock, _ = check.check_package(args.package, cli=cli)
59
+ rows = [(q, label, st) for q, e in sorted(lock.items()) for label, st in e["obligations"].items()]
60
+ else:
61
+ rows = check.status(args.package)
62
+ except sync.SyncError as e:
63
+ print(f"leanfran: {e}", file=sys.stderr)
64
+ return e.code
65
+ for q, label, st in rows:
66
+ print(f"{st:<10} {q} {label}")
67
+ if args.require == "proved" and any(st != "proved" for _, _, st in rows):
68
+ return 254
69
+ return 0
70
+
71
+
72
+ if __name__ == "__main__":
73
+ sys.exit(main())
leanfran/embed.py ADDED
@@ -0,0 +1,131 @@
1
+ """``lean(name, source)``: Lean code in a Python module.
2
+
3
+ ``leanfran sync`` checks the proofs of every block of a package with Lake and writes the Python it
4
+ generates to ``<package>/_lean/``. At run time ``lean(...)`` only imports that module, after
5
+ checking that it was generated from the same source; Lean is not needed to run the package.
6
+ """
7
+
8
+ import hashlib
9
+ import importlib
10
+ import json
11
+ import os
12
+ import subprocess
13
+ import sys
14
+ import typing
15
+ from dataclasses import dataclass, field
16
+
17
+ GENERATOR_VERSION = "3"
18
+
19
+
20
+ class LeanBlockStale(RuntimeError):
21
+ """The generated module of a block is missing or was generated from a different source."""
22
+
23
+
24
+ @dataclass
25
+ class Block:
26
+ module: str
27
+ name: str
28
+ source: str
29
+ imports: tuple
30
+ uses: tuple
31
+ models: tuple
32
+ file: str
33
+ line: int
34
+ key: str = field(default="")
35
+
36
+ @property
37
+ def qualname(self):
38
+ return f"{self.module}:{self.name}"
39
+
40
+ @property
41
+ def stem(self):
42
+ return "__".join([*self.module.split("."), self.name])
43
+
44
+ @property
45
+ def lean_module(self):
46
+ return ["LeanPythonEmbed", *self.module.split("."), self.name]
47
+
48
+ @property
49
+ def top_package(self):
50
+ return self.module.split(".")[0]
51
+
52
+
53
+ def block_key(block):
54
+ from leanfran import models
55
+
56
+ model_sources = models.modules_source(list(block.models)) if block.models else {}
57
+ data = {
58
+ "module": block.module,
59
+ "name": block.name,
60
+ "source": block.source,
61
+ "imports": list(block.imports),
62
+ "uses": list(block.uses),
63
+ "models": model_sources,
64
+ "generator": GENERATOR_VERSION,
65
+ }
66
+ return hashlib.sha256(json.dumps(data, sort_keys=True).encode()).hexdigest()
67
+
68
+
69
+ _recording = None
70
+ _registries = {}
71
+
72
+
73
+ class _Placeholder:
74
+ """What `lean(...)` returns while `leanfran sync` records the blocks: names are `Any`."""
75
+
76
+ def __getattr__(self, name):
77
+ return typing.Any
78
+
79
+
80
+ def _registry(pkg):
81
+ reg = _registries.get(pkg)
82
+ if reg is None:
83
+ try:
84
+ base = sys.modules[pkg].__path__[0]
85
+ except (KeyError, AttributeError, IndexError):
86
+ raise LeanBlockStale(f"lean(...) must be called from a module of a package (got {pkg!r})") from None
87
+ path = os.path.join(base, "_lean", "blocks.json")
88
+ try:
89
+ with open(path, encoding="utf-8") as f:
90
+ reg = json.load(f)
91
+ except FileNotFoundError:
92
+ reg = {}
93
+ _registries[pkg] = reg
94
+ return reg
95
+
96
+
97
+ def lean(name, source, *, imports=(), uses=(), models=()):
98
+ """Defines the Lean block ``name`` of the calling module; returns its generated module."""
99
+ frame = sys._getframe(1)
100
+ block = Block(
101
+ module=frame.f_globals["__name__"],
102
+ name=name,
103
+ source=source,
104
+ imports=tuple(imports),
105
+ uses=tuple(uses),
106
+ models=tuple(models),
107
+ file=frame.f_code.co_filename,
108
+ line=frame.f_lineno,
109
+ )
110
+ if _recording is not None:
111
+ _recording.append(block)
112
+ return _Placeholder()
113
+ block.key = block_key(block)
114
+ pkg = block.top_package
115
+ if _registry(pkg).get(block.qualname) != block.key:
116
+ if os.environ.get("LEANFRAN_PYTHON_AUTO_SYNC") == "1":
117
+ r = subprocess.run(
118
+ [sys.executable, "-m", "leanfran.cli", "sync", "--package", sys.modules[pkg].__path__[0]],
119
+ check=False,
120
+ env={**os.environ, "LEANFRAN_PYTHON_AUTO_SYNC": "0"},
121
+ )
122
+ if r.returncode != 0:
123
+ raise LeanBlockStale(f"`leanfran sync` of {pkg} failed (exit {r.returncode})")
124
+ _registries.pop(pkg, None)
125
+ importlib.invalidate_caches()
126
+ if _registry(pkg).get(block.qualname) != block.key:
127
+ raise LeanBlockStale(
128
+ f"the Lean block {name!r} of {block.module} has changed since `leanfran sync` generated it;"
129
+ " run `leanfran sync` (or set LEANFRAN_PYTHON_AUTO_SYNC=1)"
130
+ )
131
+ return importlib.import_module(f"{pkg}._lean.{block.stem}")
leanfran/io.py ADDED
@@ -0,0 +1,15 @@
1
+ """Running a Lean `main`-like IO action."""
2
+
3
+ import sys
4
+
5
+ from leanfran.io_externs import lean_io_result_show_error
6
+
7
+
8
+ def run_main(f):
9
+ """Runs the IR function of an `IO Unit` action as Lean's C `main` does; returns the exit code."""
10
+ r = f()
11
+ sys.stdout.flush()
12
+ if r.tag == 0:
13
+ return 0
14
+ lean_io_result_show_error(r)
15
+ return 1