ale
ale

Reputation: 11830

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] ).

Upvotes: 0

Views: 286

Answers (2)

Fred Foo
Fred Foo

Reputation: 363727

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

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

Upvotes: 1

Little Bobby Tables
Little Bobby Tables

Reputation: 5351

Your code includes two mistakes:

  1. In the first clause you're not reading X from the parsed list.
  2. Cuts in DCG's should be outside of the curly braces.

A working version is:

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

Upvotes: 1

Related Questions