<p>As long as you remain within the pure monotonic subset of Prolog, you may consider <strong>generalizations</strong> as explanations.  To take your example, the following generalizations might be thinkable depending on your precise definition of <code>blue/1</code> and <code>dog/1</code>.</p>
<pre>
?- blue(foo), * <s>dog(foo)</s>.
   false.
</pre>
<p>In this generalization, the entire goal <code>dog(foo)</code> was removed. The prefix <code>*</code> is actually a predicate defined like <code>:- op(950, fy, *).  *(_).</code>
Informally, above can be read as: <em>Not only this query fails, but even this generalized query fails. There is no blue foo at all (provided there is none).</em> But maybe there <em>is</em> a blue foo, but no blue dog at all...</p>
<pre>
?- blue(_X/*<s>foo</s>*/), dog(_X/*<s>foo</s>*/).
   false.
</pre>
<p>Now we have generalized the program by replacing <code>foo</code> with the new variable <code>_X</code>. In this manner the sharing between the two goals is retained.</p>
<p>There are more such generalizations possible like introducing <code>dif/2</code>.</p>
<p>This technique can be both manually and automatically applied. For more, there is a <a href="https://stackoverflow.com/a/30791637/772868">collection of example sessions</a>. Also see <a href="https://arxiv.org/abs/cs/0207044" rel="nofollow noreferrer">Declarative program development in Prolog with GUPU</a></p>
Reporting *why* a query failed in Prolog in a systematic way
<p>I'm looking for an approach, pattern, or built-in feature in Prolog that I can use to return <em>why</em> a set of predicates failed, at least as far as the predicates in the database are concerned. I'm trying to be able to say more than &quot;That is false&quot; when a user poses a query in a system.</p>
<p>For example, let's say I have two predicates.  <code>blue/1</code> is true if something is blue, and <code>dog/1</code> is true if something is a dog:</p>
<pre><code>blue(X) :- ...
dog(X) :- ...
</code></pre>
<p>If I pose the following query to Prolog and <code>foo</code> is a dog, but not blue, Prolog would normally just return &quot;false&quot;:</p>
<pre><code>? blue(foo), dog(foo)
false.
</code></pre>
<p>What I want is to find out <em>why</em> the conjunction of predicates was not true, even if it is an out of band call such as:</p>
<pre><code>? getReasonForFailure(X)
X = not(blue(foo))
</code></pre>
<p>I'm OK if the predicates have to be written in a certain way, I'm just looking for any approaches people have used.</p>
<p>The way I've done this to date, with some success, is by writing the predicates in a stylized way and using some helper predicates to find out the reason after the fact.  For example:</p>
<pre><code>blue(X) :-
    recordFailureReason(not(blue(X))),
    isBlue(X).
</code></pre>
<p>And then implementing recordFailureReason/1 such that it always remembers the &quot;reason&quot; that happened deepest in the stack. If a query fails, whatever failure happened the deepest is recorded as the &quot;best&quot; reason for failure. That heuristic works surprisingly well for many cases, but does require careful building of the predicates to work well.</p>
<p>Any ideas?  I'm willing to look outside of Prolog if there are predicate logic systems designed for this kind of analysis.</p>