Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

Slides:



Advertisements
Similar presentations
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Advertisements

(define (f x) (if (< x 0) (lambda (y) (- y x)) (lambda (y) (- x y)))) GE f: P1 para:x body:(if … )
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.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
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:
CS 312 Spring 2004 Lecture 18 Environment Model. Substitution Model Represents computation as doing substitutions for bound variables at reduction of.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
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.
Run-Time Storage Organization
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Chapter 8 - Control II: Procedures and Environments
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
The environment-based operational semantics Chapter
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Lecture 14: Assignment and the Environment Model (EM)
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
The Environment Model an extension of the substitution model more "operational" fully explains static scoping and the process by which variable names are.
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
Evaluators for Functional Programming 1. How to describe (specify) a programming language? 1.Syntax: atoms, primitives, combination and abstraction means.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
The interpreter.
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
6.001 SICP Environment model
Bindings, Scope, and Extent
6.001 SICP Further Variations on a Scheme
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Environment model
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
CSE 341 Lecture 11 b closures; scoping rules
Scope, Function Calls and Storage Management
Principles of Programming Languages
Chapter 3 Discussion Pages
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Practice session #9: The environment model

Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment: A finite sequence of frames, where the last frame is the global environment. The global environment: A single-frame environment, the only environment that statically exists. Enclosing environment: For an environment E, the enclosing environment is E, excluding its first frame. GE I x:3 y:5 II z:6 x:7 III n:1 y:2 E1 E2 E 1 =, E 2 =, GE= Enclosing-env (E 1 ) = GE Enclosing-env (E 2 ) = GE Environments: Environment Model: Introduction

GE I x:3 y:5 II z:6 x:7 III n:1 y:2 E1 E2 E 1 =, E 2 =, GE= Enclosing-env (E 1 ) = GE Enclosing-env (E 2 ) = GE The value of variable in an environment: The value of x in the environment E is its values in the first frame in which it is define. Environments: Environment Model: Introduction

A procedure value (closure) is a data structure with: (1) Procedure parameters. (2) Procedure body. (3) The environment in which it has been created. Procedures: > (define square (lambda (x) (* x x))) GE square: p:(x( b 1 :(* x x( p:(y( b 2 :y > (lambda (y) y) b1 b2 Environment Model: Introduction

Procedure application: (1) Create new frame, mapping the procedure params to application values. (2) The new frame extends the environment associated with the procedure. (3) Evaluate the body of the procedure in the new environment. Procedures: GE square: p:(x( b 1 :(* x x( GE E1E1 B1B1 x:5 > (define square (lambda (x) (* x x))) > (square 5) b1 Environment Model: Introduction

GE sq: sum-of-squares: f: p: (x) b 1 : (* x x) p: (x y( b 2 : (+ (sq x) (sq y)) p: (a) b 3 : (sum-of-squares (+ a 1) (* a 2)) Environment Model: Definition and Application

B3B3 E1E1 a:5 GE 136 B2B2 E2E2 x:6 y:10 B1B1 E3E3 x:6 B1B1 x:10 E2E2 100 E1E1 136 GE sq: sum-of-squares: f: p: (x) b 1 : (* x x) p: (x y( b 2 : (+ (sq x) (sq y)) p: (a) b 3 : (sum-of-squares (+ a 1) (* a 2)) Environment Model: Definition and Application E2E2 36 E4E4

Environment Model: Definition and Let GE a:8 b:5 c:8 f: b1b1 E1E1 x:8 y:8 GE 16 p: (x y) b 1 : (+ x y)

GE 53 b2b2 E1E1 a:8 b:5 c:3 p: (a b c) b 2 : (let…) E1E1 53 b3b3 E2E2 d:13 e:40 E2E2 53 b1b1 E3E3 x:40 y:13 f: p: (x y) b 1 : (+ x y) p: p: (d e) b 3 : (f e d) Environment Model: Definition and Let GE a:8 b:5 c:8

GE p:(n) b:(if…) fact: bE1E1 n:3 GE 6 bE2E2 n:2 E1E1 2 bE3E3 n:1 E2E2 1 bE4E4 n:0 E3E3 1 Environment Model: Recursion

GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE Environment Model: Pair ADT, Lazy implementation

GE b2b2 E2E2 sel: b3b3 E3E3 a:5 b:10 E25E25 GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE p:(a b) b 3 :a > (p1 (lambda (a b) a)) 5 Environment Model: Pair ADT, Lazy implementation

GE b2b2 E2E2 sel: b3b3 E3E3 a:5 b:10 E25E25 GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE p:(a b) b 3 :a > (p1 (lambda (a b) a)) 5 Environment Model: Pair ADT, Lazy implementation

p:(x y) b 3 :(p1(lambda…) b3b3 E2E2 x:1 y:2 GE p:(first second) b 4 :first b1b1 E3E3 E2E2 sel: b4b4 E4E4 first:5 second:10 GE p:(x y) b 1 :(lambda(sel)…) make-pair: P1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 GE > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) b4 b3 Environment Model: Pair ADT, Lazy implementation

Environment Model: Lexical (static) vs Dynamic Scoping Lexical Scoping: A closure “carries” the environment in which it has been created. In application of a closure, its carried environment is extended. Dynamic Scoping: A closure does not correspond to any environment. In application of a closure, the calling environment is extended. Simpler implementation (no need to “store” environments).

GE p:(x y) b 1 :(lambda(sel)…) make-pair: p1: p:(sel) b 2 :(sel x y) b1b1 E1E1 x:5 y:10 p:(x y) b 3 :(p1(lambda…) b3b3 E2E2 x:1 y:2 p:(first second) b 4 :first b2b2 E3E3 sel: b4b4 E4E4 first:1 second: Environment Model: Dynamic Scoping - Example > (let ((x 1) (y 2)) (p1 (lambda (first second) first))) b4 b3

p:(n c) b 1 :(if…) GE fact$: b1b1 E1E1 n:3 GE 6 p(fact-3) b 3 :(fact-3) c: p:(fact-n-1) b 2 :(c…) b1b1 E2E2 n:2 E1E1 6 c: p:(fact-n-1) b 2 :(c…) b1b1 E3E3 n:1 E2E2 6 c: b2b2 E4E4 fact-n-1:1 E3E3 6 b2b2 E5E5 fact-n-1:2 E4E4 6 b3b3 E6E6 fact-n-1:6 E5E5 6 Environment Model: CPS