module examples.MyCont where
import frege.control.monad.State
import Data.Char
import Control.Concurrent as C
class MonadIO m where
--- Lift a computation from the 'IO' monad.
liftIO :: IO a -> m a
class MonadTrans t where
--- Lift a computation from the argument monad to the constructed monad.
lift :: Monad m => m a -> t m a
instance MonadIO IO where
liftIO io = io
instance MonadTrans (ContT r) where
lift m = ContT (m >>=)
instance (Monad m, MonadIO m) => MonadIO (ContT r m) where
liftIO = lift . liftIO
data ContT r m a = ContT { runContT :: (a -> m r) -> m r }
instance Functor (ContT r m) where
fmap f m = ContT $ \c -> ContT.runContT m (c . f)
instance Applicative (ContT r m) where
pure x = ContT ($ x)
f <*> v = ContT $ \c -> ContT.runContT f $ \g -> ContT.runContT v (c . g)
m *> k = m >>= \_ -> k
instance Monad (ContT r m) where
pure x = ContT ($ x)
m >>= k = ContT $ \c -> ContT.runContT m (\x -> ContT.runContT (k x) c)
callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m a
callCC f = ContT $ \c -> ContT.runContT (f (\a -> ContT $ \_ -> c a)) c
data Date = native java.util.Date where
native new :: () -> IO (MutableIO Date)
native toString :: Mutable s Date -> ST s String
current :: IO String
current = do
d <- Date.new ()
d.toString
runContT = ContT.runContT
spawn = callCC $ \k -> do
lift $ putStrLn "begin capture current continuation ..."
(r, s) <- callCC $ \next -> do
callCC $ \k2 -> do
let f x = k2 (f, x)
k (f, "jump to 1")
lift $ putStrLn ("can we be here1? -- " ++ s)
(r, s) <- callCC $ \next -> do
callCC $ \k2 -> do
let f x = k2 (f, x)
k (f, "jump to 2")
lift $ putStrLn ("can we be here2? -- " ++ s)
(r, s) <- callCC $ \next -> do
callCC $ \k2 -> do
let f x = k2 (f, x)
k (f, "jump to 3")
lift $ putStrLn ("can we be here3? -- " ++ s)
return (r, s)
fun1 = (`runContT` return) $ do
lift $ putStrLn "alpha"
(k, s) <- spawn
lift $ putStrLn ("in fun1 -- " ++ s)
lift $ Thread.sleep (2000L)
k "I try to pass some string here"
lift $ putStrLn "no chance to be there"