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,开发者_如何学CY)) --> convert1(X), [or], convert1(Y).
test_convert1( F ) :-
phrase( convert1( F ), [X, or, Y] ).
There's a syntax error in test_convert1/1
. It should read
test_convert1(F) :-
phrase(convert1(F), [X, or, Y]).
Your code includes two mistakes:
- In the first clause you're not reading
X
from the parsed list. - 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).
精彩评论