Many Prolog systems are not as smart as we expect them to be. This has several reasons, mostly because of a tradeoff choice of the implementer. What appears important to some might not be that important to others.
As a consequence these leftover choicepoints may accumulate in time and prevent to free auxiliary data. For example, when you want to read in a long list of text. A list that is that long that it does not fit into memory at once, but still can be processed efficiently with library(pio)
.
If you expect exactly one answer, you might use call_semidet/1
to make it determinate.
See this answer for its definition and a use case.
?- plus(s(s(s(0))), s(0), Z).
Z = s(s(s(s(0)))) ;
false.
?- call_semidet(plus(s(s(s(0))), s(0), Z)).
Z = s(s(s(s(0)))).
But you can see it also from a more optimistic side: Modern toplevels (like the one in SWI) do show you when there are leftover choicepoints. So you can consider some countermeasures like call_semidet/1
.
Here are some related answers: