3

This code correctly calculates the number of elements in a difference list when I run it on GNU Prolog and SWI Prolog. However, Yap enters an infinite loop.

count(X-X1,0) :- unify_with_occurs_check(X,X1), !.
count([H|T]-T1,N) :- count(T-T1,M), N is M+1.

?- count([1,2|A]-A,N).

Why would a Prolog interpreter (like Yap) not terminate when asked this query?

Jay
  • 9,585
  • 6
  • 49
  • 72
  • 1
    I tried your code in Yap 6.2.2 and it worked fine. `?- count([1,2|A]-A,N). N = 2` – gusbro Jul 19 '12 at 19:53
  • 1
    @gusbro falls in loop in yap 5.1.3: trace: `?- count([1,2|A]-A,N). (1) call:count([1,2|_1059]-_1059,_1063) ? (1) redo:count([1,2,1,2,1,2,1,2,1,2,...]-_1059,_1063) ? (2) call:count([2,1,2,1,2,1,2,1,2,1,...]-_1059,_1147) ? (2) redo:count([2,1,2,1,2,1,2,1,2,1,...]-_1059,_1147) ?` – Thanos Tintinidis Jul 19 '12 at 20:49
  • Hm, I'm using 5.1.3, so it could be a fixed bug... – Jay Jul 19 '12 at 23:41
  • I have just installed Yap 6.2.3 and it works! Thanks! – Jay Jul 20 '12 at 01:18
  • @gusbro: you have actually answered the question, so it would be nice if you posted it as an answer. – Jay Jul 20 '12 at 13:30

1 Answers1

3

There seems to be a bug in Yap 5.1.3

Newer versions (tested with Yap 6.2.2) work fine:

?- count([1,2|A]-A,N).
   N = 2
gusbro
  • 22,357
  • 35
  • 46