使用 prolog 为算术表达式创建术语的一阶逻辑 [英] first order logic creating terms for arithmetic expressions using prolog

查看:14
本文介绍了使用 prolog 为算术表达式创建术语的一阶逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个签名(0,Z,{plus(2),minus(2),times(2)},常量是整数,函数是加号,减号和乘以arity 2 每个我想写一个两个谓词 arth/2printarth/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 和 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/2printarth/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/2eval_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 为算术表达式创建术语的一阶逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆