src/HOL/Data_Structures/Tree234_Map.thy
 changeset 61515 c64628dbac00 child 61581 00d9682e8dd7
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Data_Structures/Tree234_Map.thy	Sun Oct 25 17:31:14 2015 +0100
1.3 @@ -0,0 +1,193 @@
1.4 +(* Author: Tobias Nipkow *)
1.5 +
1.6 +section \<open>A 2-3-4 Tree Implementation of Maps\<close>
1.7 +
1.8 +theory Tree234_Map
1.9 +imports
1.10 +  Tree234_Set
1.11 +  "../Data_Structures/Map_by_Ordered"
1.12 +begin
1.13 +
1.14 +subsection \<open>Map operations on 2-3-4 trees\<close>
1.15 +
1.16 +fun lookup :: "('a::linorder * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
1.17 +"lookup Leaf x = None" |
1.18 +"lookup (Node2 l (a,b) r) x =
1.19 +  (if x < a then lookup l x else
1.20 +  if a < x then lookup r x else Some b)" |
1.21 +"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
1.22 +  (if x < a1 then lookup l x else
1.23 +   if x = a1 then Some b1 else
1.24 +   if x < a2 then lookup m x else
1.25 +   if x = a2 then Some b2
1.26 +   else lookup r x)" |
1.27 +"lookup (Node4 l (a1,b1) m (a2,b2) n (a3,b3) r) x =
1.28 +  (if x < a2 then
1.29 +     if x = a1 then Some b1 else
1.30 +     if x < a1 then lookup l x else lookup m x
1.31 +   else
1.32 +     if x = a2 then Some b2 else
1.33 +     if x = a3 then Some b3 else
1.34 +     if x < a3 then lookup n x
1.35 +     else lookup r x)"
1.36 +
1.37 +fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
1.38 +"upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
1.39 +"upd x y (Node2 l ab r) =
1.40 +   (if x < fst ab then
1.41 +        (case upd x y l of
1.42 +           T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
1.43 +         | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 ab r))
1.44 +    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
1.45 +    else
1.46 +        (case upd x y r of
1.47 +           T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
1.48 +         | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l ab r1 q r2)))" |
1.49 +"upd x y (Node3 l ab1 m ab2 r) =
1.50 +   (if x < fst ab1 then
1.51 +        (case upd x y l of
1.52 +           T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
1.53 +         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node2 m ab2 r))
1.54 +    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
1.55 +    else if x < fst ab2 then
1.56 +             (case upd x y m of
1.57 +                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
1.58 +              | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node2 m2 ab2 r))
1.59 +         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
1.60 +         else
1.61 +             (case upd x y r of
1.62 +                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
1.63 +              | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 q r2)))" |
1.64 +"upd x y (Node4 l ab1 m ab2 n ab3 r) =
1.65 +   (if x < fst ab2 then
1.66 +      if x < fst ab1 then
1.67 +        (case upd x y l of
1.68 +           T\<^sub>i l' => T\<^sub>i (Node4 l' ab1 m ab2 n ab3 r)
1.69 +         | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node3 m ab2 n ab3 r))
1.70 +      else
1.71 +      if x = fst ab1 then T\<^sub>i (Node4 l (x,y) m ab2 n ab3 r)
1.72 +      else
1.73 +        (case upd x y m of
1.74 +           T\<^sub>i m' => T\<^sub>i (Node4 l ab1 m' ab2 n ab3 r)
1.75 +         | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node3 m2 ab2 n ab3 r))
1.76 +    else
1.77 +    if x = fst ab2 then T\<^sub>i (Node4 l ab1 m (x,y) n ab3 r) else
1.78 +    if x < fst ab3 then
1.79 +      (case upd x y n of
1.80 +         T\<^sub>i n' => T\<^sub>i (Node4 l ab1 m ab2 n' ab3 r)
1.81 +       | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l ab1 m) ab2(*q*) (Node3 n1 q n2 ab3 r))
1.82 +    else
1.83 +    if x = fst ab3 then T\<^sub>i (Node4 l ab1 m ab2 n (x,y) r)
1.84 +    else
1.85 +      (case upd x y r of
1.86 +         T\<^sub>i r' => T\<^sub>i (Node4 l ab1 m ab2 n ab3 r')
1.87 +       | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node3 n ab3 r1 q r2)))"
1.88 +
1.89 +definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
1.90 +"update a b t = tree\<^sub>i(upd a b t)"
1.91 +
1.92 +fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d"
1.93 +where
1.94 +"del k Leaf = T\<^sub>d Leaf" |
1.95 +"del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
1.96 +"del k (Node3 Leaf p Leaf q Leaf) =
1.97 +  T\<^sub>d(if k=fst p then Node2 Leaf q Leaf else
1.98 +     if k=fst q then Node2 Leaf p Leaf
1.99 +     else Node3 Leaf p Leaf q Leaf)" |
1.100 +"del k (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
1.101 +  T\<^sub>d(if k=fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
1.102 +     if k=fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
1.103 +     if k=fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
1.104 +     else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
1.105 +"del k (Node2 l a r) =
1.106 +  (if k<fst a then node21 (del k l) a r else
1.107 +   if k > fst a then node22 l a (del k r)
1.108 +   else let (a',t) = del_min r in node22 l a' t)" |
1.109 +"del k (Node3 l a m b r) =
1.110 +  (if k<fst a then node31 (del k l) a m b r else
1.111 +   if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else
1.112 +   if k < fst b then node32 l a (del k m) b r else
1.113 +   if k = fst b then let (b',r') = del_min r in node33 l a m b' r'
1.114 +   else node33 l a m b (del k r))" |
1.115 +"del x (Node4 l ab1 m ab2 n ab3 r) =
1.116 +  (if x < fst ab2 then
1.117 +     if x < fst ab1 then node41 (del x l) ab1 m ab2 n ab3 r else
1.118 +     if x = fst ab1 then let (ab',m') = del_min m in node42 l ab' m' ab2 n ab3 r
1.119 +     else node42 l ab1 (del x m) ab2 n ab3 r
1.120 +   else
1.121 +     if x = fst ab2 then let (ab',n') = del_min n in node43 l ab1 m ab' n' ab3 r else
1.122 +     if x < fst ab3 then node43 l ab1 m ab2 (del x n) ab3 r else
1.123 +     if x = fst ab3 then let (ab',r') = del_min r in node44 l ab1 m ab2 n ab' r'
1.124 +     else node44 l ab1 m ab2 n ab3 (del x r))"
1.125 +
1.126 +definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
1.127 +"delete k t = tree\<^sub>d(del k t)"
1.128 +
1.129 +
1.130 +subsection "Functional correctness"
1.131 +
1.132 +lemma lookup: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
1.133 +by (induction t) (auto simp: map_of_simps split: option.split)
1.134 +
1.135 +
1.136 +lemma inorder_upd:
1.137 +  "sorted1(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(upd a b t)) = upd_list a b (inorder t)"
1.138 +by(induction t)
1.139 +  (auto simp: upd_list_simps, auto simp: upd_list_simps split: up\<^sub>i.splits)
1.140 +
1.141 +lemma inorder_update:
1.142 +  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
1.144 +
1.145 +
1.146 +lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
1.147 +  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
1.148 +by(induction t rule: del.induct)
1.149 +  ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+
1.150 +(* 290 secs (2015) *)
1.151 +
1.152 +lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
1.153 +  inorder(delete x t) = del_list x (inorder t)"
1.155 +
1.156 +
1.157 +subsection \<open>Balancedness\<close>
1.158 +
1.159 +lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
1.160 +by (induct t) (auto, auto split: up\<^sub>i.split) (* 33 secs (2015) *)
1.161 +
1.162 +lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)"
1.163 +by (simp add: update_def bal_upd)
1.164 +
1.165 +
1.166 +lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
1.167 +by(induction x t rule: del.induct)
1.168 +  (auto simp add: heights height_del_min split: prod.split)
1.169 +
1.170 +lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
1.171 +by(induction x t rule: del.induct)
1.172 +  (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
1.173 +(* 110 secs (2015) *)
1.174 +
1.175 +corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
1.177 +
1.178 +
1.179 +subsection \<open>Overall Correctness\<close>
1.180 +
1.181 +interpretation T234_Map: Map_by_Ordered
1.182 +where empty = Leaf and lookup = lookup and update = update and delete = delete
1.183 +and inorder = inorder and wf = bal
1.184 +proof (standard, goal_cases)
1.185 +  case 2 thus ?case by(simp add: lookup)
1.186 +next
1.187 +  case 3 thus ?case by(simp add: inorder_update)
1.188 +next
1.189 +  case 4 thus ?case by(simp add: inorder_delete)
1.190 +next
1.191 +  case 6 thus ?case by(simp add: bal_update)
1.192 +next
1.193 +  case 7 thus ?case by(simp add: bal_delete)
1.194 +qed simp+
1.195 +
1.196 +end
```