author | wenzelm |
Thu, 03 Apr 2008 16:03:57 +0200 | |
changeset 26527 | c392354a1b79 |
parent 26192 | 52617dca8386 |
child 27368 | 9f90ac19e32b |
permissions | -rw-r--r-- |
26192
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1 |
(* Title: RBT.thy |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
2 |
ID: $Id$ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
3 |
Author: Markus Reiter, TU Muenchen |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
4 |
Author: Alexander Krauss, TU Muenchen |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
5 |
*) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
6 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
7 |
header {* Red-Black Trees *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
8 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
9 |
(*<*) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
10 |
theory RBT |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
11 |
imports Main AssocList |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
12 |
begin |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
13 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
14 |
datatype color = R | B |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
15 |
datatype ('a,'b)"rbt" = Empty | Tr color "('a,'b)rbt" 'a 'b "('a,'b)rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
16 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
17 |
(* Suchbaum-Eigenschaften *) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
18 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
19 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
20 |
pin_tree :: "'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
21 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
22 |
"pin_tree k v Empty = False" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
23 |
| "pin_tree k v (Tr c l x y r) = (k = x \<and> v = y \<or> pin_tree k v l \<or> pin_tree k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
24 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
25 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
26 |
keys :: "('k,'v) rbt \<Rightarrow> 'k set" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
27 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
28 |
"keys Empty = {}" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
29 |
| "keys (Tr _ l k _ r) = { k } \<union> keys l \<union> keys r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
30 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
31 |
lemma pint_keys: "pin_tree k v t \<Longrightarrow> k \<in> keys t" by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
32 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
33 |
primrec tlt :: "'a\<Colon>order \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
34 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
35 |
"tlt k Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
36 |
| "tlt k (Tr c lt kt v rt) = (kt < k \<and> tlt k lt \<and> tlt k rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
37 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
38 |
abbreviation tllt (infix "|\<guillemotleft>" 50) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
39 |
where "t |\<guillemotleft> x == tlt x t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
40 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
41 |
primrec tgt :: "'a\<Colon>order \<Rightarrow> ('a,'b) rbt \<Rightarrow> bool" (infix "\<guillemotleft>|" 50) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
42 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
43 |
"tgt k Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
44 |
| "tgt k (Tr c lt kt v rt) = (k < kt \<and> tgt k lt \<and> tgt k rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
45 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
46 |
lemma tlt_prop: "(t |\<guillemotleft> k) = (\<forall>x\<in>keys t. x < k)" by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
47 |
lemma tgt_prop: "(k \<guillemotleft>| t) = (\<forall>x\<in>keys t. k < x)" by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
48 |
lemmas tlgt_props = tlt_prop tgt_prop |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
49 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
50 |
lemmas tgt_nit = tgt_prop pint_keys |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
51 |
lemmas tlt_nit = tlt_prop pint_keys |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
52 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
53 |
lemma tlt_trans: "\<lbrakk> t |\<guillemotleft> x; x < y \<rbrakk> \<Longrightarrow> t |\<guillemotleft> y" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
54 |
and tgt_trans: "\<lbrakk> x < y; y \<guillemotleft>| t\<rbrakk> \<Longrightarrow> x \<guillemotleft>| t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
55 |
by (auto simp: tlgt_props) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
56 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
57 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
58 |
primrec st :: "('a::linorder, 'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
59 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
60 |
"st Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
61 |
| "st (Tr c l k v r) = (l |\<guillemotleft> k \<and> k \<guillemotleft>| r \<and> st l \<and> st r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
62 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
63 |
primrec map_of :: "('a\<Colon>linorder, 'b) rbt \<Rightarrow> 'a \<rightharpoonup> 'b" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
64 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
65 |
"map_of Empty k = None" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
66 |
| "map_of (Tr _ l x y r) k = (if k < x then map_of l k else if x < k then map_of r k else Some y)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
67 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
68 |
lemma map_of_tlt[simp]: "t |\<guillemotleft> k \<Longrightarrow> map_of t k = None" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
69 |
by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
70 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
71 |
lemma map_of_tgt[simp]: "k \<guillemotleft>| t \<Longrightarrow> map_of t k = None" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
72 |
by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
73 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
74 |
lemma mapof_keys: "st t \<Longrightarrow> dom (map_of t) = keys t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
75 |
by (induct t) (auto simp: dom_def tgt_prop tlt_prop) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
76 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
77 |
lemma mapof_pit: "st t \<Longrightarrow> (map_of t k = Some v) = pin_tree k v t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
78 |
by (induct t) (auto simp: tlt_prop tgt_prop pint_keys) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
79 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
80 |
lemma map_of_Empty: "map_of Empty = empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
81 |
by (rule ext) simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
82 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
83 |
(* a kind of extensionality *) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
84 |
lemma mapof_from_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
85 |
assumes st: "st t1" "st t2" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
86 |
and eq: "\<And>v. pin_tree (k\<Colon>'a\<Colon>linorder) v t1 = pin_tree k v t2" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
87 |
shows "map_of t1 k = map_of t2 k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
88 |
proof (cases "map_of t1 k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
89 |
case None |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
90 |
then have "\<And>v. \<not> pin_tree k v t1" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
91 |
by (simp add: mapof_pit[symmetric] st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
92 |
with None show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
93 |
by (cases "map_of t2 k") (auto simp: mapof_pit st eq) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
94 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
95 |
case (Some a) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
96 |
then show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
97 |
apply (cases "map_of t2 k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
98 |
apply (auto simp: mapof_pit st eq) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
99 |
by (auto simp add: mapof_pit[symmetric] st Some) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
100 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
101 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
102 |
subsection {* Red-black properties *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
103 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
104 |
primrec treec :: "('a,'b) rbt \<Rightarrow> color" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
105 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
106 |
"treec Empty = B" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
107 |
| "treec (Tr c _ _ _ _) = c" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
108 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
109 |
primrec inv1 :: "('a,'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
110 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
111 |
"inv1 Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
112 |
| "inv1 (Tr c lt k v rt) = (inv1 lt \<and> inv1 rt \<and> (c = B \<or> treec lt = B \<and> treec rt = B))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
113 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
114 |
(* Weaker version *) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
115 |
primrec inv1l :: "('a,'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
116 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
117 |
"inv1l Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
118 |
| "inv1l (Tr c l k v r) = (inv1 l \<and> inv1 r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
119 |
lemma [simp]: "inv1 t \<Longrightarrow> inv1l t" by (cases t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
120 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
121 |
primrec bh :: "('a,'b) rbt \<Rightarrow> nat" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
122 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
123 |
"bh Empty = 0" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
124 |
| "bh (Tr c lt k v rt) = (if c = B then Suc (bh lt) else bh lt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
125 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
126 |
primrec inv2 :: "('a,'b) rbt \<Rightarrow> bool" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
127 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
128 |
"inv2 Empty = True" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
129 |
| "inv2 (Tr c lt k v rt) = (inv2 lt \<and> inv2 rt \<and> bh lt = bh rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
130 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
131 |
definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
132 |
"isrbt t = (inv1 t \<and> inv2 t \<and> treec t = B \<and> st t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
133 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
134 |
lemma isrbt_st[simp]: "isrbt t \<Longrightarrow> st t" by (simp add: isrbt_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
135 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
136 |
lemma rbt_cases: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
137 |
obtains (Empty) "t = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
138 |
| (Red) l k v r where "t = Tr R l k v r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
139 |
| (Black) l k v r where "t = Tr B l k v r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
140 |
by (cases t, simp) (case_tac "color", auto) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
141 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
142 |
theorem Empty_isrbt[simp]: "isrbt Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
143 |
unfolding isrbt_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
144 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
145 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
146 |
subsection {* Insertion *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
147 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
148 |
fun (* slow, due to massive case splitting *) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
149 |
balance :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
150 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
151 |
"balance (Tr R a w x b) s t (Tr R c y z d) = Tr R (Tr B a w x b) s t (Tr B c y z d)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
152 |
"balance (Tr R (Tr R a w x b) s t c) y z d = Tr R (Tr B a w x b) s t (Tr B c y z d)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
153 |
"balance (Tr R a w x (Tr R b s t c)) y z d = Tr R (Tr B a w x b) s t (Tr B c y z d)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
154 |
"balance a w x (Tr R b s t (Tr R c y z d)) = Tr R (Tr B a w x b) s t (Tr B c y z d)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
155 |
"balance a w x (Tr R (Tr R b s t c) y z d) = Tr R (Tr B a w x b) s t (Tr B c y z d)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
156 |
"balance a s t b = Tr B a s t b" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
157 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
158 |
lemma balance_inv1: "\<lbrakk>inv1l l; inv1l r\<rbrakk> \<Longrightarrow> inv1 (balance l k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
159 |
by (induct l k v r rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
160 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
161 |
lemma balance_bh: "bh l = bh r \<Longrightarrow> bh (balance l k v r) = Suc (bh l)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
162 |
by (induct l k v r rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
163 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
164 |
lemma balance_inv2: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
165 |
assumes "inv2 l" "inv2 r" "bh l = bh r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
166 |
shows "inv2 (balance l k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
167 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
168 |
by (induct l k v r rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
169 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
170 |
lemma balance_tgt[simp]: "(v \<guillemotleft>| balance a k x b) = (v \<guillemotleft>| a \<and> v \<guillemotleft>| b \<and> v < k)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
171 |
by (induct a k x b rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
172 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
173 |
lemma balance_tlt[simp]: "(balance a k x b |\<guillemotleft> v) = (a |\<guillemotleft> v \<and> b |\<guillemotleft> v \<and> k < v)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
174 |
by (induct a k x b rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
175 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
176 |
lemma balance_st: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
177 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
178 |
assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
179 |
shows "st (balance l k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
180 |
using assms proof (induct l k v r rule: balance.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
181 |
case ("2_2" a x w b y t c z s va vb vd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
182 |
hence "y < z \<and> z \<guillemotleft>| Tr B va vb vd vc" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
183 |
by (auto simp add: tlgt_props) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
184 |
hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
185 |
with "2_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
186 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
187 |
case ("3_2" va vb vd vc x w b y s c z) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
188 |
from "3_2" have "x < y \<and> tlt x (Tr B va vb vd vc)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
189 |
by (simp add: tlt.simps tgt.simps) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
190 |
hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
191 |
with "3_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
192 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
193 |
case ("3_3" x w b y s c z t va vb vd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
194 |
from "3_3" have "y < z \<and> tgt z (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
195 |
hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
196 |
with "3_3" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
197 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
198 |
case ("3_4" vd ve vg vf x w b y s c z t va vb vii vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
199 |
hence "x < y \<and> tlt x (Tr B vd ve vg vf)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
200 |
hence 1: "tlt y (Tr B vd ve vg vf)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
201 |
from "3_4" have "y < z \<and> tgt z (Tr B va vb vii vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
202 |
hence "tgt y (Tr B va vb vii vc)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
203 |
with 1 "3_4" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
204 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
205 |
case ("4_2" va vb vd vc x w b y s c z t dd) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
206 |
hence "x < y \<and> tlt x (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
207 |
hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
208 |
with "4_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
209 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
210 |
case ("5_2" x w b y s c z t va vb vd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
211 |
hence "y < z \<and> tgt z (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
212 |
hence "tgt y (Tr B va vb vd vc)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
213 |
with "5_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
214 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
215 |
case ("5_3" va vb vd vc x w b y s c z t) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
216 |
hence "x < y \<and> tlt x (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
217 |
hence "tlt y (Tr B va vb vd vc)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
218 |
with "5_3" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
219 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
220 |
case ("5_4" va vb vg vc x w b y s c z t vd ve vii vf) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
221 |
hence "x < y \<and> tlt x (Tr B va vb vg vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
222 |
hence 1: "tlt y (Tr B va vb vg vc)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
223 |
from "5_4" have "y < z \<and> tgt z (Tr B vd ve vii vf)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
224 |
hence "tgt y (Tr B vd ve vii vf)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
225 |
with 1 "5_4" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
226 |
qed simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
227 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
228 |
lemma keys_balance[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
229 |
"keys (balance l k v r) = { k } \<union> keys l \<union> keys r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
230 |
by (induct l k v r rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
231 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
232 |
lemma balance_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
233 |
"pin_tree k x (balance l v y r) = (pin_tree k x l \<or> k = v \<and> x = y \<or> pin_tree k x r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
234 |
by (induct l v y r rule: balance.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
235 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
236 |
lemma map_of_balance[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
237 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
238 |
assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
239 |
shows "map_of (balance l k v r) x = map_of (Tr B l k v r) x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
240 |
by (rule mapof_from_pit) (auto simp:assms balance_pit balance_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
241 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
242 |
primrec paint :: "color \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
243 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
244 |
"paint c Empty = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
245 |
| "paint c (Tr _ l k v r) = Tr c l k v r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
246 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
247 |
lemma paint_inv1l[simp]: "inv1l t \<Longrightarrow> inv1l (paint c t)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
248 |
lemma paint_inv1[simp]: "inv1l t \<Longrightarrow> inv1 (paint B t)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
249 |
lemma paint_inv2[simp]: "inv2 t \<Longrightarrow> inv2 (paint c t)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
250 |
lemma paint_treec[simp]: "treec (paint B t) = B" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
251 |
lemma paint_st[simp]: "st t \<Longrightarrow> st (paint c t)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
252 |
lemma paint_pit[simp]: "pin_tree k x (paint c t) = pin_tree k x t" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
253 |
lemma paint_mapof[simp]: "map_of (paint c t) = map_of t" by (rule ext) (cases t, auto) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
254 |
lemma paint_tgt[simp]: "(v \<guillemotleft>| paint c t) = (v \<guillemotleft>| t)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
255 |
lemma paint_tlt[simp]: "(paint c t |\<guillemotleft> v) = (t |\<guillemotleft> v)" by (cases t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
256 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
257 |
fun |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
258 |
ins :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
259 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
260 |
"ins f k v Empty = Tr R Empty k v Empty" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
261 |
"ins f k v (Tr B l x y r) = (if k < x then balance (ins f k v l) x y r |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
262 |
else if k > x then balance l x y (ins f k v r) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
263 |
else Tr B l x (f k y v) r)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
264 |
"ins f k v (Tr R l x y r) = (if k < x then Tr R (ins f k v l) x y r |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
265 |
else if k > x then Tr R l x y (ins f k v r) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
266 |
else Tr R l x (f k y v) r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
267 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
268 |
lemma ins_inv1_inv2: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
269 |
assumes "inv1 t" "inv2 t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
270 |
shows "inv2 (ins f k x t)" "bh (ins f k x t) = bh t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
271 |
"treec t = B \<Longrightarrow> inv1 (ins f k x t)" "inv1l (ins f k x t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
272 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
273 |
by (induct f k x t rule: ins.induct) (auto simp: balance_inv1 balance_inv2 balance_bh) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
274 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
275 |
lemma ins_tgt[simp]: "(v \<guillemotleft>| ins f k x t) = (v \<guillemotleft>| t \<and> k > v)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
276 |
by (induct f k x t rule: ins.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
277 |
lemma ins_tlt[simp]: "(ins f k x t |\<guillemotleft> v) = (t |\<guillemotleft> v \<and> k < v)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
278 |
by (induct f k x t rule: ins.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
279 |
lemma ins_st[simp]: "st t \<Longrightarrow> st (ins f k x t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
280 |
by (induct f k x t rule: ins.induct) (auto simp: balance_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
281 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
282 |
lemma keys_ins: "keys (ins f k v t) = { k } \<union> keys t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
283 |
by (induct f k v t rule: ins.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
284 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
285 |
lemma map_of_ins: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
286 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
287 |
assumes "st t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
288 |
shows "map_of (ins f k v t) x = ((map_of t)(k |-> case map_of t k of None \<Rightarrow> v |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
289 |
| Some w \<Rightarrow> f k w v)) x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
290 |
using assms by (induct f k v t rule: ins.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
291 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
292 |
definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
293 |
insertwithkey :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
294 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
295 |
"insertwithkey f k v t = paint B (ins f k v t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
296 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
297 |
lemma insertwk_st: "st t \<Longrightarrow> st (insertwithkey f k x t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
298 |
by (auto simp: insertwithkey_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
299 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
300 |
theorem insertwk_isrbt: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
301 |
assumes inv: "isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
302 |
shows "isrbt (insertwithkey f k x t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
303 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
304 |
unfolding insertwithkey_def isrbt_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
305 |
by (auto simp: ins_inv1_inv2) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
306 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
307 |
lemma map_of_insertwk: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
308 |
assumes "st t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
309 |
shows "map_of (insertwithkey f k v t) x = ((map_of t)(k |-> case map_of t k of None \<Rightarrow> v |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
310 |
| Some w \<Rightarrow> f k w v)) x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
311 |
unfolding insertwithkey_def using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
312 |
by (simp add:map_of_ins) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
313 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
314 |
definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
315 |
insertw_def: "insertwith f = insertwithkey (\<lambda>_. f)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
316 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
317 |
lemma insertw_st: "st t \<Longrightarrow> st (insertwith f k v t)" by (simp add: insertwk_st insertw_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
318 |
theorem insertw_isrbt: "isrbt t \<Longrightarrow> isrbt (insertwith f k v t)" by (simp add: insertwk_isrbt insertw_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
319 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
320 |
lemma map_of_insertw: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
321 |
assumes "isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
322 |
shows "map_of (insertwith f k v t) = (map_of t)(k \<mapsto> (if k:dom (map_of t) then f (the (map_of t k)) v else v))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
323 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
324 |
unfolding insertw_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
325 |
by (rule_tac ext) (cases "map_of t k", auto simp:map_of_insertwk dom_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
326 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
327 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
328 |
definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
329 |
"insrt k v t = insertwithkey (\<lambda>_ _ nv. nv) k v t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
330 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
331 |
lemma insrt_st: "st t \<Longrightarrow> st (insrt k v t)" by (simp add: insertwk_st insrt_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
332 |
theorem insrt_isrbt: "isrbt t \<Longrightarrow> isrbt (insrt k v t)" by (simp add: insertwk_isrbt insrt_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
333 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
334 |
lemma map_of_insert: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
335 |
assumes "isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
336 |
shows "map_of (insrt k v t) = (map_of t)(k\<mapsto>v)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
337 |
unfolding insrt_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
338 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
339 |
by (rule_tac ext) (simp add: map_of_insertwk split:option.split) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
340 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
341 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
342 |
subsection {* Deletion *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
343 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
344 |
(*definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
345 |
[simp]: "ibn t = (bh t > 0 \<and> treec t = B)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
346 |
*) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
347 |
lemma bh_paintR'[simp]: "treec t = B \<Longrightarrow> bh (paint R t) = bh t - 1" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
348 |
by (cases t rule: rbt_cases) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
349 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
350 |
fun |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
351 |
balleft :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
352 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
353 |
"balleft (Tr R a k x b) s y c = Tr R (Tr B a k x b) s y c" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
354 |
"balleft bl k x (Tr B a s y b) = balance bl k x (Tr R a s y b)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
355 |
"balleft bl k x (Tr R (Tr B a s y b) t z c) = Tr R (Tr B bl k x a) s y (balance b t z (paint R c))" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
356 |
"balleft t k x s = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
357 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
358 |
lemma balleft_inv2_with_inv1: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
359 |
assumes "inv2 lt" "inv2 rt" "bh lt + 1 = bh rt" "inv1 rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
360 |
shows "bh (balleft lt k v rt) = bh lt + 1" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
361 |
and "inv2 (balleft lt k v rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
362 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
363 |
by (induct lt k v rt rule: balleft.induct) (auto simp: balance_inv2 balance_bh) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
364 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
365 |
lemma balleft_inv2_app: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
366 |
assumes "inv2 lt" "inv2 rt" "bh lt + 1 = bh rt" "treec rt = B" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
367 |
shows "inv2 (balleft lt k v rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
368 |
"bh (balleft lt k v rt) = bh rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
369 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
370 |
by (induct lt k v rt rule: balleft.induct) (auto simp add: balance_inv2 balance_bh)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
371 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
372 |
lemma balleft_inv1: "\<lbrakk>inv1l a; inv1 b; treec b = B\<rbrakk> \<Longrightarrow> inv1 (balleft a k x b)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
373 |
by (induct a k x b rule: balleft.induct) (simp add: balance_inv1)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
374 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
375 |
lemma balleft_inv1l: "\<lbrakk> inv1l lt; inv1 rt \<rbrakk> \<Longrightarrow> inv1l (balleft lt k x rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
376 |
by (induct lt k x rt rule: balleft.induct) (auto simp: balance_inv1) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
377 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
378 |
lemma balleft_st: "\<lbrakk> st l; st r; tlt k l; tgt k r \<rbrakk> \<Longrightarrow> st (balleft l k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
379 |
apply (induct l k v r rule: balleft.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
380 |
apply (auto simp: balance_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
381 |
apply (unfold tgt_prop tlt_prop) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
382 |
by force+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
383 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
384 |
lemma balleft_tgt: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
385 |
fixes k :: "'a::order" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
386 |
assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
387 |
shows "k \<guillemotleft>| balleft a x t b" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
388 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
389 |
by (induct a x t b rule: balleft.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
390 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
391 |
lemma balleft_tlt: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
392 |
fixes k :: "'a::order" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
393 |
assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
394 |
shows "balleft a x t b |\<guillemotleft> k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
395 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
396 |
by (induct a x t b rule: balleft.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
397 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
398 |
lemma balleft_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
399 |
assumes "inv1l l" "inv1 r" "bh l + 1 = bh r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
400 |
shows "pin_tree k v (balleft l a b r) = (pin_tree k v l \<or> k = a \<and> v = b \<or> pin_tree k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
401 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
402 |
by (induct l k v r rule: balleft.induct) (auto simp: balance_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
403 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
404 |
fun |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
405 |
balright :: "('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
406 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
407 |
"balright a k x (Tr R b s y c) = Tr R a k x (Tr B b s y c)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
408 |
"balright (Tr B a k x b) s y bl = balance (Tr R a k x b) s y bl" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
409 |
"balright (Tr R a k x (Tr B b s y c)) t z bl = Tr R (balance (paint R a) k x b) s y (Tr B c t z bl)" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
410 |
"balright t k x s = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
411 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
412 |
lemma balright_inv2_with_inv1: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
413 |
assumes "inv2 lt" "inv2 rt" "bh lt = bh rt + 1" "inv1 lt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
414 |
shows "inv2 (balright lt k v rt) \<and> bh (balright lt k v rt) = bh lt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
415 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
416 |
by (induct lt k v rt rule: balright.induct) (auto simp: balance_inv2 balance_bh) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
417 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
418 |
lemma balright_inv1: "\<lbrakk>inv1 a; inv1l b; treec a = B\<rbrakk> \<Longrightarrow> inv1 (balright a k x b)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
419 |
by (induct a k x b rule: balright.induct) (simp add: balance_inv1)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
420 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
421 |
lemma balright_inv1l: "\<lbrakk> inv1 lt; inv1l rt \<rbrakk> \<Longrightarrow>inv1l (balright lt k x rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
422 |
by (induct lt k x rt rule: balright.induct) (auto simp: balance_inv1) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
423 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
424 |
lemma balright_st: "\<lbrakk> st l; st r; tlt k l; tgt k r \<rbrakk> \<Longrightarrow> st (balright l k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
425 |
apply (induct l k v r rule: balright.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
426 |
apply (auto simp:balance_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
427 |
apply (unfold tlt_prop tgt_prop) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
428 |
by force+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
429 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
430 |
lemma balright_tgt: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
431 |
fixes k :: "'a::order" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
432 |
assumes "k \<guillemotleft>| a" "k \<guillemotleft>| b" "k < x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
433 |
shows "k \<guillemotleft>| balright a x t b" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
434 |
using assms by (induct a x t b rule: balright.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
435 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
436 |
lemma balright_tlt: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
437 |
fixes k :: "'a::order" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
438 |
assumes "a |\<guillemotleft> k" "b |\<guillemotleft> k" "x < k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
439 |
shows "balright a x t b |\<guillemotleft> k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
440 |
using assms by (induct a x t b rule: balright.induct) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
441 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
442 |
lemma balright_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
443 |
assumes "inv1 l" "inv1l r" "bh l = bh r + 1" "inv2 l" "inv2 r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
444 |
shows "pin_tree x y (balright l k v r) = (pin_tree x y l \<or> x = k \<and> y = v \<or> pin_tree x y r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
445 |
using assms by (induct l k v r rule: balright.induct) (auto simp: balance_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
446 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
447 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
448 |
text {* app *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
449 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
450 |
fun |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
451 |
app :: "('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
452 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
453 |
"app Empty x = x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
454 |
| "app x Empty = x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
455 |
| "app (Tr R a k x b) (Tr R c s y d) = (case (app b c) of |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
456 |
Tr R b2 t z c2 \<Rightarrow> (Tr R (Tr R a k x b2) t z (Tr R c2 s y d)) | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
457 |
bc \<Rightarrow> Tr R a k x (Tr R bc s y d))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
458 |
| "app (Tr B a k x b) (Tr B c s y d) = (case (app b c) of |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
459 |
Tr R b2 t z c2 \<Rightarrow> Tr R (Tr B a k x b2) t z (Tr B c2 s y d) | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
460 |
bc \<Rightarrow> balleft a k x (Tr B bc s y d))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
461 |
| "app a (Tr R b k x c) = Tr R (app a b) k x c" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
462 |
| "app (Tr R a k x b) c = Tr R a k x (app b c)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
463 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
464 |
lemma app_inv2: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
465 |
assumes "inv2 lt" "inv2 rt" "bh lt = bh rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
466 |
shows "bh (app lt rt) = bh lt" "inv2 (app lt rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
467 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
468 |
by (induct lt rt rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
469 |
(auto simp: balleft_inv2_app split: rbt.splits color.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
470 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
471 |
lemma app_inv1: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
472 |
assumes "inv1 lt" "inv1 rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
473 |
shows "treec lt = B \<Longrightarrow> treec rt = B \<Longrightarrow> inv1 (app lt rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
474 |
"inv1l (app lt rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
475 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
476 |
by (induct lt rt rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
477 |
(auto simp: balleft_inv1 split: rbt.splits color.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
478 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
479 |
lemma app_tgt[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
480 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
481 |
assumes "k \<guillemotleft>| l" "k \<guillemotleft>| r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
482 |
shows "k \<guillemotleft>| app l r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
483 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
484 |
by (induct l r rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
485 |
(auto simp: balleft_tgt split:rbt.splits color.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
486 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
487 |
lemma app_tlt[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
488 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
489 |
assumes "l |\<guillemotleft> k" "r |\<guillemotleft> k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
490 |
shows "app l r |\<guillemotleft> k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
491 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
492 |
by (induct l r rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
493 |
(auto simp: balleft_tlt split:rbt.splits color.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
494 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
495 |
lemma app_st: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
496 |
fixes k :: "'a::linorder" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
497 |
assumes "st l" "st r" "l |\<guillemotleft> k" "k \<guillemotleft>| r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
498 |
shows "st (app l r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
499 |
using assms proof (induct l r rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
500 |
case (3 a x v b c y w d) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
501 |
hence ineqs: "a |\<guillemotleft> x" "x \<guillemotleft>| b" "b |\<guillemotleft> k" "k \<guillemotleft>| c" "c |\<guillemotleft> y" "y \<guillemotleft>| d" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
502 |
by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
503 |
with 3 |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
504 |
show ?case |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
505 |
apply (cases "app b c" rule: rbt_cases) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
506 |
apply auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
507 |
by (metis app_tgt app_tlt ineqs ineqs tlt.simps(2) tgt.simps(2) tgt_trans tlt_trans)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
508 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
509 |
case (4 a x v b c y w d) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
510 |
hence "x < k \<and> tgt k c" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
511 |
hence "tgt x c" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
512 |
with 4 have 2: "tgt x (app b c)" by (simp add: app_tgt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
513 |
from 4 have "k < y \<and> tlt k b" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
514 |
hence "tlt y b" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
515 |
with 4 have 3: "tlt y (app b c)" by (simp add: app_tlt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
516 |
show ?case |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
517 |
proof (cases "app b c" rule: rbt_cases) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
518 |
case Empty |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
519 |
from 4 have "x < y \<and> tgt y d" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
520 |
hence "tgt x d" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
521 |
with 4 Empty have "st a" and "st (Tr B Empty y w d)" and "tlt x a" and "tgt x (Tr B Empty y w d)" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
522 |
with Empty show ?thesis by (simp add: balleft_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
523 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
524 |
case (Red lta va ka rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
525 |
with 2 4 have "x < va \<and> tlt x a" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
526 |
hence 5: "tlt va a" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
527 |
from Red 3 4 have "va < y \<and> tgt y d" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
528 |
hence "tgt va d" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
529 |
with Red 2 3 4 5 show ?thesis by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
530 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
531 |
case (Black lta va ka rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
532 |
from 4 have "x < y \<and> tgt y d" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
533 |
hence "tgt x d" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
534 |
with Black 2 3 4 have "st a" and "st (Tr B (app b c) y w d)" and "tlt x a" and "tgt x (Tr B (app b c) y w d)" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
535 |
with Black show ?thesis by (simp add: balleft_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
536 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
537 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
538 |
case (5 va vb vd vc b x w c) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
539 |
hence "k < x \<and> tlt k (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
540 |
hence "tlt x (Tr B va vb vd vc)" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
541 |
with 5 show ?case by (simp add: app_tlt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
542 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
543 |
case (6 a x v b va vb vd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
544 |
hence "x < k \<and> tgt k (Tr B va vb vd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
545 |
hence "tgt x (Tr B va vb vd vc)" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
546 |
with 6 show ?case by (simp add: app_tgt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
547 |
qed simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
548 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
549 |
lemma app_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
550 |
assumes "inv2 l" "inv2 r" "bh l = bh r" "inv1 l" "inv1 r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
551 |
shows "pin_tree k v (app l r) = (pin_tree k v l \<or> pin_tree k v r)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
552 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
553 |
proof (induct l r rule: app.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
554 |
case (4 _ _ _ b c) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
555 |
hence a: "bh (app b c) = bh b" by (simp add: app_inv2) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
556 |
from 4 have b: "inv1l (app b c)" by (simp add: app_inv1) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
557 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
558 |
show ?case |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
559 |
proof (cases "app b c" rule: rbt_cases) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
560 |
case Empty |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
561 |
with 4 a show ?thesis by (auto simp: balleft_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
562 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
563 |
case (Red lta ka va rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
564 |
with 4 show ?thesis by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
565 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
566 |
case (Black lta ka va rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
567 |
with a b 4 show ?thesis by (auto simp: balleft_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
568 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
569 |
qed (auto split: rbt.splits color.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
570 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
571 |
fun |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
572 |
delformLeft :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
573 |
delformRight :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" and |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
574 |
del :: "('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
575 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
576 |
"del x Empty = Empty" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
577 |
"del x (Tr c a y s b) = (if x < y then delformLeft x a y s b else (if x > y then delformRight x a y s b else app a b))" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
578 |
"delformLeft x (Tr B lt z v rt) y s b = balleft (del x (Tr B lt z v rt)) y s b" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
579 |
"delformLeft x a y s b = Tr R (del x a) y s b" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
580 |
"delformRight x a y s (Tr B lt z v rt) = balright a y s (del x (Tr B lt z v rt))" | |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
581 |
"delformRight x a y s b = Tr R a y s (del x b)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
582 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
583 |
lemma |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
584 |
assumes "inv2 lt" "inv1 lt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
585 |
shows |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
586 |
"\<lbrakk>inv2 rt; bh lt = bh rt; inv1 rt\<rbrakk> \<Longrightarrow> |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
587 |
inv2 (delformLeft x lt k v rt) \<and> bh (delformLeft x lt k v rt) = bh lt \<and> (treec lt = B \<and> treec rt = B \<and> inv1 (delformLeft x lt k v rt) \<or> (treec lt \<noteq> B \<or> treec rt \<noteq> B) \<and> inv1l (delformLeft x lt k v rt))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
588 |
and "\<lbrakk>inv2 rt; bh lt = bh rt; inv1 rt\<rbrakk> \<Longrightarrow> |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
589 |
inv2 (delformRight x lt k v rt) \<and> bh (delformRight x lt k v rt) = bh lt \<and> (treec lt = B \<and> treec rt = B \<and> inv1 (delformRight x lt k v rt) \<or> (treec lt \<noteq> B \<or> treec rt \<noteq> B) \<and> inv1l (delformRight x lt k v rt))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
590 |
and del_inv1_inv2: "inv2 (del x lt) \<and> (treec lt = R \<and> bh (del x lt) = bh lt \<and> inv1 (del x lt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
591 |
\<or> treec lt = B \<and> bh (del x lt) = bh lt - 1 \<and> inv1l (del x lt))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
592 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
593 |
proof (induct x lt k v rt and x lt k v rt and x lt rule: delformLeft_delformRight_del.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
594 |
case (2 y c _ y') |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
595 |
have "y = y' \<or> y < y' \<or> y > y'" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
596 |
thus ?case proof (elim disjE) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
597 |
assume "y = y'" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
598 |
with 2 show ?thesis by (cases c) (simp add: app_inv2 app_inv1)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
599 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
600 |
assume "y < y'" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
601 |
with 2 show ?thesis by (cases c) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
602 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
603 |
assume "y' < y" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
604 |
with 2 show ?thesis by (cases c) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
605 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
606 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
607 |
case (3 y lt z v rta y' ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
608 |
thus ?case by (cases "treec (Tr B lt z v rta) = B \<and> treec bb = B") (simp add: balleft_inv2_with_inv1 balleft_inv1 balleft_inv1l)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
609 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
610 |
case (5 y a y' ss lt z v rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
611 |
thus ?case by (cases "treec a = B \<and> treec (Tr B lt z v rta) = B") (simp add: balright_inv2_with_inv1 balright_inv1 balright_inv1l)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
612 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
613 |
case ("6_1" y a y' ss) thus ?case by (cases "treec a = B \<and> treec Empty = B") simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
614 |
qed auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
615 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
616 |
lemma |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
617 |
delformLeft_tlt: "\<lbrakk>tlt v lt; tlt v rt; k < v\<rbrakk> \<Longrightarrow> tlt v (delformLeft x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
618 |
and delformRight_tlt: "\<lbrakk>tlt v lt; tlt v rt; k < v\<rbrakk> \<Longrightarrow> tlt v (delformRight x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
619 |
and del_tlt: "tlt v lt \<Longrightarrow> tlt v (del x lt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
620 |
by (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
621 |
(auto simp: balleft_tlt balright_tlt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
622 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
623 |
lemma delformLeft_tgt: "\<lbrakk>tgt v lt; tgt v rt; k > v\<rbrakk> \<Longrightarrow> tgt v (delformLeft x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
624 |
and delformRight_tgt: "\<lbrakk>tgt v lt; tgt v rt; k > v\<rbrakk> \<Longrightarrow> tgt v (delformRight x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
625 |
and del_tgt: "tgt v lt \<Longrightarrow> tgt v (del x lt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
626 |
by (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
627 |
(auto simp: balleft_tgt balright_tgt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
628 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
629 |
lemma "\<lbrakk>st lt; st rt; tlt k lt; tgt k rt\<rbrakk> \<Longrightarrow> st (delformLeft x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
630 |
and "\<lbrakk>st lt; st rt; tlt k lt; tgt k rt\<rbrakk> \<Longrightarrow> st (delformRight x lt k y rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
631 |
and del_st: "st lt \<Longrightarrow> st (del x lt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
632 |
proof (induct x lt k y rt and x lt k y rt and x lt rule: delformLeft_delformRight_del.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
633 |
case (3 x lta zz v rta yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
634 |
from 3 have "tlt yy (Tr B lta zz v rta)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
635 |
hence "tlt yy (del x (Tr B lta zz v rta))" by (rule del_tlt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
636 |
with 3 show ?case by (simp add: balleft_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
637 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
638 |
case ("4_2" x vaa vbb vdd vc yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
639 |
hence "tlt yy (Tr R vaa vbb vdd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
640 |
hence "tlt yy (del x (Tr R vaa vbb vdd vc))" by (rule del_tlt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
641 |
with "4_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
642 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
643 |
case (5 x aa yy ss lta zz v rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
644 |
hence "tgt yy (Tr B lta zz v rta)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
645 |
hence "tgt yy (del x (Tr B lta zz v rta))" by (rule del_tgt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
646 |
with 5 show ?case by (simp add: balright_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
647 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
648 |
case ("6_2" x aa yy ss vaa vbb vdd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
649 |
hence "tgt yy (Tr R vaa vbb vdd vc)" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
650 |
hence "tgt yy (del x (Tr R vaa vbb vdd vc))" by (rule del_tgt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
651 |
with "6_2" show ?case by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
652 |
qed (auto simp: app_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
653 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
654 |
lemma "\<lbrakk>st lt; st rt; tlt kt lt; tgt kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bh lt = bh rt; x < kt\<rbrakk> \<Longrightarrow> pin_tree k v (delformLeft x lt kt y rt) = (False \<or> (x \<noteq> k \<and> pin_tree k v (Tr c lt kt y rt)))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
655 |
and "\<lbrakk>st lt; st rt; tlt kt lt; tgt kt rt; inv1 lt; inv1 rt; inv2 lt; inv2 rt; bh lt = bh rt; x > kt\<rbrakk> \<Longrightarrow> pin_tree k v (delformRight x lt kt y rt) = (False \<or> (x \<noteq> k \<and> pin_tree k v (Tr c lt kt y rt)))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
656 |
and del_pit: "\<lbrakk>st t; inv1 t; inv2 t\<rbrakk> \<Longrightarrow> pin_tree k v (del x t) = (False \<or> (x \<noteq> k \<and> pin_tree k v t))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
657 |
proof (induct x lt kt y rt and x lt kt y rt and x t rule: delformLeft_delformRight_del.induct) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
658 |
case (2 xx c aa yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
659 |
have "xx = yy \<or> xx < yy \<or> xx > yy" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
660 |
from this 2 show ?case proof (elim disjE) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
661 |
assume "xx = yy" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
662 |
with 2 show ?thesis proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
663 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
664 |
from 2 `xx = yy` `xx = k` have "st (Tr c aa yy ss bb) \<and> k = yy" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
665 |
hence "\<not> pin_tree k v aa" "\<not> pin_tree k v bb" by (auto simp: tlt_nit tgt_prop) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
666 |
with `xx = yy` 2 `xx = k` show ?thesis by (simp add: app_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
667 |
qed (simp add: app_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
668 |
qed simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
669 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
670 |
case (3 xx lta zz vv rta yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
671 |
def mt[simp]: mt == "Tr B lta zz vv rta" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
672 |
from 3 have "inv2 mt \<and> inv1 mt" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
673 |
hence "inv2 (del xx mt) \<and> (treec mt = R \<and> bh (del xx mt) = bh mt \<and> inv1 (del xx mt) \<or> treec mt = B \<and> bh (del xx mt) = bh mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
674 |
with 3 have 4: "pin_tree k v (delformLeft xx mt yy ss bb) = (False \<or> xx \<noteq> k \<and> pin_tree k v mt \<or> (k = yy \<and> v = ss) \<or> pin_tree k v bb)" by (simp add: balleft_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
675 |
thus ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
676 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
677 |
from 3 True have "tgt yy bb \<and> yy > k" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
678 |
hence "tgt k bb" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
679 |
with 3 4 True show ?thesis by (auto simp: tgt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
680 |
qed auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
681 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
682 |
case ("4_1" xx yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
683 |
show ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
684 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
685 |
with "4_1" have "tgt yy bb \<and> k < yy" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
686 |
hence "tgt k bb" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
687 |
with "4_1" `xx = k` |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
688 |
have "pin_tree k v (Tr R Empty yy ss bb) = pin_tree k v Empty" by (auto simp: tgt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
689 |
thus ?thesis by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
690 |
qed simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
691 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
692 |
case ("4_2" xx vaa vbb vdd vc yy ss bb) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
693 |
thus ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
694 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
695 |
with "4_2" have "k < yy \<and> tgt yy bb" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
696 |
hence "tgt k bb" by (blast dest: tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
697 |
with True "4_2" show ?thesis by (auto simp: tgt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
698 |
qed simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
699 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
700 |
case (5 xx aa yy ss lta zz vv rta) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
701 |
def mt[simp]: mt == "Tr B lta zz vv rta" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
702 |
from 5 have "inv2 mt \<and> inv1 mt" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
703 |
hence "inv2 (del xx mt) \<and> (treec mt = R \<and> bh (del xx mt) = bh mt \<and> inv1 (del xx mt) \<or> treec mt = B \<and> bh (del xx mt) = bh mt - 1 \<and> inv1l (del xx mt))" by (blast dest: del_inv1_inv2) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
704 |
with 5 have 3: "pin_tree k v (delformRight xx aa yy ss mt) = (pin_tree k v aa \<or> (k = yy \<and> v = ss) \<or> False \<or> xx \<noteq> k \<and> pin_tree k v mt)" by (simp add: balright_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
705 |
thus ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
706 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
707 |
from 5 True have "tlt yy aa \<and> yy < k" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
708 |
hence "tlt k aa" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
709 |
with 3 5 True show ?thesis by (auto simp: tlt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
710 |
qed auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
711 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
712 |
case ("6_1" xx aa yy ss) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
713 |
show ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
714 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
715 |
with "6_1" have "tlt yy aa \<and> k > yy" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
716 |
hence "tlt k aa" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
717 |
with "6_1" `xx = k` show ?thesis by (auto simp: tlt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
718 |
qed simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
719 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
720 |
case ("6_2" xx aa yy ss vaa vbb vdd vc) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
721 |
thus ?case proof (cases "xx = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
722 |
case True |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
723 |
with "6_2" have "k > yy \<and> tlt yy aa" by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
724 |
hence "tlt k aa" by (blast dest: tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
725 |
with True "6_2" show ?thesis by (auto simp: tlt_nit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
726 |
qed simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
727 |
qed simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
728 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
729 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
730 |
definition delete where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
731 |
delete_def: "delete k t = paint B (del k t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
732 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
733 |
theorem delete_isrbt[simp]: assumes "isrbt t" shows "isrbt (delete k t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
734 |
proof - |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
735 |
from assms have "inv2 t" and "inv1 t" unfolding isrbt_def by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
736 |
hence "inv2 (del k t) \<and> (treec t = R \<and> bh (del k t) = bh t \<and> inv1 (del k t) \<or> treec t = B \<and> bh (del k t) = bh t - 1 \<and> inv1l (del k t))" by (rule del_inv1_inv2) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
737 |
hence "inv2 (del k t) \<and> inv1l (del k t)" by (cases "treec t") auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
738 |
with assms show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
739 |
unfolding isrbt_def delete_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
740 |
by (auto intro: paint_st del_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
741 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
742 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
743 |
lemma delete_pit: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
744 |
assumes "isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
745 |
shows "pin_tree k v (delete x t) = (x \<noteq> k \<and> pin_tree k v t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
746 |
using assms unfolding isrbt_def delete_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
747 |
by (auto simp: del_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
748 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
749 |
lemma map_of_delete: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
750 |
assumes isrbt: "isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
751 |
shows "map_of (delete k t) = (map_of t)|`(-{k})" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
752 |
proof |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
753 |
fix x |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
754 |
show "map_of (delete k t) x = (map_of t |` (-{k})) x" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
755 |
proof (cases "x = k") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
756 |
assume "x = k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
757 |
with isrbt show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
758 |
by (cases "map_of (delete k t) k") (auto simp: mapof_pit delete_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
759 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
760 |
assume "x \<noteq> k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
761 |
thus ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
762 |
by auto (metis isrbt delete_isrbt delete_pit isrbt_st mapof_from_pit) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
763 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
764 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
765 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
766 |
subsection {* Union *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
767 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
768 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
769 |
unionwithkey :: "('a\<Colon>linorder \<Rightarrow> 'b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
770 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
771 |
"unionwithkey f t Empty = t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
772 |
| "unionwithkey f t (Tr c lt k v rt) = unionwithkey f (unionwithkey f (insertwithkey f k v t) lt) rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
773 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
774 |
lemma unionwk_st: "st lt \<Longrightarrow> st (unionwithkey f lt rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
775 |
by (induct rt arbitrary: lt) (auto simp: insertwk_st) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
776 |
theorem unionwk_isrbt[simp]: "isrbt lt \<Longrightarrow> isrbt (unionwithkey f lt rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
777 |
by (induct rt arbitrary: lt) (simp add: insertwk_isrbt)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
778 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
779 |
definition |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
780 |
unionwith where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
781 |
"unionwith f = unionwithkey (\<lambda>_. f)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
782 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
783 |
theorem unionw_isrbt: "isrbt lt \<Longrightarrow> isrbt (unionwith f lt rt)" unfolding unionwith_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
784 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
785 |
definition union where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
786 |
"union = unionwithkey (%_ _ rv. rv)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
787 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
788 |
theorem union_isrbt: "isrbt lt \<Longrightarrow> isrbt (union lt rt)" unfolding union_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
789 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
790 |
lemma union_Tr[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
791 |
"union t (Tr c lt k v rt) = union (union (insrt k v t) lt) rt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
792 |
unfolding union_def insrt_def |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
793 |
by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
794 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
795 |
lemma map_of_union: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
796 |
assumes "isrbt s" "st t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
797 |
shows "map_of (union s t) = map_of s ++ map_of t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
798 |
using assms |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
799 |
proof (induct t arbitrary: s) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
800 |
case Empty thus ?case by (auto simp: union_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
801 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
802 |
case (Tr c l k v r s) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
803 |
hence strl: "st r" "st l" "l |\<guillemotleft> k" "k \<guillemotleft>| r" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
804 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
805 |
have meq: "map_of s(k \<mapsto> v) ++ map_of l ++ map_of r = |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
806 |
map_of s ++ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
807 |
(\<lambda>a. if a < k then map_of l a |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
808 |
else if k < a then map_of r a else Some v)" (is "?m1 = ?m2") |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
809 |
proof (rule ext) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
810 |
fix a |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
811 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
812 |
have "k < a \<or> k = a \<or> k > a" by auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
813 |
thus "?m1 a = ?m2 a" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
814 |
proof (elim disjE) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
815 |
assume "k < a" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
816 |
with `l |\<guillemotleft> k` have "l |\<guillemotleft> a" by (rule tlt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
817 |
with `k < a` show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
818 |
by (auto simp: map_add_def split: option.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
819 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
820 |
assume "k = a" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
821 |
with `l |\<guillemotleft> k` `k \<guillemotleft>| r` |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
822 |
show ?thesis by (auto simp: map_add_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
823 |
next |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
824 |
assume "a < k" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
825 |
from this `k \<guillemotleft>| r` have "a \<guillemotleft>| r" by (rule tgt_trans) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
826 |
with `a < k` show ?thesis |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
827 |
by (auto simp: map_add_def split: option.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
828 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
829 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
830 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
831 |
from Tr |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
832 |
have IHs: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
833 |
"map_of (union (union (insrt k v s) l) r) = map_of (union (insrt k v s) l) ++ map_of r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
834 |
"map_of (union (insrt k v s) l) = map_of (insrt k v s) ++ map_of l" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
835 |
by (auto intro: union_isrbt insrt_isrbt) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
836 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
837 |
with meq show ?case |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
838 |
by (auto simp: map_of_insert[OF Tr(3)]) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
839 |
qed |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
840 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
841 |
subsection {* Adjust *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
842 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
843 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
844 |
adjustwithkey :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a\<Colon>linorder) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'b) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
845 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
846 |
"adjustwithkey f k Empty = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
847 |
| "adjustwithkey f k (Tr c lt x v rt) = (if k < x then (Tr c (adjustwithkey f k lt) x v rt) else if k > x then (Tr c lt x v (adjustwithkey f k rt)) else (Tr c lt x (f x v) rt))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
848 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
849 |
lemma adjustwk_treec: "treec (adjustwithkey f k t) = treec t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
850 |
lemma adjustwk_inv1: "inv1 (adjustwithkey f k t) = inv1 t" by (induct t) (simp add: adjustwk_treec)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
851 |
lemma adjustwk_inv2: "inv2 (adjustwithkey f k t) = inv2 t" "bh (adjustwithkey f k t) = bh t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
852 |
lemma adjustwk_tgt: "tgt k (adjustwithkey f kk t) = tgt k t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
853 |
lemma adjustwk_tlt: "tlt k (adjustwithkey f kk t) = tlt k t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
854 |
lemma adjustwk_st: "st (adjustwithkey f k t) = st t" by (induct t) (simp add: adjustwk_tlt adjustwk_tgt)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
855 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
856 |
theorem adjustwk_isrbt[simp]: "isrbt (adjustwithkey f k t) = isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
857 |
unfolding isrbt_def by (simp add: adjustwk_inv2 adjustwk_treec adjustwk_st adjustwk_inv1 ) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
858 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
859 |
theorem adjustwithkey_map[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
860 |
"map_of (adjustwithkey f k t) x = |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
861 |
(if x = k then case map_of t x of None \<Rightarrow> None | Some y \<Rightarrow> Some (f k y) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
862 |
else map_of t x)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
863 |
by (induct t arbitrary: x) (auto split:option.splits) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
864 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
865 |
definition adjust where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
866 |
"adjust f = adjustwithkey (\<lambda>_. f)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
867 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
868 |
theorem adjust_isrbt[simp]: "isrbt (adjust f k t) = isrbt t" unfolding adjust_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
869 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
870 |
theorem adjust_map[simp]: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
871 |
"map_of (adjust f k t) x = |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
872 |
(if x = k then case map_of t x of None \<Rightarrow> None | Some y \<Rightarrow> Some (f y) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
873 |
else map_of t x)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
874 |
unfolding adjust_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
875 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
876 |
subsection {* Map *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
877 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
878 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
879 |
mapwithkey :: "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a,'b) rbt \<Rightarrow> ('a,'c) rbt" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
880 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
881 |
"mapwithkey f Empty = Empty" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
882 |
| "mapwithkey f (Tr c lt k v rt) = Tr c (mapwithkey f lt) k (f k v) (mapwithkey f rt)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
883 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
884 |
theorem mapwk_keys[simp]: "keys (mapwithkey f t) = keys t" by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
885 |
lemma mapwk_tgt: "tgt k (mapwithkey f t) = tgt k t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
886 |
lemma mapwk_tlt: "tlt k (mapwithkey f t) = tlt k t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
887 |
lemma mapwk_st: "st (mapwithkey f t) = st t" by (induct t) (simp add: mapwk_tlt mapwk_tgt)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
888 |
lemma mapwk_treec: "treec (mapwithkey f t) = treec t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
889 |
lemma mapwk_inv1: "inv1 (mapwithkey f t) = inv1 t" by (induct t) (simp add: mapwk_treec)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
890 |
lemma mapwk_inv2: "inv2 (mapwithkey f t) = inv2 t" "bh (mapwithkey f t) = bh t" by (induct t) simp+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
891 |
theorem mapwk_isrbt[simp]: "isrbt (mapwithkey f t) = isrbt t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
892 |
unfolding isrbt_def by (simp add: mapwk_inv1 mapwk_inv2 mapwk_st mapwk_treec) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
893 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
894 |
theorem map_of_mapwk[simp]: "map_of (mapwithkey f t) x = option_map (f x) (map_of t x)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
895 |
by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
896 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
897 |
definition map |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
898 |
where map_def: "map f == mapwithkey (\<lambda>_. f)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
899 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
900 |
theorem map_keys[simp]: "keys (map f t) = keys t" unfolding map_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
901 |
theorem map_isrbt[simp]: "isrbt (map f t) = isrbt t" unfolding map_def by simp |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
902 |
theorem map_of_map[simp]: "map_of (map f t) = option_map f o map_of t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
903 |
by (rule ext) (simp add:map_def) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
904 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
905 |
subsection {* Fold *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
906 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
907 |
text {* The following is still incomplete... *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
908 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
909 |
primrec |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
910 |
foldwithkey :: "('a::linorder \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'c) \<Rightarrow> ('a,'b) rbt \<Rightarrow> 'c \<Rightarrow> 'c" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
911 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
912 |
"foldwithkey f Empty v = v" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
913 |
| "foldwithkey f (Tr c lt k x rt) v = foldwithkey f rt (f k x (foldwithkey f lt v))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
914 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
915 |
primrec alist_of |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
916 |
where |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
917 |
"alist_of Empty = []" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
918 |
| "alist_of (Tr _ l k v r) = alist_of l @ (k,v) # alist_of r" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
919 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
920 |
lemma map_of_alist_of: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
921 |
shows "st t \<Longrightarrow> Map.map_of (alist_of t) = map_of t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
922 |
oops |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
923 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
924 |
lemma fold_alist_fold: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
925 |
"foldwithkey f t x = foldl (\<lambda>x (k,v). f k v x) x (alist_of t)" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
926 |
by (induct t arbitrary: x) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
927 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
928 |
lemma alist_pit[simp]: "(k, v) \<in> set (alist_of t) = pin_tree k v t" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
929 |
by (induct t) auto |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
930 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
931 |
lemma sorted_alist: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
932 |
"st t \<Longrightarrow> sorted (List.map fst (alist_of t))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
933 |
by (induct t) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
934 |
(force simp: sorted_append sorted_Cons tlgt_props |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
935 |
dest!:pint_keys)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
936 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
937 |
lemma distinct_alist: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
938 |
"st t \<Longrightarrow> distinct (List.map fst (alist_of t))" |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
939 |
by (induct t) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
940 |
(force simp: sorted_append sorted_Cons tlgt_props |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
941 |
dest!:pint_keys)+ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
942 |
(*>*) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
943 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
944 |
text {* |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
945 |
This theory defines purely functional red-black trees which can be |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
946 |
used as an efficient representation of finite maps. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
947 |
*} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
948 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
949 |
subsection {* Data type and invariant *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
950 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
951 |
text {* |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
952 |
The type @{typ "('k, 'v) rbt"} denotes red-black trees with keys of |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
953 |
type @{typ "'k"} and values of type @{typ "'v"}. To function |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
954 |
properly, the key type must belong to the @{text "linorder"} class. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
955 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
956 |
A value @{term t} of this type is a valid red-black tree if it |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
957 |
satisfies the invariant @{text "isrbt t"}. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
958 |
This theory provides lemmas to prove that the invariant is |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
959 |
satisfied throughout the computation. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
960 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
961 |
The interpretation function @{const "map_of"} returns the partial |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
962 |
map represented by a red-black tree: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
963 |
@{term_type[display] "map_of"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
964 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
965 |
This function should be used for reasoning about the semantics of the RBT |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
966 |
operations. Furthermore, it implements the lookup functionality for |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
967 |
the data structure: It is executable and the lookup is performed in |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
968 |
$O(\log n)$. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
969 |
*} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
970 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
971 |
subsection {* Operations *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
972 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
973 |
text {* |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
974 |
Currently, the following operations are supported: |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
975 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
976 |
@{term_type[display] "Empty"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
977 |
Returns the empty tree. $O(1)$ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
978 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
979 |
@{term_type[display] "insrt"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
980 |
Updates the map at a given position. $O(\log n)$ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
981 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
982 |
@{term_type[display] "delete"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
983 |
Deletes a map entry at a given position. $O(\log n)$ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
984 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
985 |
@{term_type[display] "union"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
986 |
Forms the union of two trees, preferring entries from the first one. |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
987 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
988 |
@{term_type[display] "map"} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
989 |
Maps a function over the values of a map. $O(n)$ |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
990 |
*} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
991 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
992 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
993 |
subsection {* Invariant preservation *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
994 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
995 |
text {* |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
996 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
997 |
@{thm Empty_isrbt}\hfill(@{text "Empty_isrbt"}) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
998 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
999 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1000 |
@{thm insrt_isrbt}\hfill(@{text "insrt_isrbt"}) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1001 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1002 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1003 |
@{thm delete_isrbt}\hfill(@{text "delete_isrbt"}) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1004 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1005 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1006 |
@{thm union_isrbt}\hfill(@{text "union_isrbt"}) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1007 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1008 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1009 |
@{thm map_isrbt}\hfill(@{text "map_isrbt"}) |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1010 |
*} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1011 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1012 |
subsection {* Map Semantics *} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1013 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1014 |
text {* |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1015 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1016 |
\underline{@{text "map_of_Empty"}} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1017 |
@{thm[display] map_of_Empty} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1018 |
\vspace{1ex} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1019 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1020 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1021 |
\underline{@{text "map_of_insert"}} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1022 |
@{thm[display] map_of_insert} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1023 |
\vspace{1ex} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1024 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1025 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1026 |
\underline{@{text "map_of_delete"}} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1027 |
@{thm[display] map_of_delete} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1028 |
\vspace{1ex} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1029 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1030 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1031 |
\underline{@{text "map_of_union"}} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1032 |
@{thm[display] map_of_union} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1033 |
\vspace{1ex} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1034 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1035 |
\noindent |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1036 |
\underline{@{text "map_of_map"}} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1037 |
@{thm[display] map_of_map} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1038 |
\vspace{1ex} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1039 |
*} |
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1040 |
|
52617dca8386
new theory of red-black trees, an efficient implementation of finite maps.
krauss
parents:
diff
changeset
|
1041 |
end |