prologb-prolog

Table modes and backtracking in B-Prolog


Consider the program:

:- table cost(+, min).
cost(1, 0).
cost(1, 1).
cost(2, 1).

I expected that result to cost(I, C). would be I = 1, C = 0; I = 2, C = 1 - all possible input arguments with corresponding minimum results.

But I get only one:

cost(I, C).
I = 1
C = 0 ?;
no

But if I explicitly point to all possibilities for the input argument, I get what I want:

( I = 1 ; I = 2 ), cost(I, C).
I = 1
C = 0 ?;
I = 2
C = 1 ?;
no

Is it possible to get all combinations of input arguments with corresponding minimum results without explicitly enumerating all possible inputs?


Solution

  • Some time ago I found the answer right there in the B-Prolog manual: "Note that, if table modes are not respected, or if there is no bound for an optimized argument, a program may give unexpected answers".

    Code in the question doesn't respect the "table cost(+, min)." modes and tries to use first cost/2 parameter as an output.