chapter \Exercise Sheet -- Week 9\ theory Exercise09 imports Main begin section \Exercise 1 -- 5 points\ text \As it was shown in the lecture, one can create a list type via pairs of a natural number (the length) and the index-function.\ definition is_list :: "(nat \ (nat \ 'a)) \ bool" where "is_list nf = (case nf of (n, f) \ \ i \ n. f i = undefined)" typedef 'a List = "{nf :: (nat \ (nat \ 'a)). is_list nf}" by (auto simp: is_list_def) text \Task 1: Define the upcoming four standard operations on lists on the representative type. (1 point)\ definition cons :: "'a \ nat \ (nat \ 'a) \ nat \ (nat \ 'a)" where "cons = undefined" definition tail :: "nat \ (nat \ 'a) \ nat \ (nat \ 'a)" where "tail = undefined" definition head :: "nat \ (nat \ 'a) \ 'a" where "head = undefined" definition nil :: "nat \ (nat \ 'a)" where "nil = undefined" text \Task 2: Use the lifting package to lift all four definitions from the representative type to @{typ "'a List"} (1 point)\ setup_lifting type_definition_List lift_definition NIL :: "'a List" is undefined oops lift_definition CONS :: "'a \ 'a List \ 'a List" is undefined oops lift_definition TAIL :: "'a List \ 'a List" is undefined oops lift_definition HEAD :: "'a List \ 'a" is undefined oops text \Task 3: Prove the upcoming three lemmas with the help of the transfer package. These show that the list operations work as expected. (1 point)\ lemma "HEAD (CONS x xs) = x" oops lemma "TAIL (CONS x xs) = xs" oops lemma "CONS x xs = CONS y ys \ x = y \ xs = ys" oops text \Task 4: Prove one of the following properties, i.e., either case-analysis or induction. (2 points) Of course you can also do both; the induction property is a bit more challenging.\ lemma "xs = NIL \ (\ y ys. xs = CONS y ys)" oops lemma "P NIL \ (\ x xs. P xs \ P (CONS x xs)) \ P ys" oops section \Exercise 2 -- 5 points\ text \In this exercise the aim is to define a type of distinct and ordered lists (which is enforced by @{term "sorted_wrt (<)"}), and these sets might then be used to implement operations on sets.\ text \Task 1: Complete the following definitions which should implement basic set operations via ordered lists. Here, in particular both the union-operation as well as the membership-test \elem_ol\ should exploit the order. (3 points)\ typedef (overloaded) ('a :: linorder) olist = "{ xs :: 'a list. sorted_wrt (<) xs}" sorry lift_definition empty_ol :: "'a :: linorder olist" is undefined sorry lift_definition singleton_ol :: "'a :: linorder \ 'a olist" is undefined sorry lift_definition set_ol :: "'a :: linorder olist \ 'a set" is undefined sorry lift_definition union_ol :: "'a :: linorder olist \ 'a olist \ 'a olist" is undefined sorry lift_definition elem_ol :: "'a :: linorder \ 'a olist \ bool" is undefined sorry text \Task 2: Prove the correctness of your implementation (2 points).\ lemma empty_ol: "set_ol empty_ol = {}" sorry lemma singleton_ol: "set_ol (singleton_ol x) = {x}" sorry lemma union_ol: "set_ol (union_ol xs1 xs2) = set_ol xs1 \ set_ol xs2" sorry lemma elem_ol: "elem_ol x xs = (x \ set_ol xs)" sorry end