The simplest verbs are the primitives, such as +. Larger verbs are created by connecting other verbs together.
When Is a Verb Executed?
Execution of a sentence proceeds from right to left.
This is a simplified distillation of J's parsing rules, which control the order of execution. Modifiers are executed before verbs, and usually produce verbs. For example,
2 + *: 3 4 5 11 18 27
First *: 3 4 5 is executed, since *: has a verb to its left, producing 9 16 25; then 2 + 9 16 25 is executed.
2 & + 1 2 3 3 4 5
+ 1 2 3 is not executed. + has the conjunction & to its left. 2&+ is executed, producing a derived verb, and this derived verb is executed on 1 2 3.
How Is a Verb Executed?
The first question when a verb is executed is: how is it executed? There are two ways:
- If the verb has a noun to its left, the verb is executed dyadically: the noun to the left and the noun to the right of the verb become the x and y arguments to the verb.
- If the verb does not have a noun to its left, the verb is executed monadically, and the noun to the right of the verb becomes the y argument.
The type of invocation is called the valence of the invocation: either monadic or dyadic.
The definition of the verb contains completely separate definitions to handle the two valences. These definitions are completely unrelated, like multiple personalities that inhabit the same body.
If a verb is invoked using a valence that was omitted in the verb definition, domain error is signaled.
Figuring Out The Valence
Determining the valence of an invocation can be tricky. Because modifiers have execution priority over verbs, it is not sufficient to check whether the word to the left of the verb is a noun. You must look one word farther than that, to see if the word to the left of the noun is modified by a conjunction. Only then can you tell whether the invocation of the verb is monadic or dyadic.
3 %: 64 729 4 9
The dyad %: was executed, taking the cube root.
3 + %: 64 729 11 30
The monad %: was executed, taking the square root. Then 3 was added.
+ & 3 %: 64 729 11 30
Order of Evaluation
This is an oversimplification of the parsing rules, but not much of one.
The resulting sequence of verbs is executed right to left with no deviation. All verbs have the same priority.
If the lack of an operator-precedence table seems odd to you, consider that J has about 100 verbs, and that user-written verbs are treated exactly the same as primitives. What would an operator-precedence table look like, and how long would it take to learn it?
You do have to remember that the rules of standard math notation do not apply:
3 * 4 + 1 15
- The rank of the monad is a single number
- The rank of the dyad is two numbers, one for each argument, x and y.
- Atom 0 - the monad rank (i.e. of the y-argument)
- Atom 1 - the left dyad rank (i.e. of the x-argument)
- Atom 2 - the right dyad rank (i.e. of the y-argument).
For example, if the verb rank of the monad v is k, J computes (v y) as follows:
If the rank of the argument is less than or equal to the verb rank k, the verb is applied to the entire argument.
It follows that to specify a verb rank of Infinity (_) guarantees that the corresponding k-cell is the entire argument (x or y), however large its rank happens to be.
The Value of a Verb
The value of a verb is the code needed to recreate the verb.
NB. display the definition of verb: jhostpath_j_ jhostpath_j_ '\'&(I.@(e.&'/\')@]})
- to execute a verb monadically you must have a noun to the right of it, i.e. you must give it a y-argument
- to execute a verb dyadically you must give it both an x-argument and a y-argument.
Creating A Verb
Create a verb in one of two ways:
- An anonymous verb is created when a modifier is executed.
- A named verb is created when a verb value is assigned to a name.
It is not necessary to name a verb. You can create an anonymous verb, execute it right away, and discard it.
2&+ NB. create an anonymous verb. J returns the verb's value, which it displays 2&+ 2&+ 3 4 5 NB. create an anonymous verb and execute it 5 6 7 plustwo =: 2&+ NB. create a verb, then save it as a named verb plustwo 3 4 5 NB. execute the named verb 5 6 7 plustwo NB. For the named verb, show its value (--a verb) 2&+
The (:) modifier, in its special 3 : 0 and 4 : 0 forms, creates an explicitly defined anonymous verb. The 0 part tells the interpreter to stop executing input and instead read lines until a line containing just a right parenthesis:
The lines between the : 0 line and the ) delimiter are the body of the anonymous verb. They are saved away as part of the anonymous verb. When the verb is executed, the lines of the body are executed, one by one.
mverb =: 3 : 0 NB. monad only smoutput 'mverb executed!' ) dverb =: 4 : 0 NB. dyad only smoutput 'dverb executed!' ) bverb =: 3 : 0 NB. Dual-valence, i. e. both valences smoutput 'monadic bverb executed!' 0 bverb 0 : smoutput 'dyadic bverb executed!' ) mverb '' mverb executed! 0 dverb '' dverb executed! dverb '' NB. dverb has no monadic valence |domain error: dverb | dverb'' bverb 0 monadic bverb executed! dyadic bverb executed!
You may use the synonyms monad define or verb define instead of 3 : 0, and dyad define instead of 4 : 0.
Executing an Explicit Definition: Private Namespaces
Before the body is executed, a new private namespace is allocated for the execution. The private namespace is visible only to the sentences of the executing body (not their descendants), and holds those names that have been assigned by private assignment by sentences in the body. Names defined in the private namespace hide any nouns of the same name that have been globally defined. The private namespace and its nouns disappear when the explicit definition terminates.
Private assignment uses the assignment word =. . Public assignment uses the assignment word =: and produces a name that is available to any verb.
Before the body starts executing, the interpreter privately assigns the name y with the value of the right operand of the invocation of the verb. If the invocation is dyadic, the interpreter privately assigns the name x with the value of the left operand.
The Result Of A Verb
It's easy to violate this requirement by accident. A puzzling syntax error then arises. See: Vocabulary/ErrorMessages#syntaxresult.