zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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

for 29 free Scalaz
2023-09-14 08:57:16 时间

 很多时候我们会遇到一些高阶类型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]

 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

14 def fromCoyo[F[_]: Functor,A](coyo: _Coyoneda[F,A]): F[A] =

15 Functor[F].map(coyo.fi)(coyo.k)

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]

5 type coyoInteract[A] = Coyoneda[Interact,A]