Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

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.
Dana Nau: Lecture slides for Automated Planning Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License:
Reasoning About Code; Hoare Logic, continued
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Section 1.6: Sets Sets are the most basic of discrete structures and also the most general. Several of the discrete structures we will study are built.
(c) 2007 Mauro Pezzè & Michal Young Ch 7, slide 1 Symbolic Execution and Proof of Properties.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Program Proving Notes Ellen L. Walker.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
Compiler Construction
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
ESC Java. Static Analysis Spectrum Power Cost Type checking Data-flow analysis Model checking Program verification AutomatedManual ESC.
Specifications Liskov Chapter 9 SWE 619 Last Updated Fall 2008.
Component-Based Software Engineering Components and Interfaces Paul Krause.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Introduction to Proofs
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
CS 173, Lecture B August 27, 2015 Tandy Warnow. Proofs You want to prove that some statement A is true. You can try to prove it directly, or you can prove.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Course: Software Engineering ©Alessandra RussoUnit 2: States and Operations, slide number 1 States and Operations This unit aims to:  Define: State schemas.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Reasoning about programs March CSE 403, Winter 2011, Brun.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
Understanding ADTs CSE 331 University of Washington.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
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.
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
1 Temporal logic. 2 Prop. logic: model and reason about static situations. Example: Are there truth values that can be assigned to x,y simultaneously.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Lecture 4 - Specifications (Based on slides by Mike Ernst, Hal Perkins)
Copyright © Zeph Grunschlag, Induction Zeph Grunschlag.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
1 CSE 331 Specifications slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS 173, Lecture B August 27, 2015 Tandy Warnow. Proofs You want to prove that some statement A is true. You can try to prove it directly, or you can prove.
Section 1.7. Section Summary Mathematical Proofs Forms of Theorems Direct Proofs Indirect Proofs Proof of the Contrapositive Proof by Contradiction.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
EECE 310: Software Engineering
Testing UW CSE 160 Winter 2017.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Specifications Liskov Chapter 9
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Design by Contract Fall 2016 Version.
Testing UW CSE 160 Winter 2016.
Specifications, conclusion. Abstract Data Types (ADTs)
Exam 1 Review.
slides created by Marty Stepp
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
slides adapted from Marty Stepp
Software Specifications
slides created by Marty Stepp
Presentation transcript:

Procedure specifications CSE 331

Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing via logical formulas - Comparing via transition relations Full transition relations Abbreviated transition relations Specification style; checking preconditions

Satisfaction of a specification Let P be an implementation and S a specification P satisfies S iff - Every behavior of P is permitted by S - “The behavior of P is a subset of S” The statement “P is correct” is meaningless - Though often made! If P does not satisfy S, either (or both!) could be “wrong” - “One person’s feature is another person’s bug.” - It’s usually better to change the program than the spec

Why compare specifications? We wish to compare procedures to specifications - Does the procedure satisfy the specification? - Has the implementer succeeded? We wish to compare specifications to one another - Which specification (if either) is stronger? - A procedure satisfying a stronger specification can be used anywhere that a weaker specification is required

A specification denotes a set of procedures Some set of procedures satisfies a specification Suppose a procedure takes an integer as an argument Spec 1: “returns an integer ≥ its argument” Spec 2: “returns a non-negative integer ≥ its argument” Spec 3: “returns argument + 1” Spec 4: “returns argument 2 ” Spec 5: “returns Integer.MAX_VALUE” Consider these 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; Spec1 Spec2 Spec3 Spec4 Spec5

Specification strength and substitutability A stronger specification promises more - It constrains the implementation more - The client can make more assumptions Substitutability - A stronger specification can always be substituted for a weaker one

Procedure specifications Example of a procedure specification: // requires i > 0 // modifies nothing // returns true iff i is a prime number public static boolean isPrime (int i) General form of a procedure specification: // requires // modifies // throws // effects // returns

How to compare specifications Three ways to compare 1. By hand; examine each clause 2. Logical formulas representing the specification 3. Transition relations a) Full transition relations b) Abbreviated transition relations Use whichever is most convenient

Technique 1: Comparing by hand We can weaken a specification by Making requires harder to satisfy (strengthening requires) Preconditions are contravariant (all other clauses are covariant) Adding things to modifies clause (weakening modifies) Making effects easier to satisfy (weakening effects) Guaranteeing less about throws (weakening throws) Guaranteeing less about returns value (weakening returns) The strongest (most constraining) spec has the following: requires clause: true modifies clause: nothing effects clause: false throws clause: nothing returns clause: false (This particular spec is so strong as to be useless.)

Technique 2: Comparing logical formulas Specification S1 is stronger than S2 iff: ∀ P, (P satisfies S1) ⇒ (P satisfies S2) If each specification is a logical formula, this is equivalent to: S1 ⇒ S2 So, convert each spec to a formula (in 2 steps, see following slides) This specification: // requires R // modifies M // effects E is equivalent to this single logical formula: R ⇒ (E ∧ (nothing but Mis modified)) What about throws and returns? Absorb them into effects. Final result: S1 is stronger than S2 iff (R 1 ⇒ (E 1 ∧ only-modifies-M 1 )) ⇒ (R 2 ⇒ (E 2 ∧ only-modifies-M 2 ))

Convert spec to formula, step 1: absorb throws, returns CSE 331 style: requires (unchanged) modifies (unchanged) throws effectscorrespond to resulting "effects" returns } Example (from java.util.ArrayList ): // requires: true // modifies: this[index] // throws: IndexOutOfBoundsException if index < 0 || index ≥ size() // effects: this post [index] = element // returns: this pre [index] T set(int index, T element) Equivalent spec, after absorbing throws and returns into effects: // requires: true // modifies: this[index] // effects: if index < 0 || index ≥ size() then throws IndexOutOfBoundsException //else this post [index] = element && returns this pre [index] T set(int index, T element)

Convert spec to formula, step 2: eliminate requires, modifies Single logical formula requires ⇒ (effects ∧ (not-modified)) “ not-modified” preserves every field not in the modifies clause Logical fact: If precondition is false, formula is true Recall: ∀ x. x ⇒ true; ∀ x. false ⇒ x; Example: // requires: true // modifies: this[index] // effects: E (x ⇒ y) ≡ ( ¬ x ∨ y) T set(int index, T element) Result: true ⇒ (E ∧ ( ∀ i≠index. this pre [i] = this post [i]))

Technique 3: Comparing transition relations Transition relation relates prestates to poststates Contains all possible 〈 input,output 〉 pairs Transition relation maps procedure arguments to results int increment(int i) { return i+1; } double mySqrt(double a) { if (Random.nextBoolean()) return Math.sqrt(a); else return - Math.sqrt(a); } A specification has a transition relation, too Contains just as much information as other forms of specification

Satisfaction via transition relations A stronger specification has a smaller transition relation Rule: P satisfies S iff P is a subset of S (when both are viewed as transition relations) sqrt specification (S sqrt ) // requires x is a perfect square // returns positive or negative square root int sqrt (int x) Transition relation: 〈 0,0 〉, 〈 1,1 〉, 〈 1,-1 〉, 〈 4,2 〉, 〈 4,-2 〉, … sqrt code (P sqrt ) int sqrt (int x) { // … always returns positive square root } Transition relation: 〈 0,0 〉, 〈 1,1 〉, 〈 4,2 〉, … P sqrt satisfies S sqrt because P sqrt is a subset of S sqrt

Beware transition relations in abbreviated form “ P satisfies S iff P is a subset of S” is a good rule But it gives the wrong answer for transition relations in abbreviated form (The transition relations we have seen so far are in abbreviated form!) anyOdd specification (S anyOdd ) // requires x = 0 // returns any odd integer int anyOdd (int x) Abbreviated transition relation: 〈 0,1 〉, 〈 0,3 〉, 〈 0,5 〉, 〈 0,7 〉, … anyOdd code (P anyOdd ) int anyOdd (int x) { return 3; } Transition relation: 〈 0,3 〉, 〈 1,3 〉, 〈 2,3 〉, 〈 3,3 〉, … The code satisfies the specification, but the rule says it does not P anyOdd is not a subset of S anyOdd because 〈 1,3 〉 is not in the specification’s transition relation We will see two solutions to this problem: full or abbreviated transition relations

Satisfaction via full transition relations (option 1) The transition relation should make explicit everything an implementation may do Problem: abbreviated transition relation for S does not indicate all possibilities anyOdd specification (S anyOdd ): // requires x = 0 // returns any odd integer int anyOdd (int x) Full transition relation: 〈 0,1 〉, 〈 0,3 〉, 〈 0,5 〉, 〈 0,7 〉, … 〈 1, 0 〉, 〈 1, 1 〉, 〈 1, 2 〉, …, 〈 1, exception 〉, 〈 1, infinite loop 〉, … 〈 2, 0 〉, 〈 2, 1 〉, 〈 2, 2 〉, …, 〈 2, exception 〉, 〈 2, infinite loop 〉, … anyOdd code (P anyOdd ): int anyOdd (int x) { return 3; } Transition relation: 〈 0,3 〉, 〈 1,3 〉, 〈 2,3 〉, 〈 3,3 〉, … // same as before // on previous slide // new // new // same as before The rule “P satisfies S iff P is a subset of S” gives the right answer for full relations Downside: writing the full transition relation is bulky and inconvenient It’s more convenient to make the implicit notational assumption: For elements not in the domain of S, any behavior is permitted. (Recall that a relation maps a domain to a range.)

Satisfaction via abbreviated transition relations (option 2) New rule: P satisfies S iff P | (Domain of S) is a subset of S where “P | D” = “P restricted to the domain D” i.e., remove from P all pairs whose first member is not in D (recall that a relation maps a domain to a range) anyOdd specification (S anyOdd ) // requires x = 0 // returns any odd integer int anyOdd (int x) Abbreviated transition relation: 〈 0,1 〉, 〈 0,3 〉, 〈 0,5 〉, 〈 0,7 〉, … anyOdd code (P anyOdd ) int anyOdd (int x) { return 3; } Transition relation: 〈 0,3 〉, 〈 1,3 〉, 〈 2,3 〉, 〈 3,3 〉, … Domain of S = { 0 } P | (domain of S) = 〈 0,3 〉, which is a subset of S, so P satisfies S The new rule gives the right answer even for abbreviated transition relations We’ll use this version of the notation in CSE 331

Abbreviated transition relations, summary True transition relation: Contains all the pairs, all comparisons work Bulky to read and write Abbreviated transition relation Shorter and more convenient Naively doing comparisons leads to wrong result How to do comparisons: Use the expanded transition relation, or Restrict the domain when comparing Either approach makes the “smaller is stronger rule” work

Review: strength of a specification A stronger specification is satisfied by fewer procedures A stronger specification has - weaker preconditions (note contravariance) - stronger postcondition - fewer modifications Advantage of this view: can be checked by hand A stronger specification has a (logically) stronger formula Advantage of this view: mechanizable in tools A stronger specification has a smaller transition relation Advantage of this view: captures intuition of “stronger = smaller” (fewer choices)

Specification style A procedure has a side effect or is called for its value Bad style to have both effects and returns Exception: return old value, as for HashMap.put The point of a specification is to be helpful Formalism helps, overformalism doesn't A specification should be - coherent: not too many cases - informative: bad example: HashMap.get - strong enough: to do something useful, to make guarantees - weak enough: to permit (efficient) implementation

Checking preconditions - makes an implementation more robust - provides better feedback to the client - avoids silent errors A quality implementation checks preconditions whenever it is inexpensive and convenient to do so