Docsity

CS615 Homework Assignment 2: Exercises in Logic and Programming with Exceptions - Prof. We, Assignments of Programming Languages

Toggle sidebar
bg1
CS615 — Homework Assignment 2
Wes Weimer
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 Fbe 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 Fis countable. Let the property
P(n) mean that all subsets of Fof size at most ncontain flowers that smell
the same.
P(n)def
=∀X∈ P(F).|X|≤n⇒(∀f , f0∈X. smells(f) = smells(f0))
(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 nbe arbitrary and assume that all subsets of Fof
size at most ncontain 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 0∈Xand let’s show that
1
pf3
pf4

Partial preview of the text

Download CS615 Homework Assignment 2: Exercises in Logic and Programming with Exceptions - Prof. We and more Assignments Programming Languages in PDF only on Docsity!

CS615 — Homework Assignment 2

Wes Weimer

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

  • Argue for or against the claim that it would be more natural to describe “IMP with exceptions” using small-step contextual semantics. You may use “simpler” or “more elegant” instead of “more natural” if you prefer. Do not exceed two paragraphs (one should be sufficient). Both your ideas and also the clarity with which they are expressed (i.e., your English prose) matter.
  • Download the Homework 2 code pack from the course web page. Mod- ify hw2.ml so that it implements a complete interpreter for “IMP with exceptions (and print)”. You may build on your code from Home- work 1 (although the let command is not part of this assignment). Using OCaml’s exception mechanism to implement IMP exceptions is actually slightly harder than doing it “naturally”, so I recommend that you just implement the opsem rules. The Makefile includes a “make test” target that you should use (at least) to test your work.
  • Modify the file example-imp-command so that it contains a “tricky” IMP command (presumably involving exceptions) that can be parsed by our IMP test harness (e.g., “imp < example-imp-command” should not yield a parse error).
  • Rename hw2.ml to uva email id-hw2.ml and rename example-imp-command to uva email id-imp-command and email them to me. Do not mod- ify any other files. Your submission’s grade will be based on how many of the submitted example-imp-commands it interprets correctly (in a manner just like the “make test” trials). If your submitted example-imp-command breaks the greatest number of interpreters (and more than 0!), you will receive extra credit. If there is a tie all tiers will receive the extra credit.