Re: Reference and First-Class Function

Dimitre Novatchev <[email protected]> Sat, 20 Dec 2008 07:56:07 -0800 (PST)
Newsgroups gmane.text.xml.xslt.extensions
Message-ID <[email protected]>
Re: [exslt] Reference and First-Class Function
> =

> >This is a circular definition -- a reference is supposed to be implement=
ed
> by something that relies on reference=A0 itself.
> > =

> > So in this case the concept of reference is not layered on =

> > top of=A0the concept of HOF.
> > =

> =

> No, it's not circular. If you have function closures, you get references =
for
> free.
> =

> If I can create functions dynamically:
>=A0
>=A0 let $x :=3D (<a>abcd</a>, <b>pqrs</b>)
>=A0 let $ref :=3D new function()->node() {$x}
> =

> Then $ref is for all practical purposes a reference to the node-sequence =
$x.
> I can wrap this in some syntactic sugar to create functions ref() and
> deref(), but I don't need to add any more concepts. There is no conceptual
> difference between a reference to a value and a zero-argument function wh=
ose
> result is that value: and we shouldn't invent two new concepts if one of
> them entirely subsumes the other.


No, what is buried inside the definition of $ref above is not a "value". If=
 it is not a "copy" of $x it is a *reference* to $x.

So, the definition of $ref:

>=A0 let $ref :=3D new function()->node() {$x}

already contains a reference to $x.

You may pretend this is not a reference, but this doesn't change the fact.

When the concepts of partial application and closures were first created, t=
his was done in a context in which references were already there and nobody=
 could even imagine they could not use references.

The available definitions for closure (see for example: http://en.wikipedia=
.org/wiki/Closure_(computer_science)) confirm this.

To quote from that source:

"
In this example, the lambda expression (lambda (book) (>=3D (book-sales boo=
k) threshold)) appears within the function best-selling-books. When the lam=
bda expression is evaluated, Scheme creates a closure consisting of the cod=
e for the lambda and a reference to the threshold variable, which is a free=
 variable inside the lambda.
"

So, "a closure consisting of the code for the lambda and a reference to the=
 threshold variable". =


A little bit downwards:

"Because the closure in this case outlives the scope of the function that c=
reates it, the variables f and dx live on after the function derivative ret=
urns. In languages without closures, the lifetime of a local variable coinc=
ides with the execution of the scope where that variable is declared. In la=
nguages with closures, variables must continue to exist as long as any exis=
ting closures have references to them. This is most commonly implemented us=
ing some form of garbage collection"

So, "In languages with closures, variables must continue to exist as long a=
s any existing closures have references to them."

Once again, closures "have references" to external variables.

As I said even in my previous message, a closure contains a reference. A cl=
osure relies on the concept of reference being already implemented.


Dimitre


      =