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.
What's the difference between #= and =:= in SWI prolog ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With