What is #= in Prolog
Asked Answered
A

1

7

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?

Australopithecus answered 3/7, 2016 at 16:19 Comment(0)
P
9

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.

Penniepenniless answered 3/7, 2016 at 16:38 Comment(1)
With SWI Prolog version 8.3.24 after the module charged all the examples give an error: Operator expected. Any solutions? Thxs.Sestina

© 2022 - 2024 — McMap. All rights reserved.