src/HOL/Data_Structures/Tree23_Map.thy
author nipkow
Sat, 21 Apr 2018 08:41:42 +0200
changeset 68020 6aade817bee5
parent 67965 aaa31cd0caef
child 68431 b294e095f64c
permissions -rw-r--r--
del_min -> split_min
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     1
(* Author: Tobias Nipkow *)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     2
62130
90a3016a6c12 added AA_Map; tuned titles
nipkow
parents: 61790
diff changeset
     3
section \<open>2-3 Tree Implementation of Maps\<close>
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     4
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     5
theory Tree23_Map
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     6
imports
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     7
  Tree23_Set
67965
aaa31cd0caef more name tuning
nipkow
parents: 63636
diff changeset
     8
  Map_Specs
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
     9
begin
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    10
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    11
fun lookup :: "('a::linorder * 'b) tree23 \<Rightarrow> 'a \<Rightarrow> 'b option" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    12
"lookup Leaf x = None" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    13
"lookup (Node2 l (a,b) r) x = (case cmp x a of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    14
  LT \<Rightarrow> lookup l x |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    15
  GT \<Rightarrow> lookup r x |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    16
  EQ \<Rightarrow> Some b)" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    17
"lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    18
  LT \<Rightarrow> lookup l x |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    19
  EQ \<Rightarrow> Some b1 |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    20
  GT \<Rightarrow> (case cmp x a2 of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    21
          LT \<Rightarrow> lookup m x |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    22
          EQ \<Rightarrow> Some b2 |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    23
          GT \<Rightarrow> lookup r x))"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    24
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    25
fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>i" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    26
"upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    27
"upd x y (Node2 l ab r) = (case cmp x (fst ab) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    28
   LT \<Rightarrow> (case upd x y l of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    29
           T\<^sub>i l' => T\<^sub>i (Node2 l' ab r)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    30
         | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    31
   EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    32
   GT \<Rightarrow> (case upd x y r of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    33
           T\<^sub>i r' => T\<^sub>i (Node2 l ab r')
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    34
         | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    35
"upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    36
   LT \<Rightarrow> (case upd x y l of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    37
           T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    38
         | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    39
   EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    40
   GT \<Rightarrow> (case cmp x (fst ab2) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    41
           LT \<Rightarrow> (case upd x y m of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    42
                   T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    43
                 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    44
           EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    45
           GT \<Rightarrow> (case upd x y r of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    46
                   T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r')
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    47
                 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    48
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    49
definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    50
"update a b t = tree\<^sub>i(upd a b t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    51
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    52
fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>d" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    53
"del x Leaf = T\<^sub>d Leaf" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    54
"del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    55
"del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    56
  else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    57
"del x (Node2 l ab1 r) = (case cmp x (fst ab1) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    58
  LT \<Rightarrow> node21 (del x l) ab1 r |
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    59
  GT \<Rightarrow> node22 l ab1 (del x r) |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
    60
  EQ \<Rightarrow> let (ab1',t) = split_min r in node22 l ab1' t)" |
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    61
"del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    62
  LT \<Rightarrow> node31 (del x l) ab1 m ab2 r |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
    63
  EQ \<Rightarrow> let (ab1',m') = split_min m in node32 l ab1' m' ab2 r |
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    64
  GT \<Rightarrow> (case cmp x (fst ab2) of
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    65
           LT \<Rightarrow> node32 l ab1 (del x m) ab2 r |
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
    66
           EQ \<Rightarrow> let (ab2',r') = split_min r in node33 l ab1 m ab2' r' |
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    67
           GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    68
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62130
diff changeset
    69
definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    70
"delete x t = tree\<^sub>d(del x t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    71
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    72
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    73
subsection \<open>Functional Correctness\<close>
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    74
61790
0494964bb226 avoid name clashes
nipkow
parents: 61789
diff changeset
    75
lemma lookup_map_of:
0494964bb226 avoid name clashes
nipkow
parents: 61789
diff changeset
    76
  "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x"
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    77
by (induction t) (auto simp: map_of_simps split: option.split)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    78
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    79
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    80
lemma inorder_upd:
61789
9ce1a397410a added Brother12_Map
nipkow
parents: 61686
diff changeset
    81
  "sorted1(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(upd x y t)) = upd_list x y (inorder t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    82
by(induction t) (auto simp: upd_list_simps split: up\<^sub>i.splits)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    83
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    84
corollary inorder_update:
61789
9ce1a397410a added Brother12_Map
nipkow
parents: 61686
diff changeset
    85
  "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    86
by(simp add: update_def inorder_upd)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    87
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    88
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    89
lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    90
  inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    91
by(induction t rule: del.induct)
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
    92
  (auto simp: del_list_simps inorder_nodes split_minD split!: if_split prod.splits)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    93
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    94
corollary inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow>
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    95
  inorder(delete x t) = del_list x (inorder t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    96
by(simp add: delete_def inorder_del)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    97
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    98
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
    99
subsection \<open>Balancedness\<close>
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   100
61789
9ce1a397410a added Brother12_Map
nipkow
parents: 61686
diff changeset
   101
lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t"
63636
6f38b7abb648 introduced aggressive splitter "split!"
nipkow
parents: 63411
diff changeset
   102
by (induct t) (auto split!: if_split up\<^sub>i.split)(* 16 secs in 2015 *)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   103
61789
9ce1a397410a added Brother12_Map
nipkow
parents: 61686
diff changeset
   104
corollary bal_update: "bal t \<Longrightarrow> bal (update x y t)"
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   105
by (simp add: update_def bal_upd)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   106
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   107
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   108
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   109
by(induction x t rule: del.induct)
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   110
  (auto simp add: heights max_def height_split_min split: prod.split)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   111
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   112
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   113
by(induction x t rule: del.induct)
68020
6aade817bee5 del_min -> split_min
nipkow
parents: 67965
diff changeset
   114
  (auto simp: bals bal_split_min height_del height_split_min split: prod.split)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   115
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   116
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   117
by(simp add: delete_def bal_tree\<^sub>d_del)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   118
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   119
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   120
subsection \<open>Overall Correctness\<close>
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   121
61790
0494964bb226 avoid name clashes
nipkow
parents: 61789
diff changeset
   122
interpretation Map_by_Ordered
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   123
where empty = Leaf and lookup = lookup and update = update and delete = delete
61686
e6784939d645 tuned names
nipkow
parents: 61640
diff changeset
   124
and inorder = inorder and inv = bal
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   125
proof (standard, goal_cases)
61790
0494964bb226 avoid name clashes
nipkow
parents: 61789
diff changeset
   126
  case 2 thus ?case by(simp add: lookup_map_of)
61640
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   127
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   128
  case 3 thus ?case by(simp add: inorder_update)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   129
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   130
  case 4 thus ?case by(simp add: inorder_delete)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   131
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   132
  case 6 thus ?case by(simp add: bal_update)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   133
next
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   134
  case 7 thus ?case by(simp add: bal_delete)
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   135
qed simp+
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   136
44c9198f210c no CRLF
nipkow
parents: 61581
diff changeset
   137
end