Chapter 13: Operators Tacitly Defined
J provides built-in operators such as the composition-conjunction @. In the previous chapter we looked at defining our own operators explicitly. In this chapter we look at defining operators tacitly.
It will be helpful to have a few verbs for making examples. Verbs f, g and h are meant to be trivial functions where, by looking at their results, it is easy to track how they were applied.
f =: 'f' & , g =: 'g' & , h =: 'h' & ,
13.2 A Tacit Adverb
Recall from Chapter 07 the Rank conjunction, ("). For example, the verb (< " 0) applies Box (<) to each rank-0 (scalar) item of the argument.
A tacit adverb to apply a given verb to each scalar can be written as (" 0)
The scheme is, that for a conjunction C and a noun N, the expression (C N) denotes an adverb such that:
x (C N) means x C N
Here is another example. Recall from Chapter 10 the Power conjunction (^:) which applies a verb repeatedly, so that ( f ^: 3 y) means f f f y. An adverb which applies a verb twice can be written as ^: 2
The tacit adverb (" 0) is patterned as a conjunction followed by a noun. If we write V for a verb, N for a noun, A for an adverb and C for a conjunction, then this pattern, conjunction-followed-by-noun, can be called pattern CN.
The J language attaches meaning to many different patterns. For example, the meaning of an expression of pattern VN is: the application of a verb to a noun argument. The meaning of pattern VVV is a fork of three verbs.
In this chapter we will look at further patterns useful for defining operators. We have met our first example using pattern CN above to define an adverb. The patterns fall into groups, so we look at each group in turn. Patterns of two objects are called bidents and patterns of three are called tridents.
13.4 Adverbs From Bonded Conjunctions
We have already met the bident CN. Recall its scheme, which is: if C is a conjunction, N a noun, then the bident (C N) denotes an adverb such that for an argument x
x (C N) means x C N
Altogether there are four similar schemes
Here is another example, a bident of pattern VC.
A =: h @ NB. verb conjunction
These patterns are a form of bonding (or currying) whereby we take a two-argument function and fix the value of one of its arguments to get a one-argument function
However, there is a difference between bonding a dyadic verb (as in + & 2 for example) and bonding a conjunction. With the conjunction, there is no need for a bonding operator such as &. We just write (" 0) with no intervening operator. The reason is that in the case of + & 2, omitting the & would give + 2 which means: apply the monadic case of + to 2, giving 2. However, conjunctions don't have monadic cases, so the bident (" 0) is recognised as a bonding.
13.5 Compositions of Adverbs
If A0 and A1 are adverbs, then the bident (A0 A1) denotes an adverb which applies A0 and then A1
A0 =: f @ NB. adverb (bident VC) A1 =: @ g NB. adverb (bident CV)
and similarly for a trident of 3 adverbs, A0 A1 and A2. Thus we have the two schemes:
13.6 Hook-Making Adverb
If A is an adverb, V a verb, then the bident (A V) denotes an adverb. This adverb, given an argument x generates a verb in the form of the hook (x A) V. For example:
The scheme is:
13.7 Fork-Making Adverb
If A is an adverb, V1 and V2 are verbs, then the trident (A V1 V2) denotes an adverb which generates a fork. For example:
avv =: / % # NB. adv verb verb
The scheme for trident AVV is:
13.8 Identity Operators
It will be useful at this point to introduce identity-operators for the sake of the examples which follow. The built-in adverb ]: is the identity-adverb. Its argument can be a noun or verb. Its result is identical to its argument.
The built-in conjunction [., called "Lev", is the left-identity conjunction. Its arguments can be nouns or verbs. Its result is identical to its left argument. Similarly, for the conjunction ]., called "Dex", its result is identical to its right argument.
Now we return to our theme of patterns defining operators.
13.9 Fork-making Conjunctions
Now we look at patterns denoting conjunctions which generate forks.
Suppose that C0,C1, ... are conjunctions, V0,V1, ... are verbs, and A0 ,A1 ... are adverbs.
Then the trident (C0 V1 C2) for example, denotes a conjunction according to the scheme for the pattern CVC below. We have schemes for these patterns:
Here is a derivation of the last result:
foo 'a' faga (f cvc g) 'a' NB. by defn of foo faga (f ([. , ].) g) 'a' NB. by defn of cvc faga ((f [. g) , (f ]. g)) 'a' NB. by trident CVC faga (f , g) 'a' NB. by defn of [. and ]. faga (f 'a') , (g 'a') NB. f , g is a fork faga ('fa' , 'ga') NB. by defn of f and g faga
13.10 More Patterns
There are schemes for further patterns, as shown in the following table.
This is the end of Chapter 13.
Table of Contents
Copyright © Roger Stokes 2002. This material may be freely reproduced, provided that this copyright notice is also reproduced.
last updated 16 Mar 2002