The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
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.
Defining New Special Forms (unless test body) (if (not test) body #f) (when (not test) body)
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Lecture 14: Assignment and the Environment Model (EM)
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
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.
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 ((=
Evaluating Expressions. S.W.B.A.T  Use the order of operations to evaluate expressions with variables and expressions with numbers.
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Environments and the Contour Model
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
Short Circuits Design Recipe Redux Structures
The interpreter.
Variables, Environments and Closures
The Environment Model*
Env. Model Implementation
Original material by Eric Grimson
One-Step Equations with Subtraction
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Evaluating Expressions
Scope of Variables.
Variables, Environments and Closures
The Metacircular Evaluator
Lecture 28: Types of Types
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
6.001 SICP Environment model
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Evaluating Expressions
6.001 SICP Environment model
6.001 SICP Variations on a Scheme
L Calculus.
6.001 SICP Environment model
topics interpreters meta-linguistic abstraction eval and apply
One-Step Equations with Addition and Subtraction
More Scheme CS 331.
Principles of Programming Languages
Chapter 3 Discussion Pages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are resolved in Dylan An expression only has a value with respect to an environment

Binding identifier : value Frame unordered set of bindings Environment ordered list of frames

x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff)  global environment (top level frame)

The Lookup Rule the value of an identifier x is the value associated with x in the first (lowest) frame containing a binding for x undefined if there aren't any

Define rule To evaluate (define ident expr): evaluate expr in the current environment add a binding to the top-level frame (the global environment) Set! rule To evaluate (set! ident expr): evaluate expr in the current environment look up ident using lookup rule change that binding to value of expr

(define (d ) (+ 5 7)) x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff)

(define (d ) (+ 5 7)) x:10 +:{add} y:20 d:12 z:30 x:() w:foobar x:oh y:(list of stuff)

x:10 +:{add} y:20 z:30 x:() w:foobar x:oh y:(list of stuff) (set! x 'my)

x:10 +:{add} y:20 z:30 x:() w:foobar x:my y:(list of stuff) (set! x 'my)

Method rule To evaluate (method (params) body) in environment env : create a function object consisting of params body (unevaluated) env Example: value of (method ((x ) (y )) (sqrt (+ (* x x) (* y y)))) in environment env is params: ((x ) (y )) body: (sqrt (+ (* x x) (* y y))) env: env

Eval rule To evaluate (f a1... an) in environment e : evaluate f,a1,...,an in e apply f to arguments a1,...,an Apply rule To apply f to arguments a1,...,an : create a new frame with parameters of f bound to arguments a1,...,an append that frame to front of environment e’ in the closure of f to get e’’ evaluate body of f in environment e’’

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:100 +:{add} y:200 z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:100 +:{add} y:200 z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:10 +:{add} y:200 z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 x:10 +:{add} y:200 z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15y: 20

(define x 10) (define (foo ) (method (y) x)) (bind ((x 15)) (foo 20)) => 10 params: (y) body: x env: x:10 +:{add} y:200 foo: z:300 x: 15y: 20