使用序言为算术表达式创建项的一阶逻辑

first order logic creating terms for arithmetic expressions using prolog

给定签名(0,Z,{plus(2),minus(2),times(2)},常量是整数,函数是加号、减号和乘以 arity 2。我想写一个两个谓词 arth/2printarth/1 接受上面签名中的术语并进行必要的算术计算加法,减法和乘法。arth/2 将打印结果和 printarth/1 应该得出如下所示的评估表达式。

我想实现两件事

第一个:

?- arth( plus(minus(8,2), times(4,-3)), N).
N = -6

N is evaluated as ((8−2) + (4∗−3)) = (6 +−12) =−6

秒:

?- printarth(plus(minus(8,2), times(4,-3)), N).
((8 - 2) + (4 * -3))
true.

我明白 Terms, Ops and complex terms 的使用是为了这个,并开始我的代码如下

arithmetic_operator('+').
arithmetic_operator('-').
arithmetic_operator('*').

arithmetic_expression(N) :- integer(N).

arithmetic_expression(Term) :-
    Term =..[Functor,Component1,Component2],
    arithmetic_operator(Functor),
    arithmetic_expression(Component1),
    arithmetic_expression(Component2).

从这里我发现很难创建 arth/2printarth/1,因为我无法调用 arithmetic_expression(Term) 并在调用时抛出错误。

?- arithmetic_expression(..[+,5,7]).
ERROR: Syntax error: Operator expected
ERROR: arithmetic_expression(.
ERROR: ** here **
ERROR: .[+,5,7]) .

有关此任务的任何资源都非常有用。

如果你想使用如下所示的术语:

minus(2, 3)

并把它变成等价于 2 - 3 的算术表达式 -(2, 3) (默认定义 - 作为运算符),然后计算它,你可以这样做像这样:

term_arithmetic_expression(T, E) :-
    T =.. [Name, X, Y],
    binary_op(Name, Op),
    E =.. [Op, X, Y].

eval_arithmetic_expression(T, R) :-
    term_arithmetic_expression(T, E),
    R is E.

binary_op(minus, -).
% add more binary operations

现在这至少起作用了:

?- eval_arithmetic_expression(minus(2, 3), R).
R = -1.

如您所见,term_arithmetic_expression/2eval_arithmetic_expression/2 都有两个参数。这就是您需要将 minus(2, 4) 映射到 2 - 4

您的 arithmetic_expression/1 遍历正确,但未从一种表示映射到另一种表示。您的 arithmetic_operator 也有同样的问题。最小的变化:

arithmetic_operator(plus, +).
arithmetic_operator(minus, -).
arithmetic_operator(times, *).

arithmetic_expression(N, N) :- integer(N).

arithmetic_expression(Term, Expr) :-
    Term =.. [Functor,Component1,Component2],
    arithmetic_operator(Functor, Operator),
    arithmetic_expression(Component1, Expr1),
    arithmetic_expression(Component2, Expr2),
    Expr =.. [Operator, Expr1, Expr2].

然后:

?- arithmetic_expression(plus(minus(8,2), times(4,-3)), Expr).
Expr = 8-2+4* -3 ;
false.

?- arithmetic_expression(plus(minus(8,2), times(4,-3)), Expr),
   Result is Expr.
Expr = 8-2+4* -3,
Result = -6 ;
false.

?- arithmetic_expression(plus(minus(8,2), times(4,-3)), Expr),
   Result is Expr,
   display(Expr).
+(-(8,2),*(4,-3))
Expr = 8-2+4* -3,
Result = -6 ;
false.

display 是上次查询中输出的 +(-(8,2),*(4,-3))