Lecture 27: In Praise of Idleness CS200: Computer Science

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
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.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
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.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
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 CS150: Computer Science University of Virginia Computer Science Class 33: Computing with Photons From The.
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 CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
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.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 6: Lambda Calculus
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
6.001 SICP Variations on a Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lambda Calculus Revisited
Env. Model Implementation
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Nondeterministic Evaluation
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
Class 36: The Meaning of Truth CS200: Computer Science
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Variations on a Scheme
Class 24: Computability Halting Problems Hockey Team Logo
Class 31: Universal Turing Machines CS200: Computer Science
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Lecture 3: Rules of Evaluation CS200: Computer Science
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
Presentation transcript:

David Evans http://www.cs.virginia.edu/~evans Lecture 27: In Praise of Idleness CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans

Menu Lazy Scheme Quantum Computing 27 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. The parent of the new environment is the procedure’s environment; the frame is a new frame that contains places with the formal parameters bound to the arguments. 27 March 2002 CS 200 Spring 2002

Lazy Evaluation Don’t evaluate expressions until their value is really needed. We might save work this way, since sometime we don’t need the value of an expression We might change the meaning of some expressions, since the order of evaluation matters Not a wise policy for problem sets (the values will always be needed!) 27 March 2002 CS 200 Spring 2002

Lazy Examples laziness can be useful! > (meval ‘((lambda (x) 3) (* 2 2)) the-global-environment) 3 > (lazeval ‘((lambda (x) 3) (* 2 2)) the-global-env) > (meval ‘((lambda (x) 3) (* 2 2)) (car 3)) error – can’t take car of 3 > (lazeval ‘((lambda (x) 3) (car 3)) the-global-env) > (meval ‘((lambda (x) 3) (loop-forever)) the-global-env) no value – loops forever > (lazeval ‘((lambda (x) 3) (loop-forever)) the-global-env) laziness can be useful! 27 March 2002 CS 200 Spring 2002

In Praise of Idleness Ordinary men and women, having the opportunity of a happy life, will become more kindly and less persecuting and less inclined to view others with suspicion. The taste for war will die out, partly for this reason, and partly because it will involve long and severe work for all. Good nature is, of all moral qualities, the one that the world needs most, and good nature is the result of ease and security, not of a life of arduous struggle. Modern methods of production have given us the possibility of ease and security for all; we have chosen, instead, to have overwork for some and starvation for others. Hitherto we have continued to be as energetic as we were before there were machines; in this we have been foolish, but there is no reason to go on being foolish forever. Bertrand Russell, 1932 27 March 2002 CS 200 Spring 2002

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. The parent of the new environment is the procedure’s environment; the frame is a new frame that contains places with the formal parameters bound to the argument expressions. When a formal parameter is first used, evaluate the argument expression to get its value. 27 March 2002 CS 200 Spring 2002

Evaluation of Arguments Applicative Order (“eager evaluation”) Evaluate all subexpressions before apply The standard Scheme rule Normal Order (“lazy evaluation”) Evaluate arguments just before the value is needed Algol60, Haskell, Miranda 27 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)))) 27 March 2002 CS 200 Spring 2002

lazeval (define (lazeval 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) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

lazapply (define (lazapply procedure operands env) (cond ((primitive-procedure? procedure) (apply-primitive procedure operands)) ((compound-procedure? procedure) (lazeval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) operands (procedure-environment procedure)))) (else (error "Unknown applicator: " procedure)))) 27 March 2002 CS 200 Spring 2002

apply-primitive (define (apply-primitive procedure operands) ;;; The underlying Scheme apply (apply (primitive-procedure-procedure procedure) operands)) (define (lazapply-primitive procedure operands env) ;;; The underlying Scheme apply (apply (primitive-procedure-procedure procedure) (map (lambda (op) (lazeval op env)) operands))) 27 March 2002 CS 200 Spring 2002

lazeval (define (lazeval 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) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

lazeval (define (lazeval expr env) (cond ((self-evaluating? expr) expr) ((variable? expr) (lazeval (environment-lookup-name expr env) env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

Are we lazy? > (lazeval '((lambda (x) (* x x)) 3) the-global-env) 9 > (lazeval '(define loop-forever (lambda () (loop-forever))) the-global-env) ok > (lazeval '((lambda (x) 12) (loop-forever)) the-global-env) 12 > (meval '((lambda (x) ((lambda (x) (+ x x)) x)) 7) the-global-env) 14 > (lazeval '((lambda (x) ((lambda (x) (+ x x)) x)) 7) the-global-env) Doesn’t halt! 27 March 2002 CS 200 Spring 2002

> (lazeval '((lambda (x) ((lambda (x) (+ x x)) x)) 7) the-global-env) (((+ primitive-procedure #<primitive:+>) (* primitive-procedure #<primitive:*>) (- primitive-procedure #<primitive:->)))) | (lazeval (lambda (x) ((lambda (x) (+ x x)) x)) | (procedure (x) (((lambda (x) (+ x x)) x)) | (lazapply (procedure (7) (define (lazeval expr env) (cond ((self-evaluating? expr) expr) ((variable? expr) (lazeval (environment-lookup-name expr env) env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

Fixing Lazeval We need to only evaluate things once – the first time we need the value of a name We need to evaluate them in the correct environment Instead of putting the expression in the frame, make a new kind of object called a thunk that packages the expression with the evaluation environment. The first time we need the value associated with a name, evaluate the thunk and put the value in the name’s place. 27 March 2002 CS 200 Spring 2002

I thunk I can… (define (make-thunk expr env) (list 'thunk expr env)) (define (thunk? expr) (tagged-list? expr 'thunk)) (define (thunk-expr thunk) (cadr thunk)) (define (thunk-env thunk) (caddr thunk)) 27 March 2002 CS 200 Spring 2002

thunky lazeval (define (lazeval expr env) (cond ((application? expr) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) … 27 March 2002 CS 200 Spring 2002

thunky lazapply (define (lazapply procedure operands env) (cond ((primitive-procedure? procedure) (lazapply-primitive procedure (map (lambda (op) (lazeval op env)) operands))) ((compound-procedure? procedure) (lazeval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) (map (lambda (op) (make-thunk op env)) operands) (procedure-environment procedure)))) (else (error "Unknown applicator: " procedure)))) 27 March 2002 CS 200 Spring 2002

thunky names (define (lazeval expr env) (cond ((self-evaluating? expr) expr) ((variable? expr) (environment-get-value! expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((definition? expr) (define-variable! (definition-variable expr) (lazeval (definition-value expr) env) env)) ((application? expr) (lazapply (lazeval (application-operator expr) env) (application-operands expr) env)) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

environment-lookup-name (define (environment-lookup-name name env) (if (null? env) (error "No binding " name) (if (frame-contains? name (first-frame env)) (frame-lookup-name name (environment-lookup-name name (enclosing-environment env))))) 27 March 2002 CS 200 Spring 2002

environment-get-value! (define (environment-get-value! name env) (if (null? env) (error "No binding for" name) (if (frame-contains? name (first-frame env)) (let ((np (frame-lookup-name name (first-frame env)))) (if (thunk? (cdr np)) (set-cdr! np (lazeval (thunk-expr (cdr np)) (thunk-env (cdr np))))) (cdr np)) (environment-get-value! name (enclosing-environment env))))) 27 March 2002 CS 200 Spring 2002

Laziness is Good? > (lazeval '(define true (lambda (x y) x)) the-global-env) ok > (lazeval '(define false (lambda (x y) y)) the-global-env) > (lazeval '(define if (lambda (pred tbranch fbranch) (pred tbranch fbranch))) the-global-env) > (lazeval '(if true 3 (loop-forever)) the-global-env) 3 Problem Set 7, Question 2: why does this work in LazyScheme, but not in MiniScheme? 27 March 2002 CS 200 Spring 2002

Quantum Computing 27 March 2002 CS 200 Spring 2002

Quantum Mechanics for Dummies Light behaves like both a wave and a particle at the same time A single photon is in many states at once Observing its state forces it into one state Books Search Results: we were unable to find exact matches for your search for scheme for dummies 27 March 2002 CS 200 Spring 2002

Schrödinger’s Cat Put a live cat in a box with cyanide vial that opens depending on quantum state Cat is both dead and alive at the same time until you open the box (qeval ‘(define cat (quist “alive” “dead”))) (qeval ‘(observe (lambda (q) #t) cat) “dead” or “alive” Its only a thought experiment, not necessary to try with real cat! 27 March 2002 CS 200 Spring 2002

Charge Don’t let Lazy Scheme happen to you! Next week PS7 is long and hard – don’t wait to start it! Next week Another Scheme Variation: type checking 27 March 2002 CS 200 Spring 2002