Diagnosing Rank‑Mismatch Errors in APL Nested Arrays
A step‑by‑step diagnostic guide for APL rank‑mismatch errors caused by excess nesting in arrays, with checks, fixes, and when to escalate.
22 Mar 2026, 06:58 UTC

Recognizable condition
When you apply a rank‑sensitive primitive such as ⍉⍉ (transpose twice) or A[;] (select all columns) to a deeply nested array, APL may return a RANK ERROR or produce an unexpected shape. The error often appears alongside a warning about non‑uniform depth, indicating that the array’s internal nesting exceeds the rank the primitive expects.
Cause / diagnostic table
| Symptom | Likely cause |
|---|---|
RANK ERROR on ⍉⍉A or A[;] | Array depth > expected rank; extra levels created by accidental concatenation with , or by using ⊂ without a matching ⊃ to flatten. |
| Unexpected shape (e.g., shape vector longer than anticipated) | Each concatenation adds an outer level; nested ⊂ builds depth without a corresponding ⊃. |
Ordered checks
Inspect shape and depth:
⍴ Agives the shape;≡ Areturns the depth (negative for nested arrays).⍴ A ⍝ shape vector ≡ A ⍝ depth, e.g., ¯2 means two levels of nestingView the canonical representation to see the exact structure:
⎕CR A ⍝ shows how APL stores the arrayPrint each level to locate excess nesting. Use
⎕←with⍉or⍪to step through dimensions:⎕← ⍉ A ⍝ transpose once, reveals outer level ⎕← ⍉⍉ A ⍝ transpose twice, will fail if depth too high
Fixes tied to findings
If depth is too high (e.g.,
≡ Areturns¯3when you expected¯1), collapse levels with repeated⊃or the idiom⍉∘⊂:B ← ⊃ A ⍝ remove one level B ← ⊃ ⊃ A ⍝ remove two levels B ← (⍉∘⊂) A ⍝ alternative: transpose then enclose to drop outer levelIf the shape is wrong after flattening, reshape using
⍴on a flattened version:C ← ,∘⊃ A ⍝ flatten to a simple vector then reshape C ← (⍴ desired_shape) ⍴ CAfter applying a fix, re‑run the original expression that previously failed to confirm the error disappears.
⍉⍉ B ⍝ should now succeed
Escalation criteria
- The rank‑mismatch persists after applying the above fixes.
- The array is ragged (non‑uniform depth) and simple flattening does not produce a usable result.
- You suspect a workspace‑specific limit (e.g., different maximum rank between Dyalog APL and GNU APL).
In these cases:
- Check the workspace version and consult the language manual for rank‑specific limits.
- Create a minimal reproducible example (the smallest array that still triggers the error) and post it to an APL community forum (e.g., Stack Overflow with the
apltag or the Dyalog forums). - If you are in a controlled environment, consider enclosing the original array in a variable before modification so you can revert:
orig ← A ⍝ keep a copy A ← ⊃ A ⍝ apply fix ⍝ … if needed, restore with A ← orig
Verification
- Run
⍴and≡on the corrected array; shape should match the expected rank and depth should be uniform (all elements at the same nesting level). - Re‑execute the original operation that previously failed; it should now return a result without
RANK ERRORand with values consistent with the intended computation. - If the result still fails, follow the escalation steps above.
Limitations
The fixes presented assume the array’s nesting is regular (uniform depth). For ragged arrays where different branches have different depths, additional steps such as partitioning with ⊆ or using ⎕ML‑dependent functions may be required, which are outside the scope of this guide.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.