title: Prolog infix operators of same precedence one xfy and the other yfx with two sequential operators
body: <p>In trying to understand just infix operators of the type <code>xfy</code> and <code>yfx</code> with the same precedence and in sequence I see that there are just four combinations.</p>
  
  <p>using</p>
  
  <pre><code>a = xfy  right-associative
  b = yfx  left-associative
  </code></pre>
  
  <p>There is</p>
  
  <pre><code>aa  e.g. 1 xfy 2 xfy 3   e.g. 1 ^ 2 ^ 3
  ab  e.g. 1 xfy 2 yfx 3  
  ba  e.g. 1 yfx 2 xfy 3  
  bb  e.g. 1 yfx 2 yfx 3   e.g. 1 - 2 - 3
  </code></pre>
  
  <p>Now for (xfy xfy) <code>aa</code> the operators are both right associative.<br>
  For (yfx yfx) <code>bb</code> the operators are both left associate.</p>
  
  <p>However for (xfy yfx) <code>ab</code>  the (xfy) <code>a</code> operator is right associative and the (yfx) <code>b</code> operator is left associative. If I understand this correctly Prolog will evaluate this as:</p>
  
  <p>Using a priority of <code>500</code> and <code>x</code> meaning <code>&lt;</code> and <code>y</code> meaning <code>&lt;=</code>  </p>
  
  <pre><code>    xfy
      500
  &lt;500  &lt;=500
    /     \
   1      yfx
          500
          / \
         2   3
  </code></pre>
  
  <p>and for (yfx xfy) <code>ba</code> I would expect</p>
  
  <pre><code>      500
        yfx
    &lt;=500  &lt;500
      /     \
    xfy      3
    500
    / \
   1   2
  </code></pre>
  
  <p>yet the ISO Prolog standard reads:</p>
  
  <blockquote>
    <p>An operand with the same priority as a left-associative operator which
    precedes that operator need only be bracketed if the principal functor
    of the operand is a right-associative operator.</p>
  </blockquote>
  
  <p>With notation:</p>
  
  <blockquote>
    <p>An operand (xfy) <code>a</code> with the same priority (500) as a left-associative
    operator which precedes that operator (yfx) <code>b</code>  need only be bracketed if
    the principal functor of the operand (xfy) <code>a</code> is a right-associative
    operator.</p>
  </blockquote>
  
  <p>Did I understand this correctly? If so, why does (xfy yfx) <code>ab</code> need to bracketed while (yfx xfy) <code>ba</code> does not?</p>
  
  <p>Also I looked for a practical example of two operators of the same precedence with one being xfy and the other being yfx, and only found one case for <a href="http://rosettacode.org/wiki/Operator_precedence#Seed7" rel="nofollow noreferrer">Seed7 mult and tiimes</a>. If one could provide a more practical example for <code>ab</code> and <code>ba</code> that would be appreciated.</p>
  
  <p>EDIT</p>
  
  <p>After reading the answer by <a href="https://stackoverflow.com/a/20693625/1243762">false</a> this is my understanding.</p>
  
  <p>Since this is only about associativity, the rules using &lt; and &lt;= do not apply.<br>
  Instead we use the meaning of Y = Yes, X = No.  </p>
  
  <pre><code>Unbracketed term  Yes/No                 Equivalent bracketed term
  --------------------------------------------------------------------------------------
  1 xfy 2 xfy 3     1 No|Yes 2 No|Yes 3    1 xfy (2 xfy 3)     ** right-associative
  1 xfy 2 yfx 3     1 No|Yes 2 Yes|No 3    1 xfy (2 yfx 3)     ** this is a special!
  1 yfx 2 xfy 3     1 Yes|No 2 No|Yes 3    --------------      ** invalid: noone wants association
  1 yfx 2 yfx 3     1 Yes|No 2 Yes|No 3    (1 yfx 2) yfx 3     ** left-associative
  </code></pre>
  
  <p>EDIT </p>
  
  <p>In looking for an example that has <code>xyf</code> and <code>yfx</code> of the same priority that makes sense I found <a href="https://www.cs.auckland.ac.nz/courses/compsci367s2c/resources/prologLectures/L17-Meta-Interpreters.pdf" rel="nofollow noreferrer">Meta-Interpreters</a> which defines <code>op(500,xfy,&lt;==).</code></p>
  
  <pre><code>Valid
  3 &lt;==    1 +      2  
  3 xfy    1 yfx    2  
  3 No|Yes 1 Yes|No 2  
  
  Invalid
  1 +      2 &lt;==    3
  1 yfx    2 xfy    3
  1 Yes|No 2 No|Yes 3
  
  Valid and makes sense.
  (1 + 2) &lt;== 3
  Prec 0  xfy Prec 0
  
  Valid but does not make sense.
  1       +  (2 &lt;== 3)
  Prec 0 yfx Prec 0
  </code></pre>
  
  <p>While the examples with parens are valid, it makes sense that you have to use parens to choose which one will make sense.</p>