


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Exercise 1: Bookkeeping. Indicate in a sentence or two how much time you spent on this homework, how difficult you found it subjectively, and what you found to be the hardest part. Any non-empty answer will receive full credit.
I’ve just sucked one year of your life away. I might one day go as high as five, but I really don’t know what that would do to you. So, let’s just start with what we have. What did this do to you? Tell me. And remember, this is for posterity, so be honest — how do you feel? – Count Rugen
Exercise 2: Mathematical Induction. Find the flaw in the following inductive proof that “All flowers smell the same”. Please indicate exactly which sentences are wrong in the proof. Bringing me a counterexample does not constitute an acceptable solution.
Proof: Let F be the set of all flowers and let smells(f ) be the smell of the flower f ∈ F. (The range of smells is not so important, but we’ll assume that it admits equality.) We’ll also assume that F is countable. Let the property P (n) mean that all subsets of F of size at most n contain flowers that smell the same.
P (n) def = ∀X ∈ P(F ). |X|≤n ⇒ (∀f, f ′^ ∈ X. smells(f ) = smells(f ′))
(the notation |X| denotes the number of elements of X) One way to formulate the statement to prove is ∀n ≥ 1 .P (n). We’ll prove this by induction on n, as follows: Base: n = 1. Obviously all singleton sets of flowers contain flowers that smell the same (by the definition of P (n)). Induction step:. Let n be arbitrary and assume that all subsets of F of size at most n contain flowers that smell the same. We will prove that the same thing holds for all subsets of size at most n + 1. Pick an arbitrary set X such that |X| = n + 1. Pick two distinct flowers f, f ′^ ∈ X and let’s show that
smells(f ) = smells(f ′). Let Y = X − { f } and Y ′^ = X − { f ′^ }. Obviously Y and Y ′^ are sets of size at most n so the induction hypothesis holds for both of them. Pick any arbitrary x ∈ Y ∩ Y ′. Obviously, x 6 = f and x 6 = f ′. We have that smells(f ′) = smells(x) (from the induction hypothesis on Y ) and smells(f ) = smells(x) (from the induction hypothesis on Y ′). Hence smells(f ) = smells(f ′), which proves the inductive step, and the theorem. (One indication that the proof might be wrong is the large number of occurrences of the word “obviously” :-))
Exercise 3: Induction. Prove by induction the following statement about the operational semantics: For any BExp b and any initial state σ such that σ(x) is even, if
〈while b do x := x + 2, σ〉 ⇓ σ′
then σ′(x) is even. Make sure you state what you induct on, what the base case is and what the inductive cases are. Show representative cases among the latter. Do not do a proof by mathematical induction!
Exercise 4: Language Features. We extend IMP with a notion of integer-valued exceptions (or run-time errors), as in Java, ML or C#. We introduce a new type T to represent command terminations, which can either be normal or exceptional (with an exception value n ∈ Z):
T ::= σ “normal termination” | σ exc n “exceptional termination”
We use t to range over possible terminations T. We then redefine our oper- ational semantics judgment: 〈c, σ〉 ⇓ T
The interpretation of 〈c, σ〉 ⇓ σ′^ exc n
is that command c terminated abruptly by throwing an exception with value n ∈ Z at a point in c’s execution when the state was σ′. We only model one type of exception, but every exception has an integer “argument” n (or “payload” or “value”) that is set when the exception is thrown and available when the exception is caught. Note that our previous command rules must be updated to account for exceptions, as in:
〈c 1 , σ〉 ⇓ σ′^ exc n 〈c 1 ; c 2 , σ〉 ⇓ σ′^ exc n
seq
〈c 1 , σ〉 ⇓ σ′^ 〈c 2 , σ′〉 ⇓ t 〈c 1 ; c 2 , σ〉 ⇓ t
seq