Help / JforC / Empty Operands

From J Wiki
< Help(Redirected from Help/JforC/Empty Operands)
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help J for C Programmers


                                                                                                              13. Empty Operands

Every programmer knows that errors lurk near boundaries: for example, at the beginning and end of arrays, or at the point in execution where an array becomes empty.  Our discussion of verb rank omitted one important situation: suppose there are no cells?  An operand with no cells is said to be empty, and it gets special treatment in J.

The definition of an empty operand is not as obvious as you might think.  An array is empty if it has no atoms (i. e. has a 0 in its shape), but whether an operand is empty depends on the rank of its cells: it is empty if there is a 0 in the frame with respect to the cells operated on by the verb.  Consider an array with shape 3 0 4 .  This is an empty array, because it has no atoms.  As an operand of a verb with rank 0, it has frame 3 0 4, so there are no cells and the array is an empty operand.  As an operand of a verb with rank 1, it has frame 3 0 and again is empty.  As an operand of a verb with rank 2, though, it has frame 3 and is not empty: there are 3 cells, each of which has shape 0 4 .  In this case each cell is an empty array, but the operand is not empty.  As an operand of a verb with rank 3 or higher, the frame is empty and each cell has shape 3 0 4, so there is one cell and the operand is not empty, though each cell is an empty array.  You can see that an operand may have items and yet be an empty operand, if the verb operates on cells smaller than items, as is the case in this example for a verb of rank 0 or 1.

We are left with the question, What is executed when an operand is empty?  For something must be executed.  It is fundamental in J that if a verb produces a result with shape s when applied to a single cell, executing that verb over an array of that cell--even an array of none of them--produces an array of results each with shape s.  The only way to find out what shape a verb is going to produce is to execute it and see--and that is what J does.

Execution On a Cell Of Fills

If an operand is empty, i. e. its frame contains a 0 and it therefore has no cells, the verb is executed on a cell c of fills.  The shape of c is the shape of a cell of the corresponding operand, and the value of each atom is the appropriate fill for that operand: 0 for numeric operands, ' ' for characters, a: for boxes.  The shape s and type t of the result of executing the verb on c are noted.  Then, the shape of the overall result is the frame of the operand (the longer frame, for dyads) concatenated with s, and the result is given type t.  The result will necessarily be empty, because it will have a 0 in its shape (from the frame, which contained a 0).  Example:

   $ +/"2 (3 0 3 4 $ 100)
3 0 4

Remember that a cell has a shape, even if there are none of them!  Here the verb monad +/"2 is applied to 2-cells, each with shape 3 4 .  The frame 3 0 contains a 0, so the verb is executed on the fill cell c which is (3 4 $ 0).  Monad +/ adds the items of the cell, producing a list with shape .  The frame 3 0 is concatenated with 4 to give the shape of the result, 3 0 4 .

   3!:0 +/"2 (3 0 3 4 $ 100)
4

The verb 3!:0 (one of dozens of special goodies provided by the !: conjunction and documented under Foreigns) tells you the type of its operand.  Here, 4 means numeric type: the result has shape 3 0 4 and numeric type.

   $ <"2 (3 0 3 4 $ 100)
3 0
   3!:0 <"2 (3 0 3 4 $ 100)
32

Here the verb monad < was applied to the same fill-cell c (3 4 $ 0) but it produced as  result a scalar box (shape empty), so the shape of the overall result is the frame 3 0 concatenated with an empty list, i. e. shape 3 0 and type boxed (as indicated by the 32 returned by 3!:0).

Note that in an empty array of boxes, each box is itself empty (of course, the number of such boxes is 0, since the array is empty, but by now you know that empty nouns can still have a shape--the point here is that they cannot have nonempty contents).  The contents of a box are lost when the box becomes part of an empty array.  In the example <"2 (3 0 3 4 $ 100), execution on the fill-cell produced (<3 4 $ 0), and if the frame weren't empty the box would contain an array; but when the frame is empty, the value of the result is discarded; all that remains is its type.

If executing the verb on a cell of fills results in an error, execution continues as if the verb had returned the scalar :

   5 + ' '
|domain error
|   5    +' '

Trying to add 5 to a space is nonsense...

   5 + ''
   $ 5 + ''
0
   (3!:0) 5 + ''
4

...but adding 5 to an empty list of characters produces an empty numeric list.  The addition is attempted with a cell of fills for the empty operand (the values added are 5 on the left, ' ' on the right), the addition fails, and the error-fallback result of scalar 0 is used; the 0, being a scalar, has a shape which is an empty list; appending the longer frame (0) gives shape 0, numeric.  Note that y here is an empty operand, but it nonetheless has the longer frame (the scalar x has empty shape and perforce empty frame).

Note: there is much special code in the interpreter to handle the cases of empty operands.  To improve performance, the interpreter recognizes a great many combinations of verb, operand shape, and type, handling each with separate code.  In most cases the interpreter produces its result in accordance with the rules given above, but in a few exotic cases it deviates.  You are quite unlikely to encounter these cases in practice; the most important one is

   $ > 0$a:
0

where the rules given above would predict a shape of 0 0 .  Enough applications rely on shape 0 to keep this deviation in the system, at least as of J6.01.

Empty cells

As we discussed above, a cell, like any array, is called empty if it has a 0 in its shape.  Whether the cells of an operand are empty is independent of whether the operand itself is empty.

How a verb handles an empty cell is entirely up to the verb; the fill-cell processing we discussed above does not apply.  The J primitives generally preserve the type of empty lists that are 'data' but ignore the type of empty lists that are 'control information'.  So, even though characters are not allowable left operands of dyad |.,  |. i. 5 produces the same result as (0$0) |. i. 5, because the rotation count is 'control information'.  In contrast, 3 {.  produces a 3-character string, while 3 {. (0$0) produces a 3-item numeric list, because the right operand of dyad {. is 'data'.  The distinction between 'control information' and 'data' is not clear-cut, but in all cases the interpreter does what you'd want it to, and you should experiment if you need reassurance.

If Fill-Cells Are Not Enough

Sometimes executing a verb on a cell of fills simply won't do: maybe your verb produces a side effect, or maybe it will go berserk if its operand is .  In those cases, you must take steps to ensure that it is not executed on an empty list.  To help you out with the most common case, in which the only way a list can be empty is to have no items (that is another way of saying that the first item of the shape is 0), I offer you a set of adverbs and conjunctions which you can have by executing

load 'system\packages\misc\jforc.ijs'

u Ifany applies the verb u (which may be monadic or dyadic) provided y has items; if y has 0 items, the result of u Ifany is :

   $ (,/) i. 0 4
0
   $ (,/) Ifany i. 0 4
0 4

Since i. 0 4 has no items, Ifany caused it to be passed through unchanged.

x u Ifanyx y produces x u y if x has items, or y if x has no items.

The conjunction u Butifnull n can be monadic or dyadic; it applies u if y has items; if y has no items it produces a result of .

   5 + Butifnull 6 (0)
5
   5 + Butifnull 6 (0$0)
6

x u Butifxnull n y produces x u y if x has items, or n if x has no items.


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help J for C Programmers