Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic binding

26 views
Skip to first unread message

His Holiness the Reverend Doktor Xenophon Fenderson, the Carbon(d)ated

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
I'm not certain I understand dynamic binding correctly. Here's the
example code I cooked up (being run under ACL5 on Linux):

(DEFVAR B 1)
=> B
(DEFUN FOO (A) (CONS A B))
=> FOO
(DEFUN BAR (B) (FOO 'A))
=> BAR
(DEFUN BAZ (B) (DECLAIM (SPECIAL B)) (FOO 'A))
=> BAZ
(BAR 'B)
=> (A . B) ; shouldn't that be "(A . 1)"?
(BAZ 'B)
=> (A . B) ; as expected (?)

I thought that (DECLAIM (SPECIAL B)) extends the dynamic environment
with the value of B from the lexical environment, and that the free
variable B in FOO would have dynamic binding. I'm trying to follow
Queinnec's descriptions of dynamic vs. lexical bindings in chapter 1
of _Lisp in Small Pieces_ (page 19 in my copy), along with the
HyperSpec's description of Lisp's evaluator in section 3. Er, help?

--
Rev. Dr. Xenophon Fenderson, the Carbon(d)ated, KSC, DEATH, SubGenius, mhm21x16
Pope, Patron Saint of All Things Plastic fnord, and Salted Litter of r.g.s.b
In a lecture, Werner von Braun once said "Ve haf alvays been aiming for zer
stars" and a little voice at the back replied "But ve keep hittink London".

Andrew Innes

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
On 21 Mar 1999 15:18:44 -0500, "His Holiness the Reverend Doktor Xenophon Fenderson, the Carbon(d)ated" <xeno...@irtnog.org> said:
>I'm not certain I understand dynamic binding correctly. Here's the
>example code I cooked up (being run under ACL5 on Linux):
>
>(DEFVAR B 1)
>=> B
>(DEFUN FOO (A) (CONS A B))
>=> FOO
>(DEFUN BAR (B) (FOO 'A))
>=> BAR
>(DEFUN BAZ (B) (DECLAIM (SPECIAL B)) (FOO 'A))
>=> BAZ
>(BAR 'B)
>=> (A . B) ; shouldn't that be "(A . 1)"?
>(BAZ 'B)
>=> (A . B) ; as expected (?)
>
>I thought that (DECLAIM (SPECIAL B)) extends the dynamic environment
>with the value of B from the lexical environment,

No, it declares that the enclosing binding is dynamic, not lexical. Any
lexically apparent binding is hidden.

>and that the free
>variable B in FOO would have dynamic binding.

It does, and so does the argument binding of BAR because DEFVAR
proclaims B to be special everywhere.

AndrewI

--
Andrew Innes Software Developer, Intelligence Products
Harlequin Limited, Barrington Hall, Barrington, Cambridge CB2 5RG, U.K.
Tel: +44 1223 873868 Fax: +44 1223 873873 http://www.harlequin.com

Kent M Pitman

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
"His Holiness the Reverend Doktor Xenophon Fenderson, the Carbon(d)ated" <xeno...@irtnog.org> writes:

> I'm not certain I understand dynamic binding correctly. Here's the
> example code I cooked up (being run under ACL5 on Linux):
>
> (DEFVAR B 1)
> => B

This declares B special in all subsequent uses, whether or not there
is an accompanying declare. It can't be retracted nor lexically shadowed.
(It can be shadowed by making a new package, of course.)

> (DEFUN BAZ (B) (DECLAIM (SPECIAL B)) (FOO 'A))
> => BAZ

Use DECLAIM only at top-level. Doing it in a program won't do what
you expect. Use only DECLARE in programs. Use either DECLAIM or
PROCLAIM at top-level.


His Holiness the Reverend Doktor Xenophon Fenderson, the Carbon(d)ated

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
>>>>> "KMP" == Kent M Pitman <pit...@world.std.com> writes:
>>>>> "me" == "Xenophon Fenderson, the Carbon(d)ated" <xeno...@irtnog.org> writes:

me> I'm not certain I understand dynamic binding correctly. Here's
me> the example code I cooked up (being run under ACL5 on Linux):
me>
me> (DEFVAR B 1) => B

KMP> This declares B special in all subsequent uses, whether or
KMP> not there is an accompanying declare. It can't be retracted
KMP> nor lexically shadowed. (It can be shadowed by making a new
KMP> package, of course.)

Ah, I understand. If I replace "(DEFVAR B 1)" with "(SETQ B 1)",
"(SPECIAL B)" works as expected.

me> (DEFUN BAZ (B) (DECLAIM (SPECIAL B)) (FOO 'A)) => BAZ

KMP> Use DECLAIM only at top-level. Doing it in a program won't
KMP> do what you expect. Use only DECLARE in programs. Use
KMP> either DECLAIM or PROCLAIM at top-level.

Yeah, once I read a little further down in the definition of SPECIAL I
realized I'd made an error.

Vassil Nikolov

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
In addition to the explanations in the other posting, consider this
variation of your example:

;; no DEFVAR of B
> (setf b 'g) ;give it a global value but don't make it dynamic everywhere
G
> (defun foo (a) (cons a b))
;; maybe a warning about assuming B special
FOO
> (defun lex-bar (b) (foo 'lex))
LEX-BAR
> (defun dyn-bar (b)
(declare (special b)) ;DECLAIM is usually top level only
(foo 'dyn))
DYN-BAR
> (lex-bar 'val)
(LEX . G) ;LEX-BAR binds its parameter lexically so FOO doesn't see that
> (dyn-bar 'val)
(DYN . VAL) ;FOO sees the binding of B made by DYN-BAR

In the above example, B is not globally declared (proclaimed) special
(which it would have been with DEFVAR), but just in specific places.

Disclaimer: I have not actually run this example (sorry, too lazy),
please run it!

> I thought that (DECLAIM (SPECIAL B)) extends the dynamic environment

> with the value of B from the lexical environment.
(...)

DECLAIM does something else, but I won't go into it as I think you
were thinking about DECLARE here. (After you have things about
declarations clear, read the description of DECLAIM.)

Now (DECLARE (SPECIAL B)) means: within the scope of this declaration
treat all references to B as references to a dynamic variable, ignoring
any lexical bindings that might otherwise be in force. `The scope of
this declaration' is the form in which this declaration appears including
any nested forms *but* excluding nested forms like LET and DO that bind
B. (Well, this is just a rough description; consult the literature for
the Real Thing.)

If you want (I can't think of a reason) to `extend the dynamic
environment with the value of B from the lexical environment' you
can do e.g.

(let ((x b))
(declare (special x))
...)

or

(defvar x 'init "my starless and bible black global variable")
...
(let ((x b)) ...)

---assuming that B is a lexical variable in the scope that surrounds
this LET---and for the duration of the execution of the LET, anywhere
in the program text (i.e. not just in the textual body of the LET),
the binding for X in the dynamic environment (until changed) would
have B's value. Now if you rename X as B in the above example, you
get a lexical B and a dynamic B. I hope you are not too confused.

Good luck, and keep asking questions until you are sure you get it,
Vassil.

P.S. Beware of analogies with other programming languages like C:
some things about variables are similar and some are very different.

Vassil Nikolov <vnik...@poboxes.com> www.poboxes.com/vnikolov
(You may want to cc your posting to me if I _have_ to see it.)
LEGEMANVALEMFVTVTVM (Ancient Roman programmers' adage.)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages