3

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?

false
  • 10,264
  • 13
  • 101
  • 209
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • 1
    Hopefully, [@nfz](http://stackoverflow.com/users/1890219/nfz) will answer! – false Jan 31 '13 at 12:44
  • 1
    Sergei, you are asking this question simultaneously on SO and in comp.lang.prolog - in this manner you will get half of the responses here and half there. – false Feb 01 '13 at 13:18

2 Answers2

2

A table declaration with an optimization mode (min or max) instructs the system to table one answer for each combination of the input arguments. You can give a cardinality limit to instruct the system to store multiple optimal answers. For example, you can give a declaration like:

:- table cost(+, min):3.

Then, the system will table up to 3 best answers, ordered by optimality, for each input. The limit is assumed to be 1 if not explicitly given.

The system does not store all answers. This is important because for dynamic programming problems, you only need to remember the current best answer to a sub-problem.

Neng-Fa Zhou

false
  • 10,264
  • 13
  • 101
  • 209
nfz
  • 91
  • 1
  • I can't use `table cost(+, min):3.`, because then for `cost(1, C).` I'll get 2 answers C = 0 and C = 1. I want to store only one min cost for every input parameter. But when I call `cost(I, C).` I expected to get this unique min cost for every input: I = 1, C = 0 ; I = 2, C = 1. – Sergii Dymchenko Jan 31 '13 at 20:08
1

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.

Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46