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 (<<<) = (.) (&…
Arrow class Category a => Arrow a where arr :: (b -> c) -> a b c first :: a b c -> a (b,d) (c,d) first = (*** id) second :: a b c -> a (d,b) (d,c) second = (id ***) (***) :: a b c -> a b' c' -> a (b,b') (c,c') f *** g = first f >&g…
Function, Monad, Arrow f :: Int -> (Int, Int) f = \x -> let y = 2 * x z1 = y + 3 z2 = y - 5 in (z1, z2) -- ghci> f 10 -- (23, 15) fM :: Int -> Identity (Int, Int) fM = \x -> do y <- return (2 * x) z1 <- return (y + 3) z2 <- return…
安装 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…