The Eval/Apply Cycle Eval. Evaluation and universal machines. Examining the role of Eval. Eval from perspective of language designer

Similar documents
Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Functional Languages. Hwansoo Han

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Chapter 12. Computability Mechanizing Reasoning

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Parsing Scheme (+ (* 2 3) 1) * 1

THE HALTING PROBLEM. Joshua Eckroth Chautauqua Nov

Lecture 5: The Halting Problem. Michael Beeson

Lexical vs. Dynamic Scope

Review Analyzing Evaluator. CS61A Lecture 25. What gets returned by mc-eval? (not analyzing eval) REVIEW What is a procedure?

Functional Programming

Functional Programming. Pure Functional Programming

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Lambda Calculus and Computation

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

MATH Iris Loeb.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Chapter 1. Fundamentals of Higher Order Programming

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

05. Turing Machines and Spacetime. I. Turing Machines and Classical Computability.

CS61A Lecture 38. Robert Huang UC Berkeley April 17, 2013

Chapter 11 :: Functional Languages

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Computability, Cantor s diagonalization, Russell s Paradox, Gödel s s Incompleteness, Turing Halting Problem.

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Discussion 12 The MCE (solutions)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Register Machines. Connecting evaluators to low level machine code

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Organization of Programming Languages CS3200/5200N. Lecture 11

A Small Interpreted Language

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

An introduction to Scheme

Introduction to Computer Science

6.001 Notes: Section 8.1

Programming Languages Third Edition

Introduction to the Lambda Calculus. Chris Lomont

Functional Programming. Pure Functional Languages

Objective and Subjective Specifications

Qualifying Exam in Programming Languages and Compilers

Functional Programming. Big Picture. Design of Programming Languages

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Kurt Gödel and Computability Theory

4/19/2018. Chapter 11 :: Functional Languages

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

CMSC 330: Organization of Programming Languages

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Comp 311: Sample Midterm Examination

CS 360 Programming Languages Interpreters

Nano-Lisp The Tutorial Handbook

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

An Introduction to Scheme

Functional Programming. Pure Functional Languages

4.2 Variations on a Scheme -- Lazy Evaluation

Programming Language Pragmatics

Denotational semantics

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

6.001 Notes: Section 4.1

Software System Design and Implementation

Denotational Semantics. Domain Theory

Lecture 12: Conditional Expressions and Local Binding

COMP 382: Reasoning about algorithms

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Introduction to the λ-calculus

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

What computers just cannot do. COS 116: 2/28/2008 Sanjeev Arora

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

6.001 Notes: Section 15.1

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Lecture 2: SML Basics

Principles of Programming Languages

1 Introduction. 3 Syntax

An Explicit Continuation Evaluator for Scheme

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

CS 242. Fundamentals. Reading: See last slide

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lambda Calculus. Gunnar Gotshalks LC-1

Software System Design and Implementation

LECTURE 16. Functional Programming

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Chapter 3. Describing Syntax and Semantics ISBN

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Formal Systems and their Applications

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Functional Programming

CSE341 Autumn 2017, Final Examination December 12, 2017

Transcription:

Evaluation and universal machines What is the role of evaluation in defining a language? How can we use evaluation to design a language? The Eval/Apply Cycle Eval Exp & env 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 From perspective of a language designer From perspective of a theoretician Eval from perspective of language designer Applicative order 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 interpreter Improve execution performance avoid repeating work if expression contains loops simplify execution engine expression advanced interpreter or compiler expr static analysis interpreter execution value value Catch common mistakes early garbled expression operand of incorrect type wrong number of operands to procedure Prove properties of program will be fast enough, won't run out of memory, etc. significant current research topic 5/33 6/33 1

Eval is expensive (eval '(define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) GE) ==> undef... (eval '(fact 4) GE)...... (eval '(= n 1) E1)... which executes the case statement in eval four times... (eval '(fact 3) E1)...... (eval '(= n 1) E2)... which executes the case statement in eval four times Summary of part 1 static analysis work done before execution performance catch mistakes prove program properties analyze evaluator static analysis: eliminate execution cost of eval The analyze evaluator avoids this cost 7/33 8/33 Strategy of the analyze evaluator expr analyze EP execution value Example of analyze: variable name lookup scheme's evaluator data struct name foo pi pi 3.14 Execution procedure a scheme procedure Env anytype foo pi analyze execution 3.14 analyze: expression (Env anytype) (define (a-eval exp env) ((analyze exp) env)) 10/33 p: env b: (lookup name env) 9/33 Implementing variable name lookup (define (analyze exp) (cond ((number? exp) (analyze-number exp)) ((variable? exp) (analyze-variable exp))... )) (define (analyze-variable exp) (lookup-variable exp env))) Implementing number analysis Implementing analyze-number is also easy (define (analyze-number exp) exp)) (black: analysis phase) (blue: execution phase) (black: analysis phase) (blue: execution phase) 11/33 12/33 2

Summary of part 2 output of analyze is an execution procedure given an produces value of expression within analyze execution phase code appears inside...) all other code runs during analysis phase Subexpressions (hardest concept today) (analyze '(if (= n 1) 1 (* n (...)))) analysis phase: (analyze '(= n 1)) ==> pproc (analyze 1) ==> cproc (analyze '(* n (...)))==> aproc execution 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 (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (if (true? (pproc env)) (cproc env) (aproc env))))) Visualization of analyze-if (if (= n 1) 1 (* n (...))) analyze pproc... cproc aproc... exp 1 p: env b: exp black: analysis phase blue: execution phase 15/33 p: env b: (if (true? (pproc env)) (cproc env) (aproc env)) 16/33 Your turn Implementation of analyze-definition Assume the following procedures for definitions like (define x (+ y 1)) (definition-variable exp) x (definition-value exp) (+ y 1) (define-variable! name value env) add binding to env (define (analyze-definition exp) (let ((var (definition-variable exp)) (vproc (analyze (definition-value exp)))) (define-variable! var (vproc env) env)))) Implement analyze-definition The only execution-phase work is define-variable! The definition-value might be an arbitrary expression black: analysis phase blue: execution phase 17/33 18/33 3

Summary of part 3 Within analyze recursively call analyze on subexpressions create an execution procedure which stores the EPs for subexpressions as local state Implementing lambda Body stored in double bubble is an execution procedure old make-procedure list<symbol>, expression, Env Procedure new make-procedure list<symbol>, (Env->anytype), Env Procedure (define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) (bproc (analyze (lambda-body exp)))) (make-procedure vars bproc env)))) 19/33 20/33 Implementing apply: execution phase (define (execute-application proc args) (cond ((primitive-procedure? proc)...) ((compound-procedure? proc) ((procedure-body proc) (extend- (parameters proc) args ( proc)))) (else...))) Implementing apply: analysis phase (define (analyze-application exp) (let ((fproc (analyze (operator exp))) (aprocs (map analyze (operands exp)))) (execute-application (fproc env) (map (lambda (aproc) (aproc env)) aprocs))))) 21/33 22/33 Summary of part 4 In the analyze evaluator, double bubble stores execution procedure, not expression What is Eval really? Suppose you were a circuit designer Given a circuit diagram, you could transform it into an electric 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 If it should turn out that the basic logics of a machine designed for the numerical solution of differential equations coincide with the logics of a machine intended to make bills for a department store, I would regard this as the most amazing coincidence that I have ever encountered Howard Aiken, writing in 1956 (designer of the Mark I Electronic Brain, developed jointly by IBM and Harvard starting in 1939) Why a Universal Machine? If EVAL can simulate any machine, and if EVAL is itself a description of a machine, then EVAL can simulate itself This was our example of meval In fact, EVAL can simulate an evaluator for any other language Just need to specify syntax, rules of evaluation An evaluator for any language can simulate any other language 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 Alan Mathison Turing 1912-1954 Turing s insight Was fascinated by Godel s incompleteness results in decidability (1933) In any axiomatic mathematical system there are propositions that 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 Turing proposed a theoretical model of a simple kind of machine (now called a Turing machine) and argued that any effective process can be carried out by such a machine Each machine can be characterized by its program Programs can be coded and used as input to a machine Showed how to code a universal machine Wrote the first EVAL! The halting problem If there is a problem that the universal machine can t solve, then no machine can solve, and hence no effective process Make list of all possible programs (all machines with 1 input) Encode all their possible inputs as integers List their outputs for all possible inputs (as integer, error or loops forever) Define f(n) = output of machine n on input n, plus 1 if output is a number 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 Halting problem: Take as inputs the description of a machine M and a number n, and determine whether or not M will halt and produce an answer when given n as an input Halting theorem (Turing): There is no way to write a program (for any computer, in any language) that solves the halting problem. Turing s history Published this work as a student Got exactly two requests for reprints One from Alonzo Church (professor of logic at Princeton) Had his own formalism for notion of an effective procedure, called the lambda calculus Completed Ph.D. 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