Top Banner
David Evans http://www.cs.virginia.edu/ evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification
37

David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

Mar 31, 2015

Download

Documents

Clayton Ayer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

David Evanshttp://www.cs.virginia.edu/evans

CS201j: Engineering Software?University of VirginiaComputer Science

Lecture 3: Abstraction by Specification

Page 2: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 2

Menu

• Validation

• Abstraction by Specification

Page 3: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 3

Testing Recap• Testing can find problems, but can’t prove

your program works– Since exhaustive testing is impossible, select

test cases with maximum probability of finding bugs

– A successful test case is one that reveals a bug in your program!

• If we can’t test all possible paths through a program, how can we increase our confidence that it works?

Page 4: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 4

Analysis

• Make claims about all possible paths by examining the program code directly, not executing it

• Use formal semantics of programming language to know what things mean

• Use formal specifications of procedures to know that they do

Page 5: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 5

Hopelessness of Analysis

• It is impossible to correctly determine if any interesting property is true for an arbitrary program!

The Halting Problem: it is impossible to write a program that determines if an arbitrary program halts.

Page 6: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 6

Compromises• Accept unsoundness and incompleteness• False positives: sometimes an analysis

tool will report warnings for a program, when the program is actually okay (unsoundness)

• False negatives: sometimes an analysis tool will report no warnings for a program, even when the program violates properties it checks (incompleteness)

Page 7: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 7

Extended Static Checking (ESC/Java)• Analysis tool developed at

DEC/Compaq/HP Research Lab

• Is unsound and incomplete:– False negatives: Just because it finds no

warnings, doesn’t mean your code is correct– False positives: Sometimes it will warn even

when your code is correct

• PS2: use without adding annotations

• Later: use annotations to document program assumptions

Page 8: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 8

Abstraction by Specification

Page 9: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 9

Managing Complexity• Divide problem into subproblems that

– Can be solved independently– Can be combined to solve the original

problem

• How do we know they can be solved independently?

• How do we know they can be combined to solved the original problem?

Page 10: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 10

Abstraction

A

I1I2

I3I4

An abstraction is a many-to-one map.

I5

Page 11: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 11

Using AbstractionsA

I1I2

I3I4

I5

Client

When a client uses an abstraction, it should work as the client expects it to no matter with implementation is provided.

How should client know what to expect?

Page 12: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 12

Specification• Tells the client of an abstraction what she

can expect it to do

• Tells the implementer of an abstraction what the implementation must do to satisfy the client

• Contract between client and implementer:– Client will only rely on behavior described by

specification– Implementer will provide an implementation

that satisfies the specification

Page 13: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 13

Good Specifications• Clear, precise and unambiguous

– Clients and implementers will agree on what they mean

• Complete– Describe the behavior of the abstraction in all

situations

• Declarative– Describe what the abstraction should do, not

how it should do it

Page 14: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 14

Formality of Specifications• Informal: written in a natural language

(e.g., English)– People can disagree on what it means– Degrees of informality

• Formal: written in a specification language– Meaning is defined by specification language

(whose meaning is defined precisely, but eventually informally)

– May be analyzed by machines

Page 15: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 15

What do you call people who decide what informal specifications mean?

Page 16: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 16

Example Informal Specification

Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted.

8th Amendment

Page 17: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 17

Correct Implementation?

public static boolean violatesEigthAmendment (Punishment p) { // EFFECTS: Returns true if p violates the 8th

// amendment: cruel and unusual // punishments. return (p.isCruel () && p.isUnusual ());

}

Or did they mean p.isCruel () || p.isUnusual () ?

Page 18: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 18

A player is in an offside position if: he is nearer to his opponents’ goal line than both the ball and the second last opponentA player is not in an offside position if: he is in his own half of the field of play or he is level with the second last opponent or he is level with the last two opponentsOffenceA player in an offside position is only penalised if, at the moment the ball touches or is played by one of his team, he is, in the opinion of the referee, involved in active play by: interfering with play or interfering with an opponent or gaining an advantage by being in that positionNo OffenceThere is no offside offence if a player receives the ball directly from: a goal kick or a throw-in or a corner kick.

http://www.fifa.com/fifa/handbook/laws/2002/LOTG2002_E.pdf, Law 11

Example (Good?) Informal Specification

Page 19: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 19

Procedural Specifications

• Specification for a procedure describes:– What its inputs are– What the mapping between inputs and

outputs are– What it can do the state of the world

Page 20: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 20

Requires and Effects

• Header: name of procedure, types of parameters and return value– Java declaration

• Clauses (comments in Java)– REQUIRES - precondition the client

must satisfy before calling– EFFECTS – postcondition the

implementation satisfy at return

Page 21: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 21

Contract

• Client promises to satisfy the precondition in the requires clause

• Implementer promises if client satisfies the precondition, the return value and state when the function returns will satisfy the postcondition.

Page 22: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 22

Specification Contract

f () REQUIRES: precondition EFFECTS: postcondition

precondition{ f (); }postcondition

If the precondition is true,after we call f (),the postcondition is true.

Page 23: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 23

Specification Example

public String bestStock ()

// REQUIRES: false

// EFFECTS: Returns the name of the

// best stock to buy on the NASDAQ

// tomorrow.

Can we implement a procedure that satisfies this specification?

Yes, any implementation will satisfy this specification!If the precondition in the requires clause is not satisfied,the procedure can do anything and still satisfy its specification!

Page 24: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 24

Specification Example

public String bestStock ()

// REQUIRES: true

// EFFECTS: Returns the name of the

// best stock to buy on the NASDAQ

// tomorrow.

Can we implement a procedure that satisfies this specification?

Page 25: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 25

Requires Clauses• The weaker (more easy to make true) the

requires clause:– The more useful a procedure is for clients– The more difficult it is to implement correctly– The more difficult it is to test

• Avoid requires clauses unless there is a good reason to have one– Default requires clause is: REQUIRES true– Client doesn’t need to satisfy anything before

calling

Page 26: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 26

Specification Example

public static int biggest (int [ ] a) // REQUIRES: true

// EFFECTS: Returns the value of the// biggest element of a.

Is this a reasonable specification?

No, what should client expect to happen if a is empty.

Page 27: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 27

Specification Example

public static int biggest (int [ ] a) // REQUIRES: a has at least one

element.// EFFECTS: Returns the value of the// biggest element of a.

Is this a good specification?

Maybe, depends on the client. Its risky…

Page 28: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 28

Specification Examplepublic static int biggest (int [ ] a) // REQUIRES: true

// EFFECTS: If a has at least one // element, returns the value biggest// element of a. Otherwise, returns// Integer.MIN_VALUE (smallest int // value).Better, but client has to deal with special case now.

Best would probably be to use an exception…

Page 29: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 29

Bad Use of Requires Clause

• Bug discovered in Microsoft Outlook that treats messages that start with “begin ” as empty attachments (can be exploited by viruses)

To workaround this problem: • Do not start messages with the word "begin" followed by two spaces. • Use only one space between the word "begin" and the following data. • Capitalize the word "begin" so that it is reads "Begin." • Use a different word such as "start" or "commence".

from http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q265230&

Page 30: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 30

Modifies

• How does a client know a is the same after biggest returns?

public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer.MIN_VALUE // (smallest int value).Reading the effects clause is enough – if biggest modifies

anything, it should describe it. But, that’s a lot of work.

Page 31: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 31

Modifies• Modifies clause: any state not listed in the

modifies clause may not be changed by the procedure.

public static int biggest (int [ ] a) // REQUIRES: true // MODIFIES: nothing // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer.MIN_VALUE // (smallest int value).

Page 32: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 32

Modifies Example

public static int replaceBiggest (int [ ] a, int [] b) // REQUIRES: a and b both have at least one // element // MODIFIES: a // EFFECTS: Replaces the value of the biggest // element in a with the value of the biggest // element in b.

Page 33: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 33

Defaults• What should it mean when there is no

requires clause?

• What should it mean when there is no modifies clause?

• What should it mean when there is no effects clause?

REQUIRES: true

MODIFIES: nothing

Meaningless. (Lose points for not writing a specification)

Page 34: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 34

Specifications in CS201J• PS2

– Informal, but precise– Use Requires/Modifies/Effects clauses

• Need a very good reason to have a precondition stronger than true (especially after we cover Exceptions).

• PS3 and later– Informal and some formal– Formal specifications as ESC/Java annotations

• Don’t describe all behavior (so still need the informal specs)

Page 35: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 35

PS1 Informal Specification

• If a cell is currently dead cell and has three live neighbors, then it becomes a live cell.

• If a cell is currently alive and has two or three live cells it remains alive.

• Otherwise, the cell dies.

Any ambiguities in this?

Page 36: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 36

PS1 Comments• Java has real Boolean type

isAlive () means isAlive () == true

Note this is not true in C++!

• Read the coding guidelines– Choose names wisely– Comment usefully, but not gratuitiously

• Don’t put your SSN on assignments– You should keep it as secret as possible– University shouldn’t use it as an identifier

• Ask for a student ID number that is not your SSN

Page 37: David Evans  CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.

4 September 2003 CS 201J Fall 2003 37

Charge• CS201J Bug Bounty

– If you find a bug in the code we provide, you get 10 bonus points.

– If you find a bug in the Java compiler or API code, you get 50 bonus points.

– If you find a bug in ESC/Java or the ESC/Java Specs, you get 5 bonus points.

• PS2 Due Tuesday– Much longer than PS1