Chapter 8: Composing Verbs
This chapter is concerned with operators which combine two verbs to produce new composite verbs.
8.1 Composition of Monad and Monad
Recall the composition operator @: from Chapter 03. Given verbs sum and square we can define a composite verb, sum-of-the-squares.
sum =: +/ square =: *:
The general scheme is that if f and g are monads then
(f @: g) y means f (g y)
Note in particular that f is applied to the whole result (g y). To illustrate, suppose g applies separately to each row of a table, so we have:
g =: sum " 1 f =: <
We have just seen the most basic of kind of composition. Now we look at some variations.
8.2 Composition: Monad And Dyad
If f is a monad and g is a dyad, then (f @: g) is a dyadic verb such that
x (f @: g) y means f (x g y)
For example, the sum of the product of two vectors x and y is called the "scalar product".
sp =: +/ @: *
The last example showed that, in the expression (x (f @: g) y) the verb f is applied once to the whole of (x g y)
8.3 Composition: Dyad And Monad
The conjunction &: (ampersand colon, called "Appose") will compose dyad f and monad g. The scheme is:
x (f &: g) y means (g x) f (g y)
For example, we can test whether two lists are equal in length, with the verb (= &: #)
eqlen =: = &: #
Here f is applied once to the whole of (g x) and (g y).
8.4 Ambivalent Compositions
To review, we have seen three different schemes for composition. These are:
(f @: g) y = f (g y) x (f @: g) y = f (x g y) x (f &: g) y = (g x) f (g y)
There is a fourth scheme,
(f &: g) y = f (g y)
which is, evidently, the same as the first. This apparent duplication is useful for the following reason. Suppose verb g is ambivalent, that is, has both a monadic and dyadic case. It follows from the first two schemes that the composition (f @: g) is also ambivalent. Similarly, if verb f is ambivalent, it follows from the third and fourth schemes that (f &: g) is ambivalent.
To illustrate, let g be the ambivalent built-in verb (|.) with (|. y) being the reverse of y and x |. y being the rotation of y by x places.
For an example of ambivalent (f &: g), let f be the verb % - reciprocal or divide.
8.5 More on Composition: Monad Tracking Monad
The conjunction @ is a variation of the @: conjunction. Here is an example to show the difference between (f @: g) and (f @ g).
y =: 2 2 $ 0 1 2 3
We see that with (f @: g) verb f is applied once. However, with (f@g), for each separate application of g there is a corresponding application of f. We could say that applications of f track the applications of g.
Suppose that the monadic rank of g is G. Then (f @ g) means (f @: g) applied separately to each G-cell, that is, (f @: g)"G.
RANKS =: 1 : 'x. b. 0'
and so the general scheme is:
(f @ g) y means (f @: g) " G y
There is also the & operator. For reasons of symmetry, as with the ambivalent functions mentioned above, (f&g) y means the same as (f@g) y.
8.6 Composition: Monad Tracking Dyad
Next we look at the composition (f @ g) for a dyadic g. Suppose f and g are defined by:
f =: < g =: |. " 0 1 NB. dyadic
Here x g y means: rotate vectors in y by corresponding scalars in x. For example:
Here now is an example to show the difference between f @: g and f @ g
We see that with (f @: g) verb f is applied once. With (f@g), for each separate application of g there is a corresponding application of f.
Suppose that the left and right ranks of dyad g are L and R. Then (f @ g) means (f @: g) applied separately to each pair of an L-cell from x and corresponding R-cell from y. That is, (f@g) means (f @: g)"G where G = L,R.
The scheme is:
x (f@g) y = x (f@:g) " G y
8.7 Composition: Dyad Tracking Monad
here we look at the composition (f & g) for dyadic f.
Suppose g is the "Square" function, and f is the "comma" function which joins two lists.
f =: , g =: *:
Here now is an example to show the difference between (f &: g) and (f & g)
We see that in (f &: g) the verb f is applied just once, to join the two lists of squares. By contrast, in (f & g) each separate pair of squares is combined with a separate application of f
The scheme is that
x (f & g) y means (g x) (f " G,G) (g y)
where G is the monadic rank of g. Here f is applied separately to each combination of a G-cell from x and a corresponding G-cell from y. To illustrate:
Here is a summary of the 8 cases we have looked at so far.
@: (f @: g) y = f (g y) @: x (f @: g) y = f (x g y) &: (f &: g) y = f (g y) &: x (f &: g) y = (g x) f (g y) @ (f @ g) y = (f @: g) " G y @ x (f @ g) y = x (f @: g) " LR y & (f & g) y = (f @: g) " G y & x (f & g) y = (g x) (f " (G,G)) (g y)
where G is the monadic rank of g and LR is the vector of left and right ranks of g.
The "Square" verb, (*:), is said to be the inverse of the "Square-root" verb (%:). The reciprocal verb is its own inverse.
Many verbs in J have inverses. The adverb (^: _1) produces the inverse verb of its argument verb. Let us call this adverb INV. INV produces "Square-root" from "Square":
INV can automatically find inverses, not only of built-in verbs, but of user-defined verbs such as compositions. For example, the inverse of (1 + the square-root) of y is (the square of 1 minus)y.
8.10 Composition: Verb Under Verb
We now look at composition with the conjunction &. (ampersand dot, called "Under"). The idea is that the composition "f Under g" means: apply g, then f, then the inverse of g.
For an example, suppose first that f is the verb which rounds a number to the nearest integer:
A number can be rounded to the nearest 10, say, by dividing by 10, rounding to nearest integer, then multiplying by 10 again.
Let g be division by 10, and then (g INV) will be the inverse, multiplication by 10.
g =: % & 10
The general scheme is that
(f &. g) y means (g INV) f g y
This is the end of Chapter 8.
Table of Contents
Copyright © Roger Stokes 2000. This material may be freely reproduced, provided that this copyright notice and provision is also reproduced.
last updated 10 March 00