diff -r c5232e6fb10b -r 3fb16bed5d6c src/HOL/Data_Structures/Tree2.thy --- a/src/HOL/Data_Structures/Tree2.thy Tue Sep 24 17:36:14 2019 +0200 +++ b/src/HOL/Data_Structures/Tree2.thy Wed Sep 25 17:22:57 2019 +0200 @@ -1,40 +1,29 @@ theory Tree2 -imports Main +imports "HOL-Library.Tree" begin -datatype ('a,'b) tree = - Leaf ("\\") | - Node "('a,'b)tree" 'a 'b "('a,'b) tree" ("(1\_,/ _,/ _,/ _\)") - -fun inorder :: "('a,'b)tree \ 'a list" where -"inorder Leaf = []" | -"inorder (Node l a _ r) = inorder l @ a # inorder r" +text \This theory provides the basic infrastructure for the type @{typ \('a * 'b) tree\} +of augmented trees where @{typ 'a} is the key and @{typ 'b} some additional information.\ -fun height :: "('a,'b) tree \ nat" where -"height Leaf = 0" | -"height (Node l a _ r) = max (height l) (height r) + 1" +text \IMPORTANT: Inductions and cases analyses on augmented trees need to use the following +two rules explicitly. They generate nodes of the form @{term "Node l (a,b) r"} +rather than @{term "Node l a r"} for trees of type @{typ "'a tree"}.\ -fun set_tree :: "('a,'b) tree \ 'a set" where -"set_tree Leaf = {}" | -"set_tree (Node l a _ r) = Set.insert a (set_tree l \ set_tree r)" +lemmas tree2_induct = tree.induct[where 'a = "'a * 'b", split_format(complete)] + +lemmas tree2_cases = tree.exhaust[where 'a = "'a * 'b", split_format(complete)] -fun bst :: "('a::linorder,'b) tree \ bool" where -"bst Leaf = True" | -"bst (Node l a _ r) = (bst l \ bst r \ (\x \ set_tree l. x < a) \ (\x \ set_tree r. a < x))" - -fun size1 :: "('a,'b) tree \ nat" where -"size1 \\ = 1" | -"size1 \l, _, _, r\ = size1 l + size1 r" +fun inorder :: "('a*'b)tree \ 'a list" where +"inorder Leaf = []" | +"inorder (Node l (a,_) r) = inorder l @ a # inorder r" -fun complete :: "('a,'b) tree \ bool" where -"complete Leaf = True" | -"complete (Node l _ _ r) = (complete l \ complete r \ height l = height r)" +fun set_tree :: "('a*'b) tree \ 'a set" where +"set_tree Leaf = {}" | +"set_tree (Node l (a,_) r) = Set.insert a (set_tree l \ set_tree r)" -lemma size1_size: "size1 t = size t + 1" -by (induction t) simp_all - -lemma size1_ge0[simp]: "0 < size1 t" -by (simp add: size1_size) +fun bst :: "('a::linorder*'b) tree \ bool" where +"bst Leaf = True" | +"bst (Node l (a, _) r) = (bst l \ bst r \ (\x \ set_tree l. x < a) \ (\x \ set_tree r. a < x))" lemma finite_set_tree[simp]: "finite(set_tree t)" by(induction t) auto