Reading Contracts 9 January 2019 OSU CSE 1 Recall: Mathematical - - PowerPoint PPT Presentation

reading contracts
SMART_READER_LITE
LIVE PREVIEW

Reading Contracts 9 January 2019 OSU CSE 1 Recall: Mathematical - - PowerPoint PPT Presentation

Reading Contracts 9 January 2019 OSU CSE 1 Recall: Mathematical Models Each variable in the program has a type Examples: int , double , Each program type has a mathematical type that models it: you should think of any variable


slide-1
SLIDE 1

Reading Contracts

9 January 2019 OSU CSE 1

slide-2
SLIDE 2

Recall: Mathematical Models

  • Each variable in the program has a type

– Examples: int, double, …

  • Each program type has a mathematical

type that models it: you should think of any variable of that program type as having a value from its mathematical model’s mathematical space/domain

– Examples (respectively): integer, real, …

slide-3
SLIDE 3

Mathematical Models

Program type Mathematical type boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits) String string of character

9 January 2019 OSU CSE 3

slide-4
SLIDE 4

More Mathematical Models

Program type Mathematical type NaturalNumber integer

(non-negative)

Queue<T> string of T Stack<T> string of T Sequence<T> string of T Set<T> finite set of T Map<K,V> finite set of (K,V)

(with function property)

9 January 2019 OSU CSE 4

slide-5
SLIDE 5

Recall: Method Contracts

  • Each method has:

– A precondition (requires clause) that characterizes the responsibility of the program that calls (uses) that method (client code) – A postcondition (ensures clause) that characterizes the responsibility of the program that implements that method (implementation code in the method body)

9 January 2019 OSU CSE 5

slide-6
SLIDE 6

Recall: Meaning of a Contract

  • If its precondition is true when a method is

called, then the method will terminate — return to the calling program — and the postcondition will be true when it does return

  • If its precondition is not true when a

method is called, then the method may do anything (including not terminate)

9 January 2019 OSU CSE 6

slide-7
SLIDE 7

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 7

slide-8
SLIDE 8

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 8

Note: Most of the Javadoc clutter is removed here; just contract information, not formatting commands, are shown!

slide-9
SLIDE 9

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 9

Stated in English, what does this method do?

slide-10
SLIDE 10

How Do You Approach This?

  • Recommendations:

– Know the meanings of all the individual symbols and terms appearing in the contract – Consider specific values for the parameters

  • Start with “smallest” legal input values as

examples

  • Continue with more examples
  • “See the pattern”

9 January 2019 OSU CSE 10

slide-11
SLIDE 11

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 11

What do these Javadoc tags mean?

slide-12
SLIDE 12

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 12

What does this mean?

slide-13
SLIDE 13

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 13

What does this mean?

slide-14
SLIDE 14

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 14

What does this mean?

slide-15
SLIDE 15

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 15

What does this mean?

slide-16
SLIDE 16

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 16

What does this mean?

slide-17
SLIDE 17

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 17

What does this mean?

slide-18
SLIDE 18

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 18

What does all this mean?

slide-19
SLIDE 19

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 19

The assertion that follows in parentheses here...

slide-20
SLIDE 20

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 20

... is true for every possible combination of values of i, j, a, and b...

slide-21
SLIDE 21

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 21

... at least, for every combination of values of i, j, a, and b satisfying the where clause.

slide-22
SLIDE 22

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 22

The assertion that follows is true for some possible combination of values of c and d.

slide-23
SLIDE 23

Universal Quantification

  • Universal quantification is used when

you want to say something is true for every combination of values that satisfy a certain property:

for all quantified-variables where (restriction-assertion) (main-assertion)

9 January 2019 OSU CSE 23

slide-24
SLIDE 24

Universal Quantification

  • Universal quantification is used when

you want to say something is true for every combination of values that satisfy a certain property:

for all quantified-variables where (restriction-assertion) (main-assertion)

9 January 2019 OSU CSE 24

slide-25
SLIDE 25

Example Use of for all

  • “Every non-empty string has positive

length.”

9 January 2019 OSU CSE 25

slide-26
SLIDE 26

Example Use of for all

  • “Every non-empty string has positive

length.”

for all s: string of T where (s /= < >) (|s| > 0)

9 January 2019 OSU CSE 26

slide-27
SLIDE 27

Existential Quantification

  • Existential quantification is used when

you want to say something is true for some combination of values:

there exists quantified-variables (assertion)

9 January 2019 OSU CSE 27

slide-28
SLIDE 28

Existential Quantification

  • Existential quantification is used when

you want to say something is true for some combination of values:

there exists quantified-variables (assertion)

9 January 2019 OSU CSE 28

slide-29
SLIDE 29

Example Use of there exists

  • “Some positive integer is odd.”

9 January 2019 OSU CSE 29

slide-30
SLIDE 30

Example Use of there exists

  • “Some positive integer is odd.”

there exists n, k: integer (n > 0 and n = 2 * k + 1)

9 January 2019 OSU CSE 30

slide-31
SLIDE 31

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 31

Back to the main example... What are some “smallest” possible input values for the method’s parameters s1 and s2?

slide-32
SLIDE 32

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |s1| - 1 and * for all i, j: integer, a, b: string of integer * where (s1 = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 32

Note that the incoming value of s2 does not matter because it is a replaces-mode parameter.

slide-33
SLIDE 33

A Smallest Value (for s1)

9 January 2019 OSU CSE 33

Code State

s1 = <7> s2 = <1, 2, 3> smooth(s1, s2); s1 = s2 =

slide-34
SLIDE 34

A Smallest Value (for s1)

9 January 2019 OSU CSE 34

Code State

s1 = <7> s2 = <1, 2, 3> smooth(s1, s2); s1 = s2 = How do you predict the values of s1 and s2?

slide-35
SLIDE 35

Example

/** * ... * @replaces s2 * @requires * |<7>| >= 1 * @ensures * |s2| = |<7>| - 1 and * for all i, j: integer, a, b: string of integer * where (<7> = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 35

We know s1 = <7> upon return, because s1 is a restores-mode parameter.

slide-36
SLIDE 36

Example

/** * ... * @replaces s2 * @requires * |<7>| >= 1 * @ensures * |s2| = |<7>| - 1 and * for all i, j: integer, a, b: string of integer * where (<7> = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 36

To determine whether the value of s1 is a “legal” input, substitute s1 = <7> in the requires clause.

slide-37
SLIDE 37

Example

/** * ... * @replaces s2 * @requires * |<7>| >= 1 * @ensures * |s2| = |<7>| - 1 and * for all i, j: integer, a, b: string of integer * where (<7> = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 37

To predict the value of s2, substitute s1 = <7> in the ensures clause.

slide-38
SLIDE 38

Example

/** * ... * @replaces s2 * @requires * |s1| >= 1 * @ensures * |s2| = |<7>| - 1 and * for all i, j: integer, a, b: string of integer * where (<7> = a * <i> * <j> * b) * (there exists c, d: string of integer * (|c| = |a| and s2 = c * <(i+j)/2> * d)) */ public static void smooth(Sequence<Integer> s1, Sequence<Integer> s2) {...}

9 January 2019 OSU CSE 38

We see that, in general, |s2| = |s1| - 1 so, in this case, |s2| = 0

slide-39
SLIDE 39

Vacuously True

  • To make the ensures clause true, the first

line implies we must have s2 = < >

  • But there are no values of i, j, a, and b

that make the where-clause true in:

for all i, j: integer, a, b: string of integer where (<7> = a * <i> * <j> * b) (...)

  • This universally quantified expression is

defined to be vacuously true

9 January 2019 OSU CSE 39

slide-40
SLIDE 40

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 40

Code State

s1 = <7, 23> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23> s2 =

slide-41
SLIDE 41

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 41

Code State

s1 = <7, 23> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23> s2 = How do you predict the value of s2?

slide-42
SLIDE 42

Values of Quantified Variables

9 January 2019 OSU CSE 42

s1 <7, 23> a < > i 7 j 23 b < > c < > d < > s2 <15>

slide-43
SLIDE 43

Values of Quantified Variables

9 January 2019 OSU CSE 43

s1 <7, 23> a < > i 7 j 23 b < > c < > d < > s2 <15> We see that, when s1 = <7, 23> we must have s2 = <15>

slide-44
SLIDE 44

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 44

Code State

s1 = <7, 23, 2> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23, 2> s2 =

slide-45
SLIDE 45

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 45

Code State

s1 = <7, 23, 2> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23, 2> s2 = How do you predict the value of s2?

slide-46
SLIDE 46

Values of Quantified Variables

9 January 2019 OSU CSE 46

s1 <7, 23, 2> a < > i 7 j 23 b <2> c < > d <?> s2 <15, ?>

slide-47
SLIDE 47

Values of Quantified Variables

9 January 2019 OSU CSE 47

s1 <7, 23, 2> a < > <7> i 7 23 j 23 2 b <2> < > c < > <?> d <?> < > s2 <15, ?> <?, 12>

slide-48
SLIDE 48

Values of Quantified Variables

9 January 2019 OSU CSE 48

s1 <7, 23, 2> a < > <7> i 7 23 j 23 2 b <2> < > c < > <?> d <?> < > s2 <15, ?> <?, 12> We see that, when s1 = <7, 23, 2> we must have s2 = <15, 12>

slide-49
SLIDE 49

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 49

Code State

s1 = <7, 23, 2, 6> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23, 2, 6> s2 =

slide-50
SLIDE 50

A Next Smallest Value (for s1)

9 January 2019 OSU CSE 50

Code State

s1 = <7, 23, 2, 6> s2 = <1, 2, 3> smooth(s1, s2); s1 = <7, 23, 2, 6> s2 = How do you predict the value of s2?

slide-51
SLIDE 51

Values of Quantified Variables

9 January 2019 OSU CSE 51

s1 <7, 23, 2, 6> a < > i 7 j 23 b <2, 6> c < > d <?, ?> s2 <15, ?, ?>

slide-52
SLIDE 52

Values of Quantified Variables

9 January 2019 OSU CSE 52

s1 <7, 23, 2, 6> a < > <7> i 7 23 j 23 2 b <2, 6> <6> c < > <?> d <?, ?> <?> s2 <15, ?, ?> <?, 12, ?>

slide-53
SLIDE 53

Values of Quantified Variables

9 January 2019 OSU CSE 53

s1 <7, 23, 2, 6> a < > <7> <7, 23> i 7 23 2 j 23 2 6 b <2, 6> <6> < > c < > <?> <?, ?> d <?, ?> <?> < > s2 <15, ?, ?> <?, 12, ?> <?, ?, 4>

slide-54
SLIDE 54

Values of Quantified Variables

9 January 2019 OSU CSE 54

s1 <7, 23, 2, 6> a < > <7> <7, 23> i 7 23 2 j 23 2 6 b <2, 6> <6> < > c < > <?> <?, ?> d <?, ?> <?> < > s2 <15, ?, ?> <?, 12, ?> <?, ?, 4> We see that, when s1 = <7, 23, 2, 6> we must have s2 = <15, 12, 4>

slide-55
SLIDE 55

Conclusion

  • So, stated in English, what does smooth

do?

  • Even when you can “see the pattern”, it’s

surprisingly difficult to phrase it in a simple way...

9 January 2019 OSU CSE 55