Re: [eclipse-clp-users] Question regarding the redefined variable

From: <mskala_at_...206...>
Date: Mon, 15 Dec 2014 02:55:18 -0600 (CST)
On Mon, 15 Dec 2014, Choughule, Yogesh wrote:
> :-lib(ic).
>
> solve(A, B) :-
>
> A $= 2,         % A defined as 2
> B $= 3 + A,  % A is used to calculate B
> A $= B + 2.  % A takes new value
>
> My question is,
> Is there any way i can define A twice in the same code?

Comma "," doesn't mean "Do this, then do that."  It means "This must be
true AND that must be true also."  So your code above says:

(A = 2) AND (B = A+3) AND (A = B+2).

In principle all those things must be true at the same time; not one after
another.  So A = A+5 by simple algebra.  It is not possible for A to be a
number and that statement to be true (no number is equal to itself plus
five) so the system detects it as a contradiction and returns failure.

Variables in Prolog are like variables in mathematics.  They are not like
variables in Java.  If you want to start with 2, add 3, and then add
2 more to get a sum of 7 using constraint programming, you must write
something more like this:

A $= 2,
B $= 3 + A,
C $= B + 2.

There are other ways to do arithmetic in Prolog, for instance using "is",
but that still won't cause "X AND Y" into "X, but then X stops being true
and Y becomes true instead."

If you want to undo the assignment of 2 to A, that is possible using
backtracking, or non-logical operations like assert and retract, but there
are other concepts you'll need to learn first.

-- 
Matthew Skala
mskala_at_...206...                 People before principles.
http://ansuz.sooke.bc.ca/
Received on Mon Dec 15 2014 - 08:54:05 CET

This archive was generated by hypermail 2.3.0 : Thu Feb 22 2024 - 18:13:20 CET