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

## Presentation on theme: "Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 10: Assignments."— Presentation transcript:

Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 10: Assignments and other Effects 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: T10 Assignments and other Effects: Overview Functions with memory, Version 2 set! Example: Implementing an Address Book How Assignments Change our Programming Model Sequencing Expressions with (begin..) Sharing, Equivalence and Identity Using State Variables for Communication Assignments and Modeling/Performance/Streams 2

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory Important properties of functions we have seen so far –No matter how often we use a function with one and the same argument, we always get the same result –At every place in a program we can replace a function call with its definition or its value without changing the meaning of the program e.g., we can replace (+ 3 5) by 8 e.g., we can replace (map add1 (list 3 5)) by ((lambda (f a-list) … ) add1 (list 3 5)) where … is the definition of map e.g., we can replace (map add1 (list 3 5)) by (list 4 6) 3

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory This property is sometimes called replacing equals with equals or referential transparency, this style of program is called pure functional The property follows directly from the definition of the substitution model Sometimes however, it is convenient to have functions with memory Example: Implementing a counter –E.g., we want to count how many times the “subst” function is called during the evaluation of a program in our interpreter 4 The meaning of a function is fully characterized by its input/output relation

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory “Purely functional” counter: (define (count old-count) (+ old-count 1)) Problem: –We have to modify the eval function It has to accept current count as additional input, has to provide new count as additional output –We have to modify all functions that call eval eval-if, eval-app, run-program, … they must all accept/provide an additional input/output parameter and add the respective “count” values of all eval-invocations in their own output value of the counter –This solution is highly non-modular! 5

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory What we would like to have in the example is a function with memory –It memorizes the old value of the counter –If it is called, it has a side-effect or simply effect: it increases the value of the counter This behavior is a side-effect, because it is not part of the input-output relation 6

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignments A set! -expression, also known as an assignment, has of the following form: (set! var exp) It consists of –a variable, the left-hand side, and –an expression, called right-hand side. –The left-hand side of a set!-expression is a fixed variable. –In this course, we only use variables that are defined, either at the top-level or in a local-expression. The value of a set-expression is undefined (like in a define expression) and irrelevant. What matters is the effect of evaluating a set-expression: After the assignment, all references to var will evaluate to the value of exp Switch to “Advanced Student” in DrScheme to use set! 7

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignments With assignments, the counter example could be solved as follows: What does (define (increment-counter) …) mean? –It means (define increment-counter (lambda () …) –It is a function with zero parameters! called as (increment-counter) Do not confuse increment-counter and (increment-counter) –A function without parameters would be (almost) useless if it were purely functional / effect-free: It would just be a constant Almost, because this technique can also be used to prevent evaluation 8 ;; provide initial value for counter (define counter-value 0) ;; incrementing counter (define (increment-counter) (set! counter-value (add1 counter-value))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignments 9

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory Another example: Maintaining a phone book A phone book software provides at least two services: –A service for looking up the phone number of a person –A service for adding a name and a phone number to the address book. Potential GUI for the software 10

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory The corresponding source code might look like this 11 ;; lookup : symbol -> number or false ;; to lookup the number associated with name in ADDRESS-BOOK ;; if it doesn't find name, the function produces false (define (lookup name)...) ;; add-to-address-book : symbol number -> void ;; to add name and number to address-book (define (add-to-address-book name number)...) (define ADDRESS-BOOK (list (list 'Adam 1) (list 'Eve 2)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory Now imagine the following interaction with DrScheme This is impossible to achieve with effect-free functions! –In an effect-free program, functions always returns the same results for the same parameters Without assignments, add-to-address would need to consume the old address book and produce a new one that would then be used in further calls to lookup 12 > (lookup 'Adam) 1 > (lookup 'Dawn) false > (add-to-address-book 'Dawn 4) > (lookup 'Dawn) 4

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Functions with Memory Solution with assignments The usage of the “!” in all functions which perform assignments is a useful convention but not enforced by the interpreter We call ADDRESS-BOOK a state variable 13 (define (add-to-address-book! name number) (set! ADDRESS-BOOK (cons (list name number) ADDRESS-BOOK)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 How our programming model changes Assignments are a very fundamental change to our programming model! Important invariants (referential transparency, confluence) that we were used to no longer hold Suddenly time becomes an important factor! –The time before an assignment vs. the time after an assignment –Evaluation order becomes important (no confluence anymore) Suddenly we have the notion of identity! –We can have two address books AB1 and AB2 which have the same content at some time t 1 but differ at t 2 –That is, even if they have the same content, they are still two different objects: they have an identity in addition to their current value 14

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Sequencing Expression Evaluations Assume increment-counter is supposed to return the current value of the counter in addition to incrementing it How do we combine the assignment expression with the counter- value expression? We could define a “combine” function which accepts two parameters and ignores the first one: (define (combine x y) y) Since this pattern is so common, there is a special form for sequencing: (begin exp-1... exp-n exp) –Evaluates exp-1, …, exp-n and exp in this order –Returns the value of exp 15 (define (increment-counter) … (set! counter-value (add1 counter-value)) … … counter-value … )

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Sequencing Expression Evaluations Using begin expressions The begin expression is useless if exp-1 … exp-n do not have side effects! Evaluation order matters: –If x were evaluated before the set! expression, the result would be 3 rather than 5 –We can no longer substitute a variable by its value (e.g., x by 3) because its value may change no referential transparency By altering a definition, an assignment destroys the current value. Unless the programmer carefully plans the sequence of assignments, such an action may be fatal. 16 (define (increment-counter) (begin (set! counter-value (add1 counter-value)) counter-value)) (define x 3) (begin (set! x (+ x 2)) x)

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Input/Output is another kind of effect Assignments are just one (important) kind of effect Another kind of effect that you already used in the exercises is input/output (I/O) –I/O is any kind of communication with the “external” world outside the program user inputs (mouse, keyboard, …) inputs from other machines (e.g., via network) output (screen, printer, controlling machines, …) Like assignments, I/O is also not part of the pure functional behavior of a procedure The sequence of I/O effects is important –e.g., order in which pages are printed or the engines of a machine are turned on and off 17

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Input/Output is another kind of effect Take the example of the following I/O function –draw-circle Since we did not know much about sequencing and effects at that time, we “misused” the and function to sequence the effects –(and (draw-circle (make-posn 50 50) 100 ‘yellow)) (draw-circle (make-posn 30 30) 100 ‘green))) A better way to sequence these effects is to use begin –(begin (draw-circle (make-posn 50 50) 100 ‘yellow)) (draw-circle (make-posn 30 30) 100 ‘green))) 18

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Some standard I/O functions 19 (begin (printf "Enter your name:") (printf "Hello ~v" (read))) Example : Printing a parameter to stdout –As a value : print : any -> void –Without quotes for symbols and strings etc.: display : any -> void –Traditional style, somewhere between print and display : write : any -> void –Like write, but with automatic newlines and indentation: pretty-print : any -> void –Formatting the rest of the arguments according tothe first argument: printf : string any... -> void –Printing a newline: newline : -> void To read input from the user: read : -> sexp

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Changing local values We can change every defined name –Not only global, but also local definitions Since local definitions are available per invocation of the enclosing procedure, we have a dynamic and unbound number of variables Example: Counters with local variables 20 (define (make-counter init) (local ((define counter-value init)) (lambda () (begin (set! counter-value (add1 counter-value)) counter-value)))) (define c1 (make-counter 0)) (define c2 (make-counter 0)) (c1)  1 (c1)  2 (c1)  3 (c2)  1 (c2)  2

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Changing local values The number of counters is unbounded –Example: Creating 500 counters This would not be possible with global variables –The number of global definitions is fixed! 21 (define list-of-counters (build-list 500 (lambda (n) (make-counter 0))))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Designing functions with memory How do assignments influence our design process? State variables should have a purpose statement at the place where they are defined and initialized If a function has an effect, its effect should be described 22 ;; State Variable: ;; address-book : (listof (list symbol number)) ;; to keep track of pairs of names and phone numbers (define address-book empty) ;; add-to-address-book : symbol number -> void ;; Purpose: the function always produces (void) ;; Effect: to prepend (list name phone) to address-book

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Are state variables necessary? The difference between the two versions is that the caller has to know and keep track of the address book If there are many different callers, who are supposed to share the same address book, this can be a bit elaborate State variables can be simulated by refactoring every function to accept a state object as an additional parameter and return a (possibly different) state object as an additional return value –The state object represents the current „state of the world“ The values of all state variables –A function that wants to assign a value can return a different state object 25

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Are state variables necessary? The state object is then threaded through the program For example (f (g 42) (h 23)) could become with (define-struct result (value state)) Note how this program establishes left-to-right evaluation order! This simulation is very elaborate because it works for every program that has a state For most particular programs, it is much less painful to get rid of state variables 26 (local ((define r1 (g 42 current-state)) (define r2 (h 23 (result-state r1)))) (f (result-value r1) (result-value r2) (result-state r2)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State Variables as Communication Channels Variables provide a new possibility of communication in programs Without state variables, all communication is via procedure parameters and their results With state variables, different program parts that access a shared state variable can exchange information through the variable! –A state variable is a communication channel! 27

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State Variables as Communication Channels Example 28 (define address-book empty) ;; add-to-address-book : symbol number -> void (define (add-to-address-book name phone) (set! address-book (cons (list name phone) address-book))) ;; lookup : symbol -> number or false ;; to lookup the phone number for name in address-book (define (lookup name) (local ((define (lookup-helper ab) (cond [(empty? ab) false] [else (cond [(symbol=? (first (first ab)) name) (second (first ab))] [else (lookup-helper (rest ab))])]))) (lookup-helper address-book)))

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State Variables as Communication Channels Now consider this sequence of actions: Although lookup and add-to-address do not communicate via parameters and do not know each other, they can exchange information: via the shared variable address-book 29 (add-to-address-book ‘klaus 123) (add-to-address-book 'jim 456) (lookup 'klaus)  123

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Communication Topology without State Variables The call tree of a program execution is a tree whose nodes are the procedure executions (PE) of the program. A PE pe1 is a child of another PE pe2 if pe2 triggers pe1 We can think of the call tree of a program execution as the systematic trace of the “eval” invocations within our meta interpreter –Note that the same procedure can show up multiple times in a call tree Example: 30 (define (h n) (* n 2)) (define (g n) (+ n 1)) (define (f n) (h (g n))) (f 5) (g 5) (f 5) (h 6) Information flow

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Communication Topology without State Variables In a program without state variables, all information flows along the edges of the call tree –In that sense, it is strictly hierarchical 31 (define (h n) (* n 2)) (define (g n) (+ n 1)) (define (f n) (h (g n))) (f 5) (g 5) (f 5) (h 6) Information flow

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Communication Topology with State Variables With state variables, we can “escape” the strict hierarchical communication topology 32 (define (test) (begin (add-to-address-book 'bob 123) (lookup 'bob))) test (add-to-address-book 'bob 123) (lookup 'bob) Information flow test is not involved in the information flow

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Communication Topology with State Variables Consider a deep call tree Assume we find out that c needs to communicate with g Without state variables, the definitions of b, a and e have to “know” about the communication –This is a modularity problem! –e.g., if they didn’t “know” this previously, we have to modify all these procedures With a state variable, we can create a “worm hole” from c to g Pitfall: One of the most common bugs in programs is unintended communication via state variables 33 a b e c d f g

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State Variables and Black Boxes The idea of a procedure as a black box is to hide details about its implementation –Callers only need to know its interface With state variables, the interface becomes more complex –It is hard to predict which variables will be changed –Variable changes are not visible in the contract of the function Never (!!!) use global state variables to transfer information from a function call to the function definition or vice versa In general, whenever information can be passed just as well via function parameters/results, use these and not state variables 34

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignments and Modeling A program is an executable model of a certain problem domain Sometimes, it is more natural to model the reality using time and identity For example, if a person grows older, we think of the individual as being the same person, just older –Not as a copy of the person 35

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignments and Performance In some cases, performance can be a reason for using assignments Assignments can be implemented very efficiently on typical hardware (so-called “von Neumann architecture”) Changing a large compound value (e.g., a big tree or list) can be very expensive if it is updated in a pure functional style In contrast, such a compound value can usually be modified in a non-functional (destructive) way in constant time 36

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 Assignment and Streams Streams are sometimes a good alternative to assignments –We model the time-dependent behavior via a stream Example: Random numbers –We have an initial random number random-init –We have a function that, given the previous random number, computes the next random-number: rand-update 37 Version using an infinite stream (define random-numbers (my-cons random-init (map rand-update random-numbers))) (define rand (local ((define x random-init)) (lambda () (begin (set! x (rand-update x)) x)))) Version with assignment

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State variables: The Good, the Bad and the Ugly State variables favour freedom of communication at the expense of safety and predictability Assignments are not per se good, bad (or ugly) –They can be an extremely powerful tool modularity and performance –But they can also make the program far less understandable and predictable No confluence, no referential transparency, implicit hidden (potentially unintended) communication, … You should be aware of this implicit cost of using assignments! Keep in mind that every program can be written without assignments A good rule of thumb is that you need much less assignments than you think! 38

Dr. G. Rößling Prof. Dr. M. Mühlhäuser RBG / Telekooperation © Introduction to Computer Science I: T10 State variables: The Good, the Bad and the Ugly A word of warning for those with previous experience in some programming language: –Do not fall back into old (bad) habits! –Do not feel “at home” again because you now know the mechanism with which you used to solve everything –Do not think about computation mainly in terms of sequential computation steps and assignments This does not scale for big programs Rather, think about it in terms of problem decomposition and composition One reason why assignments are introduced so late in this course is to get this kind of thinking out of your head 39

Download ppt "Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 10: Assignments."

Similar presentations