Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ jobs:
run: |
uv run python -m py_compile test/slow/generated_basic/*.py
uv run python -m py_compile test/slow/generated_full/*.py

- name: import check generated code
run: uv run pytest test/slow/run_test.py --tb=line

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ build-test-slow:
test-slow: build-test-slow
$(PYTHON) -m py_compile test/slow/generated_basic/*.py
$(PYTHON) -m py_compile test/slow/generated_full/*.py
$(PYTEST) test/slow/run_test.py

build-test-quick:
rm -rf test/quick/python_out
Expand Down
3 changes: 0 additions & 3 deletions Python.thy
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ setup \<open>fn thy =>

declare [[default_code_width = 2000]]

code_identifier
code_module Code_Numeral \<rightharpoonup> (Python) Arith

(* Panic, by a helper function, since Code.abort occurs in expression position *)
code_printing
constant Code.abort \<rightharpoonup> (Python) "'_abort"
Expand Down
4 changes: 0 additions & 4 deletions Python_Setup.thy
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ theory Python_Setup
"HOL-Library.Code_Abstract_Char"
begin

code_identifier
code_module Code_Target_Nat \<rightharpoonup> (Python) Arith
| code_module Code_Target_Int \<rightharpoonup> (Python) Arith

(* Integers *)
(* Isabelle defines a div 0 = 0 and a mod 0 = a, while Python raises ZeroDivisionError;
the guarded lambdas evaluate each operand once (here and for nat, integer, natural). *)
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ one in `.python-version`. Commit the updated list with the version change.
`isabelle build` rebuilds the `Python` session, and every session built on it,
whenever the list changes.

### Values and modules

A value is printed as a cached function without arguments, and its uses call
it, so it is evaluated once, when it is first used:

```python
@functools.cache
def num_of_nat():
return (Fun.comp(Code_Numeral.num_of_integer, lambda a: a))
```

Code that uses the generated modules therefore calls a value too
(`Example_nat.nat_sum()`). An alias of a function (`f_int = f`) is printed as an
ordinary function instead.

Importing a module thus evaluates nothing, which matters for two reasons. Each
theory becomes a Python module (unless `code_identifier` or the `module_name` of
`export_code` says otherwise), and code equations may use constants of later
theories, e.g. `Num.num_of_nat` is implemented by `Code_Numeral.num_of_integer`.
So the modules can import each other: in the HOL library, about 50 modules form
one import cycle. Python allows this as long as no module reads an attribute of
a module that is still being imported, which evaluating a value could do. And a
value that calls a constant without code (`_abort`) raises when it is used,
rather than when its module is imported.

### Tail calls as loops

Python does not eliminate tail calls, so a function that recurses once per
Expand Down Expand Up @@ -123,8 +148,13 @@ with `Python.Python_Setup`. Unfortunately, we can currently not use Isabelle's
isabelle build -d . -e Python_Test_Slow
uv run python -m py_compile test/slow/generated_basic/*.py
uv run python -m py_compile test/slow/generated_full/*.py
uv run pytest test/slow/run_test.py
```

`run_test.py` imports every generated module in a fresh interpreter, and checks
that every cached function takes no arguments; the modules that are known not to
import are listed in `KNOWN_FAILURES` there.

- **diff**

During the development of this code generator, different Isabelle features were
Expand Down
82 changes: 58 additions & 24 deletions code_python.ML
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ fun invent_names vars prefix n =
* reserved: context of names that are not useable as new names
* args_num: how many arguments needed by the current stmt for saturation
* is_class_param: true if the current symbol is a type-class method
* is_value: true for a value definition (a nullary constant without dictionaries)
* is_cached_value: true for a value, which is printed as a cached function without
* arguments (see serialize_python)
* is_constr: true for a datatype constructor without a printing
* ctxt: thoery context (neede to look up record field names)
* deresolve: module relative naming resolver
*)
fun print_python_stmt undefineds const_syntax reserved args_num
is_class_param is_value is_constr ctxt deresolve =
is_class_param is_value is_cached_value is_constr ctxt deresolve =
let
fun deresolve_const name = deresolve (Constant name);
fun deresolve_class name = deresolve (Type_Class name);
Expand Down Expand Up @@ -529,8 +532,14 @@ fun print_python_stmt undefineds const_syntax reserved args_num
SOME app => print_app const_syntax some_thm vars fxy app
(* If not, then this might be a lambda application or similar *)
| NONE =>
applify "(" ")" (print_python_expr const_syntax some_thm vars NOBR) fxy
(print_python_expr const_syntax some_thm vars BR t1) [t2])
let
(* an applied lambda needs brackets, or it would apply its body:
`lambda x: t(u)` instead of `(lambda x: t)(u)` *)
val head = case t1 of
_ `|=> _ => Pretty.enclose "(" ")"
[print_python_expr const_syntax some_thm vars NOBR t1]
| _ => print_python_expr const_syntax some_thm vars BR t1
in applify "(" ")" (print_python_expr const_syntax some_thm vars NOBR) fxy head [t2] end)
| IVar (SOME v) => Pretty.str (lookup_var vars v)
| IVar NONE => error "can't return a variable with no name"
| (params, _) `|=> (t', _) =>
Expand Down Expand Up @@ -574,6 +583,8 @@ fun print_python_stmt undefineds const_syntax reserved args_num
and print_app_expr const_syntax some_thm vars (app as ({ sym, dictss, ...}, ts)) =
let
val l = args_num sym
(* a value is referenced by calling its cached function *)
val name_pretty = Pretty.str (deresolve sym ^ (if is_cached_value sym then "()" else ""))
fun call_with head_pretty extra_dicts =
let
val (immediate, curried) = chop l ts
Expand All @@ -591,7 +602,7 @@ fun print_python_stmt undefineds const_syntax reserved args_num
one argument. A constant of higher arity is eta-expanded, since the corresponding Python
function is uncurried but function values are applied one argument at a time. *)
if null ts andalso forall null dictss andalso l = 1
then [(Pretty.str o deresolve) sym]
then [name_pretty]
else [print_python_expr const_syntax some_thm vars BR
(Code_Thingol.saturated_application l app)]
else if is_class_param sym andalso not (null dictss)
Expand All @@ -603,14 +614,14 @@ fun print_python_stmt undefineds const_syntax reserved args_num
Pretty.str (attr_name sym)]
in call_with head_pretty extra_dict_pretty end
else if is_value sym andalso forall null dictss andalso null ts
then [(Pretty.str o deresolve) sym]
then [name_pretty]
else
let
(* constructors take no dictionaries (as in SML), even when the type has sort
constraints, e.g. the abstype morphism vec_lambda of ('a, 'b::finite) vec *)
val dict_pretty = if is_constr sym then []
else map_filter (print_dicts some_thm vars BR) dictss
in call_with ((Pretty.str o deresolve) sym) dict_pretty end
in call_with name_pretty dict_pretty end
end

and print_app const_syntax some_thm vars fxy (const as { sym, ... }, ts) =
Expand Down Expand Up @@ -763,9 +774,12 @@ fun print_python_stmt undefineds const_syntax reserved args_num
in { patterns = map (IVar o fst) vs_tys, rhs = body, thm = thm } end
(* The same holds for any pattern-less equation of a function that takes
dictionaries itself, e.g. `to_nat = nat o Rep` in a class context: it is
printed as `def to_nat(a_dict)`, but callers pass the arguments too. *)
printed as `def to_nat(a_dict)`, but callers pass the arguments too; and
for one of arity 2 or more, e.g. `empty = (%_ _. None)`: as a value it would
be a curried lambda, but callers pass all arguments at once. At arity 1 both
agree, so such an equation stays a value. *)
| eta_expand_alias takes_dicts n (eq as { patterns = [], rhs, thm }) =
if n = 0 orelse not takes_dicts then eq
if n = 0 orelse (not takes_dicts andalso n < 2) then eq
else
let
val names = Name.invent
Expand Down Expand Up @@ -812,9 +826,6 @@ fun print_python_stmt undefineds const_syntax reserved args_num
else map SOME (aux_params vars1 (map #patterns eqs))
val vars2 = intro_vars (map_filter I params) vars1
val dict_params = print_dict_args vs
(* a simple function without parameters and dictionary arguments can
be printed as a plain value (i.e., an assignment x = ...) *)
val prints_as_value = simple andalso null params andalso null dict_params
val head = print_func_head const vars2 dict_params params
fun print_return some_thm vars' t' = Pretty.block [Pretty.str "return (",
print_python_expr const_syntax some_thm vars' NOBR t', Pretty.str ")"]
Expand Down Expand Up @@ -875,16 +886,16 @@ fun print_python_stmt undefineds const_syntax reserved args_num
then Pretty.chunks [Pretty.str "while True:", indent_block 4 body]
else body
in
if prints_as_value
then
(* value definition: `const = (expr)` *)
let val { rhs = t, thm = some_thm, ... } = hd eqs
in print_binding some_thm vars2 (deresolve_const const, t) end
else if simple
if simple
then
(* simple function, plain variable args *)
let val { rhs = t, thm = some_thm, ... } = hd eqs
in Pretty.chunks [head, indent_block 4 (loop_body (print_rhs some_thm vars2 t))] end
(* simple function, plain variable args; a value is a function without
arguments that is evaluated once, when it is first used *)
let
val { rhs = t, thm = some_thm, ... } = hd eqs
val decorator = if is_cached_value (Constant const)
then [Pretty.str "@functools.cache"] else []
in Pretty.chunks (decorator @
[head, indent_block 4 (loop_body (print_rhs some_thm vars2 t))]) end
else
(* multiple equations and/or patterns in arguments, this compiles
to a match/case over the arguments *)
Expand Down Expand Up @@ -1014,14 +1025,36 @@ fun serialize_python undefineds _ ctxt { module_name, reserved_syms,
| _ => false)
| is_class_param _ = raise Fail "should never be reached";

fun is_value sym =
fun is_value sym =
Code_Symbol.Graph.defined program sym andalso
case sym of
Constant _ => (
case Code_Symbol.Graph.get_node program sym of
Code_Thingol.Fun (((vs, _), [((ts, _), _)]), _) =>
forall (null o snd) vs andalso null ts
| _ => false)
| _ => false;

(* A value that print_python_func would print as an assignment `v = ...`:
eta_expand_alias turns an alias of a function (`f_int = f`) and a value of arity
2 or more into a function.
Every such value is printed as a cached function without arguments, which is
called where it is used, so importing a module evaluates nothing. Modules follow
theories, and code equations may use constants of later theories (e.g.
Num.num_of_nat is implemented by Code_Numeral.num_of_integer), so modules can
import each other, and reading an attribute of a module that is still being
imported would fail. And a value that calls a constant without code only raises
when it is used. *)
fun is_cached_value (sym as Constant name) = is_value sym andalso
not (member (op =) undefineds name) andalso
(case Code_Symbol.Graph.get_node program sym of
Code_Thingol.Fun (((_, ty), [((_, rhs), _)]), _) =>
(case length (fst (Code_Thingol.unfold_fun ty)) of
0 => true
| 1 => (case rhs of IConst _ => false | _ => true)
| _ => false)
| _ => false)
| is_cached_value _ = false;

fun classparams_of_class class =
case Code_Symbol.Graph.get_node program (Type_Class class) of
Expand Down Expand Up @@ -1084,20 +1117,21 @@ fun serialize_python undefineds _ ctxt { module_name, reserved_syms,
of (_, NONE) => NONE
| (_, SOME (export, stmt)) =>
SOME (print_python_stmt undefineds const_syntax
(make_vars reserved_syms) args_num is_class_param is_value
(make_vars reserved_syms) args_num is_class_param is_value is_cached_value
is_constr ctxt (deresolver mod_name)
(sym, (export, python_stmt_of_thingol stmt))));
in (mod_name, imports, stmt_ps) end)
|> filter (fn (_, _, stmt_ps) => not (null stmt_ps));

val nonempty_names = map #1 module_stmts;

(* modules that print a cached (dictionary-free) instance need functools *)
(* modules that print a cached (dictionary-free) instance or a value need functools *)
fun needs_functools mod_name =
let val (stmt_gr, _) = Graph.get_node python_program mod_name
in Code_Symbol.Graph.fold (fn (_, ((_, stmt), _)) => fn b => b orelse
in Code_Symbol.Graph.fold (fn (sym, ((_, stmt), _)) => fn b => b orelse
(case stmt of
SOME (_, Code_Thingol.Classinst { vs, ... }) => forall (null o snd) vs
| SOME _ => is_cached_value sym
| _ => false)) stmt_gr false
end;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def super_op_bool():
def sub_op_bool():
return Example_classrel_base.sub_op(super_op_sub_op = super_op_bool())

test_sub = (Example_classrel_base.sub_apply(sub_op_bool(), True))
@functools.cache
def test_sub():
return (Example_classrel_base.sub_apply(sub_op_bool(), True))

test_super = (sup_apply_bool(True))
@functools.cache
def test_super():
return (sup_apply_bool(True))
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass
from typing import Any, Callable
import functools
import Num

def _abort(msg, thunk):
Expand All @@ -10,15 +11,25 @@ def _abort(msg, thunk):
def cn_add(x, y):
return ((x + y))

cn_neg = (-5)
@functools.cache
def cn_neg():
return (-5)

cn_int_lit = (42)
@functools.cache
def cn_int_lit():
return (42)

cn_sum = (cn_add(cn_int_lit, 8))
@functools.cache
def cn_sum():
return (cn_add(cn_int_lit(), 8))

def cn_nat_add(x, y):
return ((x + y))

cn_nat_lit = (max(0, 7))
@functools.cache
def cn_nat_lit():
return (max(0, 7))

cn_nat_sum = (cn_nat_add(cn_nat_lit, (max(0, 3))))
@functools.cache
def cn_nat_sum():
return (cn_nat_add(cn_nat_lit(), (max(0, 3))))
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass
from typing import Any, Callable
import functools
import List
import Product_Type

Expand All @@ -22,9 +23,13 @@ def any_true(x0):
case _:
raise RuntimeError("match failed")

test_swap = (swap(Product_Type.Pair(True, False)))
@functools.cache
def test_swap():
return (swap(Product_Type.Pair(True, False)))

test_any_true = (any_true(List.Cons(False, List.Cons(False, List.Cons(True, List.Nil())))))
@functools.cache
def test_any_true():
return (any_true(List.Cons(False, List.Cons(False, List.Cons(True, List.Nil())))))

def first_two_agree(x0):
match x0:
Expand All @@ -37,4 +42,6 @@ def first_two_agree(x0):
case _:
raise RuntimeError("match failed")

test_first_two = (Product_Type.Pair(first_two_agree(List.Cons(True, List.Cons(True, List.Cons(False, List.Nil())))), first_two_agree(List.Cons(True, List.Nil()))))
@functools.cache
def test_first_two():
return (Product_Type.Pair(first_two_agree(List.Cons(True, List.Cons(True, List.Cons(False, List.Nil())))), first_two_agree(List.Cons(True, List.Nil()))))
13 changes: 10 additions & 3 deletions test/diff/golden/Python_Examples.Example_iarray/Example_iarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass
from typing import Any, Callable
import functools
import IArray
import Num
import Nat
Expand All @@ -12,14 +13,20 @@ def _abort(msg, thunk):
def sum_ends(a):
return (((a[0]) + (a[(max(0, (len(a)) - 1))])))

squares = (tuple(map(lambda i: (i * i), range((max(0, 4))))))
@functools.cache
def squares():
return (tuple(map(lambda i: (i * i), range((max(0, 4))))))

example = (((list(squares)), ((sum_ends(squares), (((all(map(lambda x: (x < (max(0, 10))), squares))), IArray.equal_iarray(Nat.equal_nat(), (tuple((list(map(lambda a: (a + 1), [1, max(0, 2)]))))), (tuple([max(0, 2), max(0, 3)])))))))))
@functools.cache
def example():
return (((list(squares())), ((sum_ends(squares()), (((all(map(lambda x: (x < (max(0, 10))), squares()))), IArray.equal_iarray(Nat.equal_nat(), (tuple((list(map(lambda a: (a + 1), [1, max(0, 2)]))))), (tuple([max(0, 2), max(0, 3)])))))))))

def sub_pair(p):
return ((lambda a, i: a[i])(*p))

example_nth = ([10, 20, 30][1])
@functools.cache
def example_nth():
return ([10, 20, 30][1])

def first_or_zero(x0):
match x0:
Expand Down
Loading
Loading