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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With