# HG changeset patch # User nipkow # Date 1588754779 -7200 # Node ID 986d5abbe77c188a4534c8f7c6b44d50916a9602 # Parent e8e0313881b9a6db51fb17565a22f21457469623 tuned diff -r e8e0313881b9 -r 986d5abbe77c src/HOL/Data_Structures/AVL_Bal_Set.thy --- a/src/HOL/Data_Structures/AVL_Bal_Set.thy Tue May 05 20:32:57 2020 +0200 +++ b/src/HOL/Data_Structures/AVL_Bal_Set.thy Wed May 06 10:46:19 2020 +0200 @@ -1,6 +1,6 @@ (* Tobias Nipkow *) -section "AVL Tree with Balance Tags (Set Implementation)" +section "AVL Tree with Balance Factors" theory AVL_Bal_Set imports @@ -9,7 +9,6 @@ begin datatype bal = Lh | Bal | Rh -(* Exercise: use 3 Node constructors instead *) type_synonym 'a tree_bal = "('a * bal) tree" @@ -27,9 +26,9 @@ subsection \Code\ -datatype 'a change = Same 'a | Diff 'a +datatype 'a tree_bal2 = Same "'a tree_bal" | Diff "'a tree_bal" -fun tree :: "'a change \ 'a" where +fun tree :: "'a tree_bal2 \ 'a tree_bal" where "tree(Same t) = t" | "tree(Diff t) = t" @@ -39,7 +38,7 @@ fun rot22 :: "bal \ bal" where "rot22 b = (if b=Lh then Rh else Bal)" -fun balL :: "'a tree_bal change \ 'a \ bal \ 'a tree_bal \ 'a tree_bal change" where +fun balL :: "'a tree_bal2 \ 'a \ bal \ 'a tree_bal \ 'a tree_bal2" where "balL AB' c bc C = (case AB' of Same AB \ Same (Node AB (c,bc) C) | Diff AB \ (case bc of @@ -51,7 +50,7 @@ Node B\<^sub>1 (b, bb) B\<^sub>2 \ Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))" -fun balR :: "'a tree_bal \ 'a \ bal \ 'a tree_bal change \ 'a tree_bal change" where +fun balR :: "'a tree_bal \ 'a \ bal \ 'a tree_bal2 \ 'a tree_bal2" where "balR A a ba BC' = (case BC' of Same BC \ Same (Node A (a,ba) BC) | Diff BC \ (case ba of @@ -63,14 +62,14 @@ Node B\<^sub>1 (b, bb) B\<^sub>2 \ Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C)))))" -fun insert :: "'a::linorder \ 'a tree_bal \ 'a tree_bal change" where +fun insert :: "'a::linorder \ 'a tree_bal \ 'a tree_bal2" where "insert x Leaf = Diff(Node Leaf (x, Bal) Leaf)" | "insert x (Node l (a, b) r) = (case cmp x a of EQ \ Same(Node l (a, b) r) | LT \ balL (insert x l) a b r | GT \ balR l a b (insert x r))" -fun baldR :: "'a tree_bal \ 'a \ bal \ 'a tree_bal change \ 'a tree_bal change" where +fun baldR :: "'a tree_bal \ 'a \ bal \ 'a tree_bal2 \ 'a tree_bal2" where "baldR AB c bc C' = (case C' of Same C \ Same (Node AB (c,bc) C) | Diff C \ (case bc of @@ -83,7 +82,7 @@ Node B\<^sub>1 (b, bb) B\<^sub>2 \ Diff(Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C))))))" -fun baldL :: "'a tree_bal change \ 'a \ bal \ 'a tree_bal \ 'a tree_bal change" where +fun baldL :: "'a tree_bal2 \ 'a \ bal \ 'a tree_bal \ 'a tree_bal2" where "baldL A' a ba BC = (case A' of Same A \ Same (Node A (a,ba) BC) | Diff A \ (case ba of @@ -96,11 +95,11 @@ Node B\<^sub>1 (b, bb) B\<^sub>2 \ Diff(Node (Node A (a,rot21 bb) B\<^sub>1) (b,Bal) (Node B\<^sub>2 (c,rot22 bb) C))))))" -fun split_max :: "'a tree_bal \ 'a tree_bal change * 'a" where +fun split_max :: "'a tree_bal \ 'a tree_bal2 * 'a" where "split_max (Node l (a, ba) r) = (if r = Leaf then (Diff l,a) else let (r',a') = split_max r in (baldR l a ba r', a'))" -fun delete :: "'a::linorder \ 'a tree_bal \ 'a tree_bal change" where +fun delete :: "'a::linorder \ 'a tree_bal \ 'a tree_bal2" where "delete _ Leaf = Same Leaf" | "delete x (Node l (a, ba) r) = (case cmp x a of @@ -111,7 +110,7 @@ lemmas split_max_induct = split_max.induct[case_names Node Leaf] -lemmas splits = if_splits tree.splits tree.splits change.splits bal.splits +lemmas splits = if_splits tree.splits tree.splits tree_bal2.splits bal.splits subsection \Proofs\ diff -r e8e0313881b9 -r 986d5abbe77c src/HOL/Data_Structures/AVL_Map.thy --- a/src/HOL/Data_Structures/AVL_Map.thy Tue May 05 20:32:57 2020 +0200 +++ b/src/HOL/Data_Structures/AVL_Map.thy Wed May 06 10:46:19 2020 +0200 @@ -8,14 +8,14 @@ Lookup2 begin -fun update :: "'a::linorder \ 'b \ ('a*'b) avl_tree \ ('a*'b) avl_tree" where +fun update :: "'a::linorder \ 'b \ ('a*'b) tree_ht \ ('a*'b) tree_ht" where "update x y Leaf = Node Leaf ((x,y), 1) Leaf" | "update x y (Node l ((a,b), h) r) = (case cmp x a of EQ \ Node l ((x,y), h) r | LT \ balL (update x y l) (a,b) r | GT \ balR l (a,b) (update x y r))" -fun delete :: "'a::linorder \ ('a*'b) avl_tree \ ('a*'b) avl_tree" where +fun delete :: "'a::linorder \ ('a*'b) tree_ht \ ('a*'b) tree_ht" where "delete _ Leaf = Leaf" | "delete x (Node l ((a,b), h) r) = (case cmp x a of EQ \ if l = Leaf then r diff -r e8e0313881b9 -r 986d5abbe77c src/HOL/Data_Structures/AVL_Set.thy --- a/src/HOL/Data_Structures/AVL_Set.thy Tue May 05 20:32:57 2020 +0200 +++ b/src/HOL/Data_Structures/AVL_Set.thy Wed May 06 10:46:19 2020 +0200 @@ -8,7 +8,7 @@ "HOL-Number_Theory.Fib" begin -fun avl :: "'a avl_tree \ bool" where +fun avl :: "'a tree_ht \ bool" where "avl Leaf = True" | "avl (Node l (a,n) r) = (abs(int(height l) - int(height r)) \ 1 \ @@ -26,16 +26,12 @@ lemma height_balL: "\ avl l; avl r; height l = height r + 2 \ \ height (balL l a r) \ {height r + 2, height r + 3}" -apply (cases l) - apply (auto simp:node_def balL_def split:tree.split) - by arith+ +by (auto simp:node_def balL_def split:tree.split) lemma height_balR: "\ avl l; avl r; height r = height l + 2 \ \ height (balR l a r) : {height l + 2, height l + 3}" -apply (cases r) - apply(auto simp add:node_def balR_def split:tree.split) - by arith+ +by(auto simp add:node_def balR_def split:tree.split) lemma height_node[simp]: "height(node l a r) = max (height l) (height r) + 1" by (simp add: node_def) diff -r e8e0313881b9 -r 986d5abbe77c src/HOL/Data_Structures/AVL_Set_Code.thy --- a/src/HOL/Data_Structures/AVL_Set_Code.thy Tue May 05 20:32:57 2020 +0200 +++ b/src/HOL/Data_Structures/AVL_Set_Code.thy Wed May 06 10:46:19 2020 +0200 @@ -13,19 +13,19 @@ subsection \Code\ -type_synonym 'a avl_tree = "('a*nat) tree" +type_synonym 'a tree_ht = "('a*nat) tree" -definition empty :: "'a avl_tree" where +definition empty :: "'a tree_ht" where "empty = Leaf" -fun ht :: "'a avl_tree \ nat" where +fun ht :: "'a tree_ht \ nat" where "ht Leaf = 0" | "ht (Node l (a,n) r) = n" -definition node :: "'a avl_tree \ 'a \ 'a avl_tree \ 'a avl_tree" where +definition node :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "node l a r = Node l (a, max (ht l) (ht r) + 1) r" -definition balL :: "'a avl_tree \ 'a \ 'a avl_tree \ 'a avl_tree" where +definition balL :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "balL AB c C = (if ht AB = ht C + 2 then case AB of @@ -36,7 +36,7 @@ Node B\<^sub>1 (b, _) B\<^sub>2 \ node (node A a B\<^sub>1) b (node B\<^sub>2 c C) else node AB c C)" -definition balR :: "'a avl_tree \ 'a \ 'a avl_tree \ 'a avl_tree" where +definition balR :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "balR A a BC = (if ht BC = ht A + 2 then case BC of @@ -47,20 +47,20 @@ Node B\<^sub>1 (b, _) B\<^sub>2 \ node (node A a B\<^sub>1) b (node B\<^sub>2 c C) else node A a BC)" -fun insert :: "'a::linorder \ 'a avl_tree \ 'a avl_tree" where +fun insert :: "'a::linorder \ 'a tree_ht \ 'a tree_ht" where "insert x Leaf = Node Leaf (x, 1) Leaf" | "insert x (Node l (a, n) r) = (case cmp x a of EQ \ Node l (a, n) r | LT \ balL (insert x l) a r | GT \ balR l a (insert x r))" -fun split_max :: "'a avl_tree \ 'a avl_tree * 'a" where +fun split_max :: "'a tree_ht \ 'a tree_ht * 'a" where "split_max (Node l (a, _) r) = (if r = Leaf then (l,a) else let (r',a') = split_max r in (balL l a r', a'))" lemmas split_max_induct = split_max.induct[case_names Node Leaf] -fun delete :: "'a::linorder \ 'a avl_tree \ 'a avl_tree" where +fun delete :: "'a::linorder \ 'a tree_ht \ 'a tree_ht" where "delete _ Leaf = Leaf" | "delete x (Node l (a, n) r) = (case cmp x a of diff -r e8e0313881b9 -r 986d5abbe77c src/HOL/Data_Structures/Height_Balanced_Tree.thy --- a/src/HOL/Data_Structures/Height_Balanced_Tree.thy Tue May 05 20:32:57 2020 +0200 +++ b/src/HOL/Data_Structures/Height_Balanced_Tree.thy Wed May 06 10:46:19 2020 +0200 @@ -12,9 +12,9 @@ The code and the proofs were obtained by small modifications of the AVL theories. This is an implementation of sets via HBTs.\ -type_synonym 'a hbt = "('a*nat) tree" +type_synonym 'a tree_ht = "('a*nat) tree" -definition empty :: "'a hbt" where +definition empty :: "'a tree_ht" where "empty = Leaf" text \The maximal amount by which the height of two siblings may differ:\ @@ -26,20 +26,20 @@ text \Invariant:\ -fun hbt :: "'a hbt \ bool" where +fun hbt :: "'a tree_ht \ bool" where "hbt Leaf = True" | "hbt (Node l (a,n) r) = (abs(int(height l) - int(height r)) \ int(m) \ n = max (height l) (height r) + 1 \ hbt l \ hbt r)" -fun ht :: "'a hbt \ nat" where +fun ht :: "'a tree_ht \ nat" where "ht Leaf = 0" | "ht (Node l (a,n) r) = n" -definition node :: "'a hbt \ 'a \ 'a hbt \ 'a hbt" where +definition node :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "node l a r = Node l (a, max (ht l) (ht r) + 1) r" -definition balL :: "'a hbt \ 'a \ 'a hbt \ 'a hbt" where +definition balL :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "balL AB b C = (if ht AB = ht C + m + 1 then case AB of @@ -50,7 +50,7 @@ Node B\<^sub>1 (ab, _) B\<^sub>2 \ node (node A a B\<^sub>1) ab (node B\<^sub>2 b C) else node AB b C)" -definition balR :: "'a hbt \ 'a \ 'a hbt \ 'a hbt" where +definition balR :: "'a tree_ht \ 'a \ 'a tree_ht \ 'a tree_ht" where "balR A a BC = (if ht BC = ht A + m + 1 then case BC of @@ -61,20 +61,20 @@ Node B\<^sub>1 (ab, _) B\<^sub>2 \ node (node A a B\<^sub>1) ab (node B\<^sub>2 b C) else node A a BC)" -fun insert :: "'a::linorder \ 'a hbt \ 'a hbt" where +fun insert :: "'a::linorder \ 'a tree_ht \ 'a tree_ht" where "insert x Leaf = Node Leaf (x, 1) Leaf" | "insert x (Node l (a, n) r) = (case cmp x a of EQ \ Node l (a, n) r | LT \ balL (insert x l) a r | GT \ balR l a (insert x r))" -fun split_max :: "'a hbt \ 'a hbt * 'a" where +fun split_max :: "'a tree_ht \ 'a tree_ht * 'a" where "split_max (Node l (a, _) r) = (if r = Leaf then (l,a) else let (r',a') = split_max r in (balL l a r', a'))" lemmas split_max_induct = split_max.induct[case_names Node Leaf] -fun delete :: "'a::linorder \ 'a hbt \ 'a hbt" where +fun delete :: "'a::linorder \ 'a tree_ht \ 'a tree_ht" where "delete _ Leaf = Leaf" | "delete x (Node l (a, n) r) = (case cmp x a of @@ -132,16 +132,12 @@ lemma height_balL: "\ hbt l; hbt r; height l = height r + m + 1 \ \ height (balL l a r) \ {height r + m + 1, height r + m + 2}" -apply (cases l) - apply (auto simp:node_def balL_def split:tree.split) - by arith+ +by (auto simp:node_def balL_def split:tree.split) lemma height_balR: "\ hbt l; hbt r; height r = height l + m + 1 \ \ height (balR l a r) \ {height l + m + 1, height l + m + 2}" -apply (cases r) - apply(auto simp add:node_def balR_def split:tree.split) - by arith+ +by(auto simp add:node_def balR_def split:tree.split) lemma height_node[simp]: "height(node l a r) = max (height l) (height r) + 1" by (simp add: node_def) @@ -149,22 +145,20 @@ lemma height_balL2: "\ hbt l; hbt r; height l \ height r + m + 1 \ \ height (balL l a r) = 1 + max (height l) (height r)" -by (cases l, cases r) (simp_all add: balL_def) +by (simp_all add: balL_def) lemma height_balR2: "\ hbt l; hbt r; height r \ height l + m + 1 \ \ height (balR l a r) = 1 + max (height l) (height r)" -by (cases l, cases r) (simp_all add: balR_def) +by (simp_all add: balR_def) lemma hbt_balL: "\ hbt l; hbt r; height r - m \ height l \ height l \ height r + m + 1 \ \ hbt(balL l a r)" -by(cases l, cases r) - (auto simp: balL_def node_def max_def split!: if_splits tree.split) +by(auto simp: balL_def node_def max_def split!: if_splits tree.split) lemma hbt_balR: "\ hbt l; hbt r; height l - m \ height r \ height r \ height l + m + 1 \ \ hbt(balR l a r)" -by(cases r, cases l) - (auto simp: balR_def node_def max_def split!: if_splits tree.split) +by(auto simp: balR_def node_def max_def split!: if_splits tree.split) text\Insertion maintains @{const hbt}. Requires simultaneous proof.\