Structural Under
⍢
|
Structural Under (⍢
) is a dyadic operator which takes two function operands, and applies the left operand to values selected by the right. The right operand must be a "structural function", meaning that it can rearrange values from the argument or inspect their structure but can't compute using the values themselves. Conceptually related to Under as defined by Iverson, structural Under may be seen as a way to obtain more useful results with structural functions that discard parts of the argument by remembering the original values there. Some version is supported by the Under primitive in APL dialects dzaima/APL, Extended Dyalog APL and Dyalog APL Vision, Kap, and TinyAPL, as well as BQN, Uiua, and (for limited cases) J.
Examples
In the example below, the right operand 2↓5↑⊢
selects elements 3 to 5 of an argument vector. The left operand ⌽∘-
negates and reverses these three elements, with other values left as they were.
⌽∘-⍢(2↓5↑⊢) ⍳10 1 2 ¯5 ¯4 ¯3 6 7 8 9 10
This ability to modify part of an array is similar to selective assignment, but in a functional style, or At, but more general. In Iverson's definition of Under, retaining non-selected values isn't possible, because the result is defined as the inverse of 2↓5↑⊢
on ¯5 ¯4 ¯3
: other values have been forgotten.
Structural operand handling
Two major strategies of implementation have appeared that handle the right operand in different ways. One method is to recognize individual functions and tacit constructs as is typically done with Inverse; it's used by dzaima/APL, Kap, and Uiua. This method provides more flexibility at the individual level, such as allowing the left operand to change its argument length in the following example. It also allows structural and other primitives to be integrated cleanly, as both are handled with the same mechanism, and in dzaima/APL is used to implement structural-like "remembering" of phase or magnitude for ⍢×
and ⍢|
.
3∘↑⍢(5∘↑) ⍳10 1 2 3 6 7 8 9 10 ÷⍢(2↓2*5↑⊢) ⍳10 1 2 ¯3 ¯4 ¯5 6 7 8 9 10 1∘+⍢| ¯2 1 4 ¯3 2 5
The other applies the structural function to a test argument and is used by BQN, TinyAPL, and Extended Dyalog APL (as a result of its use of Dyalog's selective assignment). The test value contains indices back into the original argument in order to trace where values selected by the operand came from; in TinyAPL it also includes argument values to support sorting as a selective function. This method can handle more complex combinations, such as the following dfn below that picks out the first and last two elements of its argument.
-∘⊖⍢{(2↑⍵)⍪(¯2↑⍵)} ⍳10 ¯10 ¯9 3 4 5 6 7 8 ¯2 ¯1
History
Structural Under was developed by Marshall Lochbaum around 2017,[1] and was included in Extended Dyalog APL in its initial release in 2018 and added to dzaima/APL in 2020. Later dzaima would implement ⍢×
and ⍢|
in the same spirit, now featured in dzaima/APL and dzaima/BQN. The language BQN has included structural and computational Under, using the glyph ⌾
, since its initial design in 2020. Structural Under was added to each of Kap, Uiua, and TinyAPL during their early development, and is implemented for a small number of cases in J version 9.5 (2023).