src/HOL/Data_Structures/Tree234_Map.thy
changeset 61581 00d9682e8dd7
parent 61515 c64628dbac00
child 61640 44c9198f210c
--- a/src/HOL/Data_Structures/Tree234_Map.thy	Wed Nov 04 15:07:23 2015 +0100
+++ b/src/HOL/Data_Structures/Tree234_Map.thy	Thu Nov 05 08:27:14 2015 +0100
@@ -10,118 +10,105 @@
 
 subsection \<open>Map operations on 2-3-4 trees\<close>
 
-fun lookup :: "('a::linorder * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
+fun lookup :: "('a::cmp * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
 "lookup Leaf x = None" |
-"lookup (Node2 l (a,b) r) x =
-  (if x < a then lookup l x else
-  if a < x then lookup r x else Some b)" |
-"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
-  (if x < a1 then lookup l x else
-   if x = a1 then Some b1 else
-   if x < a2 then lookup m x else
-   if x = a2 then Some b2
-   else lookup r x)" |
-"lookup (Node4 l (a1,b1) m (a2,b2) n (a3,b3) r) x =
-  (if x < a2 then
-     if x = a1 then Some b1 else
-     if x < a1 then lookup l x else lookup m x
-   else
-     if x = a2 then Some b2 else
-     if x = a3 then Some b3 else
-     if x < a3 then lookup n x
-     else lookup r x)"
+"lookup (Node2 l (a,b) r) x = (case cmp x a of
+  LT \<Rightarrow> lookup l x |
+  GT \<Rightarrow> lookup r x |
+  EQ \<Rightarrow> Some b)" |
+"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
+  LT \<Rightarrow> lookup l x |
+  EQ \<Rightarrow> Some b1 |
+  GT \<Rightarrow> (case cmp x a2 of
+          LT \<Rightarrow> lookup m x |
+          EQ \<Rightarrow> Some b2 |
+          GT \<Rightarrow> lookup r x))" |
+"lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of
+  LT \<Rightarrow> (case cmp x a1 of
+           LT \<Rightarrow> lookup t1 x | EQ \<Rightarrow> Some b1 | GT \<Rightarrow> lookup t2 x) |
+  EQ \<Rightarrow> Some b2 |
+  GT \<Rightarrow> (case cmp x a3 of
+           LT \<Rightarrow> lookup t3 x | EQ \<Rightarrow> Some b3 | GT \<Rightarrow> lookup t4 x))"
 
-fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
+fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
 "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
-"upd x y (Node2 l ab r) =
-   (if x < fst ab then
-        (case upd x y l of
+"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
+   LT \<Rightarrow> (case upd x y l of
            T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
-         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 ab r))
-    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
-    else
-        (case upd x y r of
+         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
+   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
+   GT \<Rightarrow> (case upd x y r of
            T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
-         | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l ab r1 q r2)))" |
-"upd x y (Node3 l ab1 m ab2 r) =
-   (if x < fst ab1 then
-        (case upd x y l of
+         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
+"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
+   LT \<Rightarrow> (case upd x y l of
            T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
-         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node2 m ab2 r))
-    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
-    else if x < fst ab2 then
-             (case upd x y m of
-                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
-              | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node2 m2 ab2 r))
-         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
-         else
-             (case upd x y r of
-                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
-              | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 q r2)))" |
-"upd x y (Node4 l ab1 m ab2 n ab3 r) =
-   (if x < fst ab2 then
-      if x < fst ab1 then
-        (case upd x y l of
-           T\<^sub>i l' => T\<^sub>i (Node4 l' ab1 m ab2 n ab3 r)
-         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node3 m ab2 n ab3 r))
-      else
-      if x = fst ab1 then T\<^sub>i (Node4 l (x,y) m ab2 n ab3 r)
-      else
-        (case upd x y m of
-           T\<^sub>i m' => T\<^sub>i (Node4 l ab1 m' ab2 n ab3 r)
-         | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node3 m2 ab2 n ab3 r))
-    else
-    if x = fst ab2 then T\<^sub>i (Node4 l ab1 m (x,y) n ab3 r) else
-    if x < fst ab3 then
-      (case upd x y n of
-         T\<^sub>i n' => T\<^sub>i (Node4 l ab1 m ab2 n' ab3 r)
-       | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l ab1 m) ab2(*q*) (Node3 n1 q n2 ab3 r))
-    else
-    if x = fst ab3 then T\<^sub>i (Node4 l ab1 m ab2 n (x,y) r)
-    else
-      (case upd x y r of
-         T\<^sub>i r' => T\<^sub>i (Node4 l ab1 m ab2 n ab3 r')
-       | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node3 n ab3 r1 q r2)))"
+         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
+   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
+   GT \<Rightarrow> (case cmp x (fst ab2) of
+           LT \<Rightarrow> (case upd x y m of
+                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
+                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
+           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
+           GT \<Rightarrow> (case upd x y r of
+                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
+                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" |
+"upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
+   LT \<Rightarrow> (case cmp x (fst ab1) of
+            LT \<Rightarrow> (case upd x y t1 of
+                     T\<^sub>i t1' => T\<^sub>i (Node4 t1' ab1 t2 ab2 t3 ab3 t4)
+                  | Up\<^sub>i t11 q t12 => Up\<^sub>i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) |
+            EQ \<Rightarrow> T\<^sub>i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) |
+            GT \<Rightarrow> (case upd x y t2 of
+                    T\<^sub>i t2' => T\<^sub>i (Node4 t1 ab1 t2' ab2 t3 ab3 t4)
+                  | Up\<^sub>i t21 q t22 => Up\<^sub>i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) |
+   EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) |
+   GT \<Rightarrow> (case cmp x (fst ab3) of
+            LT \<Rightarrow> (case upd x y t3 of
+                    T\<^sub>i t3' \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3' ab3 t4)
+                  | Up\<^sub>i t31 q t32 => Up\<^sub>i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) |
+            EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) |
+            GT \<Rightarrow> (case upd x y t4 of
+                    T\<^sub>i t4' => T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 ab3 t4')
+                  | Up\<^sub>i t41 q t42 => Up\<^sub>i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))"
+
+definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
+"update x y t = tree\<^sub>i(upd x y t)"
 
-definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
-"update a b t = tree\<^sub>i(upd a b t)"
-
-fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d"
-where
-"del k Leaf = T\<^sub>d Leaf" |
-"del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
-"del k (Node3 Leaf p Leaf q Leaf) =
-  T\<^sub>d(if k=fst p then Node2 Leaf q Leaf else
-     if k=fst q then Node2 Leaf p Leaf
-     else Node3 Leaf p Leaf q Leaf)" |
-"del k (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
-  T\<^sub>d(if k=fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
-     if k=fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
-     if k=fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
+fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" where
+"del x Leaf = T\<^sub>d Leaf" |
+"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
+"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
+  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
+"del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
+  T\<^sub>d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
+     if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
+     if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
      else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
-"del k (Node2 l a r) =
-  (if k<fst a then node21 (del k l) a r else
-   if k > fst a then node22 l a (del k r)
-   else let (a',t) = del_min r in node22 l a' t)" |
-"del k (Node3 l a m b r) =
-  (if k<fst a then node31 (del k l) a m b r else
-   if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else
-   if k < fst b then node32 l a (del k m) b r else
-   if k = fst b then let (b',r') = del_min r in node33 l a m b' r'
-   else node33 l a m b (del k r))" |
-"del x (Node4 l ab1 m ab2 n ab3 r) =
-  (if x < fst ab2 then
-     if x < fst ab1 then node41 (del x l) ab1 m ab2 n ab3 r else
-     if x = fst ab1 then let (ab',m') = del_min m in node42 l ab' m' ab2 n ab3 r
-     else node42 l ab1 (del x m) ab2 n ab3 r
-   else
-     if x = fst ab2 then let (ab',n') = del_min n in node43 l ab1 m ab' n' ab3 r else
-     if x < fst ab3 then node43 l ab1 m ab2 (del x n) ab3 r else
-     if x = fst ab3 then let (ab',r') = del_min r in node44 l ab1 m ab2 n ab' r'
-     else node44 l ab1 m ab2 n ab3 (del x r))"
+"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
+  LT \<Rightarrow> node21 (del x l) ab1 r |
+  GT \<Rightarrow> node22 l ab1 (del x r) |
+  EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" |
+"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
+  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
+  EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r |
+  GT \<Rightarrow> (case cmp x (fst ab2) of
+           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
+           EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' |
+           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))" |
+"del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
+  LT \<Rightarrow> (case cmp x (fst ab1) of
+           LT \<Rightarrow> node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 |
+           EQ \<Rightarrow> let (ab',t2') = del_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 |
+           GT \<Rightarrow> node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) |
+  EQ \<Rightarrow> let (ab',t3') = del_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 |
+  GT \<Rightarrow> (case cmp x (fst ab3) of
+          LT \<Rightarrow> node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 |
+          EQ \<Rightarrow> let (ab',t4') = del_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' |
+          GT \<Rightarrow> node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))"
 
-definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
-"delete k t = tree\<^sub>d(del k t)"
+definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
+"delete x t = tree\<^sub>d(del x t)"
 
 
 subsection "Functional correctness"
@@ -144,7 +131,7 @@
   inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
 by(induction t rule: del.induct)
   ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+
-(* 290 secs (2015) *)
+(* 200 secs (2015) *)
 
 lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
   inorder(delete x t) = del_list x (inorder t)"
@@ -154,7 +141,7 @@
 subsection \<open>Balancedness\<close>
 
 lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
-by (induct t) (auto, auto split: up\<^sub>i.split) (* 33 secs (2015) *)
+by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
 
 lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)"
 by (simp add: update_def bal_upd)
@@ -163,11 +150,12 @@
 lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
 by(induction x t rule: del.induct)
   (auto simp add: heights height_del_min split: prod.split)
+(* 20 secs (2015) *)
 
 lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
 by(induction x t rule: del.induct)
   (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
-(* 110 secs (2015) *)
+(* 100 secs (2015) *)
 
 corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
 by(simp add: delete_def bal_tree\<^sub>d_del)