Re: scope of function definition

Leon Bottou <[email protected]>
Newsgroups gmane.lisp.lush.devel
Message-ID <[email protected]>
On Sunday 24 April 2005 01:45 pm, Ralf Juengling wrote:
> Hi,
> 
> I noticed that de, df, dm etc. do the binding of function
> to symbol in lexical scope. 
> 
> ? (let ((f ())) (de f () 1))
> = f
> ? f
> = ()
> ?
>  
> Unlike defvar, the descriptions for de etc. are not explicit 
> about the scope of the binding. I think they should be explicit.
> 
> Although I'm still a lisp newbie my impression is that it would
> be more in line with other lisps if de/df/dm were doing the 
> binding globally (like, e.g., defun in CL). So if it doesn't 
> break too much, you might want to consider changing the 
> current behavior.

Initially, lush was a purely dynamically scoped lisp with functions as values.
Writing
 (de f(x) (+ x x)) 
was simply equivalent to writing 
 (setq f (lambda(x) (+ x x)).

This was completely consistent and workable, ... except for the fact
that dynamically scoped languages do not compile well.
The introduction of the compiler in lush has slowly changed 
the dialect towards lexical scoping.  
The intermediates states is messy.

Lush programs, obviously, do not use the fancy lexical scoping tricks 
that are commonplace in Commpn Lisp or Scheme.  But they sometimes
use fancy dynamical scoping tricks.  In practice this only happens
with global variables.

In common lisp, <defvar> is used to define a variable that is dynamically scoped
instead of lexically scoped.  Such variables happen to be global by necessity.

In lush, <defvar> is used to define a global variable regardless of the current environment.
Of course this variable will be dynamically scoped because (1) the interpreter only
handles dynamically scoped variables, and (2) the compiler does not handle global variables at all.

The addition of <defvar> is part of a nifty long term transition plan:  
First we slowly update all lush program to define global variables with <defvar>.
At some point, all variables involved in dynamic scoping tricks will be declared in such a way.
Then we can effortlessly transition to a CL semantic, where all variables are lexically scoped except
those declared with defvar.  This change would eliminate the biggest semantic difference 
between the interpreter and the compiler, make lush a better grounded language,
and save us a few headaches.

Ralf got surprised because he read the advice to declare global variables 
with <defvar> and observed that function <de> is quite different. 
This is indeed a bit confusing.

Until now <defun> was defined as an alias to <de>
for the sake of people familar with common lisp and similar dialects.
As Ralf notices, this is only a gross approximation.

We can make it a little bit better (but still far from perfect).
From now on, writing
 (defun f(x) (+ x x)
is equivalent to
 (setq :f (lambda(x) (+ x x))  
which is the same as
 (defvar f (lambda(x) (+ x x)) 

This might be a little less confusing...


- L.


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_ide95&alloc_id396&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.