3

I'm trying to add a constraint global_cardinality to my program and in the manual of SICStus Prolog is written:

global_cardinality(+Xs,+Vals)

global_cardinality(+Xs,+Vals,+Options)

where Xs = [X1,...,Xd] is a list of integers or domain variables, and Vals = [K1-V1,...,Kn-Vn] is a list of pairs where each key Ki is a unique integer and Vi is a domain variable or an integer. True if every element of Xs is equal to some key and for each pair Ki-Vi, exactly Vi elements of Xs are equal to Ki.

Now I can write:

global_cardinality([A,B,C], [1-2, 2-1]).

to say that the number 1 will be used twice. The number 2 will be used just once.

But I would like to say that the number 1 will be used: once, twice or three times

According to the manual I need a domain variable but what is the proper syntax for that?

MartyIX
  • 27,828
  • 29
  • 136
  • 207

2 Answers2

5
?- X in 1..3, global_cardinality([A,B,C], [1-X, 2-1]).
Mats Carlsson
  • 1,426
  • 7
  • 3
1

not sure about this, but from SWI-Prolog page I think you could try

...global_cardinality([A,B,C], [1-X, 2-1]), (X #= 1 #\/ X #= 2 #\/ X #= 2)...

or

?- global_cardinality([A,B,C], [1-X, 2-1]), X in 1..3, label([A,B,C]).
A = B, B = 1,
C = X, X = 2 ;
A = C, C = 1,
B = X, X = 2 ;
A = X, X = 2,
B = C, C = 1.
false
  • 10,264
  • 13
  • 101
  • 209
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Thanks. It works the same way. My ability to google answers for SICStus Prolog is so bad that similar dummy questions will follow :( – MartyIX May 06 '13 at 14:31
  • Is there any chat room with people who are interested in Prolog? I tried IRC channel ##Prolog on Freenode and it is not much active. – MartyIX May 06 '13 at 14:31
  • Sorry, I don't known. But Prolog it's a bit out of fashion, or better, it was hot too much years ago. Then I think it's improbable you can find to chat about it. – CapelliC May 06 '13 at 14:39
  • Yes, Prolog is old but constraint programming is far from being dead as far as I know.. – MartyIX May 06 '13 at 15:03
  • You're right. But I think the SemanticWeb infrastructure will rule. I hope (better, I'm proactive on this) it will revamp some interest. – CapelliC May 06 '13 at 15:14