Class 4: Making Procedures

Preview:

DESCRIPTION

LambdaMaking and Applying ProceduresSVO/SOV/OVS Languages

Citation preview

Class 4: Making Procedures

cs1120 Fall 2011David Evans30 August 2011

Announcements

PS1 is due Monday (electronic submission and paper submission): don’t wait to get started!

Quiz 1 is Wednesday (in class)Chapters 1-4 of Course BookChapters 1-3 of The InformationClasses 1-5 (including questions from class notes)

I haven’t forgotten about answering your questions from PS0. I will post my answers by tomorrow.

3

Recap: Assigning MeaningsProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression

| ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= Name ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressionsProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name ParametersIfExpression ::= (if ExpressionPred ExpressionConsequent ExpressionAlt)

This grammar generates (nearly) all surface forms in the Scheme. language. If we have a meaning rule for each grammar rule, we can determine the meaning of every Scheme program.

4

Evaluation Rules: Last ClassPrimitiveExpression ::= Number | true | false| PrimitiveProcedure

Rule 1: If the expression is a primitive, it evaluates to its pre-defined value.

NameExpression ::= Name

Rule 2: A name evaluates to the value associated with that name.ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressions

Rule 3: To evaluate an application expression:a) Evaluate all the subexpressions (in any order)b) Apply the value of the first subexpression to the values of all the other

subexpressions.

5

Last class: Rules for Application1. Primitives. If the procedure to apply is a

primitive procedure, just do it.

2. Constructed Procedures. If the procedure is a constructed procedure, evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

This only makes sense if we know what a constructed procedure is!

6

Constructing ProceduresProgram ::= ε | ProgramElement ProgramProgramElement ::= Expression | DefinitionDefinition ::= (define Name Expression)Expression ::= PrimitiveExpression | NameExpression

| ApplicationExpression | ProcedureExpression | IfExpressionPrimitiveExpression ::= Number | true | false| PrimitiveProcedureNameExpression ::= Name ApplicationExpression ::= (Expression MoreExpressions)MoreExpressions ::= ε | Expression MoreExpressions

ProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name ParametersIfExpression ::= (if ExpressionPred ExpressionConsequent ExpressionAlt)

7

Constructing Procedures

lambda means “make a procedure”

Expression ::= ProcedureExpressionProcedureExpression ::= (lambda (Parameters) Expression) Parameters ::= εParameters ::= Name Parameters

8

Evaluation Rule 4: Lambda

A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

ProcedureExpression ::= (lambda (Parameters) Expression)Parameters ::= ε | Name Parameters

Lambda Expressions

(lambda () true)

(lambda (x) (* x x))

(lambda (a) (lambda (b) (+ a b)))

Applying Compound Procedures

((lambda () true) 1120)

Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

Applying Compound Procedures

((lambda (x) (+ x 1000)) 120)

Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

Applying Compound Procedures

((lambda (x) (+ x 1000)) x)

Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

Applying Compound Procedures

((lambda (a) (lambda (b) (+ a b))) 5)

Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

Applying Compound Procedures

(((lambda (a) (lambda (b) (+ a b))) 5) 6)

Rule 3: To evaluate an application expression: (a) Evaluate all the subexpressions (in any order) (b) Apply the value of the first subexpression to the values of all the other subexpressions.

Apply Rule for constructed procedures: Evaluate the body of the procedure with each parameter name bound to the corresponding input expression value.

Evaluation Rule 4. A lambda expression evaluates to a procedure that takes the given parameters and has the expression as its body.

Do we have everything we need to describe all

computations?

16

Language Elements

Question from Class 2:When learning a foreign language, which

elements are hardest to learn?

PrimitivesMeans of CombinationMeans of Abstraction

17

Primitives: lots of them, and hard to learn real meaning (but its just memorization)

Means of CombinationComplex, but, all natural languages have similar ones [Chomsky]

Sentence ::= Subject Object Verb (45%)

Sentence ::= Subject Verb Object (42%)

Sentence ::= Verb Subject Object (9%)

Sentence ::= Object Subject Verb (<1%)

Scheme:

Means of Abstraction: few of these, but tricky to learn differences across languagesTok 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)

Scheme:

Welsh: “Lladdodd y ddraig y dyn.”

Charge

PS1: Due MondayWednesdays, 5-6:30pm (Jiamin, Rice 1xx)Thursdays, 9:45-11am (Dave, Rice 507)Thursdays, 10-11:30am (Peter, Rice 1xx)Thursdays, 1-2:30pm (Joseph, Rice 1xx)Thursdays, 4:30-6pm (Jonathan, Rice 1xx)Thursdays, 6-7:30pm (Jiamin, Rice 1xx)

By FridayFinish reading Chapters 1-4, Gleick Chapters 1-3

Quiz 1: Wednesday