
Chapter 8: Composing VerbsThis chapter is concerned with operators which combine two verbs to produce new composite verbs.8.1 Composition of Monad and MonadRecall from Chapter 03 the composition conjunction @: (at colon, called "At"). Given verbs sum and square we can define a composite verb, sumofthesquares.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 DyadIf f is a monad and g is a dyad, then (f @: g) is a dyadic verb such thatx (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 MonadThe 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 CompositionsTo 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 may be useful if we are interested in writing an ambivalent definition, that is, with both a monadic and a dyadic case. Notice that from the first and second schemes it follows that if verb g is ambivalent then the composition f @: g is also ambivalent. For example, suppose g is the ambivalent builtin verb . with . y being the reverse of y and x . y being the rotation of y by x places.
From the third and fourth schemes above it follows that if verb f is ambivalent, then (f &: g) is ambivalent. For example, suppose that f is the verb % (reciprocal or divide). and g is *: (square).
8.5 More on Composition: Monad Tracking MonadThere is a conjunction @ (at, called "Atop"). It 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. Recall from Chapter 07 that a verb has in general three ranks, monadic, left and right, and for a verb f these ranks are yielded by the expression f b. 0. For example
Suppose that the monadic rank of g is G. G =: 0 { (g b. 0)Then (f @ g) means (f @: g) applied separately to each Gcell, that is, (f @: g)"G.
and so the general scheme is: (f @ g) y means (f @: g) " G y 8.6 Composition: Monad Tracking DyadNext we look at the composition (f @ g) for a dyadic g. Suppose f and g are defined by:f =: < g =: . " 0 1 NB. dyadicHere 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 Lcell from x and corresponding Rcell 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 MonadRecall that in Chapter 03 we met the conjunction & as a bonding operator. With one argument a noun and the other argument a dyadic verb the result is a monad. For example +&6 is a monad which adds 6 to its argument.If both arguments of & are verbs then & has a different interpretation. In this case it is is a composition operator, called "Compose". Now 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 give 1 4 , 9 16. By contrast, in (f & g) there are two separate applications of f, giving firstly 1,9 and secondly 4,16. 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 Gcell from x and a corresponding Gcell from y. To illustrate:
8.8 Ambivalence AgainThe composition f&g can be ambivalent. The dyadic case, x f&g y, we saw above. For the monadic case, f&g y means the same as f@g y.f =: < g =: *:
8.9 SummaryHere 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. 8.10 InversesThe "Square" verb, (*:), is said to be the inverse of the "Squareroot" verb (%:). The reciprocal verb is its own inverse.
Many verbs in J have inverses. There is a builtin conjunction ^: (caret colon, called "Power") such that the expression f ^: _1 is the inverse of verb f. (This is like writing f^{1} in conventional notation.) For example, the inverse of square is square root:
^: can automatically find inverses, not only of builtin verbs, but of userdefined verbs such as compositions. For example, the inverse of "twice the squareroot of" is "the square of half of" foo =: (2&*) @: %: fooINV =: foo ^: _1
8.11 Composition: Verb Under VerbWe 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, the square root of a number can be found by taking the logarithm, halving and taking the antilog, that is, halving under logarithm. Recall that halve is : and logarithm is ^.
The general scheme is that (f &. g) y means (g ^: _1) f g yThis is the end of Chapter 8. 
The examples in this chapter
were executed using J version j701/beta/20101124/22:45.
This chapter last updated 22 Dec 2010
Copyright © Roger Stokes 2010.
This material may be freely reproduced,
provided that this copyright notice is also reproduced.