module examples.MyCont where
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 Monad (ContT r m) where pure x = contT ($ x) m >>= k = contT $ \c -> runContT m (\x -> runContT (k x) c)
contT :: ((a -> m r) -> m r) -> ContT r m acontT f = ContT { runContT = f }
runContT :: ContT r m a -> ((a -> m r) -> m r)runContT c = ContT.runContT c
callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m acallCC f = ContT $ \c -> ContT.runContT (f (\a -> ContT $ \_ -> c a)) c
data Data = native java.util.Date where native new :: () -> IO (MutableIO Data) native toString :: Mutable s Data -> ST s String
current :: IO Stringcurrent = do d <- Data.new () d.toString
fun :: Int -> IO Stringfun = (`runContT` id) $ do callCC $ \exit1 -> do exit1 $ liftIO current
--
You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-l...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-l...@googlegroups.com.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
There are no official rules beyond what is common practice in the open-source community.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Frege Programming Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-language+unsub...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to frege-programming-l...@googlegroups.com.
Hi,
module examples.MyCont where
import Data.Charimport 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 acallCC 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 Stringcurrent = do
d <- Date.new () d.toString
runContT = ContT.runContT
fun4 n = (`runContT` return) $ do str <- callCC $ \exit1 -> do d <- liftIO current when (n < 10) (exit1 $ d) let ns = map digitToInt (unpacked (show (n `div` 2))) n' <- callCC $ \exit2 -> do when ((length ns) < 3) (exit2 (length ns)) when ((length ns) < 5) (exit2 n) when ((length ns) < 7) $ do let ns' = map intToDigit (reverse ns) exit1 $ packed $ (dropWhile (== '0') ns') return $ sum ns return $ "(ns = " ++ (show ns) ++ ") " ++ (show n') return $ "Answer: " ++ str
main = do fun4 9 >>= putStrLn fun4 199 >>= putStrLn fun4 19999 >>= putStrLn fun4 1999999 >>= putStrLn fun4 2000000 >>= putStrLn fun4 3000000 >>= putStrLn return ()
fun5 = (`runContT` return) $ do lift $ putStrLn "alpha" (k, num) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ putStrLn "beta" lift $ putStrLn "gamma" if num < 5 then k (num + 1) >> return () else lift $ print num
fun6 n = (`runContT` return) $ do liftIO $ current
callCC $ \exit1 -> do
when (n < 10) (exit1 "< 10") return "> 10"
test6_1 = do fun6 10 >>= putStrLn
test6_2 = do fun6 4 >>= putStrLn
fun7 n = (`runContT` return) $ do
callCC $ \exit1 -> do
liftIO $ putStrLn "I am here!" when (n < 10) (exit1 3) return 4
test7_1 = do fun7 10 >>= putStrLn . show
test7_2 = do fun7 4 >>= putStrLn . show
Hi,