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 functions inductively. As a case-study we consider quick-sort.\ inductive qsortp :: "nat list \ nat list \ bool" where qsNil: "qsortp [] []" | qsCons: "qsortp (filter (\ y. y \ x) xs) low \ qsortp (filter (\ y. y > x) xs) high \ qsortp (x # 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!\ text \Task 1 (3 points)\ text \Prove that @{const qsortp} is deterministic.\ text \Hint: you will need one rule induction and two rule inversions.\ lemma qsortp_unique: "qsortp xs res1 \ qsortp xs res2 \ res1 = res2" sorry text \Remark: in the same way as for quick-sort, you can also prove that the evaluation of the while-programs in the lecture via \eval\ is deterministic.\ text \Task 2 (3 points)\ text \Prove that quick-sort delivers a result for every input\ text \Hint: @{thm length_induct} might be useful.\ lemma qsortp_exists: "\ res. qsortp xs res" sorry text \Task 3 (1 points)\ text \Now that we have uniqueness and existence of results, we define the quick-sort function as \THE\ unique result of the predicate version. Here, \THE\ is the so called definite-choice operator.\ definition qsort :: "nat list \ nat list" where "qsort xs = (THE res. qsortp xs res)" text \Convince yourself that @{const qsort} satisfies the simp-rules from a standard recursive definition of quick-sort, by finding one-line proofs of the following properties.\ (* \! is Isabelle's quantifier for "there exists exactly one element such that ..." *) lemma qsortp_exists1: "\! res. qsortp xs res" sorry lemma qsortp_qsort: "qsortp n (qsort n)" sorry lemma qsort_Nil: "qsort [] = []" sorry lemma qsort_Cons: "qsort (x # xs) = qsort (filter (\ y. y \ x) xs) @ x # qsort (filter (\ y. y > x) xs)" sorry 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 (induct 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