Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, because <code>=/2</code> does not mean <em>assign</em> in Prolog, but rather <em>unify</em>. And the unification algorithm does not know anything at all about arithmetic, just structure. So you can do some interesting things with arithmetic expressions in Prolog that are quite difficult to pull off in other languages:</p> <pre><code>?- X = 5 + 2. X = 5+2. </code></pre> <p>It looks like nothing happened there, but what actually happened is X was given the value "5 + 2" <em>as a structure</em>. Put another way:</p> <pre><code>?- A + B = 5 + 2. A = 5, B = 2. </code></pre> <p>Or even: </p> <pre><code>?- X = 5 + 2, X =.. [Op|_]. X = 5+2, Op = (+). </code></pre> <p>That last one might make more sense with the whole list though:</p> <pre><code>?- X = 5 + 2, X =.. Y. X = 5+2, Y = [+, 5, 2]. </code></pre> <p>This is an effect of the remarkable "univ" operator, <code>=../2</code>, which is able to convert between Lisp-like lists and Prolog syntax, enabling you to do interesting construction and decomposition of structures in a generic fashion.</p> <p>Now, <code>is/2</code>, on the other hand, <em>does</em> know about arithmetic. It unifies its left argument with the result of arithmetic simplification of its right. <strong>Do note</strong> that it only works in one direction:</p> <pre><code>?- 7 is 5 + 2. true. ?- 5 + 2 is 7. false. </code></pre> <p>You could say that <code>=/2</code> is interested in structure and <code>is/2</code> is interested in numeric equality. But it does mean that it's unusually easy to teach Prolog algebra:</p> <pre><code>simplify(X * Y + X * Z, X * (Y + Z)). % distributive property simplify(X, X). ?- simplify(A * 3 + A * 4, Q). Q = A* (3+4) </code></pre> <p>Now, this isn't perfect (note that we got 3+4 and not 7) and it's still a lot of work to make it really smart:</p> <pre><code>?- simplify(3 * A + 4 * A, Q). Q = 3*A+4*A. </code></pre> <p>But that's a problem for another day.</p> <p>Nutshell:</p> <ul> <li><code>=/2</code> triggers unification</li> <li><code>is/2</code> triggers arithmetic evaluation</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload