chapter \Exercise Sheet -- Week 8\ theory Exercise08 imports Main begin section \Exercise 1 -- 6 points\ text \Reconsider the binary search tree from the lecture\ datatype 'a tree = Leaf | Node "'a tree" 'a "'a tree" fun set_t :: "'a tree \ 'a set" where "set_t Leaf = {}" | "set_t (Node l x r) = set_t l \ {x} \ set_t r" inductive ordered :: "'a :: linorder tree \ bool" where oLeaf: "ordered Leaf" | oNode: "ordered l \ ordered r \ Ball (set_t l) (\ y. y < x) \ Ball (set_t r) (\ y. y > x) \ ordered (Node l x r)" text \Task 1 -- Flatten, 2 points\ text \Define a function to flatten a tree into a list and prove some properties on this conversion. Hint: all of the proofs are one-liners\ fun flatten :: "'a tree \ 'a list" where "flatten t = undefined" lemma flatten_set: "set (flatten t) = set_t t" oops lemma flatten_distinct: "ordered t \ distinct (flatten t)" oops lemma flatten_sorted: "ordered t \ sorted (flatten t)" oops text \Task 2 -- Delete, 4 points\ text \Complete the following formalization that has been developed in the lecture. Adjust proofs and specifications as required.\ fun delete_right :: "'a :: linorder tree \ 'a tree \ 'a" where "delete_right (Node l x Leaf) = (l, x)" | "delete_right (Node l x r) = (case delete_right r of (r', y) \ (Node l x r', y))" fun delete :: "'a :: linorder \ 'a tree \ 'a tree" where "delete x Leaf = Leaf" | "delete x (Node l y r) = (if x < y then Node (delete x l) y r else if x > y then Node l y (delete x r) else if l = Leaf then r else case delete_right l of (l',z) \ Node l' z r)" lemma delete_right: assumes "ordered t" "t \ Leaf" "delete_right t = (t',x)" shows "set_t t' = set_t t - {x} \ ordered t' \ Ball (set_t t') (\ x. Ball f (A x))" oops lemma delete: assumes "ordered t" shows "ordered (delete y t) \ set_t (delete y t) = set_t t - {y}" using assms proof (induction) case (oNode l r x) consider (L) "y < x" | (R) "y > x" | (Le) "x = y" "l = Leaf" | (DR) "x = y" "l \ Leaf" by fastforce then show ?case proof cases case L then show ?thesis using oNode.IH(1) oNode.hyps by (auto intro!: ordered.intros) next case R then show ?thesis using oNode.IH(2) oNode.hyps by (auto intro!: ordered.intros) next case Le then show ?thesis using oNode.hyps by (auto intro!: ordered.intros) next case DR obtain l' z where dr: "delete_right l = (l',z)" by force from dr DR show ?thesis apply simp sorry qed qed (auto intro: ordered.intros) section \Exercise 2 - 4 points\ text \Prove the summation formula of Gauss without induction, i.e., just be manipulating sets. Follow the draft.\ lemma Gauss: "2 * sum (\ i. i) {..< (n :: nat)} = (n - 1) * n" proof - have "2 * sum (\ i. i) {..< n} = sum (\ i. i) {..< n} + sum (\ i. i) {..< n}" by simp (* now Gauss' trick is applied to let one of the sums count downwards *) also have "\ = sum (\ i. i) {..< n} + sum (\ i. n - 1 - i) {..< n}" apply (rule arg_cong[of _ _ "\ s. _ + s"]) apply (rule sum.reindex_cong) proof - text \Task 1 -- 1 point: Choose undefined appropriately and remove the first two sorries\ show "inj_on undefined {.. {..Task 2 -- 2 points: remove the next sorry. Hint: one of the subset-directions is trivial; do the other direction slowly and perhaps @{thm image_eqI} is useful.\ show "{..Task 3 -- 1 point: remove the remaining two sorries where you should use @{command find_theorems}, but not @{command sledgehammer} or @{command try}.\ also have "\ = sum (\ i. i + (n - 1 - i)) {..< n}" sorry also have "\ = sum (\ i. n - 1) {..< n}" sorry also have "\ = (n - 1) * n" by simp finally show ?thesis . qed end