1 6.001 SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –Functions Today we will see two more: –symbols –pairs.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
1 Lecture 15: More about assignment and the Environment Model (EM)
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
1 Lecture 16: Lists and vectors Binary search, Sorting.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Information Retrieval: aka “Google-lite” CMSC November 27, 2006.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
1 Data abstraction, revisited Design tradeoffs: Speed vs robustness modularity ease of maintenance Table abstract data type: 3 versions No implementation.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Abstract Syntax cs7100 (Prasad) L7AST.
Additional Scheme examples
Functional Programming
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 Jeopardy.
6.001 SICP Object Oriented Programming
Introduction to Scheme
6.001: Structure and Interpretation of Computer Programs
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Closures and Streams cs784(Prasad) L11Clos
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
The Metacircular Evaluator
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
6.001 SICP Data abstractions
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily "best" Abstract data types are a technique for information hiding in the types as well as in the code CONCRETE ABSTRACT CONCEPT

2 Table: a set of bindings binding: a pairing of a key and a value Abstract interface to a table: make create a new table put! key value insert a new binding replaces any previous binding of that key get key look up the key, return the corresponding value This definition IS the table abstract data type Code shown later is an implementation of the ADT

3 Examples of using tables Fred John Bill People Age Age Job Pay 34

4 Traditional LISP structure: association list A list where each element is a list of the key and value. 15 x 20 y x: 15 y: 20 Represent the table as the alist: ((x 15) (y 20))

5 Alist operation: find-assoc (define (find-assoc key alist) (cond ((null? alist) #f) ((equal? key (caar alist)) (cadar alist)) (else (find-assoc key (cdr alist))))) (define a1 '((x 15) (y 20))) (find-assoc 'y a1) ==> 20

6 Alist operation: add-assoc (define (add-assoc key val alist) (cons (list key val) alist)) (define a2 (add-assoc 'y 10 a1)) a2 ==> ((y 10) (x 15) (y 20)) (find-assoc 'y a2) ==> 10

7 Alists are not an abstract data type Missing a constructor: Use quote or list to construct (define a1 '((x 15) (y 20))) There is no abstraction barrier: Definition in scheme language manual: "An alist is a list of pairs, each of which is called an association. The car of an association is called the key." Therefore, the implementation is exposed. User may operate on alists using list operations. (filter (lambda (a) ( ((x 15))

8 Why do we care that Alists are not an ADT? Modularity is essential for software engineering Build a program by sticking modules together Can change one module without affecting the rest Alists have poor modularity Programs may use list ops like filter and map on alists These ops will fail if the implementation of alists change Must change whole program if you want a different table To achieve modularity, hide information Hide the fact that the table is implemented as a list Do not allow rest of program to use list operations ADT techniques exist in order to do this

9 Table1: Table ADT implemented as an Alist (define table1-tag 'table1) (define (make-table1) (cons table1-tag nil)) (define (table1-get tbl key) (find-assoc key (cdr tbl))) (define (table1-put! tbl key val) (set-cdr! tbl (add-assoc key val (cdr tbl))))

10 Table1 example (define tt1 (make-table1)) (table1-put! tt1 'y 20) (table1-put! tt1 'x 15) tt1 table1 20 y 15 x (table1-get tt1 ‘y)

11 How do we know Table1 is an ADT implementation Potential reasons: Because it has a type tagNo Because it has a constructorNo Because it has mutators and accessorsNo Actual reason: Because the rest of the program does not apply any functions to Table1 objects other than the functions specified in the Table ADT For example, no car, cdr, map, filter done to tables The implementation (as an Alist) is hidden from the rest of the program, so it can be changed easily

12 Information hiding in types: opaque names Opaque: type name that is defined but unspecified Given functions m1 and m2 and unspecified type MyType: (define (m1 number)...) ; number  MyType (define (m2 myt)...) ; MyType  undef Which of the following is OK? Which is a type mismatch? (m2 (m1 10)); return type of m1 matches ; argument type of m2 (car (m1 10)); return type of m1 fails to match ; argument type of car ; car: pair  A Effect of an opaque name: no functions will match except the functions of the ADT

13 Types for table1 Here is everything the rest of the program knows Table1 opaque type make-table1 void  Table1 table1-put! Table1, k, v  undef table1-get Table1, k  (v | null) Here is the hidden part, only the implementation knows it: Table1 = symbol  Alist Alist = list

14 Lessons so far Association list structure can represent the table ADT The data abstraction technique (constructors, accessors, etc) exists to support information hiding Information hiding is necessary for modularity Modularity is essential for software engineering Opaque type names denote information hiding

15 Hash tables Suppose a program is written using Table1 Suppose we measure that a lot of time is spent in table1-get Want to replace the implementation with a faster one Standard data structure for fast table lookup: hash table Idea: keep N association lists instead of 1 choose which list to search using a hash function –given the key, hash function computes a number x where 0 <= x <= (N-1)

16 Example hash function A table where the keys are points pointgraphic object (5,5)(circle 4) (10,6)(square 8) (define (hash-a-point point N) (modulo (+ (x-coor point) (y-coor point)) N)) ; modulo x n = the remainder of x  n ; 0 <= (modulo x n) <= n-1 for any x

17 Hash function output chooses a bucket key Association list hash function index buckets N-1 If a key is in the table, it is in the Alist of the bucket whose index is hash(key)

18 Store buckets using the vector ADT Vector: fixed size collection with indexed access vector opaque type make-vector number, A  vector vector-ref vector, number  A vector-set! vector,number, A  undef (make-vector size value) ==> a vector with size locations; each initially contains value (vector-ref v index) ==> whatever is stored at that index of v (error if index >= size of v) (vector-set! v index val) stores val at that index of v (error if index >= size of v)

19 Table2: Table ADT implemented as hash table (define t2-tag 'table2) (define (make-table2 size hashfunc) (let ((buckets (make-vector size nil))) (list t2-tag size hashfunc buckets))) (define (size-of tbl) (cadr tbl)) (define (hashfunc-of tbl) (caddr tbl)) (define (buckets-of tbl) (cadddr tbl)) For each function defined on this slide, is it a constructor of the data abstraction? an accessor of the data abstraction? an operation of the data abstraction? none of the above?

20 get in table2 (define (table2-get tbl key) (let ((index ((hashfunc-of tbl) key (size-of tbl)))) (find-assoc key (vector-ref (buckets-of tbl) index)))) Same type as table1-get

21 put! in table2 (define (table2-put! tbl key val) (let ((index ((hashfunc-of tbl) key (size-of tbl))) (buckets (buckets-of tbl))) (vector-set! buckets index (add-assoc key val (vector-ref buckets index))))) Same type as table1-put!

22 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) tt2 table2 4 vector 15 point 5,7 20 point 5,5 (table2-get tt2 (make-point 5 5))

23 Is Table1 or Table2 better? Answer: it depends! Table1:makeextremely fast put!extremely fast getO(n) where n=# calls to put! Table2:makespace N where N=specified size put!must compute hash function getcompute hash function plus O(n) where n=average length of a bucket Table1 better if almost no gets or if table is small Table2 challenges: predicting size, choosing a hash function that spreads keys evenly to the buckets

24 End of lecture Introduced three useful data structures association lists vectors hash tables Operations not listed in the ADT specification are internal The goal of the ADT methodology is to hide information Information hiding is denoted by opaque type names

25 (define (add-assoc key val alist) (cons (list key val) alist)) (define (add-assoc key val alist) (cons (list key val) alist)) (define table1-tag 'table1) (define (make-table1) (cons table1-tag nil)) (define (table1-get tbl key) (find-assoc key (cdr tbl))) (define (table1-put! tbl key val) (set-cdr! tbl (add-assoc key val (cdr tbl))))

26 (define (make-table2 size hashfunc) (let ((buckets (make-vector size nil))) (list t2-tag size hashfunc buckets))) (define (table2-get tbl key) (let ((index ((hashfunc-of tbl) key (size-of tbl)))) (find-assoc key (vector-ref (buckets-of tbl) index)))) (define (table2-put! tbl key val) (let ((index ((hashfunc-of tbl) key (size-of tbl))) (buckets (buckets-of tbl))) (vector-set! buckets index (add-assoc key val (vector-ref buckets index)))))