chapter \Exercise Sheet -- Week 3\ text \For this exercise sheet you will have to figure out several auxiliary lemmas on your own!\ theory Exercise03 imports Main begin section \Exercise 1 -- 2 points\ text \Consider the append function from the lecture\ fun app :: "'a list \ 'a list \ 'a list" where "app [] ys = ys" | "app (x # xs) ys = x # (app xs ys)" datatype 'a tree = Leaf | Node "'a tree" 'a "'a tree" text \The function \count\ counts the number of nodes in a tree.\ fun count :: "'a tree \ nat" where "count Leaf = 0" | "count (Node l x r) = count l + 1 + count r" text \Flatten a tree into a list.\ fun flatten :: "'a tree \ 'a list" where "flatten Leaf = []" | "flatten (Node l x r) = app (flatten l) (x # flatten r)" text \Prove the following property:\ lemma len_flatten: "length (flatten t) = count t" sorry section \Exercise 2 -- 3 points\ text \Consider insertion sort\ fun insert_sorted :: "'a :: linorder \ 'a list \ 'a list" where "insert_sorted x Nil = Cons x Nil" | "insert_sorted x (Cons y ys) = (case x \ y of True \ Cons x (Cons y ys) | False \ Cons y (insert_sorted x ys))" fun ins_sort :: "'a :: linorder list \ 'a list" where "ins_sort Nil = Nil" | "ins_sort (Cons x xs) = insert_sorted x (ins_sort xs)" text \Prove the property that insertion sort preserves the length.\ lemma length_ins_sort: "length (ins_sort xs) = length xs" sorry section \Exercise 3 -- Natural numbers\ text \Isabelle contains natural numbers corresponding to the datatype definition: datatype nat = 0 | Suc nat Standard arithmetic operations are already predefined, e.g., +, *, <, ..., but in this exercise we provide our own arithmetic operations and prove properties about these. \ fun add :: "nat \ nat \ nat" where "add x 0 = x" | "add x (Suc y) = Suc (add x y)" fun mult :: "nat \ nat \ nat" where "mult x 0 = 0" | "mult x (Suc y) = add x (mult x y)" subsection \Exercise 3.1 -- 2 points\ text \Prove that addition is commutative.\ lemma add_comm: "add x y = add y x" sorry subsection \Exercise 3.2 -- 3 points\ text \Prove that multiplication is commutative\ text \Hint: you even can use commutativity of addition as simplification rule without introducing non-termination: Isabelle has special-treatment of commutativity laws which then normalize terms like @{term "add t1 t2"} to either @{term "add t1 t2"} or @{term "add t2 t1"}, depending on whether some internal order on terms makes @{term t1} larger or smaller than @{term t2}. However, the evaluation "add t1 t2 = add t2 t1 = add t1 t2 = ... " will not happen.\ text \Example: here y and x are swapped once.\ lemma "add y x = z" proof (simp add: add_comm) oops lemma mult_comm: "mult x y = mult y x" sorry end