Function-operator overloading
In APL syntax, function-operator overloading (sometimes schizophrenia in reference to split personality) is the practice of using a single glyph for both a primitive function and a primitive operator. Dialects with this feature include Dyalog APL, NARS2000, A+, and GNU APL. Most commonly it refers to one of the slash glyphs (such as /
for Replicate or Reduce), but assignment may also be handled in a similar manner because ordinary assignment has the form of a dyadic function while modified assignment works like a monadic operator applied dyadically. It may apply only to the glyphs themselves, or to their values as well: for example, in Dyalog APL the assignments r←/
and even r←/[3]
create variables r
which exhibit overloading.
The following glyphs may be subject to function-operator overloading:
Glyph | Function | Operator |
---|---|---|
/ |
Replicate | Reduce |
⌿
| ||
\ |
Expand | Scan |
⍀
| ||
← |
Assignment | Modified assignment |
This form of overloading relies on the fact that an expression like a / b
can be disambiguated based on whether a
is a function or not: if so, /
should be an operator. This assumption is violated when function trains are part of the syntax, because a train such as = / +
could be interpreted either as the two-train (=/) +
, Equals reduction atop Plus, or as a three train Equals Compress Plus. No interpretation can always give the result the user wants, but dialects with overloading always choose the first interpretation, in which the overloaded value is treated as an operator.
The Atop operator provides a way to obtain the other interpretation: ⊢⍤/
is identical to /
as a function, but forces the function-operator overloading to be resolved in favor of a function because there is a dyadic operator to its left.[1] When the Atop operator is not available, Compose can be used instead, but it requires an extra set of parentheses.
(2=2 1) / (2+2 1) ⍝ Desired result 4 2 (= / +) 2 1 ⍝ / is treated as an operator: unwanted here 0 2 (= ⊢⍤/ +) 2 1 ⍝ Resolved with Atop 4 2 (= (/∘⊢) +) 2 1 ⍝ Resolved with Compose 4
Function-operator overloading works by checking to the left of a potential function or operator to see if it is a function. This includes derived functions: for instance, the snippet -⍨/
is treated as a reduction with operand -⍨
. In A+, only a small set of scalar dyadic functions can be used as operands to Reduce and Scan, and the language simply checks whether these glyphs appear immediately to the left of the slash. Thus, parenthesizing or assigning a name to these functions will cause overloading resolution to fail, resulting in a valence error.
References