Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance Category (->) where id = GHC.Base.id (.) = (GHC.Base..) (<<<) :: Category cat => cat b c -> cat a b -> cat a c (<<<) = (.) (&…
RWST Monad转换器 RWST Monad转换器是将 ReaderT, WriterT 以及 StateT 这三个Monad转换器的功能集于一体的产物. newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) } instance (Monoid w, Monad m) => Monad (RWST r w s m) where return a = RWST $ \ _ s -> return (a,…
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Free f a)) instance Functor f => Functor (Free f) where fmap f = go where go (Pure a) = Pure (f a) go (Free fa) = Free (go <$> fa) instance Functor f…
自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHaskell is needed for makeLenses; RankNTypes is needed for -- a few type signatures later on. {-# LANGUAGE TemplateHaskell, RankNTypes #-} import Control…
MonadState 类型类 class Monad m => MonadState s m | m -> s where get :: m s get = state (\s -> (s, s)) put :: s -> m () put s = state (\_ -> ((), s)) state :: (s -> (a, s)) -> m a state f = do s <- get let ~(a, s') = f s put s' return…