src/HOL/Data_Structures/Tree23_Map.thy
author wenzelm
Wed, 04 Nov 2015 23:27:00 +0100
changeset 61578 6623c81cb15a
parent 61534 a88e07c8d0d5
child 61581 00d9682e8dd7
permissions -rw-r--r--
avoid ligatures;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     2
61513
nipkow
parents: 61469
diff changeset
     3
section \<open>A 2-3 Tree Implementation of Maps\<close>
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     4
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     5
theory Tree23_Map
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     6
imports
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     7
  Tree23_Set
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     8
  Map_by_Ordered
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
     9
begin
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    10
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    11
fun lookup :: "('a::linorder * 'b) tree23 \<Rightarrow> 'a \<Rightarrow> 'b option" where
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    12
"lookup Leaf x = None" |
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    13
"lookup (Node2 l (a,b) r) x =
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    14
  (if x < a then lookup l x else
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    15
  if a < x then lookup r x else Some b)" |
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    16
"lookup (Node3 l (a1,b1) m (a2,b2) r) x =
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    17
  (if x < a1 then lookup l x else
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    18
   if x = a1 then Some b1 else
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    19
   if x < a2 then lookup m x else
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    20
   if x = a2 then Some b2
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    21
   else lookup r x)"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    22
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    23
fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>i" where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    24
"upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    25
"upd x y (Node2 l ab r) =
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    26
   (if x < fst ab then
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    27
        (case upd x y l of
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    28
           T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    29
         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r))
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    30
    else if x = fst ab then T\<^sub>i (Node2 l (x,y) r)
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    31
    else
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    32
        (case upd x y r of
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    33
           T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    34
         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    35
"upd x y (Node3 l ab1 m ab2 r) =
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    36
   (if x < fst ab1 then
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    37
        (case upd x y l of
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    38
           T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    39
         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r))
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    40
    else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r)
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    41
    else if x < fst ab2 then
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    42
             (case upd x y m of
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    43
                T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    44
              | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r))
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    45
         else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r)
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    46
         else
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    47
             (case upd x y r of
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    48
                T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    49
              | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2)))"
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    50
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    51
definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    52
"update a b t = tree\<^sub>i(upd a b t)"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    53
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    54
fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>d"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    55
where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    56
"del x Leaf = T\<^sub>d Leaf" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    57
"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    58
"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    59
  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    60
"del x (Node2 l ab1 r) = (if x<fst ab1 then node21 (del x l) ab1 r else
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    61
  if x > fst ab1 then node22 l ab1 (del x r) else
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    62
  let (ab1',t) = del_min r in node22 l ab1' t)" |
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    63
"del x (Node3 l ab1 m ab2 r) = (if x<fst ab1 then node31 (del x l) ab1 m ab2 r else
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    64
  if x = fst ab1 then let (ab1',m') = del_min m in node32 l ab1' m' ab2 r else
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    65
  if x < fst ab2 then node32 l ab1 (del x m) ab2 r else
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    66
  if x = fst ab2 then let (ab2',r') = del_min r in node33 l ab1 m ab2' r'
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    67
  else node33 l ab1 m ab2 (del x r))"
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    68
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    69
definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
61534
a88e07c8d0d5 tuned names and optimized comparison order
nipkow
parents: 61513
diff changeset
    70
"delete x t = tree\<^sub>d(del x t)"
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    71
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    72
61513
nipkow
parents: 61469
diff changeset
    73
subsection \<open>Functional Correctness\<close>
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    74
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    75
lemma lookup: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    76
by (induction t) (auto simp: map_of_simps split: option.split)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    77
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    78
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    79
lemma inorder_upd:
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    80
  "sorted1(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(upd a b t)) = upd_list a b (inorder t)"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    81
by(induction t) (auto simp: upd_list_simps split: up\<^sub>i.splits)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    82
61513
nipkow
parents: 61469
diff changeset
    83
corollary inorder_update:
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    84
  "sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    85
by(simp add: update_def inorder_upd)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    86
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
    87
61513
nipkow
parents: 61469
diff changeset
    88
lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
nipkow
parents: 61469
diff changeset
    89
  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
nipkow
parents: 61469
diff changeset
    90
by(induction t rule: del.induct)
nipkow
parents: 61469
diff changeset
    91
  (auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)
nipkow
parents: 61469
diff changeset
    92
nipkow
parents: 61469
diff changeset
    93
corollary inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
nipkow
parents: 61469
diff changeset
    94
  inorder(delete x t) = del_list x (inorder t)"
nipkow
parents: 61469
diff changeset
    95
by(simp add: delete_def inorder_del)
nipkow
parents: 61469
diff changeset
    96
nipkow
parents: 61469
diff changeset
    97
nipkow
parents: 61469
diff changeset
    98
subsection \<open>Balancedness\<close>
nipkow
parents: 61469
diff changeset
    99
nipkow
parents: 61469
diff changeset
   100
lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd a b t)) \<and> height(upd a b t) = height t"
nipkow
parents: 61469
diff changeset
   101
by (induct t) (auto split: up\<^sub>i.split)(* 30 secs in 2015 *)
nipkow
parents: 61469
diff changeset
   102
nipkow
parents: 61469
diff changeset
   103
corollary bal_update: "bal t \<Longrightarrow> bal (update a b t)"
nipkow
parents: 61469
diff changeset
   104
by (simp add: update_def bal_upd)
nipkow
parents: 61469
diff changeset
   105
61469
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   106
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   107
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   108
by(induction x t rule: del.induct)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   109
  (auto simp add: heights max_def height_del_min split: prod.split)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   110
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   111
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   112
by(induction x t rule: del.induct)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   113
  (auto simp: bals bal_del_min height_del height_del_min split: prod.split)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   114
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   115
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   116
by(simp add: delete_def bal_tree\<^sub>d_del)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   117
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   118
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   119
subsection \<open>Overall Correctness\<close>
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   120
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   121
interpretation T23_Map: Map_by_Ordered
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   122
where empty = Leaf and lookup = lookup and update = update and delete = delete
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   123
and inorder = inorder and wf = bal
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   124
proof (standard, goal_cases)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   125
  case 2 thus ?case by(simp add: lookup)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   126
next
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   127
  case 3 thus ?case by(simp add: inorder_update)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   128
next
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   129
  case 4 thus ?case by(simp add: inorder_delete)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   130
next
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   131
  case 6 thus ?case by(simp add: bal_update)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   132
next
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   133
  case 7 thus ?case by(simp add: bal_delete)
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   134
qed simp+
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   135
cd82b1023932 added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff changeset
   136
end