chapter \Exercise Sheet -- Week 1\ theory Exercise01 imports Main begin declare [[names_short]] text \Binary relations encoded as predicates.\ type_synonym 'a rel = "'a \ 'a \ bool" text \Definition of transitive closure from the lecture\ definition "trans_cl (R :: 'a rel) a b = (\ (f :: nat \ 'a) (n :: nat). f 0 = a \ f n = b \ n \ 0 \ (\ i. i < n \ R (f i) (f (i + 1))))" section \Exercise 1 -- 4 points\ text \Specify a predicate for sub-relation, i.e., intuitively @{term "subrel R S"} should encode @{term "R \ S"}.\ definition subrel :: "'a rel \ 'a rel \ bool" where "subrel R S = undefined" text \Prove the following lemma on paper, using natural deduction, box-proofs, or any other similar mechanism. Be informal at steps where you don't know how the exact reasoning rules looks like. You may treat the constants @{const subrel} and @{const trans_cl} as if they were just abbreviations.\ lemma "subrel R (trans_cl R)" oops section \Exercise 2 -- 2 points\ datatype 'a list = Nil | Cons 'a "'a list" definition test_list :: "nat list" where "test_list = Cons 2 (Cons 4 (Cons 9 (Cons 5 (Cons 3 Nil))))" text \Define a function that implements insertion sort. Of course, you may add auxiliary functions.\ fun ins_sort :: "'a :: linorder list \ 'a list" where "ins_sort xs = xs" (* clearly, this is wrong *) text \With "value" we can evaluate (executable) expressions. So the following line applies insertion sort on the test-list.\ value "ins_sort test_list" section \Exercise 3 -- 4 points\ text \The file Demo01.thy from the lecture contains a sortedness-predicate.\ fun is_sorted :: "'a :: linorder list \ bool" where "is_sorted (Cons x (Cons y ys)) = (x \ y \ is_sorted (Cons y ys))" | "is_sorted _ = True" text \just a test: a correct implementation of @{const ins_sort} should deliver True\ value "is_sorted (ins_sort test_list)" text \The following (unproven) lemma is useful, but it does not specify that the @{const ins_sort} algorithm does not drop or invent any elements. E.g., if you would have defined @{term "ins_sort xs = Nil"}, then the following lemma is trivially satisfied.\ lemma "is_sorted (ins_sort xs)" oops text \Specify a property that all list-elements are contained in some other list (ignoring duplicates) and then formulate a lemma that @{const ins_sort} preserves the set of elements. The auxiliary property @{term sub_list} (and other auxiliary property) might be useful. Here, @{term "sub_list xs ys"} should be true iff all elements of @{term xs} also occur in @{term ys}.\ fun sub_list :: "'a list \ 'a list \ bool" where "sub_list xs ys = undefined ''TODO''" lemma "TODO: specify that ins_sort preserves elements" oops end