一种众所周知的替代配方Applicative
(参见,例如,Typeclassopedia)是
class Functor f => Monoidal f where
unit :: f ()
pair :: f a -> f b -> f (a, b)
Run Code Online (Sandbox Code Playgroud)
这导致法律看起来更像典型的身份和相关性法则,而不是你从中得到的法则Applicative
,但只有当你通过对重新关联同构时才能工作.几个星期前考虑到这一点,我想出了另外两个避免这个问题的配方.
class Functor f => Fapplicative f where
funit :: f (a -> a)
fcomp :: f (b -> c) -> f (a -> b) -> f (a -> c)
class Functor f => Capplicative f where
cunit :: Category (~>) => f (a ~> a)
ccomp :: Category (~>) => f (b …
Run Code Online (Sandbox Code Playgroud) 通过对Haskell的各种类别的主题类各种教程阅读,我们发现之类的东西Monoid
,Functor
,Monad
等等-所有这些有几十个实例.但出于某种原因,当我们到达时Arrow
,只有两个实例:函数和monad.在这两种情况下,使用Arrow
实例都不如直接使用底层事物那样强大且困难.
有没有人有任何有趣的箭头例子?我肯定肯定会有一些,但我从来没有遇到任何关于他们的文章......
这是我要参考的 SO 帖子。此外,我将在该问题中使用与 OP 相同的片段,以免分离材料。
据众所周知的是一个ArrowApply
实例产生一个单子,反之亦然:
newtype ArrowMonad a b = ArrowMonad (a () b)
instance Arrow a => Functor (ArrowMonad a) where
fmap f (ArrowMonad m) = ArrowMonad $ m >>> arr f
instance Arrow a => Applicative (ArrowMonad a) where
pure x = ArrowMonad (arr (const x))
ArrowMonad f <*> ArrowMonad x = ArrowMonad (f &&& x >>> arr (uncurry id))
instance ArrowApply a => Monad (ArrowMonad a) where
ArrowMonad m …
Run Code Online (Sandbox Code Playgroud)