Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWI Prolog backtracking behavior after `read/1` call

Tags:

prolog

I was looking at another Prolog question on StackOverflow and encountered this situation.

Suppose I have the following facts:

foo(1).
foo(2).
foo(3).
foo(4).

At the SWI Prolog (version 7.4.2) prompt, I exercised this:

2 ?- write('Enter number: '), read(X), nl, foo(Y), Y > X.
Enter number: 1.

X = 1,
Y = 2.

3 ?-

As you can see, SWI Prolog provides one solution with no prompt for additional solutions (which do exist). It does not backtrack.

In GNU Prolog (version 1.4.4), the behavior is more what I would expect:

| ?- write('Enter number: '), read(X), nl, foo(Y), Y > X.
Enter number: 1.

X = 1
Y = 2 ? ;

X = 1
Y = 3 ? ;

X = 1
Y = 4

yes
| ?-

Thanks to @trivelt for a reduction of the problem to simply:

?- foo(X).           % Backtracks and finds all solutions for X
?- read(_), foo(X).  % Does not backtrack and finds only one solution for X


Is this a bug in the SWI version 7.4.2 implementation? Or are these alternative acceptable/expected behaviors?
like image 835
lurker Avatar asked Nov 16 '17 11:11

lurker


1 Answers

At the very least, I would regard it as a severe deficit of the SWI toplevel that an interaction with a program may interfere in this unexpected way with the toplevel control.

In SWI-Prolog, this is filed as issue #166:

Using read/1 on the toplevel commits unexpectedly

This shortcoming of the SWI toplevel prevents users to see all solutions in many cases of practical relevance.

Since ECLiPSe has already fixed this issue, maybe someone will fix it in SWI too at some point.

like image 57
mat Avatar answered Oct 06 '22 12:10

mat