CS 3015 Lecture 19 - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

CS 3015 Lecture 19

Description:

exp 'quote)) (define (text-of-quotation exp) (cadr exp)) (define ... (null? ( cdr seq))) (define (first-exp seq) (car seq)) (define (rest-exps seq) (cdr seq) ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 45
Provided by: JVB4
Category:
Tags: lecture

less

Transcript and Presenter's Notes

Title: CS 3015 Lecture 19


1
CS 3015 Lecture 19
2
More 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

3
Example 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)))

4
With 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)))

5
Cesaro 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))

6
Map-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

7
Monte-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))))

8
Pi
  • (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

9
Metalinguistic 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

10
Special 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

11
The most fundamental idea
  • It is no exaggeration to regard this as the most
    fundamental idea in programming
  • An evaluator is just another program!

12
Almost any program is an evaluator
  • The polynomial manipulation program we wrote is
    an evaluator for the language of polynomials

13
The 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

14
The 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

15
The 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

16
The 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

17
Eval
  • 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

18
Primitive 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

19
Special 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.

20
Special 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

21
Combinations
  • 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

22
Definition 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))

23
Definition 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))))

24
A 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

25
Apply
  • 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

26
Environments 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

27
Definition 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))))

28
The 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))))

29
The 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)))

30
The 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))))

31
The eval-assignment procedure
  • (define (eval-assignment exp env)(set-variable-va
    lue! 
  • (assignment-variable exp)   (eval (assignment-v
    alue exp) env)   env)'ok)

32
Evaluating definitions
  • (define (eval-definition exp env)(define-variable
  • (definition-variable exp)   (eval (definition-v
    alue exp) env)   env)'ok)

33
Representing 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

34
Self-evaluating items
  • (define (self-evaluating? exp)  (cond ((number? e
    xp) true)        ((string? exp) true)        (el
    se false)))

35
Variables
  • (define (variable? exp) (symbol? exp))

36
Quotations
  • (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))

37
Assignments
  • (define (assignment? exp)  (tagged-list? exp 'set
    !))
  • (define (assignment-variable exp) 
  • (cadr exp))
  • (define (assignment-value exp) 
  • (caddr exp))

38
Definitions
  • (define (definition? exp)  (tagged-list? exp 'def
    ine))
  • (define (definition-variable exp)  (if (symbol? (
    cadr exp))      (cadr exp)      (caadr exp)))

39
Definitions contd
  • (define (definition-value exp)  (if (symbol? (cad
    r exp))      (caddr exp)      (make-lambda (cdad
    r exp)                      (cddr exp))))

40
Lambda expressions
  • (define (lambda? exp) 
  • (tagged-list? exp 'lambda))
  • (define (lambda-parameters exp) 
  • (cadr exp))
  • (define (lambda-body exp) (cddr exp))

41
A constructor for lambda expressions
  • (define (make-lambda parameters body)(cons 'lambd
    a (cons parameters body)))

42
Conditionals
  • (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))

43
A constructor for if
  • (define
  • (make-if
  •  predicate consequent alternative)(list 'if pre
    dicate consequent 
  • alternative))

44
Sequences
  • (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))
Write a Comment
User Comments (0)
About PowerShow.com