Class 28: Interpreters cs1120 Fall 2009 David Evans.

Slides:



Advertisements
Similar presentations
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Digital Signatures and Hash Functions. Digital Signatures.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
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.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
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 CS150: Computer Science University of Virginia Computer Science Class 29: Making Primitives Class 29: Trick-or-Treat.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Guomin Yang et al. IEEE Transactions on Wireless Communication Vol. 6 No. 9 September
Class 30: Language Construction cs1120 Fall 2009 David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Class 31: Cookie Monsters and Semi-Secure.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
Class 29: Charme School cs1120 Fall 2009 University of Virginia David Evans.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS588: Security and Privacy University of Virginia Computer Science Lecture 15: From Here to Oblivion.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Functional Programming
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Variations on a Scheme
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
Lecture 37: A Universal Computer
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
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 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
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
6.001 SICP Interpretation Parts of an interpreter
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
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
David Evans Lecture 32: Truthiness “It would appear that we have reached the limits of what it is possible to achieve.
Lecture 25: The Metacircular Evaluator Eval Apply
Lecture 6 - Recursion.
Presentation transcript:

Class 28: Interpreters cs1120 Fall 2009 David Evans

Menu Introduction to Interpreters History of Object-Oriented Programming (time permitting)

“Trick or Treat” Protocols

Trick-or-Treater must convince victim that she poses a credible threat Need to prove you know are a qualified tricker But, revealing trickiness shouldn’t allow victim to impersonate a tricker Except around Halloween, this is called an “authentication protocol”.

Trick-or-Treat Trickers? “Trick or Treat?” “Prove it!” “The magic word is: shazam!” Victim Any problems with this?

Cryptographic Hash Functions One-way Given h, it is hard to find x such that H(x) = h. Collision resistance Given x, it is hard to find y  x such that H(y) = H(x).

Example One-Way-ish Function Input: two 100 digit numbers, x 1 and x 2 Output: the middle 100 digits of x 1  x 2 Input: two 100 digit numbers, x 1 and x 2 Output: the middle 100 digits of x 1  x 2 Given x = (x 1, x 2 ) : easy to calculate f (x). Given h = f (x) : hard to find an z such h = f (z). “Easy” means there is a procedure with running time in O(N 2 ) where N is number of digits “Hard” means (we hope) the fastest possible procedure has running time in  (2 N ).

Trick-or-Treat Trickers? “Trick or Treat?”, H (secret) Trickers Bureau H (secret) Valid! Ouch! Now victim knows enough to be a tricker: can just replay H (secret) to the next victim.

Trick-or-Treat Trickers? “Trick or Treat?” Trickers Bureau R, Z Valid! Challenge: Z R = H (secret + Z )

Trick-or-Treating without Calling the Tricker’s Bureau TRICKER’S LICENSE You know what you need to survive tomorrow....we’ll talk more about how this works on the web later.

Implementing Interpreters Problem Set 7: posted today, due Monday 9 November. Understand the Charme interpreter (described in Chapter 11) Modify it to create a new language Problem Set 7: posted today, due Monday 9 November. Understand the Charme interpreter (described in Chapter 11) Modify it to create a new language

Building a Language Design the grammar What strings are in the language? Use BNF to describe all the strings in the language Make up the evaluation rules Describe what every string in the language means Build an evaluator Implement a procedure that takes a string in the language as input and an environment and outputs its value: meval: String  Environment → Value

Is this an exaggeration? 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. Abelson and Sussman, Structure and Interpretation of Computer Programs (p. 360)

Building an Evaluator To build an evaluator we need to: – Figure out how to represent data in programs What is a procedure, frame, environment, etc. – Implement the evaluation rules For each evaluation rule, define a procedure that follows the behavior of that rule. Next: we’ll look at a high-level how the application rule is implemented Next week and Chapter 11: detailed walk-through of the interpreter Next: we’ll look at a high-level how the application rule is implemented Next week and Chapter 11: detailed walk-through of the interpreter

def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression type: ' + str(expr)) Core of the evaluator: meval

To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. Stateful Application Rule

Eval Apply Eval and Apply are defined in terms of each other.

To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. evalApplication def evalApplication(expr, env): subexprs = expr subexprvals = map (lambda sexpr: meval(sexpr, env), subexprs) return mapply(subexprvals[0], subexprvals[1:])

To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. def mapply(proc, operands): if (isPrimitiveProcedure(proc)):... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment())... class Environment: def __init__(self, parent): self._parent = parent self._frame = {} def addVariable(self, name, value):... def lookupVariable(self, name):...

To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. def mapply(proc, operands): if (isPrimitiveProcedure(proc)):... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i])...

To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. To apply a constructed procedure: 1.Construct a new environment, whose parent is the environment of the applied procedure. 2.For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment of the application and initialize the value in each place to the value of the corresponding operand expression. 3.Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application. def mapply(proc, operands): if (isPrimitiveProcedure(proc)):... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) return meval(proc.getBody(), newenv)

Charge Read Chapter 11 PS7 posted today, due Monday, Nov 9 Remember to make “Trick-or-Treaters” to solve your challenge!