12
CSE 415 -- (c) S. Tanimot o, 2002 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent Global bindings Local bindings LET vs LET* Lexical vs Dynamic variables

CSE 415 -- (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

Embed Size (px)

Citation preview

Page 1: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

1

Where and When Do Symbols Refer to Values and Functions?Scope and Extent of Bindings

BindingsScopeExtentGlobal bindingsLocal bindingsLET vs LET*Lexical vs Dynamic variables

Page 2: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

2

Definition of Binding

Binding: A binding is 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.

“Binding” may refer to …•a symbol and its value in a particular context; (math definition)•a symbol and the memory location used to store one of its values; (Lisp definition)•the memory location associated with a symbol, to hold (one of) its value(s). (informal usage in talking about programming languages).

Page 3: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

3

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.

Page 4: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

4

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.

Page 5: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

5

Global Values of SymbolsSymbols 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.

hilbert% clisp> (setq x 5) ; What’s going on here?5X 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.

Page 6: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

6

Local Bindings

Certain constructs cause creation of new local bindings.

> (setq x 99) ; Establishes a global binding99> (defun foo (x) (+ x 2)) ; Makes a local oneFOO> (foo 2000)2002> (let ((x 15)) (print x)) ; Another local one15NIL> x99 ; The global binding was not disturbed.

Page 7: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

7

Stacking of Local BindingsNew 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.21NIL> (defun fact (x) (if (= x 1) 1 (* x (fact (1- x)))) )FACT> (fact 4) ;X will get 4 simultaneous bindings24

Page 8: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

8

Hiding of BindingsThe 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); global77> (defun foo (x) (+ x (symbol-value 'x)) )FOO> (foo 1900)1977

Page 9: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

9

“Unbound” VariablesA 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 persist!

> (setq newsymbol 5)5> (makunbound 'newsymbol); undo the assignmentNEWSYMBOL

Page 10: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

10

Dynamic Scope with Special VariablesOlder 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) )

Page 11: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

11

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) ].

Page 12: CSE 415 -- (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 415 -- (c) S. Tanimoto, 2002 AI Techniques

12

LET* vs LETLET (“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) )4545> (let* ((x 5)(y (* x 2)) (z (* y 3)) w) (setq w (+ x y z)) (print w) )4545