开发者

Prolog DCGs: Converting from programming notation to logical notation

开发者 https://www.devze.com 2023-02-02 19:56 出处:网络
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'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:

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

精彩评论

暂无评论...
验证码 换一张
取 消