Data-Driven Programs TDDC74 Programming: Abstraction and Modelling z z z z A modular way of organizing programs The idea will be illustrated by examples The idea is to tag data and use these tags for dispatching to the right procedure Later on in the course we’ll compare various ways of organizing programs SICP 2 (part 3) Lecture 6 Anders Haraldsson 2 Data-Driven Programs Tagging – tagged data Laboratory work media-dbs-examples.scm z z z Adds information about the type to data ((respect) (aretha franklin) 1967 soul) -> (music (respect) (aretha franklin) 1967 soul) (person (name kalle) (adress storgatan 15) (age 20)) Primitives for tagging: attach-tag z get-type z get-content z 3 4 Dispatching on type z z z Discussions and exercises in SICP section 2.4 Dispatching on type Generalized procedures that works for different types of data or different representations of data Discussions and exercises in SICP section 2.4.3 z We need recognizers for each type of representation (define (person-list? p) ; Person -> Bool (eq? (get-type p) ’person-list)) In earlier lecture we had two representations for person. We want to use both representations and tag them. (define (person-assoc-list? p) ; Person -> Bool (eq? (get-type p) ’person-assoc-list)) (define (make-person-list name age father mother sex) ; Name x Age x Name x Name x Sex -> Person z (attach-tag ’person-list (list name age father mother sex))) 5 (define (make-person-assoc-list name age father mother sex) ; Name x Age x Name x Name x Sex -> Person (attach-tag ‘person-assoc-list (list (cons ‘name name) (cons ‘dob dob) (cons ‘father father) (cons ‘mother mother) (cons ‘sex sex)))) 6 Now we want selectors for the person-dataobject knowing both types of representation. (define (get-name p) ; Person -> Name (cond ((person-list? p) (car (get-contents p))) ((person-assoc-list? p) (cdr (assq (get-contents p)))) (else (error ”Bad type” p)))) 1 Tagging and Dispatching on types Packages – data directed programming Problems z Adding new data-types means modifying all selector procedures z z z z Approach not scalable z 7 Lab 3 problem 8 - Table organization Install-package There are procedures in table-helper.scm, which help us to organize procedures in packages in a 2 dimensional table. z type1 proc11 proc21 type2 proc12 proc22 Define procedure install-type – – type3 proc13 proc23 Table primitives: z put: row x column x item-to-store -> z get: row x column -> item-to-store z display-table -> prints table Define the procedures Store them in table (put) z Evaluate procedure install-type z Lab 3 problem 8 10 Generic procedures Scheme interpreter procedures From the data object find the apropriate procedure The interpreter can be called by z z z 11 Discussions and exercises in SICP section 2.4.3 8 operation1 operation2 9 Simple procedures for each data-type grouped in packages Store packages independently of each other -> easy to add or remove packages Create generic operations that retrieves appropriate procedures from storage (eval Scheme-expression) (apply Scheme-procedures argumentlist) Get the type from the tag in the data object Get the procedure for the type from the table Apply the procedure to the data object (eval (list ’+ 1 2 3)) -> (+ 1 2 3) = 6 (eval (list ’cons (list ’quote ’a) 1)) -> (cons (quote a) 1) = (a . 1) 12 (apply + (list 1 2 3)) -> (apply + ’(1 2 3)) = 6 (apply (lambda (x y) (list y x)) (list ’a ’b)) -> -> (apply (lambda (x y) (list y x)) ’(a b)) = (b a) 2