Unformatted text preview:

PowerPoint PresentationMenuLast Time: Mini-SchemeHandling PrimitivesHandling Primitives: EvalPrimitives: EnvironmentMini-Scheme ExamplesAn Entire Mini-Scheme Interpreter!All Programs are Really Language ImplementationsWhat is a Language Implementation?Examples of Language ImplementationsLanguage ImplementationsSlide 13Changing LanguagesScheme EvaluationEvaluation OrderLaziness can save workLaziness Can Be UsefulLazy Scheme EvaluationReally Needed?Lazy EvalUnthunkingSlide 23Some Handy DefinitionsLazy EvaluationsForcing EvaluationLess Lazy EvaluationsDebugging in SchemeTrace RunEdited Trace: (geval '((lambda (x) 3) (nt 7)))Challenge #2ChargeLecture 5: Lazy Scheme(Thunking about Thunks)David Evanshttp://www.cs.virginia.edu/~evansCS655: Programming LanguagesUniversity of VirginiaComputer Science1 Feb 2001 CS 655: Lecture 5 2Menu•Finish Mini-Scheme Interpreter•Changing Language Semantics1 Feb 2001 CS 655: Lecture 5 3Last Time: Mini-Scheme(define (apply proc operand env)(bind-variable (car (car (cdr proc))) operand (extend-environment env)))))(define (eval expr env) (if (number? expr) expr (if (symbol? expr) (lookup-variable-value expr env) (if (and (list? expr) (eq? (car expr) 'lambda)) (list 'procedure (car (cdr expr)) (car (cdr (cdr expr)))) (apply (eval (car expr) env) (eval (car (cdr expr)) env) env)))))1 Feb 2001 CS 655: Lecture 5 4Handling Primitives;; represented by (primitive func)(define (apply proc operand env) (if (eq? (car proc) 'primitive) ((car (cdr proc)) operand) same as before]1 Feb 2001 CS 655: Lecture 5 5Handling Primitives: Eval(define (eval expr env) (if (or (number? expr) (and (list? expr) (eq? (car expr) 'primitive)) expr rest is same]1 Feb 2001 CS 655: Lecture 5 6Primitives: Environment(define global-env (bind-variable 'minus (list 'primitive -) (bind-variable 'inc (list 'primitive (lambda (x) (+ x 1))) '(()))))1 Feb 2001 CS 655: Lecture 5 7Mini-Scheme Examples(eval ‘(minus 3) global-env)-3(eval '((lambda (x) (inc x)) 3) global-env)41 Feb 2001 CS 655: Lecture 5 8An Entire Mini-Scheme Interpreter!(define (apply proc operand env) (if (eq? (car proc) 'primitive) ((car (cdr proc)) operand) (eval (car (cdr (cdr proc))) (bind-variable (car (car (cdr proc))) operand(extend-environment env)))))(define (eval expr env) (if (or (number? expr) (and (list? expr) (eq? (car expr) 'primitive))) expr (if (symbol? expr) (lookup-variable-value expr env) (if (and (list? expr) (eq? (car expr) 'lambda)) (list 'procedure (car (cdr expr)) (car (cdr (cdr expr)))) (apply (eval (car expr) env) (eval (car (cdr expr)) env) env)))))1 Feb 2001 CS 655: Lecture 5 9All Programs are Really Language ImplementationsRecall our definition of a language (Lecture 1): A description of pairs (S, M), where S stands for sound, or any kind of surface forms, and M stands for meaning. A theory of language must specify the properties of S and M, and how they are related.1 Feb 2001 CS 655: Lecture 5 10What is a Language Implementation? A description of pairs (S, M), where S stands for sound, or any kind of surface forms, and M stands for meaning. A theory of language must specify the properties of S and M, and how they are related. An implementation of a language is a function from S to M.1 Feb 2001 CS 655: Lecture 5 11Examples of Language Implementations•Our Mini-Scheme Interpreter (eval)S: <expression, environment>M: value = number | procedureeval: S  M1 Feb 2001 CS 655: Lecture 5 12Language Implementations•Mystery Language ImplementationS: HTML x user inputM: pixels on screen x user input  HTTP Web Browser: S  M•Mystery Language Implementation:S: user input x network inputM: pixels on screen x network output Doom: S  M1 Feb 2001 CS 655: Lecture 5 13Language Implementations•My GyromouseS: physical gestures in space x mouse clicksM: Windows eventsGyromouse and software: S  M1 Feb 2001 CS 655: Lecture 5 14Changing Languages•Change S–Allow new surface forms - new surface forms map to the old surface forms, and then to the old meanings–Gyromouse (without its software) is just a new surface form for the regular mouse•Change M–Change the meaning of surface forms–Gyromouse recognizes special gestures, and assigns them meanings not available using regular mouse1 Feb 2001 CS 655: Lecture 5 15Scheme Evaluation1. To evaluate a compound expression, evaluate the subexpressions, then apply the value of the first subexpression to the values of the other subexpressions.2. To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to.1 Feb 2001 CS 655: Lecture 5 16Evaluation Order•Applicative Order (Eager) Evaluation–Procedure operands are evaluated when the procedure is applied•“To evaluate a compound expression, evaluate the subexpressions ...”•Normal Order (Lazy) Evaluation–Evaluate and expression only when you really need its value1 Feb 2001 CS 655: Lecture 5 17Laziness can save work•Eager Evaluation (Applicative)–Read all assignments as soon as they are assigned•Lazy Evaluation (Normal)–Read something only when you absolutely need it to do the problem set•Lazy Evaluation requires less total work since some reading assignments may not be necessary to do problem set (but is not recommended for students in this class)1 Feb 2001 CS 655: Lecture 5 18Laziness Can Be Useful(define (p x) (p x))(define (f x) 3)(f (p 6))Eager: no value (does infinite work)Lazy: 3never needs to evaluate (p 6)1 Feb 2001 CS 655: Lecture 5 19Lazy Scheme Evaluation1. To evaluate a compound expression, evaluate the first subexpression, then apply the value of the first subexpression to the other subexpressions.2. To apply a procedure to a list of arguments, evaluate the procedure in a new environment that binds the formal parameters of the procedure to the arguments it is applied to. Evaluate an argument when its value is really needed.1 Feb 2001 CS 655: Lecture 5 20Really Needed?•The value of an expression is really needed when:–It is passed to a primitive procedurePrimitive procedures are “strict”.–It needs to be printed for human•Until then, we just need something (called a thunk) we can evaluate when necessary to produce the value we would have gotten if we evaluated it at first application1 Feb 2001 CS 655: Lecture 5 21Lazy Eval(define (eval expr env) (if (or (number? expr) (and (list? expr) (eq?


View Full Document

UVA CS 655 - Lecture 5: Lazy Scheme

Download Lecture 5: Lazy Scheme
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5: Lazy Scheme and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5: Lazy Scheme 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?