DOC PREVIEW
MIT 6 001 - Data abstraction

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.001 SICP Data abstraction revisitedTable: a set of bindingsExamples of using tablesTraditional LISP structure: association listAlist operation: find-assocAn aside on testing equalityAlist operation: add-assocAlists are not an abstract data typeWhy do we care that Alists are not an ADT?Table1: Table ADT implemented as an AlistCompound DataExample 1: Pair/List MutationExample 2: Pair/List MutationTable1 exampleHow do we know Table1 is an ADT implementationInformation hiding in types: opaque namesTypes for table1Lessons so farHash tablesExample hash functionHash function output chooses a bucketStore buckets using the vector ADTTable2: Table ADT implemented as hash tableget in table2put! in table2Table2 exampleIs Table1 or Table2 better?SummarySlide 29Slide 3016.001 SICPData 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 codeCONCRETEABSTRACTCONCEPT2Table: 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 valueinsert a new bindingreplaces any previous binding of that key•get keylook up the key, return the corresponding value •This definition IS the table abstract data type•Code shown later is a particular implementation of the ADT3Examples of using tablesFredJohnBillPeople3448Age200019991998AgeJobPay34Values associated with keys might be data structures..Values might be shared by multiple structures4Traditional LISP structure: association list•A list where each element is a list of the key and value.15x20yx: 15y: 20 • Represent the table as the alist: ((x 15) (y 20))5Alist 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) ==> 2015x20y6An aside on testing equality•= tests equality of numbers•Eq? Tests equality of symbols• As will see, also tests equality of list structures•Equal? Tests equality of symbols, numbers or lists of symbols and/or numbers that print the same•Eqv? Tests equality of list as actual structures, not just prints the same7Alist 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) ==> 10We say that the new binding for y “shadows” the previous one – you can see how the find-assoc procedure does this8Alists 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 anassociation. 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) (< (cadr a) 16)) a1)) ==> ((x 15))9Why 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 this10Table1: 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))))11Compound Data•constructor: (cons x y) creates a new pair p•selectors: (car p) returns car part of pair (cdr p) returns cdr part of pair•mutators: (set-car! p new-x) changes car pointer in pair (set-cdr! p new-y) changes cdr pointer in pair ; Pair,anytype -> undef -- side-effect only!12Example 1: Pair/List Mutation(define a (list 1 2))(define b a)a  (1 2)b  (1 2)1 2ba(set-car! a 10)b ==> (10 2)10XCompare with:(define a (list 1 2))(define b (list 1 2))1 2a1 2b10X(set-car! a 10)b  (1 2)13Example 2: Pair/List Mutation(define x (list 'a 'b))a bxX21(set-car! (cdr x) (list 1 2))1. Eval (cdr x) to get a pair object2. Change car pointer of that pair object•How mutate to achieve the result at right?14Table1 example(define tt1 (make-table1))(table1-put! tt1 'y 20)(table1-put! tt1 'x 15)tt1table115x(table1-get tt1 ‘y)(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))))(define (add-assoc key val alist) (cons (list key val) alist))(define (find-assoc key alist) (cond ((null? alist) #f) ((equal? key (caar alist)) (cadar alist)) (else (find-assoc key (cdr alist)))))20y15How do we know Table1 is an ADT implementation•Potential reasons:•Because it has a type tag No•Because it has a constructorNo•Because it has mutators and accessors No•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 easily16Information 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,B>  A•Effect of an opaque name: no functions will match except the functions of the ADT17Types for table1•Here is everything the rest of the program knowsTable1<k,v> opaque typemake-table1 void  Table1<anytype,anytype>table1-put! Table1<k,v>, k, v  undeftable1-get Table1<k,v>, k  (v | null)•Here is the hidden part, only the implementation knows


View Full Document

MIT 6 001 - Data abstraction

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Data abstraction
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data abstraction and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data abstraction 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?