From 47d81d1235e7d4ca80ac54b337d26521f199c65b Mon Sep 17 00:00:00 2001 From: Dominic Orchard Date: Wed, 4 Nov 2026 22:39:43 +0000 Subject: [PATCH 1/5] example from the paper --- ROOT | 1 + examples/Example_tree_weight.thy | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 examples/Example_tree_weight.thy diff --git a/ROOT b/ROOT index 9942046..d2bdc1e 100644 --- a/ROOT +++ b/ROOT @@ -41,6 +41,7 @@ session Python_Examples in "examples" = Python + Example_iarray Example_tail_calls Example_short_circuit + Example_tree_weight export_files (in "python_out") "*:**" session Python_Test_Quick in "test/quick" = Python + diff --git a/examples/Example_tree_weight.thy b/examples/Example_tree_weight.thy new file mode 100644 index 0000000..a7aed06 --- /dev/null +++ b/examples/Example_tree_weight.thy @@ -0,0 +1,44 @@ +theory Example_tree_weight + imports Main "Python.Python_Setup" +begin + +datatype 'a tree = Leaf | Node "'a tree" 'a "'a tree" + +fun ins :: "'a::linorder \ 'a tree \ 'a tree" where + "ins x Leaf = Node Leaf x Leaf" + | "ins x (Node l y r) = + (if x < y then Node (ins x l) y r + else if y < x then Node l y (ins x r) + else Node l y r)" + +definition root_or :: "'a \ 'a tree \ 'a list" where + "root_or d t = [case t of Leaf \ d | Node _ x _ \ x]" + +fun fold_tree :: "('b \ 'a \ 'b \ 'b) \ 'b \ 'a tree \ 'b" where + "fold_tree f e Leaf = e" + | "fold_tree f e (Node l x r) = f (fold_tree f e l) x (fold_tree f e r)" + +definition from_list :: "'a::linorder list \ 'a tree" where + "from_list xs = foldr ins xs Leaf" + +class weight = + fixes calc_weight :: "'a \ nat" + +instantiation nat :: weight +begin + definition calc_weight_nat :: "nat \ nat" where + "calc_weight_nat n = n" + instance .. +end + +instantiation tree :: (weight) weight +begin + definition calc_weight_tree :: "'a tree \ nat" where + "calc_weight_tree = fold_tree (\l x r. l + calc_weight x + r) 0" + instance .. +end + +definition example_weight :: nat where + "example_weight = calc_weight (Node Leaf (Node Leaf (7::nat) Leaf) Leaf)" + +end From 867bddef1483a03e463dd591e19fce0af3a31aa6 Mon Sep 17 00:00:00 2001 From: Dominic Orchard Date: Thu, 5 Nov 2026 12:14:15 +0000 Subject: [PATCH 2/5] golden tree weight outputs --- .../Example_tree_weight.py | 107 ++++++++++++++++++ .../Fun.py | 12 ++ .../HOL.py | 14 +++ .../List.py | 17 +++ .../Nat.py | 13 +++ .../Num.py | 17 +++ .../Orderings.py | 24 ++++ test/oracle/Oracle_Data.thy | 2 +- 8 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Fun.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/HOL.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/List.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Nat.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Num.py create mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Orderings.py diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py b/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py new file mode 100644 index 0000000..4c0d57b --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py @@ -0,0 +1,107 @@ +# Generated by Isabelle from theory Python_Examples.Example_tree_weight; do not edit + +from dataclasses import dataclass +from typing import Any, Callable +import functools +import Num +import Nat +import List +import HOL +import Orderings + +def _abort(msg, thunk): + raise RuntimeError(msg) + +def calc_weight_nat(n): + return (n) + +@dataclass(frozen=True) +class weight: + calc_weight: Callable + +@functools.cache +def weight_nat(): + return weight(calc_weight = lambda a: calc_weight_nat(a)) + +@dataclass(frozen=True) +class Leaf: + pass +@dataclass(frozen=True) +class Node: + a: Any + b: Any + c: Any + +def fold_tree(f, e, x2): + match (f, e, x2): + case (fa, ea, Leaf()): + return (ea) + case (fa, ea, Node(l, x, r)): + return (((fa(fold_tree(fa, ea, l)))(x))(fold_tree(fa, ea, r))) + case _: + raise RuntimeError("match failed") + +def calc_weight_tree(a_dict, x): + return (fold_tree(lambda l: lambda xa: lambda aa: (((l + a_dict.calc_weight(xa))) + aa), 0, x)) + +def weight_tree(a_dict): + return weight(calc_weight = lambda a: calc_weight_tree(a_dict, a)) + +def ins(a_dict, x, xa1): + match (x, xa1): + case (xa, Leaf()): + return (Node(Leaf(), xa, Leaf())) + case (xa, Node(l, y, r)): + if (a_dict.order_linorder.preorder_order.ord_preorder.less(xa, y)): + return (Node(ins(a_dict, xa, l), y, r)) + else: + if (a_dict.order_linorder.preorder_order.ord_preorder.less(y, xa)): + return (Node(l, y, ins(a_dict, xa, r))) + else: + return (Node(l, y, r)) + case _: + raise RuntimeError("match failed") + +def root_or(d, t): + target = (t) + match target: + case Leaf(): + x = (d) + case Node(_, xa, _): + x = (xa) + case _: + raise RuntimeError("match failed") + return ([x]) + +def from_list(a_dict, xs): + return (List.foldr(lambda aa: lambda b: ins(a_dict, aa, b), xs) (Leaf())) + +def equal_tree(a_dict, x0, x1): + match (x0, x1): + case (Leaf(), Node(x21, x22, x23)): + return (False) + case (Node(x21, x22, x23), Leaf()): + return (False) + case (Node(x21, x22, x23), Node(y21, y22, y23)): + return (equal_tree(a_dict, x21, y21) and (HOL.eq(a_dict, x22, y22) and equal_tree(a_dict, x23, y23))) + case (Leaf(), Leaf()): + return (True) + case _: + raise RuntimeError("match failed") + +def conj_case_t(t): + if (not equal_tree(Nat.equal_nat(), t, Leaf())): + target = (t) + match target: + case Leaf(): + y = (0) + case Node(_, ya, _): + y = (ya) + case _: + raise RuntimeError("match failed") + x = ((y == 0)) + else: + x = (False) + return (x) + +example_weight = (calc_weight_tree(weight_tree(weight_nat()), Node(Leaf(), Node(Leaf(), (max(0, 7)), Leaf()), Leaf()))) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Fun.py b/test/diff/golden/Python_Examples.Example_tree_weight/Fun.py new file mode 100644 index 0000000..269eaa8 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Fun.py @@ -0,0 +1,12 @@ +# Generated by Isabelle from theory HOL.Fun; do not edit + +from dataclasses import dataclass +from typing import Any, Callable + +def _abort(msg, thunk): + raise RuntimeError(msg) + +id_ = (lambda x: x) + +def comp(f, g): + return (lambda x: f(g(x))) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py b/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py new file mode 100644 index 0000000..50329ea --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py @@ -0,0 +1,14 @@ +# Generated by Isabelle from theory HOL.HOL; do not edit + +from dataclasses import dataclass +from typing import Any, Callable + +def _abort(msg, thunk): + raise RuntimeError(msg) + +@dataclass(frozen=True) +class equal: + equal_: Callable + +def eq(a_dict, aa, b): + return (a_dict.equal_(aa, b)) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/List.py b/test/diff/golden/Python_Examples.Example_tree_weight/List.py new file mode 100644 index 0000000..acc3c92 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/List.py @@ -0,0 +1,17 @@ +# Generated by Isabelle from theory HOL.List; do not edit + +from dataclasses import dataclass +from typing import Any, Callable +import Fun + +def _abort(msg, thunk): + raise RuntimeError(msg) + +def foldr(f, x1): + match (f, x1): + case (fa, []): + return (Fun.id_) + case (fa, [x, *xs]): + return (Fun.comp((fa(x)), foldr(fa, xs))) + case _: + raise RuntimeError("match failed") diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py b/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py new file mode 100644 index 0000000..ddb7ef3 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py @@ -0,0 +1,13 @@ +# Generated by Isabelle from theory HOL.Nat; do not edit + +from dataclasses import dataclass +from typing import Any, Callable +import functools +import HOL + +def _abort(msg, thunk): + raise RuntimeError(msg) + +@functools.cache +def equal_nat(): + return HOL.equal(equal_ = lambda a, b: (a == b)) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Num.py b/test/diff/golden/Python_Examples.Example_tree_weight/Num.py new file mode 100644 index 0000000..b751fd5 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Num.py @@ -0,0 +1,17 @@ +# Generated by Isabelle from theory HOL.Num; do not edit + +from dataclasses import dataclass +from typing import Any, Callable + +def _abort(msg, thunk): + raise RuntimeError(msg) + +@dataclass(frozen=True) +class One: + pass +@dataclass(frozen=True) +class Bit0: + a: Any +@dataclass(frozen=True) +class Bit1: + a: Any diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Orderings.py b/test/diff/golden/Python_Examples.Example_tree_weight/Orderings.py new file mode 100644 index 0000000..ddbb6b0 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Orderings.py @@ -0,0 +1,24 @@ +# Generated by Isabelle from theory HOL.Orderings; do not edit + +from dataclasses import dataclass +from typing import Any, Callable + +def _abort(msg, thunk): + raise RuntimeError(msg) + +@dataclass(frozen=True) +class ord_: + less_eq: Callable + less: Callable + +@dataclass(frozen=True) +class preorder: + ord_preorder: Any + +@dataclass(frozen=True) +class order: + preorder_order: Any + +@dataclass(frozen=True) +class linorder: + order_linorder: Any diff --git a/test/oracle/Oracle_Data.thy b/test/oracle/Oracle_Data.thy index ebaba0b..c2fe9c2 100644 --- a/test/oracle/Oracle_Data.thy +++ b/test/oracle/Oracle_Data.thy @@ -40,7 +40,7 @@ python_check l_map_pair: "map (\(a, b). a + b) [(1::int, 2), (3, 4)]" python_check l_list_all: "(list_all (\x. x > 0) [1::int, 2], list_ex (\x. x < 0) [1::int])" python_check l_insert: "(List.insert 2 [1::int, 3], List.insert 1 [1::int, 3])" python_check l_product: "List.product [1::int, 2] [True, False]" -python_check l_enumerate: "enumerate 5 [True, False]" +python_check l_indexed_from: "indexed_from 5 [True, False]" python_check l_splice: "splice [1::int, 3] [2, 4, 6]" python_check l_rotate: "rotate 2 [1::int, 2, 3]" python_check l_min_list: "min_list [3::int, -1, 2]" From f96f3407329c81c703651f51c43f26caecfa064e Mon Sep 17 00:00:00 2001 From: Dominic Orchard Date: Thu, 5 Nov 2026 12:34:49 +0000 Subject: [PATCH 3/5] comment an example thats at the edge of a breaking api change beween isabelle 2026 and 2025-2 --- test/oracle/Oracle_Data.thy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/oracle/Oracle_Data.thy b/test/oracle/Oracle_Data.thy index c2fe9c2..fe7b01a 100644 --- a/test/oracle/Oracle_Data.thy +++ b/test/oracle/Oracle_Data.thy @@ -40,7 +40,8 @@ python_check l_map_pair: "map (\(a, b). a + b) [(1::int, 2), (3, 4)]" python_check l_list_all: "(list_all (\x. x > 0) [1::int, 2], list_ex (\x. x < 0) [1::int])" python_check l_insert: "(List.insert 2 [1::int, 3], List.insert 1 [1::int, 3])" python_check l_product: "List.product [1::int, 2] [True, False]" -python_check l_indexed_from: "indexed_from 5 [True, False]" +(* TODO: uncomment once moving to Isabelle 2026 *) +(* python_check l_indexed_from: "indexed_from 5 [True, False]" *) python_check l_splice: "splice [1::int, 3] [2, 4, 6]" python_check l_rotate: "rotate 2 [1::int, 2, 3]" python_check l_min_list: "min_list [3::int, -1, 2]" From c5c06932d76ac358d69836c2853de3663fb56a07 Mon Sep 17 00:00:00 2001 From: Dominic Orchard Date: Thu, 5 Nov 2026 12:39:39 +0000 Subject: [PATCH 4/5] add missing export_code --- examples/Example_tree_weight.thy | 4 +++ .../Example_tree_weight.py | 30 ------------------- .../HOL.py | 14 --------- .../Nat.py | 13 -------- 4 files changed, 4 insertions(+), 57 deletions(-) delete mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/HOL.py delete mode 100644 test/diff/golden/Python_Examples.Example_tree_weight/Nat.py diff --git a/examples/Example_tree_weight.thy b/examples/Example_tree_weight.thy index a7aed06..74f32bf 100644 --- a/examples/Example_tree_weight.thy +++ b/examples/Example_tree_weight.thy @@ -41,4 +41,8 @@ end definition example_weight :: nat where "example_weight = calc_weight (Node Leaf (Node Leaf (7::nat) Leaf) Leaf)" +export_code + ins root_or fold_tree from_list example_weight + in Python file_prefix "." + end diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py b/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py index 4c0d57b..f81cfb8 100644 --- a/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py @@ -4,9 +4,7 @@ from typing import Any, Callable import functools import Num -import Nat import List -import HOL import Orderings def _abort(msg, thunk): @@ -76,32 +74,4 @@ def root_or(d, t): def from_list(a_dict, xs): return (List.foldr(lambda aa: lambda b: ins(a_dict, aa, b), xs) (Leaf())) -def equal_tree(a_dict, x0, x1): - match (x0, x1): - case (Leaf(), Node(x21, x22, x23)): - return (False) - case (Node(x21, x22, x23), Leaf()): - return (False) - case (Node(x21, x22, x23), Node(y21, y22, y23)): - return (equal_tree(a_dict, x21, y21) and (HOL.eq(a_dict, x22, y22) and equal_tree(a_dict, x23, y23))) - case (Leaf(), Leaf()): - return (True) - case _: - raise RuntimeError("match failed") - -def conj_case_t(t): - if (not equal_tree(Nat.equal_nat(), t, Leaf())): - target = (t) - match target: - case Leaf(): - y = (0) - case Node(_, ya, _): - y = (ya) - case _: - raise RuntimeError("match failed") - x = ((y == 0)) - else: - x = (False) - return (x) - example_weight = (calc_weight_tree(weight_tree(weight_nat()), Node(Leaf(), Node(Leaf(), (max(0, 7)), Leaf()), Leaf()))) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py b/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py deleted file mode 100644 index 50329ea..0000000 --- a/test/diff/golden/Python_Examples.Example_tree_weight/HOL.py +++ /dev/null @@ -1,14 +0,0 @@ -# Generated by Isabelle from theory HOL.HOL; do not edit - -from dataclasses import dataclass -from typing import Any, Callable - -def _abort(msg, thunk): - raise RuntimeError(msg) - -@dataclass(frozen=True) -class equal: - equal_: Callable - -def eq(a_dict, aa, b): - return (a_dict.equal_(aa, b)) diff --git a/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py b/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py deleted file mode 100644 index ddb7ef3..0000000 --- a/test/diff/golden/Python_Examples.Example_tree_weight/Nat.py +++ /dev/null @@ -1,13 +0,0 @@ -# Generated by Isabelle from theory HOL.Nat; do not edit - -from dataclasses import dataclass -from typing import Any, Callable -import functools -import HOL - -def _abort(msg, thunk): - raise RuntimeError(msg) - -@functools.cache -def equal_nat(): - return HOL.equal(equal_ = lambda a, b: (a == b)) From 593c4a5a5c58dc855df861c2af8274be7d5c12fe Mon Sep 17 00:00:00 2001 From: Nikolaus Huber Date: Thu, 5 Nov 2026 14:11:29 +0000 Subject: [PATCH 5/5] recompile isabelle theories for test targets --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2142e5a..157bf97 100644 --- a/Makefile +++ b/Makefile @@ -12,19 +12,19 @@ test-slow: build-test-slow $(PYTHON) -m py_compile test/slow/generated_full/*.py build-test-quick: - $(ISABELLE) build -d . -e Python_Test_Quick + $(ISABELLE) build -d . -e -c Python_Test_Quick test-quick: $(PYTEST) test/quick/run_test.py build-examples: - $(ISABELLE) build -d . -e Python_Examples + $(ISABELLE) build -d . -e -c Python_Examples test-diff: build-examples $(PYTEST) test/diff/run_test.py build-test-oracle: - $(ISABELLE) build -d . -e Python_Test_Oracle + $(ISABELLE) build -d . -e -c Python_Test_Oracle test-oracle: build-test-oracle $(PYTEST) test/oracle/run_test.py @@ -40,7 +40,7 @@ test-all: test-quick test-slow test-diff test-oracle test: test-quick build: - $(ISABELLE) build -d . -e Python + $(ISABELLE) build -d . -e -c Python .PHONY: default build-test-slow test-slow build-test-quick test-quick build-examples test-diff reseed test-all test build-test-oracle test-oracle build reserved-words