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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/Example_tree_weight.thy
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<Rightarrow> bool" where
"conj_case_t t = (t \<noteq> Leaf \<and> ((case t of Leaf \<Rightarrow> 0 | Node l y r \<Rightarrow> 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
Original file line number Diff line number Diff line change
@@ -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)))))
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())))
14 changes: 14 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/HOL.py
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 13 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/Nat.py
Original file line number Diff line number Diff line change
@@ -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))
89 changes: 89 additions & 0 deletions test/oracle/Oracle_Strings_Bools.thy
Original file line number Diff line number Diff line change
Expand Up @@ -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 \<open>Short-circuiting connectives\<close>

text \<open>A case expression in the right operand of a conjunction, guarded by the left
operand: @{term "hd []"} must not be evaluated.\<close>

definition conj_case_alt :: "nat list \<Rightarrow> bool" where
"conj_case_alt xs = (xs \<noteq> [] \<and> ((case hd xs of 0 \<Rightarrow> 0 | Suc n \<Rightarrow> 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 \<Rightarrow> bool" where
"conj_case_alt2 xs = (xs \<noteq> [] \<and> ((case xs of [] \<Rightarrow> 0 | (x # xs) \<Rightarrow> x) = 0))"

python_check_grid sc_conj_case_alt2: "conj_case_alt2" over "[[], [0], [1], [2], [0, 5], [3, 0]]"

text \<open>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}.\<close>

datatype 'a tree = Leaf | Node "'a tree" 'a "'a tree"

definition conj_case_t :: "nat tree \<Rightarrow> bool" where
"conj_case_t t = (t \<noteq> Leaf \<and> ((case t of Leaf \<Rightarrow> 0 | Node l y r \<Rightarrow> 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 \<open>The other connectives, with a case of type @{typ nat} in the right operand.\<close>

definition disj_case_alt :: "nat list \<Rightarrow> bool" where
"disj_case_alt xs = (xs = [] \<or> (case hd xs of 0 \<Rightarrow> 1 | Suc n \<Rightarrow> n) = 1)"

definition impl_case_alt :: "nat list \<Rightarrow> bool" where
"impl_case_alt xs = (xs \<noteq> [] \<longrightarrow> (case hd xs of 0 \<Rightarrow> 0 | Suc n \<Rightarrow> n) = 0)"

definition not_disj_case :: "nat list \<Rightarrow> bool" where
"not_disj_case xs = (\<not> (xs = [] \<or> (case hd xs of 0 \<Rightarrow> False | Suc _ \<Rightarrow> 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 \<open>Each guard protects the operands after it.\<close>

definition conj_chain :: "nat list \<Rightarrow> bool" where
"conj_chain xs = (xs \<noteq> [] \<and> tl xs \<noteq> [] \<and> (case hd (tl xs) of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False))"

definition disj_conj :: "nat list \<Rightarrow> bool" where
"disj_conj xs = (xs = [] \<or> (tl xs \<noteq> [] \<and> (case hd (tl xs) of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False)))"

definition case_conj_case :: "nat list \<Rightarrow> bool" where
"case_conj_case xs = ((case xs of [] \<Rightarrow> False | _ \<Rightarrow> True) \<and> (case hd xs of 0 \<Rightarrow> True | Suc _ \<Rightarrow> 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 \<open>Partial operations other than @{const hd}.\<close>

definition conj_nth :: "nat list \<Rightarrow> nat \<Rightarrow> bool" where
"conj_nth xs i = (i < length xs \<and> (case xs ! i of 0 \<Rightarrow> True | Suc n \<Rightarrow> n = 2))"

definition conj_the :: "nat option \<Rightarrow> bool" where
"conj_the x = (x \<noteq> None \<and> (case the x of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False))"

definition conj_option_case :: "nat option list \<Rightarrow> bool" where
"conj_option_case xs = (xs \<noteq> [] \<and> (case hd xs of None \<Rightarrow> False | Some n \<Rightarrow> 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 \<open>The connective in an if condition, in a lambda and in a recursive function.\<close>

definition if_conj :: "nat list \<Rightarrow> nat" where
"if_conj xs = (if xs \<noteq> [] \<and> (case hd xs of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False) then 1 else 2)"

fun zero_heads :: "nat list list \<Rightarrow> bool" where
"zero_heads [] = True"
| "zero_heads (xs # xss) =
((xs \<noteq> [] \<longrightarrow> (case hd xs of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False)) \<and> zero_heads xss)"

python_check_grid sc_if_conj: "if_conj" over "[[], [0], [1]]"
python_check sc_map_lambda:
"map (\<lambda>xs. xs \<noteq> [] \<and> (case hd xs of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False)) [[], [0::nat], [1, 0]]"
python_check sc_filter_lambda:
"filter (\<lambda>xs. xs = [] \<or> (case hd xs of 0 \<Rightarrow> True | Suc _ \<Rightarrow> False)) [[], [0::nat], [1, 0]]"
python_check_grid sc_zero_heads: "zero_heads" over "[[], [[]], [[0], [], [0, 1]], [[], [2]]]"

end
Loading