| # 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 from Chapter 03 the composition conjunction `@:` (at colon, called "At"). 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. From the first and second schemes it follows that if verb `g` is ambivalent, (that is, has both a monadic and dyadic case) then the composition `f @: g` is also ambivalent. For example, suppose `g` is 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|
    +------+
Suppose we write the definition of a function intended to be, say, monadic and monadic only. Then we need not be concerned if our definition turns out to be ambivalent, because we do not intend that dyadic cases should ever occur. Similarly, ambivalence is not a concern if we intend a dyadic function, and monadic cases will not occur) . 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).
`% *: 2` | `(% &: *:) 2` | `(*: 3) % (*: 2)` | `3 (% &: *:) 2`
    0.25
    0.25
    2.25
    2.25
## 8.5 More on Composition: Monad Tracking Monad There 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
`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`. 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
`g` | `g b. 0`
    sum"1
    1 1 1
Suppose that the monadic rank of `g` is `G`.
       G =: 0 { (g b. 0)
Then `(f @ g)` means `(f @: g)` applied separately to each `G`-cell, that is, `(f @: g)"G`.
`(f @ g) y` | `(f @: g)"G y`
    +-+-+
    |1|5|
    +-+-+
    +-+-+
    |1|5|
    +-+-+
and so the general scheme is:
             (f @ g) y    means     (f @: g) " 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 b. 0)` | `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 Recall 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 =: *:
`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 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 `G`-cell from `x` and a corresponding `G`-cell from `y`. To illustrate:
`G =: 0 { (g b. 0)` | `(g x)(f " (G,G))(g y)` | `x (f & g) y`
    0
    1  9
    4 16
    1  9
    4 16
## 8.8 Ambivalence Again The 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 =: *:
`f&g 1 2 3` | `f@g 1 2 3`
    +-+-+-+
    |1|4|9|
    +-+-+-+
    +-+-+-+
    |1|4|9|
    +-+-+-+
## 8.9 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.10 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. There is a built-in 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:
`sqrt =: *: ^: _1` | `sqrt 16`
    *:^:_1
    4
`^:` can automatically find inverses, not only of built-in verbs, but of user-defined verbs such as compositions. For example, the inverse of "twice the square-root of" is "the square of half of"
       foo    =: (2&*) @: %:
       fooINV =: foo ^: _1
`foo 16` | `fooINV 8` | `foo fooINV 36`
    8
    16
    36
## 8.11 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 ^: _1)` will be the inverse, multiplication by 10.
       g =: % & 10
`g 28` | `f g 28` | `(g ^:_1) f g 28` | `f &. g 28`
    2.8
    3
    30
    30
The general scheme is that
             (f &. g) y   means  (g ^: _1) f g y
This is the end of Chapter 8.

NEXT
Table of Contents
Index

Copyright © Roger Stokes 2003. This material may be freely reproduced, provided that this copyright notice is also reproduced.

last updated 14 Mar 2004