Prolog - 谓词中的运算符 [英] Prolog - Operator in predicate

查看:72
本文介绍了Prolog - 谓词中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在谓词的参数中包含运算符(如 +、>、=、!=、>= 等)(以下示例只是演示,没有多大用处)?

Is it possible to include operators (like +, >, =, !=, >= etc.) in argument for a predicate (examples below are just a demonstraition and do not have much use)?

test(A > B) :- A > B.
test(A >= B) :- A < B.

似乎有效,但是:

test(A != B) := A > B.
test(A <> B) := A < B.

没有 - 为什么会这样?为什么有时可以包括运算符,有时不能?我怎样才能使 test(A != B) := A >B. 工作吗?

Does not - why is that? Why sometimes the operators can be included and sometimes not? How can I make test(A != B) := A > B. work?

我在 sicstus 下工作.

I am working under sicstus.

推荐答案

符号 !=<>:=不是 Prolog 中的运算符.您需要使用 op/3 指令使它们成为运算符.Prolog 在解析您的代码之前必须看到运算符声明.

The symbols !=, <> and := are not operators in Prolog. You need to make them operators, with the op/3 directive. The operator declaration must be seen by Prolog before it parses your code.

字符序列!=在Prolog中不是记号,所以需要用单引号括起来.

The character sequence != is not a token in Prolog, so you need to surround it with single quotes.

:- op(700, xfx, '!=').
:- op(700, xfx, <>).
:- op(1100, xfx, :=).

test(A '!=' B) := A > B.
test(A <> B) := A < B.

上面定义了一个包含两个子句的谓词.子句没有主体,谓词的名称是 := 及其元数 2.完全一样:

The above defines a predicate with two clauses. The clauses has no bodies, the name of the predicate is := and its arity 2. It is exactly the same as:

:=(test('!='(A,B)), >(A,B)).
:=(test(<>(A,B)), <(A,B)).

这篇关于Prolog - 谓词中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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