Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 4: Evaluation.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

1 VBScript Session What we learn last session?
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Lecture 2 ANALYSIS OF VARIANCE: AN INTRODUCTION
Programming Language Concepts
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 14: Stepwise.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 0: Introduction.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Prof. Dr. Max Mühlhäuser Dr. Guido Rößling
Data Structures Using C++
Modern Programming Languages, 2nd ed.
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
1 Evaluations in information retrieval. 2 Evaluations in information retrieval: summary The following gives an overview of approaches that are applied.
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
25 seconds left…...
Chapter Three Arithmetic Expressions and Assignment Statements
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Pointers and Arrays Chapter 12
Chapter 11 Describing Process Specifications and Structured Decisions
Introduction to Recursion and Recursive Algorithms
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Operational Semantics of Scheme
Names and Attributes Names are a key programming language feature
Principles of programming languages 4: Parameter passing, Scope rules
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Lecture 2 מבוא מורחב.
Presentation transcript:

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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 substitute if by my-if 3 (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)))))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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)))))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 (e.g. 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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 there is the possibility 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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 7 Intermezzo: Syntax & Semantics of Scheme

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 –We discuss the meaning of Scheme programs through an extension of the familiar laws of arithmetic and algebra (substitution model) 9

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Scheme Vocabulary (Syntax) Four categories of words, each defined by a line: –Variables ( ): the names of functions and values –Functions ( ): the names of functions –Constants ( ): boolean, symbolic, and numeric constants –Primitive operations ( ): 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 = x | a | alon | … area-of-disk | perimeter |... = true | false | 'a | 'doll | 'sum |... 1 | -1 | 3/5 | 1.22 |... = + | - |...

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Beginning Student Scheme: The Full Grammar (Syntax) 11 Two categories of phrases: definitions ( ) and expressions ( ) = (define (... ) ) | (define ) | (define-struct (... )) = | | (... ) | (cond ( )...( )) | (cond ( )...(else )) | (and ) | (or ) important update: added in grammar

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Beginning Student Scheme: The Full Grammar (Syntax) The category of expressions consists of 8 alternatives: –variables, constants, –primitive applications, –(function) applications, –two kinds of conditionals, –special forms and, or –The last 6 are again composed of other expressions Keywords define, cond, and, or identifiy special forms –Keywords have no meaning. Their role resembles that of punctuation marks (, and ; in English writing) –No keyword may be used as a variable 12

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 13

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Grammatical Terminology A definition ( ) 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 14 (cond ( )...) (define (... ) ) (... )

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 ( ) following the definitions 15 =... | ( local (... ) ) (local ((define PI 3)) (* PI 5 5))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 End Intermezzo: Syntax & Semantics of Scheme 16

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Block Structure: Example two locally defined procedures: f and g g calls f body calls g 17 (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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 explicility 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 definition 18 (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)²

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 19 (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)²

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 20 (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

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 21 (local ( (define (square x) (* x x)) (define (ge q) (square (+ q y)))) (+ (ge x) (ge z)))) bound names free names name binding

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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 22

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Try the lexical scoping with the „Check Syntax“ Feature of DrScheme 23

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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. 24

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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, because all occurences of x in square are either binding or bound 25 (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) 

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Evaluation of local blocks in 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 It cannot be done statically 26

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Evaluation of local blocks: Example 27 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)

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Evaluation of local blocks Why is it important that the local definitions have to be drawn to the topmost level (with new names) during each new evaluation? –To illustrate this, consider the evaluation of the following program using DrScheme Stepper: 28 (define (fac n) (local ((define m (- n 1))) (if (zero? n) 1 (* n (fac m))))) (fac 5)

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / 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. 29

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T4 Using local : Example 30 (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))))