Technische Universität Darmstadt Telecooperation/RBG Introduction to Computer Science I Topic 4: Evaluation Order and Lexical Scoping Prof. Dr. Max Mühlhäuser Dr. Guido Rößling Copyrighted material; for TUD student use only Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 Introduction to Computer Science I: T4 2 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Applicative vs. Normal Evaluation Order revisited • Example: a “self-made” if-statement modeled as a procedure using “cond” (define (my-if test then-exp else-exp) (cond (test then-exp) (else else-exp))) – Seems to work fine: (my-if true 3 5) 3 (my-if false 3 5) 5 • Now consider the following program: ;; ! : N -> N ;; computes the factorial function (define (! n) (if (zero? n) 1 (* n (! (pred n))))) – Let us try to replace if by my-if Introduction to Computer Science I: T4 3 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Applicative vs. Normal Evaluation Order revisited • Using my-if instead of if ;; ! : N -> N ;; computes the factorial function (define (! n) (my-if (zero? n) 1 (* n (! (pred n))))) • A call such as (! 2) – does not terminate when using applicative evaluation order, – but with normal order it does • Why? Introduction to Computer Science I: T4 4 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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” Introduction to Computer Science I: T4 5 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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… Introduction to Computer Science I: T4 6 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Intermezzo: Syntax & Semantics of Scheme Introduction to Computer Science I: T4 7 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 Introduction to Computer Science I: T4 8 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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) Introduction to Computer Science I: T4 9 Scheme Vocabulary (Syntax) <var> <fct> <con> = = = <prm> = Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © x | a | alon | … area-of-disk | perimeter | ... true | false | 'a | 'doll | 'sum | ... 1 | -1 | 3/5 | 1.22 | ... + | - | ... • 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 Introduction to Computer Science I: T4 10 <def> Beginning Student Scheme: The Full Grammar (Syntax) = Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © (define (<fct> <var> ...<var>) <exp>) | (define <var> <exp>) Can be empty | (define-struct <var0> (<var-1> ...<var-n>)) <exp> = <var> | <con> Correct number of arguments | (<prm> <exp> ...<exp>) | (<fct> <exp> ...<exp>) This grammar was simplified; not all possible expressions are actually valid! | (cond [<exp> <exp>] ... [<exp> <exp>]) | (cond [<exp> <exp>] ... [else <exp>]) | (and <exp> <exp>) | (or <exp> <exp>) | (if <exp> <exp> <exp>) Two categories of phrases: definitions (<def>) and expressions (<exp>) Introduction to Computer Science I: T4 11 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 Introduction to Computer Science I: T4 12 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 (define (<function-name> <parameter> ...<parameter>) <body>) – Body: The expression component of a definition • An application consists of – Function: The first component – Arguments (actual arguments): the remaining components (<function-name> <argument> ...<argument>) • A cond-expression consists of cond-lines (cond-clauses) each consisting of two expressions: – Question (condition) and answer (cond (<question> <answer>) <cond-clause> ...) Introduction to Computer Science I: T4 13 Syntax of the local-expression Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © (local ((define PI 3)) (* PI 5 5)) <exp> = ... | ( local (<def-1> ...<def-n>) <exp> ) • 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 Introduction to Computer Science I: T4 14 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © End Intermezzo: Syntax & Semantics of Scheme Introduction to Computer Science I: T4 15 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation Block Structure: Example (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)) © LOCAL DEFINITION BODY ) • two locally defined procedures: f and g • g calls f • body calls g Introduction to Computer Science I: T4 16 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 (define (f x y z) (x+y)² + (z+y)² (local ( (define (square n) (* n n)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z)))) • Locally defined names are not visible from the outside – The same name can be re-used in a different local definition Introduction to Computer Science I: T4 17 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 (define (f x y z) (x+y)² + (z+y)² (local ( (define (plus-y-sqr q) (local ( (define (square n) (* n n))) (square (+ q y))))) (+ (plus-y-sqr x) (plus-y-sqr z)))) Introduction to Computer Science I: T4 18 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 Conceals outer z inside of square (define (f x y z) (local ( (x+y)² + (z+y)² (define (square z) (* z z)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z)))) Introduction to Computer Science I: T4 19 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 name binding (local ( (define (square x) (* x x)) (define (ge q) (square (+ q y)))) (+ (ge x) (ge z)))) free name bound name Introduction to Computer Science I: T4 20 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 Introduction to Computer Science I: T4 21 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Try the lexical scoping with the „Check Syntax“ Feature of DrScheme Introduction to Computer Science I: T4 22 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation 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. (define (f x ) (* x x)) Introduction to Computer Science I: T4 (f 5 ) 23 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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 (define (f x y z) (local ( (f 2 3 4) (define (square x) (* x x)) (define (plus-y-sqr q) (square (+ q y)))) (+ (plus-y-sqr x) (plus-y-sqr z)))) Introduction to Computer Science I: T4 24 Evaluation of local blocks in the substitution model Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © • 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 Introduction to Computer Science I: T4 25 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Evaluation of local blocks: Example (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) 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 Introduction to Computer Science I: T4 26 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © 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. Introduction to Computer Science I: T4 28 Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Using local: Example (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)))) Introduction to Computer Science I: T4 29