CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
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.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
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.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
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.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Slides by Alex Mariakakis Section 5: Midterm Review.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
Modular Decomposition, Abstraction and Specifications
EECE 310: Software Engineering
Types for Programs and Proofs
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Testing UW CSE 160 Winter 2017.
Specifications Liskov Chapter 9
Testing UW CSE 160 Spring 2018.
Abstraction Functions and Representation Invariants
CSE 331 Software Design and Implementation
Abstraction functions, Reasoning About ADTs
Lecture 2: Implementing ArrayIntList reading:
Introduction to Data Structures
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Section 6: Midterm Review
Equality, conclusion Based on material by Michael Ernst, University of Washington.
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
Data Structures and Algorithms for Information Processing
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
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 and Implementation Lecture 7 Abstraction Functions Zach Tatlock / Spring 2018

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

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')) …

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

Abstraction function and insert Goal is to satisfy the specification of insert: // modifies: this // effects: thispost = thispre 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(thispre) = eltspre On exit, meaning is AF(thispost) = AF(thispre) 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 }

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

Stack AF example Abstract stack with array and “top” index implementation new() pop() 17 -9 Top=0 stack = <> Top=1 stack = <17> push(17) 17 Abstract states are the same stack = <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 stack = <17> push(-9) 17 -9 Top=2 stack = <17,-9>

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” a AF AF op  r r’

For any correct operation…

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

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