Function-operator overloading: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 18: Line 18:
This form of overloading relies on the fact that an expression like <source lang=apl inline>a / b</source> can be disambiguated based on whether <source lang=apl inline>a</source> is a function or not: if so, <source lang=apl inline>/</source> should be an operator. This assumption is violated when [[function train]]s are part of the syntax, because a train such as <source lang=apl inline>= / +</source> could be interpreted either as the two-train <source lang=apl inline>(=/) +</source>, [[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.
This form of overloading relies on the fact that an expression like <source lang=apl inline>a / b</source> can be disambiguated based on whether <source lang=apl inline>a</source> is a function or not: if so, <source lang=apl inline>/</source> should be an operator. This assumption is violated when [[function train]]s are part of the syntax, because a train such as <source lang=apl inline>= / +</source> could be interpreted either as the two-train <source lang=apl inline>(=/) +</source>, [[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: <source lang=apl inline>⊢⍤/</source> is identical to <source lang=apl inline>/</source> 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.<ref>[[Marshall Lochbaum|Lochbaum, Marshall]]. [https://dyalog.tv/Dyalog19/?v=czWC4tjwzOQ "Tacit Techniques with Dyalog version 18.0 Operators"]. [[Dyalog '19]].</ref> When the Atop operator is not available, [[Compose]] can be used instead, but it requires an extra set of parentheses.
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 function]]s: for instance, the snippet <source lang=apl inline>-⍨/</source> is treated as a [[reduction]] with operand <source lang=apl inline>-⍨</source>. 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.
==Mitigation==
The [[Atop operator]] provides a way to obtain the other interpretation: <source lang=apl inline>⊢⍤/</source> is identical to <source lang=apl inline>/</source> 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.<ref>[[Marshall Lochbaum|Lochbaum, Marshall]]. [https://dyalog.tv/Dyalog19/?v=czWC4tjwzOQ "Tacit Techniques with Dyalog version 18.0 Operators"]. [[Dyalog '19]].</ref> When the Atop operator is not available, [[Compose]] or [[Commute]] can be used instead, but they requires an extra set of parentheses. Alternatively, the function behaviour can be forced by encapsulating the hybrid primitive in a [[dfn]].
<source lang=apl>
<source lang=apl>
       (2=2 1) / (2+2 1)  ⍝ Desired result
       (2=2 1) / (2+2 1)  ⍝ Desired result
Line 27: Line 29:
4
4
       2 (= (/∘⊢) +) 2 1  ⍝ Resolved with Compose
       2 (= (/∘⊢) +) 2 1  ⍝ Resolved with Compose
4
      2 (= (/⍨⍨) +) 2 1  ⍝ Resolved with Commute
4
      2 (= {⍺/⍵} +) 2 1  ⍝ Resolved with dfn
4
4
</source>{{Works in|[[Dyalog APL]], with version [[Dyalog APL 18.0|18.0]] for [[Atop]]}}
</source>{{Works in|[[Dyalog APL]], with version [[Dyalog APL 18.0|18.0]] for [[Atop]]}}
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 function]]s: for instance, the snippet <source lang=apl inline>-⍨/</source> is treated as a [[reduction]] with operand <source lang=apl inline>-⍨</source>. 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 ==
== References ==
<references />
<references />


{{APL syntax}}
{{APL syntax}}

Navigation menu