I'm learning Prolog but I came across this questions when learning about database manipulation. when I ask the Prolog interpreter:
findall(X,subset(X,[1]),P).
the only subset give is
P = [[]].
Why is this?
I'm learning Prolog but I came across this questions when learning about database manipulation. when I ask the Prolog interpreter:
findall(X,subset(X,[1]),P).
the only subset give is
P = [[]].
Why is this?
In SWI-Prolog, the predicate subset/2 is defined as:
% subset(+SubSet, +Set)
subset([], _) :- !.
subset([E|R], Set) :-
memberchk(E, Set),
subset(R, Set).
To obtain the desired result, you can use the following alternative definition:
% sub_set(?SubSet, +Set)
sub_set([], []).
sub_set(SubSet, [_|Set]) :- sub_set(SubSet, Set).
sub_set([X|SubSet], [X|Set]) :- sub_set(SubSet, Set).
Examples:
?- sub_set(S,[a,b,c]).
S = [] ;
S = [c] ;
S = [b] ;
S = [b, c] ;
S = [a] ;
S = [a, c] ;
S = [a, b] ;
S = [a, b, c] ;
false.
?- sub_set([a,c],[a,b,c]).
true ;
false.
?- findall(SubSet, sub_set(SubSet,[1]), PowerSet).
PowerSet = [[], [1]].