I want a Bag container which hides its 'real' order from its clients.
It also must be fully polymorphic, that is shouldn't require any constraints over its element type.
I found at least three implementations of bags: Bag module from ghc package, Data.Bag from bag and Math.Combinatorics.Multiset from multiset-comb.
However, they all have toList and fold* operations which expose the internal order of elements which may depend on implementation details or the order of bag construction.
toList is impossible, at least with type Bag a -> [a]. However, folding does not always expose the order.
For example, fold (+) 0 does not expose.
The question is, how should I design the folding interface? Is there a necessary and sufficient condition for safety of the a -> a -> a folding function? As fmap does not expose the order, is there a loss of genericity from folding with a -> b -> b?
I'm thinking of commutative monoids - they seem sufficient, but I'm not sure if associativity and identity element are necessary.