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: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions ROOT
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down
48 changes: 48 additions & 0 deletions examples/Example_tree_weight.thy
Original file line number Diff line number Diff line change
@@ -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 \<Rightarrow> 'a tree \<Rightarrow> '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 \<Rightarrow> 'a tree \<Rightarrow> 'a list" where
"root_or d t = [case t of Leaf \<Rightarrow> d | Node _ x _ \<Rightarrow> x]"

fun fold_tree :: "('b \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a tree \<Rightarrow> '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 \<Rightarrow> 'a tree" where
"from_list xs = foldr ins xs Leaf"

class weight =
fixes calc_weight :: "'a \<Rightarrow> nat"

instantiation nat :: weight
begin
definition calc_weight_nat :: "nat \<Rightarrow> nat" where
"calc_weight_nat n = n"
instance ..
end

instantiation tree :: (weight) weight
begin
definition calc_weight_tree :: "'a tree \<Rightarrow> nat" where
"calc_weight_tree = fold_tree (\<lambda>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
Original file line number Diff line number Diff line change
@@ -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())))
12 changes: 12 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/Fun.py
Original file line number Diff line number Diff line change
@@ -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)))
17 changes: 17 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/List.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 17 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/Num.py
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions test/diff/golden/Python_Examples.Example_tree_weight/Orderings.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/oracle/Oracle_Data.thy
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ python_check l_map_pair: "map (\<lambda>(a, b). a + b) [(1::int, 2), (3, 4)]"
python_check l_list_all: "(list_all (\<lambda>x. x > 0) [1::int, 2], list_ex (\<lambda>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]"
Expand Down
Loading