src/HOL/Data_Structures/AVL_Set.thy
changeset 61581 00d9682e8dd7
parent 61428 5e1938107371
child 61588 1d2907d0ed73
--- a/src/HOL/Data_Structures/AVL_Set.thy	Wed Nov 04 15:07:23 2015 +0100
+++ b/src/HOL/Data_Structures/AVL_Set.thy	Thu Nov 05 08:27:14 2015 +0100
@@ -6,7 +6,7 @@
 section "AVL Tree Implementation of Sets"
 
 theory AVL_Set
-imports Isin2
+imports Cmp Isin2
 begin
 
 type_synonym 'a avl_tree = "('a,nat) tree"
@@ -26,8 +26,8 @@
 definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
 "node l a r = Node (max (ht l) (ht r) + 1) l a r"
 
-definition node_bal_l :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
-"node_bal_l l a r = (
+definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
+"balL l a r = (
   if ht l = ht r + 2 then (case l of 
     Node _ bl b br \<Rightarrow> (if ht bl < ht br
     then case br of
@@ -35,8 +35,8 @@
     else node bl b (node br a r)))
   else node l a r)"
 
-definition node_bal_r :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
-"node_bal_r l a r = (
+definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
+"balR l a r = (
   if ht r = ht l + 2 then (case r of
     Node _ bl b br \<Rightarrow> (if ht bl > ht br
     then case bl of
@@ -44,19 +44,17 @@
     else node (node l a bl) b br))
   else node l a r)"
 
-fun insert :: "'a::order \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
+fun insert :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
 "insert x Leaf = Node 1 Leaf x Leaf" |
-"insert x (Node h l a r) = 
-   (if x=a then Node h l a r
-    else if x<a
-      then node_bal_l (insert x l) a r
-      else node_bal_r l a (insert x r))"
+"insert x (Node h l a r) = (case cmp x a of
+   EQ \<Rightarrow> Node h l a r |
+   LT \<Rightarrow> balL (insert x l) a r |
+   GT \<Rightarrow> balR l a (insert x r))"
 
 fun delete_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
 "delete_max (Node _ l a Leaf) = (l,a)" |
-"delete_max (Node _ l a r) = (
-  let (r',a') = delete_max r in
-  (node_bal_l l a r', a'))"
+"delete_max (Node _ l a r) =
+  (let (r',a') = delete_max r in (balL l a r', a'))"
 
 lemmas delete_max_induct = delete_max.induct[case_names Leaf Node]
 
@@ -64,16 +62,16 @@
 "delete_root (Node h Leaf a r) = r" |
 "delete_root (Node h l a Leaf) = l" |
 "delete_root (Node h l a r) =
-  (let (l', a') = delete_max l in node_bal_r l' a' r)"
+  (let (l', a') = delete_max l in balR l' a' r)"
 
 lemmas delete_root_cases = delete_root.cases[case_names Leaf_t Node_Leaf Node_Node]
 
-fun delete :: "'a::order \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
+fun delete :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
 "delete _ Leaf = Leaf" |
-"delete x (Node h l a r) = (
-   if x = a then delete_root (Node h l a r)
-   else if x < a then node_bal_r (delete x l) a r
-   else node_bal_l l a (delete x r))"
+"delete x (Node h l a r) = (case cmp x a of
+   EQ \<Rightarrow> delete_root (Node h l a r) |
+   LT \<Rightarrow> balR (delete x l) a r |
+   GT \<Rightarrow> balL l a (delete x r))"
 
 
 subsection {* Functional Correctness Proofs *}
@@ -83,18 +81,18 @@
 
 subsubsection "Proofs for insert"
 
-lemma inorder_node_bal_l:
-  "inorder (node_bal_l l a r) = inorder l @ a # inorder r"
-by (auto simp: node_def node_bal_l_def split:tree.splits)
+lemma inorder_balL:
+  "inorder (balL l a r) = inorder l @ a # inorder r"
+by (auto simp: node_def balL_def split:tree.splits)
 
-lemma inorder_node_bal_r:
-  "inorder (node_bal_r l a r) = inorder l @ a # inorder r"
-by (auto simp: node_def node_bal_r_def split:tree.splits)
+lemma inorder_balR:
+  "inorder (balR l a r) = inorder l @ a # inorder r"
+by (auto simp: node_def balR_def split:tree.splits)
 
 theorem inorder_insert:
   "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
 by (induct t) 
-   (auto simp: ins_list_simps inorder_node_bal_l inorder_node_bal_r)
+   (auto simp: ins_list_simps inorder_balL inorder_balR)
 
 
 subsubsection "Proofs for delete"
@@ -103,17 +101,17 @@
   "\<lbrakk> delete_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
    inorder t' @ [a] = inorder t"
 by(induction t arbitrary: t' rule: delete_max.induct)
-  (auto simp: inorder_node_bal_l split: prod.splits tree.split)
+  (auto simp: inorder_balL split: prod.splits tree.split)
 
 lemma inorder_delete_root:
   "inorder (delete_root (Node h l a r)) = inorder l @ inorder r"
 by(induction "Node h l a r" arbitrary: l a r h rule: delete_root.induct)
-  (auto simp: inorder_node_bal_r inorder_delete_maxD split: prod.splits)
+  (auto simp: inorder_balR inorder_delete_maxD split: prod.splits)
 
 theorem inorder_delete:
   "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
 by(induction t)
-  (auto simp: del_list_simps inorder_node_bal_l inorder_node_bal_r
+  (auto simp: del_list_simps inorder_balL inorder_balR
     inorder_delete_root inorder_delete_maxD split: prod.splits)
 
 
@@ -145,17 +143,17 @@
 lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
 by (induct t) simp_all
 
-lemma height_node_bal_l:
+lemma height_balL:
   "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
-   height (node_bal_l l a r) = height r + 2 \<or>
-   height (node_bal_l l a r) = height r + 3"
-by (cases l) (auto simp:node_def node_bal_l_def split:tree.split)
+   height (balL l a r) = height r + 2 \<or>
+   height (balL l a r) = height r + 3"
+by (cases l) (auto simp:node_def balL_def split:tree.split)
        
-lemma height_node_bal_r:
+lemma height_balR:
   "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
-   height (node_bal_r l a r) = height l + 2 \<or>
-   height (node_bal_r l a r) = height l + 3"
-by (cases r) (auto simp add:node_def node_bal_r_def split:tree.split)
+   height (balR l a r) = height l + 2 \<or>
+   height (balR l a r) = height l + 3"
+by (cases r) (auto simp add:node_def balR_def split:tree.split)
 
 lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
 by (simp add: node_def)
@@ -166,53 +164,53 @@
    \<rbrakk> \<Longrightarrow> avl(node l a r)"
 by (auto simp add:max_def node_def)
 
-lemma height_node_bal_l2:
+lemma height_balL2:
   "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
-   height (node_bal_l l a r) = (1 + max (height l) (height r))"
-by (cases l, cases r) (simp_all add: node_bal_l_def)
+   height (balL l a r) = (1 + max (height l) (height r))"
+by (cases l, cases r) (simp_all add: balL_def)
 
-lemma height_node_bal_r2:
+lemma height_balR2:
   "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
-   height (node_bal_r l a r) = (1 + max (height l) (height r))"
-by (cases l, cases r) (simp_all add: node_bal_r_def)
+   height (balR l a r) = (1 + max (height l) (height r))"
+by (cases l, cases r) (simp_all add: balR_def)
 
-lemma avl_node_bal_l: 
+lemma avl_balL: 
   assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
     \<or> height r = height l + 1 \<or> height l = height r + 2" 
-  shows "avl(node_bal_l l a r)"
+  shows "avl(balL l a r)"
 proof(cases l)
   case Leaf
-  with assms show ?thesis by (simp add: node_def node_bal_l_def)
+  with assms show ?thesis by (simp add: node_def balL_def)
 next
   case (Node ln ll lr lh)
   with assms show ?thesis
   proof(cases "height l = height r + 2")
     case True
     from True Node assms show ?thesis
-      by (auto simp: node_bal_l_def intro!: avl_node split: tree.split) arith+
+      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
   next
     case False
-    with assms show ?thesis by (simp add: avl_node node_bal_l_def)
+    with assms show ?thesis by (simp add: avl_node balL_def)
   qed
 qed
 
-lemma avl_node_bal_r: 
+lemma avl_balR: 
   assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
     \<or> height r = height l + 1 \<or> height r = height l + 2" 
-  shows "avl(node_bal_r l a r)"
+  shows "avl(balR l a r)"
 proof(cases r)
   case Leaf
-  with assms show ?thesis by (simp add: node_def node_bal_r_def)
+  with assms show ?thesis by (simp add: node_def balR_def)
 next
   case (Node rn rl rr rh)
   with assms show ?thesis
   proof(cases "height r = height l + 2")
     case True
       from True Node assms show ?thesis
-        by (auto simp: node_bal_r_def intro!: avl_node split: tree.split) arith+
+        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
   next
     case False
-    with assms show ?thesis by (simp add: node_bal_r_def avl_node)
+    with assms show ?thesis by (simp add: balR_def avl_node)
   qed
 qed
 
@@ -237,10 +235,10 @@
     with Node 1 show ?thesis 
     proof(cases "x<a")
       case True
-      with Node 1 show ?thesis by (auto simp add:avl_node_bal_l)
+      with Node 1 show ?thesis by (auto simp add:avl_balL)
     next
       case False
-      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_node_bal_r)
+      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_balR)
     qed
   qed
   case 2
@@ -255,12 +253,12 @@
       case True
       with Node 2 show ?thesis
       proof(cases "height (insert x l) = height r + 2")
-        case False with Node 2 `x < a` show ?thesis by (auto simp: height_node_bal_l2)
+        case False with Node 2 `x < a` show ?thesis by (auto simp: height_balL2)
       next
         case True 
-        hence "(height (node_bal_l (insert x l) a r) = height r + 2) \<or>
-          (height (node_bal_l (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
-          using Node 2 by (intro height_node_bal_l) simp_all
+        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
+          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
+          using Node 2 by (intro height_balL) simp_all
         thus ?thesis
         proof
           assume ?A
@@ -275,12 +273,12 @@
       with Node 2 show ?thesis 
       proof(cases "height (insert x r) = height l + 2")
         case False
-        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_node_bal_r2)
+        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_balR2)
       next
         case True 
-        hence "(height (node_bal_r l a (insert x r)) = height l + 2) \<or>
-          (height (node_bal_r l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
-          using Node 2 by (intro height_node_bal_r) simp_all
+        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
+          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
+          using Node 2 by (intro height_balR) simp_all
         thus ?thesis 
         proof
           assume ?A
@@ -306,10 +304,10 @@
   case (Node h l a rh rl b rr)
   case 1
   with Node have "avl l" "avl (fst (delete_max (Node rh rl b rr)))" by auto
-  with 1 Node have "avl (node_bal_l l a (fst (delete_max (Node rh rl b rr))))"
-    by (intro avl_node_bal_l) fastforce+
+  with 1 Node have "avl (balL l a (fst (delete_max (Node rh rl b rr))))"
+    by (intro avl_balL) fastforce+
   thus ?case 
-    by (auto simp: height_node_bal_l height_node_bal_l2
+    by (auto simp: height_balL height_balL2
       linorder_class.max.absorb1 linorder_class.max.absorb2
       split:prod.split)
 next
@@ -318,7 +316,7 @@
   let ?r = "Node rh rl b rr"
   let ?r' = "fst (delete_max ?r)"
   from `avl x` Node 2 have "avl l" and "avl ?r" by simp_all
-  thus ?case using Node 2 height_node_bal_l[of l ?r' a] height_node_bal_l2[of l ?r' a]
+  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
     apply (auto split:prod.splits simp del:avl.simps) by arith+
 qed auto
 
@@ -337,8 +335,8 @@
          height ?l = height(?l') + 1" by (rule avl_delete_max,simp)+
   with `avl t` Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
             \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
-  with `avl ?l'` `avl ?r` have "avl(node_bal_r ?l' (snd(delete_max ?l)) ?r)"
-    by (rule avl_node_bal_r)
+  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(delete_max ?l)) ?r)"
+    by (rule avl_balR)
   with Node_Node show ?thesis by (auto split:prod.splits)
 qed simp_all
 
@@ -351,7 +349,7 @@
   let ?l = "Node lh ll ln lr"
   let ?r = "Node rh rl rn rr"
   let ?l' = "fst (delete_max ?l)"
-  let ?t' = "node_bal_r ?l' (snd(delete_max ?l)) ?r"
+  let ?t' = "balR ?l' (snd(delete_max ?l)) ?r"
   from `avl t` and Node_Node have "avl ?r" by simp
   from `avl t` and Node_Node have "avl ?l" by simp
   hence "avl(?l')"  by (rule avl_delete_max,simp)
@@ -360,11 +358,11 @@
   have "height t = height ?t' \<or> height t = height ?t' + 1" using  `avl t` Node_Node
   proof(cases "height ?r = height ?l' + 2")
     case False
-    show ?thesis using l'_height t_height False by (subst  height_node_bal_r2[OF `avl ?l'` `avl ?r` False])+ arith
+    show ?thesis using l'_height t_height False by (subst  height_balR2[OF `avl ?l'` `avl ?r` False])+ arith
   next
     case True
     show ?thesis
-    proof(cases rule: disjE[OF height_node_bal_r[OF True `avl ?l'` `avl ?r`, of "snd (delete_max ?l)"]])
+    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (delete_max ?l)"]])
       case 1
       thus ?thesis using l'_height t_height True by arith
     next
@@ -393,10 +391,10 @@
     with Node 1 show ?thesis 
     proof(cases "x<n")
       case True
-      with Node 1 show ?thesis by (auto simp add:avl_node_bal_r)
+      with Node 1 show ?thesis by (auto simp add:avl_balR)
     next
       case False
-      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_node_bal_l)
+      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_balL)
     qed
   qed
   case 2
@@ -414,38 +412,38 @@
       case True
       show ?thesis
       proof(cases "height r = height (delete x l) + 2")
-        case False with Node 1 `x < n` show ?thesis by(auto simp: node_bal_r_def)
+        case False with Node 1 `x < n` show ?thesis by(auto simp: balR_def)
       next
         case True 
-        hence "(height (node_bal_r (delete x l) n r) = height (delete x l) + 2) \<or>
-          height (node_bal_r (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
-          using Node 2 by (intro height_node_bal_r) auto
+        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
+          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
+          using Node 2 by (intro height_balR) auto
         thus ?thesis 
         proof
           assume ?A
-          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
+          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
         next
           assume ?B
-          with `x < n` Node 2 show ?thesis by(auto simp: node_bal_r_def)
+          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
         qed
       qed
     next
       case False
       show ?thesis
       proof(cases "height l = height (delete x r) + 2")
-        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: node_bal_l_def)
+        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: balL_def)
       next
         case True 
-        hence "(height (node_bal_l l n (delete x r)) = height (delete x r) + 2) \<or>
-          height (node_bal_l l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
-          using Node 2 by (intro height_node_bal_l) auto
+        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
+          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
+          using Node 2 by (intro height_balL) auto
         thus ?thesis 
         proof
           assume ?A
-          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: node_bal_l_def)
+          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
         next
           assume ?B
-          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: node_bal_l_def)
+          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
         qed
       qed
     qed