Bagof on a tabled predicate fails *solved*

Hi,

I wrote this Tower of Hanoi Planner:


:- set_prolog_flag(table_space, 10737418240). % optional

toh_create( NTOWERS, OBJECT) :- length( OBJECT, NTOWERS).
toh_create3( OBJECT) :- toh_create( 3, OBJECT).

toh_move( X, Y2, OBJ1, OBJ2) :- true
, nth0( X, OBJ1, LVON1, OBJREST1)
, when( nonvar(Y), ( Y >= X -> plus(1,Y,Y2) ; Y = Y2) )
, nth0( Y, OBJREST1, LBIS1, OBJREST) 
, LVON1 = [ E | LVON2]
, LBIS2 = [ E | LBIS1]
, nth0( X, OBJ2, LVON2, OBJREST2)
, nth0( Y, OBJREST2, LBIS2, OBJREST) 
.

toh_search( OBJ1, OBJ2, MOVES) :- OBJ1 = OBJ2, MOVES=[].

toh_search( OBJ1, OBJ2, MOVES) :- true
, toh_move( X, Y, OBJ1, OBJM)
, MOVE= X-Y
, MOVES=[MOVE|RESTMOVES]
, toh_search(OBJM,OBJ2, RESTMOVES)
.

:- table toh_search2/3.

toh_search2( OBJ1, OBJ2, MOVES) :- OBJ1 = OBJ2, MOVES=[].

toh_search2( OBJ1, OBJ2, MOVES) :- true
, toh_move( X, Y, OBJ1, OBJM)
, nth0(X,OBJ1,[XVALUE|_])
, nth0(Y,OBJ1,YVALUES)
, ( ( YVALUES = [YVALUE|_], XVALUE < YVALUE) ; YVALUES = [])

, MOVE= X-Y
, MOVES=[MOVE|RESTMOVES]
, toh_search2(OBJM,OBJ2, RESTMOVES)
.

I need the tabling for bigger queries.

When I query all combinations, the solutions are correct so far:


?- toh_create(4, OBJ1), toh_create(4, OBJ2), L=[1,2,3,4], OBJ1=[L,[],[],[]], OBJ2=[[],[],[],L], length( MOVES,LEN), writeln(LEN), toh_search2( OBJ1, OBJ2, MOVES), writeln(MOVES).
0
1
2
3
4
5
6
7
8
9
[0-3,0-2,3-2,0-1,0-3,2-0,1-3,2-3,0-3]
OBJ1 = [[1, 2, 3, 4], [], [], []],
OBJ2 = [[], [], [], [1, 2, 3, 4]],
L = [1, 2, 3, 4],
MOVES = [0-3, 0-2, 3-2, 0-1, 0-3, 2-0, 1-3, 2-3, ... - ...],
LEN = 9 ;
[0-3,0-2,3-2,0-1,0-3,1-3,2-1,2-3,1-3]
OBJ1 = [[1, 2, 3, 4], [], [], []],
OBJ2 = [[], [], [], [1, 2, 3, 4]],
L = [1, 2, 3, 4],
MOVES = [0-3, 0-2, 3-2, 0-1, 0-3, 1-3, 2-1, 2-3, ... - ...],
LEN = 9 ;
[0-3,0-2,3-2,0-1,0-3,1-3,2-0,2-3,0-3]
OBJ1 = [[1, 2, 3, 4], [], [], []],
OBJ2 = [[], [], [], [1, 2, 3, 4]],
L = [1, 2, 3, 4],
MOVES = [0-3, 0-2, 3-2, 0-1, 0-3, 1-3, 2-0, 2-3, ... - ...],
LEN = 9 .

...

But when I try to get all combinations in a list, the predicate fails.

?- toh_create(4, OBJ1), toh_create(4, OBJ2), L=[1,2,3,4], OBJ1=[L,[],[],[]], OBJ2=[[],[],[],L], length( MOVES,9), bagof( MOVES, toh_search2( OBJ1, OBJ2, MOVES), L).
false.

Even if I created all combinatinons before that:

?- toh_create(4, OBJ1), toh_create(4, OBJ2), L=[1,2,3,4], OBJ1=[L,[],[],[]], OBJ2=[[],[],[],L], length( MOVES,9), toh_search2( OBJ1, OBJ2, MOVES), writeln(MOVES), fail.
[0-3,0-2,3-2,0-1,0-3,2-0,1-3,2-3,0-3]
[0-3,0-2,3-2,0-1,0-3,1-3,2-1,2-3,1-3]
[0-3,0-2,3-2,0-1,0-3,1-3,2-0,2-3,0-3]
[0-3,0-2,0-1,3-2,0-3,2-0,1-3,2-3,0-3]
[0-3,0-2,0-1,3-2,0-3,1-3,2-1,2-3,1-3]
[0-3,0-2,0-1,3-2,0-3,1-3,2-0,2-3,0-3]
[0-3,0-2,0-1,3-1,0-3,1-0,1-3,2-3,0-3]
[0-3,0-1,3-1,0-2,0-3,2-3,1-2,1-3,2-3]
[0-3,0-1,3-1,0-2,0-3,2-3,1-0,1-3,0-3]
[0-3,0-1,3-1,0-2,0-3,1-0,2-3,1-3,0-3]
[0-3,0-1,0-2,3-2,0-3,2-0,2-3,1-3,0-3]
[0-3,0-1,0-2,3-1,0-3,2-3,1-2,1-3,2-3]
[0-3,0-1,0-2,3-1,0-3,2-3,1-0,1-3,0-3]
[0-3,0-1,0-2,3-1,0-3,1-0,2-3,1-3,0-3]
[0-2,0-3,0-1,3-1,0-3,1-0,1-3,0-3,2-3]
[0-2,0-1,2-1,0-2,0-3,2-3,1-2,1-3,2-3]
[0-2,0-1,2-1,0-2,0-3,2-3,1-0,1-3,0-3]
[0-2,0-1,2-1,0-2,0-3,1-0,2-3,1-3,0-3]
[0-1,0-3,0-2,3-2,0-3,2-0,2-3,0-3,1-3]
[0-1,0-2,1-2,0-1,0-3,2-0,1-3,2-3,0-3]
[0-1,0-2,1-2,0-1,0-3,1-3,2-1,2-3,1-3]
[0-1,0-2,1-2,0-1,0-3,1-3,2-0,2-3,0-3]
false.

How can I fix this?

Just in case the problem is related to the prolog version : SWI-Prolog version 9.2.2 for x86_64-linux

Thanks in Advance,
Frank Schwidom

Yeah, you are reusing the “L” variable as the result of the bagof. You should try and debug or trace your program and you would see such problems.

?- toh_create(4, OBJ1),
   toh_create(4, OBJ2),
   L=[1,2,3,4], % here is the first L
   OBJ1=[L,[],[],[]],
   OBJ2=[[],[],[],L],
   length( MOVES,9),
   bagof( MOVES, toh_search2( OBJ1, OBJ2, MOVES), M). % renamed to M
OBJ1 = [[1, 2, 3, 4], [], [], []],
OBJ2 = [[], [], [], [1, 2, 3, 4]],
L = [1, 2, 3, 4],
MOVES = [_, _, _, _, _, _, _, _, _],
M = [[0-3, 0-2, 3-2, 0-1, 0-3, 2-0, 1-3, ... - ...|...], [0-3, 0-2, 3-2, 0-1, 0-3, 1-3, ... - ...|...], [0-3, 0-2, 3-2, 0-1, 0-3, ... - ...|...], [0-3, 0-2, 0-1, 3-2, ... - ...|...], [0-3, 0-2, 0-1, ... - ...|...], [0-3, 0-2, ... - ...|...], [0-3, ... - ...|...], [... - ...|...], [...|...]|...].

Thanks. Didn’t see it.