Title: CS 3015 Lecture 19
1CS 3015 Lecture 19
2More on Streams
- Modularity of functional programs
- As we have discussed, streams give us a different
way to modularize our programs - Encapsulation, or hiding,'' parts of the state
of a large system - First way is with assignment
- Hiding state in local variables
- Second way is with streams
3Example Reimplement Monte Carlo estimation of Pi
- Key issue hide the internal state of a
random-number generator from programs that used
random numbers - Recall rand-update
- (define rand (let ((x random-init)) (lambda
() (set! x (rand-update x)) x)))
4With Streams
- There is no randon number generator
- Instead there is a stream of random numbers
- Produced by successive calls to rand-update
- (define random-numbers (cons-stream
- random-init (stream-map rand-update
- random-numbers)))
5Cesaro experiment
- Now construct a stream of results of the Cesaro
experiment performed on pairs of random numbers - (define cesaro-stream (map-successive-pairs
- (lambda (r1 r2) ( (gcd r1 r2) 1)) random-nu
mbers))
6Map-successive-pairs
- (define (map-successive-pairs f s) (cons-stream
(f (stream-car s) - (stream-car (stream-cdr s))) (map-successiv
e-pairs f - (stream-cdr (stream-cdr s)))))
- Now feed to Monte-Carlo procedure
7Monte-Carlo
- (define (monte-carlo experiment-stream
- passed failed) (define (next passed failed)
(cons-stream (/ passed ( passed failed)
) (monte-carlo (stream-cdr - experiment-stream)
- passed failed))) (if (stream-car experiment
-stream) (next ( passed 1) failed) (n
ext passed ( failed 1))))
8Pi
- (define pi (stream-map
- (lambda (p) (sqrt (/ 6 p))) (monte-carlo ce
saro-stream 0 0))) - So we get modularity thats atleast as nice as
with objects, but without assignment
9Metalinguistic Abstraction
- We have seen that language is a very powerful
tool in helping us abstract a problem in the
correct way - Higher-level languages provide us with more
powerful abstraction techniques - This is true in all branches of science and
engineering - Metalinguistic abstraction is the development of
new languages that allow abstractions specialized
to particular types of problems
10Special in Computer Science
- Metalinguistic abstraction is particularly
powerful in computer programming - In programming not only can we formulate new
languages but we can also implement these
languages by constructing evaluators - An evaluator (or interpreter) is a procedure
that, when applied to an expression of a
language, performs the actions required to
evaluate that expression
11The most fundamental idea
- It is no exaggeration to regard this as the most
fundamental idea in programming - An evaluator is just another program!
12Almost any program is an evaluator
- The polynomial manipulation program we wrote is
an evaluator for the language of polynomials
13The Metacircular Evaluator
- We will write a lisp program that is an evaluator
for lisp - This may seem circular, however, evaluation is a
process just like any of the others we have
written programs to perform - An evaluator that is written in the same language
that it evaluates is said to be metacircular
14The Metacircular Evaluator
- A Scheme formulation of the environment model of
evaluation described in section 3.2 - Two parts
- To evaluate a combination, evaluate the
subexpressions and then apply the value of the
operator subexpression to the values of the
operand subexpressions - To apply a compound procedure to a set of
arguments, evaluate the body of the procedure in
a new environment
15The new Environment
- Extend the environment part of the procedure
object by a frame in which the formal parameters
of the procedure are bound to the arguments to
which the procedure is applied
16The basic cycle
- Expressions to be evaluated in environments are
reduced to procedures to be evaluated on
arguments - Arguments are reduced to new expressions to be
evaluated in new environments - Two critical procedures in the evaluator
- eval
- apply
17Eval
- Eval takes as arguments an expression and an
environment - Classifies the expression and directs its
evaluation - Structured as a case analysis of syntactic type
of the expression
18Primitive Expressions
- For self-evaluating expressions, such as numbers,
eval returns the expression itself - Eval must look up variables in the environment to
find their values
19Special Forms
- For quoted expressions, eval returns the
expression that was quoted - An assignment to (or a definition of) a variable
must recursively call eval to compute the new
value to be associated with the variable - The environment must be modified to change (or
create) the binding of the variable - An if expression requires special processing of
its parts, so as to evaluate the consequent if
the predicate is true, and otherwise to evaluate
the alternative.
20Special Forms contd
- A lambda expression must be transformed into an
applicable procedure by packaging together the
parameters and body specified by the lambda
expression with the environment of the evaluation - A begin expression requires evaluating its
sequence of expressions in the order in which
they appear - A cond is transformed into a nest of if
expressions and then evaluated
21Combinations
- For a procedure application, eval must
recursively evaluate the operator part and the
operands of the combination - The resulting procedure and arguments are passed
to apply, which handles the actual procedure
application
22Definition of eval
- (define (eval exp env)(cond ((self-evaluating? ex
p) exp) ((variable? exp) - (lookup-variable-value exp env)) ((quoted
? exp) (text-of-quotation exp)) ((assignment
? exp) - (eval-assignment exp env)) ((definition?
exp) - (eval-definition exp env)) ((if? exp) (ev
al-if exp env)) - ((lambda? exp) (make-procedure (l
ambda-parameters exp) (la
mbda-body exp) env))
23Definition of eval contd
- ((begin? exp) (eval-sequence
- (begin-actions exp) env))
((cond? exp) (eval (cond-gtif exp) env)) ((app
lication? exp) (apply (eval (operator exp
) env) (list-of-values - (operands exp) env))) (else (error
"Unknown expression type -- - EVAL" exp))))
24A more general version of eval
- The only difference between this implementation
and the real implementation is that the real
implementation is data directed - This allows new types of expressions to be
defined without changing eval
25Apply
- The apply procedures takes a procedure and a list
of arguments and applies the procedures to the
arguments - Two kinds of procedures
- For primitives, apply calls apply-primitive-proced
ure - For compound procedures, it evaluates the
expressions in the body
26Environments of compound procedures
- The environment for the evaluation of the body of
a compound procedure is constructed by extending
the base environment carried by the procedure to
include a frame that binds the parameters of the
procedure to the arguments to which the procedure
is to be applied
27Definition of apply
- (define (apply procedure arguments) (cond ((prim
itive-procedure? procedure) (apply-primit
ive-procedure - procedure arguments)) ((compound-proce
dure? procedure) (eval-sequence
(procedure-body procedure) (extend-en
vironment (procedure-parameters proce
dure) arguments (procedu
re-environment - procedure)))) (else (error
"Unknown procedure type -- - APPLY" procedure))))
28The list-of-values procedure
- (define (list-of-values exps env)(if (no-operands
? exps) '() (cons - (eval (first-operand exps) env) (list-of-
values - (rest-operands exps) env))))
29The eval-if procedure
- (define (eval-if exp env) (if (true?
- (eval (if-predicate exp) env)) (eval (if-
consequent exp) env) (eval (if-alternative e
xp) - env)))
30The eval-sequence procedure
- (define (eval-sequence exps env)(cond ((last-exp?
exps) - (eval (first-exp exps) env)) (else
- (eval (first-exp exps) env) (eval-seq
uence - (rest-exps exps) env))))
31The eval-assignment procedure
- (define (eval-assignment exp env)(set-variable-va
lue! - (assignment-variable exp) (eval (assignment-v
alue exp) env) env)'ok)
32Evaluating definitions
- (define (eval-definition exp env)(define-variable
! - (definition-variable exp) (eval (definition-v
alue exp) env) env)'ok)
33Representing Expressions
- The evaluator is similar to the symbolic
differentiator that we discussed earlier - In both programs, the result of operating on a
compound expression is determined by operating on
the pieces - Combining the results in a way that depends on
the type of expression - Use data abstraction to decouple the
representation from the behavior
34Self-evaluating items
- (define (self-evaluating? exp) (cond ((number? e
xp) true) ((string? exp) true) (el
se false)))
35Variables
- (define (variable? exp) (symbol? exp))
36Quotations
- (define (quoted? exp) (tagged-list? exp 'quote))
- (define (text-of-quotation exp)
- (cadr exp))
- (define (tagged-list? exp tag) (if (pair? exp)
(eq? (car exp) tag) false))
37Assignments
- (define (assignment? exp) (tagged-list? exp 'set
!)) - (define (assignment-variable exp)
- (cadr exp))
- (define (assignment-value exp)
- (caddr exp))
38Definitions
- (define (definition? exp) (tagged-list? exp 'def
ine)) - (define (definition-variable exp) (if (symbol? (
cadr exp)) (cadr exp) (caadr exp)))
39Definitions contd
- (define (definition-value exp) (if (symbol? (cad
r exp)) (caddr exp) (make-lambda (cdad
r exp) (cddr exp))))
40Lambda expressions
- (define (lambda? exp)
- (tagged-list? exp 'lambda))
- (define (lambda-parameters exp)
- (cadr exp))
- (define (lambda-body exp) (cddr exp))
41A constructor for lambda expressions
- (define (make-lambda parameters body)(cons 'lambd
a (cons parameters body)))
42Conditionals
- (define (if? exp)
- (tagged-list? exp 'if))
- (define (if-predicate exp) (cadr exp))
- (define (if-consequent exp) (caddr exp))
- (define (if-alternative exp) (if (not (null? (cd
ddr exp))) (cadddr exp) 'false))
43A constructor for if
- (define
- (make-if
- predicate consequent alternative)(list 'if pre
dicate consequent - alternative))
44Sequences
- (define (begin? exp)
- (tagged-list? exp 'begin))
- (define (begin-actions exp) (cdr exp))
- (define (last-exp? seq)
- (null? (cdr seq)))
- (define (first-exp seq) (car seq))
- (define (rest-exps seq) (cdr seq))