CSE331�Lecture 2
Reasoning about Loops + HTML
Ardi Madadi �Summer 2021�(Based on slides by Kevin Zatloukal, Mike Ernst, Dan Grossman, and many others)
1
Hoare Logic
A Hoare triple is two assertions and one piece of code:
{{P}} S {{Q}}
2
Hoare Logic
A Hoare triple {{ P }} S {{ Q }} is called valid if:
3
Reasoning Forward & Backward
A Hoare triple {{ P }} S {{ Q }} is called valid if:
4
Reasoning: Assignments
x = expr
Forward
Backward
5
Reasoning: Assignments
Forward reasoning
{{ P }}
if (cond)
{{ P and cond }}
S1
{{ P1 }}
else
{{ P and not cond }}
S2
{{ P2 }}
{{ P1 or P2 }}
Backward reasoning
{{ cond and Q1 or
not cond and Q2 }}
if (cond)
{{ Q1 }}
S1
{{ Q }}
else
{{ Q2 }}
S2
{{ Q }}
{{ Q }}
6
Validity with Fwd & Back Reasoning
Reasoning in either direction gives valid assertions
Just need to check adjacent assertions:
{{ P }}
S1
S2
{{ P1 }}
{{ Q }}
{{ P }}
{{ Q1 }}
S1
S2
{{ Q }}
{{ P }}
S1
{{ P1 }}
{{ Q1 }}
S2
{{ Q }}
Reasoning So Far
8
Reasoning About Loops
9
Loop Invariant
A loop invariant is an assertion that holds at the top of the loop:
{{ Inv: I }}
while (cond)
S
Notation: I’ll use “Inv:” to indicate a loop invariant.
10
Loop Invariant
A loop invariant is an assertion that holds at the top of the loop:
{{ Inv: I }}
while (cond)
S
Notation: I’ll use “Inv:” to indicate a loop invariant.
11
Lupin Variants
Loop Invariant
Consider a while-loop (other loop forms not too different)�with a loop invariant I. Let’s try forward reasoning...
12
{{ P }}
S1
{{P1}}
{{ Inv: I }}
while (cond)
{{ Inv: I }}
S2 {{P2}}
{{ I and not cond }}
S3
{{P3}}
{{ Q }}
Loop Invariant
Consider a while-loop (other loop forms not too different)�with a loop invariant I. Let’s try forward reasoning...
Need to check that P1 implies I
(i.e., that I is true the first time)
13
{{ P }}
S1
{{P1}}
{{ Inv: I }}
while (cond)
{{ Inv: I }}
S2 {{P2}}
{{ I and not cond }}
S3
{{P3}}
{{ Q }}
Loop Invariant
Consider a while-loop (other loop forms not too different)�with a loop invariant I. Let’s try forward reasoning...
Need to check that P2 implies I again
(i.e., that I is true the each time around)
14
{{ P }}
S1
{{P1}}
{{ Inv: I }}
while (cond)
{{ I and cond }}
S2 {{P2}}
{{ I and not cond }}
S3
{{P3}}
{{ Q }}
Loop Invariant
Consider a while-loop (other loop forms not too different)�with a loop invariant I. Let’s try forward reasoning...
Need to check that P3 implies Q
(i.e., Q holds after the loop)
15
{{ P }}
S1
{{P1}}
{{ Inv: I }}
while (cond)
{{ I and cond }}
S2 {{P2}}
{{ I and not cond }}
S3
{{P3}}
{{ Q }}
Loop Invariant
Consider a while-loop (other loop forms not too different)�with a loop invariant I. Let’s try forward reasoning...
Informally, we need:
Formally, we need validity of:
(can check these with backward reasoning instead)
16
{{ P }}
S1
{{P1}}
{{ Inv: I }}
while (cond)
{{ I and cond }}
S2 {{P2}}
{{ I and not cond }}
S3
{{P3}}
{{ Q }}
More on Loop Invariants
17
More on Loop Invariants
18
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
19
{{ }}
s = 0;
i = 0;
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
Equivalent to this “for” loop:
s = 0;
for (int i = 0; i != n; i++) {
s = s + b[i];
}
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
20
{{ }}
s = 0;
i = 0;
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
21
{{ }}
s = 0;
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
22
{{ }}
s = 0;
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
(s = 0 and i = 0) implies
s = b[0] + … + b[i-1] ?
Yes. (An empty sum is zero.)
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
23
{{ }}
s = 0;
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
24
{{ }}
s = 0;
i = 0;
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {� {{ s = b[0] + ... + b[i-1] and i != n }}
s = s + b[i];
i = i + 1;
{{ s = b[0] + ... + b[i-1] }}
}
{{ s = b[0] + ... + b[n-1] }}
{{ s + b[i] = b[0] + … + b[i] }}
{{ s = b[0] + … + b[i] }}
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
25
{{ }}
s = 0;
i = 0;
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
s = b[0] + … + b[n-1] ?
Example: sum of array
Consider the following code to compute b[0] + … + b[n-1]:
26
{{ }}
s = 0;
i = 0;
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
s = s + b[i];
i = i + 1;
}
{{ s = b[0] + ... + b[n-1] }}
{{ I and i = n }} implies Q
These three checks verify
the postcondition holds
(i.e., the code is correct)
Termination
27
Example HW Problem
28
Example HW Problem
The following code to compute b[0] + … + b[n-1]:
{{ }}
s = 0;
{{ ____________ }}
i = 0;
{{ ____________ }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ ____________ }}
s = s + b[i];
{{ ____________ }}
i = i + 1;
{{ ____________ }}
}
{{ ____________ }}
{{ s = b[0] + ... + b[n-1] }}
29
Example HW Problem
The following code to compute b[0] + … + b[n-1]:
{{ }}
s = 0;
{{ s = 0 }}
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ s = b[0] + … + b[i-1] and i != n }}
s = s + b[i];
{{ s = b[0] + … + b[i-1] + b[i] and i != n }}
i = i + 1;
{{ s = b[0] + … + b[i-2] + b[i-1] and i-1 != n }}
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
30
But are we done and is this ok?
Example HW Problem
We start by filling in the blanks, but we still need to check for more.
{{ }}
s = 0;
{{ s = 0 }}
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ s = b[0] + … + b[i-1] and i != n }}
s = s + b[i];
{{ s = b[0] + … + b[i-1] + b[i] and i != n }}
i = i + 1;
{{ s = b[0] + … + b[i-2] + b[i-1] and i-1 != n }}
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
31
Example HW Problem
Does the invariant hold initially (before the loop starts)?
{{ }}
s = 0;
{{ s = 0 }}
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ s = b[0] + … + b[i-1] and i != n }}
s = s + b[i];
{{ s = b[0] + … + b[i-1] + b[i] and i != n }}
i = i + 1;
{{ s = b[0] + … + b[i-2] + b[i-1] and i-1 != n }}
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
32
Yes, i = 0 implies that s = (b[0] + … + b[-1]) = 0.
Example HW Problem
Does the postcondition hold after the loop terminates?
{{ }}
s = 0;
{{ s = 0 }}
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ s = b[0] + … + b[i-1] and i != n }}
s = s + b[i];
{{ s = b[0] + … + b[i-1] + b[i] and i != n }}
i = i + 1;
{{ s = b[0] + … + b[i-2] + b[i-1] and i-1 != n }}
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
33
Yes, not (i != n) implies i = n,
thus s = b[0] + ... + b[n-1].
Example HW Problem
Does the loop body preserve our invariant?
{{ }}
s = 0;
{{ s = 0 }}
i = 0;
{{ s = 0 and i = 0 }}
{{ Inv: s = b[0] + ... + b[i-1] }}
while (i != n) {
{{ s = b[0] + … + b[i-1] and i != n }}
s = s + b[i];
{{ s = b[0] + … + b[i-1] + b[i] and i != n }}
i = i + 1;
{{ s = b[0] + … + b[i-2] + b[i-1] and i-1 != n }}
}
{{ s = b[0] + ... + b[i-1] and not (i != n) }}
{{ s = b[0] + ... + b[n-1] }}
34
Yes, weaken by dropping i-1 != n.
Yes, equivalent to (Inv and loop condition).
HTML
35
HTML, Formally
36
Anatomy of a Tag
Element�
<p> Some Text </p>
Tag Name Content Closing Tag
37
Anatomy of a Tag
Element�
<p id=”firstParagraph”> Some Text </p>
Tag Name Content Closing Tag
Attribute Name Attribute Value
38
Tags form a Tree
<div>
<p id=”firstParagraph”> Some Text </p>
<br>
<div>
<p>Hello</p>
</div>
</div>
This tree, as it lives in the browser, is often called the "DOM"
– Document Object Model
39
Tags form a Tree
<div>
<p id=”firstParagraph”> Some Text </p>
<br>
<div>
<p><a href=”mypage.html”>Go to my page</a></p>
</div>
</div>
This tree, as it lives in the browser, is often called the "DOM"
– Document Object Model
40
div
p
br
div
p
a
Tags form a Tree
a basic HTML document.
41