Midterm
Midterm
Midterm
Prof. David Walker
This midterm should be the individual work of each student in the class. Do not talk to anyone other
than myself (David Walker) or Rob Dockins about the questions on this midterm. If neither I nor Rob are
available while you are taking the exam and you have a question, make a reasonable assumption, write down
that assumption clearly and continue working. Talking to anyone else about this exam while you are taking
it constitutes a violation of Princeton’s code of academic integrity. You may consult your lecture notes, any
of the course textbooks, any of the course web pages, slides, assignments, mailing list posts, etc. Do not
search for the answers on the general Web (not that you’d find any).
You will work on the exam during a single, continuous 5-hour period occurring between Monday Nov
3rd and Monday Nov 10th (midnight), 2008. You must complete the exam in the 5-hour period of time. At
the top of the exam, write down the time you download the exam and the time you hand it in. Please also
sign your name and write: “This exam represents my own work in accordance with University regulations.”
Hand in exams by slipping them under Robs office door or by submitting them electronically via email to
rdockins@cs.princeton.edu. The time you spend walking to Rob’s office to hand in the exam does not count
as exam time so you can feel free to do the exam at home, stop when the five hours are up, write your
completion time at the top of the exam and walk it over at your leisure.
Reminders:
• Read questions carefully and completely before beginning your response. Part of the test is whether
or not you are able to read and understand the questions, including the formal inference rules.
• Always state your proof methodology clearly and explicitly before writing down the details of a proof
(eg: “Proof by induction on the derivation of Γ ` e : τ ”). Points are automatically deducted from
anyone who does not do this. Points will also be deducted for proofs that are unclear or poorly
structured.
• Always use the exact syntax of expressions, types, judgments, etc. that you are given in a question,
or clearly define the abbreviations that you’re using. When in doubt, avoid abbreviations. Definitely
do not just start using some new, informal notation without defining it – graders will not be able to
figure out your intent.
• When writing out your proofs, use plenty of space (either electronically or on paper) to make them
easy to read. One of the best ways is to format each line with one true statement (judgment) on the
left and the justification for that statement on the right (in terms of earlier statements and inference
rules, etc.).
• You might find some of the questions quite difficult – try not to get stressed out. Simply move on
to trying to solve a different question. Don’t spend all your time just trying to answer one or two
questions. Partial credit will be given where appropriate when you give partial answers.
The exam is out of 20. Good luck!
1
Terminology
Q. 1 [1 point] Explain in a sentence or two what it means for two expressions to be “alpha-equivalent.”
Q. 2 [1 point] Explain in a sentence or two what it means to do a step in a proof using “inversion.”
Inductive Definitions
Q. 3 [3 points] Here are BNF definitions of natural numbers and lists of natural numbers (the same as we
have seen in assignment #2, for example):
n ::= Z | S n
l ::= nil | cons(n, l)
Define inductive rules for a judgement that sorts lists. Your judgement should have the form ` sort l1 l2
where l1 may be unsorted and l2 contains the same elements as l1 but is sorted in non-descending order (ie:
there may be duplicate elements; each element is greater than or equal to the previous one). An insertion
sort may be one of the easiest sorting algorithms to implement, so you are encouraged to take inspiration
from it. And you can look up insertion sort in your COS 226 notes if you have forgotten how it works, though
you certainly don’t have to do it exactly that way. However, implementing any correct sorting algorithm is
just fine.
You may assume that a judgement ` leq n1 n2 has been defined for you (see assignment #2 for such a
definition). ` leq n1 n2 is valid when n1 is less than or equal to n2 . You may also assume that a “strictly
greater than” judgement ` gt n1 n2 has been defined for you (as in question 4 below).
You are also allowed to (and in fact encouraged to) define auxiliary judgements that help you perform
sorting. If you correctly implement important subcomponents of a sorting algorithm (such as an insert
judgement for use in insertion sort) and explain (in a few words) what those subcomponents do, you will
receive partial credit even if the overall sort judgement is incorrect.
Q. 4 [3 Points] Consider the following rules for the “strictly greater than” relation over natural numbers
(` gt n1 n2 ) as defined by the following two rules:”
(gt1)
` gt (S n) Z
` gt n1 n2
(gt2)
` gt (S n1 ) (S n2 )
Operational Semantics
The next couple of questions deal with the following new language my research team is investigating. It has
a set of “baffler expressions” b and “baffler values” w with the following syntax:
In this language BOF and FOB are binary operators. Values include K and ZAB w. Here are the single-step
operational semantics rules for the language:
2
b1 BOF b2 −→ K
K FOB K −→ ZAB K
b2 −→ b02
b1 FOB b2 −→ b1 FOB b02
b1 −→ b01
b1 FOB w2 −→ b01 FOB w2
Q. 5 [1 point] What does the following expression step to in a single step (according to the operational
semantics defined above)?
Q. 7 [1 point] Add one or more operational rules in such a way that the language remains deterministic
and there are no “stuck” baffler expressions. In other words, after adding your rules, it should be the case
that for every expression b, either b is a value, or there exists exactly one b0 such that b −→ b0 .
Q. 8 [3 points] Consider 2 different versions of the multi-step operational relation (which is similar to what
we saw in assignment #4).
Version 1:
(rule A)
e1 −→∗ e1
e1 −→ e2 e2 −→∗ e3
∗ (rule B)
e1 −→ e3
Version 2:
(rule C)
e1 =⇒∗ e1
e1 =⇒∗ e2 e2 −→ e3
(rule D)
e1 =⇒∗ e3
Prove that if e −→∗ e0 then e =⇒∗ e0 . In your proof, you may use (without reproving it on the exam)
either lemma from Q. 4 or Q. 5 of assignment #4. You will also receive full credit for a correct proof that
doesn’t use those lemmas.
3
Typing Rules and Type Safety
The following questions are about the Simply-typed lambda calculus with Integers and Booleans (henceforth,
I’ll call this language the “STIB”). Below is the syntax of the language. We studied it’s typing rules and
operational semantics in class. We also proved that the progress and preservation lemmas were true for this
system.
Q. 9 [2 points] Consider the STIB and suppose it is modified so that the normal typing rule for function
application, which is this:
Γ ` e1 : τ1 → τ2 Γ ` e2 : τ1
Γ ` e1 e2 : τ2
is removed and replaced by this rule:
Γ ` e1 : τ1 Γ ` e2 : τ1 → τ2
Γ ` e1 e2 : τ2
Now for each statement below write “yes” (next to the appropriate letter on your answer sheet) if you agree
with the statement. Write “no” if you disagree with the statement. For each correct answer, you get +1
point. For each incorrect answer, you get -1 point. Guessing can lose you points.
For example, for part (a) below, if you think “The progress lemma is not true in the new type system”
then you should be able to come up with a counter-example to the progress lemma to convince yourself (ie,
you should be able to find expression that type checks but is stuck). If you can come up with a counter-
example, you’ll write “yes”. If you can’t come up with a counter-example and believe no counter-example
exists then you’ll write “no.” If you can’t come up with a counter-example, but you aren’t sure whether one
exists, you should leave the question blank to avoid losing points.
(a) The progress lemma is not true in the new type system.
(b) The preservation lemma is not true in the new type system.
Q. 10 [2 points] Consider the STIB and suppose it is modified so that the normal typing rule for functions
which is this:
Γ, x:τ1 ` e : τ2 (x 6∈ Dom(Γ))
Γ ` λx:τ1 .e : τ1 → τ2
Γ ` x : τ1 Γ ` e : τ2 (x 6∈ Dom(Γ))
Γ ` λx:τ1 .e : τ1 → τ2
Now for each statement below write “yes” if you agree with the statement. Write “no” if you disagree with
the statement. For each correct answer, you get +1 point. For each incorrect answer, you get -1 point.
(Guessing can lose you points.)
(a) The progress lemma is not true in the new type system.
(b) The preservation lemma is not true in the new type system.
4
Q. 11 [2 points] Consider the STIB and suppose it is modified so that the normal typing rule for variables,
which is this:
(x : τ ) ∈ Γ
Γ`x:τ
(a) The progress lemma is not true in the new type system.
(b) The preservation lemma is not true in the new type system.