prologlogicboolean-logicpropositional-calculus

How to determine if two propositional formulas are equivalent in Prolog?


I'm new to Prolog and have some doubts.

I need to write a function form_equiv(A,B) that tells us if if B is equivalent to A where A and B are supposed to be propositions.

I know that two propositions are equivalent if

tautology (A iff B) = TRUE

But how could I make a function that checks when a formula is a tautology.

By the way, I cannot use built-in function just AND, OR and NOT.

Right now this what I have so far:


and(P,Q) :- P, Q, !.
or(P,Q) :- (P; Q), !.
impl(P,Q) :- or(not(P),Q).
syss(P,Q) :- and(impl(P,Q),impl(Q,P)).

t.
f :- fail.

t(_).
f(_) :- fail.



:- op(400,xf,not).
:- op(500,xfx,and).
:- op(500,xfx,or).
:- op(600,xfx,impl).
:- op(700,xfx,syss).

I've done a program similar in Haskell but I'm really new to Prolog.

Can anyone help me write a function to check if a formula is a tautology?

Thanks in advance...


Solution

  • First to the logical part: two formulas A and B are equivalent if A → B ∧ B → A holds. If you can prove this formula you are done.

    Now to the prolog part:

    Putting all these notes together, this a minimal complete calculus for ∧ and ¬ only:

    eval_tt(var(true)).
    eval_tt(and(A,B)) :-
        eval_tt(A),
        eval_tt(B).
    eval_tt(not(A)) :-
        eval_ff(A).
    
    
    eval_ff(var(false)).
    eval_ff(and(A,_B)) :-
        eval_ff(A).
    eval_ff(and(_A,B)) :-
        eval_ff(B).
    eval_ff(not(A)) :-
        eval_tt(A).
    

    We can query the models for ¬(A ∧ ¬B) with the query:

    ?- eval_tt(not(and(var(A), not(var(B))))).
    A = false ;
    B = true.
    

    Would we have used cut or negation as failure, we would probably nothave found both solutions.

    Also A ∧ ¬A is unsatisfiable, as expected:

    ?- eval_tt(and(var(A), not(var(A)))).
    false.
    

    Now you just need to extend this minimal calculus by other operators you would like to have (disjunction, implication, equivalence, etc.). Btw. if you have seen sequent calculus, you might recognize some of the ideas :)

    Edit: I haven't explained how to get from satisfiability to validity. The problem is the following: an answer substitution from a query to eval_tt(X) only tells us that X is satisfiable. In logic, we usually define X as valid in terms of ¬X being unsatisfiable. This can be expressed in Prolog with negation as failure by defining:

    valid(X) :-
      \+ eval_ff(X).
    

    What's the problem here? We check a formula for satisfiability, e.g.

    ?- valid2(not(and(var(X),not(var(X))))).
    true.
    

    but we don't get an answer substitution. In particular if the query is not sufficiently instantiated, we get wrong results:

    ?- valid(X).
    false.
    

    But there's certainly a valid formula - we tried one above. I have not found a good solution yet that can enumerate all the valid formulas.