src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Sun Nov 15 12:45:28 2015 +0100 (2015-11-15)
changeset 61678 b594e9277be3
parent 61588 1d2907d0ed73
child 61749 7f530d7e552d
permissions -rw-r--r--
tuned white space
nipkow@61224
     1
(* Author: Tobias Nipkow *)
nipkow@61224
     2
nipkow@61224
     3
section \<open>Red-Black Tree Implementation of Sets\<close>
nipkow@61224
     4
nipkow@61224
     5
theory RBT_Set
nipkow@61224
     6
imports
nipkow@61224
     7
  RBT
nipkow@61581
     8
  Cmp
nipkow@61224
     9
  Isin2
nipkow@61224
    10
begin
nipkow@61224
    11
nipkow@61581
    12
fun insert :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61224
    13
"insert x Leaf = R Leaf x Leaf" |
nipkow@61678
    14
"insert x (B l a r) =
nipkow@61678
    15
  (case cmp x a of
nipkow@61678
    16
     LT \<Rightarrow> bal (insert x l) a r |
nipkow@61678
    17
     GT \<Rightarrow> bal l a (insert x r) |
nipkow@61678
    18
     EQ \<Rightarrow> B l a r)" |
nipkow@61678
    19
"insert x (R l a r) =
nipkow@61678
    20
  (case cmp x a of
nipkow@61678
    21
    LT \<Rightarrow> R (insert x l) a r |
nipkow@61678
    22
    GT \<Rightarrow> R l a (insert x r) |
nipkow@61678
    23
    EQ \<Rightarrow> R l a r)"
nipkow@61224
    24
nipkow@61581
    25
fun delete :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61581
    26
and deleteL :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61581
    27
and deleteR :: "'a::cmp \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61224
    28
where
nipkow@61224
    29
"delete x Leaf = Leaf" |
nipkow@61678
    30
"delete x (Node _ l a r) =
nipkow@61678
    31
  (case cmp x a of
nipkow@61678
    32
     LT \<Rightarrow> deleteL x l a r |
nipkow@61678
    33
     GT \<Rightarrow> deleteR x l a r |
nipkow@61678
    34
     EQ \<Rightarrow> combine l r)" |
nipkow@61224
    35
"deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" |
nipkow@61224
    36
"deleteL x l a r = R (delete x l) a r" |
nipkow@61224
    37
"deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | 
nipkow@61224
    38
"deleteR x l a r = R l a (delete x r)"
nipkow@61224
    39
nipkow@61224
    40
nipkow@61224
    41
subsection "Functional Correctness Proofs"
nipkow@61224
    42
nipkow@61224
    43
lemma inorder_bal:
nipkow@61224
    44
  "inorder(bal l a r) = inorder l @ a # inorder r"
nipkow@61231
    45
by(induction l a r rule: bal.induct) (auto)
nipkow@61224
    46
nipkow@61224
    47
lemma inorder_insert:
nipkow@61224
    48
  "sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
nipkow@61231
    49
by(induction a t rule: insert.induct) (auto simp: ins_list_simps inorder_bal)
nipkow@61224
    50
nipkow@61224
    51
lemma inorder_red: "inorder(red t) = inorder t"
nipkow@61231
    52
by(induction t) (auto)
nipkow@61224
    53
nipkow@61224
    54
lemma inorder_balL:
nipkow@61224
    55
  "inorder(balL l a r) = inorder l @ a # inorder r"
nipkow@61231
    56
by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_red)
nipkow@61224
    57
nipkow@61224
    58
lemma inorder_balR:
nipkow@61224
    59
  "inorder(balR l a r) = inorder l @ a # inorder r"
nipkow@61231
    60
by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_red)
nipkow@61224
    61
nipkow@61224
    62
lemma inorder_combine:
nipkow@61224
    63
  "inorder(combine l r) = inorder l @ inorder r"
nipkow@61224
    64
by(induction l r rule: combine.induct)
nipkow@61231
    65
  (auto simp: inorder_balL inorder_balR split: tree.split color.split)
nipkow@61224
    66
nipkow@61224
    67
lemma inorder_delete:
nipkow@61678
    68
 "sorted(inorder t) \<Longrightarrow>  inorder(delete x t) = del_list x (inorder t)"
nipkow@61224
    69
 "sorted(inorder l) \<Longrightarrow>  inorder(deleteL x l a r) =
nipkow@61678
    70
    del_list x (inorder l) @ a # inorder r"
nipkow@61224
    71
 "sorted(inorder r) \<Longrightarrow>  inorder(deleteR x l a r) =
nipkow@61224
    72
    inorder l @ a # del_list x (inorder r)"
nipkow@61224
    73
by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct)
nipkow@61231
    74
  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
nipkow@61224
    75
nipkow@61581
    76
nipkow@61224
    77
interpretation Set_by_Ordered
nipkow@61224
    78
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@61588
    79
and inorder = inorder and inv = "\<lambda>_. True"
nipkow@61224
    80
proof (standard, goal_cases)
nipkow@61224
    81
  case 1 show ?case by simp
nipkow@61224
    82
next
nipkow@61224
    83
  case 2 thus ?case by(simp add: isin_set)
nipkow@61224
    84
next
nipkow@61224
    85
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@61224
    86
next
nipkow@61678
    87
  case 4 thus ?case by(simp add: inorder_delete(1))
nipkow@61428
    88
qed (rule TrueI)+
nipkow@61224
    89
nipkow@61224
    90
end