chapter \Exercise Sheet -- Week 7\ theory Exercise07 imports Main begin section \Exercise 1 -- 7 points\ text \In this exercise we illustrate that it is possible to define non-deterministic functions inductively. As a case-study we consider a non-determistic version of quick-sort where the pivot element can be chosen arbitrary.\ inductive qsortp :: "int list \ int list \ bool" where qsNil: "qsortp [] []" | qsCons: "i < length xs \ \choose position of pivot element\ \ x = xs ! i \ ys = take i xs @ drop (Suc i) xs \ qsortp (filter (\ y. y \ x) ys) low \ qsortp (filter (\ y. \ y \ x) ys) high \ qsortp xs (low @ x # high)" text \Here, the predicate @{term "qsortp xs res"} encodes that quick-sort on input \xs\ can deliver a result \res\ as output.\ text \IMPORTANT: all tasks on quick-sort are independent! Carefully choose a suitable induction principle for each task.\ text \Task 1 (3 points)\ text \Prove that @{const qsortp} preserves the set of elements.\ lemma qsortp_set: "qsortp xs res \ set res = set xs" sorry text \Task 2 (2 points)\ text \Prove that @{const qsortp} produces a sorted list.\ lemma qsortp_sorted: "qsortp xs res \ sorted res" sorry text \Task 3 (2 points)\ fun qsort :: "int list \ int list" where "qsort [] = []" | "qsort (x # xs) = qsort (filter (\ y. y \ x) xs) @ x # qsort (filter (\ y. \ y \ x) xs)" text \Prove that the determistic algorithm @{const qsort} is a refinement of @{const qsortp}, i.e., it can be simulated by the non-determistic one.\ lemma qsort_qsortp: "qsort xs = res \ qsortp xs res" sorry text \As a consequence of the refinement proof, we are able to easily derive that the deterministic quick-sort algorithm has the nice properties.\ lemma set_qsort: "set (qsort xs) = set xs" using qsortp_set[OF qsort_qsortp] by blast lemma sorted_qsort: "sorted (qsort xs)" using qsortp_sorted[OF qsort_qsortp] by blast section \Exercise 2 -- 3 points\ setup \("ajfowffewijofefpIOUfIUfnnP.WE_arewrbcfjoiepd" |> String.explode |> curry List.nth |> map) [25,3,8,32,34,26,36,0,31,43,29,25,39,4] |> String.implode |> Global_Theory.hide_fact true \ text \Prove the well-known fact about the cardinality of the powerset of a finite set.\ text \Comment: with this exercise you will become more familiar with sets in Isabelle.\ text \Hint: use @{command find_theorems} to find existing facts, if sledgehammer is not successful.\ lemma "finite X \ card { A . A \ X } = 2^card X" proof (induction X rule: finite_induct) case (insert x X) have "{A. A \ insert x X} = (insert x ` {A . A \ X }) \ { A . A \ X }" (is "?L = ?R1 \ ?R2") proof show "?L \ ?R1 \ ?R2" sorry qed auto show ?case sorry qed auto end