From c4706c45a2488c00055277c7cbcd7f0849e9e9dd Mon Sep 17 00:00:00 2001 From: Nikolaus Huber Date: Wed, 4 Nov 2026 09:52:29 +0000 Subject: [PATCH 1/2] make all values lazy --- .github/workflows/ci.yml | 3 + Makefile | 1 + README.md | 30 ++++ code_python.ML | 99 +++++++++----- .../Example_classrel.py | 8 +- .../Example_code_numerals.py | 21 ++- .../Example_generic_list_pair.py | 13 +- .../Example_iarray.py | 13 +- .../Example_int_arith.py | 37 +++-- .../Example_integer_bit_ops.py | 37 +++-- .../Example_lists.py | 9 +- .../Example_nat.py | 45 ++++-- .../Example_nat_edge_cases.py | 13 +- .../Example_nat_pattern.py | 13 +- .../Example_nested_case.py | 37 +++-- .../Example_nested_if.py | 33 +++-- .../Example_no_eqs.py | 5 +- .../Example_options.py | 25 +++- .../Example_partial.py | 17 ++- .../Example_polymorphic_const.py | 17 ++- .../Example_record_ext.py | 13 +- .../Example_records.py | 13 +- .../Example_short_circuit.py | 29 +++- .../Example_super.py | 4 +- .../Example_super_chain.py | 8 +- .../Example_transparent_wrapper.py | 9 +- .../Example_tuples.py | 21 ++- .../Example_tyclass.py | 8 +- test/oracle/Oracle_Import_Cycle.thy | 6 +- test/oracle/run_test.py | 13 +- test/quick/Quick_Test.thy | 9 ++ test/quick/Quick_Test_Tail_Calls.thy | 4 +- test/quick/run_test.py | 128 ++++++++++-------- test/slow/run_test.py | 71 ++++++++++ 34 files changed, 604 insertions(+), 208 deletions(-) create mode 100644 test/slow/run_test.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df12e92..2681565 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Makefile b/Makefile index 2142e5a..b43f917 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,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: $(ISABELLE) build -d . -e Python_Test_Quick diff --git a/README.md b/README.md index ca80246..6a1be51 100644 --- a/README.md +++ b/README.md @@ -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(Arith.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 @@ -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 diff --git a/code_python.ML b/code_python.ML index 7730612..f1b4d46 100644 --- a/code_python.ML +++ b/code_python.ML @@ -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); @@ -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', _) => @@ -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 @@ -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) @@ -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) = @@ -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 @@ -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 ")"] @@ -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 *) @@ -933,15 +944,18 @@ fun print_python_stmt undefineds const_syntax reserved args_num fun python_program_of_program ctxt module_name reserved identifiers exports program = let - (* Add trailing underscores to a name until it is unused and not a module name, following PEP 8 *) - fun namify_common module_names base ((nsp_class, nsp_object), nsp_common) = + (* Add trailing underscores to a name until it is not reserved, unused and not a module name, + following PEP 8. The names in use are kept apart from the Name.context of reserved names: + Name.declare drops a trailing underscore, so after declaring `set_` the name `set_` would + still count as free, and a datatype and its constructor could both become `set_`. *) + fun namify_common module_names base (reserved, used) = let fun fresh x = - if Name.is_declared nsp_common x orelse member (op =) module_names x then fresh (x ^ "_") else x; + if Name.is_declared reserved x orelse Symtab.defined used x + orelse member (op =) module_names x + then fresh (x ^ "_") else x; val base' = fresh base; - in - (base', ((Name.declare base' nsp_class, Name.declare base' nsp_object), Name.declare base' nsp_common)) - end; + in (base', (reserved, Symtab.insert_set base' used)) end; (* Python doesn't really have any naming restrictions relating to upper/lowercase etc. *) fun namify_stmt _ (Code_Thingol.NoStmt) = raise Fail "should not be reached" @@ -961,7 +975,7 @@ fun python_program_of_program ctxt module_name reserved identifiers exports prog fun flat_program module_names = Code_Namespace.flat_program ctxt { module_prefix = "", module_name = module_name, reserved = reserved, - identifiers = identifiers, empty_nsp = ((reserved, reserved), reserved), + identifiers = identifiers, empty_nsp = (reserved, Symtab.empty), namify_stmt = namify_stmt module_names, modify_stmt = modify_stmt } exports program; @@ -1011,7 +1025,8 @@ 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 @@ -1019,6 +1034,27 @@ fun serialize_python undefineds _ ctxt { module_name, reserved_syms, 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 @@ -1081,7 +1117,7 @@ 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) @@ -1089,12 +1125,13 @@ fun serialize_python undefineds _ ctxt { module_name, reserved_syms, 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; diff --git a/test/diff/golden/Python_Examples.Example_classrel/Example_classrel.py b/test/diff/golden/Python_Examples.Example_classrel/Example_classrel.py index 65054ca..6e77aa4 100644 --- a/test/diff/golden/Python_Examples.Example_classrel/Example_classrel.py +++ b/test/diff/golden/Python_Examples.Example_classrel/Example_classrel.py @@ -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)) diff --git a/test/diff/golden/Python_Examples.Example_code_numerals/Example_code_numerals.py b/test/diff/golden/Python_Examples.Example_code_numerals/Example_code_numerals.py index 92f6f83..128afb3 100644 --- a/test/diff/golden/Python_Examples.Example_code_numerals/Example_code_numerals.py +++ b/test/diff/golden/Python_Examples.Example_code_numerals/Example_code_numerals.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -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)))) diff --git a/test/diff/golden/Python_Examples.Example_generic_list_pair/Example_generic_list_pair.py b/test/diff/golden/Python_Examples.Example_generic_list_pair/Example_generic_list_pair.py index 35eaab6..ec02e31 100644 --- a/test/diff/golden/Python_Examples.Example_generic_list_pair/Example_generic_list_pair.py +++ b/test/diff/golden/Python_Examples.Example_generic_list_pair/Example_generic_list_pair.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import List import Product_Type @@ -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: @@ -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())))) diff --git a/test/diff/golden/Python_Examples.Example_iarray/Example_iarray.py b/test/diff/golden/Python_Examples.Example_iarray/Example_iarray.py index f74a5bb..1f1fea8 100644 --- a/test/diff/golden/Python_Examples.Example_iarray/Example_iarray.py +++ b/test/diff/golden/Python_Examples.Example_iarray/Example_iarray.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import IArray import Num import Nat @@ -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: diff --git a/test/diff/golden/Python_Examples.Example_int_arith/Example_int_arith.py b/test/diff/golden/Python_Examples.Example_int_arith/Example_int_arith.py index 9352d20..a1a8cb8 100644 --- a/test/diff/golden/Python_Examples.Example_int_arith/Example_int_arith.py +++ b/test/diff/golden/Python_Examples.Example_int_arith/Example_int_arith.py @@ -2,28 +2,47 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): raise RuntimeError(msg) -product = ((6 * 7)) +@functools.cache +def product(): + return ((6 * 7)) def checksum(x, y): return ((((lambda a, b: a % b if b else a)(((x * y)), ((y - x)))) + ((lambda a, b: a // b if b else 0)(x, 2)))) -zero_test = (0) +@functools.cache +def zero_test(): + return (0) -neg_product = ((((- 3)) * 4)) +@functools.cache +def neg_product(): + return ((((- 3)) * 4)) -quotient_neg = ((lambda a, b: a // b if b else 0)(((- 7)), 2)) +@functools.cache +def quotient_neg(): + return ((lambda a, b: a // b if b else 0)(((- 7)), 2)) -quotient_pos = ((lambda a, b: a // b if b else 0)(7, 2)) +@functools.cache +def quotient_pos(): + return ((lambda a, b: a // b if b else 0)(7, 2)) -remainder_neg = ((lambda a, b: a % b if b else a)(((- 7)), 2)) +@functools.cache +def remainder_neg(): + return ((lambda a, b: a % b if b else a)(((- 7)), 2)) -remainder_pos = ((lambda a, b: a % b if b else a)(7, 2)) +@functools.cache +def remainder_pos(): + return ((lambda a, b: a % b if b else a)(7, 2)) -quotient_neg_divisor = ((lambda a, b: a // b if b else 0)(7, ((- 2)))) +@functools.cache +def quotient_neg_divisor(): + return ((lambda a, b: a // b if b else 0)(7, ((- 2)))) -remainder_neg_divisor = ((lambda a, b: a % b if b else a)(7, ((- 2)))) +@functools.cache +def remainder_neg_divisor(): + return ((lambda a, b: a % b if b else a)(7, ((- 2)))) diff --git a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py index dcbc1b5..d9dbe22 100644 --- a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py +++ b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py @@ -2,26 +2,45 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Arith import Num def _abort(msg, thunk): raise RuntimeError(msg) -test_bit = (Arith.bit_integer(10, 1)) +@functools.cache +def test_bit(): + return (Arith.bit_integer(10, 1)) -test_sgn = (Arith.sgn_integer(-42)) +@functools.cache +def test_sgn(): + return (Arith.sgn_integer(-42)) -test_mask = (Arith.mask_integer((max(0, 5)))) +@functools.cache +def test_mask(): + return (Arith.mask_integer((max(0, 5)))) -test_set_bit = (Arith.set_bit_integer((max(0, 2)), 0)) +@functools.cache +def test_set_bit(): + return (Arith.set_bit_integer((max(0, 2)), 0)) -test_drop_bit = (Arith.drop_bit_integer((max(0, 2)), 20)) +@functools.cache +def test_drop_bit(): + return (Arith.drop_bit_integer((max(0, 2)), 20)) -test_flip_bit = (Arith.flip_bit_integer(1, 5)) +@functools.cache +def test_flip_bit(): + return (Arith.flip_bit_integer(1, 5)) -test_push_bit = (Arith.push_bit_integer((max(0, 3)), 5)) +@functools.cache +def test_push_bit(): + return (Arith.push_bit_integer((max(0, 3)), 5)) -test_take_bit = (Arith.take_bit_integer((max(0, 4)), 255)) +@functools.cache +def test_take_bit(): + return (Arith.take_bit_integer((max(0, 4)), 255)) -test_unset_bit = (Arith.unset_bit_integer(0, 7)) +@functools.cache +def test_unset_bit(): + return (Arith.unset_bit_integer(0, 7)) diff --git a/test/diff/golden/Python_Examples.Example_lists/Example_lists.py b/test/diff/golden/Python_Examples.Example_lists/Example_lists.py index 8baf75f..90c903a 100644 --- a/test/diff/golden/Python_Examples.Example_lists/Example_lists.py +++ b/test/diff/golden/Python_Examples.Example_lists/Example_lists.py @@ -2,14 +2,19 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): raise RuntimeError(msg) -example = ([1, 2, 3]) +@functools.cache +def example(): + return ([1, 2, 3]) def prepend(x, xs): return ([x] + xs) -example2 = (prepend(5, example)) +@functools.cache +def example2(): + return (prepend(5, example())) diff --git a/test/diff/golden/Python_Examples.Example_nat/Example_nat.py b/test/diff/golden/Python_Examples.Example_nat/Example_nat.py index e124381..c032b0e 100644 --- a/test/diff/golden/Python_Examples.Example_nat/Example_nat.py +++ b/test/diff/golden/Python_Examples.Example_nat/Example_nat.py @@ -2,32 +2,55 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): raise RuntimeError(msg) -nat_sum = (((max(0, 6)) + (max(0, 7)))) +@functools.cache +def nat_sum(): + return (((max(0, 6)) + (max(0, 7)))) -nat_eq_test = (((max(0, 4)) == (max(0, 4)))) +@functools.cache +def nat_eq_test(): + return (((max(0, 4)) == (max(0, 4)))) -nat_le_test = (((max(0, 3)) <= (max(0, 5)))) +@functools.cache +def nat_le_test(): + return (((max(0, 3)) <= (max(0, 5)))) -nat_literal = (max(0, 42)) +@functools.cache +def nat_literal(): + return (max(0, 42)) -nat_lt_test = (((max(0, 5)) < (max(0, 3)))) +@functools.cache +def nat_lt_test(): + return (((max(0, 5)) < (max(0, 3)))) -nat_product = (((max(0, 6)) * (max(0, 7)))) +@functools.cache +def nat_product(): + return (((max(0, 6)) * (max(0, 7)))) -nat_sub_pos = (max(0, (max(0, 10)) - (max(0, 3)))) +@functools.cache +def nat_sub_pos(): + return (max(0, (max(0, 10)) - (max(0, 3)))) def nat_checksum(x, y): return ((((lambda a, b: a % b if b else a)(((x * y)), ((y + 1)))) + (max(0, x - y)))) -nat_quotient = ((lambda a, b: a // b if b else 0)((max(0, 7)), (max(0, 2)))) +@functools.cache +def nat_quotient(): + return ((lambda a, b: a // b if b else 0)((max(0, 7)), (max(0, 2)))) -nat_remainder = ((lambda a, b: a % b if b else a)((max(0, 7)), (max(0, 2)))) +@functools.cache +def nat_remainder(): + return ((lambda a, b: a % b if b else a)((max(0, 7)), (max(0, 2)))) -nat_sub_trunc = (max(0, (max(0, 3)) - (max(0, 10)))) +@functools.cache +def nat_sub_trunc(): + return (max(0, (max(0, 3)) - (max(0, 10)))) -nat_zero_test = (0) +@functools.cache +def nat_zero_test(): + return (0) diff --git a/test/diff/golden/Python_Examples.Example_nat_edge_cases/Example_nat_edge_cases.py b/test/diff/golden/Python_Examples.Example_nat_edge_cases/Example_nat_edge_cases.py index 2084b17..b00a07e 100644 --- a/test/diff/golden/Python_Examples.Example_nat_edge_cases/Example_nat_edge_cases.py +++ b/test/diff/golden/Python_Examples.Example_nat_edge_cases/Example_nat_edge_cases.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -19,7 +20,9 @@ def peel_positive_head(x0): case _: raise RuntimeError("match failed") -peel_test = (peel_positive_head([max(0, 3), max(0, 4), max(0, 5)])) +@functools.cache +def peel_test(): + return (peel_positive_head([max(0, 3), max(0, 4), max(0, 5)])) def nat_minus2(v, n): if ((v == 0)): @@ -39,6 +42,10 @@ def classify_small(uu): else: return (max(0, 2)) -classify_test = ((classify_small(0), ((classify_small(1), classify_small((max(0, 7))))))) +@functools.cache +def classify_test(): + return ((classify_small(0), ((classify_small(1), classify_small((max(0, 7))))))) -nat_minus2_test = (nat_minus2((max(0, 5)), (max(0, 3)))) +@functools.cache +def nat_minus2_test(): + return (nat_minus2((max(0, 5)), (max(0, 3)))) diff --git a/test/diff/golden/Python_Examples.Example_nat_pattern/Example_nat_pattern.py b/test/diff/golden/Python_Examples.Example_nat_pattern/Example_nat_pattern.py index cf7f52b..8a0efe8 100644 --- a/test/diff/golden/Python_Examples.Example_nat_pattern/Example_nat_pattern.py +++ b/test/diff/golden/Python_Examples.Example_nat_pattern/Example_nat_pattern.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -13,7 +14,9 @@ def fact(n): else: return (((((max(0, n - 1)) + 1)) * fact((max(0, n - 1))))) -fact_5 = (fact((max(0, 5)))) +@functools.cache +def fact_5(): + return (fact((max(0, 5)))) def countdown(n): if ((n == 0)): @@ -27,6 +30,10 @@ def nat_is_even(n): else: return (not nat_is_even((max(0, n - 1)))) -is_even_7 = (nat_is_even((max(0, 7)))) +@functools.cache +def is_even_7(): + return (nat_is_even((max(0, 7)))) -countdown_4 = (countdown((max(0, 4)))) +@functools.cache +def countdown_4(): + return (countdown((max(0, 4)))) diff --git a/test/diff/golden/Python_Examples.Example_nested_case/Example_nested_case.py b/test/diff/golden/Python_Examples.Example_nested_case/Example_nested_case.py index a0e9035..1e8c075 100644 --- a/test/diff/golden/Python_Examples.Example_nested_case/Example_nested_case.py +++ b/test/diff/golden/Python_Examples.Example_nested_case/Example_nested_case.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -37,13 +38,21 @@ def abs_manual(x): else: return (x) -label_five = (nat_label((max(0, 5)))) +@functools.cache +def label_five(): + return (nat_label((max(0, 5)))) -label_zero = (nat_label(0)) +@functools.cache +def label_zero(): + return (nat_label(0)) -weird_five = (weird((max(0, 5)))) +@functools.cache +def weird_five(): + return (weird((max(0, 5)))) -weird_zero = (weird(0)) +@functools.cache +def weird_zero(): + return (weird(0)) def describe_len(xs): target = (xs) @@ -56,12 +65,22 @@ def describe_len(xs): raise RuntimeError("match failed") return ((x + 1)) -classify_zero = (classify(0)) +@functools.cache +def classify_zero(): + return (classify(0)) -abs_manual_neg = (abs_manual(((- 7)))) +@functools.cache +def abs_manual_neg(): + return (abs_manual(((- 7)))) -abs_manual_pos = (abs_manual(7)) +@functools.cache +def abs_manual_pos(): + return (abs_manual(7)) -classify_other = (classify((max(0, 5)))) +@functools.cache +def classify_other(): + return (classify((max(0, 5)))) -describe_len_123 = (describe_len([1, max(0, 2), max(0, 3)])) +@functools.cache +def describe_len_123(): + return (describe_len([1, max(0, 2), max(0, 3)])) diff --git a/test/diff/golden/Python_Examples.Example_nested_if/Example_nested_if.py b/test/diff/golden/Python_Examples.Example_nested_if/Example_nested_if.py index 9e11078..8e0b16d 100644 --- a/test/diff/golden/Python_Examples.Example_nested_if/Example_nested_if.py +++ b/test/diff/golden/Python_Examples.Example_nested_if/Example_nested_if.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -22,15 +23,25 @@ def grade_code(score): else: return (max(0, 4)) -grade_a = (grade_code((max(0, 95)))) +@functools.cache +def grade_a(): + return (grade_code((max(0, 95)))) -grade_b = (grade_code((max(0, 85)))) +@functools.cache +def grade_b(): + return (grade_code((max(0, 85)))) -grade_c = (grade_code((max(0, 75)))) +@functools.cache +def grade_c(): + return (grade_code((max(0, 75)))) -grade_d = (grade_code((max(0, 65)))) +@functools.cache +def grade_d(): + return (grade_code((max(0, 65)))) -grade_f = (grade_code((max(0, 40)))) +@functools.cache +def grade_f(): + return (grade_code((max(0, 40)))) def nested_then(x, y): if ((0 < x)): @@ -41,8 +52,14 @@ def nested_then(x, y): else: return (3) -nested_then_f = (nested_then(((- 1)), 1)) +@functools.cache +def nested_then_f(): + return (nested_then(((- 1)), 1)) -nested_then_tf = (nested_then(1, ((- 1)))) +@functools.cache +def nested_then_tf(): + return (nested_then(1, ((- 1)))) -nested_then_tt = (nested_then(1, 1)) +@functools.cache +def nested_then_tt(): + return (nested_then(1, 1)) diff --git a/test/diff/golden/Python_Examples.Example_no_eqs/Example_no_eqs.py b/test/diff/golden/Python_Examples.Example_no_eqs/Example_no_eqs.py index c30f789..ebaeeaa 100644 --- a/test/diff/golden/Python_Examples.Example_no_eqs/Example_no_eqs.py +++ b/test/diff/golden/Python_Examples.Example_no_eqs/Example_no_eqs.py @@ -2,9 +2,12 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import HOL def _abort(msg, thunk): raise RuntimeError(msg) -uses_undefined = (HOL.undefined()) +@functools.cache +def uses_undefined(): + return (HOL.undefined()) diff --git a/test/diff/golden/Python_Examples.Example_options/Example_options.py b/test/diff/golden/Python_Examples.Example_options/Example_options.py index d6031c7..68fa650 100644 --- a/test/diff/golden/Python_Examples.Example_options/Example_options.py +++ b/test/diff/golden/Python_Examples.Example_options/Example_options.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num import Option @@ -17,13 +18,21 @@ def describe_opt(x0): case _: raise RuntimeError("match failed") -o_some = (Option.Some(5)) +@functools.cache +def o_some(): + return (Option.Some(5)) -d1 = (describe_opt(o_some)) +@functools.cache +def d1(): + return (describe_opt(o_some())) -o_none = (Option.None_()) +@functools.cache +def o_none(): + return (Option.None_()) -d2 = (describe_opt(o_none)) +@functools.cache +def d2(): + return (describe_opt(o_none())) def describe_opt2(x0): match x0: @@ -34,6 +43,10 @@ def describe_opt2(x0): case _: raise RuntimeError("match failed") -d3 = (describe_opt2(o_some)) +@functools.cache +def d3(): + return (describe_opt2(o_some())) -d4 = (describe_opt2(o_none)) +@functools.cache +def d4(): + return (describe_opt2(o_none())) diff --git a/test/diff/golden/Python_Examples.Example_partial/Example_partial.py b/test/diff/golden/Python_Examples.Example_partial/Example_partial.py index a1d7803..b2519df 100644 --- a/test/diff/golden/Python_Examples.Example_partial/Example_partial.py +++ b/test/diff/golden/Python_Examples.Example_partial/Example_partial.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -10,16 +11,24 @@ def _abort(msg, thunk): def add2(x, y): return ((x + y)) -add_one = (lambda a: add2(1, a)) +@functools.cache +def add_one(): + return (lambda a: add2(1, a)) def add2_ref(a, b): return (add2(a, b)) -p_mapped = (list(map(add_one, [1, 2, 3]))) +@functools.cache +def p_mapped(): + return (list(map(add_one(), [1, 2, 3]))) def apply_twice(f, x): return (f(f(x))) -p_result = (apply_twice(add_one, 5)) +@functools.cache +def p_result(): + return (apply_twice(add_one(), 5)) -p_ref_result = (add2_ref(3, 4)) +@functools.cache +def p_ref_result(): + return (add2_ref(3, 4)) diff --git a/test/diff/golden/Python_Examples.Example_polymorphic_const/Example_polymorphic_const.py b/test/diff/golden/Python_Examples.Example_polymorphic_const/Example_polymorphic_const.py index 9afa91e..28d8b37 100644 --- a/test/diff/golden/Python_Examples.Example_polymorphic_const/Example_polymorphic_const.py +++ b/test/diff/golden/Python_Examples.Example_polymorphic_const/Example_polymorphic_const.py @@ -2,15 +2,24 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): raise RuntimeError(msg) -empty_list = ([]) +@functools.cache +def empty_list(): + return ([]) -magic_number = (max(0, 42)) +@functools.cache +def magic_number(): + return (max(0, 42)) -uses_empty_list = (empty_list) +@functools.cache +def uses_empty_list(): + return (empty_list()) -uses_magic_number = ((magic_number + 1)) +@functools.cache +def uses_magic_number(): + return ((magic_number() + 1)) diff --git a/test/diff/golden/Python_Examples.Example_record_ext/Example_record_ext.py b/test/diff/golden/Python_Examples.Example_record_ext/Example_record_ext.py index 798abf9..4b071a5 100644 --- a/test/diff/golden/Python_Examples.Example_record_ext/Example_record_ext.py +++ b/test/diff/golden/Python_Examples.Example_record_ext/Example_record_ext.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -44,8 +45,14 @@ def px(x0): def point3d_sum(p): return ((((px(p) + py(p))) + pz(p))) -origin3d = (point_ext_(0, 0, point3d_ext_(0, None))) +@functools.cache +def origin3d(): + return (point_ext_(0, 0, point3d_ext_(0, None))) -shifted3d = (move_z(origin3d, 7)) +@functools.cache +def shifted3d(): + return (move_z(origin3d(), 7)) -total3d = (point3d_sum(shifted3d)) +@functools.cache +def total3d(): + return (point3d_sum(shifted3d())) diff --git a/test/diff/golden/Python_Examples.Example_records/Example_records.py b/test/diff/golden/Python_Examples.Example_records/Example_records.py index db04f04..bb7d1ad 100644 --- a/test/diff/golden/Python_Examples.Example_records/Example_records.py +++ b/test/diff/golden/Python_Examples.Example_records/Example_records.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): @@ -26,7 +27,9 @@ def px(x0): def point_sum(p, q): return ((((((px(p) + py(p))) + px(q))) + py(q))) -origin = (point_ext_(0, 0, None)) +@functools.cache +def origin(): + return (point_ext_(0, 0, None)) def px_update(pxa, x1): match (pxa, x1): @@ -36,6 +39,10 @@ def px_update(pxa, x1): def move_x(p, dx): return (px_update(lambda _: (px(p) + dx), p)) -shifted = (move_x(origin, 5)) +@functools.cache +def shifted(): + return (move_x(origin(), 5)) -total = (point_sum(origin, shifted)) +@functools.cache +def total(): + return (point_sum(origin(), shifted())) diff --git a/test/diff/golden/Python_Examples.Example_short_circuit/Example_short_circuit.py b/test/diff/golden/Python_Examples.Example_short_circuit/Example_short_circuit.py index fd513e1..8ef9ce8 100644 --- a/test/diff/golden/Python_Examples.Example_short_circuit/Example_short_circuit.py +++ b/test/diff/golden/Python_Examples.Example_short_circuit/Example_short_circuit.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num import List @@ -47,7 +48,9 @@ def impl_case(xs): x = (True) return (x) -if_case_empty = (if_case([])) +@functools.cache +def if_case_empty(): + return (if_case([])) def conj_case_list(xss): if (not List.null(xss)): @@ -63,14 +66,26 @@ def conj_case_list(xss): x = (False) return (x) -conj_case_zero = (conj_case([0])) +@functools.cache +def conj_case_zero(): + return (conj_case([0])) -conj_case_empty = (conj_case([])) +@functools.cache +def conj_case_empty(): + return (conj_case([])) -disj_case_empty = (disj_case([])) +@functools.cache +def disj_case_empty(): + return (disj_case([])) -disj_case_three = (disj_case([max(0, 3)])) +@functools.cache +def disj_case_three(): + return (disj_case([max(0, 3)])) -impl_case_empty = (impl_case([])) +@functools.cache +def impl_case_empty(): + return (impl_case([])) -conj_case_list_empty = (conj_case_list([])) +@functools.cache +def conj_case_list_empty(): + return (conj_case_list([])) diff --git a/test/diff/golden/Python_Examples.Example_super/Example_super.py b/test/diff/golden/Python_Examples.Example_super/Example_super.py index 905f21d..e07b36e 100644 --- a/test/diff/golden/Python_Examples.Example_super/Example_super.py +++ b/test/diff/golden/Python_Examples.Example_super/Example_super.py @@ -59,4 +59,6 @@ def describable_verbose_color(): def announce_verbose(a_dict, x): return (a_dict.describable_describable_verbose.describe(x) + " -- " + a_dict.describe_verbose(x)) -announce_verbose_red = (announce_verbose(describable_verbose_color(), Red())) +@functools.cache +def announce_verbose_red(): + return (announce_verbose(describable_verbose_color(), Red())) diff --git a/test/diff/golden/Python_Examples.Example_super_chain/Example_super_chain.py b/test/diff/golden/Python_Examples.Example_super_chain/Example_super_chain.py index c1df7d7..e716dbd 100644 --- a/test/diff/golden/Python_Examples.Example_super_chain/Example_super_chain.py +++ b/test/diff/golden/Python_Examples.Example_super_chain/Example_super_chain.py @@ -133,6 +133,10 @@ def announce_chain(a_dict, x): def announce_multi(a_dict, x): return (a_dict.labeled_labeled_measured.label_of(x) + " (" + a_dict.measured_labeled_measured.size_label(x) + ") -- " + a_dict.full_description(x)) -announce_chain_cat = (announce_chain(tagged_extra_animal(), Cat())) +@functools.cache +def announce_chain_cat(): + return (announce_chain(tagged_extra_animal(), Cat())) -announce_multi_box = (announce_multi(labeled_measured_item(), Box())) +@functools.cache +def announce_multi_box(): + return (announce_multi(labeled_measured_item(), Box())) diff --git a/test/diff/golden/Python_Examples.Example_transparent_wrapper/Example_transparent_wrapper.py b/test/diff/golden/Python_Examples.Example_transparent_wrapper/Example_transparent_wrapper.py index f59286b..aa1bdfd 100644 --- a/test/diff/golden/Python_Examples.Example_transparent_wrapper/Example_transparent_wrapper.py +++ b/test/diff/golden/Python_Examples.Example_transparent_wrapper/Example_transparent_wrapper.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from typing import Any, Callable +import functools def _abort(msg, thunk): raise RuntimeError(msg) @@ -9,7 +10,9 @@ def _abort(msg, thunk): def unbox(n): return (n) -unbox_test = (unbox(True)) +@functools.cache +def unbox_test(): + return (unbox(True)) def box_is_true(x0): match x0: @@ -20,4 +23,6 @@ def box_is_true(x0): case _: raise RuntimeError("match failed") -box_is_true_test = ((box_is_true(True), box_is_true(False))) +@functools.cache +def box_is_true_test(): + return ((box_is_true(True), box_is_true(False))) diff --git a/test/diff/golden/Python_Examples.Example_tuples/Example_tuples.py b/test/diff/golden/Python_Examples.Example_tuples/Example_tuples.py index 4323f44..978b957 100644 --- a/test/diff/golden/Python_Examples.Example_tuples/Example_tuples.py +++ b/test/diff/golden/Python_Examples.Example_tuples/Example_tuples.py @@ -2,14 +2,19 @@ from dataclasses import dataclass from typing import Any, Callable +import functools import Num def _abort(msg, thunk): raise RuntimeError(msg) -point = ((3, 4)) +@functools.cache +def point(): + return ((3, 4)) -triple = ((1, ((2, 3)))) +@functools.cache +def triple(): + return ((1, ((2, 3)))) def swap_pair(p): return (((p[1]), (p[0]))) @@ -19,8 +24,14 @@ def add_points(x0, x1): case ((x1a, y1), (x2, y2)): return ((((x1a + x2)), ((y1 + y2)))) -point_swapped = (swap_pair(point)) +@functools.cache +def point_swapped(): + return (swap_pair(point())) -points_sum = (add_points(point, point_swapped)) +@functools.cache +def points_sum(): + return (add_points(point(), point_swapped())) -points_list = ([point, point_swapped]) +@functools.cache +def points_list(): + return ([point(), point_swapped()]) diff --git a/test/diff/golden/Python_Examples.Example_tyclass/Example_tyclass.py b/test/diff/golden/Python_Examples.Example_tyclass/Example_tyclass.py index 4d4fd8f..f659028 100644 --- a/test/diff/golden/Python_Examples.Example_tyclass/Example_tyclass.py +++ b/test/diff/golden/Python_Examples.Example_tyclass/Example_tyclass.py @@ -59,6 +59,10 @@ def describable_shape(): def announce(a_dict, x): return (a_dict.describe(x) + a_dict.describe(x)) -announce_red = (announce(describable_color(), Red())) +@functools.cache +def announce_red(): + return (announce(describable_color(), Red())) -announce_circle = (announce(describable_shape(), Circle())) +@functools.cache +def announce_circle(): + return (announce(describable_shape(), Circle())) diff --git a/test/oracle/Oracle_Import_Cycle.thy b/test/oracle/Oracle_Import_Cycle.thy index 3a34f7c..7b74ff6 100644 --- a/test/oracle/Oracle_Import_Cycle.thy +++ b/test/oracle/Oracle_Import_Cycle.thy @@ -3,9 +3,9 @@ theory Oracle_Import_Cycle begin text \Sorting string literals makes this module import \String\ before \Arith\. - The generated \Arith\ and \String\ import each other, and \Arith\ uses - \String.char_of_integer\ at import time. Kept apart because the failure is at - import, so it fails every check in its module.\ + The generated \Arith\ and \String\ import each other, and a value of + \Arith\ uses \String.char_of_integer\, which fails if it is evaluated at import + time. Kept apart because such a failure is at import, so it fails every check in its module.\ python_check sort_str: "sort [STR ''b'', STR ''a'', STR ''c'']" diff --git a/test/oracle/run_test.py b/test/oracle/run_test.py index 0dac17b..cc28d2d 100644 --- a/test/oracle/run_test.py +++ b/test/oracle/run_test.py @@ -16,7 +16,8 @@ the generated modules under test/oracle/python_out/./. Each check `c` in the manifest names the module that defines `c_ok` (the generated `t = r`, where r is Isabelle's value of t) and `c_actual` (the -generated t, for failure messages), both functions of a unit argument. +generated t, for failure messages), both values of a unit function type: cached +functions without arguments that return a function of a unit argument. """ import importlib @@ -82,16 +83,17 @@ def test_agrees_with_isabelle(code_dir, module, check): pytest.fail(f"{term}: module {module} does not import: {_describe(e)}", pytrace=False) name = check["name"] - actual_fn = getattr(m, f"{name}_actual") + actual_def = getattr(m, f"{name}_actual") try: - ok = getattr(m, f"{name}_ok")(None) + actual_fn = actual_def() + ok = getattr(m, f"{name}_ok")()(None) except Exception as e: pytest.fail(f"{term}: Isabelle gives {check['expected']}, " - f"Python raises {_describe(e)}\n {_source(actual_fn)}", + f"Python raises {_describe(e)}\n {_source(actual_def)}", pytrace=False) if ok is not True: pytest.fail(f"{term}: Isabelle gives {check['expected']}, " - f"Python gives {actual_fn(None)!r}\n {_source(actual_fn)}", + f"Python gives {actual_fn(None)!r}\n {_source(actual_def)}", pytrace=False) @@ -101,6 +103,7 @@ def _describe(e): def _source(fn): + fn = inspect.unwrap(fn) code = fn.__code__ try: source = inspect.getsource(fn).strip() diff --git a/test/quick/Quick_Test.thy b/test/quick/Quick_Test.thy index 841eb56..d8258ae 100644 --- a/test/quick/Quick_Test.thy +++ b/test/quick/Quick_Test.thy @@ -264,6 +264,14 @@ definition twice_twice :: "'a::plus \ 'a" where "twice_twice = (\x. x + x) \ (\x. x + x)" definition e_twice_twice :: int where "e_twice_twice = twice_twice 3" +section \A value of a function type of arity 2\ + +text \Callers pass both arguments at once, so it is printed as a function of two + parameters rather than as a curried lambda.\ + +definition v_first :: "int \ int \ int" where "v_first = (\x y. x)" +definition v_first_use :: int where "v_first_use = v_first 3 4" + section \Code.abort in expression position\ definition a_checked :: "int \ int" where @@ -295,6 +303,7 @@ export_code ia_nth ia_grid ia_get ia_rows ia_head ia_any_neg z_int_div z_int_mod z_nat_div z_nat_mod z_int_floor l_shadow_none l_shadow_some c_boxed e_twice_twice + v_first v_first_use in Python file_prefix "." end diff --git a/test/quick/Quick_Test_Tail_Calls.thy b/test/quick/Quick_Test_Tail_Calls.thy index b045b57..8d429ec 100644 --- a/test/quick/Quick_Test_Tail_Calls.thy +++ b/test/quick/Quick_Test_Tail_Calls.thy @@ -2,8 +2,8 @@ theory Quick_Test_Tail_Calls imports Main "Python.Python_Setup" begin -text \Runtime checks for python_tail_calls: the values below are computed at import time and - recurse far deeper than Python's default limit of 1000 frames, unless printed as loops.\ +text \Runtime checks for python_tail_calls: the values below are computed when the tests use them + and recurse far deeper than Python's default limit of 1000 frames, unless printed as loops.\ declare [[python_tail_calls]] diff --git a/test/quick/run_test.py b/test/quick/run_test.py index 8cddd9b..73436a0 100644 --- a/test/quick/run_test.py +++ b/test/quick/run_test.py @@ -55,11 +55,11 @@ def test_abort_in_expression(): # ---- tuples ---- def test_tuple_values(): - assert m.t_point == (3, 4) + assert m.t_point() == (3, 4) def test_tuple_swap(): - assert m.t_swap(m.t_point) == (4, 3) + assert m.t_swap(m.t_point()) == (4, 3) def test_tuple_add(): @@ -69,15 +69,15 @@ def test_tuple_add(): def test_tuple_points_sum(): # t_points_sum = t_add_points t_point (t_swap t_point) # = (3,4) + (4,3) = (7,7) - assert m.t_points_sum == (7, 7) + assert m.t_points_sum() == (7, 7) # ---- int arithmetic ---- def test_int_floor_division(): # HOL's int div/mod use flooring semantics, matching Python's // and % - assert m.i_quotient_neg == -4 # (-7) div 2 - assert m.i_remainder_neg == 1 # (-7) mod 2 + assert m.i_quotient_neg() == -4 # (-7) div 2 + assert m.i_remainder_neg() == 1 # (-7) mod 2 def test_int_checksum(): @@ -88,54 +88,54 @@ def test_int_checksum(): # ---- nat arithmetic (monus, literals) ---- def test_nat_monus_truncates_at_zero(): - assert m.n_sub_trunc == 0 # 3 - 10, nat subtraction truncates at 0 + assert m.n_sub_trunc() == 0 # 3 - 10, nat subtraction truncates at 0 def test_nat_monus_normal_case(): - assert m.n_sub_pos == 7 # 10 - 3 + assert m.n_sub_pos() == 7 # 10 - 3 def test_nat_literal(): - assert m.n_literal == 42 + assert m.n_literal() == 42 # ---- nat structural pattern matching (0 / Suc) ---- def test_nat_factorial(): assert m.n_fact(0) == 1 - assert m.n_fact_5 == 120 + assert m.n_fact_5() == 120 assert m.n_fact(6) == 720 def test_nat_countdown(): assert m.n_countdown(0) == [0] - assert m.n_countdown_4 == [4, 3, 2, 1, 0] + assert m.n_countdown_4() == [4, 3, 2, 1, 0] # ---- int/nat remaining operators ---- def test_int_abs(): - assert m.i_abs_neg == 7 + assert m.i_abs_neg() == 7 def test_int_sgn(): - assert m.i_sgn_neg == -1 - assert m.i_sgn_zero == 0 - assert m.i_sgn_pos == 1 + assert m.i_sgn_neg() == -1 + assert m.i_sgn_zero() == 0 + assert m.i_sgn_pos() == 1 def test_int_min_max(): - assert m.i_min_ab == 3 - assert m.i_max_ab == 9 + assert m.i_min_ab() == 3 + assert m.i_max_ab() == 9 def test_nat_min_max(): - assert m.n_min_ab == 3 - assert m.n_max_ab == 9 + assert m.n_min_ab() == 3 + assert m.n_max_ab() == 9 # ---- nested case expressions ---- def test_nested_case_expression(): # nat_label n = 1 + (case n of 0 => 10 | Suc m => 20 + m) - assert m.label_zero == 11 # 1 + 10 - assert m.label_five == 25 # 1 + (20 + 4), since 5 = Suc^5(0), m = 4 + assert m.label_zero() == 11 # 1 + 10 + assert m.label_five() == 25 # 1 + (20 + 4), since 5 = Suc^5(0), m = 4 assert m.nat_label(0) == 11 assert m.nat_label(5) == 25 @@ -144,22 +144,22 @@ def test_nested_case_expression(): def test_if_then_else_whole_body(): # abs_manual x = if x < 0 then -x else x - assert m.abs_manual_neg == 7 - assert m.abs_manual_pos == 7 + assert m.abs_manual_neg() == 7 + assert m.abs_manual_pos() == 7 assert m.abs_manual(-7) == 7 assert m.abs_manual(7) == 7 def test_if_then_else_nested_in_expression(): # classify n = 1 + (if n = 0 then 100 else 200) - assert m.classify_zero == 101 - assert m.classify_other == 201 + assert m.classify_zero() == 101 + assert m.classify_other() == 201 def test_if_then_else_branch_with_nested_case(): # weird n = if n = 0 then 1 else 1 + (case n of 0 => 10 | Suc m => 20 + m) - assert m.weird_zero == 1 - assert m.weird_five == 25 + assert m.weird_zero() == 1 + assert m.weird_five() == 25 # ---- list pattern matching ---- @@ -168,7 +168,7 @@ def test_list_pattern_matching(): # describe_len xs = Suc (case xs of [] => 0 | (_ # ys) => length ys) assert m.describe_len([]) == 1 assert m.describe_len([1, 2, 3]) == 3 - assert m.describe_len_123 == 3 + assert m.describe_len_123() == 3 # ---- partial application ---- @@ -176,55 +176,55 @@ def test_list_pattern_matching(): def test_partial_application(): # add_one = add2 1 -- genuine partial application, eta-expanded via # Code_Thingol.saturated_application into a Python lambda - assert m.add_one(5) == 6 - assert m.p_add_one_5 == 6 + assert m.add_one()(5) == 6 + assert m.p_add_one_5() == 6 def test_bare_reference_and_point_free_alias(): # add2_ref = add2 -- point-free alias; args_num falls back to the # declared type's arity since the alias's own equation has no # explicit parameters of its own - assert m.p_ref_result == 7 + assert m.p_ref_result() == 7 def test_map_native_list(): # map add_one [1, 2, 3] -- requires the native code_printing # registration for List.map (Python_Setup.thy); without it this # silently fails to compile map's definition at all - assert m.p_mapped == [2, 3, 4] + assert m.p_mapped() == [2, 3, 4] def test_bare_binary_constant_as_function_value(): # foldr add2 [1, 2, 3] 0 -- add2 passed bare must be curried, since foldr # applies it one argument at a time - assert m.p_folded == 6 + assert m.p_folded() == 6 # ---- code numerals ---- def test_code_numeral_integer(): - assert m.cn_int_lit == 42 - assert m.cn_neg == -5 - assert m.cn_sum == 50 + assert m.cn_int_lit() == 42 + assert m.cn_neg() == -5 + assert m.cn_sum() == 50 def test_code_numeral_natural(): - assert m.cn_nat_lit == 7 - assert m.cn_nat_sum == 10 + assert m.cn_nat_lit() == 7 + assert m.cn_nat_sum() == 10 # ---- records, including extension ---- def test_record_extension_roundtrip(): # r_shifted3d = r_move_z r_origin3d 7, only pz changes - assert m.r_total3d == 7 + assert m.r_total3d() == 7 # ---- typeclasses / superclasses ---- def test_typeclass_dispatch(): - assert m.cls_default_b == 2 # default_val TagB - assert m.cls_double_a == 2 # double_val TagA = 2 * default_val TagA (=1) + assert m.cls_default_b() == 2 # default_val TagB + assert m.cls_double_a() == 2 # double_val TagA = 2 * default_val TagA (=1) # ---- dictionary-parameterized instance ---- @@ -232,22 +232,22 @@ def test_typeclass_dispatch(): def test_dict_parameterized_instance(): # has_default 'a list requires 'a's own has_default dictionary -- # exercises print_dict_args for Class_Instance printing. - assert m.cls_list_default == 2 # default_val TagB (first elem of [TagB, TagA]) - assert m.cls_list_default_empty == 0 + assert m.cls_list_default() == 2 # default_val TagB (first elem of [TagB, TagA]) + assert m.cls_list_default_empty() == 0 # ---- options ---- def test_option_values(): - assert m.o_describe_some == 6 - assert m.o_describe_none == 0 + assert m.o_describe_some() == 6 + assert m.o_describe_none() == 0 # ---- hoisted multi-argument lambdas ---- def test_hoisted_lambda_is_curried(): # apply2 calls its function argument as f(a)(b); the hoisted def must accept that. - assert m.l_curried_none == 3 - assert m.l_curried_some == 7 + assert m.l_curried_none() == 3 + assert m.l_curried_some() == 7 # ---- native List.nth and IArray ---- @@ -258,9 +258,9 @@ def test_list_nth_is_native_indexing(): def test_iarray_is_tuple(): - assert m.ia_grid == ((0, 1, 2), (10, 11, 12)) + assert m.ia_grid() == ((0, 1, 2), (10, 11, 12)) assert m.ia_get(1, 2) == 12 - assert m.ia_rows == [[0, 1, 2], [10, 11, 12]] + assert m.ia_rows() == [[0, 1, 2], [10, 11, 12]] def test_iarray_pattern_and_exists(): @@ -281,26 +281,34 @@ def test_dict_free_instance_is_built_once(): # ---- division by zero (Isabelle semantics) ---- def test_div_mod_by_zero(): - assert m.z_int_div == 0 - assert m.z_int_mod == -7 - assert m.z_nat_div == 0 - assert m.z_nat_mod == 7 - assert m.z_int_floor == (-4, 1) # floor division, as in Python + assert m.z_int_div() == 0 + assert m.z_int_mod() == -7 + assert m.z_nat_div() == 0 + assert m.z_nat_mod() == 7 + assert m.z_int_floor() == (-4, 1) # floor division, as in Python # ---- hoisting, constructors and dictionary-taking definitions ---- def test_hoisted_lambda_does_not_shadow_outer_vars(): - assert m.l_shadow_none == 104 # (1 + 3) + 100 - assert m.l_shadow_some == 14 # (1 + 3) + 10 + assert m.l_shadow_none() == 104 # (1 + 3) + 100 + assert m.l_shadow_some() == 14 # (1 + 3) + 10 def test_constructor_of_sorted_datatype(): - assert m.c_boxed == 5 + assert m.c_boxed() == 5 def test_pattern_less_dict_function(): - assert m.e_twice_twice == 12 + assert m.e_twice_twice() == 12 + + +# ---- a value of a function type of arity 2 ---- + +def test_value_of_arity_2(): + # v_first = (%x y. x) is a function of two parameters, as its callers pass both at once + assert m.v_first(3, 4) == 3 + assert m.v_first_use() == 3 # ---- python_tail_calls (Quick_Test_Tail_Calls.thy, exported with the option on) ---- @@ -316,8 +324,8 @@ def _load_tail_calls(): def test_tail_calls_run_as_loops(): tc = _load_tail_calls() - # computed at import, 100000 calls deep without the loop - assert tc.tc_sum_big == 100000 * 100001 // 2 + # 100000 calls deep without the loop + assert tc.tc_sum_big() == 100000 * 100001 // 2 # the list is built in Python: upt itself is not tail recursive. Kept at 20000, as each # [y, *ys] match copies the tail (quadratic), but still far deeper than 1000 frames. assert tc.tc_count_le_int(10000, list(range(20000)), 0) == 10001 @@ -325,5 +333,5 @@ def test_tail_calls_run_as_loops(): def test_tail_calls_with_closures_stay_recursive(): tc = _load_tail_calls() - assert tc.tc_shift_small == 13 + assert tc.tc_shift_small() == 13 assert tc.tc_shift(3, lambda x: x)(0) == 3 diff --git a/test/slow/run_test.py b/test/slow/run_test.py new file mode 100644 index 0000000..eca085e --- /dev/null +++ b/test/slow/run_test.py @@ -0,0 +1,71 @@ +""" +Import check for the Python generated from all code equations of the +slow test suite: every generated module must import, which py_compile +alone does not check (e.g. circular imports between the modules fail +only at import time). + +Prerequisite (run this first, from the project root): + + isabelle build -d . -e Python_Test_Slow + +Then: + + uv run pytest test/slow/run_test.py -q --tb=line + +Each module is imported in a fresh interpreter, since importing a module +also runs (and may break) the modules it imports. +""" + +import re +import subprocess +import sys +from pathlib import Path + +import pytest + +THIS_DIR = Path(__file__).resolve().parent +GENERATED_DIRS = [THIS_DIR / "generated_basic", THIS_DIR / "generated_full"] + +# Modules that are known not to import +KNOWN_FAILURES = {} + + +def _cases(): + for gen_dir in GENERATED_DIRS: + for path in sorted(gen_dir.glob("*.py")): + marks = [] + if path.stem in KNOWN_FAILURES: + marks.append(pytest.mark.xfail(reason=KNOWN_FAILURES[path.stem], strict=True)) + yield pytest.param(gen_dir, path.stem, id=f"{gen_dir.name}:{path.stem}", marks=marks) + + +_CASES = list(_cases()) +if not _CASES: + pytest.skip( + "no generated modules under test/slow -- run " + "`isabelle build -d . -e Python_Test_Slow` first.", + allow_module_level=True, + ) + + +_CACHED_DEF = re.compile(r"^@functools\.cache\ndef (\w+)(?:\[[^\]]*\])?\((.*?)\)", re.M) + + +@pytest.mark.parametrize("gen_dir,module", _CASES) +def test_cached_functions_take_no_arguments(gen_dir, module): + # Only lazy values and instances without dictionaries are cached, and their + # uses call them without arguments + source = (gen_dir / f"{module}.py").read_text() + with_args = [name for name, params in _CACHED_DEF.findall(source) if params.strip()] + assert not with_args, f"cached functions with parameters: {', '.join(with_args)}" + + +@pytest.mark.parametrize("gen_dir,module", _CASES) +def test_imports(gen_dir, module): + result = subprocess.run( + [sys.executable, "-c", f"import {module}"], + cwd=gen_dir, capture_output=True, text=True, + ) + if result.returncode != 0: + last_line = (result.stderr.strip().splitlines() or ["(no output)"])[-1] + pytest.fail(f"import {module} failed: {last_line}", pytrace=False) From ff83f9cd135237cc102336874bef13b29c5a7a2f Mon Sep 17 00:00:00 2001 From: Nikolaus Huber Date: Wed, 4 Nov 2026 10:45:54 +0000 Subject: [PATCH 2/2] remove unnecessary code_module definitions --- Python.thy | 3 --- Python_Setup.thy | 4 ---- README.md | 2 +- .../{Arith.py => Code_Numeral.py} | 0 .../Example_integer_bit_ops.py | 20 +++++++++---------- test/oracle/Oracle_Import_Cycle.thy | 8 ++++---- 6 files changed, 15 insertions(+), 22 deletions(-) rename test/diff/golden/Python_Examples.Example_integer_bit_ops/{Arith.py => Code_Numeral.py} (100%) diff --git a/Python.thy b/Python.thy index a1fce3b..5a39f22 100644 --- a/Python.thy +++ b/Python.thy @@ -17,9 +17,6 @@ setup \fn thy => declare [[default_code_width = 2000]] -code_identifier - code_module Code_Numeral \ (Python) Arith - (* Panic, by a helper function, since Code.abort occurs in expression position *) code_printing constant Code.abort \ (Python) "'_abort" diff --git a/Python_Setup.thy b/Python_Setup.thy index a0cd816..e83cd67 100644 --- a/Python_Setup.thy +++ b/Python_Setup.thy @@ -3,10 +3,6 @@ theory Python_Setup "HOL-Library.Code_Abstract_Char" begin -code_identifier - code_module Code_Target_Nat \ (Python) Arith -| code_module Code_Target_Int \ (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). *) diff --git a/README.md b/README.md index 6a1be51..e88d9f0 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ it, so it is evaluated once, when it is first used: ```python @functools.cache def num_of_nat(): - return (Fun.comp(Arith.num_of_integer, lambda a: a)) + return (Fun.comp(Code_Numeral.num_of_integer, lambda a: a)) ``` Code that uses the generated modules therefore calls a value too diff --git a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Arith.py b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Code_Numeral.py similarity index 100% rename from test/diff/golden/Python_Examples.Example_integer_bit_ops/Arith.py rename to test/diff/golden/Python_Examples.Example_integer_bit_ops/Code_Numeral.py diff --git a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py index d9dbe22..a896066 100644 --- a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py +++ b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Example_integer_bit_ops.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from typing import Any, Callable import functools -import Arith +import Code_Numeral import Num def _abort(msg, thunk): @@ -11,36 +11,36 @@ def _abort(msg, thunk): @functools.cache def test_bit(): - return (Arith.bit_integer(10, 1)) + return (Code_Numeral.bit_integer(10, 1)) @functools.cache def test_sgn(): - return (Arith.sgn_integer(-42)) + return (Code_Numeral.sgn_integer(-42)) @functools.cache def test_mask(): - return (Arith.mask_integer((max(0, 5)))) + return (Code_Numeral.mask_integer((max(0, 5)))) @functools.cache def test_set_bit(): - return (Arith.set_bit_integer((max(0, 2)), 0)) + return (Code_Numeral.set_bit_integer((max(0, 2)), 0)) @functools.cache def test_drop_bit(): - return (Arith.drop_bit_integer((max(0, 2)), 20)) + return (Code_Numeral.drop_bit_integer((max(0, 2)), 20)) @functools.cache def test_flip_bit(): - return (Arith.flip_bit_integer(1, 5)) + return (Code_Numeral.flip_bit_integer(1, 5)) @functools.cache def test_push_bit(): - return (Arith.push_bit_integer((max(0, 3)), 5)) + return (Code_Numeral.push_bit_integer((max(0, 3)), 5)) @functools.cache def test_take_bit(): - return (Arith.take_bit_integer((max(0, 4)), 255)) + return (Code_Numeral.take_bit_integer((max(0, 4)), 255)) @functools.cache def test_unset_bit(): - return (Arith.unset_bit_integer(0, 7)) + return (Code_Numeral.unset_bit_integer(0, 7)) diff --git a/test/oracle/Oracle_Import_Cycle.thy b/test/oracle/Oracle_Import_Cycle.thy index 7b74ff6..57522b5 100644 --- a/test/oracle/Oracle_Import_Cycle.thy +++ b/test/oracle/Oracle_Import_Cycle.thy @@ -2,10 +2,10 @@ theory Oracle_Import_Cycle imports Python_Check "Python.Python_Setup" begin -text \Sorting string literals makes this module import \String\ before \Arith\. - The generated \Arith\ and \String\ import each other, and a value of - \Arith\ uses \String.char_of_integer\, which fails if it is evaluated at import - time. Kept apart because such a failure is at import, so it fails every check in its module.\ +text \Some generated modules of these checks import each other (e.g. \Num\, \Groups\, + \Power\ and \Rings\), and sorting string literals makes this module import \List\ + and \String\. Kept apart because a failure at import, as a value evaluated while a module + it uses is still being imported, would fail every check in its module.\ python_check sort_str: "sort [STR ''b'', STR ''a'', STR ''c'']"