From: Anticomuna
Subject: Dynamic variables in CL
Date: 
Message-ID: <e98be2d9-37f4-45ab-9037-0e32c1596c7c@b1g2000hsg.googlegroups.com>
Ok, I was trying to search for information in the group, but I still
don't have a clear picture of it.

What's the difference between a "special" and a "global" variable?

I can understand this, at least it seems to make sense to me:

(defun test (y) (+ y x))

(let ((x 5))
  (declare (special x))
  (test 5))

The x will be visible when I call the test function and will remain
visible while the let is executed, but what's point of it outside of a
lexical environment?

If I had proclaimed the 'x' to be special then every time I ran that
let the 'x' would be bound to the dynamic environment, making it
unnecessary for me to do the "declare"?

Other than the lexical enviroment and global environment there's also
a dynamic environment? So if Lisp searches for the binding, it will
search lexical first, then dynamic and finally global?

So setq can create non-special global variables, and defvar can create
special global variables!? Those will be created in two different
places?

From: ·······@eurogaran.com
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <f4ac790b-a088-448a-aaba-23b5b0d8773d@b5g2000pri.googlegroups.com>
> What's the difference between a "special" and a "global" variable?
>
From the perspective of programming language theory, a special
variable is a global variable that can be "shadowed" by a lexical
scope redefinition.
In other words, suppose you have a global variable x already defined,
and then you do (let ((x...
the new definition of x will work inside the scope of the let
instruction, and -as soon as you leave it- the "outer" definition will
hold again.
Therefore, all specials are global, but not all globals are special.
From: Ron Garret
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <rNOSPAMon-312B38.01513002042008@news.gha.chartermi.net>
In article 
<····································@b5g2000pri.googlegroups.com>,
 ·······@eurogaran.com wrote:

> > What's the difference between a "special" and a "global" variable?
> >
> From the perspective of programming language theory, a special
> variable is a global variable that can be "shadowed" by a lexical
> scope redefinition.
> In other words, suppose you have a global variable x already defined,
> and then you do (let ((x...
> the new definition of x will work inside the scope of the let
> instruction, and -as soon as you leave it- the "outer" definition will
> hold again.
> Therefore, all specials are global, but not all globals are special.

This is almost completely wrong.

Both of you need to read:

http://www.flownet.com/ron/specials.pdf

rg
From: ·······@eurogaran.com
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <09c1e088-40a7-45de-bf0c-d29032c7b308@s19g2000prg.googlegroups.com>
> > Therefore, all specials are global, but not all globals are special.
>
> This is almost completely wrong.

Therefore, the opposite must be true (??)
I mean... I was only discussing the initial overall concepts of
special vs. global, outside the specific Lisp arena, which you do very
well.

> Both of you need to read:
>
> http://www.flownet.com/ron/specials.pdf

Really good paper. Thanks for the link.
Just one comment for the casual reader: Notice that different
implementations (notably GCL and SBCL) behave differently under the
examples discussed (independently of the question which one is
compliant, or neither).
From: Geoffrey Summerhayes
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <dc7c391a-477c-4fcf-acd6-0ea2051e8329@r9g2000prd.googlegroups.com>
On Apr 2, 8:04 am, ·······@eurogaran.com wrote:
> > > Therefore, all specials are global, but not all globals are special.
>
> > This is almost completely wrong.
>
> Therefore, the opposite must be true (??)
> I mean... I was only discussing the initial overall concepts of
> special vs. global, outside the specific Lisp arena, which you do very
> well.

You went wrong from the start, not all specials are globals
and not all globals are special (in language theory).

In Common Lisp, all globals ARE special.

> > Both of you need to read:
>
> >http://www.flownet.com/ron/specials.pdf
>
> Really good paper. Thanks for the link.
> Just one comment for the casual reader: Notice that different
> implementations (notably GCL and SBCL) behave differently under the
> examples discussed (independently of the question which one is
> compliant, or neither).

GCL is notorious for not being compliant and, judging
from the dates on most of the sources, it won't be getting
there anytime soon so it's behaviour is irrelevant.

----
Geoff
From: Ron Garret
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <rNOSPAMon-E7F52D.13062402042008@news.gha.chartermi.net>
In article 
<····································@r9g2000prd.googlegroups.com>,
 Geoffrey Summerhayes <·······@gmail.com> wrote:

> On Apr 2, 8:04�am, ·······@eurogaran.com wrote:
> > > > Therefore, all specials are global, but not all globals are special.
> >
> > > This is almost completely wrong.
> >
> > Therefore, the opposite must be true (??)
> > I mean... I was only discussing the initial overall concepts of
> > special vs. global, outside the specific Lisp arena, which you do very
> > well.
> 
> You went wrong from the start, not all specials are globals
> and not all globals are special (in language theory).
> 
> In Common Lisp, all globals ARE special.

Not quite.  (This is why kodifik was only *almost* completely wrong.)  
Constants as defined by defconstant are global but not special.  And you 
can use symbol macros to create mutable globals that are not special.

The part that kodifik got completely wrong was "all specials are 
global."  They aren't.

rg
From: Geoffrey Summerhayes
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <5ab88369-131b-4ac9-9e79-7075a9b50e2d@c19g2000prf.googlegroups.com>
On Apr 2, 4:06 pm, Ron Garret <·········@flownet.com> wrote:
> In article
> <····································@r9g2000prd.googlegroups.com>,
>  Geoffrey Summerhayes <·······@gmail.com> wrote:
>
> > On Apr 2, 8:04 am, ·······@eurogaran.com wrote:
> > > > > Therefore, all specials are global, but not all globals are special.
>
> > > > This is almost completely wrong.
>
> > > Therefore, the opposite must be true (??)
> > > I mean... I was only discussing the initial overall concepts of
> > > special vs. global, outside the specific Lisp arena, which you do very
> > > well.
>
> > You went wrong from the start, not all specials are globals
> > and not all globals are special (in language theory).
>
> > In Common Lisp, all globals ARE special.
>
> Not quite.  (This is why kodifik was only *almost* completely wrong.)  
> Constants as defined by defconstant are global but not special.  And you
> can use symbol macros to create mutable globals that are not special.
>
> The part that kodifik got completely wrong was "all specials are
> global."  They aren't.

For a minute there I thought it was yesterday...
FINE. For the failed philosophy major, those
'variables' declared at top-level by DEFPARAMETER
or DEFVAR are special.

As for DEFCONSTANT, how do you know it's not
special? It's a lot like arguing whether a
the surface of a point is spherical or cubic.

---
Geoff
From: Ron Garret
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <rNOSPAMon-82EBFF.10303303042008@news.gha.chartermi.net>
In article 
<····································@c19g2000prf.googlegroups.com>,
 Geoffrey Summerhayes <·······@gmail.com> wrote:

> On Apr 2, 4:06�pm, Ron Garret <·········@flownet.com> wrote:
> > In article
> > <····································@r9g2000prd.googlegroups.com>,
> > �Geoffrey Summerhayes <·······@gmail.com> wrote:
> >
> > > On Apr 2, 8:04�am, ·······@eurogaran.com wrote:
> > > > > > Therefore, all specials are global, but not all globals are special.
> >
> > > > > This is almost completely wrong.
> >
> > > > Therefore, the opposite must be true (??)
> > > > I mean... I was only discussing the initial overall concepts of
> > > > special vs. global, outside the specific Lisp arena, which you do very
> > > > well.
> >
> > > You went wrong from the start, not all specials are globals
> > > and not all globals are special (in language theory).
> >
> > > In Common Lisp, all globals ARE special.
> >
> > Not quite. �(This is why kodifik was only *almost* completely wrong.) �
> > Constants as defined by defconstant are global but not special. �And you
> > can use symbol macros to create mutable globals that are not special.
> >
> > The part that kodifik got completely wrong was "all specials are
> > global." �They aren't.
> 
> For a minute there I thought it was yesterday...
> FINE. For the failed philosophy major, those
> 'variables' declared at top-level by DEFPARAMETER
> or DEFVAR are special.
> 
> As for DEFCONSTANT, how do you know it's not
> special?

Because I read the spec.  A special variable is a synonym for a dynamic 
variable, and a dynamic variable is one that is bound in the dynamic 
environment.  Variables bound with defconstant are bound in the global 
environment, not the dynamic environment, and hence are not special.

> It's a lot like arguing whether a
> the surface of a point is spherical or cubic.

No, it isn't.

rg
From: Geoffrey Summerhayes
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <d3ea85e0-6f56-4acb-bec3-2557c82988f4@i12g2000prf.googlegroups.com>
On Apr 3, 1:30 pm, Ron Garret <·········@flownet.com> wrote:
>
> Because I read the spec.  A special variable is a synonym for a dynamic
> variable, and a dynamic variable is one that is bound in the dynamic
> environment.  Variables bound with defconstant are bound in the global
> environment, not the dynamic environment, and hence are not special.
>
> > It's a lot like arguing whether a
> > the surface of a point is spherical or cubic.
>
> No, it isn't.

Yes, I've read the spec. But it really wouldn't matter if they
were in the dynamic namespace, any attempt to rebind them,
lexically or dynamically, leads to undefined behaviour, the
HOW the rebinding happens is irrelevant to correct code.

----
Geoff
From: Ron Garret
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <rNOSPAMon-D4CD0B.14382303042008@news.gha.chartermi.net>
In article 
<····································@i12g2000prf.googlegroups.com>,
 Geoffrey Summerhayes <·······@gmail.com> wrote:

> On Apr 3, 1:30�pm, Ron Garret <·········@flownet.com> wrote:
> >
> > Because I read the spec. �A special variable is a synonym for a dynamic
> > variable, and a dynamic variable is one that is bound in the dynamic
> > environment. �Variables bound with defconstant are bound in the global
> > environment, not the dynamic environment, and hence are not special.
> >
> > > It's a lot like arguing whether a
> > > the surface of a point is spherical or cubic.
> >
> > No, it isn't.
> 
> Yes, I've read the spec. But it really wouldn't matter if they
> were in the dynamic namespace

It would in a threaded environment.

rg
From: Pascal Costanza
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <65hue1F2fmnjnU1@mid.individual.net>
·······@eurogaran.com wrote:
>> What's the difference between a "special" and a "global" variable?
>>
> From the perspective of programming language theory, a special
> variable is a global variable that can be "shadowed" by a lexical
> scope redefinition.

No, that's wrong. Once a variable is special, it remains special. 
Rebinding it locally has 'special' semantics.

The best explanation for special variables is given in CLtL2.

See http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node43.html


Pascal

-- 
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Duane Rettig
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <o0abkc56pk.fsf@gemini.franz.com>
Pascal Costanza <··@p-cos.net> writes:

> ·······@eurogaran.com wrote:
>>> What's the difference between a "special" and a "global" variable?
>>>
>> From the perspective of programming language theory, a special
>> variable is a global variable that can be "shadowed" by a lexical
>> scope redefinition.
>
> No, that's wrong. Once a variable is special, it remains
> special. Rebinding it locally has 'special' semantics.

Careful; this statement is misleading if read in a particular
context.  It would be more completely accurate if the second sentence
started: "Once a variable has been globally declared as special ..."
For variables declared special only locally, the bindings do indeed
shadow the special bindings, as is seen in 3.3.4.1:
http://www.franz.com/support/documentation/current/ansicl/subsubse/exampl21.htm

> The best explanation for special variables is given in CLtL2.
>
> See http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node43.html

We should not be referring to CLtL2, especially to gather information
that has been clarified in the Ansi Spec.  Specifically, there is a
paragraph in the page you refer to that says "Thus we speak of
``special'' variables as having dynamic scope, or being dynamically
scoped, because they have indefinite scope and dynamic extent".  This
does not agree with the Ansi spec, which defines "special variable" in
the glossary as equivalent to a dynamic variable.  Note that as in the
example I linked to in the spec above, a dynamic variable isn't
necessarily a global variable.  My own habit is to always refer to
variables that have been established via defvar, defparameter, etc, or
via a global special declaim or proclaim as being a "globally-special"
variable, as opposed to just a "special" variable.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Barry Margolin
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <barmar-752589.02574102042008@newsgroups.comcast.net>
In article 
<····································@b1g2000hsg.googlegroups.com>,
 Anticomuna <············@uol.com.br> wrote:

> Ok, I was trying to search for information in the group, but I still
> don't have a clear picture of it.
> 
> What's the difference between a "special" and a "global" variable?

A global variable is one that's proclaimed special.  Locally special 
variables are not used much, IMHO.

> 
> I can understand this, at least it seems to make sense to me:
> 
> (defun test (y) (+ y x))
> 
> (let ((x 5))
>   (declare (special x))
>   (test 5))
> 
> The x will be visible when I call the test function and will remain
> visible while the let is executed, but what's point of it outside of a
> lexical environment?

There isn't one.  That's why this type of programming isn't used much.  
It means that TEST can only be called from functions that have locally 
declared X to be special.  Since TEST is dependent on that variable, it 
should have been made a global variable, by proclaiming it special 
(typically achieved using DEFVAR).

> 
> If I had proclaimed the 'x' to be special then every time I ran that
> let the 'x' would be bound to the dynamic environment, making it
> unnecessary for me to do the "declare"?

Correct.

> Other than the lexical enviroment and global environment there's also
> a dynamic environment? So if Lisp searches for the binding, it will
> search lexical first, then dynamic and finally global?

No, it searches the closest enclosing environment first.

(let ((x 5))
  (let ((x 10))
    (declare (special x))
    (print x)))

will print X from the dynamic environment.

> So setq can create non-special global variables, and defvar can create
> special global variables!? Those will be created in two different
> places?

SETQ doesn't create variables, it just assigns to existing variables.  
The consequences are undefined if you SETQ a variable that hasn't been 
created yet -- most implementations treat it as if you first proclaimed 
the variable special.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Pascal Costanza
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <65i0g1F2fg5v5U1@mid.individual.net>
Barry Margolin wrote:
> In article 
> <····································@b1g2000hsg.googlegroups.com>,
>  Anticomuna <············@uol.com.br> wrote:
> 
>> Ok, I was trying to search for information in the group, but I still
>> don't have a clear picture of it.
>>
>> What's the difference between a "special" and a "global" variable?
> 
> A global variable is one that's proclaimed special.  Locally special 
> variables are not used much, IMHO.

...but see http://www.tfeb.org/lisp/hax.html#DYNAMIC-STATE


Pascal

-- 
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Alex Mizrahi
Subject: Re: Dynamic variables in CL
Date: 
Message-ID: <47f3811c$0$90270$14726298@news.sunsite.dk>
 A> What's the difference between a "special" and a "global" variable?

variable can be globally proclaimed special, or locally declared special.

there is no such thing as "global variable" in standard. SYMBOL-VALUE of a 
symbol is sort of global value, but it doesn't make that symbol a 
"variable".

 A> I can understand this, at least it seems to make sense to me:

 A> (defun test (y) (+ y x))

 A> (let ((x 5))
 A>   (declare (special x))
 A>   (test 5))

 A> The x will be visible when I call the test function

no, it won't be visible. if you write this in SBCL:

CL-USER> (defun test (y) (+ y x))
; in: LAMBDA NIL
;     (+ Y X)
;
; caught WARNING:
;   undefined variable: X


it detects that X is undefined.
however your code works correctly, because by default SBCL assumes that 
undefined variables are special.

you should write it explicitly instead of relying on non-standard behaviour:

(defun test (y) (declare (special x)) (+ y x))

then it will run fine without warnings.

 A>  and will remain visible while the let is executed, but what's point of
 A> it outside of a lexical environment?

outside scope where variable was declared special X can be used as lexical 
one.
i.e.:

(defun test2 (y)
 (declare (special x))
     (+ y x))

(defun test1 (y)
 (let ((x 2))
   (test2 (+ x y))))

(let ((x 1))
 (declare (special x))
 (test1 4))

you get 7 as result. lexical binding in test1 did not affect special 
binding. if you add (declare (special x)) in LET of TEST1, you'll get 
different result: 8.

this is pretty cool, but it's rarely needed -- confusion between lexical and 
special variables never happens because all sane people surrond special ones 
with **.
still, you might find (declare special) useful if you use a special variable 
inside single function, i.e. with some recursive tree traversal -- that way 
definition will be self-contained.

 A> If I had proclaimed the 'x' to be special then every time I ran that
 A> let the 'x' would be bound to the dynamic environment, making it
 A> unnecessary for me to do the "declare"?

yup

 A> Other than the lexical enviroment and global environment there's also
 A> a dynamic environment?

no, global = dynamic in Common Lisp

 A>  So if Lisp searches for the binding, it will search lexical first, then
 A> dynamic and finally global?

no, Lisp knows at compile time whether it's lexical or dynamic.

 A> So setq can create non-special global variables,

it works like that in all implementations i know, but it is not a standard 
behaviour.
standard does not define what happens if you use setq on undefined 
variable -- it could crash, for example,
but all implementations just set symbol-value in this case, what setq 
actually meant to do.

standard way of implementing global non-special variables is via 
symbol-macros -- their binding rules work this way.

 A>  Those will be created in two different places?

no, in both cases it uses SYMBOL-VALUE place.
difference is that if you use defvar, compiler knows that variable is 
special and uses it appropriately.