|
From: | Juergen Sauermann |
Subject: | Re: [Bug-apl] Bug in the parser? |
Date: | Wed, 26 Nov 2014 14:45:58 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.0 |
Hi again, I have analyzed this a bit further. Below are my conclusions. 1. Problem description -------------------------------- Elias' initial question can be reduced to this: Given APL values A and B, say A←1 2 3 4 5 and B←1 1 0 1 0 how shall the following expressions be evaluated: A /⍨ B A (/⍨) B (A) /⍨ B The matter is a bit complicated by the ambiguity of the / token: For historical reasons the token / is ambiguous and could mean the dyadic function compress or the monadic operator reduce. GNU APL resolves this ambiguity at ⎕FX time when possible. For example, in 1 2 3 / B the operator / is "downgraded" from "operator reduce" to "function compress" because 1 2 3 is a value and therefore only function compress makes sense. However in A / B it is not known at ⎕FX time whether A is a function or a value. In that case a symbol is assumed to be a function (and hence / an operator) while something in parentheses like (A) is assumed to be a value. That is the reason why A /⍨ B gives a syntax error while (A) /⍨ B does not. IBM APL2 also downgrades / from operator reduce to function compress, but at a later time. The behavior of IBM APL2 in that special case is somewhat sub-optimal because insisting in / being an operator even if it is obviously not leads to the following inconsistency: IBM APL2: 1 (+¨) 1 2 1 (/¨) 1 SYNTAX ERROR GNU APL: 1 (+¨) 1 2 1 (/¨)1 1 2. Alternatives --------------------- I have tested what happens if we would introduce a M M pattern into GNU APL in order to get IBM APL2's behavior. In the above examples (I used ¨ instead of Elias' original ⍨ because ¨ is present in IBM APL2 while ⍨ is not). (+¨) is reduced by a pattern F M (function monadic-operator) to a derived function. In contrast (/¨) is not reduced because there is no M M pattern (except in the cases where / was downgraded). The M M is shifted rather than reduced and the first F in a sequence F M M ... causes the whole chain to be unrolled from left to right, This is the difference that Jay has observed between IBM APL2 and the others. Adding a M M rule forces the parser to reduce M M immediately rather than shifting it. After doing that, a few regression testcases did fail. Looking at the test results my impression was that the current behavior of GNU APL is the preferred one. 3. Conclusion -------------------- My conclusion so far is that we should leave things as they are. Putting things in parentheses is, in my opinion, not such a bad thing and it makes programs more explicit and more portable. When choosing between: A (/⍨) B and (A) /⍨ B I would recommend the former because that expresses better what is desired and the latter may change at some point in time. /// Jürgen On 11/25/2014 04:01 PM, Juergen Sauermann wrote: Hi Jay, |
[Prev in Thread] | Current Thread | [Next in Thread] |