Lecture 7: Data Abstraction

Slides:



Advertisements
Similar presentations
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Advertisements

Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
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.
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
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
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.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
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.
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.
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.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
(Thunking about Thunks)
EECE 310: Software Engineering
Lecture 4: Metacircles Eval Apply David Evans
Principles of programming languages 10: Object oriented languages
Principles of programming languages 12: Functional programming
Lecture 6: Lambda Calculus
Week 7 - Friday CS221.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Class 22: Inheritance CS150: Computer Science University of Virginia
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Review: Two Programming Paradigms
Type Definitions cs776 (prasad) L8tdef.
Lambda Calculus Revisited
11.1 The Concept of Abstraction
Lists in Lisp and Scheme
Programming Language Concepts (CIS 635)
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Lecture 9 Concepts of Programming Languages
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 8: SmallTalking about Objects
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 9: Exceptions in Java CS201j: Engineering Software
Data Abstraction David Evans cs205: engineering software
Lecture 4: Data Abstraction CS201j: Engineering Software
Lecture 21: Crosscutting Aspect-Oriented Programming Background
Lecture 10: The Return of Paco Background just got here last week
Lecture 10: Using Object-Oriented Languages
6.001 SICP Variations on a Scheme
Lecture 19: Proof-Carrying Code Background just got here last week
Compiler Construction
Suggested self-checks: Section 7.11 #1-11
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
Lab4 problems More about templates Some STL
CS2013 Lecture 7 John Hurley Cal State LA.
David Evans Lecture 19: ||ism I don’t think we have found the right programming concepts for parallel computers yet.
Lecture 15: Crazy Eddie and the Fixed Points Background
Lists CMSC 202, Version 4/02.
CMPE212 – Reminders Assignment 2 due next Friday.
Group 4: Song Li, Ying Lu, Hexin Wang, and Michael Walker May 1, 2000
Compiler Construction
Reasoning about Data Abstractions
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Week 7 - Monday CS 121.
Abstract Types Defined as Classes of Variables
Week 9 - Monday CS222.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Lecture 7: Data Abstraction Background just got here last week finished degree at MIT week before Philosophy of advising students don’t come to grad school to implement someone else’s idea can get paid more to do that in industry learn to be a researcher important part of that is deciding what problems and ideas are worth spending time on grad students should have their own project looking for students who can come up with their own ideas for research will take good students interested in things I’m interested in – systems, programming languages & compilers, security rest of talk – give you a flavor of the kinds of things I am interested in meant to give you ideas (hopefully even inspiration!) but not meant to suggest what you should work on It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis

University of Virginia CS 655 Menu Data Abstraction before CLU Data Abstraction in CLU Reasoning about Data Abstractions Abstraction Functions Rep Invariants 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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 17 January 2019 University of Virginia CS 655

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. 17 January 2019 University of Virginia CS 655

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 17 January 2019 University of Virginia CS 655

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 17 January 2019 University of Virginia CS 655

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 friends to access representation LCLint: in files and functions according to a naming convention, elsewhere when explicitly annotated [Stata97]: operations can access the only some of the representation 17 January 2019 University of Virginia CS 655

Data Abstraction in CLU Rest of program sees black box described by specification. 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 spost 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. 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Black-box Interface intmap down (intmap) returns (rep) up (rep) returns (intmap) rep = representation of intmap Only code in the cluster implementing intmap can call intmap$up or intmap$down. There is nothing else special about code in the cluster! 17 January 2019 University of Virginia CS 655

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? 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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 (spost. 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. 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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. 17 January 2019 University of Virginia CS 655

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 () ) 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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? 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 CLU Operators Assignment (:=) Always means sharing (recall immutable types) Types by name must match Everything else is syntactic sugar, all types can use: 3 + 2  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 17 January 2019 University of Virginia CS 655

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 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Describing maps A map can be described by a sequence of (key, value) pairs with unique keys: [ (key0, value0), (key1, value1), … ] such that if key = keyi the value associated with key is valuei . A: rep  [(key0, value0), (key1, value1), …] 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Abstraction Function A: array [record [key: tkey, value: tval]]  [(key0, value0), (key1, value1), …] 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? 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Rep Invariant “It better not!” I: rep  Boolean I(r) = (r[i].key = r[k].key implies i = k) 17 January 2019 University of Virginia CS 655

Reasoning with Rep Invariants Prove by induction, for a datatype t: For each operation that creates new t: prove that returned reps r of returned t satisfies I(r) 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. 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 What can go wrong? map = cluster [tkey: type, tval: type] is ... choose = proc (m: map) returns (pair) % 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 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 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 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Next Time Would adding syntactic sugar make CLU object-oriented? x.operation (args)  typeof (x)$operation (x, args) e.g.: m.insert (s, v)  map[string, int]$insert (m, s, v) 17 January 2019 University of Virginia CS 655

University of Virginia CS 655 Charge Read Stroustrup paper before class Thursday Project Proposals by midnight tomorrow Be ready to give elevator speeches in class Thursday 17 January 2019 University of Virginia CS 655