Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is #= in Prolog

Tags:

prolog

clpfd

The operator #= is mentioned on some page, e.g. https://www.metalevel.at/prolog but not on most other pages, e.g.: http://www.swi-prolog.org/pldoc/man?section=operators

What does this operator mean?

like image 365
rnso Avatar asked Jul 03 '16 16:07

rnso


1 Answers

Operators are simply syntactical sugar for predicates: if you write X #= Y, it is short for #=(X,Y), so lookup the predicate (#=)/2.

The operator is mentioned as a predicate in the 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).

They are part of the Constraint Logic Programming on Finite Domains (CLP(FD)) package. One advantage of this constraint over the (is)/2 operator, is that it can be used in multiple directions. For instance:

?- use_module(library(clpfd)).
true.

?- 4 #= 2*Y.
Y = 2.

?- X #= 2*16.
X = 32.

Furthermore, constraints can be delayed. For example:

?- X #= 2*Y, Y #= 14.
X = 28,
Y = 14.

For a more extensive introduction read this clpfd primer by @mat.

like image 119
Willem Van Onsem Avatar answered Nov 03 '22 16:11

Willem Van Onsem