User:Dan Bron/Snippets/DOOG

From J Wiki
Jump to: navigation, search

Distribute Operators Over Gerunds

definition

Given the formal name of a primitive modifier (adverb or conjunction), doog will derive an operator of the same class. The derived operator is related to the one named, except that it is extended to gerunds.

Much in the same way that rank abstracts the operation of verbs on nouns, so doog abstracts the operation of modifiers on verbs. Both rank and doog modify their argument such that the derived entity properly handles arrays of arguments.

Howerver, whereas arrays of nouns are just nouns, arrays of verbs are gerunds. Therefore doog extends primitive modifiers (such as @), to operate on gerunds. The arguments to a doog-derived modifier may be gerunds, and the result is always a gerund.

In the case that that the arguments to a doog-derived modifier are not gerunds, then the modifier acts just like its named counterpart (except that it produces a gerund; the atomic representation of the result of its counterpart).

Otherwise, doog applies the modifier to or between each verb or pair of corresponding verbs in the in the gerund(s). In the case of a derived conj with only one gerundal argument, the other argument may be a verb or a noun, with the expected results.

Gerunds are distinguised from other nouns in that each of their atoms is a valid atomic representation.

motivation

This adverb emulates the undocumented, pre-J601c behavior of compositions with gerund:

   At     =:  '@'  doog
   Atop   =:  '@:' doog

   1:    At  3:      -:   {. 1:@3:`[
1
   1:`2: At  3:      -:      1:@3:`(2:@3:)
1
   1:    At (3:`4:)  -:      1:@3:`(1:@4:)
1
   1:`2: At (3:`4:)  -:      1:@3:`(2:@4:)
1

   First  =:  Atop  {.
   Across =:  First (;.1)~ (`1:) (`:6)

The adverb Across provides a mechanism to apply succesive verbs (a gerund) to a corresponding list of nouns (the gerund will be extended cyclically if required). Silly example:

   +:`-:`*:`%: Across  i. 4*3
0 0.5 4 1.73205 8 2.5 36 2.64575 16 4.5 100 3.31662

extensions

Morever, doog extends this behavior (in two ways). Whereas the J implementation restricted compositions with gerunds to the forms (m@v u@n m@n), (m@:v u@:n m@:n), (m&:v u&:n m&:n), and m&n, doog can distribute any primitive conjunction. In particular, '&.' doog will probably find many uses. Silly example:

   Each         =:  '&.' doog >
   AcrossBoxed  =:  Each Across
   +:`-:`*:`%: AcrossBoxed  (1+i.4)  *&.>   <i.3 3
+--------+-----+-----------+----------------------+
| 0  2  4|0 1 2|  0   9  36|      0      2 2.82843|
| 6  8 10|3 4 5| 81 144 225| 3.4641      4 4.47214|
|12 14 16|6 7 8|324 441 576|4.89898 5.2915 5.65685|
+--------+-----+-----------+----------------------+

The second extension doog provides is that it can distribute primitive adverbs as well. Silly example:

   Over         =: '/' doog
   +`* Over Across i. 4 10x
45 335221286400 245 2306992893004800

which gives the sums of the even rows and the products of the odd rows. We can also use this feature to implement a dyadic Across:

   Every        =: '&:' doog >
   AcrossDyad   =:  Over Every Across ( @:(,&:<"_1) )

   1 10 *`^ AcrossDyad   5 3
5 1000

(though there are probably better or faster ways to achieve this).

download

All the definitions in this paper are available in the DOOG script

references

Please see the Distribute composition over gerunds thread in the Forums.