specifications, continued. announcements hw1 was due today at 2 hw2 will be out tonight no class on...

45
Specifications, continued

Upload: bruno-mervin-craig

Post on 17-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specifications, continued

Page 2: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Announcements

HW1 was due today at 2

HW2 will be out tonight

No class on Tuesday, class next Friday

Quiz 2 today

Spring 15 CSCI 2600, A Milanova 2

Page 3: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

So Far

Specifications Benefits of specifications Specification conventions

Javadoc JML PoS specifications

3

Page 4: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specifications

Abstraction

Modularity A client module can be developed in parallel with

a server module

Enable reasoning about correctness

Spring 15 CSCI 2600, A Milanova 4

Page 5: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Javadoc for java.util.binarysearch

public static int binarySearch(int[] a,int key)

Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:

a - the array to be searched.

key - the value to be searched for.

Spring 15 CSCI 2600, A Milanova 5

Page 6: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Javadoc for java.util.binarysearch

Returns:

index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted

into the array; the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

So, what is wrong with this spec?

Spring 15 CSCI 2600, A Milanova 6

Page 7: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

The JML Convention

Javadocs and PoS specifications are not machine-checkable

JML (Java Modeling Language) is a formal specification language Builds on the ideas of Hoare logic End goal is automatic verification

Does implementation obey contract? Given a spec S and implementation I, does I conform to S?

Does client obey contract? Does it meet preconditions? Does it expect what method provides?

7

Page 8: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

The JML Spec for binarySearch

Precondition:

requires: a != null

&& (\forall int i;

0 < i && i < a.length;

a[i-1] <= a[i];

Postcondition:

ensures: // long logical formula…Difficult problem and an active research area

8

Page 9: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

PoS Specifications

PoS specifications, due to Mike Ernst, are readable and rigorous

Readability ensures that the spec is a helpful abstraction

Rigor facilitates reasoning Apply reasoning to prove correctness Compare specifications (more on this later today)

Spring 15 CSCI 2600, A Milanova 9

Page 10: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

PoS Specification for binarySearch

public static int binarySearch(int[] a,int key)

Precondition:

requires: a is sorted in ascending order

Postcondition:

modifies: none

effects: none

returns:

i such that a[i] = key if such an i exists

a negative integer otherwise

throws: none

Spring 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 10

Page 11: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Aside: Specifications and Dynamic Languages

In statically-typed languages (C/C++, Java) type signature is a form of specification:List<Integer> add(List<Integer> l1, List<Integer> l2)

In dynamically-typed languages (Python, JavaScript), there is no type signature!

def add(l1,l2): i = 0 res = [] for j in l1: res.append(j+l2[i]); i=i+1 return res

11

Page 12: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specifications are imperative. Even more so for dynamically-typed languages! Why?

def add(l1,l2): i = 0 res = [] for j in l1: res.append(j+l2[i]); i=i+1 return res

Start spec with type signature. Then fill in the rest of the clauses. Write spec before code!

Fall 15 CSCI 2600, A Milanova 12

Aside: Specifications and Dynamic Languages

Page 13: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specification for addLists

Spring 15 CSCI 2600, A Milanova 13

List<Number> add(List<Number> l1, List<Number> l2)

requires: ? modifies: ? effects: ? returns: ?

def add(l1, l2): i = 0 res = [] for j in l1: res.append(j+l2[i]); i=i+1 return res}

Page 14: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Outline

Specifications Specification Style Specification Strength Substitutability

Comparing specifications By hand By logical formulas

Spring 15 CSCI 2600, A Milanova 14

Page 15: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specification Style

A method is called for its side effects (effects clause) or its return value (returns clause) It is bad style to have both effects and return There are exceptions. Can you think of one?

E.g., HashMap.put returns the previous value E.g., Box.add returns true if successfully added

Main point of spec is to be helpful Being overly formal does not help Being too informal does not help either

Spring 15 CSCI 2600, A Milanova (slide based on slides by Michael Ernst) 15

Page 16: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specification Style A specification should be

Concise: not too many cases Informative and precise Specific (strong) enough: to make guarantees General (weak) enough: to permit (efficient)

implementation Too weak a spec imposes too many preconditions and/or

gives too few guarantees Too strong a spec imposes too few preconditions and/or

gives too many guarantees. Burden on imple-mentation (e.g., is input sorted?); may hinder efficiency

Spring 15 CSCI 2600, A Milanova (slide based on slides by Michael Ernst) 16

Page 17: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specification Strength

Sometimes, we need to compare specifications (we’ll see why a little later)

“A is stronger than B” (i.e. A => B) means For every implementation I

“I satisfies A” implies “I satisfies B” The opposite is not necessarily true

For every client C “C meets the obligations of B” implies “C meets the

obligations of A” The opposite is not necessarily true

17

Page 18: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Which One is Stronger?

int find(int[] a, int value) { for (int i=0; i<a.length; i++) { if (a[i] == value) return i; } return -1;}Specification A:

requires: a is non-null and value occurs in a returns: the smallest index i such that a[i] = value

Specification B:requires: a is non-null and value occurs in a returns: i such that a[i] = value

18Spring 15 CSCI 2600, A Milanova (example modified from Michael Ernst)

Page 19: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Which One is Stronger?

int find(int[] a, int value) { for (int i=0; i<a.length; i++) { if (a[i] == value) return i; } return -1;}Specification A:

requires: a is non-null and value occurs in a returns: i such that a[i] = value

Specification B:requires: a is non-null returns: i such that a[i] = value or i = -1 if value is not in a

Spring 15 CSCI 2600, A Milanova (example modified from Michael Ernst) 19

Page 20: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Which One is Stronger?

String substring(int beginIndex)

Specification A:requires: 0 <= beginIndex < length of this String objectreturns: new string with same value as the substring beginning at beginIndex and extending until the end of the current string

Specification B:requires: nothingreturns: new string with same value as the substring beginning at beginIndex and extending until the end of the current stringthrows: IndexOutOfBoundsException --- if beginIndex is negative or ≥ length of this String object

20

Page 21: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Why Care About Specification Strength?

Because of substitutability!

Principle of substitutability A stronger specification can always be

substituted for a weaker one I.e., an implementation that conforms to a

stronger specification can be used in a client that expects a weaker specification

Spring 15 CSCI 2600, A Milanova 21

Page 22: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Substitutability

Substitutability ensures correct hierarchies Client code: X x; … x.foo(index);

Client is “polymorphic”: written against X, but it is expected to work with any subclass of X

A subclass of X, say Y, may have its own implementation of foo, Y.foo(int). Client must work correctly with Y.foo(int)too!

If the spec of Y.foo(int) is stronger than the spec of X.foo(int) then we can safely substitute Y.foo(int) for X.foo(int)!

22

Page 23: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Substitutability

23

Client has contract with X:

// meets preconditiony = x.foo(0);

// expects non-zero:z = w/y;

Client has contract with X:

// meets preconditiony = x.foo(0);

// expects non-zero:z = w/y;

Class X

requires: index >= 0returns: result > 0

int foo(int index)

Class X

requires: index >= 0returns: result > 0

int foo(int index)

Class Y

requires: index >= 1returns: result >= 0

int foo(int index)

Class Y

requires: index >= 1returns: result >= 0

int foo(int index)

BAD! Y surprises the client!The principle of substitutability tells usthat if the specification of Y.foo is strongerthan the specification of X.foo, then it willbe ok to use Y.foo!

Page 24: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Strengthening and Weakening Specification

Strengthen a specification Require less of client: fewer/weaker conditions in

requires clause AND/OR Promise more to client: effects, modifies, returns

Effects/modifies affect fewer objects

Weaken a specification Require more of client: more/stronger conditions to

requires AND/OR Promise less to client: effects, modifies, returns

clauses are weaker, easier to write into code

24

Page 25: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Ease of Use by Client; Ease of Implementation

A stronger specification is easier to use Client has fewer preconditions to meet Client gets more guarantees in postconditions But a stronger spec is harder to implement!

Weaker specification is easier to implement Larger set of preconditions, relieves implementation

from the burden of handling different cases Easier to guarantee less in postcondition But weaker spec is harder to use

25

Page 26: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Specification Strength

Let specification A consist of precondition PA and postcondition QA

Let specification B consist of precondition PB and postcondition QB

A is stronger than B if (but not only if!) PB is stronger than PA (this means, stronger

specifications require less) QA is stronger than QB (stronger specifications

promise more)

26

Page 27: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Exercise: Order by Strength

Spec A: requires: a non-negative int argument

returns: an int in [1..10]

Spec B: requires: int argument

returns: an int in [2..5]

Spec C: requires: true // the weakest condition

returns: an int in [2..5]

Spec D: requires: an int in [1..10]

returns: an int in [1..20] Spring 15 CSCI 2600, A Milanova 27

Page 28: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Group Exercise

Spec A: “returns: an integer ≥ its argument”

Spec B: “returns: a non-negative integer ≥ its argument”

Spec C: “returns: argument + 1”

Spec D: “returns: argument2 “

Spec E: “returns: Integer.MAX_VALUE”

Implementations:

Code 1: return arg*2;

Code 2: return abs(arg);

Code 3: return arg+5;

Code 4: return arg*arg;

Code 5: return Integer.MAX_VALUE;

Spring 15 CSCI 2600, A Milanova (due to Michael Ernst) 28

A B C

D E

1

2

3

4

5

Page 29: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Review

“A is stronger than B” means For every implementation I

“I satisfies A” implies “I satisfies B” The opposite is not necessarily true!

A larger world of implementations satisfy the weaker spec B than the stronger spec A

Consequently, a it is easier to implement a weaker spec! Weaker specs require more AND/OR Weaker specs guarantee (promise) less

29

Page 30: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Outline

Specifications Specification Style Specification Strength Substitutability

Comparing specifications By hand By logical formulas

Spring 15 CSCI 2600, A Milanova 30

Page 31: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Comparing Specifications

By hand; examine each clause Logical formulas representing the spec Use whichever is most convenient

Comparing specs enables reasoning about class hierarchies. Make Y subclass of X only if Y’s methods have stronger specs than X’s

Spring 15 CSCI 2600, A Milanova (slides based on Michael Ernst) 31

Page 32: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Comparing by Hand

requires clause Stronger spec has fewer conditions in requires

modifies clause Stronger spec modifies fewer objects. Stronger

spec guarantees more objects stay unmodified! effects, returns and throws clauses

Stronger spec guarantees more in effects, returns and throws clauses. They are harder to implement, but easier to use by client

Spring 15 CSCI 2600, A Milanova (due to Mike Ernst) 32

Page 33: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Exercise

Specification A:requires: a is non-null and value occurs in a modifies: noneeffects: nonereturns: the smallest index i such that a[i] = value

Specification B:requires: a is non-null and value occurs in a // same as Amodifies: none // same as Aeffects: none // same as Areturns: i such that a[i] = value // fewer guarantees

Therefore, A is stronger. In fact, A’s postcondition implies B’s postcondition

33

Page 34: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Example

Specification A:requires: a is non-null and value occurs in a modifies: noneeffects: nonereturns: i such that a[i] = value

Specification B:requires: a is non-null // fewer conditions!modifies: none // sameeffects: none // samereturns: i such that a[i] = value if value occurs in a and i = -1 if value is not in a // guarantees more!

Therefore, B is stronger!Spring 15 CSCI 2600, A Milanova 34

Page 35: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Comparing by Logical Formulas

Specification A is a logical formula: PA => QA

(meaning, precondition of A implies postcondition of A)

Spec A is stronger than spec B if and only if for each implementation I, (I satisfies A)=>(I satisfies B)which is equivalent to A => B

A => B means (PA => QA) => (PB => QB)

Recall from FoCS and/or Intro to Logic: p => q Ξ !p V q

Spring 15 CSCI 2600, A Milanova 35

Page 36: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Comparing by Logical Formulas

(PA => QA) => (PB => QB) =

!(PA => QA) ∨ (PB => QB) = [ due to law p => q = !p∨q ]

!(!PA ∨ QA) ∨ (!PB ∨ QB) = [ due to p => q = !p ∨ q ]

(P A !Q∧ A ) ∨ (!PB ∨ QB) = [ due to !(p V q) = !p !q ]∧

(!PB ∨ QB) ∨ (P A !Q∧ A ) = [ due to commutativity of ∨ ]

(!PB ∨ QB ∨ PA) (!P∧ B ∨ QB ∨ !QA) [ distributivity ]

[ PB => (QB ∨ PA) ] [ ( P∧ B Q∧ A ) => QB ] Translation: A is stronger than B if and only ifPB implies QB or PA AND QA together with PB implies QB

Spring 15 CSCI 2600, A Milanova 36

Page 37: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Example

Specification B:requires: a is non-null and value occurs in a [ PB ]returns: i such that a[i] = value [ QB ]

Specification A:requires: a is non-null [ PA ]returns: i such that a[i] = value if value occurs in a and i = -1 if value is not in a [ QA ]

Clearly, PB => PA (PB includes PA and one more condition)Also, PB ∧ QA => QB. PB says that “value occurs in a” andQA, says “value occurs in a => returns i such that a[i]=value”. Thus, “returns i such that a[i]=value”, which is exactly QB, holds.

37

Page 38: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Comparison by Logical Formulas

We often use this stronger (but simpler) test:

If PB => PA and QA => QB then A is stronger than B

Spring 15 CSCI 2600, A Milanova 38

Page 39: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Converting PoS Specs into Logical Formulas

PoS specification

requires: R

modifies: M

effects: Eis equivalent to this logical formula

R => ( E ∧ (nothing but M is modified) )

throws and returns are absorbed into effects E

Spring 15 CSCI 2600, A Milanova (slide modified from slides by Michael Ernst) 39

Page 40: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Convert Spec to Formula, step 1: absorb throws and returns into effects

PoS specification convention

requires: (unchanged)

modifies: (unchanged)

effects:

returns: absorbed into “effects”

throws:

Spring 15 CSCI 2600, A Milanova (slide modified from slides by Michael Ernst) 40

Page 41: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

set from java.util.ArrayList<T>

T set(int index, T element)requires: truemodifies: this[index]effects: thispost[index] = elementthrows: IndexOutOfBoundsException if index < 0 || index ≥ sizereturns: thispre[index]

Absorb effects, returns and throws into new effects:if index < 0 || index ≥ size then

throws IndexOutOfBoundsException else thispost[index] = element and returns thispre[index]

Spring 15 CSCI 2600, A Milanova (slide modified from slides by Michael Ernst) 41

Convert Spec to Formula, step 1: absorb throws and returns into effects

Page 42: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

set from java.util.ArrayList<T>

T set(int index, T element)requires: truemodifies: this[index]effects: if index < 0 || index ≥ size then throws IndexOutOfBoundsException else thispost[index] = element and returns thispre[index]

Denote effects expression by E. Resulting formula is:

true => (E ∧ (foreach i ≠ index, thispost[i] = thispre[i]))

Spring 15 CSCI 2600, A Milanova (slide modified from slides by Michael Ernst) 42

Convert Spec to Formula, step 2: Convert into Formula

Page 43: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Exercise

Convert PoS spec into logical formulapublic static int binarySearch(int[] a,int key)

requires: a is sorted in ascending order

modifies: none

effects: none

returns: i such that a[i] = key if such an i exists; -1 otherwise

Spring 15 CSCI 2600, A Milanova 43

Page 44: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Exercisestatic void listAdd2(List<Integer> lst1, List<Integer> lst2) requires: lst1, lst2 are non-null. lst1 and lst2 are same size. modifies: lst1 effects: i-th element of lst1 is replaced with the sum of i-th elements of lst1 and lst1 returns: none

Spring 15 CSCI 2600, A Milanova 44

Page 45: Specifications, continued. Announcements HW1 was due today at 2 HW2 will be out tonight No class on Tuesday, class next Friday Quiz 2 today Spring 15

Exercise

private static void swap(int[] a, int i, int j)

requires: a non-null, 0<=i,j<a.length modifies: a[i] and a[j] effects: apost[i]=apre[j] and apost[j]=apre[i] returns: none

Spring 15 CSCI 2600, A Milanova 45

static void swap(int[] a, int i, int j) { int tmp = a[j];

a[j] = a[i]; a[i] = tmp;}