1

Let's assume I have a variable V and value of V can be any number from the range 0..5. However, some values are more preferred than other others therefore it would help me to specify the domain of V as an ordered sequence.

Can I do it in SICStus Prolog?

Example:

% PSEUDOCODE
%
% 3 is more preferred than 4; 4 is more preferred than 2; and so on..
% So I would write something like this: 

V in {3,4,2,5,1,0},
getDomainAsList(V, List), % the predicate do not exist
% and the List would be: [3,4,2,5,1,0] and not [1,2,3,4,5]

I read the manual and I did not find anything that would help. I can solve the problem by custom labeling (i.e., convert the domain of V to a list, sort it and assign a value to V) but I expect worse performance.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • You may have missed the tag "clpfd". If not please tell me how to declare the variable then. – MartyIX Jun 05 '13 at 16:30
  • I know how lists work in Prolog. I have my own labeling for CLPFD variables and I do the following: 1) Take a variable X 2) Find out its domain 3) assign a value to X. Obviously, it would be much faster to assign first value (3 from example) than taking domain of V and sorting it. – MartyIX Jun 05 '13 at 16:36

2 Answers2

3

There is a manual page describing this.

See the value(Enum) option to labeling/2, here:

Mats Carlsson
  • 1,426
  • 7
  • 3
2

You can have an array or list of all the values in the preferred order.

Then you work with array indexes in your program, and at the very end you return values corresponding to the indexes.

Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • This works for one variable. But it is an interesting idea, anyway. – MartyIX Jun 05 '13 at 17:24
  • BTW, may I ask why are you using non-free SICStus Prolog, instead, of, say, free and open source ECLiPSe http://eclipseclp.org/ ? – Sergii Dymchenko Jun 05 '13 at 17:29
  • 1
    ECLiPSe did not provide distinct2 constraint when I started programming my task. However, I found out that distinct2 will be supported in ECLiPSe 6.1 and then I can easily switch. – MartyIX Jun 05 '13 at 17:45