我正在尝试调用trans(~P).
,并希望输出为Not P.
对Prolog来说还是新手。
trans(A, A).
trans(B, B).
trans(~, Not).
trans(~A) :- trans(~), trans(A).
答案 0 :(得分:0)
按照我的想法行事:
% Define operators for input expressions
:- op(100,fy,~).
:- op(150,xfy,^).
:- op(200,xfy,v).
:- op(250,xfy,=>).
:- op(300,xfy,<=>).
% Define translators
trans(P) :- atom(P), write(P).
trans(P ^ Q) :-
trans(P), write(' and '), trans(Q).
trans(P v Q) :-
trans(P), write(' or '), trans(Q).
trans(~P) :-
write('not '), trans(P).
trans(P => Q) :-
write('if '), trans(P), write(' then '), trans(Q).
trans(P <=> Q) :-
trans(P), write(' if and only if '), trans(Q).
这会给:
| ?- trans((a ^ b) <=> (c v d)).
a and b if and only if c or d
yes
那么,似乎可能有一个我们想要做的括号分组。 Prolog将使用括号,因此我们不能说trans((a ^ b)).
,因为它看起来就像Prolog一样trans(a ^ b).
。但翻译人员可以输出括号(冒着过度括号的风险):
trans(P) :- atom(P), write(P).
trans(P ^ Q) :-
write('('), trans(P), write(' and '), trans(Q), write(')').
trans(P v Q) :-
write('('), trans(P), write(' or '), trans(Q), write(')').
trans(~P) :-
write('not '), trans(P).
trans(P => Q) :-
write('('), write('if '), trans(P), write(' then '), trans(Q), write(')').
trans(P <=> Q) :-
write('('), trans(P), write(' if and only if '), trans(Q), write(')').
然后你得到:
| ?- trans(a ^ b <=> c v d).
((a and b) if and only if (c or d))
yes
| ?-
Prolog会根据您在op/3
指令中设置的运算符优先级对它们进行分组(然后它们将被正确地括起来)。