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

Notes: Monday 24 March 2003

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:
• Decidable: it is easy enough to be solved by some procedure that always terminates
• NP: it is easy enough to be solved by a procedure that tries an exponential number of guesses, but takes only P-time to check one if correct
• P: it is easy enough to be solved by a polynomial time procedure (that is, a procedure that is O(nk).)

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:

• Undecidable: if you had a procedure that solves this problem, you could use it to solve a known undecidable problem (e.g., the halting problem)
• NP-Complete: if you had a procedure that solves this problem, you could use it to solve a known NP-Complete problem (e.g., the travelling salesman problem). Subtlety: the transformation of the problem and answer must be in P.
Metacircular Evaluator

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

SICP, p. 360

```(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 200: Computer Science Department of Computer Science University of Virginia Circle Fractal by Ramsey Arnaoot and Qi Wang cs200-staff@cs.virginia.edu Using these Materials