Consider some monad transformers stack, say
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
...
newtype J = J { runJ :: ErrorT Foo (StateT Bar IO) a } deriving (Applicative, Functor, etc)
And some functions in J
:
peekNextQuux :: J Quux
peekNextQuux = ...
withJ :: J a -> IO (Either Foo a)
withJ = ...
Then I found myself inside J
context. I can write
f = withJ $ peekNextQuux >>= liftIO . print
Now I want to peek and print quuxes inside separate thread inside J
context
g = withJ . liftIO . forkIO . forever $ peekNextQuux >>= liftIO . print
Which obviously won't work. I guess there is some way to solve such a simple problem, just can't figure it out.