CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Advertisements

Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
MATH 224 – Discrete Mathematics
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 6 Representation Invariants.
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.
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
Linked Data Structures
Sets.
EECE 310: Software Engineering
EECE 310: Software Engineering
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Building Java Programs
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
MATH 224 – Discrete Mathematics
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 2 Sets Slides are adopted from “Discrete.
Sets Set Identities Sets and propositional logic have some similar properties In fact both are forms of boolean algebra Compare Set Identities table (p.
Stack Data Structure, Reverse Polish Notation, Homework 7
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Design by Contract Fall 2016 Version.
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Data Structures and Algorithms for Information Processing
Introduction to Data Structures
slides created by Marty Stepp and Ethan Apter
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Building Java Programs
Equality, conclusion Based on material by Michael Ernst, University of Washington.
Data Abstraction David Evans cs205: engineering software
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
Data Structures and Algorithms for Information Processing
CSE 331 Software Design & Implementation
Mathematical Background 1
Mathematical Background 1
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
Administrivia- Introduction
Stacks Chapter 5.
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Administrivia- Introduction
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Building Java Programs
CSE 331 Software Design & Implementation
Lecture 7: Linked List Basics reading: 16.2
Software Specifications
Reasoning about Data Abstractions
Presentation transcript:

CSE 331 Software Design and Implementation Lecture 7 Abstraction Functions Leah Perlmutter / Summer 2018

Announcements

Announcements HW2 due tonight 10 pm Wednesday, July 4 is Independence Day No lecture Section Thursday, July 5 HW3 due Thursday, July 5 at 10 pm Seek HW3 help on Tuesday; no office hours Wednesday! Reading 3 posted on website Quiz 3 (coming soon!) due Thursday, July 5 at 10 pm

Motivation

Review Method Specification Abstract Data Type (abstraction) lec04 lec05 IMPLEMENTS IMPLEMENTS An ADT is a kind of specification, for data a method spec can have multiple implementations that all meet the spec an ADT can have multiple implementations that satisfy the data abstraction Method Body (concrete code) Data Structure (concrete code)

Example: CharSet Abstraction // Overview: A CharSet is a finite mutable set of Characters // @effects: creates a fresh, empty CharSet public CharSet() {…} // @modifies: this // @effects: thispost = thispre + {c} public void insert(Character c) {…} // @effects: thispost = thispre - {c} public void delete(Character c) {…} // @return: (c  this) public boolean member(Character c) {…} // @return: cardinality of this public int size() {…} set – see Wolfram Alpha definition set union set difference Let’s revisit our charset example from last lecture. Here’s the charset spec Informal notation warning

Charset Representation Invariant class CharSet { // Rep invariant: // this.elts has no nulls and no duplicates private List<Character> elts = … … } - Here’s the charset rep invariant

Rep inv. constrains structure, not meaning An implementation of insert that preserves the rep invariant: public void insert(Character c) { Character cc = new Character(encrypt(c)); if (!elts.contains(cc)) elts.addElement(cc); } public boolean member(Character c) { return elts.contains(c); Program is wrong Clients observe incorrect behavior What client code exposes the error? Where is the error? We must consider the meaning The abstraction function helps us CharSet s = new CharSet(); s.insert('a'); if (s.member('a')) … Does this code respect the rep invariant? (yes.) What is wrong with it? different levels of abstraction in the different methods doesn't meet spec Problem Rep invariiant places restrictions on the states that the rep can take on Rep invariant doesn’t tell different methods what the rep means

An ADT has an abstract value Abstract Value: An Int List is a finite sequence of integer values Integer(1) Integer(2) Integer(42) Integer(17) null size: 4 head Integer(1) Integer(2) Integer(42) Integer(17) null size: 3 head null Integer(2) Integer(42) Integer(17) size: 0 head In the last lecture, we informally thought about converting a concrete value to an abstract value Today we will formalize this notion 1, 2, 42, 17 ? ?????

Connecting implementations to specs Representation Invariant: maps Object → boolean Indicates if an instance is well-formed Defines the set of valid concrete values Only values in the valid set make sense as implementations of an abstract value For implementors/debuggers/maintainers of the abstraction: no object should ever violate the rep invariant Such an object has no useful meaning Abstraction Function: maps Object → abstract value What the data structure means as an abstract value How the data structure is to be interpreted Only defined on objects meeting the rep invariant For implementors/debuggers/maintainers of the abstraction: Each procedure should meet its spec (abstract values) by “doing the right thing” with the concrete representation lec06 lec07 (today)

Functions You’ve likely seen the concept of a function several times before We’ll develop some mathematical formalisms for functions here If you’ve taken certain math courses, these formalisms may be review Otherwise, they may be new

Set An unordered collection of objects S = {3, 1, 2, mouse} An object can be in the set or not 3 ∈ S -1 ∉ S Set builder notation T = {x | x ∈ S and x is an integer} = {2, 1, 3} Some familiar sets = {...-1, 0, 1, 2, ...} “the integers” ℚ = {p/q | p, q ∈ } “the rational numbers” ∈ = “elelment of” Set review concept of a set element of subset set construction notation Familiar sets: Z = integers = {... -1, 0, 1, 2, ...} Q = rational numbers = { p/q | p, q, in Z} | = “such that”

Function A relation that uniquely associates members of one set with members of another set [Wolfram] F : S  Y “F maps S to Y” Domain Codomain Range: {animal, number} mouse 1 2 3 animal vegetable mineral number Function notation -- f: X  Y “f maps X to Y”, where X is the domain and Y is the codomain circle diagram – domain  codomain (1 slide) each point in the domain maps to a point in the codomain (its image) range is the set of images of all elements in the domain it’s a subset of the codomain ”all values that are reached by the function”

Example Function F(x) = x2 F :  F(x) = x2 passes vertical line test 4 ... -2 1 2 2.5 4 6.25 passes vertical line test Example Function Example function: f(x) = x^2 Draw as a circle diagram and as a graph domain: real numbers codomain: real numbers range: non-negative real numbers Every value in the domain maps to something multiple domain values may map to the same element (e.g. (-2)^2 = 4 && 4^2 = 4) Every domain value maps to exactly one range value (vertical line test) +- sqrt is not a function (it’s two functions)

Example NOT Function Inverse of F(x) = x2 y = ± sqrt(x) sqrt(25) = 5 Example Function Example function: f(x) = x^2 Draw as a circle diagram and as a graph domain: real numbers codomain: real numbers range: non-negative real numbers Every value in the domain maps to something multiple domain values may map to the same element (e.g. (-2)^2 = 4 && 4^2 = 4) Every domain value maps to exactly one range value (vertical line test) +- sqrt is not a function (it’s two functions) Does not pass vertical line test – Not a function!

Functions in Math and Programming In programming, the term “function” is often loosely used Related to the concepts of “method” and “subroutine” float square(float x) { return x * x; } This method implements a mathematical function void greet(String name) { System.out.println("Hello, " + name); This method does not implement a mathematical function Discuss in groups: similarities and differences between mathematical functions and java methods circulation question: Do you feel comfortable with the difference between mathematical functions and java methods? Mathematical function (AF) is different from the programming concept of a function Programming concept of a function how is it similar to and different from a mathematical function function - a named section of a program that performs a specific task. (webopedia) subroutine -- sequence of program instructions that performs a specific task, packaged as a unit. (wikipedia) method – a function associated with an object

Abstraction Functions

AF: concrete rep → abstract value Abstraction Function The abstraction function maps concrete representations to the abstract values they represent AF: concrete rep → abstract value AF(CharSet this) = { c | c is contained in this.elts } “set of Characters contained in this.elts” The abstraction function lets us reason about what [concrete] methods do in terms of the clients’ [abstract] view Makes sure that all methods use the rep in the same way Math concept of function, not programming concept of function AF not implementable in code since range is abstract values TODO: homework-y example of how this looks in the code

that have a corresponding Abstraction Function Values allowed by the Data Structure – all concrete values Well Formed Values – concrete values that have a corresponding abstract value Rep Invariant Holds

Abstraction Function Codomain Range Domain All concrete values All Abstract Values Codomain Concretely Representable Abstract Values Well Formed concrete values Range Domain representation: all possible values of int size + chain of nodes containing Integer and pointer to next node rep invariant restricts us to the green area (circle diagram) Abstraction Function is defined for all concrete values in the green area this is the domain of our AF

Abstraction Function 1, 2, 42, 17 Codomain Range Domain All concrete values All Abstract Values Integer(1) Integer(2) Integer(42) Integer(17) null size: 4 head Codomain Concretely Representable Abstract Values Well Formed concrete values Range Domain example: 1, 2, 42, 17 concrete rep in the domain abstract value in the range Codomain of AF abstract state of an int list – finite sequence of integer values Are there any values in this codomain that can’t be represented by our rep? 1, 2, 42, 17

Abstraction Function Codomain Range Domain All concrete values All Abstract Values null Integer(2) Integer(42) Integer(17) size: 0 head Codomain Concretely Representable Abstract Values Well Formed concrete values Range Domain - concrete rep in the gray area not in the domain

Abstraction Function 0, 1, 210,000 Codomain Range Domain All concrete values All Abstract Values Codomain 0, 1, 210,000 Concretely Representable Abstract Values Well Formed concrete values Range Domain abstract value with no concrete rep (2 ^ 10,000 is larger than int max) Are there any two concrete linked list values that map to the same abstract value? I can’t think of any... but let’s consider another data structure

Summary so far: AF: concrete rep → abstract value The abstraction function maps concrete representations to the abstract values they represent AF: concrete rep → abstract value Well Formed concrete values Concretely Representable Abstract Values

The abstraction function is a function Why do we map concrete to abstract and not vice versa? It’s not a function in the other direction Example: lists [a,b] and [b,a] might each represent the set {a, b} It’s not as useful in the other direction Purpose is to reason about whether our methods are manipulating concrete representations correctly in terms of the abstract specifications

Writing an abstraction function Domain: all representations that satisfy the rep invariant Range: concretely representable abstract values Overview section of the specification should provide a notation of writing abstract values Could implement a method for printing in this notation Useful for debugging Often a good choice for toString

Abstraction Function and Stack /** A last-in, first-out stack. A typical stack is e0, e1, ... en where en is the top element of the stack and is most recently pushed and first available to be popped. */ public class Stack { // Rep invariant: // 0 <= this.top <= this.a.length // this.a != null // Abstraction Function: // AF(this) = A last-in, first-out stack // defined by an ordered sequence of integers // this.a[0] ... this.a[this.top-1] // where the rightmost integer in the // sequence is at the top of the stack private int[] a; private int top; ... } implicit: the number of elements in the stack is top implicit: top points to the array element just “after” the top of the stack

Stack AF example 17 17 17, -9 empty stack recall: top points to the array element just after the top of the stack new() pop() 17 -9 top=0 empty stack top=1 17 push(17) 17 Abstract states are the same 17 = 17 Concrete states are different <[17,0,0], top=1> ≠ <[17,-9,0], top=1> AF is a function Inverse of AF is not a function top=1 17 - this example is stack AF! push(-9) 17 -9 top=2 17, -9

Benevolent side effects concrete abstract Different implementation of member: boolean member(Character c1) { int i = elts.indexOf(c1); if (i == -1) return false; // move-to-front optimization Character c2 = elts.elementAt(0); elts.set(0, c1); elts.set(i, c2); return true; } Move-to-front speeds up repeated membership tests Mutates rep, but does not change abstract value AF maps both reps to the same abstract value Precise reasoning/explanation for “clients can’t tell” r r’ a op  AF It’s possible for two concrete values to map to the same abstract value this means we can do operations on the rep that don’t affect the abstract value

Abstract and Concrete operations - in the last slide, we did a concrete operation that did not affect the abstract value here’s an illustration of a concrete operation that does affect the abstract value example: add character to the character set

Abstraction Function and Charset The AF tells us what the rep means... public void insert(Character c) { Character cc = new Character(encrypt(c)); if (!elts.contains(cc)) elts.addElement(cc); } public boolean member(Character c) { return elts.contains(c); The two methods assume different abstraction functions! BAD!!! AF(this) = { c | encrypt(c) is contained in this.elts } - What is wrong with this code? - Try to think of an answer that incorporates abstraction functions. AF(this) = { c | c is contained in this.elts }

Charset Abstraction Function class CharSet { // Rep invariant: // this.elts has no nulls and no duplicates // Abstraction Function: // AF(this) = { c | c is contained in this.elts } private List<Character> elts = … … } Defined in terms of the representation (this.elts) Internal comment (not javadoc) located just inside of the class definition at the very beginning Now we can re-implement insert to respect the AF - Here’s the charset rep invariant

Data Abstraction: Summary Representation Invariant describes what makes the concrete representation valid (green area) Abstraction Function maps valid concrete values to abstract values Neither one is part of the ADT’s specification Both are needed to reason an implementation satisfies the specification -

Closing

Closing Announcements HW2 due tonight 10 pm HW3 due Thursday, July 5 at 10 pm Quiz 3 (coming soon!) due Thursday, July 5 at 10 pm Happy Independence Day!