diff --git a/examples/Example_tree_weight.thy b/examples/Example_tree_weight.thy index 74f32bf..2724e8d 100644 --- a/examples/Example_tree_weight.thy +++ b/examples/Example_tree_weight.thy @@ -41,8 +41,14 @@ end definition example_weight :: nat where "example_weight = calc_weight (Node Leaf (Node Leaf (7::nat) Leaf) Leaf)" +(* A further example not in the paper looking at how nested case in a short-circuiting context +gets translated *) + +definition conj_case_t :: "nat tree \ bool" where + "conj_case_t t = (t \ Leaf \ ((case t of Leaf \ 0 | Node l y r \ y) = 0))" + export_code - ins root_or fold_tree from_list example_weight + ins conj_case_t root_or fold_tree from_list example_weight in Python file_prefix "." end diff --git a/test/diff/golden/Python_Examples.Example_integer_bit_ops/Code_Numeral.py b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Code_Numeral.py new file mode 100644 index 0000000..3eea699 --- /dev/null +++ b/test/diff/golden/Python_Examples.Example_integer_bit_ops/Code_Numeral.py @@ -0,0 +1,62 @@ +# Generated by Isabelle from theory HOL.Code_Numeral; do not edit + +from dataclasses import dataclass +from typing import Any, Callable +import functools +import Num +import Power +import Groups + +def _abort(msg, thunk): + raise RuntimeError(msg) + +@functools.cache +def one_integer(): + return Groups.one(one_ = lambda : 1) + +@functools.cache +def times_integer(): + return Groups.times(times_ = lambda a, b: (a * b)) + +@functools.cache +def power_integer(): + return Power.power(one_power = one_integer(), times_power = times_integer()) + +def drop_bit(i, k): + return ((lambda a, b: a // b if b else 0)(k, Power.power_(power_integer(), 2, (max(0, (abs(i))))))) + +def push_bit(i, k): + return ((k * Power.power_(power_integer(), 2, (max(0, (abs(i))))))) + +def sgn_integer(k): + if ((k == 0)): + return (0) + else: + if ((k < 0)): + return (-1) + else: + return (1) + +def push_bit_integer(n, k): + return (push_bit(n, k)) + +def bit_integer(k, n): + return (not ((((k & push_bit_integer(n, 1))) == 0))) + +def mask_integer(n): + return ((push_bit_integer(n, 1) - 1)) + +def set_bit_integer(n, k): + return ((k | push_bit_integer(n, 1))) + +def drop_bit_integer(n, k): + return (drop_bit(n, k)) + +def flip_bit_integer(n, k): + return ((k ^ push_bit_integer(n, 1))) + +def take_bit_integer(n, k): + return ((k & mask_integer(n))) + +def unset_bit_integer(n, k): + return ((k & ((~ push_bit_integer(n, 1))))) 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 f81cfb8..4c0d57b 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,7 +4,9 @@ from typing import Any, Callable import functools import Num +import Nat import List +import HOL import Orderings def _abort(msg, thunk): @@ -74,4 +76,32 @@ 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 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/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/oracle/Oracle_Strings_Bools.thy b/test/oracle/Oracle_Strings_Bools.thy index 6e75658..d4a2e18 100644 --- a/test/oracle/Oracle_Strings_Bools.thy +++ b/test/oracle/Oracle_Strings_Bools.thy @@ -31,4 +31,93 @@ python_check b_if_arith: "(if True then 1 else 2::int) + (if False then 10 else python_check b_neg_neg: "- (- (3::int))" python_check b_if_cond_eq: "(if (1::int) = 1 = True then 1 else (0::int))" +section \Short-circuiting connectives\ + +text \A case expression in the right operand of a conjunction, guarded by the left + operand: @{term "hd []"} must not be evaluated.\ + +definition conj_case_alt :: "nat list \ bool" where + "conj_case_alt xs = (xs \ [] \ ((case hd xs of 0 \ 0 | Suc n \ n) = 0))" + +python_check_grid sc_conj_case_alt: "conj_case_alt" over "[[], [0], [1], [2], [0, 5], [3, 1]]" + +definition conj_case_alt2 :: "nat list \ bool" where + "conj_case_alt2 xs = (xs \ [] \ ((case xs of [] \ 0 | (x # xs) \ x) = 0))" + +python_check_grid sc_conj_case_alt2: "conj_case_alt2" over "[[], [0], [1], [2], [0, 5], [3, 0]]" + +text \The same on a user datatype (as in examples/Example_tree_weight.thy): the guard is + an equality on the datatype rather than @{const List.null}.\ + +datatype 'a tree = Leaf | Node "'a tree" 'a "'a tree" + +definition conj_case_t :: "nat tree \ bool" where + "conj_case_t t = (t \ Leaf \ ((case t of Leaf \ 0 | Node l y r \ y) = 0))" + +python_check_grid sc_conj_case_t: "conj_case_t" over + "[Leaf, Node Leaf 0 Leaf, Node Leaf 3 Leaf, Node (Node Leaf 0 Leaf) 2 Leaf, + Node Leaf 0 (Node Leaf 5 Leaf)]" + +text \The other connectives, with a case of type @{typ nat} in the right operand.\ + +definition disj_case_alt :: "nat list \ bool" where + "disj_case_alt xs = (xs = [] \ (case hd xs of 0 \ 1 | Suc n \ n) = 1)" + +definition impl_case_alt :: "nat list \ bool" where + "impl_case_alt xs = (xs \ [] \ (case hd xs of 0 \ 0 | Suc n \ n) = 0)" + +definition not_disj_case :: "nat list \ bool" where + "not_disj_case xs = (\ (xs = [] \ (case hd xs of 0 \ False | Suc _ \ True)))" + +python_check_grid sc_disj_case_alt: "disj_case_alt" over "[[], [0], [1], [2], [2, 0]]" +python_check_grid sc_impl_case_alt: "impl_case_alt" over "[[], [0], [1], [2], [2, 0]]" +python_check_grid sc_not_disj_case: "not_disj_case" over "[[], [0], [1]]" + +text \Each guard protects the operands after it.\ + +definition conj_chain :: "nat list \ bool" where + "conj_chain xs = (xs \ [] \ tl xs \ [] \ (case hd (tl xs) of 0 \ True | Suc _ \ False))" + +definition disj_conj :: "nat list \ bool" where + "disj_conj xs = (xs = [] \ (tl xs \ [] \ (case hd (tl xs) of 0 \ True | Suc _ \ False)))" + +definition case_conj_case :: "nat list \ bool" where + "case_conj_case xs = ((case xs of [] \ False | _ \ True) \ (case hd xs of 0 \ True | Suc _ \ False))" + +python_check_grid sc_conj_chain: "conj_chain" over "[[], [0], [0, 0], [0, 1], [1, 0, 2]]" +python_check_grid sc_disj_conj: "disj_conj" over "[[], [0], [0, 0], [0, 1]]" +python_check_grid sc_case_conj_case: "case_conj_case" over "[[], [0], [1]]" + +text \Partial operations other than @{const hd}.\ + +definition conj_nth :: "nat list \ nat \ bool" where + "conj_nth xs i = (i < length xs \ (case xs ! i of 0 \ True | Suc n \ n = 2))" + +definition conj_the :: "nat option \ bool" where + "conj_the x = (x \ None \ (case the x of 0 \ True | Suc _ \ False))" + +definition conj_option_case :: "nat option list \ bool" where + "conj_option_case xs = (xs \ [] \ (case hd xs of None \ False | Some n \ 0 < n))" + +python_check_grid sc_conj_nth: "conj_nth" over "[[], [0], [3, 1]]" "[0, 1, 2]" +python_check_grid sc_conj_the: "conj_the" over "[None, Some 0, Some 4]" +python_check_grid sc_conj_option_case: "conj_option_case" over "[[], [None], [Some 0], [Some 2, None]]" + +text \The connective in an if condition, in a lambda and in a recursive function.\ + +definition if_conj :: "nat list \ nat" where + "if_conj xs = (if xs \ [] \ (case hd xs of 0 \ True | Suc _ \ False) then 1 else 2)" + +fun zero_heads :: "nat list list \ bool" where + "zero_heads [] = True" +| "zero_heads (xs # xss) = + ((xs \ [] \ (case hd xs of 0 \ True | Suc _ \ False)) \ zero_heads xss)" + +python_check_grid sc_if_conj: "if_conj" over "[[], [0], [1]]" +python_check sc_map_lambda: + "map (\xs. xs \ [] \ (case hd xs of 0 \ True | Suc _ \ False)) [[], [0::nat], [1, 0]]" +python_check sc_filter_lambda: + "filter (\xs. xs = [] \ (case hd xs of 0 \ True | Suc _ \ False)) [[], [0::nat], [1, 0]]" +python_check_grid sc_zero_heads: "zero_heads" over "[[], [[]], [[0], [], [0, 1]], [[], [2]]]" + end