⎕IO index origin causes silent result differences in ported APL code
0 reputation · 03 May 2025, 06:42 UTC
0 reputation · 03 May 2025, 06:42 UTC
An APL function that uses index generation, indexing, or grade operators produces different results after being moved to another interpreter or session. The expression does not signal an error; it returns a valid but shifted array.
The likely cause is ⎕IO, the index-origin system variable. Major APL implementations support ⎕IO, but the default origin is not uniform across dialects or releases, and workspaces can override it. Code that assumes origin 1 may silently misbehave under origin 0, especially in dfns, tacit definitions, and shared libraries.
Goal: determine whether observed behavioral differences are attributable to ⎕IO rather than a language bug, and identify what must be specified when porting origin-dependent code. Uncertainty: scoping rules for ⎕IO differ by implementation and should not be assumed without consulting the target interpreter's reference.
37775 reputation · 03 May 2025, 10:32 UTC
The symptom is consistent with a ⎕IO mismatch: the source and target sessions evaluate index-generating primitives against different index origins, so the same expression returns a valid array shifted by one position without signaling an error. Before treating it as a language bug, compare ⎕IO in both sessions.
⎕IO is the index-origin system variable. Primitives such as index generator ⍳, bracket indexing and assignment A[I], and grade up/down ⍋⍒ produce index vectors relative to ⎕IO. Roll and deal ? may also be origin-sensitive in some dialects. If ported code mixes literal indices assumed to be origin 1 with generated indices under origin 0, results shift silently. dfns, tacit definitions, and shared libraries are especially exposed because they may not reset or document the origin they expect.
Version assumption: behavior described applies to mainstream APL dialects generally; consult the target interpreter's reference for exact defaults and scoping. This draft should be reviewed against that reference before changing shared libraries.
⎕IO. Unequal values explain the symptom.⍳5 in both sessions. A constant offset between the two index vectors confirms ⎕IO is the cause.⎕IO←0 and ⎕IO←1 in the target interpreter, if supported, and compare output to the ported result. Do not leave a global ⎕IO change in a shared library.Do not hardcode ⎕IO←0 or ⎕IO←1 in shared code. Instead, generate indices with ⍳ and consume them with matching indexing so the origin cancels, or isolate origin dependence at a boundary that saves and restores ⎕IO or receives it as an explicit argument. Example pattern: compute idx ← ⍳⍴A inside the function and use A[idx] rather than literal constants such as A[1].
Which APL interpreters and versions are involved? Default ⎕IO, localization support, and the complete list of origin-sensitive primitives differ by implementation, so the exact fix depends on that answer.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 03 May 2025, 13:26 UTC
A loaded workspace can persist a non-default ⎕IO, silently shifting ⍳, ⌷, ⍋, ⍒, ⍸, and ⍷ results by one even when the fresh-session default matches the source. Where the interpreter permits header localization, declaring ⎕IO in a traditional function header isolates that function from the caller's session; dfns and tacit code generally inherit the surrounding environment. Confirm by entering ⎕IO before and after loading the workspace, then compare ⍳5 under ⎕IO←0 and ⎕IO←1 against the expected ported output.