1. Problem A.3 asks for a specification of a program that, given a fixed array B, sets integer variables start and len to values such that len is the length of a longest segment of B in which no value occurs more than once, and such a segment begins at location start.
The problem did not mention the type of values in B, nor did it specify what predicate characterizes "equality" (or "sameness", or "non-distinctness", or whatever) of two such values. For our purposes, suppose that this type is T and that = serves as the predicate in question.
Here is (an edited version of) one student's answer:
|[ con B : array of T;
var p,q,i,j : int;
longest_distinct_segment
{ Q0 ∧ Q1 }
]|
where
where
allDiff.p.q ::= (∀i,j | p≤i<j<q : B.i ≠ B.j)
Questions:
Another student had this postcondition:
Q: z = (MAX start,len | 0≤start≤len≤#B : noDup.start.len)
where noDup.p.q ::= (∀i | p≤i≤q : B.i ≠ B.(i+1))
Questions:
2. Problem A.4 asks for a specification of a program that, given a fixed integer N>2, sets integer variable p to the smallest perfect number that is greater than N.
Assume that the predicate perfect.k is satisfied if (and only if) k meets the definition of "perfect".
One student's postcondition was
Q: z = (∀i | N<i : perfect.i<perfect.(i+1) ∧ perfect.i>N)
Here's another postcondition (which is a combination of those given by two students):
Q: (∀j | j < p : ¬perfect.p)
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding itself. Surprisingly, not a single student gave a correct formal definition. Here's one attempt, with some details omitted:
perfect.k ::= (∀k | ... )
Here is another attempt:
perfect.k ::= (+j | 0<j<k ∧ k%j=0 : j)
3. Problem A.5 asks for a specification of a program that, given an integer array b and a constant integer K, rearranges the elements of b so that all occurrences in b of values less than K precede all occurrences in b of values greater than K.
Startlingly, only one student included in her/his postcondition the need for b's final value to be in a certain relationship to its original value.
Here is one student's postcondition:
Q: (∀i,j | 0≤i≤j<#b : b.i≤K ∧ b.j≥K)
Here is another student's postcondition, which is slightly different from the one above:
Q: (∀i,j | 0≤i<j<#b : b.i≤K ∧ b.j>K)
A correct answer to the next question will put you well on your way towards formulating a "correct" postcondition:
4. What stuck out most about students' answers to Problem B.1 was the incorrect usage (and omission) of square brackets. Recall from HW#9 that [k] is simply a shorthand notation for k ⊕ empty, where
was defined in HW#7 (and where Elem is, as it were, a "generic" type that can be anything we want it to be). The other relevant operator is | (list catenation), which is associative and has signature
In Problem B.1, Elem corresponds to int and INSERT has signature
Suppose that k is of type int and both x and y are of type List of int. For each of the following expressions, insert/remove square brackets where necessary in order to make the expression meaningful: