n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Similar documents
Assertions, pre/postconditions

CS 220: Discrete Structures and their Applications. Loop Invariants Chapter 3 in zybooks

Program Verification (Rosen, Sections 5.5)

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

a correct statement? You need to know what the statement is supposed to do.

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

Programming with assertions Oracle guidelines

Assertions. Assertions - Example

3. Design by Contract

When Brunel s ship the SS Great Britain was launched into the River Thames, it made such a splash that several spectators on the opposite bank were

6.092 Introduction to Software Engineering in Java January (IAP) 2009

CSE wi Midterm Exam 2/8/18. Name UW ID #

Testing. UW CSE 160 Winter 2016

Java Modelling Language (JML) References

CSC Advanced Object Oriented Programming, Spring Specification

Static program checking and verification

Write for your audience

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

CS 161 Computer Security

CSE wi Midterm Exam 2/8/18 Sample Solution

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

INTERNATIONAL EDITION. Problem Solving with C++ Data Abstraction & SIXTH EDITION. Walls and Mirrors. Frank M. Carrano Timothy Henry

CS 1110: Introduction to Computing Using Python Loop Invariants

CS 201 Advanced Object-Oriented Programming Lab 4 - Asteroids, Part 2 Due: February 24/25, 11:30 PM

CS Lecture 19: Loop invariants

CSE 331 Software Design & Implementation

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

15-122: Principles of Imperative Computation (Section G)

CSE 331 Final Exam 3/16/15 Sample Solution

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

CSE331 Winter 2014, Midterm Examination February 12, 2014

Solutions to Quiz 1 (March 14, 2016)

Testing, Debugging, and Verification

CS108, Stanford Handout #3. HW1 CodeCamp

Designing Robust Classes

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CSE 20 DISCRETE MATH WINTER

Lecture Notes on Contracts

Hoare Logic: Proving Programs Correct

CS 1110 Final, December 17th, Question Points Score Total: 100

Program Correctness and Efficiency. Chapter 2

Eiffel: Analysis, Design and Programming. ETH Zurich, September-December Exception handling

Java Programming. Atul Prakash

Lab 5 : WordAnalyzer class

CIT 590 Homework 10 Battleship

Assignment 2 - Specifications and Modeling

Chapter 3 (part 3) Describing Syntax and Semantics

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Java Modelling Language (JML) References

CS-152: Software Testing

Formale Entwicklung objektorientierter Software

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

CSE 331 Midterm Exam Sample Solution 2/13/12

APCS Semester #1 Final Exam Practice Problems

CSE 331 Midterm Exam Sample Solution 2/18/15

Mock exam 1. ETH Zurich. Date: 9./10. November 2009

Errors. And How to Handle Them

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

CSE 331 Final Exam 3/12/12

Exceptions and assertions

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Dynamic Analysis Techniques Part 2

The Java Modeling Language JML

Contract Programming For C++0x

Induction and Semantics in Dafny

Runtime Checking and Test Case Generation for Python

CSE 307: Principles of Programming Languages

Lecture 24: Loop Invariants

Classes, interfaces, & documentation. Review of basic building blocks

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

Programming Languages Third Edition

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

EXAMINATIONS 2009 END-OF-YEAR. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Taking Apart Numbers and Shapes

Chapter 4 Defining Classes I

Code Documentation.

Chap 6. Test Models and Strategies

Applications. segment CD. 1. Find the area of every square that can be drawn by connecting dots on a 3 dot-by-3 dot grid.

Chapter Goals. Contents LOOPS

Abstraction Functions and Representation Invariants

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Combining Static and Dynamic Contract Checking for Curry

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

CS 1110 Final, December 9th, Question Points Score Total: 100

An Annotated Language

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Testing and Debugging

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

C a; C b; C e; int c;

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Transcription:

Programming as a contract Assertions, pre/postconditions and invariants Assertions: Section 4.2 in Savitch (p. 239) Loop invariants: Section 4.5 in Rosen Specifying what each method does q Specify it in a comment before method's header Precondition q What is assumed to be true before the method is executed q Caller obligation Postcondition q Specifies what will happen if the preconditions are met what the method guarantees to the caller q Method obligation Example /* ** precondition: x >= 0 ** postcondition: return value satisfies: ** result * result == x */ double sqrt(double x) { Enforcing preconditions /* ** precondition: x >= 0 ** postcondition: return value satisfies: ** result * result == x */ double sqrt(double x) { if (x < 0) throw new ArithmeticExpression ( you tried to take sqrt of a neg number! ); Class Invariants A class invariant is a condition that all objects of that class must satisfy while it can be observed by clients What about a Rectangle object? What is an assertion? An assertion is a statement that says something about the state of your program Should be true if there are no mistakes in the program //n == 1 while (n < limit) { n = 2 * n; // what could you state here? 1

What is an assertion? assert An assertion is a statement that says something about the state of your program Should be true if there are no mistakes in the program //n == 1 while (n < limit) { n = 2 * n; //n >= limit Using assert: assert n == 1; while (n < limit) { n = 2 * n; assert n >= limit; When to use Assertions Another example if (i % 3 == 0) {... else if (i % 3 == 1) {... else { // We know (i % 3 == 2)... When to use Assertions We can use assertions to guarantee the behavior. if (i % 3 == 0) {... else if (i % 3 == 1) {... else { assert i % 3 == 2;... Another example Another example int p=,d= ; int q = p/d; int r = p%d; assert? int p=,d= ; int q = p/d; int r = p%d; assert p == q*d + r; 2

Control Flow If a program should never reach a point, then a constant false assertion may be used private void search() { for (...) {... if (found) // will always happen return; assert false; // should never get here Assertions Syntax: assert Boolean_Expression; Each assertion is a Boolean expression that you claim is true. By verifying that the Boolean expression is indeed true, the assertion confirms your claims about the behavior of your program, increasing your confidence that the program is free of errors. If assertion is false when checked, the program raises an exception. When to use assertions? Programming by contract Preconditions in methods (eg value ranges of parameters) should be enforced rather than asserted because assertions can be turned off Postconditions q Assert post-condition Performance Assertions may slow down execution. For example, if an assertion checks to see if the element to be returned is the smallest element in the list, then the assertion would have to do the same amount of work that the method would have to do Therefore assertions can be enabled and disabled Assertions are, by default, disabled at run-time In this case, the assertion has the same semantics as an empty statement Think of assertions as a debugging tool Don t use assertions to flag user errors, because assertions can be turned off Assertions in Eclipse To enable assert statements, you must set a compiler flag. Go to Run -> Run Configurations - > Arguments, and in the box labeled VM arguments, enter either -enableassertions or just -ea More Information For more information: http://java.sun.com/j2se/1.4.2/docs/guide/ lang/assert.html 3

Loop invariants We can use predicates (logical expressions) to reason about our programs. A loop invariant is a predicate q that is true directly before the loop executes q that is true before and after the loop body executes q and that is true directly after the loop has executed i.e., it is kept invariant by the loop. Loop invariants Combined with the loop condition, the loop invariant allows us to reason about the behavior of the loop: <loop invariant> while(test){ <test AND loop invariant> S; <loop invariant> < not test AND loop invariant> What does it mean... Example: loop index value after loop <loop invariant> while(test){ <test AND loop invariant> S; <loop invariant> < not test AND loop invariant> If we can prove that the loop invariant holds before the loop and that the loop body keeps the loop invariant true i.e. <test AND loop invariant> S; <loop invariant> then we can infer that not test AND loop invariant holds after the loop terminates <precondition: n>0> int i = 0; while (i < n){ i = i+1; <post condition: i==n > We want to prove: i==n right after the loop Example: loop index value after loop Example: sum of elements in an array // precondition: n>0 int i = 0; // i<=n loop invariant while (i < n){ // i < n test passed // AND // i<=n loop invariant i++; // i <= n loop invariant // i>=n AND i <= n à i==n So we can conclude the obvious: i==n right after the loop int total (int[] elements){! int sum = 0, i = 0, n = elements.length;! // sum == sum of elements from 0 to i-1! while (i < n){! // sum == sum of elements 0...i-1! sum += elements [i];! i++;! // sum == sum of elements 0...i-1!! // i==n (previous example) AND! // sum == sum elements 0...i-1! // à sum == sum of elements 0...n-1! return sum;!!! 4

There are two players, Red and Blue. The game is played on a rectangular grid of points: 6.......! 5.......! 4.......! 3.......! 2.......! 1.......! 1 2 3 4 5 6 7! Red draws a red line segment, either horizontal or vertical, connecting any two adjacent points on the grid that are not yet connected by a line segment. Blue takes a turn by doing the same thing, except that the line segment drawn is blue. Red's goal is to form a closed curve of red line segments. Blue's goal is to prevent Red from doing so.! See http://www.cs.uofs.edu/~mccloske/courses/cmps144/invariants_lec.html We can express this game as a computer program: while (more line segments can be drawn) {! Red draws line segment;! Blue draws line segment;!!! Question: Does either Red or Blue have a winning strategy?! Answer: Yes! Blue is guaranteed to win the game by responding to each turn by Red in the following manner: if (Red drew a horizontal line segment) {! let i and j be such that Red's line segment connects (i,j) with (i,j+1)! if (i>1) {! draw a vertical line segment connecting (i-1,j+1) with (i,j+1)! else {! draw a line segment anywhere!! else // Red drew a vertical line segment! let i and j be such that Red's line segment connects (i,j) with (i+1,j)! if (j>1) {! draw a horizontal line segment connecting (i+1,j-1) with (i+1,j)! else {! draw a line segment anywhere!!! By following this strategy Blue guarantees that Red does not have an upper right corner at any step. So, the invariant is: There does not exist on the grid a pair of red line segments that form an upper right corner. And in particular, Red has no closed curve! Example: Egyptian multiplication A B 19 5 19 x 5: /2 9 10 *2 /2 4 20 *2 /2 2 40 *2 /2 1 80 *2 throw away all rows with even A: A B 19 5 9 10 1 80 add B's 95 --> the product!! Can we show it works? Loop invariants!! // pre: left >0 AND right >0 int a=left, b=right, p=0; //p: the product // p + (a*b) == left * right loop invariant while (a!=0){ // a!=0 and p + (a*b) == left * right // loop condition and loop invariant if (odd(a)) p+=b; a/=2; b*=2; // p+(a*b) == left*right // a==0 and p+a*b == left*right à p == left*right 5

Try it on 7 * 8 left right a b p 7 8 7 8 0 3 16 +=b: 8 1 32 +=b: 24 0 64 +=b: 56 Try it on 8*7 left right a b p 8 7 8 7 0 4 14 0 2 28 0 1 56 0 0 118 +=b: 56 Relation to int representation 19*5 00101 10011 101 5 1010 10 00000 000000 1010000 80 1011111 95 Summary: Loop Invariant Reasoning //loop invariant true before loop while (b){ // b AND loop invariant S; // loop invariant // not b AND loop invariant not b helps you make a stronger observation than loop invariant alone. 6