[plt-scheme] Closures

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Nov 5 16:02:35 EST 2007

I don't think Joel is asking for callable environments.

Plus closures don't have to be represented as structs containing a  
code ptr and an environment ptr.

Let's go back to d/dx:

(define (d/dx f)
   (local ((define (fprime x)
              ... f ... ;; your favorite numerical differentiation here
              ))
     ;; return:
     fprime))

So d/dx returns a local function definition, called fprime here. You  
can all it anything of course.

This function definition is lexically located in a context that  
specifies f. Thus, if you ever invoke fprime after d/dx returns it,  
you better know what values f was bound to when you defined fprime --  
otherwise, fprime means something else.

To accomplish this, you can create a data structure that collects the  
values from the lexical context that are relevant for a function  
definition. Historically, this data structure is called an  
environment. ( This may sound expensive; it really isn't if your  
compiler is any good.)

If you combine this data structure with the (representation of the)  
definition of the locally defined function, you get a closure.

BUT, neither environments nor closures are a necessity for executing  
programs that deal with functions as ordinary values. The history of  
computing has proposed a number of mechanisms and for all we know, we  
chose the wrong one.

-- Matthias







On Nov 5, 2007, at 3:41 PM, hendrik at topoi.pooq.com wrote:

> On Mon, Nov 05, 2007 at 02:40:52PM -0500, Joel J. Adamson wrote:
>> hendrik at topoi.pooq.com writes:
>>
>>> Ah.  Let's say you're a function.
>>
>> "Your immune system is like an army..."
>>
>>> Now this package of code-plus-environment, which closes off all the
>>> dangling reference a function might have, is called a closure.
>>> Abd indeed, because it is the closure of a function you can call it.
>>
>> Okay, I have not read the materials Matthias or Shriram suggested, so
>> I'm still naïve about this: this makes sense, however, from your
>> description you (the programmer) call the function, right?  The
>> environment exists (it's there), but you don't invoke an environment.
>> If you can, however, that's pretty cool and please tell me about  
>> it ;)
>
> Don't know about Scheme, but there have been proposals for modules
> system where a module evaluates to an environment and then somewhere
> else you import that environment.  Usually the environment is  
> trimmed to
> make available just those names the module is willing to export.
>
> This was in a module system proposed for Algol 68; I think something
> like it is in ML.  Adapting this idea to a statically-typed
> language works.  I suspect it would feel like unhygienic macros if  
> done
> in something like Scheme.
>
>>
>>> I don't even know if Scheme has any kinds of closures that can't be
>>> called.  I don't know of any.  So if your frame-sets are different
>>> (you seem to think they can't be called) perhaps you could give  
>>> me an
>>> intuitive explanation what *they* are.
>>
>> Just to clarify, I'm not dealing with a specific problem, I'm just
>> always looking for more ways to "think in Scheme."
>>
>> Thanks, this definitely helps,
>> Joel
>>
>>
>> -- 
>> Joel J. Adamson
>> Biostatistician
>> Pediatric Psychopharmacology Research Unit
>> Massachusetts General Hospital
>> Boston, MA  02114
>> (617) 643-1432
>> (303) 880-3109
>>
>>
>> The information transmitted in this electronic communication is  
>> intended only for the person or entity to whom it is addressed and  
>> may contain confidential and/or privileged material. Any review,  
>> retransmission, dissemination or other use of or taking of any  
>> action in reliance upon this information by persons or entities  
>> other than the intended recipient is prohibited. If you received  
>> this information in error, please contact the Compliance HelpLine  
>> at 800-856-1983 and properly dispose of this information.
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.