Andreas_Lng
Andreas_Lng

Reputation: 1197

Prolog formula parser

I have to write a predicate parse(FIn, FOut) that takes a logical formula containing prolog operands "+", "," and ";" as parameter FIn and a logical formula containing "not", "and" and "or" instead of the prolog operands as parameter FOut. The predicate has to return true if FIn and FOut are equivalent.

For example parse(+(a, b), not(and(a, b))) => true

To check if the formulas are negated i can write

parse(FIn, FOut) :- isNeg(FIn, FOut).
isNeg(\+(F1), not(F2)).

But now i don't know how to check for "," and "and" and ";" and "or". If anyone has some hints this would be great.

Upvotes: 0

Views: 185

Answers (1)

malbarbo
malbarbo

Reputation: 11197

Here is the predicate for translating , (; is similar)

% translates (a, b, c, ...) to and(a, and(b, and(c, ...)))
parse(A, A) :- atom(A).

parse((A, B), and(Ao, Bo)) :-
    parse(A, Ao),
    parse(B, Bo).

% translates (a, b, c, ...) to and(a, b, c, ...)
parse1(A, A) :- atom(A).

parse1((A, B), O) :-
    parse1(A, Ao),
    parse1(B, Bo),
    Bo =.. [and | R],
    O =.. [and, Ao | R], !.

parse1((A, B), O) :-
    parse1(A, Ao),
    parse1(B, Bo),
    O =.. [and, Ao, Bo].

Prolog is homoiconic, this allow this kind of predicate to be written nicely.

Upvotes: 1

Related Questions