Diagnosing APL Compression Errors with Boolean Masks
When APL compression with a Boolean left argument produces length errors or unexpected shapes, this diagnostic guide walks through recognizable conditions, a cause table, ordered checks, and fixes tied to findings.
15 Aug 2026, 19:03 UTC

Diagnosing APL Compression Errors with Boolean Masks
You have an APL array and a Boolean mask, and you apply the slash / operator to select elements. Instead of getting the expected subarray, the interpreter raises a length error, produces an unexpectedly empty result, or returns a shape that doesn't match your intent. This guide diagnoses the most common conditions and provides ordered checks, fixes tied to findings, and escalation criteria.
What APL compression does
In APL, the slash operator with a Boolean left argument is called compression (or select). The left argument acts as a mask: each position set to 1 includes the corresponding element from the right argument, and each 0 skips it. The operation preserves the order of selected elements and shapes the result to conform along the selected axes. Terms like Boolean vector, leading dimension, and DOMAIN ERROR are explained as they appear.
Diagnostic table: common compression conditions
| Condition | Observed behavior | Likely cause |
|---|---|---|
| Boolean vector shorter than leading dimension | DOMAIN ERROR: length mismatch | Vector does not cover all leading-axis elements |
| Boolean vector longer than leading dimension | DOMAIN ERROR: length mismatch | Vector extends beyond array bounds |
| All-zero Boolean vector | Empty result array; shape conforms to selected axes; element type matches right argument | Selects no elements; valid but may be unintended |
| Boolean vector with some 1s | Selected elements preserved in order; result length equals count of 1s in left argument | Standard compress behavior |
Ordered checks
- Count the elements in the Boolean left argument; let this count be n.
- Confirm that the vector length equals the length of the array's leading dimension. In APL, the leading dimension is the first element of the shape vector, accessible as 1↑⍴array.
- If the vector length matches, verify that the result length equals n.
- For nested arrays, confirm that only the leading dimension was compressed; the inner structure of each selected element remains unchanged.
Fixes tied to findings
Mismatched vector length: Adjust the Boolean vector so its length matches the array's leading dimension. If you intend to select a subset, ensure the vector length is correct for the axis you're compressing.
All-zero mask: This is valid and produces an empty array of the right argument's element type. If you expected selected elements, review the mask construction.
Unexpected nesting behavior: Compression applies only to the leading dimension. To compress inner axes, restructure the array (e.g., using transpose ⍉) before applying /.
Concrete example (verification from standard APL behavior)
In a standard APL interpreter, entering the following produces a result whose length equals the number of 1s in the left argument:
1 0 1 0 1 / 2 3 4 5 6
The expected result is 2 4 6; the result length is three, matching the three 1s in the mask. To verify: count the 1s in the left argument (three) and confirm the result array has three elements. Note that the left argument length (five) must match the leading dimension of the right argument (five in this example). A mismatched length will cause a DOMAIN ERROR.
Where to run this: In a REPL or script of your APL dialect (e.g., Dyalog APL, GNU APL, or NARS). No special permissions are required for read-only execution. Expected check: After execution, use ≢result to count result elements and compare to the count of 1s in the mask. Relevant risk: If the mask length differs from the array's leading dimension, the interpreter will raise a length error; verify dimensions beforehand.
Limitations and practical verification
The compression operation only affects the leading dimension of the right argument. If your array has multiple leading axes and you need to select along a different axis, consult your APL dialect's documentation for axis specification—some implementations allow a left operand to specify the axis, but this is not universal. Additionally, nested inner structures are never recursed into; selected elements retain their original shape and contents. If you observe unexpected shape changes beyond the leading dimension, the issue may stem from array construction or implicit type conversions rather than compression itself.
Practical verification: isolate the array and test compression with a simple vector of known length. Confirm that the result length equals the count of 1s, and that the element type matches the right argument. If the behavior differs across APL implementations, the discrepancy likely reflects dialect-specific handling rather than a fundamental error.
Escalation criteria
If the Boolean vector length matches the leading dimension but the result shape is still incorrect, check whether the array has more than one leading dimension and whether you intended to compress a different axis. Consult your APL dialect's documentation for axis specification. If the issue persists across multiple dialects, isolate the array and test with a minimal vector to confirm expected behavior. At that point, reviewing the array's construction or consulting the interpreter's source documentation may be required.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.