Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic comparison in Prolog

Let's say we have two variables X and Y. X is 53 and Y is 52. What I want to do is compare them by adding 1 to the Y, so that it would be 53 - therefore X would be equal to Y + 1.

I am trying to do it by simply using equal operator and addition for Y variables, like so:

X == Y + 1

Even though this looks simple enough, I am getting false as the result. What am I missing?

like image 827
WheelPot Avatar asked Oct 26 '25 22:10

WheelPot


2 Answers

?- X = 50+2, Y = 50+1, X =:= Y + 1.

as you can see, (=:=)/2 evaluates both sides, as do (>)/2, etc

like image 140
CapelliC Avatar answered Oct 28 '25 23:10

CapelliC


If you are reasoning over integers, use your Prolog system's CLP(FD) constraints to compare and evaluate arithmetic integer expressions.

For example, in SICStus, SWI and YAP, after use_module(library(clpfd):

?- 53 #= 52 + 1. 
true.

This works in all directions.

Other examples:

?- X #= 52 + 1.
X = 53.

?- 53 #= Y + 1.
Y = 52.

?- 53 #= 52 + 1.
true.
like image 31
mat Avatar answered Oct 29 '25 00:10

mat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!