title: Deducing facts from a set via a predicate by metaprogramming
body: <p>Suppose I have a set of Literals (represented as a list for instance) and a predicate specified dynamically, what I want is to produce a set of literals that contains all the previous ones in addition to the ones that can be deducted by applying the predicate to the set.</p>
  
  <p>An example, having defined the predicate</p>
  
  <pre><code>pred(A, B) :- base(A, B).
  pred(A, C) :- base(A, B), pred(B, C).
  </code></pre>
  
  <p>and supposing such a signature for the predicate</p>
  
  <pre><code>deduce_set(+Set, +Pred, ?DeducedSet)
  </code></pre>
  
  <p>the following statement holds (is true):</p>
  
  <pre><code>deduce_set([base(a,b), base(a,c), base(b,d), base(d, e)],
             pred/2,
             [base(a,b), base(a,c), base(b,d), base(d,e), pred(a,d), pred(a,e), pred(b,e)]
            ).
  </code></pre>
  
  <p>What is the most efficient and general way to do so? I've been thinking about something like: </p>
  
  <ul>
  <li>asserting all literals in Set</li>
  <li>call Pred</li>
  <li>if it succeeds assert its head</li>
  <li>collect all the asserted facts in the resulting set and put into a list</li>
  </ul>
  
  <p>isn't there a better way?</p>
  
  <p><strong>UPDATE</strong> This solution, better defined by CapelliC, by using metaprogramming can't cope with vars in the set under Object Identity. Any workaround for this?</p>