-
Notifications
You must be signed in to change notification settings - Fork 108
[feature request] first class verbs #116
Description
The idea of "first class verbs" or more commonly "first class functions" is a general principle that gives a language the following features:
- closures (may be used as classes or namespaces in OOP, analogous to locales in J)
- higher order functions (adverbs and conjunctions in J)
- ability to pass functions around (gerunds in J)
- ability to define new control structures in the language itself
Today some of the features of first class verbs already exist in J, but have limitations and require verbose syntax.
I'll put some examples below to show how this could look in future J code.
accept verbs as parameters
This is awkward in J, as it requires first turning the verb into a gerund with ` (tie), and then using `: (evoke gerund) to turn it back into a verb:
({{2<y}}`'') {{ ((x`:6) y) # y }} i.9
3 4 5 6 7 8First class verbs simplifies the above example to this:
NB. proposed change
{{2<y}} {{ (x y) # y }} i.9
3 4 5 6 7 8NOTE: The phrase {{ x y }} is currently a syntax error, so adding this feature would not break any existing code.
return verbs as values
This feature is often called a "closure" in other programming languages, because the inner scope "closes over" (inheriits) the variables from the outer scope.
Similar to the previous example, now returning a verb from another verb:
f1 =: {{
s =. y
({{ s + y }}`'') }}
((f1 3)`:6) 10
13However, I think gerunds may be buggy (perhaps only inside{{}}), because executing it with different arguments gives an unchanging result:
NB. buggy behavior
((f1 3)`:6) 1
4
((f1 99)`:6) 1
4The above should work like this instead:
NB. correct behavior
((f1 3)`:6) 1
4
((f1 99)`:6) 1
100Proposed enhancement: returning a verb and using that verb can work like this:
NB. proposed change
f1 =: {{
s =. y
{{ s + y }} }}
(f1 3) 10
13
(f1 4) 10
14Currently, the explicit definition above is not a syntax error, but running (f1 3) is an error:
|noun result was required: f1
| {{s + y }}
I don't think adding this feature (return verbs from verbs) would break any code, but I'm not sure.
rationale
- First class verbs are a more flexible alternative to
`:, because they allow users to define their own composition patterns (currentlym`:naccepts0,3or6as values ofn, with 3 hard-coded behaviors). - Treating verbs as regular values removes the verb/adverb/conjunction distinction. Many adverbs and conjunctions can be written using only first class verbs. This might make J easier to learn.
- First class verbs can emulate locales, but with lexical scope (which arguably makes programs easier to debug). For example, it is possible to emulate OOP-style classes with mutable closures.
- Non-breaking: This proposal does not explicitly add any syntax to J, but rather gives meaning to syntax which is currently an error.