Prolog - formulas in propositional logic

I am trying to make a predicate to check if a given input matches a formula.

I can use only propositional atoms like p, q, r, s, t, etc. The formulas I have to check are as follows:

neg(X) - represents the negation of X  
and(X, Y) - represents X and Y  
or(X, Y) - represents X or Y  
imp(X, Y) - represents X implies Y  

I made a predicate wffthat returns true if the given structure is a formula and false otherwise. In addition, I do not need to use variables inside the formula, only propositional atoms, as indicated below.

logical_atom( A ) :-
    atom( A ),     
    atom_codes( A, [AH|_] ),
    AH >= 97,
    AH =< 122.

wff(A):-
    \+ ground(A),
    !,
    fail.

wff(and(A, B)):-
    wff(A),
    wff(B).

wff(neg(A)):-
    wff(A).

wff(or(A, B)):-
    wff(A),
    wff(B).

wff(imp(A, B)):-
    wff(A),
    wff(B).

wff(A):-
    ground(A),
    logical_atom(A),
    !.

When I introduced a test like this wff(and(q, imp(or(p, q), neg(p))))., the call returns the values trueand false. Could you tell me why this happens?

+3
source share
1 answer

, , "defaulty", " " : , - (, , neg, imp) logical_atom/1 - ( ). . , // .., , "atom (...)". wff/1 :

wff(atom(_)).
wff(and(A, B))    :- wff(A), wff(B).
wff(neg(A))       :- wff(A).
wff(or(A, B))     :- wff(A), wff(B).
wff(imp(A, B))    :- wff(A), wff(B).

:

?- wff(and(atom(q), imp(or(atom(p), atom(q)), neg(atom(p))))).
true.

, ​​, , .

, , :

wff(atom(_))  --> [].
wff(and(A,B)) --> [_,_], wff(A), wff(B).
wff(neg(A))   --> [_], wff(A).
wff(or(A,B))  --> [_,_], wff(A), wff(B).
wff(imp(A,B)) --> [_,_], wff(A), wff(B).

:

?- length(Ls, _), phrase(wff(W), Ls), writeln(W), false.

:

atom(_G490)
neg(atom(_G495))
and(atom(_G499),atom(_G501))
neg(neg(atom(_G500)))
or(atom(_G499),atom(_G501))
imp(atom(_G499),atom(_G501))
and(atom(_G502),neg(atom(_G506)))
and(neg(atom(_G504)),atom(_G506))
neg(and(atom(_G504),atom(_G506)))
neg(neg(neg(atom(_G505))))
neg(or(atom(_G504),atom(_G506)))
neg(imp(atom(_G504),atom(_G506)))
etc.

.

+8

Source: https://habr.com/ru/post/1769855/


All Articles