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 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..74f32bf --- /dev/null +++ b/examples/Example_tree_weight.thy @@ -0,0 +1,48 @@ +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)" + +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 new file mode 100644 index 0000000..f81cfb8 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_tree_weight/Example_tree_weight.py @@ -0,0 +1,77 @@ +# 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 List +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())) + +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/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/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..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_enumerate: "enumerate 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]"