University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2002

Notes: Monday 25 March 2002

Schedule
Problem Classification Arguments
To show a problem is decidable/in NP/in P, you need to show it is easy enough to be solved with a procedure in that class:

To show a problem is undecidable or NP-complete, you need to show it is as hard as a problem you know enough to be solved with a procedure in that class:

Metacircular Evaluator

The evaluator, which determines themeaning of expressions in the programming language, is just another program.

SICP, p. 360

Complete Code for Mini-Scheme Evaluator
(define (meval expr env)
  (cond
   ((self-evaluating? expr) expr)
   ((variable? expr) (environment-lookup-name expr env))
   ((lambda? expr)  
        (make-procedure (lambda-parameters expr)
	               (lambda-body expr) env))
   ((application? expr)     
        (mapply (meval (application-operator expr) env)
                    (map (lambda (subexpr) (meval subexpr env))
                             (application-operands expr))))
   (else (error "Unknown expression: " exp))))






(define (mapply procedure operands)
  (cond
   ((primitive-procedure? procedure)
        (apply-primitive procedure operands))
   ((compound-procedure? procedure)
       (meval-sequence
          (procedure-body procedure)
          (extend-environment
            (procedure-parameters procedure)
            operands
            (procedure-environment procedure))))
   (else (error "Can't apply: " procedure))))

> (meval '((lambda (x) (* x x)) 4) the-global-environment)
|(meval
  ((lambda (x) (* x x)) 4)
  (((+ primitive-procedure #)
    (* primitive-procedure #)
    (- primitive-procedure #))))
| (meval
   (lambda (x) (* x x))
   (((+ primitive-procedure #)
     (* primitive-procedure #)
     (- primitive-procedure #))))
| (procedure
    (x)
    ((* x x))
    (((+ primitive-procedure #)
      (* primitive-procedure #)
      (- primitive-procedure #))))
| (meval
   4
   (((+ primitive-procedure #)
     (* primitive-procedure #)
     (- primitive-procedure #))))
| 4
| (mapply
    (procedure
      (x)
      ((* x x))
      (((+ primitive-procedure #)
        (* primitive-procedure #)
        (- primitive-procedure #))))
    (4))
| |(meval
    (* x x)
    (((x . 4))
     ((+ primitive-procedure #)
      (* primitive-procedure #)
      (- primitive-procedure #))))
| | (meval
     *
     (((x . 4))
      ((+ primitive-procedure #)
       (* primitive-procedure #)
       (- primitive-procedure #))))
| | (primitive-procedure #)
| | (meval
     x
     (((x . 4))
      ((+ primitive-procedure #)
       (* primitive-procedure #)
       (- primitive-procedure #))))
| | 4
| | (meval
     x
     (((x . 4))
      ((+ primitive-procedure #)
       (* primitive-procedure #)
       (- primitive-procedure #))))
| | 4
| | (mapply (primitive-procedure #) (4 4))
| | 16
| |16
| 16
|16
16

CS 655 University of Virginia
Department of Computer Science
CS 200: Computer Science
David Evans
evans@cs.virginia.edu
Using these Materials