@@ -84,29 +84,6 @@ instance Monad (AsyncT m) => Functor (AsyncT m) where
84-- Applicative
84-- Applicative
85------------------------------------------------------------------------------
85------------------------------------------------------------------------------
88−-- | Warning: Radically untyped stuff. handle with care
89−getContinuations :: Monad m => StateM m [a -> AsyncT m b]
91− EventF { fcomp = fs } <- get
92− return $ unsafeCoerce fs
94−-- | Save a closure and a continuation ('x' and 'f' in 'x >>= f').
95−setContinuation :: Monad m
96− => AsyncT m a -> (a -> AsyncT m b) -> [c -> AsyncT m c] -> StateM m ()
97−setContinuation b c fs = do
98− modify $ \EventF{..} -> EventF { xcomp = b
99− , fcomp = unsafeCoerce c : fs
102−-- | Restore the continuations to the provided ones.
103−-- | NOTE: Events are also cleared out.
104−restoreStack :: MonadState EventF m => t -> m ()
105−restoreStack fs = modify $ \EventF {..} ->
106− EventF { event = Nothing, fcomp = (unsafeCoerce fs), .. }
110instance Monad m => Applicative (AsyncT m) where
87instance Monad m => Applicative (AsyncT m) where
111 pure a = AsyncT . return $ Just a
88 pure a = AsyncT . return $ Just a
112 m1 <*> m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) }
89 m1 <*> m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) }
@@ -177,56 +154,6 @@ instance (Monoid a, Monad (AsyncT m)) => Monoid (AsyncT m a) where
177 mappend x y = mappend <$> x <*> y
154 mappend x y = mappend <$> x <*> y
178 mempty = return mempty
155 mempty = return mempty
180−------------------------------------------------------------------------------
182−------------------------------------------------------------------------------
185−-- | Run the closure (the 'x' in 'x >>= f') of the current bind operation.
186−runClosure :: EventF -> StateM m (Maybe a)
187−runClosure EventF { xcomp = x } = runAsyncT (unsafeCoerce x)
189−-- | Run the continuation (the 'f' in 'x >>= f') of the current bind operation with the current state.
190−runContinuation :: MonadIO m => EventF -> a -> StateM m (Maybe b)
191−runContinuation EventF { fcomp = fs } =
192− runAsyncT . (compose $ (unsafeCoerce fs))
194−data Backtrack b = Show b => Backtrack
195− { backtracking :: Maybe b
196− , backStack :: [EventF]
199−backStateOf :: (Monad m, Show a) => a -> m (Backtrack a)
200−backStateOf reason = return $ Backtrack (Nothing `asTypeOf` (Just reason)) []
202−-- | Start the undo process for the given undo track id. Performs all the undo
203−-- actions registered till now in reverse order. An undo action can use
204−-- 'forward' to stop the undo process and resume forward execution. If there
205−-- are no more undo actions registered execution stops and a 'stop' action is
208−back :: (MonadIO m, Typeable b, Show b) => b -> AsyncT m a
209−back reason = AsyncT $ do
210− bs <- getData `onNothing` backStateOf reason
215− goBackt (Backtrack _ [] )= return Nothing
216− goBackt (Backtrack _ (stack@(first : bs)) )= do
217− (setData $ Backtrack (Just reason) stack)
219− mr <- runClosure first
221− Backtrack b _ <- getData `onNothing` backStateOf reason
223− Nothing -> return empty
225− Nothing -> runContinuation first x
226− justreason -> goBackt $ Backtrack justreason bs
230------------------------------------------------------------------------------
157------------------------------------------------------------------------------
231-- MonadIO
158-- MonadIO
232------------------------------------------------------------------------------
159------------------------------------------------------------------------------
@@ -284,8 +211,8 @@ waitForChildren chan pendingRef = do
284------------------------------------------------------------------------------
211------------------------------------------------------------------------------
286-- | Run a transient computation with a default initial state
213-- | Run a transient computation with a default initial state
287−runTransient :: forall m a. MonadIO m => AsyncT m a -> m (Maybe a, Context)
214+runContext :: forall m a. MonadIO m => AsyncT m a -> m (Maybe a, Context)
288−runTransient t = do
215+runContext t = do
289 childChan <- liftIO $ atomically newTChan
216 childChan <- liftIO $ atomically newTChan
290 pendingRef <- liftIO $ newIORef []
217 pendingRef <- liftIO $ newIORef []
291 credit <- liftIO $ newIORef maxBound
218 credit <- liftIO $ newIORef maxBound
@@ -304,7 +231,7 @@ runTransient t = do
304--waitAsync :: MonadIO m => AsyncT m a -> m [a]
231--waitAsync :: MonadIO m => AsyncT m a -> m [a]
305waitAsync :: MonadIO m => AsyncT m a -> m (Maybe a)
232waitAsync :: MonadIO m => AsyncT m a -> m (Maybe a)
306waitAsync m = do
233waitAsync m = do
307− (r, _) <- runTransient m
234+ (r, _) <- runContext m
310------------------------------------------------------------------------------
237------------------------------------------------------------------------------
@@ -386,6 +313,56 @@ sandbox mx = do
386 mx <*** modify (\s ->s { mfData = sd})
313 mx <*** modify (\s ->s { mfData = sd})
316+------------------------------------------------------------------------------
318+------------------------------------------------------------------------------
321+-- | Run the closure (the 'x' in 'x >>= f') of the current bind operation.
322+runClosure :: EventF -> StateM m (Maybe a)
323+runClosure EventF { xcomp = x } = runAsyncT (unsafeCoerce x)
325+-- | Run the continuation (the 'f' in 'x >>= f') of the current bind operation with the current state.
326+runContinuation :: MonadIO m => EventF -> a -> StateM m (Maybe b)
327+runContinuation EventF { fcomp = fs } =
328+ runAsyncT . (compose $ (unsafeCoerce fs))
330+data Backtrack b = Show b => Backtrack
331+ { backtracking :: Maybe b
332+ , backStack :: [EventF]
335+backStateOf :: (Monad m, Show a) => a -> m (Backtrack a)
336+backStateOf reason = return $ Backtrack (Nothing `asTypeOf` (Just reason)) []
338+-- | Start the undo process for the given undo track id. Performs all the undo
339+-- actions registered till now in reverse order. An undo action can use
340+-- 'forward' to stop the undo process and resume forward execution. If there
341+-- are no more undo actions registered execution stops and a 'stop' action is
344+back :: (MonadIO m, Typeable b, Show b) => b -> AsyncT m a
345+back reason = AsyncT $ do
346+ bs <- getData `onNothing` backStateOf reason
351+ goBackt (Backtrack _ [] )= return Nothing
352+ goBackt (Backtrack _ (stack@(first : bs)) )= do
353+ (setData $ Backtrack (Just reason) stack)
355+ mr <- runClosure first
357+ Backtrack b _ <- getData `onNothing` backStateOf reason
359+ Nothing -> return empty
361+ Nothing -> runContinuation first x
362+ justreason -> goBackt $ Backtrack justreason bs
389------------------------------------------------------------------------------
366------------------------------------------------------------------------------
390-- More operators, instances
367-- More operators, instances
391------------------------------------------------------------------------------
368------------------------------------------------------------------------------
@@ -399,33 +376,29 @@ instance (Num a, Monad (AsyncT m)) => Num (AsyncT m a) where
399 signum f = f >>= return . signum
376 signum f = f >>= return . signum
402−class AdditionalOperators m where
379+-- | Warning: Radically untyped stuff. handle with care
403−
380+getContinuations :: Monad m => StateM m [a -> AsyncT m b]
404− -- | Run @m a@ discarding its result before running @m b@.
381+getContinuations = do
405− (**>) :: m a -> m b -> m b
382+ EventF { fcomp = fs } <- get
406−
383+ return $ unsafeCoerce fs
407− -- | Run @m b@ discarding its result, after the whole task set @m a@ is
409− (<**) :: m a -> m b -> m a
411− atEnd' :: m a -> m b -> m a
414− -- | Run @m b@ discarding its result, once after each task in @m a@, and
415− -- once again after the whole task set is done.
416− (<***) :: m a -> m b -> m a
418− atEnd :: m a -> m b -> m a
385+-- | Save a closure and a continuation ('x' and 'f' in 'x >>= f').
419− atEnd = (<***)
386+setContinuation :: Monad m
387+ => AsyncT m a -> (a -> AsyncT m b) -> [c -> AsyncT m c] -> StateM m ()
388+setContinuation b c fs = do
389+ modify $ \EventF{..} -> EventF { xcomp = b
390+ , fcomp = unsafeCoerce c : fs
421−instance (Monad m, Monad (AsyncT m)) => AdditionalOperators (AsyncT m) where
393+-- | Restore the continuations to the provided ones.
394+-- | NOTE: Events are also cleared out.
395+restoreStack :: MonadState EventF m => t -> m ()
396+restoreStack fs = modify $ \EventF {..} ->
397+ EventF { event = Nothing, fcomp = (unsafeCoerce fs), .. }
423− (**>) :: AsyncT m a -> AsyncT m b -> AsyncT m b
399+-}
429 (<***) :: AsyncT m a -> AsyncT m b -> AsyncT m a
402 (<***) :: AsyncT m a -> AsyncT m b -> AsyncT m a
430 (<***) ma mb =
403 (<***) ma mb =
431 AsyncT $ do
404 AsyncT $ do
@@ -436,14 +409,6 @@ instance (Monad m, Monad (AsyncT m)) => AdditionalOperators (AsyncT m) where
436 restoreStack fs
409 restoreStack fs
439− (<**) :: AsyncT m a -> AsyncT m b -> AsyncT m a
447infixr 1 <***, <**, **>
412infixr 1 <***, <**, **>
449-- | Run @b@ once, discarding its result when the first task in task set @a@
414-- | Run @b@ once, discarding its result when the first task in task set @a@
@@ -467,10 +432,17 @@ infixr 1 <***, <**, **>
437+-- | Run @m a@ discarding its result before running @m b@.
438+(**>) :: Monad m => AsyncT m a -> AsyncT m b -> AsyncT m b
439+(**>) x y = AsyncT $ do
443+-- | Run @m b@ discarding its result, after the whole task set @m a@ is done.
470(<**) :: Monad m => AsyncT m a -> AsyncT m b -> AsyncT m a
444(<**) :: Monad m => AsyncT m a -> AsyncT m b -> AsyncT m a
471(<**) ma mb = AsyncT $ do
445(<**) ma mb = AsyncT $ do
472 a <- runAsyncT ma
446 a <- runAsyncT ma
473 runAsyncT mb
447 runAsyncT mb