1

I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground.

Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
user1283759
  • 51
  • 1
  • 2

1 Answers1

6

Since you use SICStus Prolog you can use the subseq0(+Sequence, ?SubSequence) from library(lists), which "is true when SubSequence is a subsequence of Sequence, but may be Sequence itself" (Quoting from the manual http://www.sics.se/sicstus/docs/4.0.2/html/sicstus/lib_002dlists.html).

      ?- setof(X, subseq0([a,b,c],X), Xs).
      Xs = [[],[a],[a,b],[a,b,c],[a,c],[b],[b,c],[c]]

If you are not allowed to use library predicates you can implement the subseteq0 as explained in gnu Prolog powerset modification, which I quote here for the sake of completeness (with thanks to gusbro)

powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).
Community
  • 1
  • 1
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32
  • 1
    I just noted [this odd terminological problem](http://en.wikipedia.org/wiki/Subsequence#Substring_vs._subsequence): A sequence and a string are often used synonymously, but a subsequence and a substring are not identical... – false Apr 26 '12 at 21:39
  • 1
    Somehow I'm used to this naming convention but indeed it is not very consistent. – Alexander Serebrenik Apr 27 '12 at 08:49