Solution of Assignment 2(New)

advertisement
SFWR ENG 2FA3. Solution to the Assignment #2
Total = 55, 100%= 50
The solutions below are often very detailed on purpose. Such level of details is not
required from students’ solutions. Most questions have more than one solution.
If you think your solution has been marked wrongly, write a short memo stating
where marking in wrong and what you think is right, and resubmit to me during class,
office hours, or just slip under the door to my office.
1.[2]
Prove Instantiation (9.13), ∀(x | : P) =⇒ P[x := E].
First note ∀(x | : P) = ∀(x | true : P).
∀(x | : P)
≡
h true = (x = E ∨ true) since true = p ∨ true i
∀(x | x = E ∨ true : P)
=⇒
h (9.10) i
∀(x | x = E : P)
≡
h (8.14) i
P[x := E]
2.[2]
Prove Range weakening/strengthening (9.25), ∃(x | R : P) =⇒ ∃(x | Q ∨ R : P).
∃(x | R : P)
h (9.19) i
∃(x | : R ∧ P)
≡
h (9.21) i
R ∧ ∃(x | : P)
=⇒
h since (R =⇒ Q ∨ R) and ((p =⇒ q) =⇒ ((p ∧ r) =⇒ (q ∧ r)))- both
from propositional logic i
(Q ∨ R) ∧ ∃(x | : P)
≡
h (9.21) i
∃(x | : (Q ∨ R) ∧ P)
≡
h (9.19) i
∃(x | Q ∨ R : P)
≡
3.[2] Prove ∃(x | : R) =⇒ ((∀(x | R : P) ⇒ Q) ≡ (∃(x | R : P ⇒ Q))) (provided x
does not occur free in P).
The proof will use the same pattern as the proof of (9.7) on page 160 of the textbook.
Suppose ∃(x | : R)
1
∀(x | R : P) ⇒ Q
h since p ⇒ q = ¬p ∨ q i
¬(∀(x | R : P)) ∨ Q
≡
h (9.17) - Generalized De Morgan i
∃(x | R : ¬P) ∨ Q
≡
h (9.23) i
∃(x | R : ¬P ∨ Q)
≡
h since p ⇒ q = ¬p ∨ q i
∃(x | R : P ⇒ Q)
≡
Hence: ∃(x | : R) =⇒ ((∀(x | R : P) ⇒ Q) ≡ (∃(x | R : P ⇒ Q)))
4.[3]
(f)[1]
Cubes of integers are never even.
¬∃(n, k | n, k ∈ ZZ : n · n · n = 2 · k).
(g)[1]
Real number i is the largest real solution of the equation f (i) = i + 1.
i ∈ IR ∧ f (i) = i + 1 ∧ ∀( j | j ∈ IR ∧ f ( j) = j + 1 : j ≤ i).
(h)[1]
Value f ( j) is always j + i greater than f (i).
∀(i, j |: f ( j) = f (i) + i + j).
5.[1] Assuming that each task t requires work.t seconds, the start time start.t for a
task t is the earliest time such that all prerequisites tasks in the set prereq.y have completed.
∀(s | s ∈ prereq.y : start.s + work.s ≤ start.t)
4a.[2]
(c)[1]
You can’t foll all the people all the time.
Let f ool(p,t) denotes ‘a person p is fooled at time t’, People denotes the set of all
people and let Time denotes the type time (usually Time is IN or positive Reals). Then:
¬(∀(p,t | p ∈ People ∧ t ∈ Time : f ool(p,t)).
(d)[1] You can’t fool a person all the time.
Let p denotes a person (i.e. p ∈ People). Then one possibility is
¬(∀(t | t ∈ Time : f ool(p,t)).
However natural language is often ambiguous, so the below version can also be
valid in some context:
2
¬(∀(p,t | p ∈ People ∧ t ∈ Time : f ool(p,t)).
6.[2] Show that the following argument is sound by translating it into predicate calculus and proving that the translation is a theorem: All men are mortal; Socrates is a
man; therefore, Socrates is mortal.
Let Men denotes the set of all men and mortal(x) denotes x is mortal:
All men are mortal: ∀(x | x ∈ Men : mortal(x)).
Socrates is a man: Socrates ∈ Men.
Socrates is mortal: By (9.13), i.e. Instantiation for ∀, we can write:
∀(x | : x ∈ Men ∧ mortal(x)) ⇒ mortal(x)[x := Socrates]
Hence mortal(Socrates).
7.[2]
(c)[1] Some values of b[ j..k] are zero:
∃(i | j ≤ i ≤ k : b[i] = 0).
(k)[1]
The values of b[ j..k] are in ascending order.
∀(i, l | j ≤ i ≤ k ∧ j ≤ l ≤ k : i ≤ l ⇒ b[i] ≤ b[l]), or
∀(i, l | j ≤ i ≤ l ≤ k : b[i] ≤ b[l])
(l)[2]
If x is in b[ j..k] then x + 1 is in b[k + 1..n − 1].
∃(i | j ≤ i ≤ k : x = b[i]) =⇒ ∃(i | k + 1 ≤ i ≤ n − 1 : x + 1 = b[i]).
8.[2]
Define the reverse of an array, e.g. the reverse of (3, 2, 5, 5) is (5, 5, 2, 3).
Assume the array is b[0..n − 1]. Then:
∀(i | 0 ≤ j < n : reverseb[i] = b[n − i − 1]).
9.[7] (g)[2] Suppose we have an array integer b[0..n − 1]. Each of subsegments
b[i.. j − 1] has a sum. Find the largest such sum.
Let Si, j = Σ(k | i ≤ k ≤ j − 1 : b[k]) and let i, j ∈ [0..n − 1] be an abbreviation for
0 ≤ i < n ∧ 0 ≤ j < n. Then:
{n > 1}
lsum :=?
{∃(i, j | i, j ∈ [0..n − 1] : lsum = Si j ) ∧ ∀(i, j | i, j ∈ [0..n − 1] : Si, j ≤ lsum)}
(i)[3] Integer array s[0..n] contains the grade of each student on a homework,
where negative number means that no grade was handed in find the median (i.e. the
number that half of the grades are lower and half higher).
3
Solution 1
Let os[0..n] be the array s[0..n] but in ascending order. Formally os[0..n] must
satisfy the following formula:
∀(i | 0 ≤ i ≤ n : ∃( j | 0 ≤ j ≤ n : os[i] = s[ j])) ∧ ∀(i, j | 0 ≤ i ≤ j ≤ n : os[i] ≤ os[ j]).
Let ordering(s[0..n]) be an abbreviation of the above formula.
Now we have two formulas for median, dependently if n is even or odd, formally:
∃(k : IN |: n = 2k) =⇒ median = ordered.s[k + 1], and
∃(k : IN |: n = 2k + 1) =⇒ median =
ordered.s[k]+ordered.s[k+1]
.
2
Let median(even.n) be an abbreviation of the formula with n = 2k and median(odd.n)
be an abbreviation of the formula with n = 2k + 1.
We can now write:
{n > 0} median :=? {ordering(s[0..n]) ∧ (median(even.n) ∨ median(even.n))}
The above solution suggests a simple implementation by sorting, which has a complexity O(nlogn), so it is not the fastest one as there are O(n) solutions. This is a
danger when specification is too algorithmic.
Solution 2
Let Lower(x) be the set of all elements of s[0..n] that are smaller or equal to an
integer x, formally: Lower(x) = {s[i] | 0 ≤ i ≤ n : s[i] ≤ x}.
Let Higher(x) be the set of all elements of s[0..n] that are bigger or equal to an
integer x, formally: Higher(x) = {s[i] | 0 ≤ i ≤ n : s[i] ≥ x}.
For every finite set X, let #(X) denotes the number of elements in X, for example
/ = 0, etc.
#({1, a, b, 4}) = 4, #(0)
We can now write:
{n > 0} median :=? {#(Lower(median)) = #(Higher(median))}
One can prove that if n is odd (so s[0..n]) has even number of elements) median
from the solution 2 equals the median from the solution 1, while if n is even (so
s[0..n]) has odd number of elements), solution does not specify the median exactly,
namely any number between ordered.s[k] and ordered.s[k + 1], where ordered.s[k]
and ordered.s[k + 1] are from solution 1, will do.
4
(k)[2] Array b contains the list of students at Cornell and c contains the list of
people who have part time jobs in Ithaca. Both lists are alphabetically ordered. Find
the first person who is on both lists.
Assume b[1..n] and c[1..m]. Let denotes the alphabetical ordering. Let Al pha(b[1..n])
be an abbreviation for the formula
∀(i, j|0 ≤ i ≤ n ∧ 0 ≤ j ≤ n : i ≤ j ⇒ b[i] b[ j]),
and similarly let Al pha(c[1..n]) be an abbreviation for the formula
∀(i, j|0 ≤ i ≤ n ∧ 0 ≤ j ≤ n : i ≤ j ⇒ c[i] c[ j]).
We can now write:
{1 ≤ n ∧ 1 ≤ m ∧ Al pha(b[1..n]) ∧ Al pha(c[1..n])} f irst :=?
{∃(i, j | f irst = b[i] = c[ j] ∧ ∀(k, l | k < i ∧ l < j : f irst 6= b[k] ∨ f irst 6= c[l]))}.
10.[3] (f)[1] Integer array b contains the grades of students on a homework (a negative number means the grade was not handed in). Change John’s grade (it is b[j]) to 80,
but if it is a late grade, also subtract 10 percent.
Comment: Rigid variables from the textbook and prime notation from the class are
in fact a very similar solution to the problem how to write for example x := x + 1, with
rigid variable x = X + 1, or with prime notation x0 = x + 1.
Assume late( j) means that this is late grade.
{b[ j] = X} b[ j] :=? {(¬late( j) ∧ b[ j] = 80) ∨ (late( j) ∧ b[ j] = 70}.
(h)[2] Array b contains red, white, and blue members. Put all the red ones first,
then the blues, then the whites.
Assume b[1..n], Colours = {red, blue, white} and ≺ is the following relation on
Colours: red ≺ blue, red ≺ white and blue ≺ white, i.e. red ≺ blue ≺ white. Define as α β ⇐⇒ α ≺ β ∨ α = β, for α, β ∈ Colours.
{n > 1 ∧ ∀(i | 1 ≤ i ≤ n : b[i] ∈ Colours} newb[1..n] :=?
{ ∀(i | 1 ≤ i ≤ n : ∃( j | 1 ≤ j ≤ n : newb[i] = b[ j])) ∧
∀(i, j | 1 ≤ i ≤ j ≤ n : newb[i] newb[ j]) }.
11.[2] Suppose that the number of apples that Mary and John have (represented by m
and j, respectively) are related by the formula (C is some constant):
P : C = m + 2 j.
Find a solution for e in {P ∧ even.m} m, j := m ÷ 2, e {P}.
5
Solution does not exist unless apple halves are allowed. For example C = 9, m = 5
and j = 2 satisfies P ∧ even.m, but since 5 ÷ 2 = 2, the new value of j must be 3.5
as 2 + 2 · 3.5 = 9. If halves of apples are allowed, then e = 0.5(C − m ÷ 2), since
(C = m + 2 j)[m, j := m ÷ 2, e] ≡ C = m ÷ 2 + 2e.
12.[2]
Use method (10.5) to prove that the following annotated program is correct.
{y > 0 ∧ z · xy = X}
if odd.y then z, y := z · x, y − 1 else x, y := x · x, y/2
{y ≥ 0 ∧ z · xy = X}
Comment. We do not need to use the rigid variable X above. Just any constant C
would suffice.
We have to show:
{y > 0 ∧ z · xy = X ∧ odd.y} z, y := z · x, y − 1 {y ≥ 0 ∧ z · xy = X}
and
{y > 0 ∧ z · xy = X ∧ even.y} x, y := x · x, y/2 {y ≥ 0 ∧ z · xy = X}.
We have
(y > 0 ∧ z · xy = X ∧ odd.y)[z, y := z · x, y − 1] = y − 1 > 0 ∧ z · x · xy−1 = X ∧ even.y =
y ≥ 0 ∧ z · xy = X ∧ even.y =⇒ y ≥ 0 ∧ z · xy = X,
so we have proved {Q ∧ B}S1{R} of (10.5).
Moreover
(y > 0 ∧ z · xy = X ∧ even.y)[x, y := x · x, y/2] = y/2 > 0 ∧ z · (x · x)y/2 = X ∧ even.y =
y > 0 ∧ z · xy = X ∧ even.y =⇒ y ≥ 0 ∧ z · xy = X,
so we have proved {Q ∧ ¬B}S2{R} of (10.5).
13.[1]
Show the postcondition R for the following program:
{y = 3}
x := 2;
z := x + y;
if y > 0 then x := z + 1
else z := 0
{R}
Solution: R : x = 6 ∧ y = 3 ∧ z = 5.
14.[2]
Show that the following Hoare triple is valid:
{true}
if x < y then min := x
6
else min := y
{(x ≤ y ∧ min = x) ∨ (x > y ∧ min = y)}
We will use (10.5) of the textbook (or identical from Lecture Notes). Also notes
that min = X is always valid as it just says that min has some value, so true ∧ x < y =
true ∧ x < y ∧ min = X.
We have
(true ∧ x < y ∧ min = X)[min := x] = x < y ∧ min = x =⇒
(x < y ∧ min = x) ∨ (x ≥ y ∧ min = y) ⇐⇒
(x ≤ y ∧ min = x) ∨ (x > y ∧ min = y).
so we have proved {Q ∧ B}S1{R} of (10.5).
Similarly
(true ∧ x ≥ y ∧ min = X)[min := y] = x ≥ y ∧ min = y =⇒
(x < y ∧ min = x) ∨ (x ≥ y ∧ min = y) ⇐⇒
(x ≤ y ∧ min = x) ∨ (x > y ∧ min = y).
so we have proved {Q ∧ ¬B}S2{R} of (10.5).
15.[2]
Show the postcondition R for the following program:
{z = 0 ∧ y = 5}
for i = 1 to 5 do
z := z + b[i];
y := y ∗ z od
{R}
The above Hoare triple is equivalent to:
{z = 0 ∧ y = 5}
z := z + b[1]; y := y ∗ z;
z := z + b[2]; y := y ∗ z;
z := z + b[3]; y := y ∗ z;
z := z + b[4]; y := y ∗ z;
z := z + b[5]; y := y ∗ z
{R}.
Hence:
R : z = b[1] + b[2] + b[3] + b[4] + b[5] ∧
y = 5b[1](b[1] + b[2])(b[1] + b[2] + b[3])(b[1] + b[2] + b[3] + b[4])(b[1] + b[2] + b[3] +
b(4) + b[5]).
16.[2]
(d)[1] Prove by induction: for n ≥ 0, Σ(i | 0 ≤ i < n : 3i ) = (3n − 1)/2.
Clearly true for n = 0 as 30 = 1. Suppose it is true for n, so we consider n + 1:
7
Σ(i | 0 ≤ i < n + 1 : 3i ) = Σ(i | 0 ≤ i < n : 3i ) + 3n = (3n − 1)/2 + 3n =
0.5(3n − 1 + 2 · 3n ) = 0.5(3 · 3n − 1) = (3n+1 − 1)/2.
(f)[1] Prove by induction: for n ≥ 0, Σ(i | 1 ≤ i ≤ n : i · 2i ) = (n − 1) · 2n+1 + 2.
Clearly true for n = 0 as (0 − 1) · 20+1 + 2 = −2 + 2 = 0, so we consider n + 1:
Σ(i | 1 ≤ i ≤ n + 1 : i · 2i ) = Σ(i | 1 ≤ i ≤ n : i · 2i ) + (n + 1) · 2n+1 =
(n − 1) · 2n+1 + 2 + (n + 1) · 2n+1 = 2 · n · 2n+1 + 2 = n · 2n+2 + 2.
17.[3]
Prove that
Σ(i | 0 ≤ i ≤ n : i · ri ) =
n · rn+2 − (n + 1) · rn+1 + r
(r − 1)2
for r a real number, r 6= 1, and n a natural number.
It can be easily checked that this is true for n = 0.
It is more convenient to show:
(r − 1)2 · Σ(i | 0 ≤ i ≤ n : i · ri ) = n · rn+2 − (n + 1) · rn+1 + r.
Suppose it is true for n and consider n = 1,
(r − 1)2 · Σ(i | 0 ≤ i ≤ n + 1 : i · ri ) =
(r − 1)2 · Σ(i | 0 ≤ i ≤ n : i · ri ) + (r − 1)2 · (n + 1) · rn+1 =
(n · rn+2 − (n + 1) · rn+1 + r) + (r − 1)2 · (n + 1) · rn+1 =
n · rn+2 − n · rn+1 − rn+1 + r + (r2 − 2 · r + 1)(n · rn+1 + rn+1 ) =
n · rn+2 −n · rn+1 − rn+1 + r + r2 · n · rn+1 − 2 · r · n · rn+1 + n · rn+1 +
r2 · rn+1 −2 · r · rn+1 + rn+1 =
r2 · n · rn+1 + r2 · rn+1 + n · rn+2 − 2 · r · n · rn+1 − 2 · r · rn+1 + r =
(n + 1) · rn+3 + (n + 2) · rn+2 + r.
So we are done.
18.[2]
Prove by induction that 22n − 1 is divisible by 3 for n ≥ 0.
Clearly true for n = 0 as 22·0 − 1 = 0. Also note that if n ≥ 1 then 22n − 1 > 0.
22n − 1 is divisible by 3 and 22n − 1 > 0 means 22n − 1 = 3 · k for some k.
Consider 22(n+1) − 1. Suppose 22n − 1 = 3 · kn for some kn .
22(n+1) − 1 = 22n+2 − 1 = 22 · 2n − 1 = 4 · 2n − 1 = 3 · 2n + (2n − 1) = 3 · 2n + 3 · kn =
3 · (2n + kn ) = 3 · kn+1 , where kn+1 = 2n + kn , so we are done.
8
19.[3]
Prove by induction that for x 6= y, xn − yn is divisible by x − y, for n ≥ 0.
Clearly true for n = 0 as x0 − y0 = 0 and 0 is divisible by any number different than 0.
xn − yn is divisible by x − y means xn − yn = fn (x, y) · (x − y) for some function
fn : IR × IR → IR.
Suppose xn − yn is divisible by x − y and consider xn+1 − yn+1 .
xn+1 − yn+1 = xn+1 − yn+1 + x · yn − x · yn = (xn+1 − x · yn ) + (x · yn − yn+1 ) =
x · (xn − yn ) + yn · (x − y) = x · fn (x, y) · (x − y) + yn · (x − y) =
(x · fn (x, y) + yn ) · (x − y) = fn+1 (x, y) · (x − y), where fn+1 (x, y) = x · fn (x, y) + yn , so
we are done.
20.[3]
ing.
Juris Jones maintains that he is exactly one-third Latvian. Prove that he is ly-
The idea is that if one of your parents is pure Latvian and another has zero Latvian
genes, you are 0.5 Latvian, and this goes back to all your ancestors. Hence if your
mother is x Latvian and your father is y Latvian, you are (x + y)/2 Latvian.
Let Latvian be the following set of numbers:
1. 0 ∈ Latvian and 1 ∈ Latvian
2. if x ∈ Latvian and y ∈ Latvian then (x + y)/2 ∈ Latvian.
/ Latvian.
We will show that 13 ∈
Define Latviann as the set of all number that can be derived from 0 and 1 using the
rule (2) above n times. For example Latvian0 = {0, 1}, Latvian1 = {0, 21 , 1}, Latvian2 =
S
{0, 41 , 12 , 34 , 1}, etc. Clearly Latvian = ∞
0 Latviann , i.e.
x ∈ Latvian ⇐⇒ ∃(n | 0 ≤ n : x ∈ Latviann ).
We show (by induction) that x ∈ Latviann if an only if x = k/2n where 0 ≤ k ≤ 2n .
This is clearly true for Latvian0 as 0 = 0/20 and 1 = 20 /20 . Assume that this holds
for n and consider n + 1.
Note that Latviann+1 = Latviann ∪ {(z + y)/2 | z ∈ Latviann ∧ y ∈ Latviann }.
n
If x ∈ Latviann then x = k/2n for some k, hence x = k/2n = 22·k
n+1 . If 0 ≤ k ≤ 2
them 0 ≤ 2 · k ≤ 2n+1 , so x satisfies the pattern.
Suppose x ∈ {(z + y)/2 | z ∈ Latviann ∧ y ∈ Latviann }, i.e. x = (z + y)/2 where
z ∈ Latviann and y ∈ Latviann . Assume z = r/2n and y = s/2n .
Here we have
r
s
r+s
r+s
n + 2n
n
x= 2
= 2 = n+1 .
2
2
2
9
Since 0 ≤ r ≤ 2n and 0 ≤ s ≤ 2n implies 0 ≤ r + s ≤ 2n + 2n = 2n+1 , x satisfies a given
pattern as well.
In this way we have proved that x ∈ Latviann if an only if x = k/2n where 0 ≤ k ≤ 2n .
Now it suffice to show that there are no such natural numbers k and n that 13 = 2kn .
Suppose that there are, i.e. 31 = 2kn , which means 2n = 3 · k, which is false because 2n
is not divided by 3 (if n > 0).
Which means Juris Jones cannot be 1/3 Latvian.
21.[1]
Define the values mn for n ≥ 0 recursively by
m0 = 0,
mn+1 = 2 · mn + 1, for n ≥ 0.
Prove by induction that mn = 2n − 1 for n ≥ 0.
Clearly true for n = 0. Suppose it holds for n and consider n + 1.
mn+1 = 2 · mn + 1 = 2 · (2n − 1) + 1 = 2 · 2n − 2 + 1 = 2n+1 − 1,
which ends this proof.
10
Download