monad可以成为comonad吗?

Mat*_*hid 27 haskell category-theory

我知道monad是什么.我我已经正确地把我的想法包围在一个comonad是什么.(或者说,什么人看起来非常简单,最棘手的部分是理解什么是有用的这个...)

我的问题是:有什么东西可以成为monad comonad吗?

我预见到两个可能的答案:

  • 是的,这是常见且广泛有用的.
  • 不,他们做了不同的工作,没有理由想要两者兼而有之.

那么,这是什么?

scl*_*clv 25

Cofree Comonad产生一些数据结构,可用作Monads和Comonads:

data Cofree f a = a :< f (Cofree f a)
Run Code Online (Sandbox Code Playgroud)

每个Cofree Comonad上的另一个仿函数产生一个Monad - 请参见此处的实例:

http://hackage.haskell.org/packages/archive/free/3.4.1/doc/html/Control-Comonad-Cofree.html

instance Alternative f => Monad (Cofree f) where
  return x = x :< empty
  (a :< m) >>= k = case k a of
                     b :< n -> b :< (n <|> fmap (>>= k) m)
Run Code Online (Sandbox Code Playgroud)

这给了我们,例如Monad和Comonads的非空列表(以及非空的f分支树等).

Identity不是替代方案,但Cofree Identity产生无限流,我们实际上可以为该流提供不同的 monad实例:

http://hackage.haskell.org/packages/archive/streams/3.1/doc/html/Data-Stream-Infinite.html

data Stream a = a :> Stream a
instance Comonad Stream where
  duplicate = tails
  extend f w = f w :> extend f (tail w)
  extract = head

instance Monad Stream where
  return = repeat
  m >>= f = unfold (\(bs :> bss) -> (head bs, tail <$> bss)) (fmap f m)
Run Code Online (Sandbox Code Playgroud)

(注意上面的函数不在列表中,而是在streams包中定义).

类似地,读者箭头不是替代品,而是Cofree ((->) r)产生摩尔机器,而摩尔机器也是monad和comonads:

http://hackage.haskell.org/packages/archive/machines/0.2.3.1/doc/html/Data-Machine-Moore.html

data Moore a b = Moore b (a -> Moore a b)
instance Monad (Moore a) where
  return a = r where r = Moore a (const r)
  Moore a k >>= f = case f a of
    Moore b _ -> Moore b (k >=> f)
  _ >> m = m
instance Comonad (Moore a) where
  extract (Moore b _) = b
  extend f w@(Moore _ g) = Moore (f w) (extend f . g)
Run Code Online (Sandbox Code Playgroud)

那么所有这些例子背后的直觉是什么?好吧,我们免费获得comonadic操作.我们得到的monadic操作都是对角化的形式.有了替代方案,我们可以将这些<|>东西结合在一起"扼杀"结构,并在我们用完结构来抹去时,将"空"的东西魔术化.这让我们可以处理有限的案例.缺乏替代方案我们需要有一个无限量的结构,这样无论我们制造了多少"连接"操作(我们可以认为是拼接或替换),拼接元素总是有更多的空间(比如希尔伯特酒店:http://www.encyclopediaofmath.org/index.php/Hilbert_infinite_hotel).

相关地,每个 Comonad都会产生一个相关的Monad(尽管我认为这更像是一个好奇心):

http://hackage.haskell.org/packages/archive/kan-extensions/3.1.1/doc/html/Control-Monad-Co.html

http://comonad.com/reader/2011/monads-from-comonads/

  • "每个Cofree Comonad对替代Functor都会产生一个Monad" - 这与"monad只是一个monofunors的幺半群"一样值得一提!:-D (12认同)

Phi*_* JF 22

是.将一些评论转化为答案:

newtype Identity a = Identity {runIdenity :: a} deriving Functor
instance Monad Identity where
  return = Identity
  join = runIdentity
instance CoMonad Identity where
  coreturn = runIdentity
  cojoin = Identity
Run Code Online (Sandbox Code Playgroud)

读者和作者是精确的对偶,如图所示

class CoMonoid m where
  comempty :: (m,a) -> a
  comappend :: m -> (m,m)
--every haskell type is a CoMonoid
--that is because CCCs are boring!

instance Monoid a => Monad ((,) a) where
  return x = (mempty,x)
  join (a,(b,x)) = (a <> b, x)
instance CoMonoid a => CoMonad ((,) a) where
  coreturn = comempty
  cojoin = associate . first comappend

instance CoMonoid a => Monad ((->) a) where
  return = flip (curry comempty)
  join f = uncurry f . comappend
instance Monoid a => CoMonad ((->) a)  where
  coreturn f = f mempty
  cojoin f a b = f (a <> b)
Run Code Online (Sandbox Code Playgroud)

  • 我真的很喜欢这种对称性.就像在物理学中一样,如果事物是​​对称的,那就意味着你正在做某事. (3认同)
  • 如果有人像我一样困惑,CCC =笛卡尔封闭类别(我认为) (3认同)
  • 我只是真的笑了起来i)符号名称("翻转咖喱")和ii)我自己对我的了解有多少.这本身就值得投票. (2认同)

Edw*_*ETT 15

有许多有趣的结构既是a Monad又是a Comonad.

Identity函子已在此间指出,通过其他几人,但也有不平凡的例子.

Writer Monad扮演Reader的角色般Comonad.

instance Monoid e => Monad ((,) e)
instance Comonad ((,) e)
Run Code Online (Sandbox Code Playgroud)

Reader Monad扮演Writer的角色般Comonad.

instance Monad ((->) e)
instance Monoid e => Comonad ((->)e)
Run Code Online (Sandbox Code Playgroud)

非空列表也形成monad和comonad,实际上是涉及cofree comonad的更大结构的特例.该Identity情况下,也可以看作是这种特殊情况.

也有各种各样的YonedaCodensity基于侃扩展式的结构,这项工作转变单子和comonads,虽然他们青睐的一个或另一个在操作效率方面.

我还有一个适配器,可以将任意comonad转换为monad转换器.可悲的是,在Haskell中不可能进行相反的转换.

在线性代数中有一个代数的概念.理想的情况下,如果我们有形成既有的东西MonadComonad我们想用这些行动没有一起推理对案件逐案基础上,一想有一个returnjoin是Comonad余代数和扩展,extractduplicateMonad代数.如果这些条件成立,那么您实际上可以推断出具有这两个条件和约束条件的代码,Monad f并将Comonad f每个组合器混合在一起,而无需逐个推理.


J. *_*son 5

这取决于你认为是什么"monad".如果你问"有可能是一种既实例Monad,并Comonad在一次?" 好的.这是一个简单的例子.

newtype Id a = Id a

instance Monad Identity where
  return       = Id
  (Id a) >>= f = f a

instance Comonad Identity where
  extract (Id a) = a
  extend f ida = Id (f ida)
Run Code Online (Sandbox Code Playgroud)

如果你的数学意思是,那么monad是一个三元组(X, return, bind),它X是一种类型,returnbind遵循你期望的类型和规律.同样,comonad是(X, extend, extract).我刚刚证明了XS可能会发生相同,但是由于种类extendreturn或者extractbind不同它不可能对它们进行相同的功能.所以数学monad永远不会是一个comonad.