Specifications, conclusion. Abstract Data Types (ADTs)

Slides:



Advertisements
Similar presentations
Logical Abstract Interpretation Sumit Gulwani Microsoft Research, Redmond.
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Verification with Array Variables Book: Chapter 7.2.
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Karlstad University Computer Science Design Contracts and Error Management Design Contracts and Errors A Software Development Strategy Eivind J. Nordby.
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.
Partial correctness © Marcelo d’Amorim 2010.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
Testing, cont. Based on material by Michael Ernst, University of Washington.
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.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Specifications (Slides by Mike Ernst) 1.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Chapter 2 Logic 2.1 Statements 2.2 The Negation of a Statement 2.3 The Disjunction and Conjunction of Statements 2.4 The Implication 2.5 More on Implications.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 2 Formal Reasoning.
1 CSE 331 Specifications slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
Testing (final thoughts). equals() and hashCode() Important when using Hash-based containers class Duration { public final int min; public final int sec;
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
EECE 310: Software Engineering
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Subtype Polymorphism, Subtyping vs
Testing, conclusion Based on material by Michael Ernst, University of Washington.
Propositional Calculus: Boolean Algebra and Simplification
Representation Invariants and Abstraction Functions
Abstraction functions, Reasoning About ADTs
Elementary Metamathematics
Priority Queues.
Information Technology Department
Testing, cont., Equality and Identity
Reasoning About Code; Hoare Logic
Lecture 5 Floyd-Hoare Style Verification
Testing.
Reasoning about Loops, Conclusion
Exam 1 Review.
Specifications.
Equality, conclusion Based on material by Michael Ernst, University of Washington.
Subtype Polymorphism, Subtyping vs
Hoare-style program verification
CSE 331 Software Design & Implementation
Propositional Equivalences
Semantics In Text: Chapter 3.
Java Reasoning About Code
Reasoning About ADTs, Assertions and Exceptions
Announcements Quiz 6 HW7 due Tuesday, October 30
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Announcements Exam 2 on Friday, November 2nd Topics
CSCI N207 Data Analysis Using Spreadsheet
CSE 331 Software Design & Implementation specifications
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Program Verification with Hoare Logic
CS210- Lecture 3 Jun 6, 2005 Announcements
Programming Languages and Compilers (CS 421)
Software Specifications
Presentation transcript:

Specifications, conclusion. Abstract Data Types (ADTs) Based on notes by Michael Ernst, University of Washington

Announcements HW1 is due at 2pm HW2 will be posted tomorrow Currently grading HW0 and Quiz 1 Switching to Submitty Discussion Board Starting from HW2 discussion LMS will no longer be monitored for new messages but all existing content stays Dafny exercise 1 (optional, 2 pts extra towards HW1)

Dafny Exercise 1 Weakest precondition: ?? if (x < 0) { y = x * x; } else { y = x * 2; y = y * 5 / x - 7; if (y = 3) { y = (y * 2 + 10) / 2 - y y = y + 4 Postcondition: y < 15 Compute the weakest precondition Verify your program with Dafny Submit your Dafny code on Submitty, “Dafny exercise 1”. Deadline midnight Saturday.

So far Specifications Benefits of specifications Specification conventions Javadoc JML/Dafny PoS specifications

Outline Specifications, conclusion Specification style Specification strength Substitutability Comparing specifications, informally Comparing specifications with logical formulas Converting PoS specification into logical formulas

Comparison by Logical Formulas The following is a sufficient condition: If PB => PA and QA => QB then A is stronger than B PB => PA and QA => QB A is stronger than B Too strict a requirement E.g., we may have a case when QA => QB does not hold and yet A is stronger than B. Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Example: int find(int[] a, int val) Specification B: requires: a is non-null and val occurs in a [ PB ] returns: i such that a[i] = val [ QB ] Specification A: requires: a is non-null [ PA ] returns: i such that a[i] = val if value val occurs in a and -1 if value val does not occur in a [ QA ] Clearly, PB => PA. But QA, which states “val occurs in a => returns i such that a[i]=val AND val does not occur in a => returns -1“ does not imply QB!

Comparing by Logical Formulas (I satisfies specification A) is a logical formula: PA => QA (PA is the precondition of A, QA is the 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 is stronger than B iff (PA => QA) => (PB => QB) Recall from FoCS and/or Intro to Logic: p => q Ξ !p V q Comparison using logical formulas is precise but can be difficult to carry out. It is often difficult to express all preconditions and postconditions with precise logical formulas! Spring 18 CSCI 2600, K. Kuzmin, A Milanova

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 ∧ !QA ) ∨ (!PB ∨ QB) = [ due to !(p V q) = !p ∧ !q ] (!PB ∨ QB) ∨ (P A ∧ !QA ) = [ due to commutativity of ∨ ] (!PB ∨ QB ∨ PA) ∧ (!PB ∨ QB ∨ !QA) [ distributivity ] [ PB => (QB ∨ PA) ] ∧ [ ( PB ∧ QA ) => QB ] A is stronger than B if and only if PB => QB is true trivially or PB implies PA AND QA together with PB imply QB (i.e., for the inputs permitted by PB, QB holds) First term in conjunction states what’s required of PA in order for A to be stronger than B. Second term in conjunction states what’s required of QA in order for A to be stronger than B. Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Example: int find(int[] a, int val) Specification B: requires: a is non-null and val occurs in a [ PB ] returns: i such that a[i] = val [ QB ] Specification A: requires: a is non-null [ PA ] returns: i such that a[i] = val if val occurs in a and -1 if val does not occur in a [ QA ] PB => PA (PB includes PA and one more condition) Now, let’s show PB ∧ QA => QB. PB implies “val occurs in a”. QA, states “val occurs in a => returns i s.t. a[i]=val”. PB ∧ QA => “returns i s.t. a[i]=val”, precisely QB!

Example: int find(int[] a, int val) Specification B: requires: a is non-null and val occurs in a [ PB ] returns: i such that a[i] = val [ QB ] Specification A: requires: a is non-null [ PA ] returns: i such that a[i] = val if val occurs in a and -1 if val does not occur in a [ QA ] Intuition: QA, by itself, does not imply QB because A may return -1. But QA does imply QB for the inputs permitted by B. Thus, it’s still ok to substitute A for B.

Converting PoS Specs into Logical Formulas PoS specification has 5 clauses Precondition: requires: … Postcondition: modifies: … , effects: …, returns: …, throws: … To reason about PoS specs, we must first convert specs into P => Q logical formulas Step 1: absorbs returns and throws into effects Step 2: convers into logical formula Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Converting PoS Specs into Logical Formulas PoS specification requires: R modifies: M effects: E is equivalent to this logical formula R => ( E ∧ (nothing but M is modified) ) throws and returns are absorbed into effects E Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide modified from slides by Michael Ernst)

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 18 CSCI 2600, K. Kuzmin, A Milanova (slide modified from slides by Michael Ernst)

Convert Spec to Formula, step 1: absorb throws and returns into effects set from java.util.ArrayList<T> T set(int index, T element) requires: true modifies: this[index] effects: thispost[index] = element throws: IndexOutOfBoundsException if index < 0 || index ≥ size returns: thispre[index] Absorb effects, returns and throws into new effects: if index < 0 || index ≥ this.size then throw IndexOutOfBoundsException else thispost[index] = element and returns thispre[index] Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide modified from slides by Michael Ernst)

Convert Spec to Formula, step 2: Convert into Formula set from java.util.ArrayList<T> T set(int index, T element) requires: true modifies: this[index] effects: if index < 0 || index ≥ this.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 18 CSCI 2600, K. Kuzmin, A Milanova (slide modified from slides by Michael Ernst)

Exercise Convert PoS spec into logical formula public 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 effects: E: if key occurs in a then returns i such that a[i] = key else returns -1. a is sorted => (E & (foreach i, a_pre[i] = a_post[i])) Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Exercise Convert spec into logical formula static 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 lst2 returns: none effects Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Exercise Convert spec into logical formula 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 R => ( E ^ (foreach k != i,j apost[k] = apre[k]) ) static void swap(int[] a, int i, int j) { int tmp = a[j]; a[j] = a[i]; a[i] = tmp; } Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Specifications, review Benefits Conventions Javadocs, PoS specifications, JML Style Strength Comparing specifications By hand, by logical formulas Converting PoS specs into logical formulas Spring 18 CSCI 2600, K. Kuzmin, A Milanova