TDDC74 Programmering: Abstraktion och modellering VT 2015 Johannes Schmidt Institutionen för datavetenskap Linköpings universitet Lecture 7 State and environment model Part 2 Definition: a binding A binding refers to association of a value to a name, for example, x:10, the value of x is 10 f:<procedure>, the value of f is a procedure Definition: Frame A frame is simply a set of bindings The set could be empty Graphical representation: x: 10 y: 20 Definition: Environment An environment is a finite ordered set (or sequence) of frames Graphical representation: E1 x: 42 y: #f a: 33 y: 20 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 E2 E3 E6 w: 127 y: 20 Definition: The Global Environment The so called global environment (GE) is distinguished from other environments and contains the global state. GE contains only one frame; this frame is the final frame of all other environemts as well Graphical representation: GE Lots of Environments GE pi: 3.1415 E1 x: 42 y: #f a: 33 y: 20 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 E2 E3 E6 w: 127 y: 20 Definition: Procedure A procedure is an object that contains two parts: - one part contains the parameters and the body of the procedure and - another part contains the environment in which the procedure was created. Graphical representation: Env Para: Body: Definition: The Current Environment The current environment (CE) indicates the environment in which we are currently computing. Graphical representation: CE The Current Environment GE pi: 3.1415 E1 x: 42 y: #f a: 33 y: 20 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 E2 E3 CE E6 w: 127 y: 20 The Creation of Environment Diagrams Initially, we have GE CE define, lambda, set! and procedure application change the diagram define (define <name> <value>) creates a new binding in the current (or first) frame of the current environment The Creation of Environment Diagrams: define (define y 69) GE pi: 3.1415 a: 33 y: 20 x: 10 y: 20 E4 x: 30 y:69 z: 20 a: 10 b: 1729 E5 E2 E3 CE E6 w: 127 y: 20 lambda (lambda (par1 par2 ...) body) creates a procedure object in the current environment The Creation of Environment Diagrams: lambda GE pi: 3.1415 CE Para: Body: a: 33 y: 20 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 E2 E3 E6 w: 127 y: 20 set! (set! <name> <new-value>) changes the old value of <name> in the current environment to <new-value>. The most recent binding in the current environment is affected. The Manipulation of Environment Diagrams: set! (set! y 69) GE pi: 3.1415 Para: Body: a: 33 y: 20 69 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 E2 E3 CE E6 w: 127 y: 20 The Manipulation of Environment Diagrams: set! (set! y 69) GE pi: 3.1415 Para: Body: a: 33 y: 20 x: 10 y: 20 E4 x: 30 z: 20 a: 10 b: 1729 E5 w: 127 y: 20 69 CE E2 E3 E6 Procedure Application When applying a procedure object to arguments, - create a new environment by creating a new frame in which we bind the parameters to the given arguments - connect this frame to the environment of the procedure and let CE point to this new environment - evaluate the body of the procedure in CE Evaluation of let Translate let to its lambda-equivalent and evaluate as usual let*, letrec and others are treated similarly Programming Environments The concept of programming environments should not be confused with the notion of environment we use here. A Programming Environment usually referes to a user-friendly interactive system that enables a programmer to write, edit, execute, analyse, test, debug and simulate programs DrRacket is an example of a programming environment Programming Paradigms Programmeringsparadigmer Functional Imperative Object Oriented Equation-based We have concentrated on functional programming so far. We will now see more and more examples of Imperative and object-oriented style.