# Scalaz（29）－ Free ：Coyoneda

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

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

`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 = Aval k = _kval 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])`

` 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 I4 def k: I => A5 def fi: F[I]6 }78 def toCoyo[F[_],A](fa: F[A]) =9 new _Coyoneda[F, A] {10 type I = A11 val k = (a: A) => a12 val fi = fa13 }14 def fromCoyo[F[_]: Functor,A](coyo: _Coyoneda[F,A]): F[A] =15 Functor[F].map(coyo.fi)(coyo.k)1617 }`

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

Top