Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To create a search tree you start with your query and step through your clauses pretending to be the Prolog interpreter. The blocks in the tree represent the next clause to execute, and the "legs" of the tree are what variable instantiations are occurring. If this case is complex, start with a very simple case to get the idea. There are several <a href="http://www.fb10.uni-bremen.de/anglistik/ling/ws08/intro-CL-materials/prolog-search-tree.pdf" rel="nofollow">examples</a> online.</p> <p>Here's just one path through the tree and I'll leave it as an exercise to fill in the rest:</p> <pre><code>?- p(X, [a,b,c]), q(X, [a,b,c]) | | X = [] </code></pre> <p>{ Result from the first matched clause: <code>p([], _).</code> }</p> <pre><code> | q([], [a,b,c]) % `p` clause succeeded, moving on to `q` in the query | | [] = X, [a,b,c] = [_|T] (so T = [b,c]) </code></pre> <p>{ Results from matching second <code>q</code> clause, <code>q(X, [_|T])</code>. First <code>q</code> clause did not match }</p> <pre><code> | q([], [b,c]) | | [] = X, [b,c] = [_|T] (so T = [c]) </code></pre> <p>{ Results from matching second <code>q</code> clause }</p> <pre><code> | q([], [c]) | | [] = X, [c] = [_|T] (so T = []) </code></pre> <p>{ Results from matching second <code>q</code> clause }</p> <pre><code> | q([], []) | | [] = X </code></pre> <p>{ Matches first <code>q</code> clause <code>q(X, X).</code> }</p> <pre><code> | SUCCESS (X = []) </code></pre> <p>There's another branch off of the first clause to be followed, which corresponds to matching the second <code>p</code> clause instead of the first:</p> <pre><code>?- p(X, [a,b,c]), q(X, [a,b,c]) | \ | X = [] \ X = [H|T] [a,b,c] = [H|T2] (H = a and T2 = [b,c]) | \ p([a|T], [a|[b,c]]) % matches second `p` clause ... \ (first p match ... (continue) shown above) </code></pre>
 

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