Message Passing vs Tagged Data
eharetea

Message passing is the underlying theme of object oriented programming. Data directed programming uses tagged-data.

Message passing is the underlying theme of object oriented programming. Data directed programming uses tagged-data.

Date Created:Friday October 10th, 2008 07:55 PM
Date Modified:Tuesday January 19th, 2010 10:38 PM

D A T A - D I R E C T E D
Data Directed: INTELLIGENT OPERATIONS DISPATCH ON DIFFERENT TAGGED DATA There is a table, and there are intelligent operations that know how to dispatch on different representations of data

ADDING DATA-TYPES To add new data types, you must add a new entry in to

ADDING OPERATION To add a new operation for n data types (i.e. adding get-angle to polar and rectangular representations), you must add n put commands.

(put op type item) ADDING get-angle to the 2 types
(put 'get-angle 'angle-polar (lambda (x) ... ))
(put 'get-angle 'rectg-polar (lambda (x) ... ))



M E S S A G E - P A S S I N G Message Passing: INTELLIGENT OBJECTS ARE THE DATA There are intelligent objects that are data representations/types that know how to dispatch or deal with different operations

ADDING DATA-TYPES So for two different programmers to code different representations of data (i.e. a new column), a new procedure, the intelligent object has to be created.

ADDING OPERATIONS You must add a new clause within each object that would need this operation
;; MESSAGE PASSING PARADIGM


;; FIRST CREATE INTELLIGENT DATA OBJECT(S)

;; message passing data type with contained operatoins:


(define (angle-degree value)
   (lambda (op)
     (cond ((eq? op 'rad) (* (/ 3.14159265 180) value))
           ((eq? op 'deg) value)
           (else (print "What the?")))))

(define (angle-radian value)
   (lambda (op)
     (cond ((eq? op 'rad) value)
           ((eq? op 'deg) (* (/ 180 3.14159265) value))
           (else (print "what the?")))))

;; IF NEEDED, create this arbitrary procedure

(define (apply-generic op arg) (arg op))

;; DEFINE AN "INSTANCE" OF THE DATA TYPE

(define a1 (angle-degree 30))
;; TWO METHODS FOR CALLING

;; (a1 'rad)

;; (apply-generic 'rad a1)


;; CREATE ABSTRACTION FOR apply-generic

;; angle in this context is what knows about the data-representation inherent within it

(define (radian angle)
   (apply-generic 'rad angle))
(define (degree angle)
   (apply-generic 'deg angle))

;; END MESSAGE PASSING



            

;; DATA DIRECTED VERSION


(define (attach-tag type-tag contents)
   (cons type-tag contents)
)

(define (type-tag datum)
   (if (pair? datum)
       (car datum)
       (error "Bad datum -- TYPE-TAG" datum)
   )
)

(define (contents datum)
   (if (pair? datum)
       (cdr datum)
       (error "Bad datum -- CONTENTS" datum)
   )
)

;; CREATE MAKE-ANGLE PROCEDURES

(define (make-angle-degree angle)
   (attach-tag 'angle-degree angle))

(define (make-angle-radian angle)
   (attach-tag 'angle-radian angle))

;; CREATE TABLE OF ASSOCIATED FUNCTIONS AND TYPES


;; (put <op> <type> <function/value to return>

(put 'angle-radian 'rad (lambda (x) x))
(put 'angle-radian 'deg (lambda (x) (* (/ 180 3.14159265) x)))
(put 'angle-degree 'rad (lambda (x) (* (/ 3.14159265 180) x)))
(put 'angle-degree 'deg (lambda (x) x))

;; CREATE GENERIC OPERATE PROCEDURE

(define (operate op obj)
  (let ((proc (get (type-tag obj) op)))
    (if proc
         (proc (contents obj))
         (error "unknown operator for type"))))

;; CREATE SOME "INSTANCES"

(define a2 (make-angle-degree 30))

;; CALLING PROCEDURES IS SIMILAR

;; (operate 'deg a2)


;; CREATE ABSTRACTION PROC

(define (get-degree angle)
   (operate 'deg angle))
(define (get-radian angle)
   (operate 'rad angle))
;; END DATA DIRECTED



            


Downloads:
Download: message.scm 1 KB
Download: dd.scm 1 KB

Please login or Click Here to register for downloads
Creative Commons License
Message Passing vs Tagged Data by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com