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]
相关文章
- 【论文阅读】Hierarchical multi-task graph recurrent network for next POI recommendation
- 终端SSH工具:SecureCRT for Mac
- RAR Extractor - ZIP Unarchiver for Mac(rar压缩解压工具)
- mac虚拟机pd18完美激活版Parallels Desktop 18 for Mac 永久证书版
- ScreenFlow for mac(最强大的屏幕录像软件)v10.0.9中/英文激活版
- ORA-01915: invalid auditing option for views ORACLE 报错 故障修复 远程处理
- ORA-25259: cannot specify protocol for agent ORACLE 报错 故障修复 远程处理
- ORA-31650: timeout waiting for master process response ORACLE 报错 故障修复 远程处理
- ORA-02069: global_names parameter must be set to TRUE for this operation ORACLE 报错 故障修复 远程处理
- ORA-14642: Bitmap index mismatch for tables in ALTER TABLE EXCHANGE PARTITION ORACLE 报错 故障修复 远程处理
- 循环Linux中使用For循环的正确方式(linux里的for)
- loopOracle中的循环编程:For Loop游标(oracle游标for)
- 使用Linux中的For循环实现简单程序(linux的for循环)
- 微软OneDrive for Mac客户端不久后将以.pkg格式提供
- 进程探索Linux中For循环进程管理(linux中for)
- javascript下for循环用法小结