topic 5
play

Topic 5 Reminder: primitive expressions, means of Data Abstraction - PDF document

Data abstraction Topic 5 Reminder: primitive expressions, means of Data Abstraction combination, means of abstraction Chapter 1 computational processes and role of procedures in program design. Combining procedures Note: This


  1. Data abstraction Topic 5 • Reminder: primitive expressions, means of Data Abstraction combination, means of abstraction • Chapter 1 – computational processes and role of procedures in program design. Combining procedures Note: This represents a change in order. We to form compound procedures, abstraction of are skipping to chapter 2 without finishing 1.3. procedures, procedures as a pattern for local We will pick up the 1.3 concepts as they are evolution of a process, algorithmic analysis. motivated here. Section 2.1 • Here look similar concepts for data: primitive data, compound data, data abstraction September 2008 Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques Why Compound Data? General Technique Isolate • Elevate conceptual level at which we can design our programs • parts of a program that deal with how data objects are represented • Increase modularity of our design From • Enhance expressive power of the language • Parts of program that deal with how objects are used • Example: Dealing with rational numbers e.g., ¾ This is a powerful design methodology called • Issue: has numerator and denominator data abstraction • Want to deal with them as a unit Note similarity with procedural abstraction! Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Let's pretend! Data abstraction • Methodology that combines many data objects so that • Pretend that Scheme only has integers and real they can be treated as one data object numbers, no rationals or complex numbers • The new data objects are abstract data: they are used • We will define our own implementation of rational without making any assumptions about how they are numbers and complex numbers implemented • Illustrates data abstraction, multiple representation • Data abstraction: define representation, hide with selectors and constructors • Extends the programming language Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1

  2. Language extensions for Rational numbers handliing absract data • Mathematically represented by a pair of integers: 1/2, 56/874, 78/23, etc. • Constructor: a procedure that creates instances • Constructor : of abstract data from data that is passed to it (make-rat numerator denominator ) ; creates a rational number given an • Selector: a procedure that returns a component ; integer numerator and denominator datum that is in an abstract data object • Selectors: (numer rn ) , (denom rn ) ; given a rational number returns an • The component datum returned might be the value of an internal variable, or it might be ; integer rerpresenting the numerator and computed ; denominator Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques User defines operations on Rational addition rational numbers! (define (add-rat x y) Case of wishful thinking. You can start (make-rat (+ (* (numer x) programming/thinking about programming up various (denom y)) operations without worrying about implementation of (* (numer y) rational numbers. Just assume (wish) the (denom x))) constructors and selectors work! (* (denom x) (denom y)))) Add: n 1 /d 1 + n 2 /d 2 = (n 1 * d 2 + n 2 * d 1 )/(d 1 * d 2 ) Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques Another operation Rational multiplication (define (mul-rat x y) Multiply: (make-rat (* (numer x) (n 1 /d 1 ) * (n 2 /d 2 ) = (n 1 * n 2 ) / (d 1 * d 2 ) (numer y)) (* (denom x) (denom y)))) Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2

  3. A test Rational equality (define (equal-rat? x y) Equality: (= (* (numer x) (denom y)) n 1 /d 1 = n 2 /d 2 iff n 1 * d 2 = n 2 * d 1 (* (numer y) (denom x)))) Subtraction and division defined similarly to addition and multiplication Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques OK – now ready to implement Compound data structure in rational numbers Scheme • Have written programs that use the constructor and • Called a pair selectors for rational numbers. • Constructor is cons – takes two arguments and • Now need to implement the concrete level of our data returns a compound data object with those two abstraction by defining these functions. arguments as parts. • To do so, we need an implementation of rational numbers. • Selectors are car and cdr (define x (cons 4 9)) • Need a way to glue together the numerator and (car x) --> 4 denominator into a single unit. (cdr x) --> 9 Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques Pairs as records with two fields Building a larger data structure (define x (cons 4 9)) produces (define y (cons 3 2)) (define z (cons x y)) z y X x 4 9 4 9 3 2 (4 . 9) ((4 . 9) 3 . 2) Fall 2008 Programming Development 17 Fall 2008 Programming Development 18 Techniques Techniques 3

  4. Extracting data List structures (car (car z)) --> 4 Any data structure built using cons (car (cdr z)) --> 3 (cdr (car z)) --> 9 Lists are a subset of the possible list structures (cdr (cdr z)) --> 2 None of the list structures on the last three slides are lists Fall 2008 Programming Development 19 Fall 2008 Programming Development 20 Techniques Techniques Representing rational numbers Using rational numbers the implementation (define (make-rat n d) (cons n d)) (define one-third (make-rat 1 3)) (define (numer x) (car x)) (define four-fifths (make-rat 4 5)) (define (denom x) (cdr x)) (print-rat one-third) (define (print-rat x) 1/3 (display (numer x)) (display "/") (print-rat (add-rat one-third (display (denom x)) four-fifths)) (newline)) 17/15 Fall 2008 Programming Development 21 Fall 2008 Programming Development 22 Techniques Techniques To get the standard Some more rational numbers representation (print-rat (mul-rat one-third four-fifths)) (define (make-rat n d) 4/15 (let ((g (gcd n d))) (print-rat (add-rat four-fifths (cons (/ n g) (/ d g)))) four-fifths)) 40/25 (print-rat (add-rat four-fifths four-fifths)) 8/5 Fall 2008 Programming Development 23 Fall 2008 Programming Development 24 Techniques Techniques 4

  5. Levels of abstraction Bottom level • Programs are built up as layers of language • level of pairs extensions • Each layer is a level of abstraction • procedures cons , car and cdr are already • Each level hides some implementation details provided in the programming language • There are four levels of abstraction in our rational numbers example • The actual implementation of pairs is hidden Fall 2008 Programming Development 25 Fall 2008 Programming Development 26 Techniques Techniques Second level Third level • Level of rational numbers as data objects • Level of service procedures on rational numbers • Procedures make-rat , numer and denom are • Procedures add-rat , mul-rat , equal-rat , etc. defined at this level are defined at this level • Actual implementation of rational numbers is • I mplementation of these procedures are hidden hidden at this level at this level Fall 2008 Programming Development 27 Fall 2008 Programming Development 28 Techniques Techniques Top level Abstraction barriers • Program level • Each level is designed to hide implementation details from higher-level procedures • Rational numbers are used in calculations as if they • These levels act as abstraction barriers were ordinary numbers Fall 2008 Programming Development 29 Fall 2008 Programming Development 30 Techniques Techniques 5

  6. Example of changing an Advantages of data abstraction implementation • Programs can be designed one level of abstraction at a time (define (make-rat n d) (cons n d)) • We don't have to be aware of implementation details below the level at which we are programming (define (numer x) (let ((g (gcd (car x) (cdr x)))) • This means there is less to keep in mind at any one time while programming (/ (car x) g))) • An implementation can be changed later without changing procedures written at higher levels (define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g))) Fall 2008 Programming Development 31 Fall 2008 Programming Development 32 Techniques Techniques Another advantage Message passing paradigm • Data abstraction supports top-down design • A way of using procedure abstraction to implement data abstraction • A procedure is used to represent an object • We can gradually figure out representations, constructors, selectors and service procedures that we • A higher-order procedure is used to act as a need, one level at a time constructor • A message is passed to the object (value passed as input to the procedure) to act as a selector Fall 2008 Programming Development 33 Fall 2008 Programming Development 34 Techniques Techniques How pairs could be implemented (Return a procedure from a Procedure) • Implementing the selectors requires using procedures as arguments – something we didn’t (define (cons x y) cover yet from section 1.3… (define (dispatch message) (cond ((= message 0) x) ((= message 1) y) (else (error "bad message" message)))) dispatch) Fall 2008 Programming Development 35 Fall 2008 Programming Development 36 Techniques Techniques 6

Recommend


More recommend