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

From: Choughule, Yogesh <Yogesh.Choughule_at_...390...>
Date: Mon, 15 Dec 2014 09:16:47 +0000
Hi Matthew 

Thank you for your explanation in detail.

Regards

Yogesh C.


________________________________________
From: mskala_at_...206... [mskala@...206...]
Sent: 15 December 2014 17:55:18
To: Choughule, Yogesh
Cc: Kish Shen; eclipse-clp-users_at_lists.sourceforge.net
Subject: Re: [eclipse-clp-users] Question regarding the redefined variable

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/

______________________________________________________________________
Disclaimer: This email and any attachments are sent in strictest confidence
for the sole use of the addressee and may contain legally privileged,
confidential, and proprietary data. If you are not the intended recipient,
please advise the sender by replying promptly to this email and then delete
and destroy this email and any attachments without any further use, copying
or forwarding.
Received on Mon Dec 15 2014 - 09:16:59 CET

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:21 CEST