105 fun invc :: "'a rbt \<Rightarrow> bool" where |
105 fun invc :: "'a rbt \<Rightarrow> bool" where |
106 "invc Leaf = True" | |
106 "invc Leaf = True" | |
107 "invc (Node l a c r) = |
107 "invc (Node l a c r) = |
108 (invc l \<and> invc r \<and> (c = Red \<longrightarrow> color l = Black \<and> color r = Black))" |
108 (invc l \<and> invc r \<and> (c = Red \<longrightarrow> color l = Black \<and> color r = Black))" |
109 |
109 |
110 fun invc2 :: "'a rbt \<Rightarrow> bool" \<comment> \<open>Weaker version\<close> where |
110 text \<open>Weaker version:\<close> |
111 "invc2 Leaf = True" | |
111 abbreviation invc2 :: "'a rbt \<Rightarrow> bool" where |
112 "invc2 (Node l a c r) = (invc l \<and> invc r)" |
112 "invc2 t \<equiv> invc(paint Black t)" |
113 |
113 |
114 fun invh :: "'a rbt \<Rightarrow> bool" where |
114 fun invh :: "'a rbt \<Rightarrow> bool" where |
115 "invh Leaf = True" | |
115 "invh Leaf = True" | |
116 "invh (Node l x c r) = (invh l \<and> invh r \<and> bheight l = bheight r)" |
116 "invh (Node l x c r) = (invh l \<and> invh r \<and> bheight l = bheight r)" |
117 |
117 |
122 "rbt t = (invc t \<and> invh t \<and> color t = Black)" |
122 "rbt t = (invc t \<and> invh t \<and> color t = Black)" |
123 |
123 |
124 lemma color_paint_Black: "color (paint Black t) = Black" |
124 lemma color_paint_Black: "color (paint Black t) = Black" |
125 by (cases t) auto |
125 by (cases t) auto |
126 |
126 |
127 lemma paint_invc2: "invc2 t \<Longrightarrow> invc2 (paint c t)" |
127 lemma paint2: "paint c2 (paint c1 t) = paint c2 t" |
128 by (cases t) auto |
|
129 |
|
130 lemma invc_paint_Black: "invc2 t \<Longrightarrow> invc (paint Black t)" |
|
131 by (cases t) auto |
128 by (cases t) auto |
132 |
129 |
133 lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)" |
130 lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)" |
134 by (cases t) auto |
131 by (cases t) auto |
135 |
132 |
155 |
152 |
156 lemma invh_baliR: |
153 lemma invh_baliR: |
157 "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (baliR l a r)" |
154 "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (baliR l a r)" |
158 by (induct l a r rule: baliR.induct) auto |
155 by (induct l a r rule: baliR.induct) auto |
159 |
156 |
|
157 text \<open>All in one:\<close> |
|
158 |
|
159 lemma inv_baliR: "\<lbrakk> invh l; invh r; invc l; invc2 r; bheight l = bheight r \<rbrakk> |
|
160 \<Longrightarrow> invc (baliR l a r) \<and> invh (baliR l a r) \<and> bheight (baliR l a r) = Suc (bheight l)" |
|
161 by (induct l a r rule: baliR.induct) auto |
|
162 |
|
163 lemma inv_baliL: "\<lbrakk> invh l; invh r; invc2 l; invc r; bheight l = bheight r \<rbrakk> |
|
164 \<Longrightarrow> invc (baliL l a r) \<and> invh (baliL l a r) \<and> bheight (baliL l a r) = Suc (bheight l)" |
|
165 by (induct l a r rule: baliL.induct) auto |
160 |
166 |
161 subsubsection \<open>Insertion\<close> |
167 subsubsection \<open>Insertion\<close> |
162 |
168 |
163 lemma invc_ins: assumes "invc t" |
169 lemma invc_ins: "invc t \<longrightarrow> invc2 (ins x t) \<and> (color t = Black \<longrightarrow> invc (ins x t))" |
164 shows "color t = Black \<Longrightarrow> invc (ins x t)" "invc2 (ins x t)" |
|
165 using assms |
|
166 by (induct x t rule: ins.induct) (auto simp: invc_baliL invc_baliR invc2I) |
170 by (induct x t rule: ins.induct) (auto simp: invc_baliL invc_baliR invc2I) |
167 |
171 |
168 lemma invh_ins: assumes "invh t" |
172 lemma invh_ins: "invh t \<Longrightarrow> invh (ins x t) \<and> bheight (ins x t) = bheight t" |
169 shows "invh (ins x t)" "bheight (ins x t) = bheight t" |
|
170 using assms |
|
171 by(induct x t rule: ins.induct) |
173 by(induct x t rule: ins.induct) |
172 (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR) |
174 (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR) |
173 |
175 |
174 theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)" |
176 theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)" |
175 by (simp add: invc_ins(2) invh_ins(1) color_paint_Black invc_paint_Black invh_paint |
177 by (simp add: invc_ins invh_ins color_paint_Black invh_paint rbt_def insert_def) |
176 rbt_def insert_def) |
178 |
|
179 text \<open>All in one variant:\<close> |
|
180 |
|
181 lemma inv_ins: "\<lbrakk> invc t; invh t \<rbrakk> \<Longrightarrow> |
|
182 invc2 (ins x t) \<and> (color t = Black \<longrightarrow> invc (ins x t)) \<and> |
|
183 invh(ins x t) \<and> bheight (ins x t) = bheight t" |
|
184 by (induct x t rule: ins.induct) (auto simp: inv_baliL inv_baliR invc2I) |
|
185 |
|
186 theorem rbt_insert2: "rbt t \<Longrightarrow> rbt (insert x t)" |
|
187 by (simp add: inv_ins color_paint_Black invh_paint rbt_def insert_def) |
177 |
188 |
178 |
189 |
179 subsubsection \<open>Deletion\<close> |
190 subsubsection \<open>Deletion\<close> |
180 |
191 |
181 lemma bheight_paint_Red: |
192 lemma bheight_paint_Red: |
195 |
206 |
196 lemma invc_baldL: "\<lbrakk>invc2 l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (baldL l a r)" |
207 lemma invc_baldL: "\<lbrakk>invc2 l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (baldL l a r)" |
197 by (induct l a r rule: baldL.induct) (simp_all add: invc_baliR) |
208 by (induct l a r rule: baldL.induct) (simp_all add: invc_baliR) |
198 |
209 |
199 lemma invc2_baldL: "\<lbrakk> invc2 l; invc r \<rbrakk> \<Longrightarrow> invc2 (baldL l a r)" |
210 lemma invc2_baldL: "\<lbrakk> invc2 l; invc r \<rbrakk> \<Longrightarrow> invc2 (baldL l a r)" |
200 by (induct l a r rule: baldL.induct) (auto simp: invc_baliR paint_invc2 invc2I) |
211 by (induct l a r rule: baldL.induct) (auto simp: invc_baliR paint2 invc2I) |
201 |
212 |
202 lemma invh_baldR_invc: |
213 lemma invh_baldR_invc: |
203 "\<lbrakk> invh l; invh r; bheight l = bheight r + 1; invc l \<rbrakk> |
214 "\<lbrakk> invh l; invh r; bheight l = bheight r + 1; invc l \<rbrakk> |
204 \<Longrightarrow> invh (baldR l a r) \<and> bheight (baldR l a r) = bheight l" |
215 \<Longrightarrow> invh (baldR l a r) \<and> bheight (baldR l a r) = bheight l" |
205 by(induct l a r rule: baldR.induct) |
216 by(induct l a r rule: baldR.induct) |
207 |
218 |
208 lemma invc_baldR: "\<lbrakk>invc l; invc2 r; color l = Black\<rbrakk> \<Longrightarrow> invc (baldR l a r)" |
219 lemma invc_baldR: "\<lbrakk>invc l; invc2 r; color l = Black\<rbrakk> \<Longrightarrow> invc (baldR l a r)" |
209 by (induct l a r rule: baldR.induct) (simp_all add: invc_baliL) |
220 by (induct l a r rule: baldR.induct) (simp_all add: invc_baliL) |
210 |
221 |
211 lemma invc2_baldR: "\<lbrakk> invc l; invc2 r \<rbrakk> \<Longrightarrow>invc2 (baldR l a r)" |
222 lemma invc2_baldR: "\<lbrakk> invc l; invc2 r \<rbrakk> \<Longrightarrow>invc2 (baldR l a r)" |
212 by (induct l a r rule: baldR.induct) (auto simp: invc_baliL paint_invc2 invc2I) |
223 by (induct l a r rule: baldR.induct) (auto simp: invc_baliL paint2 invc2I) |
213 |
224 |
214 lemma invh_combine: |
225 lemma invh_combine: |
215 "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> |
226 "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> |
216 \<Longrightarrow> invh (combine l r) \<and> bheight (combine l r) = bheight l" |
227 \<Longrightarrow> invh (combine l r) \<and> bheight (combine l r) = bheight l" |
217 by (induct l r rule: combine.induct) |
228 by (induct l r rule: combine.induct) |
218 (auto simp: invh_baldL_Black split: tree.splits color.splits) |
229 (auto simp: invh_baldL_Black split: tree.splits color.splits) |
219 |
230 |
220 lemma invc_combine: |
231 lemma invc_combine: |
221 assumes "invc l" "invc r" |
232 "\<lbrakk> invc l; invc r \<rbrakk> \<Longrightarrow> |
222 shows "color l = Black \<Longrightarrow> color r = Black \<Longrightarrow> invc (combine l r)" |
233 (color l = Black \<and> color r = Black \<longrightarrow> invc (combine l r)) \<and> invc2 (combine l r)" |
223 "invc2 (combine l r)" |
|
224 using assms |
|
225 by (induct l r rule: combine.induct) |
234 by (induct l r rule: combine.induct) |
226 (auto simp: invc_baldL invc2I split: tree.splits color.splits) |
235 (auto simp: invc_baldL invc2I split: tree.splits color.splits) |
227 |
236 |
228 lemma neq_LeafD: "t \<noteq> Leaf \<Longrightarrow> \<exists>c l x r. t = Node c l x r" |
237 lemma neq_LeafD: "t \<noteq> Leaf \<Longrightarrow> \<exists>c l x r. t = Node c l x r" |
229 by(cases t) auto |
238 by(cases t) auto |
250 (auto simp: invh_baldR_invc invc_baldR invc2_baldR dest: neq_LeafD) |
259 (auto simp: invh_baldR_invc invc_baldR invc2_baldR dest: neq_LeafD) |
251 qed |
260 qed |
252 qed auto |
261 qed auto |
253 |
262 |
254 theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete x t)" |
263 theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete x t)" |
255 by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc2I invh_paint) |
264 by (metis delete_def rbt_def color_paint_Black del_invc_invh invc2I invh_paint) |
256 |
265 |
257 text \<open>Overall correctness:\<close> |
266 text \<open>Overall correctness:\<close> |
258 |
267 |
259 interpretation S: Set_by_Ordered |
268 interpretation S: Set_by_Ordered |
260 where empty = empty and isin = isin and insert = insert and delete = delete |
269 where empty = empty and isin = isin and insert = insert and delete = delete |