Scalaz(29)- Free :Coyoneda - Functor for free

简介:

 很多时候我们会遇到一些高阶类型F[_],但又无法实现它的map函数,也就是虽然形似但F不可能成为Functor。看看下面的例子:


trait Interact[A]
case class Ask(prompt: String) extends Interact[String]
case class Tell(msg: String) extends Interact[Unit]

Interact类型只容许两种实例:Ask继承了Interact[String], Tell继承Interact[Unit]。我们无法获取map[A,B]函数的B值,因而无法实现map函数了。但是,往往在一些场合里我们需要把F当做Functor来使用,如用Free Structure把F升格成Monad。也就是说我们需要把Interact当做Functor才能构建一个基于Interact的Free Monad。Scalaz里的Coyoneda与任何F[_]类型成同构(互等),而Coyoneda是个Functor,这样我们可以用Coyoneda来替代F。在上面的例子里我们只要得出F的Coyoneda,然后我们就可以用这个Coyoneda来替代F,因为它们是同构的。我们来看看Coyoneda的定义:scalaz/Coyoneda.scala


sealed abstract class Coyoneda[F[_], A] { coyo =>
  /** The pivot between `fi` and `k`, usually existential. */
  type I

  /** The underlying value. */
  val fi: F[I]

  /** The transformer function, to be lifted into `F` by `run`. */
  val k: I => A
...
  /** Like `lift(fa).map(_k)`. */
  def apply[F[_], A, B](fa: F[A])(_k: A => B): Aux[F, B, A] =
    new Coyoneda[F, B]{
      type I = A
      val k = _k
      val fi = fa
    }
...
  type Aux[F[_], A, B] = Coyoneda[F, A] {type I = B}

  /** `F[A]` converts to `Coyoneda[F,A]` for any `F` */
  def lift[F[_],A](fa: F[A]): Coyoneda[F, A] = apply(fa)(identity[A])

即使F不是Functor,我们还是可以把F[A]拆成Coyoneda[F,A]。而Coyoneda和F同构,看下面scalaz里的代码:


  type CoyonedaF[F[_]] = ({type A[α] = Coyoneda[F, α]})

  import Isomorphism._

  def iso[F[_]: Functor]: CoyonedaF[F]#A <~> F =
    new IsoFunctorTemplate[CoyonedaF[F]#A, F] {
      def from[A](fa: F[A]) = lift(fa)
      def to[A](fa: Coyoneda[F, A]) = fa.run
    }

我们自己同样可以用更简单的方法来证明:


 1 object proof_coyo {
 2  trait _Coyoneda[F[_],A] {
 3   type I
 4   def k: I => A
 5   def fi: F[I]
 6  }
 7 
 8  def toCoyo[F[_],A](fa: F[A]) =
 9     new _Coyoneda[F, A] {
10       type I = A
11       val k = (a: A) => a
12       val fi = fa
13     }
14  def fromCoyo[F[_]: Functor,A](coyo: _Coyoneda[F,A]): F[A] =
15     Functor[F].map(coyo.fi)(coyo.k)
16    
17 }

对于任何类型F及A, 我们通过toCoyo, fromCoyo可以证明_Coyoneda和F[A]同构。 既然Coyoneda和F[A]同构,那么我们可以这样表述:F[A] >>> Coyoneda[F,A]。也就是说我们可以在任何地方用Coyoneda[F,A]替代F[A]。上面例子中的Interact也可以用Coyoneda替代:


1 trait Interact[A]
2 case class Ask(prompt: String) extends Interact[String]
3 case class Tell(msg: String) extends Interact[Unit]
4 
5 type coyoInteract[A] = Coyoneda[Interact,A]

相关文章
|
5月前
|
存储 Cloud Native Linux
malloc、free 和 new、delete 的区别
malloc、free 和 new、delete 的区别
|
3月前
关于malloc和free
关于malloc和free
25 0
|
9月前
|
安全 C语言 C++
new delete和malloc free的区别
一个对象我们可以建立在栈上也可以建立在堆上,但是在C语言里与C++里他们的实现还不同,本文将详细介绍new delete和malloc free之间的区别。
|
9月前
|
C++
【C++】 new/delete与 malloc/free
【C++】 new/delete与 malloc/free
73 0
|
12月前
|
安全 Linux 开发工具
C++11 智能指针之shared_ptr<void>
基于Alexa的全链路智能语音SDK基于C++实现了跨平台特性,跑通了Android、Mac、Linux等设备,在兼容iOS时发现iOS未提供音频采集和播放的C++接口,所以需要改造SDK,允许SDK初始化时注入外部的采集器和播放器实现类,同时SDK中的Android播放器是基于ffmpeg解码 + opensl实现,但是考虑到包体积的问题,准备也基于这个接口在外部实现基于Android硬件解码的播放器。
279 0
|
安全 编译器 C语言
malloc、free与new、delete的区别
malloc、free与new、delete的区别
128 0
free函数使用理解
free函数使用理解
130 0