0

I'm wanting to create an interleaved thread with suspended functions, which I can pass a single function at the time of invocation.

Basic continuation type:

data ContT m a = ContT {runContT :: (a -> m a) -> m a}

suspendedFunction :: Int -> ContT Maybe Int
suspendedFunction i = ContT $ \todo -> todo i

calculation :: Int -> Maybe Int --(a -> m a)
calculation i = Just 100

Continuations with Poor Mans Concurrency:

data C m a = Atomic (m (C m a)) | Done a

instance Monad m => Monad (C m) where
  (>>=) (Atomic m) f      = Atomic $ (liftM (>>= f) m)
  (>>=) (Done a) f        = f a
  return                  = Done

atomic :: Monad m => m a -> C m a
atomic m = Atomic $ liftM Done m

interleave :: Monad m => C m a -> C m a -> C m a
interleave (Atomic m1) (Atomic m2) = do
  n1 <- atom $ m1
  n2 <- atom $ m2
interleave n1 n2
interleave m1 (Done _)             = m1
interleave (Done _) m2             = m2

runInterleavedThread :: Monad m => C m a -> m a
runInterleavedThread m = m >>= runInterleaved

createSuspendedThread :: Int -> C (ContT Maybe) Int
createSuspendedThread i = atomic $ suspendedFunction i

main = do
 let inter = interleave (createSuspendedThread 1) (createSuspendedThread 2)
 let complete = runContT $ runThreads inter $ calculation

Error:

 "No instance for (Monad (ContT Maybe))
    arising from a use of ‘atomic’"

Am I missing something basic here, can anyone shed some light?

Babra Cunningham
  • 2,949
  • 1
  • 23
  • 50
  • 3
    `"No instance for (Monad (ContT Maybe))` Is there one? I don't see an instance for this anywhere. – Lazersmoke Mar 17 '17 at 20:10
  • 1
    If you don't want to define your own instance, you should use [`Control.Monad.Trans.Cont`](https://hackage.haskell.org/package/transformers-0.5.4.0/docs/Control-Monad-Trans-Cont.html) – luqui Mar 19 '17 at 16:13

0 Answers0