0
0
0
1. 云栖社区>
2. 博客>
3. 正文

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

``````  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 }``````

``````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]``````

+ 关注

corcosa 13570人浏览