What is "closure"? 
Author Message
 What is "closure"?

I'm always seeing reference to "closure" and "downward closure", but I've
never run across an explanation of the terms (or are they the same?).  Can
anybody point me to one?  Thanks.

Mike



Mon, 21 Jan 2002 03:00:00 GMT  
 What is "closure"?
/ I'm always seeing reference to "closure" and "downward closure", but I've
/ never run across an explanation of the terms (or are they the same?).  Can
/ anybody point me to one?  Thanks.

I haven't seen "downward closure", but for a closure, suppose you have a
function which returns a function:

function plusX(x: integer)
    function F(y: integer)
        begin
            F := x+y
        end;
    begin
        plusX := F
    end;

Then
        plusX(4)(5) = F-with-x-equals-4(5) = 4+5 = 9

The "-with-x-equals-4" is the "closure". The closure is all global
variables that the function references. When F is returned from plusX, it
still needs a local variable of plusX. To allow this kind of programming,
you need a mechanism to make sure the closure survives at least as long as
the function value.

In C, the closure of a function consists of statickally allocated
variables, so no special mechanism is needed. Pascal prohibits this kind
of construct.

--
Collective Against Consensual Sanity
http://www.angelfire.com/ca3/cacs
(umm...ignore that: there's only an empty index page for now)
For a free CACS T-shirt send $42 shipping and handling.



Mon, 21 Jan 2002 03:00:00 GMT  
 What is "closure"?

Quote:

>In C, the closure of a function consists of statickally allocated
>variables, so no special mechanism is needed. Pascal prohibits this kind
>of construct.

Not entirely true. Pascal allows functions (closures) to be passed as
arguments to functions or procedures, but not to be returned from
these. This makes it possible to allocate the closures ("thunks") on
the stack. Returning closures from functions requires them to be
allocated on the heap (or some other place that survives procedure
returns).

The word "closure" has many other CS-related meanings, though. If you
have a set and needs it to be closed under certain operations or
relations on the elements of the set, you can apply a closure operator
that adds the required extra elements. The downwards closure of a set
is the smallest superset such that whenever an element is in the set,
all smaller elements (under some (possibly partial) ordering) are also
in the set. There are probably several other unrelated meanings of the
word.




Tue, 22 Jan 2002 03:00:00 GMT  
 What is "closure"?


Quote:

>>In C, the closure of a function consists of statickally allocated
>>variables, so no special mechanism is needed. Pascal prohibits this kind
>>of construct.

>Not entirely true. Pascal allows functions (closures) to be passed as
>arguments to functions or procedures, but not to be returned from
>these. This makes it possible to allocate the closures ("thunks") on
>the stack. Returning closures from functions requires them to be
>allocated on the heap (or some other place that survives procedure
>returns).

This is a "downward closure", which the original poster referred to.  It
means that the closure can be passed down the stack but not up the stack.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Tue, 22 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

2. BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

3. Parsing ""D""?

4. "Fifth", "Forth", zai nar?

5. Ruby "finalize", "__del__"

6. beginners "let"/"random" question

7. ANNOUNCE: new "plus"- and "dash"-patches available for Tcl7.5a2/Tk4.1a2

8. Looking for "stdin", "stdout"

9. Hi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "Two") text1.sub!("@BLAH", "Two") print text0,"\n" print text1,"\n" produces thiHi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "T

10. Match "ab" in "abc", but not in "abd"

11. replace string AFTER "size","initial", "next"

12. Lack of "D Tests", "E Tests", and "L Tests" for Generics

 

 
Powered by phpBB® Forum Software