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