Why doesn't ord_subset generate subsets?

This seems to have resolved in the meantime, but maybe still helpful info that I didn’t find in the thread.

Why doesn’t ord_subset generate subsets?

The technically correct answer is that it is not meant to do that. If you look at the docs, you will see that it says: ord_subset(+Sub, +Super). The plusses there mean that both arguments are “input” arguments. The meaning of those annotations is discussed in the docs as Notation of Predicate Descriptions.

You have a solution that works already. Another idea: since library(ordsets) represents a set as a sorted list without duplicates, a subset is a subsequence of that list. A simple implementation of a list subsequence in Prolog without arithmetic or CLP(FD) could go like this:

list_subseq(L, S) :-
    length(L, N),
    between(0, N, M),
    length(S, M),
    list_subseq_1(S, L).

list_subseq_1([], _).
list_subseq_1([X|Xs], L) :-
    append(_, [X|Rest], L),
    list_subseq_1(Xs, Rest).

This enumerates in order of subsequence length:

?- list_subseq([a,b,c], S).
S = [] ;
S = [a] ;
S = [b] ;
S = [c] ;
S = [a, b] ;
S = [a, c] ;
S = [b, c] ;
S = [a, b, c] ;
false.

?- list_subseq(L, S), numbervars(L-S).
L = S, S = [] ;
L = [A], S = [] ;
L = S, S = [A] ;
L = [A, B], S = [] ;
L = [A, B], S = [A] ;
L = [A, B], S = [B] ;
L = S, S = [A, B] ;
L = [A, B, C], S = [] ;
L = [A, B, C], S = [A] ;
L = [A, B, C], S = [B] ;
L = [A, B, C], S = [C] ;
L = [A, B, C], S = [A, B] ;
L = [A, B, C], S = [A, C] ;
L = [A, B, C], S = [B, C] ;
L = S, S = [A, B, C] ; % and so on

Of course I have no idea how this helps you with your larger problem at hand.

1 Like