1 / 15

Data Abstraction

Data Abstraction. Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types. Goals. abstract datatype makes program independent of concrete representation look at implementation strategies

rhonda
Télécharger la présentation

Data Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Abstraction • Programming Language Essentials • 2nd edition • Chapter 2.3 Representation Strategies for Data Types

  2. Goals • abstract datatype makes program independent of concrete representation • look at implementation strategies • environment maps a finite set of Scheme symbols to values taken from all Scheme values • e.g., values of variables • e.g., lexical addresses of symbols

  3. Environment Interface • (empty-env) • constructor, represents an empty environment • (apply-env environmentsymbol) • observer, applies a representation to a symbol, returns associated value • (extend-env symbol-list value-list environment) • constructor, represents an environment which returns given values for given symbols and defaults to old environment otherwise

  4. Example • (define dxy-env • (extend-env '(d x) '(6 7) • (extend-env '(y) '(8) • (empty-env)))) • (apply-env dxy-env 'x) • has to return 7 • $ cd code/2; make 2.3.2 2.3.3

  5. Represent values as procedures • (define empty-env ; returns function that fails • (lambda () • (lambda (s) (error 'apply-env "No binding" s)) • ) ) • (define extend-env ; returns function returning vals • (lambda (syms vals env) ; or asks env • (lambda (sym) • (let ((pos (list-find-position sym syms))) • (if (number? pos) • (list-ref vals pos) • (apply-env env sym) • ) ) ) ) ) • (define apply-env • (lambda (env sym) (env sym)) • )

  6. Searching a linear list • (define list-find-position ; returns pos >= 0 or #f • (lambda (s list) ; of s in list • (list-index (lambda (x) (eqv? x s)) list) • ) ) • (define list-index ; returns pos >= 0 or #f • (lambda (p? l) ; where p? is true in l • (cond • ((null? l) #f) • ((p? (car l)) 0) • (else • (let ((list-index-r (list-index p? (cdr l)))) • (if (number? list-index-r) • (+ list-index-r 1) • #f • ) ) ) ) ) )

  7. How-to abstract using procedures • if client uses data-type only by way of values returned by procedure calls: • for each lambda expression returning such a value: embed it into a constructor which takes the free variables as parameters. • replace the lambda expression in the client by invoking the constructor. • define an observer apply-x to invoke the result returned by the constructor. • replace every value by invoking apply-x.

  8. Represent values using syntax trees • env-rep: '(' 'empty-env' ')' • empty-env-record • | '(' 'extend-env' '(' 'Symbol'* ')' • '(' 'Value'* ')' env-rep ')' • extended-env-record (syms vals env)

  9. Abstract syntax tree • (define-datatype environment environment? • (empty-env-record) • (extended-env-record • (syms (list-of symbol?)) • (vals (list-of scheme-value?)) • (env environment?) • ) ) • (define scheme-value? (lambda (v) #t))

  10. Constructors • (define empty-env • (lambda () • (empty-env-record) • ) ) • (define extend-env • (lambda (syms vals env) • (extended-env-record syms vals env) • ) )

  11. Observer • (define apply-env • (lambda (env sym) • (cases environment env • (empty-env-record () • (error 'apply-env "No binding" sym)) • (extended-env-record (syms vals env) • (let • ((pos • (list-find-position sym syms))) • (if (number? pos) • (list-ref vals pos) • (apply-env env sym) • ) ) ) ) ) )

  12. How-to abstract using syntax trees • if client uses data-type only by way of values returned by procedure calls: • for each lambda expression returning such a value: create one variant with one field per free variable in lambda expression. • use define-datatype with those variants. • replace the lambda expression in the client by invoking the constructor. • define an observer apply-x based on cases where the bodies of the lambda expressions are the bodies of the consequents of cases. • replace every value by invoking apply-x.

  13. Represent environment with lists • env-rep: '()' • | '(' '(' '(' 'Symbol'* ')' '.' • '#(' 'Value'* ')' ')' '.' env-rep ')' • ribcage a b c 1 2 3 d e 4 5

  14. Constructors • (define empty-env • (lambda () '()) • ) • (define extend-env • (lambda (syms vals env) • (cons • (cons syms (list->vector vals)) • env • ) ) ) • $ cd code/2; make 2.3.4

  15. Observer • (define apply-env • (lambda (env sym) • (if (null? env) • (error 'apply-env "No binding" sym) • (let* • ((syms (car (car env))) • (vals (cdr (car env))) • (pos (list-find-position sym syms)) • ) • (if (number? pos) • (vector-ref vals pos) • (apply-env (cdr env) sym) • ) ) ) ) )

More Related