title: Why does this Prolog Fibonacci function cause a "instantiation_error"?
body: <p>I am trying to calculate the <a href="http://en.wikipedia.org/wiki/Fibonacci_series" rel="nofollow">Fibonacci series</a> using the following function:</p>
  
  <pre><code>fib(0,A,_,A).
  fib(N,A,B,F) :-
      N1 is N-1, Sum is A+B, fib(N1, B, Sum, F).
  fib(N, F) :- fib(N, 0, 1, F).
  </code></pre>
  
  <p>This is intended to works like this:</p>
  
  <pre><code>| ?- fib(20,Result).
  
  Result = 6765 ? 
  </code></pre>
  
  <p>But when I try this, it complains:</p>
  
  <pre><code>| ?- fib(What,6765).
  uncaught exception: error(instantiation_error,(is)/2)
  </code></pre>
  
  <p>Does anyone understand why this is happening?</p>