Bindings, Scope, and Extent

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

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 COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Chapter 7: User-Defined Functions II
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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).
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
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.
CSE S. Tanimoto Introduction 1 LISP Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
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
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
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,
Basic Semantics Associating meaning with language entities.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
CSE 341, S. Tanimoto Lisp LISP LISP = LISt Processing Intended for processing symbolic information Implementations from noncommercial sites: GNU.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Lisp S-Expressions: ATOMs
Defining Macros in Lisp
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
LISP LISP = LISt Processing
Bindings, Scope, and Extent
CSE S. Tanimoto Introduction
Representation, Syntax, Paradigms, Types
CSE S. Tanimoto Explicit Function Application
CSE S. Tanimoto Introduction
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
6.001 SICP Variations on a Scheme
CSE S. Tanimoto Introduction
Functional Programming Concepts
Lisp: Representation of Data
Defining Macros in Lisp
Representation, Syntax, Paradigms, Types
CSE (c) S. Tanimoto, 2002 Introducing Lisp
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
CSE 3302 Programming Languages
LISP: Basic Functionality
CSE 341 Lecture 11 b closures; scoping rules
Functional Programming Concepts
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
CSE 190p University of Washington Michael Ernst
Lisp: Representation of Data
LISP primitives on sequences
Presentation transcript:

Bindings, Scope, and Extent Global bindings Local bindings and lexical scope Function bindings LET vs LET* 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 Lisp object such as the global value of the symbol or the function binding 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. (defun 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. (defun fumble (y) (setq 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 Symbols are normally grouped into packages. A package represents a mapping between a set of symbols and their various bindings within the package. In simple programs, we can usually ignore the fact that there are different packages. Allegro Common Lisp V5.0 > (setq x 5) ; What’s going on here? 5 X is a symbol which is registered (“interned”) in the current package. Its global value (“symbol value”) has been set to 5. Its symbol-value 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. > (setq x 99) ; Establishes a global binding 99 > (defun foo (x) (+ x 2)) ; Makes a local one FOO > (foo 2000) 2002 > (let ((x 15)) (print x)) ; Another local one 15 NIL > 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 NIL > (defun fact (x) (if (= x 1) 1 (* x (fact (1- x)))) ) FACT > (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 (“symbol value”) can be reached, even within the scopes of local bindings. > (setq x 77); global 77 > (defun foo (x) (+ x (symbol-value 'x)) ) FOO > (foo 1900) 1977 CSE 341 -- S. Tanimoto Bindings, Scope, and Extent

CSE 341 -- S. Tanimoto Bindings, Scope, and Extent “Unbound” Variables A symbol that has not been given a value by assignment or in a function call is said to be “unbound.” > (+ newsymbol 5) ERROR Unbound variable NEWSYMBOL. The use of “unbound” here is actually a misnomer, because NEWSYMBOL has a binding. The binding just doesn’t have any value. But old uses of terminology persists! > (setq newsymbol 5) 5 > (makunbound 'newsymbol); undo the assignment NEWSYMBOL 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. (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]. Symbol-to-Function bindings established with DEFUN have indefinite scope and indefinite extent. Dynamic variables 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) (setq w (+ x y z) ) (print w) ) 45 > (let* ((x 5)(y (* x 2)) (z (* y 3)) w) (setq w (+ x y z)) 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, functions, etc.) is effective. Examples: Symbols and their SYMBOL-VALUE bindings in the Common-Lisp-User package Symbols and their operator (functions and macros) bindings in a Lisp graphics package. Lisp’s main means of organizing name spaces are: Packages and Binding types (value vs operator)  An alternative definition of a name space is a coherent set of possible names that can be used to unambiguously reference some coherent set of possible values, functions, etc. CSE 341 -- S. Tanimoto Bindings, Scope, and Extent