Set
, similarly to []
has a perfectly defined monadic operations. The problem is that they require that the values satisfy Ord
constraint, and so it's impossible to define return
and >>=
without any constraints. The same problem applies to many other data structures that require some kind of constraints on possible values.
The standard trick (suggested to me in a haskell-cafe post) is to wrap Set
into the continuation monad. ContT
doesn't care if the underlying type functor has any constraints. The constraints become only needed when wrapping/unwrapping Set
s into/from continuations:
import Control.Monad.Cont
import Data.Foldable (foldrM)
import Data.Set
setReturn :: a -> Set a
setReturn = singleton
setBind :: (Ord b) => Set a -> (a -> Set b) -> Set b
setBind set f = foldl' (\s -> union s . f) empty set
type SetM r a = ContT r Set a
fromSet :: (Ord r) => Set a -> SetM r a
fromSet = ContT . setBind
toSet :: SetM r r -> Set r
toSet c = runContT c setReturn
This works as needed. For example, we can simulate a non-deterministic function that either increases its argument by 1 or leaves it intact:
step :: (Ord r) => Int -> SetM r Int
step i = fromSet $ fromList [i, i + 1]
-- repeated application of step:
stepN :: Int -> Int -> Set Int
stepN times start = toSet $ foldrM ($) start (replicate times step)
Indeed, stepN 5 0
yields fromList [0,1,2,3,4,5]
. If we used []
monad instead, we would get
[0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5]
instead.
The problem is efficiency. If we call stepN 20 0
the output takes a few seconds and stepN 30 0
doesn't finish within a reasonable amount of time. It turns out that all Set.union
operations are performed at the end, instead of performing them after each monadic computation. The result is that exponentially many Set
s are constructed and union
ed only at the end, which is unacceptable for most tasks.
Is there any way around it, to make this construction efficient? I tried but without success.
(I even suspect that there could be some kinds of theoretical limits following from Curry-Howard isomorphism and Glivenko's theorem. Glivenko's theorem says that for any propositional tautology φ the formula ¬¬φ can be proved in intuitionistic logic. However, I suspect that the length of the proof (in normal form) can be exponentially long. So, perhaps, there could be cases when wrapping a computation into the continuation monad will make it exponentially longer?)
Monad
instance forSet
unless there is also an efficientFunctor
instance. And I'm having a hard time seeing how you can have an efficientfmap
forSet
. The existingmap
forSet
is n * log n.Set
's implemented as strict trees, so laziness won't help you ever either. – LyricismOrd
or evenEq
. – Lissy