src/HOL/Data_Structures/Tree234_Map.thy
author nipkow
Mon Jan 11 20:51:13 2016 +0100 (2016-01-11)
changeset 62130 90a3016a6c12
parent 61790 0494964bb226
child 63411 e051eea34990
permissions -rw-r--r--
added AA_Map; tuned titles
nipkow@61640
     1
(* Author: Tobias Nipkow *)
nipkow@61640
     2
nipkow@62130
     3
section \<open>2-3-4 Tree Implementation of Maps\<close>
nipkow@61640
     4
nipkow@61640
     5
theory Tree234_Map
nipkow@61640
     6
imports
nipkow@61640
     7
  Tree234_Set
nipkow@61640
     8
  "../Data_Structures/Map_by_Ordered"
nipkow@61640
     9
begin
nipkow@61640
    10
nipkow@61640
    11
subsection \<open>Map operations on 2-3-4 trees\<close>
nipkow@61640
    12
nipkow@61640
    13
fun lookup :: "('a::cmp * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where
nipkow@61640
    14
"lookup Leaf x = None" |
nipkow@61640
    15
"lookup (Node2 l (a,b) r) x = (case cmp x a of
nipkow@61640
    16
  LT \<Rightarrow> lookup l x |
nipkow@61640
    17
  GT \<Rightarrow> lookup r x |
nipkow@61640
    18
  EQ \<Rightarrow> Some b)" |
nipkow@61640
    19
"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
nipkow@61640
    20
  LT \<Rightarrow> lookup l x |
nipkow@61640
    21
  EQ \<Rightarrow> Some b1 |
nipkow@61640
    22
  GT \<Rightarrow> (case cmp x a2 of
nipkow@61640
    23
          LT \<Rightarrow> lookup m x |
nipkow@61640
    24
          EQ \<Rightarrow> Some b2 |
nipkow@61640
    25
          GT \<Rightarrow> lookup r x))" |
nipkow@61640
    26
"lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of
nipkow@61640
    27
  LT \<Rightarrow> (case cmp x a1 of
nipkow@61640
    28
           LT \<Rightarrow> lookup t1 x | EQ \<Rightarrow> Some b1 | GT \<Rightarrow> lookup t2 x) |
nipkow@61640
    29
  EQ \<Rightarrow> Some b2 |
nipkow@61640
    30
  GT \<Rightarrow> (case cmp x a3 of
nipkow@61640
    31
           LT \<Rightarrow> lookup t3 x | EQ \<Rightarrow> Some b3 | GT \<Rightarrow> lookup t4 x))"
nipkow@61640
    32
nipkow@61640
    33
fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where
nipkow@61640
    34
"upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
nipkow@61640
    35
"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
nipkow@61640
    36
   LT \<Rightarrow> (case upd x y l of
nipkow@61640
    37
           T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
nipkow@61640
    38
         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
nipkow@61640
    39
   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
nipkow@61640
    40
   GT \<Rightarrow> (case upd x y r of
nipkow@61640
    41
           T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
nipkow@61640
    42
         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
nipkow@61640
    43
"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
nipkow@61640
    44
   LT \<Rightarrow> (case upd x y l of
nipkow@61640
    45
           T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
nipkow@61640
    46
         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
nipkow@61640
    47
   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
nipkow@61640
    48
   GT \<Rightarrow> (case cmp x (fst ab2) of
nipkow@61640
    49
           LT \<Rightarrow> (case upd x y m of
nipkow@61640
    50
                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
nipkow@61640
    51
                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
nipkow@61640
    52
           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
nipkow@61640
    53
           GT \<Rightarrow> (case upd x y r of
nipkow@61640
    54
                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
nipkow@61640
    55
                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" |
nipkow@61640
    56
"upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
nipkow@61640
    57
   LT \<Rightarrow> (case cmp x (fst ab1) of
nipkow@61640
    58
            LT \<Rightarrow> (case upd x y t1 of
nipkow@61640
    59
                     T\<^sub>i t1' => T\<^sub>i (Node4 t1' ab1 t2 ab2 t3 ab3 t4)
nipkow@61640
    60
                  | Up\<^sub>i t11 q t12 => Up\<^sub>i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) |
nipkow@61640
    61
            EQ \<Rightarrow> T\<^sub>i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) |
nipkow@61640
    62
            GT \<Rightarrow> (case upd x y t2 of
nipkow@61640
    63
                    T\<^sub>i t2' => T\<^sub>i (Node4 t1 ab1 t2' ab2 t3 ab3 t4)
nipkow@61640
    64
                  | Up\<^sub>i t21 q t22 => Up\<^sub>i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) |
nipkow@61640
    65
   EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) |
nipkow@61640
    66
   GT \<Rightarrow> (case cmp x (fst ab3) of
nipkow@61640
    67
            LT \<Rightarrow> (case upd x y t3 of
nipkow@61640
    68
                    T\<^sub>i t3' \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3' ab3 t4)
nipkow@61640
    69
                  | Up\<^sub>i t31 q t32 => Up\<^sub>i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) |
nipkow@61640
    70
            EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) |
nipkow@61640
    71
            GT \<Rightarrow> (case upd x y t4 of
nipkow@61640
    72
                    T\<^sub>i t4' => T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 ab3 t4')
nipkow@61640
    73
                  | Up\<^sub>i t41 q t42 => Up\<^sub>i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))"
nipkow@61640
    74
nipkow@61640
    75
definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
nipkow@61640
    76
"update x y t = tree\<^sub>i(upd x y t)"
nipkow@61640
    77
nipkow@61640
    78
fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" where
nipkow@61640
    79
"del x Leaf = T\<^sub>d Leaf" |
nipkow@61640
    80
"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
nipkow@61640
    81
"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
nipkow@61640
    82
  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
nipkow@61640
    83
"del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) =
nipkow@61640
    84
  T\<^sub>d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else
nipkow@61640
    85
     if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else
nipkow@61640
    86
     if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf
nipkow@61640
    87
     else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" |
nipkow@61640
    88
"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
nipkow@61640
    89
  LT \<Rightarrow> node21 (del x l) ab1 r |
nipkow@61640
    90
  GT \<Rightarrow> node22 l ab1 (del x r) |
nipkow@61640
    91
  EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" |
nipkow@61640
    92
"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
nipkow@61640
    93
  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
nipkow@61640
    94
  EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r |
nipkow@61640
    95
  GT \<Rightarrow> (case cmp x (fst ab2) of
nipkow@61640
    96
           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
nipkow@61640
    97
           EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' |
nipkow@61640
    98
           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))" |
nipkow@61640
    99
"del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of
nipkow@61640
   100
  LT \<Rightarrow> (case cmp x (fst ab1) of
nipkow@61640
   101
           LT \<Rightarrow> node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 |
nipkow@61640
   102
           EQ \<Rightarrow> let (ab',t2') = del_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 |
nipkow@61640
   103
           GT \<Rightarrow> node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) |
nipkow@61640
   104
  EQ \<Rightarrow> let (ab',t3') = del_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 |
nipkow@61640
   105
  GT \<Rightarrow> (case cmp x (fst ab3) of
nipkow@61640
   106
          LT \<Rightarrow> node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 |
nipkow@61640
   107
          EQ \<Rightarrow> let (ab',t4') = del_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' |
nipkow@61640
   108
          GT \<Rightarrow> node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))"
nipkow@61640
   109
nipkow@61640
   110
definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where
nipkow@61640
   111
"delete x t = tree\<^sub>d(del x t)"
nipkow@61640
   112
nipkow@61640
   113
nipkow@61640
   114
subsection "Functional correctness"
nipkow@61640
   115
nipkow@61790
   116
lemma lookup_map_of:
nipkow@61790
   117
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
nipkow@61640
   118
by (induction t) (auto simp: map_of_simps split: option.split)
nipkow@61640
   119
nipkow@61640
   120
nipkow@61640
   121
lemma inorder_upd:
nipkow@61640
   122
  "sorted1(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(upd a b t)) = upd_list a b (inorder t)"
nipkow@61640
   123
by(induction t)
nipkow@61640
   124
  (auto simp: upd_list_simps, auto simp: upd_list_simps split: up\<^sub>i.splits)
nipkow@61640
   125
nipkow@61640
   126
lemma inorder_update:
nipkow@61640
   127
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
nipkow@61640
   128
by(simp add: update_def inorder_upd)
nipkow@61640
   129
nipkow@61640
   130
nipkow@61640
   131
lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
nipkow@61640
   132
  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
nipkow@61640
   133
by(induction t rule: del.induct)
nipkow@61640
   134
  ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+
nipkow@61640
   135
(* 200 secs (2015) *)
nipkow@61640
   136
nipkow@61640
   137
lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
nipkow@61640
   138
  inorder(delete x t) = del_list x (inorder t)"
nipkow@61640
   139
by(simp add: delete_def inorder_del)
nipkow@61640
   140
nipkow@61640
   141
nipkow@61640
   142
subsection \<open>Balancedness\<close>
nipkow@61640
   143
nipkow@61640
   144
lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
nipkow@61640
   145
by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *)
nipkow@61640
   146
nipkow@61640
   147
lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)"
nipkow@61640
   148
by (simp add: update_def bal_upd)
nipkow@61640
   149
nipkow@61640
   150
nipkow@61640
   151
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
nipkow@61640
   152
by(induction x t rule: del.induct)
nipkow@61640
   153
  (auto simp add: heights height_del_min split: prod.split)
nipkow@61640
   154
(* 20 secs (2015) *)
nipkow@61640
   155
nipkow@61640
   156
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
nipkow@61640
   157
by(induction x t rule: del.induct)
nipkow@61640
   158
  (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
nipkow@61640
   159
(* 100 secs (2015) *)
nipkow@61640
   160
nipkow@61640
   161
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
nipkow@61640
   162
by(simp add: delete_def bal_tree\<^sub>d_del)
nipkow@61640
   163
nipkow@61640
   164
nipkow@61640
   165
subsection \<open>Overall Correctness\<close>
nipkow@61640
   166
nipkow@61790
   167
interpretation Map_by_Ordered
nipkow@61640
   168
where empty = Leaf and lookup = lookup and update = update and delete = delete
nipkow@61686
   169
and inorder = inorder and inv = bal
nipkow@61640
   170
proof (standard, goal_cases)
nipkow@61790
   171
  case 2 thus ?case by(simp add: lookup_map_of)
nipkow@61640
   172
next
nipkow@61640
   173
  case 3 thus ?case by(simp add: inorder_update)
nipkow@61640
   174
next
nipkow@61640
   175
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61640
   176
next
nipkow@61640
   177
  case 6 thus ?case by(simp add: bal_update)
nipkow@61640
   178
next
nipkow@61640
   179
  case 7 thus ?case by(simp add: bal_delete)
nipkow@61640
   180
qed simp+
nipkow@61640
   181
nipkow@61640
   182
end