In the APL array model, an empty array is one with a bound of zero, that is, an array with no elements. While a nested list model has only one empty list, APL has many different empty arrays. These arrays are distinguished by their shape and prototype.
Empty array shape
The shape of an empty array is usually determined by shape arithmetic in the function which produces it. Because in most primitive functions the result shape is determined by the argument shapes and sometimes the numeric values of elements from the arguments (for instance, in the left argument of Reshape), the shape of an empty array is rarely in doubt.
The primary exception is when using the Rank operator on an array with no cells of the specified rank. Because the shape of each result cell might be determined by values in an argument cell, the appropriate shape may be impossible to determine.
⍴ ⍳⍤0 ⊢2⍴3 2 3 ⍴ ⍳⍤0 ⊢1⍴3 1 3 ⍴ ⍳⍤0 ⊢0⍴3 0 0
The example above shows how an arithmetic rule can fail when using Rank with an empty array. For any positive integer
n, it's clear that
(n,3) ≡ ⍴ ⍳⍤0 ⊢n⍴3 because each result cell is the array
⍳3. However, the empty array
0⍴3 is indistinguishable from any other empty numeric array because it has shape
,0 and prototype
0. This means Rank cannot determine the appropriate result shape. The Rank operator still attempts to find a sensible result shape: it executes the operand on a prototype cell obtained by reshaping the argument to the argument cell shape (here, the prototype cell is a scalar
0), and uses the resulting shape to determine the shape of its final result. In this case, this succeeds in finding the appropriate result rank but not the desired shape.
If Rank is implemented by splitting the argument(s) into cells, applying the operand with Each, and mixing, then the above problem actually becomes an issue of empty array prototypes: the result of Split is empty, and calling Each on an empty array uses prototypes to determine prototypes, an unreliable operation.
Empty array prototype
Determining the prototype of an empty array when it is produced is often difficult, and may break identities even for simple functions. An empty array's prototype is far less reliable than its shape.
a ≡ ⍬,a b ≡ b,''
These identities always hold when
b are non-empty, because the result is non-empty and its elements are entirely determined by the non-empty argument. However, if we consider the catenation
then the first identity tells us that the result should be
'' while the second gives a result of
⍬. These two arrays do not match, so one of the identities must be wrong! In fact, the choice of which prototype to use for the result is a source of incompatibility among APLs. In Dyalog APL it has even been changed in the past. At one point the right argument's prototype was used; now we can inspect the first element of
⍬,'' to see that the left argument's prototype is used.
Reduction over a length-0 axis
If a reduction (using one of
⍀) is performed over an axis of length 0, the resulting array is filled with identity elements. For example, the sum of an empty list is
0 because the identity element for addition is
The identity element for a specific reduction always depends on the operand, and may depend on the argument array prototype. Dialects differ in their support for such reductions. All define identity elements for most scalar primitives, and some stop there (e.g. SAX), while others (e.g. Dyalog APL and APL2) define identity elements for various mixed functions.
|APL features |
|Syntax||Array ∙ Function ∙ Operator ∙ Assignment|
|Built-ins||Primitive function ∙ Primitive operator ∙ Quad name|
|Array model||Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Box ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype|
|User definition||Tradfn ∙ Dfn ∙ Tacit|
|Concepts and paradigms||Leading axis theory ∙ Scalar extension ∙ Conformability ∙ Scalar function ∙ Glyph ∙ Identity element|
|Errors||LIMIT ERROR ∙ RANK ERROR|