# HG changeset patch # User nipkow # Date 1568655098 -7200 # Node ID a95446297373b100888789302590d5b0941481d4 # Parent 125705f5965f12f76c120e4787c0cf834ca8c1c1# Parent 3e11f35496b3fdff432f3dabcc4e1ef82582a8d1 merged diff -r 125705f5965f -r a95446297373 src/HOL/Data_Structures/RBT_Map.thy --- a/src/HOL/Data_Structures/RBT_Map.thy Mon Sep 16 17:03:13 2019 +0100 +++ b/src/HOL/Data_Structures/RBT_Map.thy Mon Sep 16 19:31:38 2019 +0200 @@ -72,8 +72,7 @@ (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR) theorem rbt_update: "rbt t \ rbt (update x y t)" -by (simp add: invc_upd(2) invh_upd(1) color_paint_Black invc_paint_Black invh_paint - rbt_def update_def) +by (simp add: invc_upd(2) invh_upd(1) color_paint_Black invh_paint rbt_def update_def) subsubsection \Deletion\ @@ -102,7 +101,7 @@ qed auto theorem rbt_delete: "rbt t \ rbt (delete k t)" -by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc2I invh_paint) +by (metis delete_def rbt_def color_paint_Black del_invc_invh invc2I invh_paint) interpretation M: Map_by_Ordered where empty = empty and lookup = lookup and update = update and delete = delete diff -r 125705f5965f -r a95446297373 src/HOL/Data_Structures/RBT_Set.thy --- a/src/HOL/Data_Structures/RBT_Set.thy Mon Sep 16 17:03:13 2019 +0100 +++ b/src/HOL/Data_Structures/RBT_Set.thy Mon Sep 16 19:31:38 2019 +0200 @@ -107,9 +107,9 @@ "invc (Node l a c r) = (invc l \ invc r \ (c = Red \ color l = Black \ color r = Black))" -fun invc2 :: "'a rbt \ bool" \ \Weaker version\ where -"invc2 Leaf = True" | -"invc2 (Node l a c r) = (invc l \ invc r)" +text \Weaker version:\ +abbreviation invc2 :: "'a rbt \ bool" where +"invc2 t \ invc(paint Black t)" fun invh :: "'a rbt \ bool" where "invh Leaf = True" | @@ -124,10 +124,7 @@ lemma color_paint_Black: "color (paint Black t) = Black" by (cases t) auto -lemma paint_invc2: "invc2 t \ invc2 (paint c t)" -by (cases t) auto - -lemma invc_paint_Black: "invc2 t \ invc (paint Black t)" +lemma paint2: "paint c2 (paint c1 t) = paint c2 t" by (cases t) auto lemma invh_paint: "invh t \ invh (paint c t)" @@ -157,23 +154,37 @@ "\ invh l; invh r; bheight l = bheight r \ \ invh (baliR l a r)" by (induct l a r rule: baliR.induct) auto +text \All in one:\ + +lemma inv_baliR: "\ invh l; invh r; invc l; invc2 r; bheight l = bheight r \ + \ invc (baliR l a r) \ invh (baliR l a r) \ bheight (baliR l a r) = Suc (bheight l)" +by (induct l a r rule: baliR.induct) auto + +lemma inv_baliL: "\ invh l; invh r; invc2 l; invc r; bheight l = bheight r \ + \ invc (baliL l a r) \ invh (baliL l a r) \ bheight (baliL l a r) = Suc (bheight l)" +by (induct l a r rule: baliL.induct) auto subsubsection \Insertion\ -lemma invc_ins: assumes "invc t" - shows "color t = Black \ invc (ins x t)" "invc2 (ins x t)" -using assms +lemma invc_ins: "invc t \ invc2 (ins x t) \ (color t = Black \ invc (ins x t))" by (induct x t rule: ins.induct) (auto simp: invc_baliL invc_baliR invc2I) -lemma invh_ins: assumes "invh t" - shows "invh (ins x t)" "bheight (ins x t) = bheight t" -using assms +lemma invh_ins: "invh t \ invh (ins x t) \ bheight (ins x t) = bheight t" by(induct x t rule: ins.induct) (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR) theorem rbt_insert: "rbt t \ rbt (insert x t)" -by (simp add: invc_ins(2) invh_ins(1) color_paint_Black invc_paint_Black invh_paint - rbt_def insert_def) +by (simp add: invc_ins invh_ins color_paint_Black invh_paint rbt_def insert_def) + +text \All in one variant:\ + +lemma inv_ins: "\ invc t; invh t \ \ + invc2 (ins x t) \ (color t = Black \ invc (ins x t)) \ + invh(ins x t) \ bheight (ins x t) = bheight t" +by (induct x t rule: ins.induct) (auto simp: inv_baliL inv_baliR invc2I) + +theorem rbt_insert2: "rbt t \ rbt (insert x t)" +by (simp add: inv_ins color_paint_Black invh_paint rbt_def insert_def) subsubsection \Deletion\ @@ -197,7 +208,7 @@ by (induct l a r rule: baldL.induct) (simp_all add: invc_baliR) lemma invc2_baldL: "\ invc2 l; invc r \ \ invc2 (baldL l a r)" -by (induct l a r rule: baldL.induct) (auto simp: invc_baliR paint_invc2 invc2I) +by (induct l a r rule: baldL.induct) (auto simp: invc_baliR paint2 invc2I) lemma invh_baldR_invc: "\ invh l; invh r; bheight l = bheight r + 1; invc l \ @@ -209,7 +220,7 @@ by (induct l a r rule: baldR.induct) (simp_all add: invc_baliL) lemma invc2_baldR: "\ invc l; invc2 r \ \invc2 (baldR l a r)" -by (induct l a r rule: baldR.induct) (auto simp: invc_baliL paint_invc2 invc2I) +by (induct l a r rule: baldR.induct) (auto simp: invc_baliL paint2 invc2I) lemma invh_combine: "\ invh l; invh r; bheight l = bheight r \ @@ -218,10 +229,8 @@ (auto simp: invh_baldL_Black split: tree.splits color.splits) lemma invc_combine: - assumes "invc l" "invc r" - shows "color l = Black \ color r = Black \ invc (combine l r)" - "invc2 (combine l r)" -using assms + "\ invc l; invc r \ \ + (color l = Black \ color r = Black \ invc (combine l r)) \ invc2 (combine l r)" by (induct l r rule: combine.induct) (auto simp: invc_baldL invc2I split: tree.splits color.splits) @@ -252,7 +261,7 @@ qed auto theorem rbt_delete: "rbt t \ rbt (delete x t)" -by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc2I invh_paint) +by (metis delete_def rbt_def color_paint_Black del_invc_invh invc2I invh_paint) text \Overall correctness:\ diff -r 125705f5965f -r a95446297373 src/HOL/Data_Structures/Set2_Join_RBT.thy --- a/src/HOL/Data_Structures/Set2_Join_RBT.thy Mon Sep 16 17:03:13 2019 +0100 +++ b/src/HOL/Data_Structures/Set2_Join_RBT.thy Mon Sep 16 19:31:38 2019 +0200 @@ -101,7 +101,7 @@ (* unused *) lemma rbt_join: "\ invc l; invh l; invc r; invh r \ \ rbt(join l x r)" -by(simp add: invc2_joinL invc2_joinR invc_paint_Black invh_joinL invh_joinR invh_paint rbt_def +by(simp add: invc2_joinL invc2_joinR invh_joinL invh_joinR invh_paint rbt_def color_paint_Black join_def) text \To make sure the the black height is not increased unnecessarily:\ @@ -206,7 +206,7 @@ by(auto simp: bst_paint bst_joinL bst_joinR join_def) lemma inv_join: "\ invc l; invh l; invc r; invh r \ \ invc(join l x r) \ invh(join l x r)" -by (simp add: invc2_joinL invc2_joinR invc_paint_Black invh_joinL invh_joinR invh_paint join_def) +by (simp add: invc2_joinL invc2_joinR invh_joinL invh_joinR invh_paint join_def) subsubsection "Interpretation of \<^locale>\Set2_Join\ with Red-Black Tree"