问题描述
给定一个签名(0,Z,{plus(2),minus(2),times(2)}
,常量是整数,函数是加号、减号和乘以arity 2
对于每个.我想编写两个谓词 arth/2
和 printarth/1
,它们采用上述签名中的术语并进行必要的算术计算加法、减法和乘法.arth/2
将打印结果,printarth/1
应得出如下所示的评估表达式.
given a signature (0,Z,{plus(2),minus(2),times(2)}
, constants are integers and functions are plus, minus and times with arity 2
for each. I wanted to write a two predicates arth/2
and printarth/1
which takes terms in the above signature and do the necessary arithmetic calculations addition, subtraction and multiplication.arth/2
will print the results and printarth/1
should results out the evaluation expression as shown below.
我想实现两件事
首先:
?- 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
用于此并开始我的代码如下
I understand that the use of Terms, Ops and complex terms
are used for this and started my code as below
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/2
和 printarth/1
很困难,因为我无法调用 arithmetic_expression(Term)
和当我调用它时抛出一个错误.
From here I find it difficult on how to create arth/2
and printarth/1
as I cannot call arithmetic_expression(Term)
and throws me an error when I call it.
?- arithmetic_expression(..[+,5,7]).
ERROR: Syntax error: Operator expected
ERROR: arithmetic_expression(.
ERROR: ** here **
ERROR: .[+,5,7]) .
有关此任务的任何资源都非常有用.
any resources on this task is very useful.
推荐答案
如果你想要一个看起来像这样的术语:
If you want to take a term that looks like this:
minus(2, 3)
并将其转化为等价于2 - 3
的算术表达式-(2, 3)
(默认定义为-
> 作为运算符),然后对其进行评估,您可以这样做:
and turn it into an arithmetic expression -(2, 3)
which is equivalent to 2 - 3
(with the default definition of -
as an operator), then evaluate it, you could do it like this:
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/2
和 eval_arithmetic_expression/2
都有两个参数.这是将 minus(2, 4)
映射到 2 - 4
所需要的.
As you see, both term_arithmetic_expression/2
and eval_arithmetic_expression/2
have two arguments. This is what you need to map minus(2, 4)
to 2 - 4
.
您的 arithmetic_expression/1
正确遍历,但没有从一种表示映射到另一种表示.您的 arithmetic_operator
有同样的问题.更改最少:
Your arithmetic_expression/1
is correctly traversing, but not mapping from the one representation to the other. Your arithmetic_operator
has the same problem. With minimal changes:
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))
的内容.
The display
is what is outputting +(-(8,2),*(4,-3))
in the last query.
这篇关于使用 prolog 为算术表达式创建项的一阶逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!