The Environment Model*

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 312 Spring 2002 Lecture 16 The Environment Model.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Functional Programming: Lisp MacLennan Chapter 10.
PS4 #6: if you got points off because you didn’t do an intersect with accessible and the start states, submit a re- grade. Today: let, let*, letrec in.
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CS314 – Section 5 Recitation 9
Functional Programming
Lecture 4: Metacircles Eval Apply David Evans
CS314 – Section 5 Recitation 10
Additional Scheme examples
Functional Programming
Edited by Original material by Eric Grimson
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
COP4020 Programming Languages
Env. Model Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
Lecture 28: Types of Types
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Procedures App B: SLLGEN 1.
CSE S. Tanimoto Explicit Function Application
Lecture 26: The Metacircular Evaluator Eval Apply
Explicit Application of Procedures, and Explicit Evaluation
Lisp: Using Functions as Data
overview today’s ideas set-car! and set-cdr!
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
Functional Programming: Lisp
Lisp: Using Functions as Data
CSE 341 Lecture 11 b closures; scoping rules
Common Lisp II.
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

The Environment Model* Jed Liu CSE 341 11 April 2003 *Adapted from Andrew Myers, CS 312, Spring 2002, Lec. 16 at Cornell University.

Important Note You will NOT be tested on this.... ....but knowing it will probably make understanding a future topic (closures) a lot easier.

Environment Model A method for reasoning about computation in LISP. Makes explicit the environment in which computation is being performed.

The Environment The environment is a map from variables to values. All computation is performed with respect to an environment. Example: (let* ((x 2) (y "hello") (f #'(lambda (z) x))) (funcall f (+ x (length y)))) Evaluate (funcall f (+ x (length y))) in the environment an anonymous function x: 2 y: "hello" f: #'(lambda (z) x)

Environment Frames A frame is a set of variables and their values. Each frame has a parent pointer that points to a parent environment. An environment is a linked list list of frames ending with the null environment (equivalent to NIL for LISP linked lists), sometimes called the global environment. The global environment implicitly contains the bindings for everything visible in the initial LISP environment.

Variables Variables are evaluated with respect to some environment. To do this, start with the last binding added to the environment and walk towards the nil. Evaluating x in this environment yields 3: nil x : 2 y : 4 x : 3 Blue boxes: bindings env z : 3

Let Expressions To evaluate (let ((x1 e1) ... (xn en)) e): Evaluate, in left-to-right order, all the ei’s in the current environment. Extend the current environment with bindings that maps each xi to the value of ei. Evaluate e in the extended environment. Restore the old environment (i.e., remove the binding for the xi’s). Return the value of e.

Let Example (let ((x '(1 2))) (car x)) current env nil

Let Example (let ((x '(1 2))) (car x)) 2 1 nil current env 1. Evaluating '(1 2) yields a pointer to a list in memory. 1 current env nil

Let Example (let ((x '(1 2))) (car x)) 2 1 current env x: nil 1. Evaluating '(1 2) yields a pointer to a list in memory. 1 2. Extend the environment with a binding for x. current env x: current env nil

Let Example (let ((x '(1 2))) (car x)) 2 1 current env x: nil 1. Evaluating '(1 2) yields a pointer to a list in memory. 1 2. Extend the environment with a binding for x. 3. Evaluate the body of the let in the new environment. x evaluates to a pointer to the list, so (car x) evaluates to the first element, namely 1. current env x: nil

Let Example (let ((x '(1 2))) (car x)) 2 1 current env x: nil 1. Evaluating '(1 2) yields a pointer to a list in memory. 1 2. Extend the environment with a binding for x. 3. Evaluate the body of the let in the new environment. x evaluates to a pointer to the list, so (car x) evaluates to the first element, namely 1. current env x: 4. Restore the old environment. current env nil

Let Example (let ((x '(1 2))) (car x)) 2 1 x: nil current env 1. Evaluating '(1 2) yields a pointer to a list in memory. 1 2. Extend the environment with a binding for x. 3. Evaluate the body of the let in the new environment. x evaluates to a pointer to the list, so (car x) evaluates to the first element, namely 1. x: 4. Restore the old environment. current env nil 5. Return the value we got: 1

Let* Expressions To evaluate: Do the same thing as you would for: (let* ((x e1) (y e2) (z e3)) e4) Do the same thing as you would for: (let ((x e1)) (let ((y e2)) (let ((z e3)) e4)))

Let* Example current env nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env nil

Let* Example 3 4 current env nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) 3 4 current env nil

Let* Example 3 4 current env x : current env nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env 3 4 x : current env nil

Let* Example 3 4 current env x : nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env 3 4 x : nil

Let* Example current env y : 3 4 current env x : nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env y : current env 3 4 x : nil

Let* Example current env y : 3 4 x : nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env y : 3 4 x : nil

Let* Example current env y : 3 4 x : Result is 3 nil (let* ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) current env y : 3 4 x : Result is 3 nil

Let* Example y : Restore last env 3 4 current env x : Result is 3 nil ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) y : Restore last env current env 3 4 x : Result is 3 nil

Let* Example y : 3 4 x : Restore original env Result is 3 current env ((x '(3 . 4)) (y (cons x x))) (car (cdr y))) (let ((x '(3 . 4))) (let ((y (cons x x))) (car (cdr y)))) y : 3 4 x : Restore original env Result is 3 current env nil

Dynamic scope: Perl, Python, BASIC Functions (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) How do we make sure the environment has the (correct) binding for x? We must keep track of the environment at the point where the function was evaluated. Function evaluation: #'(lambda (z) x), not (funcall f (length x)) We create a closure A pair of a function and its environment Static scope: ML, Java, Scheme, … Dynamic scope: Perl, Python, BASIC

Functions To evaluate a function #'(lambda (x) e), create a closure out of the function and the current environment and return a pointer to the closure. nil x : 2 y : 4 current env

Creating Closures To evaluate a function #'(lambda (x) e), create a closure out of the function and the current environment and return a pointer to the closure. (lambda (x) e) nil x : 2 y : 4 current env I’ll draw closures using yellow. Result

Function Example nil (lambda (z) x) x : 2 current env (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) (lambda (z) x) x : 2 current env nil

Function Example nil f: (lambda (z) x) current env x : 2 (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) f: (lambda (z) x) current env x : 2 nil

Function Example nil "bye" x : current env f: (lambda (z) x) x : 2 (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) "bye" current env x : f: (lambda (z) x) x : 2 nil

Function Example nil "bye" x : current env f: (lambda (z) x) x : 2 (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) "bye" current env x : f: (lambda (z) x) x : 2 nil

Function Calls To evaluate (e0 e1 e2 ... en): Evaluate e0 – you should get a pointer to a closure. Evaluate e1, e2, ..., en to values, in left-to-right order. Save the current environment – we’ll come back to it after the function call. Extend the environment of the closure, mapping the formal arguments to the actual arguments. Evaluate the body of the function within the extended environment – this gives us our result value. Restore the old environment (saved in step 3). Return the result.

Function Call Example nil 1. Evaluate e0, e1 "bye" x : current env f: (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) 1. Evaluate e0, e1 "bye" current env x : f: (lambda (z) x) x : 2 nil

Function Call Example nil 1. Evaluate e0, e1 2. Save environ. "bye" (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) 1. Evaluate e0, e1 2. Save environ. "bye" saved env x : f: (lambda (z) x) current env x : 2 nil

Function Call Example nil 1. Evaluate e0, e1 2. Save environ. (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) 1. Evaluate e0, e1 2. Save environ. 3. Extend env with actual "bye" saved env x : f: (lambda (z) x) current env z : 3 x : 2 nil

Function Call Example nil 1. Evaluate e0, e1 2. Save environ. (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) 1. Evaluate e0, e1 2. Save environ. 3. Extend env with actual 4. Evaluate body (result: 2) "bye" saved env x : f: (lambda (z) x) current env z : 3 x : 2 nil

Function Call Example nil 1. Evaluate e0, e1 2. Save environ. (let* ((x 2) (f #'(lambda (z) x))) (let ((x "bye")) (funcall f (length x)))) 1. Evaluate e0, e1 2. Save environ. 3. Extend env with actual 4. Evaluate body (result: 2) 5. Restore env. (result: 2) "bye" current env x : f: (lambda (z) x) z : 3 x : 2 nil