OmG
OmG

Reputation: 18838

Integer range suspension in prolog

I have the following query:

?- Remainder :: 0..8, Qoutient #:: 0..Dividened,
   Dividened #= Qoutient * 9 + Remainder, Dividened = 12.

As you can see I have an integer suspension Qoutient #:: 0..Dividened, and try to clear the value of the Dividend at the end. However, I get the following error:

instantiation fault in Qoutient #:: 0 .. Dividened

So how can I solve the problem in Eclipse CLP?

Upvotes: 1

Views: 88

Answers (1)

jschimpf
jschimpf

Reputation: 5034

You could write Quotient#>=0, Quotient#=<Dividend, but there is actually no need to give any a-priori bounds on that variable at all. Simply use

?- Remainder :: 0..8, Dividend #= Quotient * 9 + Remainder, Dividend = 12.
Remainder = 3
Dividend = 12
Quotient = 1
Yes (0.00s cpu)

You may want to generalize this for arbitrary Divisors and package the whole thing into an auxiliary predicate, such as

divmod(Dividend, Divisor, Quotient, Remainder) :-
        0 #=< Remainder, Remainder #=< Divisor-1,
        Dividend #= Quotient*Divisor + Remainder.

Then your query becomes

?- divmod(D, 9, Q, R), D = 12.
D = 12
Q = 1
R = 3
Yes (0.00s cpu)

Upvotes: 2

Related Questions