Review2Review Session for Second Midterm
General Notes
Hints
- On each version of the exam, there will be at least one problem
taken more or less verbatim from a homework assignment.
- Both versions will include one or more decorated programs.
- On the advanced version, there will be an informal proof.
- This set of review questions is biased toward ones that can be
discussed in class / using clickers, so it doesn't fully
represent the range of questions that might show up on the exam.
On a piece of paper, write down the precise definition of what it
means to say that two Imp programs are equivalent.
On a piece of paper, write down precisely what it means to say
that a Hoare triple is valid.
In the Hoare chapter, we used the notation bassn b to "lift a
boolean expression into an assertion." Briefly explain what this
means and why it's needed.
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
X::= Y+1
Y::= X-1
Y::= X-1
X::= Y+1
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
X::= Y-1
Y::= X+1
Y::= X+1
X::= Y-1
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
IFB X <> 1 THEN Y ::=0 ELSE Y ::= 1 FI
IFB X > 0 THEN Y ::=0 ELSE Y ::= 1 FI
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
IFB X <> 1 THEN Y ::= 1 ELSE Y ::= 1 FI
IFB X >= 0 THEN Y ::= 1 ELSE Y ::= 0 FI
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
IFB X <> 0 THEN SKIP ELSE Y ::= 1 FI
IFB X < 0 THEN Y ::= 0 ELSE Y ::= 1 FI
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
Y ::= 0
WHILE X > 0 DO
Y ::= Y+X
X ::= X-1
END
Z ::= Y
WHILE X > 0 DO
Y ::= Y+X
X ::= X-1
END
Z ::= Y
Y ::= 0
WHILE X > 0 DO
Y ::= Y+X
Z ::= Y
X ::= X-1
END
WHILE X > 0 DO
Y ::= Y+X
Z ::= Y
X ::= X-1
END
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
WHILE X > 0 DO
Y ::= Y+1
X ::= X-1
END
Y ::= Y+1
X ::= X-1
END
Y ::= X + Y
Are the two following programs equivalent?
(1) yes
(2) no (and give a counterexample)
WHILE X > 0 DO
Y ::= 0
X ::= X+1
END
Y ::= 0
X ::= X+1
END
IF X > 0 THEN
WHILE True DO
Y ::= X + Y
END
ELSE
SKIP
FI
WHILE True DO
Y ::= X + Y
END
ELSE
SKIP
FI
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ X = 5 ∧ Y= 6 }}
X := Y
{{ X = 6 ∧ Y = 6 }}
X := Y
{{ X = 6 ∧ Y = 6 }}
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ X = 5 ∧ Y= 6 }}
Y := X
{{ X = 6 ∧ Y = 6 }}
Y := X
{{ X = 6 ∧ Y = 6 }}
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ True }}
IF Y <= X THEN
X ::= Y
ELSE
Y ::= X
FI
{{ X > Y }}
IF Y <= X THEN
X ::= Y
ELSE
Y ::= X
FI
{{ X > Y }}
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ X = m ∧ Y = n }}
IF Y <= X THEN
X ::= Y
ELSE
Y ::= X
FI
{{ X = Y ∧ X = min(m,n) }}
IF Y <= X THEN
X ::= Y
ELSE
Y ::= X
FI
{{ X = Y ∧ X = min(m,n) }}
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ X = m ∧ Y = 0 }}
WHILE X <> 0 DO
Y ::= Y + X
X ::= X - 1
END
{{ Y = (m*(m-1))/2 ∧ X = 0 }}
WHILE X <> 0 DO
Y ::= Y + X
X ::= X - 1
END
{{ Y = (m*(m-1))/2 ∧ X = 0 }}
Is the following Hoare triple valid ?
(1) yes
(2) no (and give a counterexample)
{{ X = m + 1 }}
WHILE X > 0 DO
X ::= X+1
END
{{ False }}
WHILE X > 0 DO
X ::= X+1
END
{{ False }}
Fill in valid decorations for the following program:
{{ X = m ∧ Y = n }}
IF Y <= X THEN
{{ }} ⇾
{{ }}
X ::= Y
{{ }} ⇾
{{ }}
ELSE
{{ }} ⇾
{{ }}
Y ::= X
{{ }} ⇾
{{ }}
FI
{{ X = Y ∧ Y = min(m,n) }}
IF Y <= X THEN
{{ }} ⇾
{{ }}
X ::= Y
{{ }} ⇾
{{ }}
ELSE
{{ }} ⇾
{{ }}
Y ::= X
{{ }} ⇾
{{ }}
FI
{{ X = Y ∧ Y = min(m,n) }}
Fill in valid decorations for the following program:
{{ X = m ∧ Y = 0 }}
WHILE X <> 0 DO
{{ }} ⇾
{{ }}
Y ::= Y + X
{{ }}
X ::= X - 1
{{ }}
END
{{ }} ⇾
{{ Y = (m*(m+1))/2 }}
WHILE X <> 0 DO
{{ }} ⇾
{{ }}
Y ::= Y + X
{{ }}
X ::= X - 1
{{ }}
END
{{ }} ⇾
{{ Y = (m*(m+1))/2 }}
Good luck on the exam!