I did not have any problem with your presentation in
http://jsoftware.com/pipermail/programming/2016-July/045440.html
(other than a mild reaction to the bulk of code you used for such a
simple concept).

But, since this topic provokes a lot of back and forth, I suppose it
won't hurt to go over it yet again.

In the context of programming, a "closure" means a variable which has
a value bound to it across calls to a function or procedure.

In some languages this value can be updated.

In J you can approximate this in one of several ways:

(*) bind a function argument (or part of one) to a specific value.

F bind 1 2 3  NB. binding a function argument to a specific value

(G 1 2 3&,)   NB. binding part of a function argument to a specific value

(*) Use a locale (cocreate something to hold the values and then refer to it).

(*) Store the information outside of J.

And, of course, the link Pascal provided gives examples of those.

That said, in other programming languages, closures are typically
implemented as a side effect of implementing nested scopes, and J does
not provide nested scopes (unless you emulate them using locales).

For example, if you can get to the command line of a web browser (for
example, in Chrome or Safari, right click on a web page and select
"Inspect Element" and then select the console in the panel that pops
up, or hit escape to toggle another view of the console), try this:

function F(y) {
    var Y= y;
    return function G(y) {
        Y= Y+y;
        return Y;
    }
}

H= F(10);

H(1);
H(2);
H(3);

So... perhaps now we can agree that that grease spot on the ground is
no longer a horse?

-- 
Raul


On Mon, Jul 11, 2016 at 11:55 PM, Jose Mario Quintana
<[email protected]> wrote:
> Thank you for providing the link Pascal.
>
> I am somewhat confused by the concept of closure.  My perspective is from
> what an interpreter allows, as opposed to what the Dictionary allows.  Part
> of the problem is that currently I am only familiar with J, mostly tacit J, 
> and
> its related terminology (although in ancient times I used other programming
> languages).  Thus, let me to begin with my own tentative mapping of some
> terms (criticisms, comments and suggestions are, of course, welcome).
>
> Mapping of terms
>
> J Others
>
> noun data (atom in FP)
> name variable (also name)
> verb function
> adverb functional (aka, functor)
> conjunction functional (aka, functor)
> higher-order (wicked) verb      higher-order function (which is a
> functional)
> tacit point-free style
> fixed tacit variable-free
>
> The question is: Are closures missing (and cannot be closely emulated) in
> J, tacit J, or fixed tacit J (which is the one I care the most)?
>
> On the one hand, in the context of fixed tacit J, according the reference
> [2] in my first message, closures "are a technique for implementing
> lexically scoped name binding in languages with first-class functions"
> which implies that the question above is vacuous since fixed tacit code
> includes no names at all."  One the other hand, the item in [0] on
> Higher-order functions states "When returning a function, we are in fact
> returning its closure." implying that the example in my first message was
> indeed a closure...  Except, perhaps, for a technicality: I used a
> functional (an adverb), instead of a higher-order function (a verb) to
> produce a function (a verb).  However, using a higher-order function (verb)
> is not difficult:
>
> (Warning, I will show definitions using the latest toolkit (and providing a
> link where one can get it); in other words, potential Jym spoilers will
> follow shortly.)
>
>    JVERSION
> Engine: j805/j64/windows
> Beta-9: commercial/2016-07-05T15:45:22
> Library: 8.04.15
> Qt IDE: 1.4.10/5.4.2
> Platform: Win 64
> Installer: J804 install
> InstallPath: j:/program files/j
> Contact: www.jsoftware.com
>
> The latest Wicked Tacit toolkit is in the zipped folder [1].  Run it as a
> silent script if you do not want to see potential spoilers,
>
>    (0!:0)<'/.../J Tacit Tollkit.ijs'
>
> The interface of the function (verb) derivative follows closely its
> JavaScript counterpart; that is, its a monadic verb and takes a function
> together with (the length of ) an interval (*: and 0.000001 as an
> example).  Since these words have different type they are expected as a
> boxed list (with a tally of two).  One convenient way for producing a list
> of boxed words (nouns, verbs, adverbs and conjunctions) is via the
> recurrent (aka, multiple) utility adverb sb.  For example,
>
>    [: *: 0.000001 ]sb
> ┌──┬────┐
> │*:│1e_6│
> └──┴────┘
>
> The higher-order function derivative can be defined as,
>
>    derivative=. ([: ([: ([ at [: an y +cv ]cv Train) -cv [ Train) %cv (]
> rank _:) Train)p f.
>      NB.             (   v @: (  n    +   ]   )      -   v )      %    n "
>    _
>
> It is fixed,
>
>    66 Wrap 'derivative'
> ,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(,^:(0:``:)&6 :.(<@:((,'0') ,
> &:< ]))@:(([ ,^:(0:`@:) ,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:(
> (,'0') ,&:< ])@:] ; >@:((<+)"_) ; >@:((<])"_))) ; >@:((<-)"_) ; [)
>  ; >@:((<%)"_) ; ] ,^:(0:`") _:)&:>/
>
> It produces a function,
>
>    derivative [: *: 0.000001 ]sb
> (*:@:(9.9999999999999995e_7 + ]) - *:) % 9.9999999999999995e_7"_
>
> and it seems to work according to the specifications,
>
>    (derivative [: *: 0.000001 ]sb) 0 1 2 3 4
> 1e_6 2 4 6 8
>
> Notice that, after all this, the adverb Der and the higher-order verb
> derivative produce the same verb given the same arguments; thus, it might
> seem irrelevant whether one uses and adverb or a higher-order verb to
> produce the closure verb.  Yet, I used almost the same higher-order verb
> derivative (the same verb but without the trailing adverb p) behind the
> scenes to define the adverb Der.  The products of the higher-order verb
> derivative may, or may not, strictly qualify as closures but their behavior
> corresponds to those of closures with immutable variables (as far as I can
> see).
>
> Despite the title, the main subject of the link you provided is the
> famous/infamous Graham's Accumulator Generator challenge which seems to
> require the production of a named stateful function (verb).  If I am not
> mistaken, this could be achieved, using languages that support closures, by
> binding mutable variables.  However, this does not imply that closures with
> immutable bound variables are not closures.
>
> Nevertheless, one could produce, emulate if you will, a named fixed tacit
> stateful function by forcing it to dynamically modify and fix its own code
> at each invocation (as a side effect).  That would be an interesting
> exercise for the Jym.
>
> Allow me to reiterate: my statements regarding closures and related terms
> are tentative and I am willing to be educated.  Actually, that was my
> purpose for starting this thread albeit with hesitation because sometime
> ago closures were a subject matter of heated discussions (see the thread
> associated to the message [2]).  An extra complication arises because
> details of other related concepts, such as, function-level programming and
> functions as first-class citizens, seem to be controversial, or unclear, to
> some extent.
>
> References
>
> [0]  Higher-order functions: returning functions as result (in First-class
> function)
>
> https://en.wikipedia.org/wiki/First-class_function#Higher-order_functions:_returning_functions_as_results
>
> [1]  Jx/J/Wicked Tacit Toolkit.ijs file in
> http://2bestsystems.com/foundation/j/Jx.zip
>
> [2]  [Jprogramming] Recursive programming (and scoping therein)
>
> http://www.jsoftware.com/pipermail/programming/2013-February/031561.html
>
>
> On Thu, Jul 7, 2016 at 9:29 AM, 'Pascal Jasmin' via Programming <
> [email protected]> wrote:
>
>>
>>
>> for another look at closures,
>>
>>
>> http://code.jsoftware.com/wiki/Guides/Lexical_Closure
>>
>> Your examples don't seem to follow the pattern of binding a dyad with the
>> result of its application as a side effect of its application.
>>
>> A side effect and "singleton" assigned closure makes sense if the closure
>> is to act as a guard that updates on each call.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> ________________________________
>> From: Jose Mario Quintana <[email protected]>
>> To: Programming forum <[email protected]>
>> Sent: Wednesday, July 6, 2016 10:56 PM
>> Subject: [Jprogramming] Are closures possible in J?
>>
>>
>> I recently saw the following message from June Kim in the Chat Forum [0].
>>
>>   http://procyonic.org/blog/?p=171
>>
>>    I miss closure in J.
>>
>> which seems to imply the closures are missing in J.
>>
>> Perhaps I am misunderstanding the concept but, as far as I can see,
>> closures or similar alternatives are available in J, at least within the
>> (wicked) tacit dialect which is my focus of interest.
>>
>> One indication that closures (or similar alternatives) are available in J
>> is the solution [1] to the Rosetta Code J task Closures/Value capture,
>> which presumably involves closures.  The following is another example: the
>> function derivative (verb) described in the Wikipedia entry [2] can be
>> implemented easily with wicked spells. ;)  I am not showing at the details
>> because this can be an interesting future Jym exercise but the outline of
>> the implementation in the form of an edited session follows.
>>
>> First some good news, at least from my perspective, the functionality of a
>> new updated version of the old Wicked Tacit Toolkit (WTT) [3] survives all
>> the improvements up to version Beta-9.
>>
>>    JVERSION
>> Engine: j805/j64/windows
>> Beta-9: commercial/2016-07-05T15:45:22
>> Library: 8.04.15
>> Qt IDE: 1.4.10/5.4.2
>> Platform: Win 64
>> Installer: J804 install
>> InstallPath: j:/program files/j
>> Contact: www.jsoftware.com
>>
>> Running the (new) WTT,
>>
>> (0!:1) < '/ ... /Wicked Tacit Toolkit.ijs'
>>    NB.
>>    NB.
>>
>> ----------------------------------------------------------------------------
>>    NB. Wicked Tacit Toolkit...
>>    NB.
>>
>> ----------------------------------------------------------------------------
>> .
>> .
>> .
>>
>> Defining the producing adverb,
>>
>>    NB. Derivative (secant)...
>>
>>    Der=. ...  NB. A one-liner, in terms of the utilities, is not shown to
>> avoid a spoiler
>>
>> The defined word Der is a wicked fixed adverb; actually, it is a recurrent
>> adverb (aka, multiple adverb (a double adverb in this instance)).  I guess
>> I am not spoiling much by showing its linear representation which is faulty
>> (unfortunately, linear representations often are faulty for complex fixed
>> tacit adverbs, not to mention wicked tacit adverbs).
>>
>>    type'Der'
>> ┌──────┐
>> │adverb│
>> └──────┘
>>    66 Wrap'Der'  NB. It is fixed...
>> ((("_)(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((,'0') ,
>> &:< ])@:((<,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(,^:(0:``:)&6 :.(<
>> @:((,'0') ,&:< ]))@:(([ ,^:(0:`@:) ,^:(0:``:)&6 :.(<@:((,'0') ,&:<
>> ]))@:(<@:((,'0') ,&:< ])@:] ; >@:((<+)"_) ; >@:((<])"_))) ; >@:((
>> <-)"_) ; [) ; >@:((<%)"_) ; ] ,^:(0:`") _:)) ,~ <) ; >@:((<,~)"_)
>> ; >@:((<<)"_))@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((0;1;0)
>> &({::))))@:[)))((`_)(`:6))))(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:
>> ((<@:((,'0') ,&:< ])@:(,^:(0:`/))@:(,^:(0:`&)) >@:((<>)"_))@:>@:{:
>> , <@:((,'0') ,&:< ])@:}:)@:))(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0')
>> ,&:< ]))@:((<(,'0');"_) ; ] ; (<(,'0');(@:(,^:(0:``:)&6 :.(<@:((,
>> '0') ,&:< ]))@:(<@:((0;1;0)&({::)))))((((`''`)(`(((@:[)(&`))(`:6))
>> ))(`((`_)(`:6))))(`:6)))"_)@:[)))((`_)(`:6)))
>>
>>
>>    ( double=. *: 0.000001 Der )  NB. Is the verb double a closure (*: and
>> 0.000001 persist)?
>> (*:@:(9.9999999999999995e_7 + ]) - *:) % 9.9999999999999995e_7"_
>>
>>
>>    *: double 0 1 2 3 4
>> 1e_12 4 16 36 64
>>
>> Since the produced verb double is fixed it becomes independent of the
>> producing adverb Der.  Likewise, an intermediate adverb can be defined as,
>>
>>    der=. 0.000001 Der  NB. Is the adverb der is also a closure (0.000001
>> persists)?
>>
>>    type'der'
>> ┌──────┐
>> │adverb│
>> └──────┘
>>    66 Wrap'der'  NB. It is fixed
>> ("_)(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:((<@:((,'0') ,&
>> :< ])@:(,^:(0:`/))@:(,^:(0:`&)) >@:((<>)"_))@:>@:{: , <@:((,'0') ,
>> &:< ])@:}:)@:((9.9999999999999995e_7;,^:(0:``:)&6 :.(<@:((,'0') ,&
>> :< ]))@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(([ ,^:(0:`@:) ,^:(0
>> :``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((,'0') ,&:< ])@:] ; >@:((<+)"
>> _) ; >@:((<])"_))) ; >@:((<-)"_) ; [) ; >@:((<%)"_) ; ] ,^:(0:`")
>> _:)) ,~ <)@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((0;1;0)&({:
>> :))))@:[)))((`_)(`:6)))
>>
>> Again, once der is produced it becomes independent of Der because it is
>> fixed.
>>
>>    *: der
>> (*:@:(9.9999999999999995e_7 + ]) - *:) % 9.9999999999999995e_7"_
>>
>>    *: der 0 1 2 3 4
>> 1e_6 2 4 6 8
>>
>> These examples show that nouns and verbs in an "environment" can be
>> embedded in verbs and adverbs.  Adverbs can be embedded in adverbs and
>> verbs, as well (and verbs, adverbs and conjunctions can be taken indirectly
>> as arguments by verbs and adverbs; for instance, by wrapping them in
>> gerunds or boxed arrays).  Alas, user defined tacit conjunctions apparently
>> are not possible employing current official interpreters; one would have to
>> revert to official Golden Age interpreters or use alternative Unbox/Jx
>> interpreters.
>>
>> I cannot pretend that I am very familiar with the concept of closures; yet,
>> it seems to me that, the conventional way for producing closures in J would
>> be via explicit adverbs or conjunctions with nested (local) explicit verbs,
>> adverbs or conjunctions which could be deliver as products (of course,
>> these constructions are not supported, not yet anyway).  The methods I have
>> partially shown for producing fixed tacit verbs and adverbs are
>> unconventional (then again, J, tacit writing and wicked spells are
>> unconventional) but the effects seem to be the same or at least very
>> similar.
>>
>> Any enlightenment would be appreciated.
>>
>> References
>>
>> [0]  [Jchat] Interesting Critique of J
>>     http://www.jsoftware.com/pipermail/chat/2016-July/007002.html
>>
>> [1]  Tacit (unorthodox) version in task Closures/Value capture
>>
>>
>> http://rosettacode.org/wiki/Closures/Value_capture#Tacit_.28unorthodox.29_version
>>
>> [2]  First-class functions in Applications of Closure (computer
>> programming)
>>
>>
>> https://en.wikipedia.org/wiki/Closure_(computer_programming)#First-class_functions
>>
>> [3]  [Jprogramming] Tacit Toolkit (was dyadic J)
>>
>> http://www.jsoftware.com/pipermail/programming/2015-December/043757.html
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to