4

Here is my failed attempt at the problem any help would be appreciated.

I tried to come up with the best algo for the power set that worked on eager lists. This part seems to be working fine. The part I'm having trouble with is translating it to work with Sequences so it can run it on streaming\infinite lists. I really don't like the yield syntax maybe because I don't understand it well but I would rather have it without using the yield syntax as well.

//All Combinations of items in a list
//i.e. the Powerset given each item is unique
//Note: lists are eager so can't be used for infinite
let listCombinations xs =
    List.fold (fun acc x ->
        List.collect (fun ys -> ys::[x::ys]) acc) [[]] xs

//This works fine (Still interested if it could be faster)
listCombinations [1;2;3;4;5] |> Seq.iter (fun x -> printfn "%A" x)

//All Combinations of items in a sequence
//i.e. the Powerset given each item is unique
//Note: Not working
let seqCombinations xs =
    Seq.fold (fun acc x ->
        Seq.collect (fun ys -> 
            seq { yield ys
                  yield seq { yield x
                              yield! ys} }) acc) Seq.empty xs

//All Combinations of items in a sequence
//i.e. the Powerset given each item is unique
//Note: Not working (even wrong type signature)
let seqCombinations2 xs =
    Seq.fold (fun acc x ->
        Seq.collect (fun ys ->
            Seq.append ys (Seq.append x ys)) acc) Seq.empty xs

//Sequences to test on
let infiniteSequence = Seq.initInfinite (fun i -> i + 1)
let finiteSequence = Seq.take 5 infiniteSequence

//This should work easy since its in a finite sequence
//But it does not, so their must be a bug in 'seqCombinations' above
for xs in seqCombinations finiteSequence do
    for y in xs do
        printfn "%A" y

//This one is much more difficult to get to work
//since its the powerset on the infinate sequence
//None the less If someone could help me find a way to make this work
//This is my ultimate goal
let firstFew = Seq.take 20 (seqCombinations infiniteSequence)
for xs in firstFew do
    for y in xs do
        printfn "%A" y
Aaron Stainback
  • 3,489
  • 2
  • 31
  • 32
  • 2
    There are 2^N elements in the powerset of a set of size N, so even for a set of just 60 elements you're looking at a powerset size of ~10^18 elements, which you'll never be able to completely enumerate. It's possible that you could use less memory by using sequences instead of lists, but it would be helpful if you could better explain your goals. – kvb Jan 10 '12 at 01:20
  • My goal is to see how far I can push lazy eval sequences in f# and this seemed like a simple yet complex enough problem to start with. I want to make the power set of the infinite set in the absolute laziest way while still keeping some elegance in the code and okay performance. – Aaron Stainback Jan 10 '12 at 01:59
  • 1
    That's fine, but given that there are an unenumerably high number of elements in the powerset of even a modestly sized set, the order in which the entries are produced will be important if you want to do anything with the results, but you haven't specified an order. – kvb Jan 10 '12 at 14:56
  • What order do you suggest returning the values in. Can they be returned in an indexable fashion? Actually could just the indexes be calculated based on input? Thanks. – Aaron Stainback Jan 10 '12 at 21:58
  • It depends on what you want to do with the resulting sets - you could do it in increasing order of size, decreasing order of size, some sort of lexicographic order (all sets without the first element first, followed by all sets with it), etc. If you have some problem you're trying to solve, then perhaps there's a natural order. I just wanted to point out that there's not much point in having an infinite powerset if you have no way to get to the items you care about in a bounded amount of time. – kvb Jan 11 '12 at 02:00
  • In terms of an indexed representation, given any number in [0, 2^N), you can easily get a single item from the powerset of a set of size N: the item has element i if and only if the ith bit of the number is 1. However, this doesn't easily extend to infinite sets. – kvb Jan 11 '12 at 02:53
  • @kvb What is a problem I could solve with an "ordered" power set to give me a feel of how the problem would influence the order I choose? If there is an easy one you can think of off the top of your head :) Thanks. – Aaron Stainback Jan 11 '12 at 03:07
  • 1
    I can't think of a concrete example offhand, but maybe you are looking for a minimal set obeying some property (and you know one exists). Of course, with the powerset of an infinite set, you can't go in strictly increasing order (or you'll just reproduce Daniel's tongue in cheek solution of enumerating the one-element subsets), but you might be able to find some order that you know will hit the instances you care about. – kvb Jan 11 '12 at 03:20

3 Answers3

6

Your seqCombinations is almost correct, but you didn't translate it from lists to sequences properly. The equivalent of [[]] is not Seq.empty, but Seq.singleton Seq.empty:

let seqCombinations xs =
    Seq.fold (fun acc x ->
        Seq.collect (fun ys -> 
            seq { yield ys
                  yield seq { yield x
                              yield! ys} }) acc) (Seq.singleton Seq.empty) xs

The code above works for finite sequences. But for infinite ones, it doesn't work, because it first tries to reach the end, which it obviously never does for infinite sequences.

If you want a function that will work with infinite sequences I managed to figure out two ways, but neither of them is particularly nice. One of them uses mutable state:

let seqCombinations xs =
    let combs = ref [[]]
    seq {
        yield! !combs
        for x in xs do
            let added = List.map (fun ys -> x::ys) !combs
            yield! added
            combs := !combs @ added
    }

The other is too much about dealing with details of seq<T>:

open System.Collections.Generic

let seqCombinations (xs : seq<_>) =
    let rec combs acc (e : IEnumerator<_>) =
        seq {
            if (e.MoveNext()) then
                let added = List.map (fun ys -> (e.Current)::ys) acc
                yield! added
                yield! combs (acc @ added) e }

    use enumerator = xs.GetEnumerator()
    seq {
        yield []
        yield! combs [[]] enumerator
    }

I think this would be much easier if you could treat infinite sequences as head and tail, like finite lists in F# or any sequence in Haskell. But it's certainly possible there is a nice way to express this in F#, and I just didn't find it.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Thank you so much this fixes my problem with finite sequences. Do you have any suggestions on making it support infinite sequences? or how to do it without yield syntax? Thanks. – Aaron Stainback Jan 10 '12 at 01:55
  • Thanks so much this is exactly what I am looking for. I'd be interested if anything cleaner comes up. Thanks. – Aaron Stainback Jan 10 '12 at 03:39
  • @AaronStainback Have a look at this gist of an infinite sequence of Fibonnaci numbers, it doesn't use lists as that might be inefficient because appending at the end of a list needs to rebuild the entire list: https://gist.github.com/1159257. The Seq.cache is important for efficiency reasons. The let rec binding will generate a warning as this isn't a function. – David Grenier Jan 11 '12 at 16:03
3

I've asked a similar question recently at Generate powerset lazily and got some nice answers.

For powerset of finite sets, the answer by @Daniel in the above link is an efficient solution and probably suits your purpose. You can come up with a test case to compare between his approach and yours.

Regarding powerset of infinite sets, here is a bit of maths. According to Cantor's theorem, the power set of a countably infinite set is uncountably infinite. It means there is no way to enumerate powerset of all integers (which is countably infinite) even in a lazy way. The intuition is the same for real numbers; since real number is uncountably infinite, we can't actually model them using infinite sequences.

Therefore, there is no algorithm to enumerate powerset of a countably infinite set. Or that kind of algorithm just doesn't make sense.

Community
  • 1
  • 1
pad
  • 41,040
  • 7
  • 92
  • 166
  • All of the solutions seem to work with `list`, not `seq`, so you can't use them to work with infinite sequences (at least not directly). – svick Jan 10 '12 at 12:52
  • You're right. IMO, finding powerset of an infinite sequence is a bit weird. Modeling powerset as a lazy sequence of list (or finite sequence) seems more reasonable. – pad Jan 10 '12 at 16:32
  • I agree that it is weird, but that is the question as it stands. – svick Jan 10 '12 at 17:09
  • That is great, weird is what I was going for. Trying to see what it's like to have to write every function to handle getting an infinite input. Thanks for the help. – Aaron Stainback Jan 10 '12 at 21:49
  • Wow that really puts it into words that I never could, it's uncountably infinite. Thanks for the info. – Aaron Stainback Jan 11 '12 at 00:01
1

This is sort of a joke, but will actually generate the correct result for an infinite sequence (it's just that it can't be proven--empirically, not mathematically).

let powerset s =
  seq {
    yield Seq.empty
    for x in s -> seq [x]
  }
Daniel
  • 47,404
  • 11
  • 101
  • 179
  • In what way does this compute the value? When I try to update the code to your definition then the following seqCombinations infiniteSequence fails with type seq cannot be converted into int. I really wish I could get it this small. Thanks. – Aaron Stainback Jan 10 '12 at 21:55
  • @AaronStainback: Like I said, it's a bit of a joke. Since _powerset(S)_ includes the empty set and _S_ itself the result--as far as you care to observe it--is correct. – Daniel Jan 10 '12 at 22:01
  • As kvb commented, since the result can be only partially enumerated, it's meaningless without specifying an order. – Daniel Jan 10 '12 at 22:05
  • Thank you so much for this "joke" I see what you mean that order is very important now. I also see how this is cheating :) This has made me understand this subject better, thanks. Anyway to index these results in some meaningful fashion that you know of? So you could just spit in an x and y and it would pop out the item number from the original sequence. Is their some accepted meaningful order to the power set? Thanks. – Aaron Stainback Jan 10 '12 at 22:27
  • I'm no expert, but according to the [Wikipedia article](http://en.wikipedia.org/wiki/Power_set), _inclusion_ is a common ordering (which this "solution" satisfies). – Daniel Jan 10 '12 at 22:34
  • Surely it satisfies because you can never get out of the range of (mathematical) integer, for which you create sets consisting only one element. – pad Jan 10 '12 at 22:49