Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)

Slides:



Advertisements
Similar presentations
David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 17: ProvingUndecidability.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Class 24: Programming with Objects University of Virginia cs1120 David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Cs3102: Theory of Computation Class 24: NP-Completeness Spring 2010 University of Virginia David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
Class 28: Interpreters cs1120 Fall 2009 David Evans.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Variations on a Scheme
Class 30: Models of Computation CS200: Computer Science
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Lecture 28: Types of Types
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Lecture 24: Metalinguistics CS200: Computer Science
The Metacircular Evaluator (Continued)
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Variations on a Scheme
Class 24: Computability Halting Problems Hockey Team Logo
Class 34: Models of Computation CS200: Computer Science
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture 3: Rules of Evaluation CS200: Computer Science
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 23: Computability CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Lecture 2 - Names & Functions
Presentation transcript:

David Evans http://www.cs.virginia.edu/~evans Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4) the-global-environment) 16 CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans

Menu Problem Classification Problems Making New Languages PS7 25 March 2002 CS 200 Spring 2002

Problem Classification 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 – O (nk) harder to show, means problem is easier 25 March 2002 CS 200 Spring 2002

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 25 March 2002 CS 200 Spring 2002

Virus Detection Problem Problem 7. Melissa Problem Input: A Word macro (like a program, but embedded in an email message) Output: true if the macro will forward the message to people in your address book; false otherwise. How can we show it is undecidable? 25 March 2002 CS 200 Spring 2002

Undecidability Proof (define (halts? P) Suppose we could define is-virus? that decides the Melissa problem. Then: (define (halts? P) (if (is-virus? ‘(begin P virus-code)) #t #f)) Since it is a virus, we know virus-code was evaluated, and P must halt (assuming P wasn’t a virus). Its not a virus, so the virus-code never executed. Hence, P must not halt. 25 March 2002 CS 200 Spring 2002

Undecidability Proof (define (halts? P) Suppose we could define is-virus? that decides the Melissa problem. Then: (define (halts? P) (is-virus? ‘(begin (vaccinate P) virus-code)) Where (vaccinate P) evaluates to P with all mail commands replaced with print commands (to make sure (is-virus? P) is false. 25 March 2002 CS 200 Spring 2002

Proof If we had is-virus? we could define halts? We know halts? is undecidable Hence, we can’t have is-virus? Thus, we know is-virus? is undecidable Practice the other problems from Friday. I won’t hand out solutions, but will answer questions about them. 25 March 2002 CS 200 Spring 2002

Metalinguistic Abstraction 25 March 2002 CS 200 Spring 2002

Solving Problems: Recap PS1-4: Divide a problem into procedures that can be combined to solve it PS5: Divide a problem into procedures and state that can be combined to solve it PS6: Divide a problem into objects that can be used to model it 25 March 2002 CS 200 Spring 2002

Solving Problems PS7: Divide a problem into creating a good language for solving the problem, and defining a solution using that language Languages change the way we think. Sometimes the best way to solve a problem is to invent a new language first. 25 March 2002 CS 200 Spring 2002

“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type) 25 March 2002 CS 200 Spring 2002

“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501) J S Bach, “Coffee Cantata”, BWV 211 (1732) www.npj.com/homepage/teritowe/jsbhand.html 25 March 2002 CS 200 Spring 2002

Inventing a Language Design the grammar Make up the evaluation rules What strings are in the language? Use BNF to describe all the strings in the language Make up the evaluation rules Describe what everything the grammar can produce means Build an evaluator A procedure that evaluates expressions in the language 25 March 2002 CS 200 Spring 2002

Is this an exaggeration? (SICP, p. 360) It is no exaggeration to regard this as the most fundamental idea in programming: The evaluator, which determines the meaning of expressions in the programming language, is just another program. To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others. 25 March 2002 CS 200 Spring 2002

Programming an Evaluator If a language is just a program, what language should we program the language in? 25 March 2002 CS 200 Spring 2002

The Metacircular Evaluator 25 March 2002 CS 200 Spring 2002

Environmental Model of Evaluation To evaluate a combination, evaluate all the subexpressions and apply the value of the first subexpression to the values of the other subexpressions. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, make a new frame with an environment pointer that is the environment of the procedure that contains places with the formal parameters bound to the arguments. 25 March 2002 CS 200 Spring 2002

Eval Apply Eval and Apply are defined in terms of each other. 25 March 2002 CS 200 Spring 2002

meval (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)))) 25 March 2002 CS 200 Spring 2002

mapply (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)))) 25 March 2002 CS 200 Spring 2002

> (meval 3 the-global-environment) |(meval 3 (((+ primitive-procedure #<primitive:+>) (* primitive-procedure #<primitive:*>)))) |3 > (meval '(+ 2 2) the-global-environment) |(meval (+ 2 2) the-global-environment) | (meval + the-global-environment) | (primitive-procedure #<primitive:+>) | (meval 2 the-global-environment) | 2 | (mapply (primitive-procedure #<primitive:+>) (2 2)) | 4 |4 4 25 March 2002 CS 200 Spring 2002

Charge Defining eval and apply is the guts of it Wednesday we will see the details PS7 out Wednesday If you’ve done the reading and want to start it before then, send me email. This is powerful: once we have an metacircular evaluator, we can easily make changes to the language! 25 March 2002 CS 200 Spring 2002