Prolog 中的 #= 是什么
What is #= in Prolog
运算符 #=
在某些页面上被提及,例如https://www.metalevel.at/prolog but not on most other pages, e.g.: http://www.swi-prolog.org/pldoc/man?section=operators
这个运算符是什么意思?
运算符只是谓词的语法糖:如果你写 X #= Y
,它是 #=(X,Y)
的缩写,所以查找谓词 (#=)/2
.
运算符在 SWI-Prolog documentation 中作为谓词提到:
The arithmetic expression X
equals Y
. When reasoning over integers, replace (is)/2
by (#=)/2
to obtain more general relations. See declarative integer arithmetic (section A.8.3).
它们是有限域上的约束逻辑编程 (CLP(FD)) 包的一部分。 constraint 相对于 (is)/2
运算符的一个优点是它可以在多个方向上使用。例如:
?- use_module(library(clpfd)).
true.
?- 4 #= 2*Y.
Y = 2.
?- X #= 2*16.
X = 32.
此外,约束可以延迟。例如:
?- X #= 2*Y, Y #= 14.
X = 28,
Y = 14.
有关更广泛的介绍,请阅读
@mat.
的 clpfd 入门
运算符 #=
在某些页面上被提及,例如https://www.metalevel.at/prolog but not on most other pages, e.g.: http://www.swi-prolog.org/pldoc/man?section=operators
这个运算符是什么意思?
运算符只是谓词的语法糖:如果你写 X #= Y
,它是 #=(X,Y)
的缩写,所以查找谓词 (#=)/2
.
运算符在 SWI-Prolog documentation 中作为谓词提到:
The arithmetic expression
X
equalsY
. When reasoning over integers, replace(is)/2
by(#=)/2
to obtain more general relations. See declarative integer arithmetic (section A.8.3).
它们是有限域上的约束逻辑编程 (CLP(FD)) 包的一部分。 constraint 相对于 (is)/2
运算符的一个优点是它可以在多个方向上使用。例如:
?- use_module(library(clpfd)). true. ?- 4 #= 2*Y. Y = 2. ?- X #= 2*16. X = 32.
此外,约束可以延迟。例如:
?- X #= 2*Y, Y #= 14.
X = 28,
Y = 14.
有关更广泛的介绍,请阅读 @mat.
的 clpfd 入门