nipkow@61224: (* Author: Tobias Nipkow *) nipkow@61224: nipkow@61224: section \Red-Black Tree Implementation of Sets\ nipkow@61224: nipkow@61224: theory RBT_Set nipkow@61224: imports nipkow@61224: RBT nipkow@61581: Cmp nipkow@61224: Isin2 nipkow@61224: begin nipkow@61224: nipkow@61581: fun insert :: "'a::cmp \ 'a rbt \ 'a rbt" where nipkow@61224: "insert x Leaf = R Leaf x Leaf" | nipkow@61581: "insert x (B l a r) = (case cmp x a of nipkow@61581: LT \ bal (insert x l) a r | nipkow@61581: GT \ bal l a (insert x r) | nipkow@61581: EQ \ B l a r)" | nipkow@61581: "insert x (R l a r) = (case cmp x a of nipkow@61581: LT \ R (insert x l) a r | nipkow@61581: GT \ R l a (insert x r) | nipkow@61581: EQ \ R l a r)" nipkow@61224: nipkow@61581: fun delete :: "'a::cmp \ 'a rbt \ 'a rbt" nipkow@61581: and deleteL :: "'a::cmp \ 'a rbt \ 'a \ 'a rbt \ 'a rbt" nipkow@61581: and deleteR :: "'a::cmp \ 'a rbt \ 'a \ 'a rbt \ 'a rbt" nipkow@61224: where nipkow@61224: "delete x Leaf = Leaf" | nipkow@61581: "delete x (Node _ l a r) = (case cmp x a of nipkow@61581: LT \ deleteL x l a r | nipkow@61581: GT \ deleteR x l a r | nipkow@61581: EQ \ combine l r)" | nipkow@61224: "deleteL x (B t1 a t2) b t3 = balL (delete x (B t1 a t2)) b t3" | nipkow@61224: "deleteL x l a r = R (delete x l) a r" | nipkow@61224: "deleteR x t1 a (B t2 b t3) = balR t1 a (delete x (B t2 b t3))" | nipkow@61224: "deleteR x l a r = R l a (delete x r)" nipkow@61224: nipkow@61224: nipkow@61224: subsection "Functional Correctness Proofs" nipkow@61224: nipkow@61224: lemma inorder_bal: nipkow@61224: "inorder(bal l a r) = inorder l @ a # inorder r" nipkow@61231: by(induction l a r rule: bal.induct) (auto) nipkow@61224: nipkow@61224: lemma inorder_insert: nipkow@61224: "sorted(inorder t) \ inorder(insert a t) = ins_list a (inorder t)" nipkow@61231: by(induction a t rule: insert.induct) (auto simp: ins_list_simps inorder_bal) nipkow@61224: nipkow@61224: lemma inorder_red: "inorder(red t) = inorder t" nipkow@61231: by(induction t) (auto) nipkow@61224: nipkow@61224: lemma inorder_balL: nipkow@61224: "inorder(balL l a r) = inorder l @ a # inorder r" nipkow@61231: by(induction l a r rule: balL.induct)(auto simp: inorder_bal inorder_red) nipkow@61224: nipkow@61224: lemma inorder_balR: nipkow@61224: "inorder(balR l a r) = inorder l @ a # inorder r" nipkow@61231: by(induction l a r rule: balR.induct) (auto simp: inorder_bal inorder_red) nipkow@61224: nipkow@61224: lemma inorder_combine: nipkow@61224: "inorder(combine l r) = inorder l @ inorder r" nipkow@61224: by(induction l r rule: combine.induct) nipkow@61231: (auto simp: inorder_balL inorder_balR split: tree.split color.split) nipkow@61224: nipkow@61224: lemma inorder_delete: nipkow@61224: "sorted(inorder t) \ inorder(delete x t) = del_list x (inorder t)" and nipkow@61224: "sorted(inorder l) \ inorder(deleteL x l a r) = nipkow@61224: del_list x (inorder l) @ a # inorder r" and nipkow@61224: "sorted(inorder r) \ inorder(deleteR x l a r) = nipkow@61224: inorder l @ a # del_list x (inorder r)" nipkow@61224: by(induction x t and x l a r and x l a r rule: delete_deleteL_deleteR.induct) nipkow@61231: (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR) nipkow@61224: nipkow@61581: nipkow@61224: interpretation Set_by_Ordered nipkow@61224: where empty = Leaf and isin = isin and insert = insert and delete = delete nipkow@61588: and inorder = inorder and inv = "\_. True" nipkow@61224: proof (standard, goal_cases) nipkow@61224: case 1 show ?case by simp nipkow@61224: next nipkow@61224: case 2 thus ?case by(simp add: isin_set) nipkow@61224: next nipkow@61224: case 3 thus ?case by(simp add: inorder_insert) nipkow@61224: next nipkow@61224: case 4 thus ?case by(simp add: inorder_delete) nipkow@61428: qed (rule TrueI)+ nipkow@61224: nipkow@61224: end