Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.

Slides:



Advertisements
Similar presentations
Fall 2008Programming Development Techniques 1 Topic 12 Multiple Representations of Abstract Data – Complex Numbers Section
Advertisements

מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 2: Structured.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Any questions on the Section 6.2 homework?. Please CLOSE YOUR LAPTOPS, and turn off and put away your cell phones, and get out your note- taking materials.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
1.4 Absolute Values Solving Absolute Value Equations By putting into one of 3 categories.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 Extending the Number System.
1 Topic Multiplying Rational Expressions Multiplying Rational Expressions.
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Warm-up: Here is an example to help you do this warm-up.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Data Structures Using C++ 2E
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Forms Writing your own procedures CS 480/680 – Comparative Languages.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Functional Programming
Functional Programming
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Surds Simplifying a Surd Rationalising a Surd.
6.001 SICP Data abstractions
Higher-Order Procedures
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Algorithm Discovery and Design
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
Mutators for compound data Stack Queue
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
Today’s topics Abstractions Procedural Data
Rational Expressions and Equations
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing 1.3. We will pick up the 1.3 concepts as they are motivated here. Section 2.1 September 2008

Fall 2008Programming Development Techniques 2 Data abstraction Reminder: primitive expressions, means of combination, means of abstraction Chapter 1 – computational processes and role of procedures in program design. Combining procedures to form compound procedures, abstraction of procedures, procedures as a pattern for local evolution of a process, algorithmic analysis. Here look similar concepts for data: primitive data, compound data, data abstraction

Fall 2008Programming Development Techniques 3 Why Compound Data? Elevate conceptual level at which we can design our programs Increase modularity of our design Enhance expressive power of the language Example: Dealing with rational numbers e.g., ¾ Issue: has numerator and denominator Want to deal with them as a unit

Fall 2008Programming Development Techniques 4 General Technique Isolate parts of a program that deal with how data objects are represented From Parts of program that deal with how objects are used This is a powerful design methodology called data abstraction Note similarity with procedural abstraction!

Fall 2008Programming Development Techniques 5 Let's pretend! Pretend that Scheme only has integers and real numbers, no rationals or complex numbers We will define our own implementation of rational numbers and complex numbers Illustrates data abstraction, multiple representation

Fall 2008Programming Development Techniques 6 Data abstraction Methodology that combines many data objects so that they can be treated as one data object The new data objects are abstract data: they are used without making any assumptions about how they are implemented Data abstraction: define representation, hide with selectors and constructors Extends the programming language

Fall 2008Programming Development Techniques 7 Language extensions for handliing absract data Constructor: a procedure that creates instances of abstract data from data that is passed to it Selector: a procedure that returns a component datum that is in an abstract data object The component datum returned might be the value of an internal variable, or it might be computed

Fall 2008Programming Development Techniques 8 Rational numbers Mathematically represented by a pair of integers: 1/2, 56/874, 78/23, etc. Constructor: (make-rat numerator denominator ) ; creates a rational number given an ; integer numerator and denominator Selectors: (numer rn ), (denom rn ) ; given a rational number returns an ; integer rerpresenting the numerator and ; denominator

Fall 2008Programming Development Techniques 9 User defines operations on rational numbers! Case of wishful thinking. You can start programming/thinking about programming up various operations without worrying about implementation of rational numbers. Just assume (wish) the constructors and selectors work! Add: n 1 /d 1 + n 2 /d 2 = (n 1 *d 2 + n 2 *d 1 )/(d 1 *d 2 )

Fall 2008Programming Development Techniques 10 Rational addition (define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))

Fall 2008Programming Development Techniques 11 Another operation Multiply: (n 1 /d 1 ) * (n 2 /d 2 ) = (n 1 *n 2 ) / (d 1 *d 2 )

Fall 2008Programming Development Techniques 12 Rational multiplication (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y))))

Fall 2008Programming Development Techniques 13 A test Equality: n 1 /d 1 = n 2 /d 2 iff n 1 *d 2 = n 2 *d 1

Fall 2008Programming Development Techniques 14 Rational equality (define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x)))) Subtraction and division defined similarly to addition and multiplication

Fall 2008Programming Development Techniques 15 OK – now ready to implement rational numbers Have written programs that use the constructor and selectors for rational numbers. Now need to implement the concrete level of our data abstraction by defining these functions. To do so, we need an implementation of rational numbers. Need a way to glue together the numerator and denominator into a single unit.

Fall 2008Programming Development Techniques 16 Compound data structure in Scheme Called a pair Constructor is cons – takes two arguments and returns a compound data object with those two arguments as parts. Selectors are car and cdr (define x (cons 4 9)) (car x) --> 4 (cdr x) --> 9

Fall 2008Programming Development Techniques 17 Pairs as records with two fields (define x (cons 4 9)) produces (4. 9) 49 X

Fall 2008Programming Development Techniques 18 Building a larger data structure (define y (cons 3 2)) (define z (cons x y)) z y x ((4. 9) 3. 2)

Fall 2008Programming Development Techniques 19 Extracting data (car (car z)) --> 4 (car (cdr z)) --> 3 (cdr (car z)) --> 9 (cdr (cdr z)) --> 2

Fall 2008Programming Development Techniques 20 List structures Any data structure built using cons Lists are a subset of the possible list structures None of the list structures on the last three slides are lists

Fall 2008Programming Development Techniques 21 Representing rational numbers the implementation (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (print-rat x) (display (numer x)) (display "/") (display (denom x)) (newline))

Fall 2008Programming Development Techniques 22 Using rational numbers (define one-third (make-rat 1 3)) (define four-fifths (make-rat 4 5)) (print-rat one-third) 1/3 (print-rat (add-rat one-third four-fifths)) 17/15

Fall 2008Programming Development Techniques 23 Some more rational numbers (print-rat (mul-rat one-third four-fifths)) 4/15 (print-rat (add-rat four-fifths four-fifths)) 40/25

Fall 2008Programming Development Techniques 24 To get the standard representation (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) (print-rat (add-rat four-fifths four-fifths)) 8/5

Fall 2008Programming Development Techniques 25 Levels of abstraction Programs are built up as layers of language extensions Each layer is a level of abstraction Each level hides some implementation details There are four levels of abstraction in our rational numbers example

Fall 2008Programming Development Techniques 26 Bottom level level of pairs procedures cons, car and cdr are already provided in the programming language The actual implementation of pairs is hidden

Fall 2008Programming Development Techniques 27 Second level Level of rational numbers as data objects Procedures make-rat, numer and denom are defined at this level Actual implementation of rational numbers is hidden at this level

Fall 2008Programming Development Techniques 28 Third level Level of service procedures on rational numbers Procedures add-rat, mul-rat, equal-rat, etc. are defined at this level Implementation of these procedures are hidden at this level

Fall 2008Programming Development Techniques 29 Top level Program level Rational numbers are used in calculations as if they were ordinary numbers

Fall 2008Programming Development Techniques 30 Abstraction barriers Each level is designed to hide implementation details from higher-level procedures These levels act as abstraction barriers

Fall 2008Programming Development Techniques 31 Advantages of data abstraction Programs can be designed one level of abstraction at a time We don't have to be aware of implementation details below the level at which we are programming This means there is less to keep in mind at any one time while programming An implementation can be changed later without changing procedures written at higher levels

Fall 2008Programming Development Techniques 32 Example of changing an implementation (define (make-rat n d) (cons n d)) (define (numer x) (let ((g (gcd (car x) (cdr x)))) (/ (car x) g))) (define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g)))

Fall 2008Programming Development Techniques 33 Another advantage Data abstraction supports top-down design We can gradually figure out representations, constructors, selectors and service procedures that we need, one level at a time

Fall 2008Programming Development Techniques 34 Message passing paradigm A way of using procedure abstraction to implement data abstraction A procedure is used to represent an object A higher-order procedure is used to act as a constructor A message is passed to the object (value passed as input to the procedure) to act as a selector

Fall 2008Programming Development Techniques 35 How pairs could be implemented (Return a procedure from a Procedure) (define (cons x y) (define (dispatch message) (cond ((= message 0) x) ((= message 1) y) (else (error "bad message" message)))) dispatch)

Fall 2008Programming Development Techniques 36 Implementing the selectors requires using procedures as arguments – something we didn’t cover yet from section 1.3…

Fall 2008Programming Development Techniques 37 Implementing the selectors (define (car z) (z 0)) (define (cdr z) (z 1)) ("Don't try this at home!")

Fall 2008Programming Development Techniques 38 Alternate version of cons (define (cons x y) (lambda (message) (cond ((= message 0) x) ((= message 1) y) (else (error "bad message" message)))))