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/translate.py ADDED
@@ -0,0 +1,469 @@
1
+ """Translation of a typed, pure Python function into a Lean definition (``leanfran check``).
2
+
3
+ The body becomes Lean ``do`` notation, which has Python's statements: ``let mut`` for local
4
+ variables, ``for``/``while`` with ``break``/``continue``, ``if``, early ``return``. A function that
5
+ may fail (indexing, ``//``, ``%``, ``assert``, ``raise``, or a call of such a function) returns
6
+ ``Py.M α`` (``Except Py.Exc α``); the others return ``α`` through ``Id.run do``. Python's ``int``
7
+ is ``Int``; a ``Nat`` parameter is converted to ``Int`` on entry.
8
+ """
9
+
10
+ import ast
11
+ import inspect
12
+ import textwrap
13
+ import typing
14
+
15
+ from pydantic import BaseModel
16
+
17
+ from leanfran import models
18
+ from leanfran.types import Nat
19
+
20
+
21
+ class TranslateError(Exception):
22
+ def __init__(self, msg, fn=None, node=None):
23
+ where = ""
24
+ if fn is not None:
25
+ line = fn.__code__.co_firstlineno + (node.lineno - 1 if node is not None and hasattr(node, "lineno") else 0)
26
+ where = f"{fn.__code__.co_filename}:{line}: "
27
+ super().__init__(f"{where}{msg}")
28
+
29
+
30
+ INT, BOOL, STR, FLOAT, NONE = "int", "bool", "str", "float", "none"
31
+
32
+
33
+ def py_type(t, fn):
34
+ """A small type descriptor of a Python annotation."""
35
+ if t is Nat or (typing.get_origin(t) is typing.Annotated and typing.get_args(t)[0] is int):
36
+ return INT
37
+ if t is int:
38
+ return INT
39
+ if t is bool:
40
+ return BOOL
41
+ if t is str:
42
+ return STR
43
+ if t is float:
44
+ return FLOAT
45
+ if t is type(None) or t is None:
46
+ return NONE
47
+ origin = typing.get_origin(t)
48
+ if origin is list:
49
+ return ("list", py_type(typing.get_args(t)[0], fn))
50
+ if origin is tuple:
51
+ return ("tuple", tuple(py_type(a, fn) for a in typing.get_args(t)))
52
+ args = typing.get_args(t)
53
+ if args and type(None) in args and len(args) == 2:
54
+ return ("opt", py_type(next(a for a in args if a is not type(None)), fn))
55
+ if isinstance(t, type) and issubclass(t, BaseModel):
56
+ return ("model", t)
57
+ raise TranslateError(f"unsupported type {t!r}", fn)
58
+
59
+
60
+ def lean_type(t):
61
+ if t == INT:
62
+ return "Int"
63
+ if t == BOOL:
64
+ return "Bool"
65
+ if t == STR:
66
+ return "String"
67
+ if t == FLOAT:
68
+ return "Float"
69
+ if t == NONE:
70
+ return "Unit"
71
+ kind = t[0]
72
+ if kind == "list":
73
+ return f"(List {lean_type(t[1])})"
74
+ if kind == "tuple":
75
+ return "(" + " × ".join(lean_type(a) for a in t[1]) + ")"
76
+ if kind == "opt":
77
+ return f"(Option {lean_type(t[1])})"
78
+ return models.class_name(t[1])
79
+
80
+
81
+ def is_nat(annotation):
82
+ if annotation is Nat:
83
+ return True
84
+ if typing.get_origin(annotation) is typing.Annotated:
85
+ import annotated_types as at
86
+
87
+ base, *meta = typing.get_args(annotation)
88
+ return base is int and any(isinstance(m, at.Ge) and m.ge >= 0 for m in meta)
89
+ return False
90
+
91
+
92
+ def field_info(cls, name):
93
+ """The Python type of a model field and whether its Lean field is a `Nat`."""
94
+ f = cls.model_fields.get(name)
95
+ if f is None:
96
+ raise TranslateError(f"{cls.__name__} has no field {name}")
97
+ t = py_type(f.annotation, None)
98
+ nat = False
99
+ if t == INT:
100
+ import annotated_types as at
101
+
102
+ lo = [m.ge for m in f.metadata if isinstance(m, at.Ge)] + [m.gt + 1 for m in f.metadata if isinstance(m, at.Gt)]
103
+ nat = is_nat(f.annotation) or (bool(lo) and max(lo) >= 0)
104
+ return t, nat
105
+
106
+
107
+ class Fn:
108
+ """A function to translate: its signature and the names Lean knows it by."""
109
+
110
+ def __init__(self, fn, lean_name):
111
+ self.fn = fn
112
+ self.lean_name = lean_name
113
+ self.hints = typing.get_type_hints(fn, include_extras=True)
114
+ sig = inspect.signature(fn)
115
+ self.params = []
116
+ for p in sig.parameters.values():
117
+ if p.name not in self.hints:
118
+ raise TranslateError(f"parameter {p.name} of {fn.__qualname__} has no type annotation", fn)
119
+ self.params.append((p.name, self.hints[p.name]))
120
+ if "return" not in self.hints:
121
+ raise TranslateError(f"{fn.__qualname__} has no return annotation", fn)
122
+ self.ret = py_type(self.hints["return"], fn)
123
+ self.ret_nat = is_nat(self.hints["return"])
124
+ src = textwrap.dedent(inspect.getsource(fn))
125
+ tree = ast.parse(src)
126
+ self.node = next(n for n in tree.body if isinstance(n, ast.FunctionDef))
127
+ self.fails = None
128
+
129
+
130
+ class Translator:
131
+ def __init__(self, fns):
132
+ self.fns = {f.fn.__name__: f for f in fns}
133
+ for f in fns:
134
+ f.fails = self._may_fail(f.node)
135
+ changed = True
136
+ while changed:
137
+ changed = False
138
+ for f in fns:
139
+ if not f.fails and any(
140
+ isinstance(n, ast.Call)
141
+ and isinstance(n.func, ast.Name)
142
+ and n.func.id in self.fns
143
+ and self.fns[n.func.id].fails
144
+ for stmt in f.node.body
145
+ for n in ast.walk(stmt)
146
+ ):
147
+ f.fails = True
148
+ changed = True
149
+
150
+ def _may_fail(self, node):
151
+ for stmt in node.body:
152
+ for n in ast.walk(stmt):
153
+ if isinstance(n, (ast.Subscript, ast.Assert, ast.Raise)):
154
+ return True
155
+ if isinstance(n, ast.BinOp) and isinstance(n.op, (ast.FloorDiv, ast.Mod)):
156
+ return True
157
+ return False
158
+
159
+ # -- expressions -------------------------------------------------------
160
+
161
+ def expr(self, e, env, fn):
162
+ """Lean source and type of a Python expression."""
163
+ f = fn.fn
164
+ if isinstance(e, ast.Constant):
165
+ v = e.value
166
+ if isinstance(v, bool):
167
+ return ("true" if v else "false"), BOOL
168
+ if isinstance(v, int):
169
+ return (f"({v} : Int)"), INT
170
+ if isinstance(v, str):
171
+ return json_str(v), STR
172
+ if isinstance(v, float):
173
+ return repr(v), FLOAT
174
+ if v is None:
175
+ return "none", ("opt", None)
176
+ raise TranslateError(f"unsupported constant {v!r}", f, e)
177
+ if isinstance(e, ast.Name):
178
+ if e.id not in env:
179
+ raise TranslateError(f"unknown name {e.id}", f, e)
180
+ return lean_ident(e.id), env[e.id]
181
+ if isinstance(e, ast.BinOp):
182
+ a, ta = self.expr(e.left, env, fn)
183
+ b, _ = self.expr(e.right, env, fn)
184
+ op = e.op
185
+ if isinstance(op, ast.Add):
186
+ if ta in (STR,) or (isinstance(ta, tuple) and ta[0] == "list"):
187
+ return f"({a} ++ {b})", ta
188
+ return f"({a} + {b})", ta
189
+ if isinstance(op, ast.Sub):
190
+ return f"({a} - {b})", ta
191
+ if isinstance(op, ast.Mult):
192
+ return f"({a} * {b})", ta
193
+ if isinstance(op, ast.FloorDiv):
194
+ return f"(← Py.floordiv {a} {b})", INT
195
+ if isinstance(op, ast.Mod):
196
+ return f"(← Py.mod {a} {b})", INT
197
+ raise TranslateError(f"unsupported operator {type(op).__name__}", f, e)
198
+ if isinstance(e, ast.UnaryOp):
199
+ a, ta = self.expr(e.operand, env, fn)
200
+ if isinstance(e.op, ast.USub):
201
+ return f"(-{a})", ta
202
+ if isinstance(e.op, ast.Not):
203
+ return f"(!{a})", BOOL
204
+ raise TranslateError("unsupported unary operator", f, e)
205
+ if isinstance(e, ast.BoolOp):
206
+ parts = [self.expr(v, env, fn)[0] for v in e.values]
207
+ op = " && " if isinstance(e.op, ast.And) else " || "
208
+ return "(" + op.join(parts) + ")", BOOL
209
+ if isinstance(e, ast.Compare):
210
+ parts = []
211
+ left, _ = self.expr(e.left, env, fn)
212
+ for op, right in zip(e.ops, e.comparators, strict=True):
213
+ r, _ = self.expr(right, env, fn)
214
+ if isinstance(op, ast.In):
215
+ parts.append(f"({r}.contains {left})")
216
+ elif isinstance(op, ast.NotIn):
217
+ parts.append(f"(!{r}.contains {left})")
218
+ else:
219
+ sym = {ast.Eq: "==", ast.NotEq: "!=", ast.Lt: "<", ast.LtE: "≤", ast.Gt: ">", ast.GtE: "≥"}.get(
220
+ type(op)
221
+ )
222
+ if sym is None:
223
+ raise TranslateError("unsupported comparison", f, e)
224
+ parts.append(f"({left} {sym} {r})" if sym in ("==", "!=") else f"(decide ({left} {sym} {r}))")
225
+ left = r
226
+ return "(" + " && ".join(parts) + ")", BOOL
227
+ if isinstance(e, ast.IfExp):
228
+ c, _ = self.expr(e.test, env, fn)
229
+ a, ta = self.expr(e.body, env, fn)
230
+ b, _ = self.expr(e.orelse, env, fn)
231
+ return f"(if {c} then {a} else {b})", ta
232
+ if isinstance(e, ast.Attribute):
233
+ obj, to = self.expr(e.value, env, fn)
234
+ if isinstance(to, tuple) and to[0] == "model":
235
+ t, nat = field_info(to[1], e.attr)
236
+ s = f"{obj}.{lean_ident(e.attr)}"
237
+ return (f"({s} : Int)" if nat else s), t
238
+ raise TranslateError(f"attribute {e.attr} of a non-model value", f, e)
239
+ if isinstance(e, ast.Subscript):
240
+ obj, to = self.expr(e.value, env, fn)
241
+ if isinstance(to, tuple) and to[0] == "tuple" and isinstance(e.slice, ast.Constant):
242
+ i = e.slice.value
243
+ n = len(to[1])
244
+ proj = obj
245
+ for _ in range(i):
246
+ proj = f"{proj}.2"
247
+ if i < n - 1:
248
+ proj = f"{proj}.1"
249
+ return proj, to[1][i]
250
+ if isinstance(to, tuple) and to[0] == "list":
251
+ i, _ = self.expr(e.slice, env, fn)
252
+ return f"(← Py.getItem {obj} {i})", to[1]
253
+ raise TranslateError("unsupported subscript", f, e)
254
+ if isinstance(e, ast.Tuple):
255
+ parts = [self.expr(v, env, fn) for v in e.elts]
256
+ return "(" + ", ".join(p for p, _ in parts) + ")", ("tuple", tuple(t for _, t in parts))
257
+ if isinstance(e, ast.List):
258
+ parts = [self.expr(v, env, fn) for v in e.elts]
259
+ t = parts[0][1] if parts else INT
260
+ return "[" + ", ".join(p for p, _ in parts) + "]", ("list", t)
261
+ if isinstance(e, ast.ListComp):
262
+ return self.listcomp(e, env, fn)
263
+ if isinstance(e, ast.Call):
264
+ return self.call(e, env, fn)
265
+ raise TranslateError(f"unsupported expression {type(e).__name__}", f, e)
266
+
267
+ def listcomp(self, e, env, fn):
268
+ if len(e.generators) != 1 or e.generators[0].is_async or not isinstance(e.generators[0].target, ast.Name):
269
+ raise TranslateError("only list comprehensions with one `for x in ...` are supported", fn.fn, e)
270
+ g = e.generators[0]
271
+ src, ts = self.iterable(g.iter, env, fn)
272
+ x = g.target.id
273
+ inner = {**env, x: ts}
274
+ for cond in g.ifs:
275
+ c, _ = self.expr(cond, inner, fn)
276
+ src = f"({src}.filter (fun {lean_ident(x)} => {c}))"
277
+ body, t_body = self.expr(e.elt, inner, fn)
278
+ if "←" in body:
279
+ raise TranslateError("a list comprehension must not fail", fn.fn, e)
280
+ return f"({src}.map (fun {lean_ident(x)} => {body}))", ("list", t_body)
281
+
282
+ def iterable(self, e, env, fn):
283
+ if isinstance(e, ast.Call) and isinstance(e.func, ast.Name) and e.func.id == "range":
284
+ args = [self.expr(a, env, fn)[0] for a in e.args]
285
+ if len(args) == 1:
286
+ return f"(Py.range 0 {args[0]})", INT
287
+ if len(args) == 2:
288
+ return f"(Py.range {args[0]} {args[1]})", INT
289
+ raise TranslateError("range with a step is not supported", fn.fn, e)
290
+ src, t = self.expr(e, env, fn)
291
+ if isinstance(t, tuple) and t[0] == "list":
292
+ return src, t[1]
293
+ raise TranslateError("iteration over a non-list", fn.fn, e)
294
+
295
+ def call(self, e, env, fn):
296
+ f = fn.fn
297
+ if e.keywords and not (isinstance(e.func, ast.Name) and e.func.id not in ("len", "abs", "min", "max", "sum")):
298
+ raise TranslateError("keyword arguments are not supported", f, e)
299
+ if isinstance(e.func, ast.Name):
300
+ name = e.func.id
301
+ args = [self.expr(a, env, fn) for a in e.args]
302
+ if name == "len":
303
+ ((a, ta),) = args
304
+ return (f"(Py.strLen {a})" if ta == STR else f"(Py.len {a})"), INT
305
+ if name == "abs":
306
+ return f"(Py.abs {args[0][0]})", INT
307
+ if name in ("min", "max") and len(args) == 2:
308
+ return f"({name} {args[0][0]} {args[1][0]})", args[0][1]
309
+ if name == "sum":
310
+ return f"(Py.sum {args[0][0]})", INT
311
+ if name in self.fns:
312
+ target = self.fns[name]
313
+ call = f"({target.lean_name} {' '.join(self.arg_for(target, i, a) for i, (a, _) in enumerate(args))})"
314
+ return (f"(← {call})" if target.fails else call), target.ret
315
+ raise TranslateError("unsupported call (only len, abs, min, max, sum and @model functions of the module)", f, e)
316
+
317
+ def arg_for(self, target, i, a):
318
+ if is_nat(target.params[i][1]):
319
+ return f"({a}).toNat"
320
+ return a
321
+
322
+ # -- statements --------------------------------------------------------
323
+
324
+ def body(self, stmts, env, fn, indent, declared):
325
+ out = []
326
+ pad = " " * indent
327
+ for s in stmts:
328
+ if isinstance(s, ast.Return):
329
+ if s.value is None:
330
+ raise TranslateError("a function must return a value", fn.fn, s)
331
+ v, _ = self.expr(s.value, env, fn)
332
+ out.append(f"{pad}return {v}")
333
+ elif isinstance(s, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
334
+ out.extend(self.assign(s, env, fn, pad, declared))
335
+ elif isinstance(s, ast.If):
336
+ c, _ = self.expr(s.test, env, fn)
337
+ out.append(f"{pad}if {c} then")
338
+ out.extend(self.body(s.body, env, fn, indent + 1, declared) or [f"{pad} pure ()"])
339
+ if s.orelse:
340
+ out.append(f"{pad}else")
341
+ out.extend(self.body(s.orelse, env, fn, indent + 1, declared))
342
+ elif isinstance(s, ast.For):
343
+ if not isinstance(s.target, ast.Name):
344
+ raise TranslateError("only `for x in ...` is supported", fn.fn, s)
345
+ src, t = self.iterable(s.iter, env, fn)
346
+ inner = dict(env)
347
+ inner[s.target.id] = t
348
+ out.append(f"{pad}for {lean_ident(s.target.id)} in {src} do")
349
+ out.extend(self.body(s.body, inner, fn, indent + 1, declared) or [f"{pad} pure ()"])
350
+ elif isinstance(s, ast.While):
351
+ c, _ = self.expr(s.test, env, fn)
352
+ out.append(f"{pad}while {c} do")
353
+ out.extend(self.body(s.body, env, fn, indent + 1, declared) or [f"{pad} pure ()"])
354
+ elif isinstance(s, ast.Break):
355
+ out.append(f"{pad}break")
356
+ elif isinstance(s, ast.Continue):
357
+ out.append(f"{pad}continue")
358
+ elif isinstance(s, ast.Pass):
359
+ out.append(f"{pad}pure ()")
360
+ elif isinstance(s, ast.Assert):
361
+ c, _ = self.expr(s.test, env, fn)
362
+ out.append(f"{pad}Py.assert {c}")
363
+ elif isinstance(s, ast.Raise):
364
+ msg = '""'
365
+ if isinstance(s.exc, ast.Call) and s.exc.args and isinstance(s.exc.args[0], ast.Constant):
366
+ msg = json_str(str(s.exc.args[0].value))
367
+ out.append(f"{pad}throw (.valueError {msg})")
368
+ elif (
369
+ isinstance(s, ast.Expr)
370
+ and isinstance(s.value, ast.Call)
371
+ and isinstance(s.value.func, ast.Attribute)
372
+ and s.value.func.attr == "append"
373
+ ):
374
+ target = s.value.func.value
375
+ if not (isinstance(target, ast.Name) and target.id in declared):
376
+ raise TranslateError("append only to a list created in the function", fn.fn, s)
377
+ v, _ = self.expr(s.value.args[0], env, fn)
378
+ out.append(f"{pad}{lean_ident(target.id)} := {lean_ident(target.id)} ++ [{v}]")
379
+ elif isinstance(s, ast.Expr) and isinstance(s.value, ast.Constant) and isinstance(s.value.value, str):
380
+ continue
381
+ else:
382
+ raise TranslateError(f"unsupported statement {type(s).__name__}", fn.fn, s)
383
+ return out
384
+
385
+ def assign(self, s, env, fn, pad, declared):
386
+ if isinstance(s, ast.AugAssign):
387
+ target = s.target
388
+ op = {ast.Add: "+", ast.Sub: "-", ast.Mult: "*"}.get(type(s.op))
389
+ if not isinstance(target, ast.Name) or target.id not in declared:
390
+ raise TranslateError("augmented assignment to an undefined local", fn.fn, s)
391
+ v, tv = self.expr(s.value, env, fn)
392
+ t = env[target.id]
393
+ if op == "+" and (t == STR or (isinstance(t, tuple) and t[0] == "list")):
394
+ op = "++"
395
+ if op is None:
396
+ if isinstance(s.op, ast.FloorDiv):
397
+ return [f"{pad}{lean_ident(target.id)} := (← Py.floordiv {lean_ident(target.id)} {v})"]
398
+ if isinstance(s.op, ast.Mod):
399
+ return [f"{pad}{lean_ident(target.id)} := (← Py.mod {lean_ident(target.id)} {v})"]
400
+ raise TranslateError("unsupported augmented assignment", fn.fn, s)
401
+ return [f"{pad}{lean_ident(target.id)} := {lean_ident(target.id)} {op} {v}"]
402
+ targets = [s.target] if isinstance(s, ast.AnnAssign) else s.targets
403
+ if len(targets) != 1:
404
+ raise TranslateError("chained assignment is not supported", fn.fn, s)
405
+ target = targets[0]
406
+ v, tv = self.expr(s.value, env, fn)
407
+ if isinstance(s, ast.AnnAssign):
408
+ tv = py_type(eval(compile(ast.Expression(s.annotation), "<annotation>", "eval"), fn.fn.__globals__), fn.fn)
409
+ names = [target] if isinstance(target, ast.Name) else (target.elts if isinstance(target, ast.Tuple) else None)
410
+ if names is None or not all(isinstance(n, ast.Name) for n in names):
411
+ raise TranslateError("only assignment to names is supported", fn.fn, s)
412
+ if isinstance(target, ast.Tuple):
413
+ types = tv[1] if isinstance(tv, tuple) and tv[0] == "tuple" else [None] * len(names)
414
+ out = []
415
+ tmp = "__t"
416
+ out.append(f"{pad}let {tmp} := {v}")
417
+ for i, (n, t) in enumerate(zip(names, types, strict=True)):
418
+ proj = tmp + ".2" * i + (".1" if i < len(names) - 1 else "")
419
+ out.extend(self._bind(n.id, proj, t, env, pad, declared))
420
+ return out
421
+ return self._bind(target.id, v, tv, env, pad, declared)
422
+
423
+ def _bind(self, name, v, t, env, pad, declared):
424
+ if name in declared:
425
+ return [f"{pad}{lean_ident(name)} := {v}"]
426
+ declared.add(name)
427
+ env[name] = t
428
+ ty = f" : {lean_type(t)}" if t is not None and not (isinstance(t, tuple) and t[1] is None) else ""
429
+ return [f"{pad}let mut {lean_ident(name)}{ty} := {v}"]
430
+
431
+ # -- definitions -------------------------------------------------------
432
+
433
+ def definition(self, fn):
434
+ env = {}
435
+ binders = []
436
+ prelude = []
437
+ for name, ann in fn.params:
438
+ t = py_type(ann, fn.fn)
439
+ env[name] = t
440
+ if is_nat(ann):
441
+ binders.append(f"({lean_ident(name)} : Nat)")
442
+ prelude.append(f" let {lean_ident(name)} : Int := {lean_ident(name)}")
443
+ else:
444
+ binders.append(f"({lean_ident(name)} : {lean_type(t)})")
445
+ declared = set()
446
+ ret = lean_type(fn.ret)
447
+ rty = f"Py.M {ret}" if fn.fails else ret
448
+ head = "do" if fn.fails else "Id.run do"
449
+ lines = self.body(fn.node.body, env, fn, 1, declared)
450
+ doc = f"/-- `{fn.fn.__module__}.{fn.fn.__qualname__}`, translated from Python. -/"
451
+ return "\n".join([doc, f"def {fn.lean_name} {' '.join(binders)} : {rty} := {head}", *prelude, *lines])
452
+
453
+
454
+ def lean_ident(name):
455
+ return models.ident(name)
456
+
457
+
458
+ def json_str(s):
459
+ out = '"'
460
+ for c in s:
461
+ if c == '"':
462
+ out += '\\"'
463
+ elif c == "\\":
464
+ out += "\\\\"
465
+ elif c == "\n":
466
+ out += "\\n"
467
+ else:
468
+ out += c
469
+ return out + '"'
leanfran/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
+ ]
leanfran/verify.py ADDED
@@ -0,0 +1,147 @@
1
+ """``@model``: bind a Python function to Lean specifications.
2
+
3
+ ``@model(refines=spec.f)`` says the function computes ``spec.f`` (a function of a ``lean(...)``
4
+ block); ``requires=`` and ``ensures=`` name ``Bool`` functions of blocks (the precondition takes
5
+ the arguments, the postcondition the arguments and the result). ``leanfran check`` translates the
6
+ function into Lean and reports whether the obligations are proved; ``check_model`` runs the same
7
+ specifications as a property test with Hypothesis. The decorator returns the function unchanged.
8
+ """
9
+
10
+ import inspect
11
+ import typing
12
+
13
+ REGISTRY = []
14
+
15
+
16
+ class ModelSpec:
17
+ def __init__(self, fn, refines, requires, ensures):
18
+ self.fn = fn
19
+ self.refines = refines
20
+ self.requires = requires
21
+ if ensures is None:
22
+ self.ensures = {}
23
+ elif isinstance(ensures, dict):
24
+ self.ensures = dict(ensures)
25
+ else:
26
+ self.ensures = {"ensures": ensures}
27
+
28
+ @property
29
+ def qualname(self):
30
+ return f"{self.fn.__module__}:{self.fn.__qualname__}"
31
+
32
+ def specs(self):
33
+ out = {}
34
+ if self.refines is not None:
35
+ out["refines"] = self.refines
36
+ if self.requires is not None:
37
+ out["requires"] = self.requires
38
+ out.update(self.ensures)
39
+ return out
40
+
41
+
42
+ def model(*, refines=None, requires=None, ensures=None):
43
+ """Binds the decorated function to Lean specifications (see the module docstring)."""
44
+
45
+ def deco(fn):
46
+ spec = ModelSpec(fn, refines, requires, ensures)
47
+ fn.__lean_model__ = spec
48
+ REGISTRY.append(spec)
49
+ return fn
50
+
51
+ return deco
52
+
53
+
54
+ def models_of(module):
55
+ return [v.__lean_model__ for v in vars(module).values() if callable(v) and hasattr(v, "__lean_model__")]
56
+
57
+
58
+ # ---------------------------------------------------------------------------
59
+ # Property tests
60
+
61
+
62
+ def strategy(annotation, metadata=()):
63
+ """A Hypothesis strategy of the values of a parameter or field type."""
64
+ import annotated_types as at
65
+ from hypothesis import strategies as st
66
+ from pydantic import BaseModel
67
+
68
+ metadata = list(metadata)
69
+ while typing.get_origin(annotation) is typing.Annotated:
70
+ annotation, *more = typing.get_args(annotation)
71
+ metadata.extend(more)
72
+ if annotation is int:
73
+ lo = hi = None
74
+ for m in metadata:
75
+ if isinstance(m, at.Ge):
76
+ lo = m.ge if lo is None else max(lo, m.ge)
77
+ elif isinstance(m, at.Gt):
78
+ lo = m.gt + 1 if lo is None else max(lo, m.gt + 1)
79
+ elif isinstance(m, at.Le):
80
+ hi = m.le if hi is None else min(hi, m.le)
81
+ elif isinstance(m, at.Lt):
82
+ hi = m.lt - 1 if hi is None else min(hi, m.lt - 1)
83
+ return st.integers(min_value=lo if lo is not None else -1000, max_value=hi if hi is not None else 1000)
84
+ if annotation is bool:
85
+ return st.booleans()
86
+ if annotation is str:
87
+ lo = max([m.min_length for m in metadata if isinstance(m, at.MinLen)], default=0)
88
+ hi = min([m.max_length for m in metadata if isinstance(m, at.MaxLen)], default=lo + 8)
89
+ return st.text(min_size=lo, max_size=hi)
90
+ if annotation is float:
91
+ return st.floats(allow_nan=False, allow_infinity=False, width=64)
92
+ origin = typing.get_origin(annotation)
93
+ if origin is list:
94
+ (a,) = typing.get_args(annotation)
95
+ return st.lists(strategy(a), max_size=8)
96
+ if origin is tuple:
97
+ return st.tuples(*(strategy(a) for a in typing.get_args(annotation)))
98
+ args = typing.get_args(annotation)
99
+ if args and type(None) in args:
100
+ rest = [a for a in args if a is not type(None)]
101
+ return st.none() | st.one_of(*(strategy(a) for a in rest))
102
+ if isinstance(annotation, type) and issubclass(annotation, BaseModel):
103
+ fields = {n: strategy(f.annotation, f.metadata) for n, f in annotation.model_fields.items()}
104
+ return st.builds(annotation, **fields)
105
+ raise TypeError(f"no strategy for {annotation!r}")
106
+
107
+
108
+ class PropertyFailure(AssertionError):
109
+ pass
110
+
111
+
112
+ def check_model(fn, max_examples=200):
113
+ """Runs the function against its specifications on generated arguments; raises on a counterexample."""
114
+ from hypothesis import given, settings
115
+ from hypothesis import strategies as st
116
+
117
+ spec = fn.__lean_model__
118
+ hints = typing.get_type_hints(fn, include_extras=True)
119
+ params = list(inspect.signature(fn).parameters)
120
+ strategies = st.tuples(*(strategy(hints[p]) for p in params))
121
+
122
+ @settings(max_examples=max_examples, deadline=None, database=None)
123
+ @given(strategies)
124
+ def run(args):
125
+ from hypothesis import assume
126
+
127
+ if spec.requires is not None:
128
+ assume(spec.requires(*args))
129
+ try:
130
+ result = fn(*args)
131
+ except Exception as e: # noqa: BLE001
132
+ raise PropertyFailure(f"{fn.__qualname__}{args} raised {e!r}") from None
133
+ if spec.refines is not None:
134
+ expected = spec.refines(*args)
135
+ if result != expected:
136
+ raise PropertyFailure(f"{fn.__qualname__}{args} = {result!r}, the spec gives {expected!r}")
137
+ for label, e in spec.ensures.items():
138
+ if not e(*args, result):
139
+ raise PropertyFailure(f"{fn.__qualname__}{args} = {result!r} breaks {label}")
140
+
141
+ run()
142
+
143
+
144
+ def check_module(module, max_examples=200):
145
+ """``check_model`` for every ``@model`` function of the module."""
146
+ for spec in models_of(module):
147
+ check_model(spec.fn, max_examples)