class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a instance MonadCont (ContT r m) where callCC = ContT.callCC
newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r } instance Monad (ContT r m) where return x = ContT ($ x) m >>= k = ContT $ \c -> runContT m (\x -> runContT (k x) c)
证实ContT符合Monad法则: 1. return a >>= f ≡ f a return a >>= f ≡ ContT (\k -> k a) >>= f ≡ ContT $ \c -> runContT (ContT (\k -> k a)) (\x -> runContT (f x) c) ≡ ContT $ \c -> (\k -> k a) (\x -> runContT (f x) c) ≡ ContT $ \c -> (\x -> runContT (f x) c) a ≡ ContT $ \c -> runContT (f a) c ≡ ContT $ runContT (f a) ≡ f a 2. m >>= return ≡ m m >>= return ≡ ContT $ \c -> runContT m (\x -> runContT (return x) c) ≡ ContT $ \c -> runContT m (\x -> runContT (ContT $ \k -> k x) c) ≡ ContT $ \c -> runContT m (\x -> (\k -> k x) c) ≡ ContT $ \c -> runContT m (\x -> c x) ≡ ContT $ \c -> runContT m c ≡ ContT $ runContT m ≡ m 3. (m >>= f) >>= g ≡ m >>= (\x -> f x >>= g) (m >>= f) >>= g ≡ (ContT $ \c -> runContT m (\x -> runContT (f x) c)) >>= g ≡ ContT $ \c -> runContT (ContT $ \c -> runContT m (\x -> runContT (f x) c)) (\x -> runContT (g x) c) ≡ ContT $ \c -> (\c -> runContT m (\x -> runContT (f x) c)) (\x -> runContT (g x) c) ≡ ContT $ \c -> runContT m (\x -> runContT (f x) (\x -> runContT (g x) c)) m >>= (\x -> f x >>= g) ≡ ContT $ \c -> runContT m (\x -> runContT ((\x -> f x >>= g) x) c) ≡ ContT $ \c -> runContT m (\x -> runContT (f x >>= g) c) ≡ ContT $ \c -> runContT m (\x -> runContT (ContT $ \c -> runContT (f x) (\x -> runContT (g x) c)) c) ≡ ContT $ \c -> runContT m (\x -> (\c -> runContT (f x) (\x -> runContT (g x) c)) c) ≡ ContT $ \c -> runContT m (\x -> runContT (f x) (\x -> runContT (g x) c))
instance MonadTrans (ContT r) where lift m = ContT (m >>=) instance (MonadIO m) => MonadIO (ContT r m) where liftIO = lift . liftIO
lift m 将封装在内部 Monad m 的值封装进了 ContT Monad 之中。
lift m = ContT $ \k -> m >>= k
这里 m 的类型为 m a, k 的类型为 a -> m r,m >> k 的类型为 m r,
\k -> m >>= k 的类型为 (a -> m r) -> m r,
ContT $ \k -> m >>= k 也就是 ContT (m >>=) 的类型为 ContT r m a。函数
证实 ContT 中 lift 函数的定义符合 lift 的法则。 1. lift . return ≡ return lift . return $ a ≡ lift (m a) ≡ ContT (m a >>=) ≡ ContT $ \k -> m a >>= k ≡ ContT $ \k -> k a ≡ return a 2. lift (m >>= f) ≡ lift m >>= (lift . f) 假设 m = n a 而且 f a = n b 因而 m >>= f = n b lift (m >>= f) ≡ lift (n b) ≡ ContT (n b >>=) ≡ ContT $ \k -> n b >>= k ≡ ContT $ \k -> k b ≡ return b lift m >>= (lift . f) ≡ ContT (n a >>=) >>= \x -> ContT (f x >>=) ≡ ContT (\k -> k a) >>= \x -> ContT (\k -> f x >>= k) ≡ ContT $ \c -> runContT $ ContT (\k -> k a) (\x -> runContT $ ContT (\k -> f x >>= k) c) ≡ ContT $ \c -> (\k -> k a) (\x -> (\k -> f x >>= k) c) ≡ ContT $ \c -> (\x -> (\k -> f x >>= k) c) a ≡ ContT $ \c -> (\k -> f a >>= k) c ≡ ContT $ \c -> (\k -> n b >>= k) c ≡ ContT $ \c -> (\k -> k b) c ≡ ContT $ \c -> c b ≡ return b
Prelude Control.Monad.Trans.Cont Control.Monad.Trans> f a = if even a then Just (a `div` 2) else Nothing Prelude Control.Monad.Trans.Cont Control.Monad.Trans> c = lift . f :: Int -> ContT Int Maybe Int Prelude Control.Monad.Trans.Cont Control.Monad.Trans> runContT (c 3) return Nothing Prelude Control.Monad.Trans.Cont Control.Monad.Trans> runContT (c 4) f Just 1 Prelude Control.Monad.Trans.Cont Control.Monad.Trans> c = lift getLine :: ContT r IO String Prelude Control.Monad.Trans.Cont Control.Monad.Trans> runContT c print abc "abc"
callCC :: ((a -> ContT r m b) -> ContT r m a) -> ContT r m a callCC f = ContT $ \c -> runContT (f (\x -> ContT $ \_ -> c x)) c
CallCC 是 Call With Current Continuation(对当前延续函数进行调用)的缩写,它为 ContT Monad 提供了退出的手段。
CallCC 有一个参数 f,CallCC f 返回一个 ContT Monad。
f 的类型是 (a -> ContT r m b) -> ContT r m a,也就是说 f 自己是个函数,它的返回值类型和 CallCC f 相同,都是一个ContT Monad。
f 一般采用 \exit -> do ... 这种形式。code
Prelude Control.Monad.Trans.Cont Control.Monad.Trans> runContT (callCC (\exit -> exit 1) :: ContT Int Maybe Int) return Just 1 Prelude Control.Monad.Trans.Cont Control.Monad.Trans> runContT (callCC (\exit -> do{exit 1; return 2}) :: ContT Int Maybe Int) return Just 1
也就是说只要 CallCC 的参数采用 \exit -> do {...; exit a; ...} 这种形式,
那么该函数所返回的 ContT Monad 将无视 exit a 后面的处理流程,无条件地将 a 传递给外围函数。
下面看看 CallCC 函数是如何作到这一点的。接口
从以上推导过程能够看出 CallCC 的参数若是采用 \exit -> do {...; exit a; ...} 这种形式,
该函数所返回的 ContT Monad 确实会无视 exit a 后面的处理流程,无条件地将 a 传递给外围函数。get
evalContT :: (Monad m) => ContT r m r -> m r evalContT m = runContT m return mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a mapContT f m = ContT $ f . runContT m withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b withContT f m = ContT $ runContT m . f resetT :: (Monad m) => ContT r m r -> ContT r' m r resetT = lift . evalContT shiftT :: (Monad m) => ((a -> m r) -> ContT r m r) -> ContT r m a shiftT f = ContT (evalContT . f) liftLocal :: (Monad m) => m r' -> ((r' -> r') -> m r -> m r) -> (r' -> r') -> ContT r m a -> ContT r m a liftLocal ask local f m = ContT $ \c -> do r <- ask local f (runContT m (local (const r) . c))
cont :: ((a -> r) -> r) -> Cont r a cont f = ContT (\c -> Identity (f (runIdentity . c))) runCont :: Cont r a -> (a -> r) -> r runCont m k = runIdentity (runContT m (Identity . k)) evalCont :: Cont r r -> r evalCont m = runIdentity (evalContT m) mapCont :: (r -> r) -> Cont r a -> Cont r a mapCont f = mapContT (Identity . f . runIdentity) withCont :: ((b -> r) -> (a -> r)) -> Cont r a -> Cont r b withCont f = withContT ((Identity .) . f . (runIdentity .)) reset :: Cont r r -> Cont r' r reset = resetT shift :: ((a -> r) -> Cont r r) -> Cont r a shift f = shiftT (f . (runIdentity .))
Cont Monad 是 ContT Monad(转换器) 的一个特例。it
import Control.Monad.Trans.Cont import Control.Monad (when) add :: Int -> Int -> Int add x y = x + y square :: Int -> Int square x = x * x add_cont :: Int -> Int -> Cont r Int add_cont x y = return (add x y) square_cont :: Int -> Cont r Int square_cont x = return (square x) pythagoras_cont :: Int -> Int -> Cont r Int pythagoras_cont x y = do x_squared <- square_cont x y_squared <- square_cont y add_cont x_squared y_squared pythagoras_cont' :: Int -> Int -> Cont r Int pythagoras_cont' x y = callCC $ \exit -> do when (x < 0 || y < 0) $ exit (-1) x_squared <- square_cont x y_squared <- square_cont y add_cont x_squared y_squared main = do runCont (pythagoras_cont 3 4) print -- 25 runCont (pythagoras_cont' 3 4) print -- 25 runCont (pythagoras_cont' (-3) 4) print -- -1