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