1

If I have the following:

friends(tim,joe).

if i go:

?- friends(X,Y).

I would get:

X=tim
Y=joe

What would I have to print the following with out adding any new facts

X=tim
Y=joe
X=joe
Y=tim
spikestar
  • 163
  • 1
  • 3
  • 10
  • do you want all four lines printed out all at once? – Will Ness Apr 28 '13 at 09:10
  • if it is possible, yes – spikestar Apr 28 '13 at 09:50
  • this is not possible in Prolog. If `X=joe`, it can't be also `X=tim`, because `tom=joe` is impossible. `tim` is not `joe`. But, you can have `[s(tim,joe), s(joe,tim)]` printed as a solution to a predicate ("s" for "solution"). – Will Ness Apr 28 '13 at 09:55
  • actually, everything is possible in Prolog as Prolog is Turing complete, so it is possible to make a predicate that will _print_ the four lines as you requested; but it will not produce them as a solution, because the solution that has `X=tim` and at the same time `X=joe` is self-contradictory. – Will Ness Apr 28 '13 at 10:00
  • by any chance would you know how to go about it – spikestar Apr 28 '13 at 12:28
  • this is really an ill-advised adventure, but OK, I'll update my answer. – Will Ness Apr 28 '13 at 12:33

1 Answers1

1

You will have to add a new rule:

are_friends(X,Y):- friends(X,Y).
are_friends(X,Y):- friends(Y,X).

Then you ask:

?- are_friends(X,Y).

Prolog will answer

X=tim, Y=joe   _

and it will wait for your further command. If you press ;, then it will print the next solution:

X=tim, Y=joe   ;

X=joe, Y=tim   _

To just show the results twice - as opposed to producing them in a proper Prolog fashion - we can write

show_friends :- 
  friends(X,Y),
  write('X='), write(...), write(', Y='), write(...), nl,
  write('X='), write(...), write(', Y='), write(...), nl,
  fail.

but this is really, really, really just faking it. Ughgh. You fill in the blanks.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • is it possible to do this if i ask friend(X,Y) for it to print both of the solutions without using are_friends – spikestar Apr 28 '13 at 09:11
  • @spikestar no. if you call `friends(X,Y)`, and the only fact there is, is `friends(tim,joe)`, then this is what you will get. Does this answer your question? – Will Ness Apr 28 '13 at 09:13