{"id":7576,"date":"2023-01-26T14:17:58","date_gmt":"2023-01-26T14:17:58","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=7576"},"modified":"2023-03-22T09:57:02","modified_gmt":"2023-03-22T09:57:02","slug":"structural-vs-mathematical-under","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2023\/01\/structural-vs-mathematical-under\/","title":{"rendered":"Structural vs. Mathematical &#8220;Under&#8221;"},"content":{"rendered":"<p><a href=\"https:\/\/aplwiki.com\/wiki\/APL_Farm\" target=\"_blank\" rel=\"noopener\">The APL Farm<\/a> has recently hosted discussions about the proposed <em>under<\/em> operator, asking &#8220;why Dyalog hasn&#8217;t implemented it a long time ago&#8221;. I claimed that an important reason was that <a href=\"https:\/\/aplwiki.com\/wiki\/Roger_Hui\" target=\"_blank\" rel=\"noopener\">Roger Hui<\/a> wasn&#8217;t a big fan of the proposed extension known as &#8220;structural under&#8221;. Stefan Kruger then asked me why that was so. Since Roger isn&#8217;t able to respond himself, I&#8217;ll do my best to explain my own understanding of the issues.<\/p>\n<p>For anyone not familiar with the concept of <em><a href=\"https:\/\/aplwiki.com\/wiki\/Under\" target=\"_blank\" rel=\"noopener\">under<\/a><\/em>, or <em>dual<\/em> as it was known when first implemented in <a href=\"https:\/\/aplwiki.com\/wiki\/SHARP_APL\" target=\"_blank\" rel=\"noopener\">SHARP APL<\/a>, it is an operator that provides a way to express a common mathematical pattern, where a computation or proof is simplified by first translating the input domain, applying a function, and then performing the inverse transformation. For example, if we use the symbol <code class=\"language-apl\">\u2362<\/code> to denote <em>under<\/em>, have the translation function as the right operand, and the function that applies to the translated domain as the left, then we could use <code class=\"language-apl\">+\u2362\u235f<\/code> to demonstrate that multiplication is equivalent to addition under logarithm, which is why <a href=\"https:\/\/en.wikipedia.org\/wiki\/Slide_rule\" target=\"_blank\" rel=\"noopener\">slide rules<\/a> work:<\/p>\n<pre><code class=\"language-apl\">      1 2 3 4 (\u00d7 \u2261 +\u2362\u235f) 0.1 1.1 17 100\r\n1\r\n<\/code><\/pre>\n<p>Another example of simplifying multiplication using <em>dual<\/em> is the use of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fast_Fourier_transform\" target=\"_blank\" rel=\"noopener\">Fast Fourier Transforms<\/a> to multiply very large numbers, as explained in the <a href=\"https:\/\/dfns.dyalog.com\/n_xtimes.htm\" target=\"_blank\" rel=\"noopener\">notes about <code class=\"language-apl\">xtimes<\/code> in the dfns workspace<\/a>.<\/p>\n<p>It is easy to find examples of dualities outside the mathematical domain, such as midnight snacking: (open refrigerator door, remove food, close door) or Roger&#8217;s favourite example of medical surgery: apply anaesthetic to greatly simplify the operation, perform surgery and (very important) back out the anaesthetic.<\/p>\n<p>I believe that Roger was of the opinion that <em>under<\/em>\/<em>dual<\/em> should be restricted to functions that have well-defined inverses using the &#8220;mathematical&#8221; definition used in SHARP APL and <a href=\"https:\/\/aplwiki.com\/wiki\/J\" target=\"_blank\" rel=\"noopener\">the J language<\/a>. We can model this with a defined operator, let us call it <code class=\"language-apl\">Um<\/code>:<\/p>\n<pre><code class=\"language-apl\">   Um\u2190{\u237a\u2190{\u2375 \u22c4 \u237a\u237a} \u22c4 \u2375\u2375\u2363\u00af1\u22a2(\u2375\u2375 \u237a)\u237a\u237a(\u2375\u2375 \u2375)}\r\n<\/code><\/pre>\n<p>The extension known as <em>structural under<\/em> allows the transformation function to be a structural function, even though such functions do not have inverses in the mathematical sense. In this case, the left operand is applied to the subset of items selected from the right argument by the structural function. This is easy to model using a feature of some APLs known as selective specification, in which you can have a function to the left of an assignment whereby the assignment only modifies items selected from an array using so-called &#8220;structural&#8221; functions \u2013 those functions that select or reorder items of an array without doing math, such as <code class=\"language-apl\">\u2191 \u2193 \u2349 \u233d \u220a<\/code>.<\/p>\n<p>Let us call this <code class=\"language-apl\">Us<\/code>:<\/p>\n<pre><code class=\"language-apl\">   Us\u2190{\u237a\u2190{\u2375 \u22c4 \u237a\u237a} \u22c4 w\u2190\u2375 \u22c4 ((\u2375\u2375)w)\u2190(\u2375\u2375 \u237a)\u237a\u237a(\u2375\u2375 \u2375) \u22c4 w}\r\n<\/code><\/pre>\n<p>This is very useful, for example, we could negate all items of an array except the first two by writing:<\/p>\n<pre><code class=\"language-apl\">      - Us (2\u2218\u2193) \u23735\r\n1 2 \u00af3 \u00af4 \u00af5\r\n<\/code><\/pre>\n<p>The big question is whether <code class=\"language-apl\">Us<\/code> and <code class=\"language-apl\">Um<\/code> can be combined into the same operator, or two separate operators are needed. One problem is that <code class=\"language-apl\">\u2193<\/code> already has a defined inverse in Dyalog APL, which pads with zeros, so <code class=\"language-apl\">Um<\/code> returns something different:<\/p>\n<pre><code class=\"language-apl\">      - Um (2\u2218\u2193) \u23735\r\n0 0 \u00af3 \u00af4 \u00af5\r\n<\/code><\/pre>\n<p>If we want to combine the two into one, then we need a rule that allows us to decide which definition to use. <a href=\"https:\/\/aplwiki.com\/wiki\/Marshall_Lochbaum\" target=\"_blank\" rel=\"noopener\">Marshall Lochbaum<\/a>&#8216;s <a href=\"https:\/\/aplwiki.com\/wiki\/BQN\" target=\"_blank\" rel=\"noopener\">BQN language<\/a> has a table of structural primitive functions; it performs a <em>structural under<\/em> when possible (according to an <a href=\"https:\/\/mlochbaum.github.io\/BQN\/spec\/inferred.html#under\" target=\"_blank\" rel=\"noopener\">extensively described rule<\/a>), and otherwise attempts a <em>mathematical under<\/em>.<\/p>\n<p>Personally, this sort of conditional logic in the definition of a key primitive makes me very uneasy, and I think Roger was struggling with this as well. Unfortunately he passed away before we were able to finish <span style=\"text-decoration: line-through;\">arg<\/span>discussing the design. Another reason for feeling that the two operators are different is that, while it often makes sense for the transformation to apply to both arguments in the mathematical case, it is extremely rare for <em>structural under<\/em>. You cannot do:<\/p>\n<pre><code class=\"language-apl\">       1 + Us (2\u2218\u2193) \u23735\r\n<\/code><\/pre>\n<p>because when you apply (<code class=\"language-apl\">2\u2218\u2193<\/code>) to the left argument, you get an empty vector. Instead, you need to turn the left operand into a monadic function to add one to the three trailing elements:<\/p>\n<pre><code class=\"language-apl\">       1\u2218+ Us (2\u2218\u2193) \u23735\r\n1 2 4 5 6\r\n<\/code><\/pre>\n<p>This increases my personal unease about combining the two into a single operator. Even though this would work &#8220;just fine&#8221; most of the time, it would still make it difficult to explain exactly what is going on.<\/p>\n<p>It is my opinion that APL needs to remain a rationalised mathematical notation if it is to remain useful and relevant in the long term. Deciding whether to implement a strict mathematical operator, a combined operator, or two separate operators (or do nothing for a while longer), is one of the key design decisions that we will be faced with in the near future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The APL Farm has recently hosted discussions about the proposed under operator, asking &#8220;why Dyalog hasn&#8217;t implemented it a long time ago&#8221;. I claimed that an important reason was that Roger Hui wasn&#8217;t a big fan of the proposed extension &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2023\/01\/structural-vs-mathematical-under\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12],"tags":[44,45,46],"class_list":["post-7576","post","type-post","status-publish","format-standard","hentry","category-apl-language","category-cto","tag-apl-language","tag-language-design","tag-operators"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=7576"}],"version-history":[{"count":24,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7576\/revisions"}],"predecessor-version":[{"id":7583,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/7576\/revisions\/7583"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=7576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=7576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=7576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}