Lecture 3: Rules of Evaluation CS200: Computer Science

Slides:



Advertisements
Similar presentations
Class 3: Rules of Evaluation David Evans cs1120 Fall 2009.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
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 CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
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 Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
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.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 6: Lambda Calculus
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 22: Inheritance CS150: Computer Science University of Virginia
Class 27: Universal Turing Machines CS150: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
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.
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lecture 11: All Sorts CS200: Computer Science University of Virginia
Lecture 28: Types of Types
Functional Programming
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
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
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.
Material in the textbook Sections to 1.2.1
Class 31: Universal Turing Machines CS200: Computer Science
Class 34: Models of Computation CS200: Computer Science
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/~evans Lecture 3: Rules of Evaluation CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans

Menu Unresolved Questions from Lecture 2 Evaluation Procedures Origami Programming 23 January 2002 CS 200 Spring 2002

Language Elements When learning a foreign language, which of primitives, means of combination or means of abstraction is hardest to learn? Primitives There are a lot of them Learning the real meaning is hard Means of Combination They are complex But, all languages have similar ones! Chomsky Universal Grammar Means of Abstraction Few of these, but tricky to learn differences across languages 23 January 2002 CS 200 Spring 2002

Primitives Means of Combination Means of Abstraction Pages in Revised5 Report on the Algorithmic Language Scheme Primitives Means of Combination Means of Abstraction 48 pages total (includes formal specification and examples) 23 January 2002 CS 200 Spring 2002

Primitives Means of Combination Means of Abstraction Pages in Revised5 Report on the Algorithmic Language Scheme Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 Means of Combination Expressions Program structure Means of Abstraction Definitions ½ 48 pages total (includes formal specification and examples) 23 January 2002 CS 200 Spring 2002

Primitives Means of Combination Means of Abstraction Pages in Revised5 Report on the Algorithmic Language Scheme Pages in C++ Language Specification (1998) Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 Means of Combination Expressions Program structure Means of Abstraction Definitions ½ 48 pages total (includes formal specification and examples) 23 January 2002 CS 200 Spring 2002

Primitives Means of Combination Means of Abstraction Pages in Revised5 Report on the Algorithmic Language Scheme Pages in C++ Language Specification (1998) Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 356 30 10 Means of Combination Expressions Program structure Expressions, Statements Program Structure 197 35 Means of Abstraction Definitions ½ Declarations, Classes 173 48 pages total (includes formal specification and examples) 776 pages total (includes no formal specification or examples) Core language issues list has 313 items! 23 January 2002 CS 200 Spring 2002

Evaluation

Expressions and Values (Almost) every expression has a value Have you seen any expressions that don’t have values? When an expression with a value is evaluated, its value is produced 23 January 2002 CS 200 Spring 2002

DrScheme When you press “Execute”, DrScheme will evaluate all the expressions in the Definitions window When you type return in the Interactions Window, DrScheme evaluates the expression at the last prompt, and prints its value in blue. 23 January 2002 CS 200 Spring 2002

Evaluation Rule 1: Primitives If the expression is a primitive, it is self-evaluating. > 2 2 > #t #t > + #<primitive:+> 23 January 2002 CS 200 Spring 2002

Evaluation Rule 2: Names If the expression is a name, it evaluates to the value associated with that name. > (define two 2) > two 2 23 January 2002 CS 200 Spring 2002

Evaluation Rule 3: Application If the expression is an application: Evaluate all the subexpressions of the combination (in any order) Apply the value of the first subexpression to the values of all the other subexpressions. (expression0 expression1 expression2 … ) 23 January 2002 CS 200 Spring 2002

Rules for Application If the procedure to apply is a primitive, just do it. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. 23 January 2002 CS 200 Spring 2002

Making Procedures lambda means “make a procedure” Expression ::= (lambda (Parameters) Expression) Parameters ::= Parameters ::= Name Parameters 23 January 2002 CS 200 Spring 2002

Lambda Example: Tautology Function make a procedure with no parameters with body #t (lambda () #t) > ((lambda () #t) 200) #<procedure>: expects no arguments, given 1: 200 > ((lambda () #t)) #t > ((lambda (x) x) 200) 200 23 January 2002 CS 200 Spring 2002

You’ve Already Used Lambda! (define (closer-color? sample color1 color2) Expr) is a shortcut for: (define closer-color? (lambda (sample color1 color2) Expr)) 23 January 2002 CS 200 Spring 2002

A Squarish Example (define square (lambda (x) (* x x))) (square 4) Its an application, use evaluation Rule 3: Evaluate all the subexpressions of the combination (in any order) Apply the value of the first subexpression to the values of all the other subexpressions. 23 January 2002 CS 200 Spring 2002

(lambda (x) (* x x)) (define square (lambda (x) (* x x))) (square 4) Its an application, use evaluation Rule 3: Evaluate all the subexpressions of the combination (in any order) Apply the value of the first subexpression to the values of all the other subexpressions. Evaluate: square It’s a name, use evaluation Rule 2: If the expression is a name, evaluate the expression associated with that name. (lambda (x) (* x x)) 23 January 2002 CS 200 Spring 2002

(lambda (x) (* x x)) 4 (define square (lambda (x) (* x x))) (square 4) Its an application, use evaluation Rule 3: Evaluate all the subexpressions of the combination (in any order) Apply the value of the first subexpression to the values of all the other subexpressions. Evaluate: square (lambda (x) (* x x)) Evaluate: 4 Rule 1: If the expression is a primitive, it is self-evaluating 4 23 January 2002 CS 200 Spring 2002

(lambda (x) (* x x)) (define square (lambda (x) (* x x))) (square 4) Its an application, use evaluation Rule 3: Evaluate all the subexpressions of the combination (in any order) Apply the value of the first subexpression to the values of all the other subexpressions. Apply: square (lambda (x) (* x x)) to 4 Application Rule 2. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. 23 January 2002 CS 200 Spring 2002

(lambda (x) (* x x) ) Apply: square to 4 Body of the procedure Application Rule 2. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. 23 January 2002 CS 200 Spring 2002

(lambda (x) (* x x) ) (* 4 4) Apply: square to 4 Body of the procedure Application Rule 2. If the procedure is a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. (* 4 4) 23 January 2002 CS 200 Spring 2002

(* 4 4) To be continued… Next time… evaluating 23 January 2002 CS 200 Spring 2002

Charge PS1 Due Friday Staffed Lab Hours Tonight Reading for next week: 5-9 in Cocke 6-9 in Small Hall Reading for next week: GEB Chapter 5 SICP Section 1.2 23 January 2002 CS 200 Spring 2002