Specifications. Announcements HW1 is due Friday To submit answers, commit Error in HW1: In problem 8, part 2 z = 0 should be z = MAX_INT 2.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
De necessariis pre condiciones consequentia sine machina P. Consobrinus, R. Consobrinus M. Aquilifer, F. Oratio.
Semantics Static semantics Dynamic semantics attribute grammars
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Identity and Equality Based on material by Michael Ernst, University of Washington.
Reasoning About Code; Hoare Logic, continued
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.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 2 – Reasoning About Code With Logic 1CSE 331 Winter 2014.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
Reading and Writing Mathematical Proofs
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Specifications (Slides by Mike Ernst) 1.
Reasoning about programs March CSE 403, Winter 2011, Brun.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
COP4020 Programming Languages Introduction to Axiomatic Semantics Prof. Robert van Engelen.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 4 - Specifications (Based on slides by Mike Ernst, Hal Perkins)
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
1 CSE 331 Specifications slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Slides by Alex Mariakakis Section 5: Midterm Review.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Modular Decomposition, Abstraction and Specifications
Reasoning about code CSE 331 University of Washington.
CSE 331 Software Design & Implementation
Reasoning About Code; Hoare Logic
Reasoning about Loops, Conclusion
Specifications, conclusion. Abstract Data Types (ADTs)
Exam 1 Review.
Programming Languages 2nd edition Tucker and Noonan
Specifications.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Hoare-style program verification
CSE 331 Software Design & Implementation
Java Reasoning About Code
CSE 331 Software Design & Implementation specifications
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Program correctness Axiomatic semantics
Software Specifications
Programming Languages 2nd edition Tucker and Noonan
Formal Methods Lecture 16 March 22, 2011 CS 315 Spring 2011
Presentation transcript:

Specifications

Announcements HW1 is due Friday To submit answers, commit Error in HW1: In problem 8, part 2 z = 0 should be z = MAX_INT 2

3 So Far We discussed reasoning about code Forward reasoning and backward reasoning Hoare Logic Hoare Triples Rule for assignment Rule for sequence Rule for if-then-else Rule for method call Reasoning about loops Spring 15 CSCI 2600, A Milanova

Reasoning About Loops total correctness=partial correctness+termination 1. Partial correctness “Guess”, then prove loop invariant Loop invariant and loop exit condition must imply the postcondition This gives us: “If the loop terminated then the postcondtion holds”. But does the loop terminate? 2. Termination (Informally) “Guess” decrementing function D. Each iteration decrements D, D at 0 along with the loop invariant must imply loop exit condition 4

Precondition: len ≥ 0 ∧ a.length = len double sum = 0.0; int i = 0; { Inv: sum = a[0]+…+a[i-1] ∧ i<=len } while (i < len) { sum = sum + a[i]; i = i+1; } Postcondition: sum = a[0]+…+a[a.length-1] 5 A “tedious” Invariant … i=2 Inv: sum = a[0]+a[1]

Precondition: len ≥ 0 ∧ a.length = len double sum = 0.0; int i = 0; { Inv: sum = a[0]+…+a[i-1] ∧ i<=len } while (i < len) { sum = sum + a[i]; i = i+1; } Postcondition: sum = a[0]+…+a[a.length-1] 6 A “tedious” Invariant … i=3 Inv: sum = a[0]+a[1]+a[2]

As the induction variable i moves through the array, sum contains the correct partial result. At the end of the array, sum contains the correct total Similar invariants for min, max, avg, sorting algorithms, etc. These invariants are easy to guess. Spring 15 CSCI 2600, A Milanova 7 A “tedious” Invariant … i=a.length Inv: sum = a[0]+a[1]+…+a[a.length-1]

Precondition: len ≥ 0 ∧ a.length = len double sum = 0.0; int i = 0; { Inv: sum = a[0]+…+a[i-1] ∧ i<=len+1 } while (i <= len) { sum = sum + a[i]; i = i+1; } Postcondition: sum = a[0]+…+a[a.length-1] Spring 15 CSCI 2600, A Milanova 8 Let’s Catch the Bug

An “elegant” Invariant 9 Precondition: x1 > 0 ∧ x2 > 0 y1 = x1; y2 = x2; { Inv: gcd(y1,y2) = gcd(x1,x2) } while (y1 != y2) { if (y1 > y2) { y1 = y1-y2 } else { y2 = y2-y1; } } Postcondition: y1 = gcd(x1,x2)

An “elegant” Invariant 10 Precondition: x != 0 zeros = 0; y = x; while (y%10 == 0) { y = y/10; zeros = zeros + 1; } Postcondition: x = y*10 zeros ∧ y%10 != 0 There is no good way of guessing such invariants… Spring 15 CSCI 2600, A Milanova (example due to Mike Ernst)

An “elegant” Invariant 11 Precondition: x != 0 zeros = 0; y = x; while (y%10 == 0) { y = y/10; zeros = zeros + 1; } Postcondition: x = y*10 zeros ∧ y%10 != 0

Outline of Today’s Lecture Specifications Benefits of specifications Specification conventions Javadoc PoS specifications JML 12

Specifications A specification consists of a precondition and a postcondition Precondition: conditions that hold before method executes Postcondition: conditions that hold after method finished execution (if precondition held!) 13

Specifications A specification is a contract between a method and its caller Obligations of the method (implementation of the specification): agrees to provide postcondition if precondition held Obligations of the caller (user of specification): agrees to meet the precondition and not expect more than the promised postcondition A specification is an abstraction 14

Example Specification Precondition: len ≥ 0 ∧ a.length = len Postcondition: result = a[0]+…+a[a.length-1] double sum(int[] a, int len) { double sum = 0.0; int i = 0; while (i < len) { sum = sum + a[i]; i = i+1; } return sum; } Spring 15 CSCI 2600, A Milanova 15 For our purposes, we will be writing specifications that are a bit less formal than this example. Mathematical rigor is welcome, but not always necessary.

Benefits of Specifications Precisely documents method behavior Imagine if you had to read the code of the Java libraries to figure what they do! Promotes modularity Modularity is key to the development of correct and maintainable software Specifications help organize code into small modules, with clear-cut boundaries between those modules Spring 15 CSCI 2600, A Milanova 16

Benefits of Specifications Promote modularity… Client relies on description in specification, no need to know the implementation Method must support specification, but its implementation is free otherwise! Method and client can be built simultaneously Enables reasoning about correctness “Does code do the right thing?” means “Does code conform to its specification?” Confirmed by testing and verification 17

So, Why Not Just Read Code? boolean sub(List src, List part) { int part_index = 0; for (Object o : src) { if (o.equals(part.get(part_index))) { part_index++; if (part_index == part.size()) { return true; } } else { part_index = 0; } return false; } Spring 15 CSCI 2600, A Milanova (example due to Michael Ernst, UW) 18

So, Why Not Just Read Code? Code is complicated Gives a lot more detail than client needs Understanding code is a burden Code is ambiguous What is essential and what is incidental (e.g., result of an optimization) Client needs to know what the code does, not how it does it! Spring 15 CSCI 2600, A Milanova (based on slides by Michael Ernst, UW) 19

What About Comments? // method checks if part appears as // subsequence in src boolean sub(List src, List part) { … } Comments are important, but insufficient. They are informal, imprecise and often ambiguous Spring 15 CSCI 2600, A Milanova 20

Specifications Are Concise and Precise Unfortunately, lots of code lacks specification  Programmers guess what code does by reading the code or running it This results in bugs and/or complex code with unclear behavior Specification inference is an active area of research Spring 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 21

So, What’s in sub ’s Specification? boolean sub(List src, List part) { int part_index = 0; for (Object o : src) { if (o.equals(part.get(part_index))) { part_index++; if (part_index == part.size()) { return true; } } else { part_index = 0; } return false; } Spring 15 CSCI 2600, A Milanova (example due to Michael Ernst, UW) 22

Choice 1: // sub returns true if part is a subsequence of src ; it returns false otherwise. Choice 2: // src and part must be non-null // If src is the empty list, then sub returns false // If there is a partial match in the beginning, sub returns false, even if there is a full match later. E.g. sub ([1, 2, 1, 2, 1, 3], [1, 2, 1, 3]) is false. 23 So, What’s in sub ’s Specification?

What’s in sub ’s Specification? This complex specification is a red flag Rule: it is better to simplify design and code rather than try to describe complex behavior! If you end up writing a complicated specification, redesign and rewrite your code --- either something is wrong or code is extremely complex and hard to reason about (Mike Ernst calls this “a sneaky fringe benefit of specifications”) Spring 15 CSCI 2600, A Milanova (based on slides by Michael Ernst) 24

Goal of Principles of Software One goal of Principles of Software is to instill a discipline of writing concise and precise specifications Spring 15 CSCI 2600, A Milanova 25

Outline of Today’s Lecture Specifications Benefits of specifications Specification conventions Javadoc PoS specifications JML 26

Javadoc Javadoc convention Method’s type signature Text description of what method does Parameters: text description of what gets passed Return: text description of what gets returned Throws: list of exceptions that may get thrown Spring 15 CSCI 2600, A Milanova 27

Example: Javadoc for String.substring public String substring(int beginIndex) Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of the string Parameters: beginIndex --- the beginning index, inclusive. Returns: the specified substring. Throws: IndexOutOfBoundsException --- if beginIndex is negative or larger than the length of this String object. 28

PoS Specifications Specification convention due to Michael Ernst The precondition requires: clause spells out constraints on client The postcondition modifies: lists objects (typically parameters) that may be modified by the method. Any object not listed under this clause is guaranteed untouched throws: lists possible exceptions effects: describes final state of modified objects returns: describes return value 29

Example 0 String substring(int beginIndex) requires: none modifies: none effects: none returns: new string with same value as the substring beginning at beginIndex and extending until the end of current string throws: IndexOutOfBoundsException --- if beginIndex is negative or larger than the length of this String object. Spring 15 CSCI 2600, A Milanova 30

Example 1 static int change(List lst, T old, T newelt) requires: lst, old, newelt are non-null. old occurs in lst. modifies: lst effects: change first occurrence of old in lst with newelt. makes no other changes. returns: position of element in lst that was old and now newelt Spring 15 CSCI 2600, A Milanova (due to Michael Ernst) 31 static int change(List lst, T old, T newelt) { int i = 0; for (T curr : lst) { if (curr == old) { lst.set(i, newelt); return i; } i = i + 1; } return -1; }

Example 2 static List listAdd(List lst1, List lst2) requires: lst1, lst2 are non-null. lst1 and lst2 are same size. modifies: none effects: none returns: a new list of the same size, whose i-the element is the sum of the i-the elements of lst1 and lst2 Spring 15 CSCI 2600, A Milanova (due to Michael Ernst) 32 static List listAdd(List lst1, List lst2) { List res = new ArrayList (); for (int i = 0; i < lst1.size(); i++) res.add(lst1.get(i) + lst2.get(i)); return res; }

Aside: Autoboxing and Unboxing Boxed primitives. E.g., int vs. Integer Autoboxing: automatic conversion from primitive to boxed type Java generics require reference type arguments ArrayList al = new … for (int i=0; i<10; i++) al.add(i); Unboxing: automatic conversion from boxed type to primitive res.add(lst1.get(i) + lst2.get(i)) 33

Example 3 static void listAdd2(List lst1, List lst2) requires: ?? modifies: ?? effects: ?? returns: ?? Spring 15 CSCI 2600, A Milanova (due to Michael Ernst) 34 static void listAdd(List lst1, List lst2) { for (int i = 0; i < lst1.size(); i++) { lst1.set(i,lst1.get(i) + lst2.get(i)); }

Example 4 static void uniquefy(List lst) requires: ? modifies: ? effects: ? returns: ? Spring 15 CSCI 2600, A Milanova (due to Michael Ernst) 35 static void uniquefy(List lst) { for (int i = 0; i < lst.size()-1; i++) if (lst.get(i) == lst.get(i+1)) lst.remove(i); }

Example 5 private static void swap(int[] a, int i, int j) requires: ? modifies: ? effects: ? returns: ? Spring 15 CSCI 2600, A Milanova 36 static void swap(int[] a, int i, int j) { int tmp = a[j]; a[j] = a[i]; a[i] = tmp; }

Example 6 private static void selection(int[] a) requires: ? modifies: ? effects: ? returns: ? Spring 15 CSCI 2600, A Milanova 37 static void selection(int[] a) { for (int i = 0; i < a.length; i++) { int min = i; for (int j = i+1; j < a.length; j++) if (a[j] < a[min]) min = j; swap(a, i, min); }

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 38

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 39

Better binarySearch Specification 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 value otherwise Spring 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 40

Shall We Check requires Clause? If client (i.e., caller) fails to provide the preconditions, method can do anything --- throw an exception or pass bad value back It is polite to check Checking preconditions Makes an implementation more robust Provides feedback to the client Avoids silent errors Spring 15 CSCI 2600, A Milanova (based on slides by Michael Ernst) 41

Rule If private method, may not check If public method, do check unless such a check is expensive E.g., requires: lst is non-null. Check E.g., requires: lst contains positive integers. Don’t check Spring 15 CSCI 2600, A Milanova 42

Shall We Check requires Clause? Example 1: requires: lst, old, new are non-null. old occurs in lst. Check? Example 2: requires: lst1, lst2 are non- null. lst1 and lst2 are same size. Check? binarySearch: requires: a is sorted. Check? Spring 15 CSCI 2600, A Milanova 43

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? 44

The JML Spec for binarySearch Precondition: requires: a != null && (\forall int i; 0 < i && i < a.length; a[i-1] <= a[i]; Postcondition: ensures: // complex statement… Difficult problem and an active research area. Look up Sir Tony Hoare’s Grand Challenge 45