reading contracts
play

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


  1. Reading Contracts 9 January 2019 OSU CSE 1

  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 , …

  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

  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

  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

  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

  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

  8. Example Note: Most of the /** Javadoc clutter is * ... removed here; just * @replaces s2 * @requires contract information, not * |s1| >= 1 formatting commands, * @ensures are shown! * |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

  9. Example /** Stated in English, what * ... does this method do? * @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

  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

  11. Example /** What do these Javadoc * ... tags mean? * @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

  12. Example /** * ... What does this mean? * @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

  13. Example /** * ... What does this mean? * @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

  14. Example /** * ... What does this mean? * @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

  15. Example /** * ... What does this mean? * @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

  16. Example /** * ... What does this mean? * @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

  17. Example /** * ... What does this mean? * @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

  18. Example /** * ... What does all this mean? * @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

  19. Example /** The assertion that follows * ... * @replaces s2 in parentheses here... * @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

  20. Example /** ... is true for every possible * ... combination of values of i , * @replaces s2 j , a , and b ... * @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

  21. Example ... at least, for every /** combination of values of i , * ... * @replaces s2 j , a , and b satisfying the * @requires where clause. * |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

  22. Example The assertion that follows /** is true for some possible * ... * @replaces s2 combination of values of c * @requires and d . * |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

  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

  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

  25. Example Use of for all • “Every non-empty string has positive length.” 9 January 2019 OSU CSE 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend