CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
Your job is to deliver code that you know works.
{{ P: i = 0 }}
{{ Inv: I }}
while (i < n && 0 <= a[i]) {
i = i + 1;
}
{{ Q: 0 <= i <= n and a[0..i] contains no negative values
and (i = n or a[i] < 0) }}n is the length of a; a[0..i] is a's first i elementsP: the claim before the loop; b: its condition; Q: the claim afterI?I, lettered as on the Exams page:
B. 0 <= i <= n
C. 0 <= i <= n and a[0..i] contains only positive values
E. 0 <= i < n and a[0..i] contains no negative values
The three checks, from Lecture 8:
(1) P implies I.
(2) The triple {{ I and b }} body {{ I }} is valid.
(3) I and not b implies Q.
I says{{ P: i = 0 }}
{{ Inv: 0 <= i <= n }}
while (i < n && 0 <= a[i]) {
i = i + 1;
}
{{ Q: 0 <= i <= n and a[0..i] contains no negative values
and (i = n or a[i] < 0) }}P implies I: Y
(2) {{ I and b }} body {{ I }} is valid: Y
(3) I and not b implies Q: N
a = {-1, -2}, i = 1: I and not b are true, Q false
{{ P: i = 0 }}
{{ Inv: 0 <= i <= n and a[0..i] contains only positive values }}
while (i < n && 0 <= a[i]) {
i = i + 1;
}
{{ Q: 0 <= i <= n and a[0..i] contains no negative values
and (i = n or a[i] < 0) }}P implies I: Y
(2) {{ I and b }} body {{ I }} is valid: N
a = {0}, i = 0: then a[0..1] contains 0, not positive
(3) I and not b implies Q: Y
{{ P: i = 0 }}
{{ Inv: 0 <= i < n and a[0..i] contains no negative values }}
while (i < n && 0 <= a[i]) {
i = i + 1;
}
{{ Q: 0 <= i <= n and a[0..i] contains no negative values
and (i = n or a[i] < 0) }}P implies I: N: if a is empty, i < n is false
(2) {{ I and b }} body {{ I }} is valid: N
a = {0}, i = 0: after the body, i = n, not below n
(3) I and not b implies Q: Y
Candidate I |
Check (1) | Check (2) | Check (3) |
|---|---|---|---|
B. {{ 0 <= i <= n }} |
Y | Y | N |
Definition: Behavioral subtype
A type B is a behavioral subtype of a type A if: every object that satisfies B's specification also satisfies A's. In other words: for each method of A, that method exists in B, and the B version's specification is at least as strong as the A version's.
/** @modifies this
* @effects increases the value by exactly 1 */
public void increment() { n++; }/** @requires value() < limit
* @modifies this
* @effects increases the value by exactly 1 */
public void increment()BoundedCounter (below) a behavioral subtype of Counter (top)?value() < limit/** @modifies this
* @effects increases the value by 1 if below the limit;
* otherwise leaves it unchanged */
public void increment() { if (n < limit) n++; }/** @requires value() < limit
* @modifies this
* @effects increases the value by exactly 1 */
public void increment()SaturatingCounter (top) a behavioral subtype of BoundedCounter?/** @requires value() < limit
* @modifies this
* @effects increases the value by exactly 1 */
public void increment()/** @modifies this
* @effects increases the value by 1 if below the limit;
* otherwise leaves it unchanged */
public void increment() { if (n < limit) n++; }BoundedCounter (top) a behavioral subtype of SaturatingCounter?Counter$ curl -d 'name=ada&message=hello' http://localhost:9331/api/messages ["ada: hello"] $ curl http://localhost:9331/api/messages []
Definition: persistent
Data is persistent if it outlasts the run of the program that wrote it.
The promise
When the server responds to a post request, that message will be saved forever.
In the constructor, at startup:
128if (Files.exists(file)) {
129 messages.addAll(Files.readAllLines(file, UTF_8));
130}In save, which add calls on every post:
154try (FileOutputStream out = new FileOutputStream(file.toFile())) {
155 out.write(text().getBytes(UTF_8));
156}save writes the book to book.txt154try (FileOutputStream out = new FileOutputStream(file.toFile())) {
155 out.write(text().getBytes(UTF_8));
156}save lose a message whose post got a response?159try (FileOutputStream out = new FileOutputStream(file.toFile())) {
160 Crash.at("opened");
161 out.write(text().getBytes(UTF_8));
162}Crash.at, added for testingopened$ curl -d 'name=cy&message=hey' http://localhost:9331/api/messages curl: (52) Empty reply from server $ curl http://localhost:9331/api/messages []
book.txt is empty: truncated on open, crashed before the writesave, at a point we chose159Path temp = Path.of(file + ".tmp");
160try (FileOutputStream out = new FileOutputStream(temp.toFile())) {
161 Crash.at("opened");
162 out.write(text().getBytes(UTF_8));
163 out.getFD().sync();
164}
165Crash.at("written");
166Files.move(temp, file, ATOMIC_MOVE);The claim, at every point in save
book.txt holds a whole book: the old one or the new one.
book.txt.tmp changes, so book.txt is oldbook.txt is the new file, wholeATOMIC_MOVE asks the operating system for exactly that$ curl -d 'name=bob&message=hi' http://localhost:9331/api/messages curl: (52) Empty reply from server $ curl http://localhost:9331/api/messages ["ada: hello"]
opened and at written: the old bookwritten, book.txt.tmp holds ada and bob; book.txt holds ada159Path temp = Path.of(file + ".tmp");
160try (FileOutputStream out = new FileOutputStream(temp.toFile())) {
161 Crash.at("opened");
162 out.write(text().getBytes(UTF_8));
163 out.getFD().sync();
164}
165Crash.at("written");
166Files.move(temp, file, ATOMIC_MOVE);out.getFD().sync(): every crash test still passesout.getFD().sync(): every run comes out the samehalt crashes the program; the system still writes the bytes latersync returns once the bytes are on the disk, before the movesync promises puts that call in the codeLazyFS: Ramos et al., When Amnesia Strikes, VLDB 2024
ALICE: Pillai et al., All File Systems Are Not Created Equal, OSDI 2014
save writes the whole book on every post: a million lines for one