Bindings, Scope, and Extent

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Scope and Symbol Table.
Advertisements

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Scheme for Python Programmers CSE Valeria Montero.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
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.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
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.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
SCHEME Scheme is elegant, clear, small, and powerful programming language. It is a dialect of LISP and can be classified as a semi-functional programming.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
Programming Languages: Design, Specification, and Implementation G Rob Strom September 21, 2006.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Variables, Environments and Closures
Representation, Syntax, Paradigms, Types
Variables, Environments and Closures
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Representation, Syntax, Paradigms, Types
Procedures App B: SLLGEN 1.
Scheme: Basic Functionality
Representation, Syntax, Paradigms, Types
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
6.001 SICP Variations on a Scheme
Functional Programming Concepts
Representation, Syntax, Paradigms, Types
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
CSE 3302 Programming Languages
CSE 341 Lecture 11 b closures; scoping rules
Functional Programming Concepts
Bindings, Scope, and Extent
Common Lisp II.
Recursive Procedures and Scopes
More Scheme CS 331.
Presentation transcript:

Bindings, Scope, and Extent Global bindings Local bindings and lexical scope Function bindings LET vs LET* and LETREC Name spaces CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Binding: an association between a symbol and a memory location that holds another Scheme object such as the top-level value of the symbol. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

Scoping and Extent of Bindings Scope: The region of a program in which a binding is available. (define (foo x) ; X gets bound in the call. (+ x 2) ; The binding is only ) ; available here. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent Extent of Bindings Extent: The span of time during which a binding is in effect. (define z 0) (define (fumble y) (set! z 2) ; Z has indefinite extent (+ z y) ; Y’s extent is the period ) ; during which the body ; of FUMBLE is being executed. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

Global Values of Symbols The collection of active global variables and their values represents a mapping between a set of symbols and their various bindings. In simple programs, we can usually ignore the fact that there are different packages. Welcome to DrScheme, version 201 > (define x 5) ; What’s going on here? X is a symbol which is registered (“interned”) in the Scheme session. Its global value has been set to 5. Its global binding has indefinite extent. It has global scope. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent Local Bindings Certain constructs cause creation of new local bindings. > (define x 99) ; Establishes a global binding ; when entered at the top level. > (define (foo x) (+ x 2)) ; Makes a local one > (foo 2000) 2002 > (let ((x 15)) (print x)) ; Another local one 15 > x 99 ; The global binding was not disturbed. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

Stacking of Local Bindings New local bindings are independent of existing local bindings. > (let ((x 1)) (let ((x 2)) (print x) ) ;Here X has 2 local bindings (print x) ) ;Here X has only 1 local b. 2 1 > (define (fact x) (if (= x 1) 1 (* x (fact (- x 1)))) ) > (fact 4) ;X will get 4 simultaneous bindings 24 CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent Hiding of Bindings The most recent existing local binding hides all other existing local bindings. The global value can be reached, even within the scopes of local bindings. > (define x 77); global 77 > (define (getx) x) > (define (foo x) (+ x (getx)) ) > (foo 1900) 1977 CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

Dynamic Scope with Special Variables Older dialects of Lisp did not limit the scopes of variables to function bodies or other restricted regions of the program. Variable values could be accessed anywhere in the program, provided the bindings were still in effect and were not shadowed by other bindings of the same symbols. Common Lisp supports this dynamic scoping if you ask for it. Scheme adheres strictly to lexical scoping. (Common Lisp Example -- Not Scheme!) (defun double (x) (declare (special x)) (announce) (+ x x) ) (defun announce () (format t "The value of X is ~A." x) ) CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

Comparisons for Lexical Variables and Functions Normal, lexical variables have lexical scope and indefinite extent [via closures]. Top-level bindings established with DEFINE have indefinite scope and indefinite extent. Dynamic variables (not available in Scheme) have indefinite scope and limited extent. [Limited by duration of evaluation of the construct that establishes the binding, e.g., by (double 2) ]. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent LET* vs LET LET (“Parallel”) begins the scope of each local binding at the first form after all the local bindings have been established. LET* (“Sequential”) begins the scope of each local binding immediately after its initial assignment. > (let ((x 5) (y 10) (z 30) w) (set! w (+ x y z) ) (print w) ) 45 > (let* ((x 5) (y (* x 2)) (z (* y 3)) w) (set! w (+ x y z)) CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent LETREC LETREC (“Parallel, recursive”) begins all the scopes of the local bindings at the beginning of the list in which they are established established. This permits local recursive procedures to be created. > (letrec ((addup (lambda (x) (if (= x 0) 0 (+ x (addup (- x 1))) ) ) )) (print (addup 4)) ) 10 > (addup 4) reference to undefined identifier: addup CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent Name Spaces A name space is a (dynamic) set of names (strings) in which a particular corresponding (dynamic) set of bindings (e.g., values, procedures, etc.) is effective. Examples: Symbols and their global bindings. Symbols and their local bindings within a procedure. Lexical scoping allows separation of name spaces. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent