Show HN: Automatically interpret and validate nested natural logic arguments
Nice! For comparison, here is how one could approach this task in Prolog. First, let us introduce a few suitable <i>operators</i>, so that we can naturally state the given facts:<p><pre><code>    :- op(700, xfy, and).
    :- op(700, xfy, ==&gt;).
    :- op(750, xfy, or).
    :- op(700, fx, not).
</code></pre>
With these operators, the given knowledge could be represented as follows:<p><pre><code>    fact(&#x27;I go to school&#x27; and &#x27;I study&#x27; and &#x27;I exercise&#x27;).
    fact(&#x27;I exercise&#x27; ==&gt; &#x27;I\&#x27;m healthy&#x27;).
    fact(&#x27;I study&#x27; or &#x27;I\&#x27;m sick&#x27;).
    fact(not &#x27;it rains today&#x27;).
    fact(&#x27;I study&#x27; and (&#x27;I go to gym&#x27; or &#x27;I drink a coffee&#x27;)).
    fact(&#x27;it rains today&#x27; or (&#x27;I exercise&#x27; and &#x27;I study&#x27;)).
    fact(&#x27;I\&#x27;m healthy&#x27; ==&gt; (&#x27;I\&#x27;m happy&#x27; or &#x27;I\&#x27;m fit&#x27;)).

</code></pre>
Now, all it takes is an <i>interpreter</i> for such domain-specific rules. We can easily implement one in Prolog. Here is a starting point:<p><pre><code>    true(C)      :- fact(F), conjunction_conjunct(F, C).
    true(B)      :- fact(A ==&gt; B), true(A).
    true(A or _) :- true(A).
    true(_ or B) :- true(B).

    conjunction_conjunct(C, C).
    conjunction_conjunct(A and B, D) :-
            (   conjunction_conjunct(A, D)
            ;   conjunction_conjunct(B, D)
            ).

</code></pre>
With this knowledge base and the given deduction rules, we can query for example:<p><pre><code>    ?- true(&#x27;I study&#x27; and &#x27;I exercise&#x27;).
    true .

    ?- true(&#x27;I study&#x27; or &#x27;I\&#x27;m sick&#x27;).
    true .

    ?- true(&#x27;I go to gym&#x27;).
    false.

    ?- true(&#x27;I\&#x27;m healthy&#x27;).
    true .

    ?- true(&#x27;it rains today&#x27; or &#x27;I\&#x27;m healthy&#x27;).
    true .
</code></pre>
We can also use this interpreter to ask the most general query, and it enumerates statements it can derive:<p><pre><code>    ?- true(T).
    T =  (&#x27;I go to school&#x27;and&#x27;I study&#x27;and&#x27;I exercise&#x27;) ;
    T = &#x27;I go to school&#x27; ;
    T =  (&#x27;I study&#x27;and&#x27;I exercise&#x27;) ;
    T = &#x27;I study&#x27; ;
    T = &#x27;I exercise&#x27; ;
    T =  (&#x27;I exercise&#x27;==&gt;&#x27;I\&#x27;m healthy&#x27;) ;
    T =  (&#x27;I study&#x27;or&#x27;I\&#x27;m sick&#x27;) ;
    T =  (not&#x27;it rains today&#x27;) ;
    T =  (&#x27;I study&#x27;and(&#x27;I go to gym&#x27;or&#x27;I drink a coffee&#x27;)) .
</code></pre>
Note that a more direct formulation is possible too with Prolog. However, an important advantage of the interpreter-based approach is that it lets us stay very close to the task description. Further, we can easily extend this interpreter with more features that can be implemented on top of Prolog.