Reputation: 1
Trying to convert a CNF expression such as
(a'+b+c')(a+b+c)
to a list in prolog such that it is similar to
[[-a,+b,-c],[+a,+b,+c]].
Each literal is represented either as positive or negative. i.e. if a'
this is equivalent to an atom that is -a
.
Upvotes: 0
Views: 90
Reputation: 5519
I think you can try something like this:
cnf(A, F) :-
atom_chars(A, C),
conjunction(F, C, []), !.
conjunction([D|C]) --> disjunction(D), conjunction(C).
conjunction([C]) --> disjunction(C).
disjunction(D) --> ['('], disjuncts(D), [')'].
disjunction(L) --> literal(L).
disjuncts([L|D]) --> literal(L), disjuncts(D).
disjuncts([L]) --> literal(L).
literal(-P) --> [+], proposition(P), ['\''].
literal(+P) --> [+], proposition(P).
literal(-P) --> proposition(P), ['\''].
literal(+P) --> proposition(P).
proposition(P) --> [P], { char_type(P,lower) }.
Some examples:
?- cnf('a', F).
F = [+a].
?- cnf('a\'', F).
F = [-a].
?- cnf('(a\')(+b)', F).
F = [[-a], [+b]].
?- cnf('(a\'+b+c\')', F).
F = [[-a, +b, -c]].
?- cnf('a\'+b+c\'', F).
F = [-a, +b, -c].
?- cnf('(a\'+b+c\')(a+b+c)', F).
F = [[-a, +b, -c], [+a, +b, +c]].
Upvotes: 2