Environments and the Contour Model

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
(define (f x) (if (< x 0) (lambda (y) (- y x)) (lambda (y) (- x y)))) GE f: P1 para:x body:(if … )
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
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.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Multiplication Properties of Exponents. To multiply two powers that have the same base, you ADD the exponents. OR.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
The Order of Operations Chapter Evaluate inside grouping symbols ( ), { }, [ ], | |, √ (square root), ─ (fraction bar) 2.Evaluate exponents 3.Multiply.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
2.3 Multiplying Rational Numbers The product of numbers having the same sign is positive. The product of numbers having different signs is negative.
Expressions Methods if else Statements Loops Potpourri.
C# Programming Methods.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
CS314 – Section 5 Recitation 9
User-Written Functions
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Chapter 12 Enumerated, Structure, and Union Types Objectives
The interpreter.
Principles of programming languages 4: Parameter passing, Scope rules
Organization of Programming Languages
Function There are two types of Function User Defined Function
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Run-Time Environments
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.
Variables, Environments and Closures
More Loops.
The Metacircular Evaluator
6.001 SICP Environment model
Writing Functions( ) (Part 4)
Objective - To multiply integers.
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
Chapter 3-1 Distributive Property
6.001 SICP Environment model
Functions Imran Rashid CTO at ManiWeber Technologies.
Lecture 13: Assignment and the Environment Model (EM)
CSE 190p University of Washington Michael Ernst
Rules of evaluation The value of a number is itself.
Corresponds with Chapter 5
Lecture 25: The Metacircular Evaluator Eval Apply
Distributive Property
Lecture 2 - Names & Functions
Chapter 4 Test Review First day
Presentation transcript:

Environments and the Contour Model

Environments Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” … Consist of: frames chained together Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” … Frames are:

Global Environment Name Value + … Name Value X 2 Y Primitives Name Value + … Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” … Global user declarations

Function Evaluation > (Y 2) Name Value + … Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” … Name Value X 2 …

Function Evaluation So, X is bound in Y What about +? Unbound When unbound lookup …

Traverse Frames Name Value + … Name Value X 2 Y (lambda (x) (+ x 2)) Z “testing” … Name Value X 2 …

What about multiple bindings? (define x 3) (define y 2) (define z (lambda (x) (let ((y 3)) (+ x y)))) (z 4) X and Y multiply defined. Always use first binding! Name Value +  … Name Value X 3 Y 2 Z  Name Value X 4 Y 3

Passing Mechanism By value: Copy of argument is value for formal argument Pointer in the case of lists Copy of string or integer for primitive types

Primitive-Environment User-Environment Points at line where  defined b 5 square (2) Points at environment in which  defined line 1:    (define b 5) line 2:    (define (square num) line 3:        (* num num)) line 4:    (define result (square 5)) P = 4 Points at line where execution snapshot created

Points at line where function execution ends Primitive-Environment User-Environment b 5 square (2) square’ line 1:    (define b 5) line 2:    (define (square num) line 3:        (* num num)) line 4:    (define result (square 5)) num 5 P = 3 P = 4 Points at line where function execution ends Return pointer

Primitive-Environment User-Environment b 5 square 2 result 25 line 1:    (define b 5) line 2:    (define (square num) line 3:        (* num num)) line 4:    (define result (square 5)) P = 5

Primitive-Environment User-Environment factorial (1) line 1:    (define (factorial n) line 2:            (if (< n 2) line 3:                    1 line 4:                    (* n (factorial (- n 1))))) line 5:    (define result (factorial 3)) P = 5

Primitive-Environment User-Environment factorial (1) factorial’ n 3 line 1:    (define (factorial n) line 2:            (if (< n 2) line 3:                    1 line 4:                    (* n (factorial (- n 1))))) line 5:    (define result (factorial 3)) P = 4 P = 5

Primitive-Environment User-Environment factorial (1) factorial’ n 3 P = 4 P = 5 factorial’’ line 1:    (define (factorial n) line 2:            (if (< n 2) line 3:                    1 line 4:                    (* n (factorial (- n 1))))) line 5:    (define result (factorial 3)) n 2 P = 4

Primitive-Environment User-Environment factorial (1) factorial’ n 3 P = 4 P = 5 factorial’’ n line 1:    (define (factorial n) line 2:            (if (< n 2) line 3:                    1 line 4:                    (* n (factorial (- n 1))))) line 5:    (define result (factorial 3)) 2 P = 4 factorial’’’ n 1 P = 4

Primitive-Environment User-Environment factorial (1) result 6 P = 6

Warning Simplified Diagrams! Next slides are simplified: Lambdas for a, b, etc not present Extra environment frame is for the let inside of main. Remember: (let ((a 0) (b 0)) <body>) is equivalent to ((lambda (a b) <body>) 0 0) Main’ a 5 b 6 f (3) g (8)

Code Line 1: (define (main) Line 2: (let (( a 0) (b 0)) Line 3:             (define (f) Line 4:                (let ((a 0)) Line 5:                    (set! a b) Line 6:                     (writeln "in f: A and B are - " a b) Line 7:                    (g))) Line 8:             (define (g) Line 9:                (let ((b 0)) Line10:                    (set! b (+ a 2)) Line11:                    (writeln "in g: A and B are - " a b) Line12:                     (f))) Line13:            (set! a 5) Line14:            (set! b 6) Line15:            (f))) Line16:     (define a 10) Line17:     (define b 20) Line18:     (main)

Primitive-Environment User-Environment a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ a 6 P = 7

Primitive-Environment User-Environment a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) G’ F’ a 6 b 7 P = 7 P = 12

Primitive-Environment User-Environment a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ G’ F’’ a 6 b 7 a 6 P = 7 P = 12 P = 7

Primitive-Environment User-Environment a 10 b 20 main (1) Main’ a 5 P = 15 b 6 f (3) g (8) F’ G’ F’’ G’’ a 6 b 7 a 6 b 7 P = 7 P = 12 P = 7 P = 12

Primitive-Environment User-Environment a 10 P = 18 b 20 main (1) Main’ P = 15 F’ a 5 a 6 P = 7 b 6 f (3) g (8)

Primitive-Environment User-Environment a 10 P = 18 b 20 main (1) Main’ P = 15 F’ a 5 a 6 P = 7 b 6 G’ f (3) b 8 g (8) P = 12

Primitive-Environment User-Environment a 10 P = 18 b 20 main (1) Main’ F’ P = 15 a 5 a 6 P = 7 b 6 G’ f (3) b 8 F’’ g (8) a 8 P = 12 P = 7

Primitive-Environment User-Environment a 10 P = 18 b 20 main (1) Main’ F’ P = 15 a 5 a 6 P = 7 b 6 G’ f (3) b 8 F’’ g (8) G’’ a 8 b 10 P = 12 P = 7 P = 12