title: Prolog: deduction given that items can be in exactly one of two sets, with set sizes known
body: <p>I have 5 people in a room. I'll be writing rules to determine whether the people are happy or sad. However, before I even start with that, I have the overlying knowledge that - of the 5 - exactly 3 are happy and 2 are sad (and none can be both). It should therefore be possible to make deductions based on this: if - by any means - I know who the three happy people are, then I can deduce the two sad people, and vice versa.</p>
  
  <p>What I've got so far is as follows:</p>
  
  <pre><code>person(bob).
  person(tim).
  person(steve).
  person(roy).
  person(jack).
  
  sad(bob).
  sad(tim).
  
  happy(X) :-
    person(X),
    \+ sad(X),
    findall(Y, sad(Y), YS),
    length(YS, 2).
  </code></pre>
  
  <p>When asked <code>happy(X)</code>, Prolog will give me Roy, Steve and Jack, because it already knows who the two sad people are. Problem: I'm unable to define a <code>sad/1</code> rule in the same manner, because of the mutual recursion with <code>happy/1</code>. I want to be able to add in rules such that the outcome in the above example remains the same, yet the following initialisation would list Bob and Tim as sad:</p>
  
  <pre><code>person(bob).
  person(tim).
  person(steve).
  person(roy).
  person(jack).
  
  happy(steve).
  happy(roy).
  happy(jack).
  </code></pre>
  
  <p>Is there a better way I should be thinking about this? It's important that I'll be able to go on to later write more rules for <code>sad/1</code> and <code>happy/1</code>, adding additional logic beyond the fact that deduction should be possible based on the knowledge that the 5 are split into 3 happy and 2 sad.</p>