Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does #= mean in Prolog?

Tags:

prolog

clpfd

I recently saw a program in Prolog using the predicate #=/2. I looked this up on the SWI prolog website where they defined it as

The arithmetic expression X equals Y. When reasoning over integers, replace is/2 by #=/2 to obtain more general relations.

What I don't understand about this is how #=/2 can be more 'general' since it is only for integers.

like image 360
IIM Avatar asked Mar 13 '23 17:03

IIM


1 Answers

From the documentation entry page:

They implement pure relations between integer expressions and can be used in all directions

Just an example:

?- X+3 #= X*2.
X = 3.

Looks simple, but actually it's rather difficult to obtain such result within conventional arithmetic expression evaluation.

?- X+3 is X*2.
ERROR: is/2: Arguments are not sufficiently instantiated

From the is/2 page, you can see the signature

-Number is +Expr

where +Expr means it must be ground.

Also, the left argument of is/2 actually should be atomic:

?- 3+3 is 3*2.
false.

despite we know the above statement should be true...

A note: the name CLP(FD) is a bit an 'understatement', as pointed out by @false, could be named CLP(Z), since finiteness of domain can often be relaxed.

like image 193
CapelliC Avatar answered Mar 20 '23 15:03

CapelliC