Lecture 1: Contracts Flashcards

(28 cards)

1
Q

What are the four types of contracts?

A

precondition, postcondition, loop invariant, assertions

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

What are the objectives of this lesson? (3 main)

A

develop contracts to help you prove the safety and validity of your code; develop informal termination arguments for loops and recursion; specification vs implementation

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

What is the command to run the c0 interpreter with contracts?

A

coin -d filename.c0

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

When is the best place to tabulate values while tracing code? What is iteration 0?

A

right before the exit condition is tested; when we enter the loop for the first time

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

How do we prove the loop invariant?

A

By assuming that it holds before the loop is run, and showing that it holds after the loop is run

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

How do we prove the loop invariant? (two parts)

A

First, demonstrate that the invariant holds initially; second, assume that the invariant holds just before the exit condition is checked. We have to show it is true again when we reach the exit condition after one iteration of the loop

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

How do we show that a loop terminates

A

define some quantity that always gets strictly smaller during any arbitrary iteration of the loop, and can never become negative (or go below a certain value)

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

What do we need when something isn’t defined for a certain range of functions? (ex. x^(a negative irrational number)) How do you write the necessary contract?

A

a precondition; to require that the exponent cannot be negative: //@requires y>=0;

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

What is the syntax for preconditions? Where are they written? When are they checked?

A

//@requires; written right below the function name, above the body of the function; checked before the function runs (NOT linear in sequence)

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

What are the two parts/sides of a function contract?

A

The precondition that “requires” a certain type of input; the promise of a postcondition

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

How do you construct a post-condition if the language doesn’t have a built in function?

A

Make a “lemma” function of the simplest form, even if it is not computationally efficient. In the main function, promise that the post-condition will equal the result of this “lemma function”

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

What is the syntax for a postcondition?

A

//@ensures e; where e is a boolean expression

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

Where is the postcondition written relative to the function?

A

The postcondition comes under the function name, under the precondition (of the form //@requires) and right before the body of the function

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

What special variable can be used in the postcondition only that is the value returned by the function?

A

\result

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

What does the -d in compiling or calling the interpreter stand for?

A

dynamically checking

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

What does error: cannot assign to variable ‘x’ used in @ensures annotation come from?

A

We are changing the value of x in the body when the postcondition is counting on the initial value of x to remain unchanged; would violate the principle that we need only to look at the contract to decide whether or not to call the function, because reassigning x changes the postcondition

17
Q

Do all contracts need to be boolean statements?

18
Q

When is @requires checked?

A

A precondition, checked just before the function body executes

19
Q

When is @ensures checked?

A

A postcondition, checked just after function body has been executed; can use \result to refer to the value returned by the function to impose a condition on it

20
Q

When is @loop_invariant checked?

A

checked every time just before the loop exit condition (or the loop guard) is checked

21
Q

When is @assert checked?

A

acts like a statement, checked everytime it is encountered

22
Q

For what two purposes are contracts important for?

A

Testing and Reasoning?

23
Q

How do contracts help with testing?

A

they represent a generic test of a function; contracts talk about expected values of arbitrary values. Drawback is that they are only useful if we have good test cases b/c contracts not executed with values that cause them to fail cannot cause execution to abort

24
Q

How does @requires contracts help with reasoning?

A

At the call sites, we have to prove that the precondition is satisfied for the given arguments, then assume if for reasoning in the body of the function

25
How does @ensures contracts help with reasoning?
At the return sites, we have to prove that postcondition is satisfied, so we can then assume it at the call site
26
How do @loop_invariants help with reasoning?
Allowed to assume that loop_invariant holds after the loop exits, together with the exit condition.
27
Hoe does @asserts contracts help with reasoning?
After showing assert is satisfied when it is reached during program execution; we can assume it for subsequent statements
28
How does @asserts contracts help with reasoning?
After showing assert is satisfied when it is reached during program execution; we can assume it for subsequent statements