Jay
Jay

Reputation: 9656

Prolog difference lists: code works on SWI and GNU Prolog, but not Yap

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?

Upvotes: 3

Views: 670

Answers (1)

gusbro
gusbro

Reputation: 22585

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

Upvotes: 3

Related Questions