UMBC ebiquity
Eliminating Duplicate Answers in Prolog

Eliminating Duplicate Answers in Prolog

Tim Finin, 1:00pm 12 March 2006

I was recently asked

Say I have the sister relation defined like this:

sister(X,Y) :- female(X),parent(P,X),parent(P,Y), + X=Y.

And say I have a DB full pairs that Prolog can figure out are sisters. If I try to find all the sisters using a query like this:

?- sister(X,Y).

I get each pair in each order. Is there any way to make the query such that all of the pairs of sisters are listed only once, in whichever order Prolog finds first?

This is a classic problem that’s also common in a databases query context (SQL is also very declarative). The problem gets right to the issue of whether we want to program declaratively or procedurally. Additional complications are introduced by the desire to write Prolog predicates that can serve either to prove that a relation holds between values or to generate values for which a given relation holds. I tried to fire off a quick and hopefully helpful response to the question, but it spun out of control. To quote Piet Hein, “Problems worthy. of attack. prove their worth. by hitting back.” After working to write it up, I thought I’d put it online in case others might find it useful.

Related posts:

  1. Making $$ with Online Bookstores
  2. Google Answers is no longer accepting questions
  3. Stuxnet questions and answers from F-Secure

Comments are closed.