src/HOL/Data_Structures/Tree234_Map.thy
changeset 61581 00d9682e8dd7
parent 61515 c64628dbac00
child 61640 44c9198f210c
     1.1 --- a/src/HOL/Data_Structures/Tree234_Map.thy	Wed Nov 04 15:07:23 2015 +0100
     1.2 +++ b/src/HOL/Data_Structures/Tree234_Map.thy	Thu Nov 05 08:27:14 2015 +0100
     1.3 @@ -10,118 +10,105 @@
     1.4  
     1.5  subsection \<open>Map operations on 2-3-4 trees\<close>
     1.6  
     1.7 -fun lookup :: "('a::linorder * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
     1.8 +fun lookup :: "('a::cmp * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
     1.9  "lookup Leaf x = None" |
    1.10 -"lookup (Node2 l (a,b) r) x =
    1.11 -  (if x < a then lookup l x else
    1.12 -  if a < x then lookup r x else Some b)" |
    1.13 -"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
    1.14 -  (if x < a1 then lookup l x else
    1.15 -   if x = a1 then Some b1 else
    1.16 -   if x < a2 then lookup m x else
    1.17 -   if x = a2 then Some b2
    1.18 -   else lookup r x)" |
    1.19 -"lookup (Node4 l (a1,b1) m (a2,b2) n (a3,b3) r) x =
    1.20 -  (if x < a2 then
    1.21 -     if x = a1 then Some b1 else
    1.22 -     if x < a1 then lookup l x else lookup m x
    1.23 -   else
    1.24 -     if x = a2 then Some b2 else
    1.25 -     if x = a3 then Some b3 else
    1.26 -     if x < a3 then lookup n x
    1.27 -     else lookup r x)"
    1.28 +"lookup (Node2 l (a,b) r) x = (case cmp x a of
    1.29 +  LT \<Rightarrow> lookup l x |
    1.30 +  GT \<Rightarrow> lookup r x |
    1.31 +  EQ \<Rightarrow> Some b)" |
    1.32 +"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
    1.33 +  LT \<Rightarrow> lookup l x |
    1.34 +  EQ \<Rightarrow> Some b1 |
    1.35 +  GT \<Rightarrow> (case cmp x a2 of
    1.36 +          LT \<Rightarrow> lookup m x |
    1.37 +          EQ \<Rightarrow> Some b2 |
    1.38 +          GT \<Rightarrow> lookup r x))" |
    1.39 +"lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of
    1.40 +  LT \<Rightarrow> (case cmp x a1 of
    1.41 +           LT \<Rightarrow> lookup t1 x | EQ \<Rightarrow> Some b1 | GT \<Rightarrow> lookup t2 x) |
    1.42 +  EQ \<Rightarrow> Some b2 |
    1.43 +  GT \<Rightarrow> (case cmp x a3 of
    1.44 +           LT \<Rightarrow> lookup t3 x | EQ \<Rightarrow> Some b3 | GT \<Rightarrow> lookup t4 x))"
    1.45  
    1.46 -fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
    1.47 +fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
    1.48  "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
    1.49 -"upd x y (Node2 l ab r) =
    1.50 -   (if x < fst ab then
    1.51 -        (case upd x y l of
    1.52 +"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
    1.53 +   LT \<Rightarrow> (case upd x y l of
    1.54             T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
    1.55 -         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 ab r))
    1.56 -    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
    1.57 -    else
    1.58 -        (case upd x y r of
    1.59 +         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
    1.60 +   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
    1.61 +   GT \<Rightarrow> (case upd x y r of
    1.62             T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
    1.63 -         | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l ab r1 q r2)))" |
    1.64 -"upd x y (Node3 l ab1 m ab2 r) =
    1.65 -   (if x < fst ab1 then
    1.66 -        (case upd x y l of
    1.67 +         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
    1.68 +"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
    1.69 +   LT \<Rightarrow> (case upd x y l of
    1.70             T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
    1.71 -         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node2 m ab2 r))
    1.72 -    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
    1.73 -    else if x < fst ab2 then
    1.74 -             (case upd x y m of
    1.75 -                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
    1.76 -              | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node2 m2 ab2 r))
    1.77 -         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
    1.78 -         else
    1.79 -             (case upd x y r of
    1.80 -                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
    1.81 -              | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 q r2)))" |
    1.82 -"upd x y (Node4 l ab1 m ab2 n ab3 r) =
    1.83 -   (if x < fst ab2 then
    1.84 -      if x < fst ab1 then
    1.85 -        (case upd x y l of
    1.86 -           T\<^sub>i l' => T\<^sub>i (Node4 l' ab1 m ab2 n ab3 r)
    1.87 -         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node3 m ab2 n ab3 r))
    1.88 -      else
    1.89 -      if x = fst ab1 then T\<^sub>i (Node4 l (x,y) m ab2 n ab3 r)
    1.90 -      else
    1.91 -        (case upd x y m of
    1.92 -           T\<^sub>i m' => T\<^sub>i (Node4 l ab1 m' ab2 n ab3 r)
    1.93 -         | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node3 m2 ab2 n ab3 r))
    1.94 -    else
    1.95 -    if x = fst ab2 then T\<^sub>i (Node4 l ab1 m (x,y) n ab3 r) else
    1.96 -    if x < fst ab3 then
    1.97 -      (case upd x y n of
    1.98 -         T\<^sub>i n' => T\<^sub>i (Node4 l ab1 m ab2 n' ab3 r)
    1.99 -       | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l ab1 m) ab2(*q*) (Node3 n1 q n2 ab3 r))
   1.100 -    else
   1.101 -    if x = fst ab3 then T\<^sub>i (Node4 l ab1 m ab2 n (x,y) r)
   1.102 -    else
   1.103 -      (case upd x y r of
   1.104 -         T\<^sub>i r' => T\<^sub>i (Node4 l ab1 m ab2 n ab3 r')
   1.105 -       | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node3 n ab3 r1 q r2)))"
   1.106 +         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
   1.107 +   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
   1.108 +   GT \<Rightarrow> (case cmp x (fst ab2) of
   1.109 +           LT \<Rightarrow> (case upd x y m of
   1.110 +                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
   1.111 +                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
   1.112 +           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
   1.113 +           GT \<Rightarrow> (case upd x y r of
   1.114 +                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
   1.115 +                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" |
   1.116 +"upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
   1.117 +   LT \<Rightarrow> (case cmp x (fst ab1) of
   1.118 +            LT \<Rightarrow> (case upd x y t1 of
   1.119 +                     T\<^sub>i t1' => T\<^sub>i (Node4 t1' ab1 t2 ab2 t3 ab3 t4)
   1.120 +                  | Up\<^sub>i t11 q t12 => Up\<^sub>i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) |
   1.121 +            EQ \<Rightarrow> T\<^sub>i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) |
   1.122 +            GT \<Rightarrow> (case upd x y t2 of
   1.123 +                    T\<^sub>i t2' => T\<^sub>i (Node4 t1 ab1 t2' ab2 t3 ab3 t4)
   1.124 +                  | Up\<^sub>i t21 q t22 => Up\<^sub>i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) |
   1.125 +   EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) |
   1.126 +   GT \<Rightarrow> (case cmp x (fst ab3) of
   1.127 +            LT \<Rightarrow> (case upd x y t3 of
   1.128 +                    T\<^sub>i t3' \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3' ab3 t4)
   1.129 +                  | Up\<^sub>i t31 q t32 => Up\<^sub>i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) |
   1.130 +            EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) |
   1.131 +            GT \<Rightarrow> (case upd x y t4 of
   1.132 +                    T\<^sub>i t4' => T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 ab3 t4')
   1.133 +                  | Up\<^sub>i t41 q t42 => Up\<^sub>i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))"
   1.134 +
   1.135 +definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   1.136 +"update x y t = tree\<^sub>i(upd x y t)"
   1.137  
   1.138 -definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   1.139 -"update a b t = tree\<^sub>i(upd a b t)"
   1.140 -
   1.141 -fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d"
   1.142 -where
   1.143 -"del k Leaf = T\<^sub>d Leaf" |
   1.144 -"del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
   1.145 -"del k (Node3 Leaf p Leaf q Leaf) =
   1.146 -  T\<^sub>d(if k=fst p then Node2 Leaf q Leaf else
   1.147 -     if k=fst q then Node2 Leaf p Leaf
   1.148 -     else Node3 Leaf p Leaf q Leaf)" |
   1.149 -"del k (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
   1.150 -  T\<^sub>d(if k=fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
   1.151 -     if k=fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
   1.152 -     if k=fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
   1.153 +fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" where
   1.154 +"del x Leaf = T\<^sub>d Leaf" |
   1.155 +"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
   1.156 +"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
   1.157 +  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
   1.158 +"del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
   1.159 +  T\<^sub>d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
   1.160 +     if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
   1.161 +     if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
   1.162       else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
   1.163 -"del k (Node2 l a r) =
   1.164 -  (if k<fst a then node21 (del k l) a r else
   1.165 -   if k > fst a then node22 l a (del k r)
   1.166 -   else let (a',t) = del_min r in node22 l a' t)" |
   1.167 -"del k (Node3 l a m b r) =
   1.168 -  (if k<fst a then node31 (del k l) a m b r else
   1.169 -   if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else
   1.170 -   if k < fst b then node32 l a (del k m) b r else
   1.171 -   if k = fst b then let (b',r') = del_min r in node33 l a m b' r'
   1.172 -   else node33 l a m b (del k r))" |
   1.173 -"del x (Node4 l ab1 m ab2 n ab3 r) =
   1.174 -  (if x < fst ab2 then
   1.175 -     if x < fst ab1 then node41 (del x l) ab1 m ab2 n ab3 r else
   1.176 -     if x = fst ab1 then let (ab',m') = del_min m in node42 l ab' m' ab2 n ab3 r
   1.177 -     else node42 l ab1 (del x m) ab2 n ab3 r
   1.178 -   else
   1.179 -     if x = fst ab2 then let (ab',n') = del_min n in node43 l ab1 m ab' n' ab3 r else
   1.180 -     if x < fst ab3 then node43 l ab1 m ab2 (del x n) ab3 r else
   1.181 -     if x = fst ab3 then let (ab',r') = del_min r in node44 l ab1 m ab2 n ab' r'
   1.182 -     else node44 l ab1 m ab2 n ab3 (del x r))"
   1.183 +"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
   1.184 +  LT \<Rightarrow> node21 (del x l) ab1 r |
   1.185 +  GT \<Rightarrow> node22 l ab1 (del x r) |
   1.186 +  EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" |
   1.187 +"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
   1.188 +  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
   1.189 +  EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r |
   1.190 +  GT \<Rightarrow> (case cmp x (fst ab2) of
   1.191 +           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
   1.192 +           EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' |
   1.193 +           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))" |
   1.194 +"del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
   1.195 +  LT \<Rightarrow> (case cmp x (fst ab1) of
   1.196 +           LT \<Rightarrow> node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 |
   1.197 +           EQ \<Rightarrow> let (ab',t2') = del_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 |
   1.198 +           GT \<Rightarrow> node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) |
   1.199 +  EQ \<Rightarrow> let (ab',t3') = del_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 |
   1.200 +  GT \<Rightarrow> (case cmp x (fst ab3) of
   1.201 +          LT \<Rightarrow> node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 |
   1.202 +          EQ \<Rightarrow> let (ab',t4') = del_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' |
   1.203 +          GT \<Rightarrow> node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))"
   1.204  
   1.205 -definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   1.206 -"delete k t = tree\<^sub>d(del k t)"
   1.207 +definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
   1.208 +"delete x t = tree\<^sub>d(del x t)"
   1.209  
   1.210  
   1.211  subsection "Functional correctness"
   1.212 @@ -144,7 +131,7 @@
   1.213    inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
   1.214  by(induction t rule: del.induct)
   1.215    ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+
   1.216 -(* 290 secs (2015) *)
   1.217 +(* 200 secs (2015) *)
   1.218  
   1.219  lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
   1.220    inorder(delete x t) = del_list x (inorder t)"
   1.221 @@ -154,7 +141,7 @@
   1.222  subsection \<open>Balancedness\<close>
   1.223  
   1.224  lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
   1.225 -by (induct t) (auto, auto split: up\<^sub>i.split) (* 33 secs (2015) *)
   1.226 +by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
   1.227  
   1.228  lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)"
   1.229  by (simp add: update_def bal_upd)
   1.230 @@ -163,11 +150,12 @@
   1.231  lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
   1.232  by(induction x t rule: del.induct)
   1.233    (auto simp add: heights height_del_min split: prod.split)
   1.234 +(* 20 secs (2015) *)
   1.235  
   1.236  lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
   1.237  by(induction x t rule: del.induct)
   1.238    (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
   1.239 -(* 110 secs (2015) *)
   1.240 +(* 100 secs (2015) *)
   1.241  
   1.242  corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
   1.243  by(simp add: delete_def bal_tree\<^sub>d_del)