0

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?

false
  • 10,264
  • 13
  • 101
  • 209
azureai
  • 181
  • 1
  • 13
  • 1
    The additional "false" means Prolog failed to find any additional solutions after offering what it has already offered. – lurker Nov 07 '18 at 23:46
  • @lurker So why isn't there an additional false when querying the predicate defined by the simple definition g(X,Y):-f(X,Y).? I don't understand the difference. – azureai Nov 07 '18 at 23:48
  • 1
    With regards to transitive closure you may want to find the book [The Craft of Prolog](http://www.worldcat.org/title/craft-of-prolog/oclc/21152424) and read Section 5.4 `The Problem of Transitive Closure` – Guy Coder Nov 08 '18 at 00:05
  • 1
    @azureai because there was no choice point in that case. That means Prolog knows, in that case, that there were no other options to test/explore which may or may not succeed. – lurker Nov 08 '18 at 00:20
  • @lurker I see, thank you very much. Should I delete this question? – azureai Nov 08 '18 at 00:47
  • 1
    Glad I could help. You can leave the question. The community can delete it if they see fit. – lurker Nov 08 '18 at 01:02

0 Answers0