如何从Cofree Comonad推导Monad实例?
利用
Co从任意Cofree f a推导Monad实例 Edward Kmett在其博客中提到,借助kan-extensions包中的Co newtype,可从任意Comonad推导Monad。我希望学习如何为任意Cofree f a机械地完成这一操作,因为部分Cofree类型我无法通过其他可靠方式获取Monad实例。
比如下面这个树类型:
data Tree a = Leaf a | Branch a (Tree a) (Tree a)
它与以下类型同构:
type Tree' = Cofree (Compose Maybe V2) -- V2来自`linear`包
我发现该类型十分有趣,它无法建模为Free monad,也不具备Representable特性。据我理解,Co Tree也与Tree同构,且拥有Monad实例。Phil Freeman在其comonadic UI库中大量使用Co,足见其实用性。在《Declarative UIs are the Future — And the Future is Comonadic!》一文中,他提到:
实际上,在f满足特定条件时,Co (Cofree f) monad与由f确定的free monad同构。
虽然我可以通过类型推导编写类型间的转换函数,但不确定这样能否得到正确实现。
内容的提问来源于stack exchange,提问作者Johannes Riecken
相关产品推荐
相关产品推荐

