James Course - Hoare Logic Flashcards

1
Q

What it means for a program to be wrong?

A

A program is wrong if the reasoning for why it should be correct is flawed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When we talk about programs, we speak at one of three levels. What are these three levels?

A

Level 1: Runtime. The runtime level deals with specific values and a specific environment from a single execution of the program. A lot of debugging is done at Level 1.

Level 2: Code. At the level of code, we think about what the current implementation could do when given arbitrary inputs and an arbitrary environment. Behaviors that cannot happen are not considered, even if it requires global reasoning to rule them out. A lot of implementation work is done at Level 2.

Level 3: Design/Logic: At the level of logic, we consider the abstract specification of each unit of a program. When using other units, we only consider the guarantees made by the spec, and assume they may be replaced at any time with a different implementation. Many programs which are correct when viewed at Level 2 are not correct when viewed at Level 3, because they rely on behavior which is not guaranteed to hold in all future versions. Most software design is done at Level 3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Isn’t our goal to deliver working software to customers, and so Level 2 correctness is all that’s important?

A

No, our goal is to be able to continue to deliver working software far into the future. Level 3 is all about how modular are the interactions between the components of your software, and that’s important if you care at all about having different versions of those components, like, say, if you wanted to rewrite one of them tomorrow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the lesson we can learn from the SimCity use after free error?

A

This is why it’s important to make your APIs conform as strictly to the spec as possible, at least in Debug mode.
This all could have been avoided if DOS’s free were to deliberately zero-out the memory.
Hyrum’s Law

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reminder: The most important parts of our craft deal not with code, but with the logic underneath

A

Software Design only exists at the level of logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List Level 2 elements

A

The object of study of Level 2 is the code. Any statement that can be phrased in terms of the code, but not a specific trace or state, is a Level 2 statement.

The corresponding manner of reasoning is first-order logic. This means we can write down formulas that say “for all inputs X, does this property hold?” or “does there exist an input Y, such that this function crashes?” What we can’t do is quantify over other functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List Level 3 elements

A

The object of study of Level 3 is the specification. There are many, many ways of writing specifications, but a popular one is the Hoare triple: preconditions and postconditions.

At Level 3, we can ask questions about all possible implementations of malloc, like “does this program have a memory error?”

The corresponding manner of reasoning is higher-order logic. Higher-order logic is like first-order logic, but we can now quantify over functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you retrieve design information from the code?

A

The information of a program’s design is largely not present in its code. There are many designs that correspond to the exact same code, and so recovering all the design information is actually impossible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why self documenting code is not possible?

A

Because the information of a program’s design is largely not present in its code. There are many designs that correspond to the exact same code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why TDD is not a substitute for design?

A

Test too much and be too specific. Is it part of the design that postProfileUpdateToServer must call appState.saveCurrentState twice, and it chooses to do so once by calling logger.logUpdateEvent?

The code should not follow the tests, nor the tests the code. They should both follow the design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why TDD is not a substitute for design?

A

Test too much and be too specific. Is it part of the design that postProfileUpdateToServer must call appState.saveCurrentState twice, and it chooses to do so once by calling logger.logUpdateEvent?

The code should not follow the tests, nor the tests the code. They should both follow the design.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of a documentation?

A

The purpose of documentation is not just to describe how the system works today, but also how it will work in the future and across many versions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the benefits of a spec in Joel eyes?

A

When you design your product in a human language, it only takes a few minutes to try thinking about several possibilities, revising, and improving your design.

Giant reason number two is to save time communicating. When you write a spec, you only have to communicate how the program is supposed to work once. Everybody on the team can just read the spec.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does “code knowledge” means

A

Code A knows about code B when B is referenced in the proof of correctness of A.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What smell do you identify when you search for some behavior very deep in the code?

A

When you have to read a ton of things to make sure a thing can happen or not you are probably searching for unstable guarantees, that is, implementation guarantees, instead of design guarantees. You shouldn’t have to dig deep, use the specification.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What new way of thinking about function calling Level 3 give us?

A

Don’t think about one function calling another. Think about one function calling all future versions of that function.

17
Q

What are assertions in Hoare Logic?

A

Logical facts that must be true at a certain point in the program. We typically write assertions in curly braces.

18
Q

How Hoare triples can reflect complexity in a program?

A

Even though each line is simple, the assertions grow in complexity, reflecting how the current state of the program grows in complexity.

19
Q

How Hoare tiples show when we can forget something?

A
{true}
x  =  1 0 ;
{x>1}
y  =  4 2 ;
{x>1 ,   y>1}
z  =  x  +  y ;
{z>1}

we can “forget” about x and y, and about the exact value of z.

20
Q

How many pieces are necessary in a Hoare triple?

A

We only need two, the third can be inferred.
Given a statement and a precondition we can find the strongest postcondition.
Given a statement and a postcondition we can find the weakest precondition.
So, for a given precondition, there is a canonical postcondition, and vice versa.
We can also mechanically transform the precondition and postcondition into code.

21
Q

A Hoare triple is composed of:

A

precondition, command, and postcondition
{P}S{Q}
A Hoare triple {P} S {Q} should be read “if P is true, then, after S executes, Q will be true”.

22
Q

What are inference rules?

A

An inference rule says what premises we need to deduce something.
If the premises of the rule hold, then one can deduce
that the conclusions hold.

23
Q

What is a derivation tree?

A

Is a sequence of applications of inference rules in a tree layout. The derivation tree is a proof for that program.

24
Q

What is the inference rule for assignment?

A

————————– assign

{[E/x]P} x := E {P}

25
Q

What is the sequence inference rule?

A

{P} S {Q} {Q} T {R}
—————————- sequence
{P} S ; T {R}

This inference rule show us how to append/sequence programs.

26
Q

What is the consequence inference rule?

A

P′ ⇒ P {P} S {Q} Q ⇒ Q′
————————————- consequence
{P′} S {Q′}

Consequence rule show to us how to strenghten preconditions or how to weaken postconditions.

When we express Hoare logic as assertions between each line of a program, the consequence rule lets us modify an assertion using purely logical reasoning,
without any intervening code.

27
Q

When a program has modularity?

A

Suppose a program consists of two subprograms, A followed by B. If the strongest postcondition of A is stronger than the weakest precondition of B, the program has modularity, because it is possible to change A without changing B.

28
Q

What is the while inference rule?

A

{P ∧ B} S {P}
————————————– while
{P} while B do S {¬B ∧ P}

P is the loop invariant

29
Q

What is the if inference rule?

A

{P} S {R} {Q} T {R}
———————————————————— if
{(B⇒P) ∧ (¬B⇒Q)} if B then S else T {R}

Note that the if rule doubles the size of the precondition, so that a chain of them can cause exponential blowup in complexity. This corresponds to how a chain of if statements can have exponentially many paths. Being able to forget which path was taken using the consequence rule is paramount to making programs with conditionals tractable to reason about.