CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
CSE503: SOFTWARE ENGINEERING COMPLEXITY, PROVING ADTS David Notkin Spring 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.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Specifications Liskov Chapter 9 SWE 619 Last Updated Fall 2008.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Testing Michael Ernst CSE 140 University of Washington.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Abstraction ADTs, Information Hiding and Encapsulation.
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.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Events, Listeners, and Callbacks (slides by Mike Ernst and David Notkin) 1.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
Section 6: Interfaces, Midterm Slides by Vinod Rathnam and Geoffrey Liu (with material from Alex Mariakakis, Kellen Donohue, David Mailhot, and Hal Perkins)
CSE 331 Software Design & Implementation Hal Perkins Winter 2015 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman,
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
Slides with material from Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 3: HW4, ADTs, and more.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Slides by Alex Mariakakis Section 5: Midterm Review.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Specifications Liskov Chapter 9
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Abstraction functions, Reasoning About ADTs
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Section 6: Midterm Review
CSE 331 Software Design and Implementation
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
CSE 331 Software Design & Implementation
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
Warmup ["hip","hip"] = Hip Hip Array! ???.
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 331 Software Design & Implementation
Software Specifications
Reasoning about Data Abstractions
Presentation transcript:

CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)

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 2CSE331 Fall 2014

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 still 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')) … 3CSE331 Fall 2014

Abstraction function: rep→abstract value The abstraction function maps the concrete representation to the abstract value it represents AF: Object → abstract value AF(CharSet this) = { c | c is contained in this.elts } “set of Characters contained in this.elts” Not executable because abstract values are “just” conceptual The abstraction function lets us reason about what [concrete] methods do in terms of the clients’ [abstract] view 4CSE331 Fall 2014

Abstraction function and insert Goal is to satisfy the specification of insert: // modifies: this // effects: this post = this pre U {c} public void insert (Character c) {…} The AF tells us what the rep means, which lets us place the blame AF(CharSet this) = { c | c is contained in this.elts } Consider a call to insert : On entry, meaning is AF(this pre ) ≈ elts pre On exit, meaning is AF(this post ) = AF(this pre ) U {encrypt('a')} What if we used this abstraction function instead? AF(this) = { c | encrypt(c) is contained in this.elts } = { decrypt(c) | c is contained in this.elts } 5CSE331 Fall 2014

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 6CSE331 Fall 2014

Stack AF example Abstract stack with array and “top” index implementation new()000 push(17)1700 Top=1 push(-9)17-90 Top=2 Top=0 stack = <> stack = pop()17-90 stack = Top=1 Abstract states are the same stack = = Concrete states are different ≠ AF is a function Inverse of AF is not a function 7CSE331 Fall 2014

Benevolent side effects 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” rr’ a op  AF 8CSE331 Fall 2014

For any correct operation… 9CSE331 Fall 2014

Writing an abstraction function Domain: all representations that satisfy the rep invariant Range: can be tricky to denote For mathematical entities like sets: easy For more complex abstractions: give names to specification –AF defines the value of each “specification field” 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 10CSE331 Fall 2014

Data Abstraction: Summary Rep invariant –Which concrete values represent abstract values Abstraction function –For each concrete value, which abstract value it represents Together, they modularize the implementation –Neither one is part of the ADT’s specification –Both are needed to reason an implementation satisfies the specification In practice, representation invariants are documented more often and more carefully than abstraction functions –A more widely understood and appreciated concept 11CSE331 Fall 2014