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 =: *:

sumsq =: sum @: square sumsq 3 4
sum@:square
25

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 =: <

y =: 2 2 $ 1 2 3 4 g y f g y (f @: g) y
1 2 
3 4
3 7
+---+ 
|3 7| 
+---+
+---+ 
|3 7| 
+---+

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 =: +/ @: *

x =: 1 2 y =: 2 3 x * y +/(x * y) x sp y
1 2
2 3
2 6
8
8

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 =: = &: #

x y #x #y (#x) = (#y) x eqlen y
1 2
2 3
2
2
1
1

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.

y =: 'abcdef' (< @: |.) y 1 (< @: |.) y
abcdef
+------+ 
|fedcba| 
+------+
+------+ 
|bcdefa| 
+------+

For an example of ambivalent (f &: g), let f be the verb % - reciprocal or divide.

% *: 2 % &: *: 2 (*: 3)%(*: 2) 3 % &: *: 2
0.25
0.25
2.25
2.25

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

y f g (f @: g) y (f @ g) y
0 1 
2 3
<
sum"1
+---+ 
|1 5| 
+---+
+-+-+ 
|1|5| 
+-+-+

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'

G =: 0 { g RANKS (f @ g) y (f @: g)"G y
1
+-+-+ 
|1|5| 
+-+-+
+-+-+ 
|1|5| 
+-+-+

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:

x=: 1 2 y=: 2 3 $ 'abcdef' x g y
1 2
abc 
def
bca 
fde

Here now is an example to show the difference between f @: g and f @ g

f (x g y) x (f @: g) y x (f @ g) y
+---+ 
|bca| 
|fde| 
+---+
+---+ 
|bca| 
|fde| 
+---+
+---+---+ 
|bca|fde| 
+---+---+

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.

G =: 1 2 { g RANKS x (f @:g)" G y x (f @ g) y
0 1
+---+---+ 
|bca|fde| 
+---+---+
+---+---+ 
|bca|fde| 
+---+---+

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 =: *: 

x =: 1 2 y =: 3 4 g x g y
1 2
3 4
1 4
9 16

Here now is an example to show the difference between (f &: g) and (f & g)

(g x) f (g y) x (f &: g) y x (f & g) y
1 4 9 16
1 4 9 16
1  9 
4 16

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:

G =: 0 { g RANKS (g x)(f " (G,G))(g y) x (f & g) y
0
1  9 
4 16
1  9 
4 16

8.8 Summary

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.

8.9 Inverses

The "Square" verb, (*:), is said to be the inverse of the "Square-root" verb (%:). The reciprocal verb is its own inverse.

*: 2 %: 4 % 4 % 0.25
4
2
0.25
4

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 =: ^: _1 %: 16 *: INV 16
^:_1
4
4

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.

foo =: (1&+) @: %: foo 16 foo INV 5
(1&+)@:%:
5
16

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:

f =: <. @ (0.5 & +) f 1.2 1.8
<.@((0.5)&+)
1 2

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

g 28 f g 28 (g INV) f g 28 f &. g 28
2.8
3
30
30

The general scheme is that

         (f &. g) y   means  (g INV) f g y

This is the end of Chapter 8.


NEXT
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