问题描述
SWI序言中的#=
和=:=
有什么区别.
我从SWI序言中找到了定义,但仍然对此感到困惑.
http://www.swi-prolog.org/pldoc/man?section=阿里帕德人
http://www.swi-prolog.org /pldoc/man?section = clpfd-arith-constraints
?- 3=:=3.
true.
?- (3-2) =:= (9-8).
true.
?- 3 #= 3.
true.
?- (3-2) #= (9-8).
true.
区别在于#=/2
是CLPFD库运算符(您需要执行:use_module(library(clpfd)).
才能使用它)并且用于算术约束,并且同时包含is/2
和=:=/2
超过整数.这意味着您只能将其用于整数:
例如,使用列表会引发错误:
?- L #= [1,2,3].
ERROR: Domain error: `clpfd_expression' expected, found `[1,2,3]'
(也使用=:=/2
中的列表会引发错误,该列表示例仅用于理解两个运算符都用于表达式!)
对于整数,它可以在可以使用=:=
的任何地方使用,但是如上所述,它可以用作is/2
,这意味着您可以将其用于统一-只需将变量与某个整数值绑定即可,例如:
?- X #= 2.
X = 2.
上面的内容不检查X和2之间的相等性,因为X在无界变量中,它的作用是将X与值2绑定.
这对于=:=/2
运算符是不可能的:
?- X =:= 2.
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [8] _2494=:=2
ERROR: [7] <user>
那是因为=:=/2
仅用于检查是否相等!
这是#=/2
和=:=/2
之间的区别.两者检查两个算术表达式之间的相等性,但是使用=:=/2
所有变量时都应实例化.当将#=/2
与变量一起使用时,这将在这些变量之间设置约束:
?- X #= 2.
X = 2. % constraints X to integer value 2
?- X + Y #= 2.
X+Y#=2. % constraints X,Y such that sum equals to 2 see next example:
?- X + Y #= 2 , X = 3.
X = 3,
Y = -1. % binds Y with -1 in order to succeed the constraint
?- X + Y #= 2 , X = 3 , Y > 0.
false. % false since constraint can't succeed!
如您所见,#=/2
显然是更相关的,因为即使在约束中包含多个变量(例如X + Y #= 2.
),这也会设置X,Y之间的关系,绑定一个变量可能导致对另一个变量的推理. /p>
在您的测试中,您看不出有什么区别,因为所有变量都具有值(例如,它们已实例化),并且您只需检查两个操作符均可实现的相等性.
What is the difference between #=
and =:=
in SWI prolog.
I have found the definition from SWI prolog, but still confused about it.
http://www.swi-prolog.org/pldoc/man?section=arithpreds
http://www.swi-prolog.org/pldoc/man?section=clpfd-arith-constraints
?- 3=:=3.
true.
?- (3-2) =:= (9-8).
true.
?- 3 #= 3.
true.
?- (3-2) #= (9-8).
true.
The difference is that #=/2
is a CLPFD library operator (you need to execute: use_module(library(clpfd)).
in order to use it ) and it is used for arithmetic constraints and subsumes both is/2
and =:=/2
over integers. What this means is that you can use it only for integers:
e.g using list will raise error:
?- L #= [1,2,3].
ERROR: Domain error: `clpfd_expression' expected, found `[1,2,3]'
(Also using lists in =:=/2
will raise error ,the list example is just for understanding that both operators are used for expressions !)
For integers it can be used anywhere =:=
could be used but as mentioned above it can be used as is/2
which means that you can use it for unification - simply to bind a variable with some integer value, for example:
?- X #= 2.
X = 2.
the above does not check for equality between X and number 2 since X in unbounded variable, what it does is bonding X with value 2.
This is not possible with =:=/2
operator:
?- X =:= 2.
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [8] _2494=:=2
ERROR: [7] <user>
That's because =:=/2
is used only to check for equality !!
This is the difference between #=/2
and =:=/2
. Both check for equality between two arithmetic expressions but when using =:=/2
all variables should be instantiated. When using #=/2
with variables this sets constraints between these variables:
?- X #= 2.
X = 2. % constraints X to integer value 2
?- X + Y #= 2.
X+Y#=2. % constraints X,Y such that sum equals to 2 see next example:
?- X + Y #= 2 , X = 3.
X = 3,
Y = -1. % binds Y with -1 in order to succeed the constraint
?- X + Y #= 2 , X = 3 , Y > 0.
false. % false since constraint can't succeed!
As you can see #=/2
is clearly more relational since even when having a constraint with more that one variable e.g X + Y #= 2.
this sets a relation between X,Y, binding one variable can lead to reasoning on the other.
In your tests you see no difference since all your variables have values (e.g they are instantiated) and you simple check for equality which both operators an achieve.
这篇关于SWI序言中#=和=:=有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!