Lecture 3: Rules of Evaluation CS150: Computer Science

Slides:



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

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
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.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
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.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
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.
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 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.
1 Prof.Roseline WEEK-4 LECTURE -4 SYNTAX. 2 Prof.Roseline Syntax Concentrate on the structure and ordering of components within a sentence Greater focus.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 2: Formal Systems and Languages MU!
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.
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/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 8: Recursing Recursively Richard Feynman’s.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
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
Lambda Calculus Revisited
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
Functional Programming
Dynamic Scoping Lazy Evaluation
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
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.
Class 31: Universal Turing Machines 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
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 CS150: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Menu Language Elements Why don’t we just program computers using English? Evaluation Procedures

Are there any non-recursive natural languages Are there any non-recursive natural languages? What would happen to a society that spoke one? Not for humans at least. They would run out of original things to say. Chimps and Dolphins are able to learn non-recursive “languages” (some linguists argue they are not really “languages”), but only humans can learn recursive languages.

“Its all been said before.” Running out of Ideas “Its all been said before.” Eventually true for a non-recursive language. Never true for a recursive language. There is always something original left to say!

Language Elements When learning a foreign language, which elements are hardest to learn? Primitives: lots of them, and hard to learn real meaning Means of Combination Complex, but, all natural languages have similar ones [Chomsky] SOV (45% of all languages) Sentence ::= Subject Object Verb (Korean) SVO (42%) Sentence ::= Subject Verb Object VSO (9%) Sentence ::= Verb Subject Object (Welsh) “Lladdodd y ddraig y dyn.” (Killed the dragon the man.) OSV (<1%): Tobati (New Guinea) Schemish: Expression ::= (Verb Object) Means of Abstraction: few of these, but tricky to learn differences across languages English: I, we Tok Pisin (Papua New Guinea): mi (I), mitupela (he/she and I), mitripela (both of them and I), mipela (all of them and I), yumitupela (you and I), yumitripela (both of you and I), yumipela (all of you and I)

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)

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)

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)

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) C++ Core language issues list has 529 items!

English Primitives Means of Combination Means of Abstraction Pages in Revised5 Report on the Algorithmic Language Scheme English Primitives Standard Procedures Primitive expressions Identifiers, numerals 18 2 1 Morphemes Words in Oxford English Dictionary ? 500,000 Means of Combination Expressions Program structure Grammar Rules English Grammar for Dummies Book 100s (?) 384 pages Means of Abstraction Definitions ½ Pronouns ~20 48 pages total (includes formal specification and examples)

Why don’t we just program computers using English?

Why don’t we just program computers using English? Too hard and complex Non-native English speakers don’t need convincing. The rest of you have spent your whole life learning English (and first 5 years of your life doing little else) and still don’t know useful words like floccipoccinihilipilification! There are thoughts that even native speakers find it hard to express. By the end of today you will know enough Scheme (nearly the entire language) to express and understand every computation. By PS7, you will know enough to completely and precisely describe Scheme in terms of itself (try doing that in English!)

Why don’t we just program computers using English? Not concise enough English: To find the maximum of two numbers, compare them. If the first number is greater than the second number, the maximum is the first number. Otherwise, the maximum is the second number. Scheme: (define (max a b) (if (> a b) a b))

Why don’t we just program computers using English? Limited means of abstraction There are only a few pronouns: he, she, it, they, these, … (English doesn’t even have a gender-neutral pronoun for a person!) Only Webster and Oxford can make up new ones. define allows any programmer to make up as many pronouns as she wants, and use them to represent anything.

Why don’t we just program computers using English? Mapping between surface forms and meanings are ambiguous and imprecise Would you rather be paid biweekly or every week? The exact meaning(s) of every Scheme expression is determined by simple, unambiguous rules we will learn today (and refine later in the course).

Essential Scheme Expression ::= (Expression1 Expression*) Expression ::= (if Expression1 Expression2 Expression3 ) Expression ::= (define name Expression) Expression ::= Primitive Primitive ::= number Primitive ::= + | - | * | … Primitive ::= … Grammar is clear, just follow the replacement rules. But what does it all mean?

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

Evaluation Rule 1: Primitives If the expression is a primitive, it is self-evaluating. > 2 2 > #t #t > + #<primitive:+>

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

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 … )

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.

Making Procedures lambda means “make a procedure” Expression ::= (lambda (Parameters) Expression) Parameters ::= Parameters ::= Name Parameters

Lambda Example: Tautology Function make a procedure with no parameters with body #t (lambda () #t) > ((lambda () #t) 150) #<procedure>: expects no arguments, given 1: 150 > ((lambda () #t)) #t > ((lambda (x) x) 150) 150

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

Eval Apply Eval and Apply are defined in terms of each other. Without Eval, there would be no Apply, Without Apply there would be no Eval! Eval Apply

All of Scheme Once you understand Eval and Apply, you can understand all Scheme programs! Except: We have special Eval rules for special forms (like if) Evaluation Rule 4: If it is a special form, do something special.

Evaluating Special Forms Eval 4-if. If the expression is (if Expression0 Expression1 Expression2) evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression2. Otherwise, the value of the if expression is the value of Expression1. Eval 4-lambda. Lambda expressions self-evaluate. (Do not do anything until it is applied.)

More Special Forms Eval 4-define. If the expression is (define Name Expression) associate the Expression with Name. Eval 4-begin. If the expression is (begin Expression0 Expression1 … Expressionk) evaluate all the sub-expressions. The value of the begin expression is the value of Expressionk.

Scheme Expression ::= Primitive Eval 1: If the expression is a primitive, it is self-evaluating. Expression ::= Name Eval 2: If the expression is a name, it evaluates to the value associated with that name. Expression ::= (Expression ExpressionList) Eval 3: If the expression is an application:     (a) Evaluate all the subexpressions (in any order)     (b) Apply the value of the first subexpression to the values of all the other subexpressions. ExpressionList ::= ExpressionList ::= Expression ExpressionList

Special Forms Expression ::= (lambda (Parameters) Expression) Eval 4-lambda. Lambda expressions self-evaluate. Parameters ::= Parameters ::= Name Parameters Expression ::= (define Name Expression) Eval 4-define. If the expression is (define Name Expression) associate the Expression with Name. Expression ::= (if Expression0 Expression1 Expression2) Eval 4-if. Evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression2. Otherwise, the value of the if expression is the value of Expression1. Expression ::= (begin ExpressionList Expression) Eval 4-begin. Evaluate all the sub-expressions. The value of the begin expression is the value of Expression.

Now, you know all of Scheme! (Except for 3 more special forms you will learn for PS5.)

Charge PS1 Due Wednesday Reading for Friday: SICP, 1.2 Staffed Lab hours: today, 3:30-5pm Tuesday, 4-5:30pm Reading for Friday: SICP, 1.2 Reading for Monday: GEB, Ch 5