introduction to computer science i topic 4: evaluation order and lexical scoping

28
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 4: Evaluation Order and Lexical Scoping Prof. Dr. Max Mühlhäuser Dr. Guido Rößling

Upload: shilah

Post on 24-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Computer Science I Topic 4: Evaluation Order and Lexical Scoping. Prof. Dr. Max Mühlhäuser Dr. Guido Rößling. Applicative vs. Normal Evaluation Order revisited. Review: Applicative order : First evaluate the operator and all operands, then substitute - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Telecooperation/RBG

Technische Universität Darmstadt

Copyrighted material; for TUD student use only

Introduction to Computer Science ITopic 4: Evaluation Order and Lexical ScopingProf. Dr. Max MühlhäuserDr. Guido Rößling

Page 2: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Applicative vs. Normal Evaluation Order revisited

• Review: – Applicative order: First evaluate the operator and all

operands, then substitute– Normal order: Evaluate operator, then substitute the

(not evaluated) operands for the formal arguments of the operator

• Stated earlier: – The result does not depend on the order of evaluation, if

a result exists– Now we want to look at this last point in more detail

2

Page 3: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Applicative vs. Normal Evaluation Order revisited

• Example: a “self-made” if-statement modeled as a procedure using “cond”

– Seems to work fine:(my-if true 3 5) 3(my-if false 3 5) 5

• Now consider the following program:

– Let us try to replace if by my-if3

(define (my-if test then-exp else-exp) (cond (test then-exp) (else else-exp)))

;; ! : N -> N;; computes the factorial function(define (! n) (if (zero? n) 1 (* n (! (pred n)))))

Page 4: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Applicative vs. Normal Evaluation Order revisited

• Using my-if instead of if

• A call such as (! 2) – does not terminate when using applicative evaluation

order, – but with normal order it does

• Why?

4

;; ! : N -> N;; computes the factorial function(define (! n) (my-if (zero? n) 1 (* n (! (pred n)))))

Page 5: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Applicative vs. Normal Evaluation Order revisited

• Many of the special forms in Scheme would not have to be special forms if Scheme used the normal evaluation order– e.g. if, cond, and, or– This would simplify the semantics of the language– Normal evaluation order allows for very elegant programming

techniques (e.g. “streams”)

• So why does Scheme (as most other languages) use applicative evaluation?– Some features such as assignments or I/O (later…) destroy

confluence (the evaluation order affects the result)– In this case, we prefer the applicative evaluation order, because

it is more transparent when an expression is evaluated– However, there are some modern techniques (i.e. so-called

“monads”) that allow assignments, I/O, etc., while maintaining independence of the order of evaluation

• This is part of the graduate course “Concepts of Programming Languages”

5

Page 6: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Block Structure• We have discussed the value of auxiliary procedures• However, there are some problems with their use:

1. You can quickly get lost in your code if the number of procedures gets too large

2. Every procedure „costs“ a name, i.e., the namespace shrinks• risk of confusion or name conflicts rises

3. A lot of data must be explicitly passed as parameters to the auxiliary functions

• That is why it is possible to define local names and bind them to values or procedures

• Before we discuss the means in Scheme for doing so, let us review the subset of the Scheme language we have been using so far in a more formal way…

6

Page 7: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

7

Intermezzo:

Syntax & Semantics of Scheme

Page 8: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

The syntax of a language• Like any spoken language, a programming

language also has a vocabulary and a grammar:– The vocabulary is the collection of those “basic

words” from which we can compose “sentences” in our language.

– A sentence in a programming language is an expression or a function

– The language grammar dictates how to form complete sentences from words.

• The term syntax refers to the vocabularies and grammars of programming languages

8

Page 9: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

The Semantics of a language• Not all grammatically correct sentences are

meaningful– Neither in English nor in a programming language. – The sentence “The cat is black” is a meaningful sentence– But “the brick is a car” makes no sense, even though it is

grammatically correct

• To determine whether or not a sentence is meaningful, we must study the meaning, or semantics, of words and sentences – For programming languages, there are several ways to

explain the meaning of individual sentences/expressions– We discuss the meaning of Scheme programs through an

extension of the familiar laws of arithmetic and algebra (substitution model)

9

Page 10: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Scheme Vocabulary (Syntax)

• Four categories of words, each defined by a line:– Variables (<var>): the names of functions and values – Functions (<fct>): the names of functions – Constants (<con>): boolean, symbols, and numeric

constants – Primitive operations (<prm>): The basic functions that

Scheme provides from the very beginning• Notation:

– Lines enumerate simple examples separated by “|” – Dots indicate that there are more things of the same

kind in some category 10

<var><fct>

==

x | a | alon | …area-of-disk | perimeter | ...

<con> = true | false |'a | 'doll | 'sum | ... 1 | -1 | 3/5 | 1.22 | ...

<prm> =  + | - | ...

Page 11: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

<def> =   (define (<fct> <var> ...<var>) <exp>)

| (define <var> <exp>)

| (define-struct <var0> (<var-1> ...<var-n>))

<exp>

=   <var>

| <con>

| (<prm> <exp> ...<exp>)

| (<fct> <exp> ...<exp>)

| (cond [<exp> <exp>] ... [<exp> <exp>])| (cond [<exp> <exp>] ... [else <exp>])| (and <exp> <exp>)

| (or <exp> <exp>)| (if <exp> <exp> <exp>)

Beginning Student Scheme: The Full Grammar (Syntax)

11

Two categories of phrases:definitions (<def>) and expressions (<exp>)

This grammar was simplified; not all possible expressions are actually valid!

Correct number

of argument

s

Can be empty

Page 12: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Scheme Grammar: Examples• Examples of expressions:

– 'all: symbol, hence, an expression– x: every variable is an expression– (f x): a function application, because x is a variable

• The following sentences are not correct expressions: – (f define): partially matches shape of a function

application but uses define as if it were a variable – (cond x): fails to be a correct cond-expression because

it contains a variable as the second item, not a pair of expressions surrounded by parentheses

– (): grammar requires that every left parenthesis is followed by something other than a right parenthesis

12

Page 13: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Grammatical Terminology• A definition (<def>) consists of:

– Header: The 2nd component of a definition, i.e., the non-empty sequence of variables

– Parameters of a function: The variables that follow the first variable in a header

– Body: The expression component of a definition • An application consists of

– Function: The first component– Arguments (actual arguments): the remaining

components

• A cond-expression consists of cond-lines (cond-clauses) each consisting of two expressions: – Question (condition) and answer

13

(cond (<question> <answer>) <cond-clause> ...)

(define (<function-name> <parameter> ...<parameter>) <body>)

(<function-name> <argument> ...<argument>)

Page 14: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Syntax of the local-expression

• Local definition: parenthesized sequence following the keyword local – Definitions in the local definition block are called

“LOCALLY DEFINED” variables, functions, or structures.

• Definitions in the Definitions window are called “TOP-LEVEL DEFINITIONS”

– Each name may occur at most once on the left-hand side, be it in a variable definition or a function definition.

– The expression in each definition is called the RIGHT-HAND SIDE expression

• Body: expression (<exp>) following the definitions

14

<exp> = ... | ( local (<def-1> ...<def-n>) <exp> )

(local ((define PI 3)) (* PI 5 5))

Page 15: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

End Intermezzo:

Syntax & Semantics of Scheme

15

Page 16: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Block Structure: Example

• two locally defined procedures: f and g• g calls f • body calls g

16

(local ( (define (f x) (+ x 5)) (define (g alon)

(cond [(empty? alon) empty] [else

(cons (f (first alon)) (g (rest alon)))]))) (g (list 1 2 3)))

BODY

LOCAL DEFINITION

Page 17: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Properties of Local Definitions• The body of the local definition can access names

defined outside its scope– Auxiliary procedure plus-y-sqr can access y

• When using a non-local definition, y would have to be passed explicitly

• Thus, local definitions can lead to less parameters

• Locally defined names are not visible from the outside– The same name can be re-used in a different local

definition17

(define (f x y z) (local ( (define (square n) (* n n)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z))))

(x+y)² + (z+y)²

Page 18: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Properties of Local Definitions• By being just a new type of an expression, a local

expression can be used universally where an expression is expected – as an operator or operand or as a body of a procedure– closure property

• In particular, local definitions can be nested arbitrarily (so-called block structure)– e.g., in the body of a local expression, or a local definition– scalability

18

(define (f x y z) (local ( (define (plus-y-sqr q) (local ( (define (square n) (* n n))) (square (+ q y)))))

(+ (plus-y-sqr x) (plus-y-sqr z))))

(x+y)² + (z+y)²

Page 19: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Properties of Local Definitions• An important property of local definitions is lexical

scoping• Scoping is the strategy for associating a name to a

definition• Lexical scoping means that the next definition in the

nesting structure is used– For example, we could rename the parameter of square from

n to z– However, z will not be confused in the body of square with the

parameter z of function f

1919

(define (f x y z) (local ( (define (square z) (* z z)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z))))

(x+y)² + (z+y)²

Conceals outer z inside of square

Page 20: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Lexical scoping• It is important to distinguish between

name binding, bound names and free names to understand lexical scoping– „free“ and „bound“ are always relative to an

expression

20

(local ( (define (square x) (* x x)) (define (ge q) (square (+ q y)))) (+ (ge x) (ge z))))

name binding

free name bound name

Page 21: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Lexical scoping• The scope of a name binding is the textual

region in which an occurrence of the name relates to that name binding– Top-level definitions have global scope– The scope of a procedure parameter is the body of the

procedure– The scope of a local definition is the expression (last

operand) in the local definition

• As we have seen, there can be “holes” in the scope of a name binding– When a name binding is concealed by a binding of the

same name

21

Page 22: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Try the lexical scoping with the „Check Syntax“ Feature of

DrScheme

22

Page 23: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

(define (f x ) (* x x)) (f 5 )

Reminder: Evaluation Rule for Procedures

• Evaluation rule for procedures

– The procedure is a primitive procedure → execute the respective machine instructions.

– The procedure is a compound procedure • Evaluate the procedure body;• substitute each formal parameter with the

respective current value that is passed when applying the procedure.

23

Page 24: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Parameter substituion and lexical scope

• Do not blindly substitute parameters by the current values!– Keep the scope rules in mind– Only substitute occurrences of parameters names in

the scope of the parameter definition– More precisely: Only the occurrences of the name

which are free in the procedure body must be substituted

• Example:– x is not substituted by 2 in square

• x is not free in square

24

(define (f x y z) (local ( (define (square x) (* x x)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z))))

(f 2 3 4)

Page 25: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Evaluation of local blocksin the substitution model

• We need to extend the substitution model to evaluate local blocks– Until now, we do not have a rule for local blocks

• Idea: Local definitions are “drawn” to Top-Level– During this process, we have to assign “fresh”

names– This must be done in every evaluation of the local

definition• Can not be done statically

25

Page 26: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Evaluation of local blocks: Example

26

Step 1: Renaming local names with “fresh” names

Step 2: Extracting local definitions to the topmost level

For the remaining evaluation, we can proceed according to the existing substitution model

(define y 10)(+ y (local ((define y 10)

(define z (+ y y))) z))

(define y 10)(+ y (local ((define y1 10) (define z1 (+ y1 y1))) z1))

(define y 10)(define y1 10)(define z1 (+ y1 y1))(+ y z1)

Page 27: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Using local • Guidelines for the use of local :

– If you notice during the development of a procedure using our design recipe that you need an auxiliary procedure which is only needed by that procedure, define the auxiliary procedure inside of a local block.

– Use the fact that inside of the local block, you have access to names that lie (lexically) further outside (e.g., parameters of procedures).

– If you compute a value several times inside a procedure body, define a local name for this value and use it in those places where you have computed the value before.

28

Page 28: Introduction to Computer Science  I Topic 4: Evaluation Order and Lexical Scoping

Dr. G. RößlingProf. Dr. M. MühlhäuserRBG / Telekooperation

©

Introduction to Computer Science I: T4

Using local: Example

29

(define (make-rat n d) (make-xy

(/ n (gcd n d)) (/ d (gcd n d))))

(define (make-rat n d) (local ((define t (gcd n d))) (make-xy

(/ n t) (/ d t))))