Docsity

Advanced Programming Languages: Homework Assignment 2F and 2C - EECS 590, Exams of Programming Languages

Toggle sidebar
bg1
Advanced Programming Languages
Homework Assignment 2F and 2C
EECS 590
Logistics. You must work alone. Your name and Michigan email address (and the book-
keeping problem) must appear on the first page of your PDF submission but may not appear
anywhere else. This is to protect your identity during peer review. The first page of your
submission is not shared during peer view but all subsequent pages are.
Exercise 2F-1. Bookkeeping [2 points]. These answers should appear on the first page
of your submission and are kept private.
1. Indicate in a sentence or two how much time you spent on this homework.
2. Indicate in a sentence or two how difficult you found it subjectively.
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, The Princess Bride
All subsequent answers should appear after the first page of your submission and may be
shared publicly during peer review.
Exercise 2F-2. Mathematical Induction [5 points]. Find the flaw in the following
inductive proof that “All flowers smell the same”. Please indicate exactly which sentences
are wrong in the proof via highlighting or underlining.
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, f 0∈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:
1
pf3
pf4

Partial preview of the text

Download Advanced Programming Languages: Homework Assignment 2F and 2C - EECS 590 and more Exams Programming Languages in PDF only on Docsity!

Advanced Programming Languages

Homework Assignment 2F and 2C

EECS 590

Logistics. You must work alone. Your name and Michigan email address (and the book- keeping problem) must appear on the first page of your PDF submission but may not appear anywhere else. This is to protect your identity during peer review. The first page of your submission is not shared during peer view but all subsequent pages are.

Exercise 2F-1. Bookkeeping [2 points]. These answers should appear on the first page of your submission and are kept private.

  1. Indicate in a sentence or two how much time you spent on this homework.
  2. Indicate in a sentence or two how difficult you found it subjectively.

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, The Princess Bride

All subsequent answers should appear after the first page of your submission and may be shared publicly during peer review.

Exercise 2F-2. Mathematical Induction [5 points]. Find the flaw in the following inductive proof that “All flowers smell the same”. Please indicate exactly which sentences are wrong in the proof via highlighting or underlining. 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 Case: 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 2F-3. While Induction [10 points]. Prove by induction the following state- ment 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 2F-4. Language Features, Large-Step [12 points]. 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 operational 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.

Exercise 2F-5. Language Features, Analysis [6 points]. 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.

Exercise 2C. Language Features, Coding. Download the Homework 2 code pack from the course web page. Modify hw2.ml so that it implements a complete interpreter for “IMP with exceptions (and print)”. You may build on your code from Homework 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 operational semantics rules. The Makefile includes a “make test” target that you should use (at least) to test your work. Hint: to check if a termination term is an exception, use syntax like

begin match term with | Normal -> do_something | Exceptional(n) -> do_something_else using n end

Modify the file example-imp-command so that it contains a “tricky” terminating 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).

Submission. Turn in the formal component of the assignment as a single PDF document via the gradescope website. Your name and Michigan email address must appear on the first page of your PDF submission but may not appear anywhere else. Turn in the coding component of the assignment via the autograder.io website.