 # Distinct people sharing a birthday

Let’s say I have the following database:

``````birthday(jan,date(1975,11,9)).
birthday(kim,date(1968,7,2)).
birthday(rupert,date(2001,11,1)).
birthday(eli,date(1975,1,2)).
birthday(bobbie,date(1988,8,29)).
``````

The question I want to answer is “Are there two distinct people who share a birthday, and if so, who are they?” To answer this, I queried the following, but it just cycles through all the options and I’m not sure why this is:

``````?- birthday(X, date(Y, M, D)), birthday(Z, date(Y, M, D)).
``````

Why doesn’t this only return `true` if `X` and `Z` have the same birthday?

You get solutions in your query where X = Z.
SWI-Prolog shows it this way, often overlooked:

``````?- birthday(X, date(Y, M, D)), birthday(Z, date(Y, M, D)).
X = Z, Z = jan,   <<< here
``````

Try this symmetry breaking, this will not only implement the
“distinct people” condition but also half the number of solutions:

``````?- birthday(X, B), birthday(Y, B), X @< Y.
``````
1 Like

That works, but I don’t understand why because I didn’t cover symmetry breaking.

As an alternative you could also do:
`birthday(X, date(Y, M, D)), birthday(Z, date(Y, M, D)), X \= Z.`
i.e. "two people X and Z, such that they have the same birthday and not the same name (indicated by `X\=Z`).
/JC