我正在 SWI Prolog 中编写一个小型解释器,并遇到以下问题:我需要编写一个谓词来确定逻辑表达式是否为真。我已经有一个谓词
evaluate_arithmetic_expression(+Expr, +State, -Value)
在给定状态下计算算术表达式。现在我需要一个逻辑表达式的谓词:
is_logical_expression(+Expr, +State)
以下子句似乎工作得很好:
is_logical_expression_true(Expr1 = Expr2, State) :-
evaluate_arithmetic_expression(Expr1, State, Value1),
evaluate_arithmetic_expression(Expr2, State, Value2),
Value1 =:= Value2.
但这不会编译:
is_logical_expression_true(Expr1 <> Expr2, State) :-
evaluate_arithmetic_expression(Expr1, State, Value1),
evaluate_arithmetic_expression(Expr2, State, Value2),
Value1 =\= Value2.
我收到错误:
Syntax error: Operator expected
看起来
<>
运算符比其他任何运算符都更难绑定,但我认为 <>
甚至不是 SWI 中的运算符。我该如何解决这个问题?
用户定义的运算符必须使用 ISO 谓词 op/3:
进行声明:- op(700, xfx, (<>)).