@@ -102,6 +102,14 @@ type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
103------------------------------------------------------------------------------
103------------------------------------------------------------------------------
105+-- | Appends the results of two AsyncT computations in order.
106+instance Monad m => Monoid (AsyncT m a) where
107+ mempty = AsyncT $ \_ stp _ -> stp
108+ mappend (AsyncT m1) m2 = AsyncT $ \ctx stp yld ->
109+ m1 ctx ((runAsyncT m2) ctx stp yld) $ \a c r ->
110+ let yield x = yld a c (Just x)
111+ in maybe (yield m2) (\rx -> yield $ mappend rx m2) r
105-- We do not use bind for parallelism. That is, we do not start each iteration
113-- We do not use bind for parallelism. That is, we do not start each iteration
106-- of the list in parallel. That will introduce too much uncontrolled
114-- of the list in parallel. That will introduce too much uncontrolled
107-- parallelism. Instead we control parallelism using <|>, it is used for
115-- parallelism. Instead we control parallelism using <|>, it is used for
@@ -114,13 +122,6 @@ type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
114-- thread or not based on the level of the bind in the tree. Maybe at some
122-- thread or not based on the level of the bind in the tree. Maybe at some
115-- point we shall be able to do that?
123-- point we shall be able to do that?
117−-- XXX make this (<>)
118−serially :: AsyncT m a -> AsyncT m a -> AsyncT m a
119−serially (AsyncT m1) m2 = AsyncT $ \ctx stp yld ->
120− m1 ctx ((runAsyncT m2) ctx stp yld) $ \a c r ->
121− let yield x = yld a c (Just x)
122− in maybe (yield m2) (\rx -> yield $ serially rx m2) r
124instance Monad m => Monad (AsyncT m) where
125instance Monad m => Monad (AsyncT m) where
125 return a = AsyncT $ \ctx _ yld -> yld a ctx Nothing
126 return a = AsyncT $ \ctx _ yld -> yld a ctx Nothing
@@ -130,7 +131,7 @@ instance Monad m => Monad (AsyncT m) where
130 m Nothing stp $ \a _ r ->
131 m Nothing stp $ \a _ r ->
131 let run x = (runAsyncT x) Nothing stp yld
132 let run x = (runAsyncT x) Nothing stp yld
132 next = f a
133 next = f a
133− in maybe (run next) (\rx -> run $ serially next (rx >>= f)) r
134+ in maybe (run next) (\rx -> run $ next <> (rx >>= f)) r
135------------------------------------------------------------------------------
136------------------------------------------------------------------------------
136-- Functor
137-- Functor
@@ -299,12 +300,6 @@ instance MonadAsync m => MonadPlus (AsyncT m) where
299 mzero = empty
300 mzero = empty
300 mplus = (<|>)
301 mplus = (<|>)
302−-- XXX Move it in the beginning
303−-- | Appends the results of two AsyncT computations in order.
304−instance MonadAsync m => Monoid (AsyncT m a) where
306− mappend x y = serially x y
308------------------------------------------------------------------------------
303------------------------------------------------------------------------------
310------------------------------------------------------------------------------
305------------------------------------------------------------------------------
@@ -363,7 +358,7 @@ instance MonadThrow m => MonadThrow (AsyncT m) where
363-- MonadRecorder
358-- MonadRecorder
364------------------------------------------------------------------------------
359------------------------------------------------------------------------------
366−instance (Monad m, MonadRecorder m) => MonadRecorder (AsyncT m) where
361+instance MonadRecorder m => MonadRecorder (AsyncT m) where
367 getJournal = lift getJournal
362 getJournal = lift getJournal
368 putJournal = lift . putJournal
363 putJournal = lift . putJournal
369 play = lift . play
364 play = lift . play