Vocabulary/Modifiers

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Modifiers

A Modifier is either an Adverb or a Conjunction.

Modifiers take one or two operands, which can be nouns or verbs, and produce a derived verb.

Actually, a modifier may produce any part of speech, but almost all modifiers produce verbs.

An adverb takes one operand, which is the verb or noun to its left.

A conjunction takes two operands: the verb or noun on each side.

The left-hand operand to a modifier is called u (or m if it is known to be a noun). The right-hand operand to a conjunction is called v (or n if it is known to be a noun).

NB: Take careful note of the terminology: modifiers take operands, verbs take arguments.


Grouping And Order Of Execution

Modifiers grab the entire verb-phrase/noun-phrase on the left for use as u. Conjunctions also take the single verb/noun to the right for v.

A verb or noun phrase starts with a verb or noun that does not have a conjunction immediately to its left.

sentence:
   2 * %: @ + / @: *: 1 2 3
start-points of phrases:
   2 * %: @ + / @: *: 1 2 3
   | | |              |
parenthesized version:
   2 * (((%: @ +)/)@:*:) 1 2 3

sentence:
   2 * %: @ (+ /) @: *: 1 2 3
start-points of phrases:
   2 * %: @ (+ /) @: *: 1 2 3
   | | |     |          |
parenthesized version:
   2 * ((%: @ (+/))@:*:) 1 2 3


Modifier Execution: Creating The Derived Verb

Consider the sentence

   +/ 1 2 3
6

Two executions were performed. First, the adverb / was executed, with its u set to +, to produce an anonymous derived verb. Then, the anonymous verb was executed on the noun argument 1 2 3 to produce the result 6.


Executing A Modifier Produces A Derived Verb

We can understand what's going on if we split the process in two. We will first execute the adverb / to produce a derived verb, but we will give that verb a name. Then we will use the named verb:

   SumItems =: +/
   SumItems 1 2 3
6

The modifier / was executed when +/ was encountered. This produced a derived verb. Then this verb value was assigned to the name SumItems.

In the second sentence, SumItems calls up this verb value for execution.

Strictly speaking, executing the sentence

   SumItems 1 2 3

does not execute any modifiers. / was executed when SumItems was defined.

The derived verb is guaranteed to execute according to the definition of the modifier. For example, monadic +/ produces a derived verb that adds all the items of y. Many combinations of verbs and modifiers are recognized and implemented with customized derived verbs.


Displaying The Derived Verb

As with all names, you can display the value of a derived verb by typing the name as its own sentence:

   SumItems
+/

The display doesn't tell whether the verb is supported by special code. It is simply a description of what the verb will do. Verbs with the same display can have difference performance.


Executing The Derived Verb

The derived verb is executed like any other verb. It has a verb rank for each argument, which controls how the argument is split into cells. The rank of the derived verb is part of the description of a modifier. Sometimes the rank depends on the operands of the modifier.


User-Defined Modifiers

The : conjunction can create modifiers as well as verbs. 1 : 0 will create an adverb, and 2 : 0 will create a conjunction. Assign the resulting derived modifier to a name so you can use it later.

Executing the derived modifier will produce a derived verb. The u (and v, for a conjunction), and the body of the derived modifier, will be saved as the derived verb.

When the derived verb is executed, the names u (and v, for a conjunction) are set with the values that were saved when the derived modifier was executed. If a u and v is a noun, the name m or n is also set. y (and x for a dyadic verb) are also set, and then the body of the derived verb is executed sentence by sentence.

   NB. Adverb.  u is applied to y and creates a Boolean list of elements to keep.
   NB. Result is the culled y
   usedtocull =: 1 : 0
(u y) # y
)
   NB. verb to cull numbers less than 0
   cullminus =: >:&0 usedtocull
   cullminus _2 _1 0 1 2
0 1 2
   >:&0 usedtocull _2 _1 0 1 2
0 1 2

There are three executions here. Executing : creates a derived adverb, which is assigned to the name usedtocull.

Then usedtocull is given the left operand >:&0. This produces a derived verb, which contains the body of usedtocull, and the saved u value of >:&0.

Finally, the derived verb is executed on a noun argument.