Evaluation and universal machines The Eval/ Cycle Eval • What is the role of evaluation in defining a language? Exp & env • How can we use evaluation to design a language? Apply Proc & args

• Eval and Apply execute a cycle that unwinds our abstractions • Reduces to simple applications of built in procedure to primitive data structures • Key: • Evaluator determines meaning of programs (and hence our language) • Evaluator is just another program!!

1/33 2/33

Examining the role of Eval Eval from perspective of language designer

• From perspective of a language designer • Applicative order • From perspective of a theoretician • Dynamic vs. lexical scoping • Lazy evaluation • Full normal order • By specifying arguments • Just for pairs • Decoupling analysis from evaluation

3/33 4/33

static analysis: work done before execution Reasons to do static analysis

• straight • Improve execution performance environment • avoid repeating work if expression contains loops • simplify execution engine

expression interpreter value • Catch common mistakes early • garbled expression • operand of incorrect type • advanced interpreter environment • wrong number of operands to procedure or • Prove properties of program • will be fast enough, won't run out of memory, etc. static expr execution value • significant current research topic analysis

5/33 6/33

1 Eval is expensive Summary of part 1

(eval '(define (fact n) • static analysis (if (= n 1) 1 (* n (fact (- n 1))))) GE) • work done before execution ==> undef • performance ... (eval '(fact 4) GE) ... • catch mistakes ... (eval '(= n 1) E1) ... • prove program properties which executes the case statement in eval four times • analyze evaluator ... (eval '(fact 3) E1) ... • static analysis: eliminate execution cost of eval ... (eval '(= n 1) E2) ... which executes the case statement in eval four times

• The analyze evaluator avoids this cost

7/33 8/33

Strategy of the analyze evaluator Example of analyze: variable name lookup environment evaluator data struct scheme's environment environment pi 3.14 expr analyze EP execution value foo name pi

foo Execution procedure pi analyze execution 3.14 a scheme procedure Env → anytype p: env analyze → → : expression (Env anytype) b: (lookup name env) (define (a-eval exp env)

((analyze exp) env)) 9/33 10/33

Implementing variable name lookup Implementing number analysis (define (analyze exp) • Implementing analyze-number is also easy (cond ((number? exp) (analyze-number exp)) (define (analyze-number exp) ((variable? exp) (analyze-variable exp)) (lambda (env) exp )) ... )) (black: analysis phase) (blue: execution phase) (define (analyze-variable exp) (lambda (env) (lookup-variable exp env)))

(black: analysis phase) (blue: execution phase)

11/33 12/33

2 Summary of part 2 Subexpressions (hardest concept today)

• output of analyze is an execution procedure (analyze ' (if (= n 1) 1 (* n (...))) ) • given an environment • produces value of expression • analysis phase: (analyze ' (= n 1) ) ==> pproc (analyze 1) ==> cproc • within analyze (analyze ' (* n (...)) )==> aproc • execution phase code appears inside (lambda (env) ...) • execution phase • all other code runs during analysis phase (pproc env) ==> #t or #f (depending on n) if #t, (cproc env) if #f, (aproc env)

13/33 14/33

Implementation of analyze-if Visualization of analyze-if exp 1 (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (if (= n 1) pproc ... (cproc (analyze (if-consequent exp))) 1 cproc (aproc (analyze (if-alternative exp)))) (* n (...))) aproc ... (lambda (env) (if (true? (pproc env)) p: env (cproc env) analyze b: exp (aproc env)) )))

p: env black: analysis phase blue: execution phase b: (if (true? (pproc env)) (cproc env) (aproc env)) 15/33 16/33

Your turn Implementation of analyze-definition

• Assume the following procedures for definitions like (define x (+ y 1)) (define (analyze-definition exp) (let ((var (definition-variable exp)) (definition-variable exp) x (vproc (analyze (definition-value exp)))) (definition-value exp) (+ y 1) (lambda (env) (define-variable! name value env) add binding (define-variable! var (vproc env) env)))) to env

• Implement analyze-definition black: analysis phase blue: execution phase • The only execution-phase work is define-variable! • The definition-value might be an arbitrary expression

17/33 18/33

3 Summary of part 3 Implementing lambda

• Within analyze • Body stored in double bubble is an execution procedure • recursively call analyze on subexpressions • old make-procedure list, expression, Env → Procedure • create an execution procedure which stores the EPs for subexpressions as local state • new make-procedure list, (Env->anytype), Env → Procedure

(define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) (bproc (analyze (lambda-body exp)))) (lambda (env) (make-procedure vars bproc env))))

19/33 20/33

Implementing apply: execution phase Implementing apply: analysis phase (define (execute-application proc args) (define (analyze-application exp) (cond (let ((fproc (analyze (operator exp))) ((primitive-procedure? proc) (aprocs (map analyze (operands exp)))) ...) (lambda (env) ((compound-procedure? proc) (execute-application ((procedure-body proc) (fproc env) (extend-environment (parameters proc) (map (lambda (aproc) (aproc env)) args aprocs)) ))) (environment proc)))) (else ...)))

21/33 22/33

Summary of part 4 What is Eval really?

• In the analyze evaluator, • Suppose you were a circuit designer • double bubble stores execution procedure, not • Given a circuit diagram, you could transform it into an electric expression signal encoding the layout of the diagram • Now suppose you wanted to build a circuit that could take any such signal as input (any other circuit) and could then reconfigure itself to simulate that input circuit • What would this general circuit look like??? • Suppose instead you describe a circuit as a program • Can you build a program that takes any program as input and reconfigures itself to simulate that input program? • Sure – that’s just EVAL!! – it’s a UNIVERSAL MACHINE

23/33 24/33

4 It wasn’t always this obvious Why a Universal Machine?

• “If it should turn out that the basic logics of a machine • If EVAL can simulate any machine, and if EVAL is itself a designed for the numerical solution of differential equations description of a machine, then EVAL can simulate itself coincide with the logics of a machine intended to make bills • This was our example of meval for a department store, I would regard this as the most • In fact, EVAL can simulate an evaluator for any other amazing coincidence that I have ever encountered” language • Just need to specify syntax, rules of evaluation Howard Aiken, writing in 1956 (designer of the Mark I • An evaluator for any language can simulate any other “Electronic Brain”, developed jointly by IBM and Harvard language starting in 1939) • Hence there is a general notion of computability – idea that a process can be computed independent of what language we are using, and that anything computable in one language is computable in any other language

25/33 26/33

Turing’s insight Turing’s insight

• Alan Mathison Turing • Was fascinated by Godel’s incompleteness results in decidability (1933) • In any axiomatic mathematical system there are propositions that • 1912-1954 cannot be proved or disproved within the axioms of the system • In particular the consistency of the axioms cannot be proved. • Led Turing to investigate Hilbert’s Entscheidungsproblem • Given a mathematical proposition could one find an algorithm which would decide if the proposition was true of false? • For many propositions it was easy to find such an algorithm. • The real difficulty arose in proving that for certain propositions no such algorithm existed. • In general – Is there some fixed definite process which, in principle, can answer any mathematical question? • E.g., Suppose want to prove some theorem in geometry – Consider all proofs from axioms in 1 step – … in 2 steps ….

27/33 28/33

Turing’s insight The halting problem

• Turing proposed a theoretical model of a simple kind of • If there is a problem that the universal machine can’t solve, machine (now called a Turing machine) and argued that then no machine can solve, and hence no effective process any “effective process” can be carried out by such a • Make list of all possible programs (all machines with 1 input) machine • Encode all their possible inputs as integers • Each machine can be characterized by its program • List their outputs for all possible inputs (as integer, error or • Programs can be coded and used as input to a machine loops forever) • Define f(n) = output of machine n on input n, plus 1 if output is • Showed how to code a universal machine a number • Wrote the first EVAL! • Define f(n) = 0 if machine n on input n is error or loops • But f can’t be computed by any program in the list!! • Yet we just described process for computing f?? • Bug is that can’t tell if a machine will always halt and produce an answer

29/33 30/33

5 The Halting theorem Turing’s history

• Halting problem: Take as inputs the description of a • Published this work as a student machine M and a number n, and determine whether or not • Got exactly two requests for reprints M will halt and produce an answer when given n as an • One from Alonzo Church (professor of logic at input Princeton) • Halting theorem (Turing): There is no way to write a – Had his own formalism for notion of an effective program (for any computer, in any language) that solves procedure, called the the halting problem. • Completed Ph.. with Church, proving Church-Turing Thesis: • Any procedure that could reasonably be considered to be an effective procedure can be carried out by a universal machine (and therefore by any universal machine)

31/33 32/33

Turing’s history

• Worked as code breaker during WWII • Key person in Ultra project, breaking German’s Enigma coding machine • Designed and built the Bombe, machine for breaking messages from German Airforce • Designed statistical methods for breaking messages from German Navy • Spent considerable time determining counter measures for providing alternative sources of information so Germans wouldn’t know Enigma broken • Designed general-purpose digital computer based on this work • Turing test: argued that intelligence can be described by an effective procedure – foundation for AI • World class marathoner – fifth in Olympic qualifying (2:46:03 – 10 minutes off Olympic pace) • Working on computational biology – how nature “computes” biological forms. • His death

33/33

6