Basically I'm wondering why one of my definitions for a new predicate g
results in my query ending whith a 'false', while the other definition jumps straight back to '?-'.
Given a database like this:
f(a,b).
f(b,c).
f(c,d).
I want to produce a new predicate which represents some kind of transitive closure of f
with the addition that it should also include a list of how the element was created. I.e. I want a g
such that ?- g(X,Y,Z).
will produce this:
g(a,c,[f(a,b),f(b,c)]).
g(b,d,[f(b,c),f(c,d)]).
g(a,d,[f(a,b),f(b,c),f(c,d)])
I introduce an auxiliary concatenation predicate:
con([],L,L).
con([X|L1],L2,[X|C]):-con(L1,L2,C).
And then my attempt at a solution would be:
g(X,Z,[f(X,Y),f(Y,Z)]):-f(X,Y),f(Y,Z).
g(X,Z,C):-f(Y,Z),g(X,Y,L),con(L,[f(Y,Z)],C).
While the correct output is produced, at the end an additional false.
is printed:
?-g(X,Y,Z).
X = a,
Y = c,
Z = [f(a, b), f(b, c)] ;
X = b,
Y = d,
Z = [f(b, c), f(c, d)] ;
X = a,
Y = d,
Z = [f(a, b), f(b, c), f(c, d)] ;
false.
Whereas a simple definition like this:
g(X,Y):-f(X,Y).
produces
g(a,b).
g(b,c).
g(c,d).
without the 'false'.
Does this mean my program has an error? If yes, what is it?