It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Programming Languages and Paradigms
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Encapsulation by Subprograms and Type Definitions
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Lecture 9 Concepts of Programming Languages
Describing Syntax and Semantics
Abstract Data Types and Encapsulation Concepts
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Imperative Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Defining New Types Lecture 21 Hartmut Kaiser
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Built-in Data Structures in Python An Introduction.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Abstractions EECE 310: Software Engineering.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Understanding ADTs CSE 331 University of Washington.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
Unified Modeling Language (UML)
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
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.
EECE 310: Software Engineering
Principles of programming languages 10: Object oriented languages
Principles of programming languages 12: Functional programming
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Data Abstraction David Evans cs205: engineering software
Lecture 4: Data Abstraction CS201j: Engineering Software
Lecture 7: Data Abstraction
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction

20 March 2001CS 655: Lecture 152 Menu Data Abstraction before CLU Data Abstraction in CLU Reasoning about Data Abstractions –Abstraction Functions –Rep Invariants

20 March 2001CS 655: Lecture 153 What is a type? Last time: a set of values Today: an abstraction for decomposing a program that provides a set of operations Sets of values don’t work because you are tied to the representation

20 March 2001CS 655: Lecture 154 Data Abstraction in C/Pascal? User-defined types: typedef enum { red = 0, green, blue } color; typedef struct { int locx; int locy; } location; Type checking either: –By structure (e.g., color dumb = 23) –By name (maybe in Pascal, ambiguous) Only way to use type, is to access its representation; no restrictions on where you can do this.

20 March 2001CS 655: Lecture 155 Data Abstraction in BLISS? User specifies accessing algorithm for structure elements May modify either structure definition or algorithms without affecting the other structure array[i, j] = (.array +.i * 10 +.j) May define memory allocation routines But: only arrays, no typed elements

20 March 2001CS 655: Lecture 156 Data Abstraction in Simula67 Define a class with hidden attributes (visible only in the class implementation) and protected attributes (visible in subclass implementations also) Unfortunately, not widely known: –From Sweden –Few Publications (mostly in Swedish), no language Report, no decent textbook until 1986 Alan Kay learned about Simula by reading the source code, thinking it was an Algol compiler! –Big influence on Smalltalk and C++; small influence on CLU

20 March 2001CS 655: Lecture 157 Providing Data Abstraction Type check by name Restrict what code can access the representation of a data type –CLU, Alphard: only operations of the type –Other (possibly) reasonable answers: C++: allow functions outside the type that are declared friend s to access representation C with LCLint: in files and functions according to a naming convention, elsewhere when explicitly annotated [Stata97]: operations can access the only some of the representation

20 March 2001CS 655: Lecture 158 Data Abstraction in CLU intmap = data type is create, insert, lookup Operations create = proc () returns (intset) effects Returns a new, empty intmap. insert = proc (s: intmap, k: string, val: int) requires s does not have a key k. modifies s effects s post maps k to val. lookup = proc (s: intmap, k: string) returns (int) requires There is a key k in s. effects Returns value associated with key in s. Rest of program sees black box described by specification.

20 March 2001CS 655: Lecture 159 Black-box Interface intmap down (intmap) returns (rep) rep = representation of intmap Only code in the cluster implementing intmap can call intmap$up or intmap$down. up (rep) returns (intmap) There is nothing else special about code in the cluster!

20 March 2001CS 655: Lecture 1510 Parameterized Data Abstractions Don’t want to implement stringintmap, stringrealmap, intintmap, etc. Value Parameters: –Don’t want to implement Factorial2 (), Factorial3 (), Factorial4 (),... –Implement Factorial (n: int) Type Parameters: –Implement map[tkey: type, tval: type] Problem: how will we implement lookup if we don’t know anything about tkey?

20 March 2001CS 655: Lecture 1511 Specification map = data type [tkey: type, tval: type] is create, insert, lookup Requires tkey has an operation equal: proctype (t, t) returns (bool) that is an equivalence relation on t. Operations create = proc () returns (map) effects Returns a new, empty map. insert = proc (s: map, k: tkey, val: tval) requires s has no key k’ such that tkey$equal (k, k’). modifies s effects lookup (s post. k) = val. lookup = proc (s: map, k: tkey) returns (tval) requires s has a key k’ in s such that tkey$equal (k, k’). effects Returns value associated with k in s.

20 March 2001CS 655: Lecture 1512 Where Clauses map = cluster [tkey: type, tval: type] is create, insert, lookup where tkey has equal: proctype (tkey, tkey) returns bool Used in implementation, not specification. Checked by compiler.

20 March 2001CS 655: Lecture 1513 Implementing Data Abstractions Need a concrete representation map = cluster [tkey: type, tval: type] is create, insert, lookup where tkey has equal: proctype (tkey, tkey) returns (bool) pair = record [key: tkey, value: tval] rep = array [pair] create = proc () returns (map) return end create up(rep$new ())

20 March 2001CS 655: Lecture 1514 Implementing map insert = proc (m: map, k: tkey, v: tval) % Better spec would remove requires % clause and signal exception if key % is already in the map. down (m).addh (pair${key: k, value: v}) end insert

20 March 2001CS 655: Lecture 1515 Printing maps map = cluster [tkey: type, tval] is... unparse... unparse = proc (m: map) returns (string) where tkey has unparse: proctype (tkey) returns (string) tval has unparse: proctype (tval) returns (string) Why put the where clause about equal on the cluster instead of member operation?

20 March 2001CS 655: Lecture 1516 CLU: Special Types bool –Language control structures (if, while) depend on type bool int, char, real, string, null –Built-in language support for literals record, struct, variant, oneof, array, sequence –Special constructor syntax T${ … } any –Union of all possible types, use force to convert (with checking) to actual type

20 March 2001CS 655: Lecture 1517 CLU Operators Assignment (:=) –Always means sharing (recall immutable types) –Types by name must match Everything else is syntactic sugar, all types can use:  int$add (3, 2) m1,m2: map[string,int] m1 + m2  map[string,int]$add (m1, m2) ai: array[int] ai[n] := ai[n-1]  array[int]$store (ai, n, array[int]$fetch (ai, n-1)) Four exceptions: up, down, cand, cor

Questions? Next: Reasoning About Data Abstractions

20 March 2001CS 655: Lecture 1519 Reasoning about Data Abstractions They are abstractions – need to invent a formal notation ( A ) for describing them They have representations – need to define a mapping from concrete representation to that formal notation Abstraction Function: A: rep  A

20 March 2001CS 655: Lecture 1520 Describing maps A map can be described by a sequence of (key, value) pairs with unique keys: [ (key 0, value 0 ), (key 1, value 1 ), … ] such that if key = key i the value associated with key is value i. A: rep  [(key 0, value 0 ), (key 1, value 1 ), …]

20 March 2001CS 655: Lecture 1521 Abstraction Function A: array [record [key: tkey, value: tval]]  [(key 0, value 0 ), (key 1, value 1 ), …] A(r) = [(r[rep$low(r)].key, r[rep$low(r)].value), (r[rep$low(r) + 1].key, r[rep$low(r)+1].value),... (r[rep$high(r)].key, r[rep$high(r)].value)] Problem: What if r contains duplicate keys?

20 March 2001CS 655: Lecture 1522 Rep Invariant “It better not!” I : rep  Boolean I (r) = (r[i].key = r[k].key implies i = k)

20 March 2001CS 655: Lecture 1523 Reasoning with Rep Invariants Prove by induction, for a datatype t: 1.For each operation that creates new t: prove that returned reps r of returned t satisfies I (r) 2.For each cluster operation: assume all t objects passed as parameters satisfy have reps r that satisfy I (r), prove they do at all cluster exit points. Argue that only cluster operations can alter the rep, so if you can prove invariant holds for all cluster operations, it must always hold.

20 March 2001CS 655: Lecture 1524 What can go wrong? map = cluster [tkey: type, tval: type] is... choose = proc (m: map) returns (record [key: tkey, value: tval]) % requires m is not empty. % effects Returns a (key, value) pair in m. return (down (m)[rep$low (down(m))] end choose p = proc (m: map[string, int]) map[string,int]$insert (m, “duplicate”, 3) pair p := map[string,int]$choose (m) p.key = “duplicate” end p

20 March 2001CS 655: Lecture 1525 Rep Exposure Can’t share mutable objects in data representations Sharing immutable objects is okay Could compiler prevent this? –Yes, pretty easy Why doesn’t CLU compiler prevent this? –Sometimes efficiency requires rep exposure –e.g., create a map by passing in an array of pairs

20 March 2001CS 655: Lecture 1526 The programming community must soon come to terms with the topics that they address, including: What are the qualitative and quantitave effects of strong type- checking? How do verification considerations affect language design? What abstraction mechanisms should languages provide? How can security of high-level languages be extended to real- time applications? Jim Horning, 1977 (from 6 March manifest)

20 March 2001CS 655: Lecture 1527 Charge Read Stroustrup and Smalltalk papers before class Thursday Think about what Object-Oriented Programming really means – could Stroustrup and Ingalls really be writing about the same thing? Is CLU Object-Oriented? If not, would adding syntactic sugar make CLU object- oriented?