序言中的整数范围暂停

Integer range suspension in prolog

我有以下查询:

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

如你所见,我有一个整数暂停Qoutient #:: 0..Dividened,并尝试在最后清除Dividend的值。但是,我收到以下错误:

instantiation fault in Qoutient #:: 0 .. Dividened

那么如何解决 Eclipse CLP 中的问题?

您可以写成 Quotient#>=0, Quotient#=<Dividend,但实际上根本不需要对该变量给出任何先验界限。只需使用

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

您可能希望将此概括为任意除数并将整个事物打包到辅助谓词中,例如

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

那么你的查询就变成了

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