clpz icon indicating copy to clipboard operation
clpz copied to clipboard

Assert domain from set of values

Open lewtds opened this issue 3 years ago • 4 comments

I see from your knight's tour example , you wrote a small utility to convert a list of values to a domain:

foldl(num_to_dom, Nexts, Next, Dom)

where:

num_to_dom(N, D0, D0\/N).

I think this should be part of the library itself. Something like:

Var in_set [1, 2, 3, 4]
Vars ins_set [1, 2, 3, 4]

lewtds avatar Dec 08 '20 15:12 lewtds

Thank you, this is certainly worth considering!

Do you have any concrete use case in mind? In my experience, this need arises quite rarely.

Maybe it would be even more useful to add a predicate like domain_list/2 or list_domain/2, which can be used in both directions, relating a domain to a list of integers? This would be more versatile, and take only a single additional predicate that can be used in conjunction with both (in)/2 and (ins)/2.

triska avatar Dec 08 '20 16:12 triska

I have not played around enough with Prolog and clpfd/z to have a concrete use case right now but from the top of my head, from dealing with other languages, discrete domains happen all the time, like HTTP status code for example, that's why they usually have the Enum type with custom values.

Agree with the list_domain/domain_list suggestion. I think that's a much more general approach!

lewtds avatar Dec 09 '20 08:12 lewtds

OK! Please let's leave this issue open for now, and implement it when there is a clear need for it.

One reason why I do not immediately add it is that every new primitive for reasoning about domains will reduce compatibility with other constraint systems. Another reason is that beginners tend to quickly adopt bad patterns if too many primitives are available. For example, we will quickly see:

list_domain([400,402,404], Dom), X in Dom

which can already now be written more compactly and also more portably as:

X in 400\/402\/404

Would this solve your issue with HTTP status codes?

Also, we will likely even see:

list_domain([1,2,3], Dom), X in Dom

which can be written as:

X in 1..3

I have seen this occur repeatedly in the past. For instance, library(pio) provides phrase_from_file/2, and there are also libraries that provide phrase_from_stream/2. Beginners will quickly find a way to use the most complex and least portable combination of predicates, when a single phrase_from_file/2 would be sufficient.

Therefore, if at all possible, I would like to keep the provided features as simple and as portable as possible. I'm certainly sympathetic to adding list_domain/2, and I will do it once a few good use cases become known. If anyone has them, please provide them.

triska avatar Dec 09 '20 18:12 triska

That's reasonable. At list this issue can serve as a pointer to anyone having the same problem.

lewtds avatar Dec 10 '20 02:12 lewtds