Download presentation
Presentation is loading. Please wait.
Published byBlaze Warner Modified over 9 years ago
1
plt-2002-2 10/12/2015 3.3-1 Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types
2
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-3 Environment Interface (empty-env) constructor, represents an empty environment (apply-env environment symbol) 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
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 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
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-7 How-to abstract using procedures if client uses data-type only by way of values returned by procedure calls: (1)for each lambda expression returning such a value: embed it into a constructor which takes the free variables as parameters. (2)replace the lambda expression in the client by invoking the constructor. (3)define an observer apply-x to invoke the result returned by the constructor. (4)replace every value by invoking apply-x.
8
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-10 Constructors (define empty-env (lambda () (empty-env-record) ) (define extend-env (lambda (syms vals env) (extended-env-record syms vals env) )
11
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-12 How-to abstract using syntax trees if client uses data-type only by way of values returned by procedure calls: (1)for each lambda expression returning such a value: create one variant with one field per free variable in lambda expression. (2)use define-datatype with those variants. (3)replace the lambda expression in the client by invoking the constructor. (4)define an observer apply-x based on cases where the bodies of the lambda expressions are the bodies of the consequents of cases. (5)replace every value by invoking apply-x.
13
plt-2002-2 10/12/2015 3.3-13 Represent environment with lists env-rep: '()' | '(' '(' '(' 'Symbol'* ')' '.' '#(' 'Value'* ')' ')' '.' env-rep ')' ribcage c c b b a a 1 1 2 2 3 3 e e d d 4 4 5 5
14
plt-2002-2 10/12/2015 3.3-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
plt-2002-2 10/12/2015 3.3-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) ) ) ) ) )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.