Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Problem 1
We would like to show that
n N, P (n) : power n x = xn
P (0), the base case: We would like to show that power 0 x = x0 .
Problem 2
Principle of induction:
n nat, P (n) if P (Zero) and P (Succ n) when P (n)
We would like to show that
n nat, P (n) : power n x = xtoInt n
P (Zero), the base case: We would like to show that power Zero x = xtoInt Zero .
Homework #5
Homework #5
Problem 3
Principle of induction:
l list, P (l) if P ([]) and P (v :: l0 ) when P (l0 )
We would like to show that
l, r list, P (l, r) : length (l @ r) = length l + length r
Let r be arbitrary in list. We consider the cases for l list.
P ([], r), the base case: We would like to show that length ([] @ r) = length [] + length r.
Homework #5
Problem 4
We would like to show that
l list, P (l) : length (reverse l) = length l
P ([]), the base case: We would like to show that length (reverse []) = length [].
This follows from the definition of reverse.
P (x :: xs), the inductive case:
We would like to show that length (reverse x :: xs) = length x :: xs.
Inductive hypothesis: length (reverse xs) = length xs holds.
Homework #5
Problem 5
We would like to show that
l1, l2 list, P (l1, l2) : reverse (l1 @ l2) = reverse l2 @ reverse l1
Let l2 be arbitrary in list. We consider the cases for l1 list.
P ([], l2), the base case: We would like to show that reverse ([] @ l2) = reverse l2 @ reverse [].
Problem 6
We would like to show that
e elements, l list, P (l, e) : sorted l sorted (place e l)
P ([], e), the base case: We would like to show that sorted [] sorted (place e []).
Homework #5
Homework #5
Case: e < x, xs 6= []
Assume sorted x :: xs holds. We will show that sorted (place e x :: xs) follows given the inductive
hypothesis.
Case: e x, xs 6= []
Assume sorted x :: xs holds. We will show that sorted (place e x :: xs) follows given the inductive
hypothesis.
sorted (place e x :: xs) = sorted (x :: (place e xs)), by definition of place
Let x0 be the first element of xs. By assumption, sorted x :: xs = true, so (x x0 ) is true and sorted xs
is true by the definition of sorted.
sorted x :: xs = true sorted xs = true sorted (place e xs) = true
The base case and all subcases of the inductive case have been shown to be true, therefore we can conclude:
e elements, l lists, sorted l sorted (place e l)
Homework #5
Problem 7
iselem e (place e l) evaluates to true even for unsorted lists because place inserts e into l such that:
1. All elements at lower indices in the list are less than e.
2. e is less than the value at the next index.
iselem searches for e in l by going from the lowest index to the highest. It gives up once it finds an element
in the list greater than e.
Therefore, if e is placed in the l, iselem will not encounter a value in l larger than e before encountering
e. Sorting is not necessary.
The premise sorted l is necessary for question 6. If the entire list is unsorted, there is no way place can
guarantee a sorted list by merely inserting one element.
Problem 8
Loop invariant: z = y i
1. The precondition is that x, y, z, i are integers and x 0. Before the loop,
z = 1, i = 0
yi = y0 = 1 = z
z = y i before entering the loop
2. We have showed that before the loop, z = y i . Only z and i get updated each iteration, so let z 0 and i0
be z and i after one iteration.
0
z 0 = z y = y i y = y i+1 = y i
Problem 9
tab tab tab
n lower = lower
upper = n
n 1.0 n2 n
Given n 1.0, so upper = n
lower
Homework #5
Homework #5
n upper.
Let lower0 and upper0 be lower and upper updated after one iteration.
There are two cases:
Case 1: ((lower + upper)/2.0)2 > n
((lower + upper)/2.0)2 > n 1.0 (lower + upper)/2.0 >
lower0 = lower n
upper0 = upper n
3. When the loop terminates, the negation of the loop condition and the loop invariant, which we have
shown to hold, implies the post condition.
loop condition upper lower accuracy = .01
lower
10