src/HOL/Data_Structures/RBT_Map.thy
author nipkow
Mon, 16 Nov 2015 15:59:47 +0100
changeset 61688 d04b1b4fb015
parent 61686 e6784939d645
child 61749 7f530d7e552d
permissions -rw-r--r--
corrected inefficient implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     2
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     3
section \<open>Red-Black Tree Implementation of Maps\<close>
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     4
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     5
theory RBT_Map
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     6
imports
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     7
  RBT_Set
61231
nipkow
parents: 61224
diff changeset
     8
  Lookup2
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     9
begin
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    10
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    11
fun update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) rbt \<Rightarrow> ('a*'b) rbt" where
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    12
"update x y Leaf = R Leaf (x,y) Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    13
"update x y (B l (a,b) r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    14
  LT \<Rightarrow> bal (update x y l) (a,b) r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    15
  GT \<Rightarrow> bal l (a,b) (update x y r) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    16
  EQ \<Rightarrow> B l (x,y) r)" |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    17
"update x y (R l (a,b) r) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    18
  LT \<Rightarrow> R (update x y l) (a,b) r |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    19
  GT \<Rightarrow> R l (a,b) (update x y r) |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    20
  EQ \<Rightarrow> R l (x,y) r)"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    21
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    22
fun delete :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    23
and deleteL :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    24
and deleteR :: "'a::cmp \<Rightarrow> ('a*'b)rbt \<Rightarrow> 'a*'b \<Rightarrow> ('a*'b)rbt \<Rightarrow> ('a*'b)rbt"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    25
where
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    26
"delete x Leaf = Leaf" |
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    27
"delete x (Node c t1 (a,b) t2) = (case cmp x a of
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    28
  LT \<Rightarrow> deleteL x t1 (a,b) t2 |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    29
  GT \<Rightarrow> deleteR x t1 (a,b) t2 |
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61231
diff changeset
    30
  EQ \<Rightarrow> combine t1 t2)" |
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    31
"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    32
"deleteL x t1 a t2 = R (delete x t1) a t2" |
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    33
"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    34
"deleteR x t1 a t2 = R t1 a (delete x t2)"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    35
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    36
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    37
subsection "Functional Correctness Proofs"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    38
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    39
lemma inorder_update:
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    40
  "sorted1(inorder t) \<Longrightarrow> inorder(update x y t) = upd_list x y (inorder t)"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    41
by(induction x y t rule: update.induct)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    42
  (auto simp: upd_list_simps inorder_bal)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    43
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    44
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    45
lemma inorder_delete:
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    46
 "sorted1(inorder t1) \<Longrightarrow>  inorder(delete x t1) = del_list x (inorder t1)" and
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    47
 "sorted1(inorder t1) \<Longrightarrow>  inorder(deleteL x t1 a t2) =
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    48
    del_list x (inorder t1) @ a # inorder t2" and
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    49
 "sorted1(inorder t2) \<Longrightarrow>  inorder(deleteR x t1 a t2) =
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    50
    inorder t1 @ a # del_list x (inorder t2)"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    51
by(induction x t1 and x t1 a t2 and x t1 a t2 rule: delete_deleteL_deleteR.induct)
61231
nipkow
parents: 61224
diff changeset
    52
  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    53
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    54
interpretation Map_by_Ordered
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    55
where empty = Leaf and lookup = lookup and update = update and delete = delete
61686
e6784939d645 tuned names
nipkow
parents: 61581
diff changeset
    56
and inorder = inorder and inv = "\<lambda>_. True"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    57
proof (standard, goal_cases)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    58
  case 1 show ?case by simp
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    59
next
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    60
  case 2 thus ?case by(simp add: lookup_eq)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    61
next
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    62
  case 3 thus ?case by(simp add: inorder_update)
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    63
next
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    64
  case 4 thus ?case by(simp add: inorder_delete)
61686
e6784939d645 tuned names
nipkow
parents: 61581
diff changeset
    65
qed auto
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    66
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    67
end