Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

Expressing numerical constraints using Prolog without CLP(FD)

ordering(A, B, C) :-
    integer(A),
    integer(B),
    integer(C),
    A > B,
    B > C,
    A > 0,
    10 > C.

is satisfied by ordering(3,2,1).. But when I leave one or more as variables ordering(3,X,1). it evaluates to false.

What am I missing?

Update: thanks for all the extensive answers. I’ve learned something from all of them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

integer/1 fails if the argument is not an integer, for example if you pass an unbound variable. I believe you should use CLP(FD) for these kind of tasks. Otherwise you may manually bind/test variables in some integer range, using between/3 to set that range.

E.g.:

ordering(A, B, C) :-
   between(0, 100, A),  % A is an integer in the range [0,100]
   between(0, 100, B),  % same for B
   between(0, 100, C),  % and C
   A > B,
   B > C,
   A > 0,
   10 > C.

Sample run:

?- ordering(3,X,1).
X = 2 ;
false.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...