Vocabulary/EmptyArguments

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Empty Arguments

Empty Arrays vs. Empty Arguments

These are different conditions.

The cells of an argument depend on the rank of the verb that is applied.

Consider the array (e =. 3 0 4 $ 0) . This array has no atoms, so it is an empty array. But it has 3 items, each with shape (0 4) . So if a verb with rank 2 is applied to e, it will be applied to 3 cells (each of which is empty). e is not empty with respect to 2-cells.

Contrast this with applying a verb with rank 1 to e. The 1-cells of e have shape (,4), but there are none of them: the frame of e with respect to 1-cells is (3 0) . e is empty with respect to 1-cells.

What is executed when an argument 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 be sure what shape a verb is going to produce is to execute it and see. That is what J does.


Fill Cells

If an argument 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.

If the verb is of the form <@f y, <@:f y, or [x] f&.> y, which always returns an atomic box, the verb is not executed at all. This also applies to equivalent forms using & instead of @.

The result will necessarily be empty, because it will have a 0 in its shape (from the frame, which contained a 0).

   $ +/"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 (,4) . 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 tells you the type and precision of its argument. Here, 4 means integer precision: the result has shape (3 0 4) and integer precision.

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

Here the verb (<) was applied to the same fill-cell c (3 4 $ 0) but it produced as its 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).


Error During Execution On A Cell Of Fills

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

   5 + ' '  NB. Trying to add 5 to a space is nonsense...
|domain error
|   5    +' '
   5 + ''  NB. ...but adding 5 to an empty list of characters produces an empty integer list.

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

the values added are 5 on the left, ' ' on the right.

Note that y here is an empty argument, but nonetheless its frame, (,0), is the longer frame. The scalar x has empty shape and therefore empty frame.

Exceptions - Integrated Rank Support

When a general verb u or a compound u"r is executed on an argument whose rank is higher than the rank of u, the interpreter breaks the argument into cells and runs u on each cell. Since starting a verb takes a bit of time, this becomes very inefficient for a verb like + which has rank 0 and merely adds two numbers.

To solve this problem, most J primitive verbs, and some compounds, include integrated rank support (IRS), which means that the verb itself takes the responsibility for handling arguments of any rank. IRS applies only to primitives, not to user-written explicit verbs.

In some cases the type and precision of the results of verbs with IRS do not match the results that would be predicted from the fill-cell rules given above.

   3!:0 ext =. 1x   NB. An atom, extended-integer precision
64
   3!:0 emptyreal =. 0 $ 1.1  NB. An empty list, real precision
8
   plus =: dyad : 'x + y'"0  NB. A verb without IRS
   3!:0 ext plus emptyreal  NB. fill-cell is real, real+ext=real, so result real
8
   3!:0 ext + emptyreal  NB. IRS gives extended result
64
   3!:0 ext +"0 emptyreal  NB. +"0 has IRS, also extended reault
64
   3!:0 ext +"0"0 emptyreal  NB. +"0"0 does not have IRS, so it follows the fill-cell rules
8

Observe that the verbs plus, +, +"0, and +"0"0 all have the same rank and do the same thing.

Verbs With Integrated Rank Support
Monads Dyads
< <. <: >. >: + +. +: +/%# * *. *: - -. -: % %: ^ ^. ~: | |. |: , ,. ,: #. ! / /: /:@/: \ \. \: [ ] {. {: }. }: @. ? j. o. p. p: q: r. t. x: A. H. = < <. <: > >. >: + +. +: * *. *: - -.@-: -: % ^ $ ($,) ~: | |. |: , ,: # ! /: \: [ ] { {. }. (m b.) e. i. i: o.

Arguments With Empty Cells

As noted above, a cell, like any array, is called empty if it has a 0 in its shape.

Whether the cells of an argument are empty is independent of whether the argument 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 x-argument of (|.), the phrase  '' |. 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, and:  3 {. (0$0) produces (0 0 0), because the x-argument of ({.) is data.

Many modifiers have infinite rank and produce verbs that operate on cells (often items) of an argument. When there are no cells to work on, these modifiers produce results that follow the pattern established by nonempty arguments.

   $ 3 ]\ 1 2 3 4  NB. 2 infixes of 3 items
2 3
   $ 3 ]\ 1 2 3  NB. 1 infix of 3 items
1 3
   $ 3 ]\ 1 2   NB. 0 such infixes
0 3
   $ 3 ]\ 1
0 3
   $ 3 ]\ 0$0
0 3

Empty Arrays Have No Values

An empty array has shape, type and precision but no values.

Even if all the atoms of an array have the same value, or even if they are all boxes with contents, the values and the contents are lost whenever they are made into an empty array.

   ]struct =. <"0 ;:'Piltdown Man'   NB. two boxes, each containing a box
+----------+-----+
|+--------+|+---+|
||Piltdown|||Man||
|+--------+|+---+|
+----------+-----+
   ]struct =. 1 }. struct            NB. discard the first one
+-----+
|+---+|
||Man||
|+---+|
+-----+
   ]struct =. 1 }. struct            NB. discard the other one too.  Now empty

   ]struct =. 1 {. struct            NB. Bring back a box, but it doesn't have the structure
++
||
++

Types produced by empty arguments

Precision Produced When Argument Of Monad Is Empty

 

Verbs Precision Produced
= *: ~: ; ! ? ?. Boolean
<: >: +: - #. /: \: A. C. I.

L. p..

integer
*. -: % ^ ^. o. r. floating-point
j. complex
< ;: { p. boxed
<. > >. + * %. %: ~. | |. |:

, ,. ,: #: [ ] {. {: }. }:

same type as argument
+. Boolean, except when argument is complex: then real
-. integer, except when argument is Boolean: then Boolean
$ # i. p: integer, except when argument is extended or rational: then extended
q: integer, except when argument is extended or rational: then Boolean
i: Boolean if argument is byte, boxed, symbol, or unicode; integer if Boolean, integer, float, or complex; extended if extended or rational


Precision Produced When Arguments Of Dyad Are Both Empty

 

Verbs Precision Produced
= > >. >: < <. <: +. +: * *.

*: -: ^ ~: | ! e. p. r.

Boolean
+ - %. $ #. A. C.!.2

i. i: I. j.

integer
% ^. floating-point
; boxed
-. [ the precision of x
|. |: # ] { {. }. C. the precision of y
%: Boolean except when one argument is extended and the other is Boolean, byte, integer, boxed, or extended: then extended
, ,. ,: the higher of the nonempty argument priorities for framing fill
#: boxed unless an argument is Boolean or integer: then integer
? ?. Boolean except when one argument is extended or rational and the other is numeric: then extended
p.. the precision of x if x is numeric; otherwise Boolean