Com S 342 meeting -*- Outline -*- * data abstraction (3.5) raise your hand if you aren't familiar with ADTs? example: records ** terminology ------------------------------------------ DATA ABSTRACTION terms: data abstraction, ADT example: records divide program into 2 parts 1. knows about representation (the ADT) 2. doesn't know details of rep (the client) def: a program exhibits if it works correctly with any correct implementation of an ADT. example: ------------------------------------------ ...representation independence ... most programs are independent of the rep for numbers, lists, we'll also be independent of the rep of records. Q: Are C programs independent of the representation of numbers? Q: What good is this? it allows you to change the representation by only changing small number of procedures ** opaque vs. transparent implementations ------------------------------------------ OPAQUE VS. TRANSPARENT REPS > (define-record app (rator rand)) > (define my-app (make-app (make-varref 'car) (make-varref 'ls))) > (app? my-app) > (vector? my-app) > my-app def: a type is *opaque* if otherwise it is transparent. ------------------------------------------ ... #t, #t, #(app #(varref car) #(varref ls)) ... there is no way to find out its rep, even by printing Q: which kind does Scheme have? C++? Smalltalk has opaque Q: What's the advantage of an opaque type? of transparent? opaque enforces use of the defining procedures, transparent is easier to extend (also a disadvantage!) ** type checking and ADTs ------------------------------------------ TYPE CHECKING AND ADTS abstract type (cell T) rep type (vector T) ;;;;; in cell.def (defrep (cell T) (vector T)) (deftype make-cell (-> (T) (cell T))) (deftype cell-ref (-> ((cell T)) T)) ... ;;;;; in cell.scm (define make-cell ;; TYPE: (lambda (x) (vector x))) (define cell-ref ;; TYPE: (lambda (x) (vector-ref x 0))) ------------------------------------------