Assignment #2: COS 441 Due by midnight on Wednesday Oct. 1. Either hand in in class or deliver to Rob's office. You mail also email a completed assignment to him at rdockins@princeton.edu. Please put "COS 441 A2" in the subject line of any such email. There are 15 points to gain on this assignment. Question 1: Remember our natural numbers and our lists: n ::= Z | S Z l ::= nil | cons (n,l) Here is a definition of the less-than-or-equal-to judgement for natural numbers: Judgement Form: |- leq n1 n2 Rules: ------------ (Z-LEQ) |- leq Z n2 |- leq n1 n2 --------------------- (S-LEQ) |- leq (S n1) (S n2) (a) [1 point] Use the leq judgement to define a new judgement with the form |- ascend l that is valid whenever the elements of l are in ascending order (duplicates are allowed). For example, these judgements are valid: |- ascend cons (Z, cons (Z, cons (S S S Z, cons (S S S S S S Z, nil)))) |- ascend nil |- ascend cons (S S Z, nil) This judgement is not valid: |- ascend cons (Z, cons (S S Z, cons ( S Z, nil))) (b) [2 points] Define a judgement with the form: |- reverse l1 l2 where the elements in l2 appear in the reverse order as in l1. eg: |- reverse (cons (Z, cons (S Z, nil))) (cons (S Z, cons (Z, nil))) (c) [4 points] Consider the judgement |- dup l1 l2 and its rules: ---------------(nil-dup) |- dup nil nil |- dup l1 l2 ----------------------------------------- (cons-dup) |- dup cons(n,l1) cons(n, cons(n, l2)) Prove this theorem: If |- ascend l1 and |- dup l1 l2 then |- ascend l2. (d) [1 point] Define a judgement |- incr l1 l2 where l2 is the same as l1 except you have added one to every element (ie, gone from n to (S n) for every element n of the list). For example, this judgement is valid: |- incr (cons (Z,cons (S S S Z, nil))) (cons (S Z, cons (S S S S Z, nil))) (e) [4 points] Prove this theorem: If |- ascend l1 and |- incr l1 l2 then |- ascend l2. Question 2: Here is the definition of a very simple logic with true, false, inequalities, conjunction and disjunction. The formulas F are the following: F ::= true | false | n1 <= n2 | F1 /\ F2 | F1 \/ F2 The truth of a logical formula is determined by a judgement with the form: |- F Here are the rules: -------- (T) |- true |- leq n1 n2 -------------- (LEQ) |- n1 <= n2 |- F1 |- F2 ----------------------- (/\) |- F1 /\ F2 |- F1 |- F2 ------------------ (\/1) ----------- (\/2) |- F1 \/ F2 |- F1 \/ F2 (a) [1 Point] Write down a complete derivation for the judgement: |- (false \/ ((Z <= S Z) \/ (S Z <= Z))) /\ true (b) [1 Point] Prove that: if |- (F1 /\ (F2 /\ F3)) is a valid derivation (for any F1, F2, F3) then |- ((F2 /\ F3) /\ F1)) is a valid derivation. Hint: Don't prove it by induction. Explain what the derivation of |- (F1 /\ (F2 /\ F3)) must look like and why. Then use that information to construct a derivation for |- ((F2 /\ F3) /\ F1)). (c) [1 Point] Prove that: if |- (F1 \/ (F2 \/ F3)) is a valid derivation (for any F1, F2, F3) then |- ((F2 \/ F3) \/ F1)) is a valid derivation. Hint: Again, don't use induction. Consider the possible forms that the derivation of |- (F1 \/ (F2 \/ F3)) could have and go from there.