Additional 'false' at end of query

31 Views Asked by At

Basically I'm wondering why one of my definitions for a new predicate gresults 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 gsuch 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?

0

There are 0 best solutions below