Data abstraction, revisited

Similar documents
6.001: Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Programming

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Important Example: Gene Sequence Matching. Corrigiendum. Central Dogma of Modern Biology. Genetics. How Nucleotides code for Amino Acids

Vectors in Scheme. Data Structures in Scheme

An Explicit-Continuation Metacircular Evaluator

CSc 520 Principles of Programming Languages

CS61A Midterm 2 Review (v1.1)

Project 2: Scheme Interpreter

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

A Brief Introduction to Scheme (II)

Discussion 12 The MCE (solutions)

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Introduction to Scheme

CS 275 Name Final Exam Solutions December 16, 2016

SOFTWARE ARCHITECTURE 6. LISP

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Administrivia. Simple data types

Turtles All The Way Down


CSC 533: Programming Languages. Spring 2015

Essentials of Programming Languages Language

Essentials of Programming Languages Language

Typed Racket: Racket with Static Types

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

ALISP interpreter in Awk

6.001 recitation 3/21/07

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Scheme Quick Reference

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Discussion 4. Data Abstraction and Sequences

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

User-defined Functions. Conditional Expressions in Scheme

Representing Images as Lists of Spots

UMBC CMSC 331 Final Exam

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Scheme Quick Reference

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

University of Massachusetts Lowell

Building a system for symbolic differentiation

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Lisp Basic Example Test Questions

Functional Programming

Documentation for LISP in BASIC

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

SCHEME AND CALCULATOR 5b

Lexical vs. Dynamic Scope

Building a system for symbolic differentiation

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

Simplifying Logical Formulas

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

FP Foundations, Scheme

Functional Programming. Pure Functional Languages

Midterm Examination (Sample Solutions), Cmput 325

(scheme-1) has lambda but NOT define

From Syntactic Sugar to the Syntactic Meth Lab:

Intro. Scheme Basics. scm> 5 5. scm>

Sample Final Exam Questions

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

6.001 Notes: Section 8.1

A LISP Interpreter in ML

The Typed Racket Guide

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

A Genetic Algorithm Implementation

Lecture Notes on Lisp A Brief Introduction

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Programming Languages

INTRODUCTION TO SCHEME

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

CS 314 Principles of Programming Languages

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

Functional Programming. Pure Functional Languages

LECTURE 16. Functional Programming

Principles of Programming Languages

Functional Languages. Hwansoo Han

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Review of Functional Programming

CS 61A Midterm #2 - October 5, 1998

Announcements. Today s Menu

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

CSCI337 Organisation of Programming Languages LISP

Transcription:

Data abstraction, revisited Design tradeoffs: Speed vs robustness modularity ease of maintenance Table abstract data type: 3 versions No implementation of an ADT is necessarily "best" Abstract data types hide information, in types as well as in the code 1

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 a particular implementation of the ADT 2

Examples of using tables People Fred John. Bill Age Job Pay 34 2000 1999 1998 Age 34 48. Values associated with keys might be data structures Values might be shared by multiple structures 3

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

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 x 15 y 20 5

An aside on testing equality = Eq? Equal? tests equality of numbers Tests equality of symbols Tests equality of symbols, numbers or lists of symbols and/or numbers that print the same 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 We say that the new binding for y shadows the previous one 7

Alists are not an abstract data type Missing a constructor: Used quote or list to construct (define a1 '((x 15) (y 20))) There is no abstraction barrier: the implementation is exposed. User may operate on alists using standard list operations. (filter (lambda (a) (< (cadr a) 16)) a1)) ==> ((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) (table1-get tt1 y) tt1 (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))))) table1 x 15 y 20 14

How do we know Table1 is an ADT implementation Potential reasons: Because it has a type tag Because it has a constructor Because it has mutators and accessors No No 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 easily 15

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,b> A Effect of an opaque name: no functions have the correct types except the functions of the ADT 16

Types for table1 Here is everything the rest of the program knows Table1<k,v> make-table1 table1-put! table1-get opaque type void Table1<anytype,anytype> Table1<k,v>, k, v undef Table1<k,v>, k (v nil) Here is the hidden part, only the implementation knows it: Table1<k,v> = symbol Alist<k,v> Alist<k,v> = list< k v > 17

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 18

Now let's talk about efficiency Speed of operations put get Fast Slow What if it's the Boston Yellow Pages? Really need to use other information to get to right place to search 19

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) Speed of hash table? 20

What s a hash function? Maps an input to a fixed length output (e.g. integer between 0 and N) Ideally the set of inputs is uniformly distributed over the output range Ideally the function is very rapid to compute Example: First letter of last name: 26 buckets Non-uniform Convert last name by position in alphabet, add, take modular arithmetic GRIMSON: 7+18+9+13+19+15+14 = 95 (mod 26 = 17) GREEN: 7+18+5+5+14=49 (mod 26 = 23) Uses: Fast storage and retrieval of data Hash functions that are hard to invert are very valuable in cryptography 21

Hash function output chooses a bucket key 0 Search in alist using normal operations hash function index 1 2 3... Association list Association list If a key is in the table, it is in the Alist of the bucket whose index is hash(key) N-1 buckets Association list 22

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

The Bucket Abstraction (define (make-buckets N v) (make-vector N v)) (define make-buckets make-vector) (define bucket-ref vector-ref) (define bucket-set! vector-set!) 24

Table2: Table ADT implemented as hash table (define t2-tag 'table2) (define (make-table2 size hashfunc) (let ((buckets (make-buckets 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? 25

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

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

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

Is Table1 or Table2 better? Answer: it depends! Table1: make put! get extremely fast extremely fast O(n) where n=# calls to put! Table2: make put! get space N where N=specified size must compute hash function compute 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 29

Summary 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 30