Search code examples
prologdcg

Prolog DCGs: Converting from programming notation to logical notation


I'm trying to convert from this notation:

A and (B or C) equ A and B or A and C)

to standard logical notation i.e. things like and(A,B), or(A,B), neg(A)...

I thought that I nice way of doing it would be using DCGs (I made up this question because I want to practice DCGs). Any ideas why my conversion isn't working? So far I've just written the disjunction and the case when we get a variable. The answer I want should be or(atom(X),atom(Y)).

convert1(atom(X)) --> [X], {var(X)},!.
convert1(or(X,Y)) --> convert1(X), [or], convert1(Y).

test_convert1( F ) :-
   phrase( convert1( F ), [X, or, Y] ).

Solution

  • There's a syntax error in test_convert1/1. It should read

    test_convert1(F) :-
        phrase(convert1(F), [X, or, Y]).