Vocabulary/Agreement

From J Wiki
Jump to: navigation, search

Back to: Vocabulary

How Argument Cells Are Matched For Dyads

Dyads split their arguments into cells whose ranks are given by the ranks of the verb. Each cell of x is matched with a cell of y. Usually this is straightforward because these arrays have the same shape:

   100 200 300 + 4 5 6
104 205 306

both arguments are lists.

   polys =. 2 3 $ 0 0 1  1 0 2   NB. Two polynomials, x^2 and 1 + 2*x^2
   polys p. 0 1
0 3

Because the rank of p. is 1 0, the cells of x are lists, while the cells of y are atoms. There are 2 of each.

Another well-known case is when one argument has only one cell, which is applied to each cell of the other argument:

   100 + 1 2 3
101 102 103

Matching of argument cells can be more complex, for example:

   100 200 + i. 2 3
100 101 102
203 204 205

The details of this are what we are concerned with here.

x and y Are Split Into Cells

Each argument is initially viewed as an array of cells where the rank of each cell is the corresponding rank of the verb. If s is the shape of the argument, and r is the rank of the verb, this array of r-cells has the shape (-r)}.s. This shape is called the frame of the argument.

Examples:

  100 200 + i. 2 3
100 101 102
203 204 205
Argument Shape Rank Frame
x ,2 0 ,2
y 2 3 0 2 3

x is treated as a list of 2 zero-cells (atoms) and y is treated as a 2x3 table of atoms.

   24 60 60 #: 1800 7200  NB. convert seconds to hr,min,sec
0 30 0
2  0 0
Argument Shape Rank Frame
x ,3 1 empty
y ,2 0 ,2

x is treated as a single 1-cell and y is treated as a list of 2 atoms.

Agreement: Macrocells Of x and y Are Matched

The frames of x and y must start identically, and must match identically for the entire length of the shorter frame. If they don't, the result is a length error which is signaled before the verb is executed on any cells.

The identical part of the frames is called the common frame. The surplus part of the longer frame is called the surplus frame. Either or both of these may be empty.

Next the common frame is used to reinterpret the arguments as arrays of macrocells. The macrocells may have different rank, but each macrocell in one argument will match with one corresponding macrocell in the other argument.

The macrocells for the argument with the shorter frame will be cells of that argument, while the macrocells of the operand with longer frame will be arrays of cells. If the frames are identical, the macrocells are simply the cells of the arguments. Examples:

  100 200 + i. 2 3
100 101 102
203 204 205
Argument Shape Rank Frame Common Frame Surplus Frame Shape Of Macrocell Macrocells
x ,2 0 ,2 ,2 empty empty 100 and 200
y 2 3 0 2 3 ,3 ,3 0 1 2 and 3 4 5
   24 60 60 #: 1800 7200  NB. convert seconds to hr,min,sec
0 30 0
2  0 0
Argument Shape Rank Frame Common Frame Surplus Frame Shape Of Macrocell Macrocells
x ,2 1 empty empty empty ,3 24 60 60
y ,2 0 ,2 ,2 ,2 1800 7200

Cells Are Matched Within Macrocells

After the macrocells are identified and matched up, the surplus frame must be accounted for. If there is no surplus frame, each macrocell contains a single argument cell, and those cells are matched.

Otherwise, within each pair of matched macrocells, one macrocell contain a single argument cell and the other contains an array of argument cells. The single cell is matched with each argument cell in the array.

Examples:

  100 200 + i. 2 3
100 101 102
203 204 205
Argument Shape Rank Frame Common Frame Surplus Frame Shape Of Macrocell Macrocells Matched Cells
x ,2 0 ,2 ,2 empty empty 100 and 200 (100,0), (100,1), (100,2), (200,3), (200,4), (200,5)
y 2 3 0 2 3 ,3 ,3 0 1 2 and 3 4 5
   24 60 60 #: 1800 7200  NB. convert seconds to hr,min,sec
0 30 0
2  0 0
Argument Shape Rank Frame Common Frame Surplus Frame Shape Of Macrocell Macrocells Matched Cells
x ,2 1 empty empty empty ,3 24 60 60 (24 60 60,1800), (24 60 60,7200)
y ,2 0 ,2 ,2 ,2 1800 7200

The Verb Is Executed And The Result Collected

Finally each argument cell has been matched with a cell in the other argument. Now:

  • The verb is now executed on each matched pair.
  • The results from individual cells are brought to a common rank and shape by adding framing fill if necessary.
  • The filled results for each macrocell are organized into an array, using the surplus frame.
  • The filled results of the macrocells are organized into an array, using the common frame.

Using Rank To Control Agreement

Verb rank interacts with agreement to allow exquisite control of the order of computation.

The agreement mechanism matches the operands by examining the frames starting at the left. Raising the verb rank shortens the frames, thus altering the matching of cells. The mechanisms work effectively together.

Suppose you want to add one list to each list of a table:

   1 2 3 + i. 2 3
|length error
|   1 2 3    +i.2 3

The arguments do not agree, since the rank of the verb is 0 0 and the frames are therefore 3 and 2 3 and lack a common prefix.

   1 2 3 +"1 i. 2 3
1 3 5
4 6 8

Now the frames are empty and 2, which agree.

You may have to use rank more than once to get the result you want. In a case like <@,"0"0 1 below, the agreement rules are used to match cells using rank 0 1, and then within those matched cells, the agreement rules are used again to match cells using rank 0. In fact the agreement rules are used a third time to match cells for the inner verb <@,, but this verb has infinite rank so the matching is trivial.

   1 2 3 , 4 5 6
1 2 3 4 5 6
   1 2 3 ,"0 (4 5 6)
1 4
2 5
3 6
   1 2 3 <@,"0 (4 5 6)
+---+---+---+
|1 4|2 5|3 6|
+---+---+---+
   1 2 3 <@,"0 1 (4 5 6)
+-------+-------+-------+
|1 4 5 6|2 4 5 6|3 4 5 6|
+-------+-------+-------+
   1 2 3 <@,"0"0 1 (4 5 6)
+---+---+---+
|1 4|1 5|1 6|
+---+---+---+
|2 4|2 5|2 6|
+---+---+---+
|3 4|3 5|3 6|
+---+---+---+