泛函编程(33)-泛函IO:Free Functor - Coyoneda
在前几期讨论中我们终于推导出了Free Monad。这是一个Monad工厂,它可以把任何F[A]变成Monad。可惜的是它对F[A]是有所要求的:F必须是个Functor。Free Monad由此被称为由Functor F 产生的Monad。F必须是Functor,这个门槛使我们在使用Free Monad时很不方便。举个前面讨论过的例子:
- trait Console[A]
- case object GetLine extends Console[String]
- case class PutLine(line: String) extends Console[Unit]
我们想用Free Monad把Console[A]变成Monad: Free[Console,A],但我们必须先得到Console的Functor实例:
- implicit val consoleFunctor = new Functor[Console] {
- def map[A,B](ca: Console[A])(f: A => B): Console[B] = ca match {
- case GetLine => ?????
- case PutLine(l) => ????
- }
- }
讲老实话,我到现在还没能想出如何实现这个map函数。除非把Console类型修改一下,这个可以参考前面讨论中的代码。
现在的问题是如果能有个什么方法把F[A]变成Functor,就像Free Monad那样有个Free Functor就好了。范畴学中Yoneda lemma结论中的Coyoneda就是一个Free Functor。
Yoneda lemma是这样推论的:如果我们有个这样的函数定义:def map[B](f: A => B): F[B],那我们就肯定能得出F[A]值,因为我们只需要把一个恒等函数当作f就能得到F[A]。反过来推论:如果我们有个F[A],F是任何Functor,A是任何类型,我们同样可以得出以上的map函数。我们可以用个类型来表示:
- trait Yoneda[F[_],A] {
- def map[B](f: A => B): F[B]
- }
当然,这也意味着如果:有个类型B,一个函数(B => A),A是任意类型,一个F[B],F是任意Functor,我们肯定能得出F[A]:因为我们只要把(B => A)和F[B]传入map:
map(fb: F[B])(f: B => A): F[A]。
我们同样可以用一个类型来表示:
- trait Coyoneda[F[_],A] { coyo =>
- type I
- def fi: F[I]
- def k(i: I): A
- }
在下面我们可以证明F[A]同等Coyoneda[F,A],而Coyoneda是个Functor。我们只需将F[A]升格(lift)到Coyoneda就能得到一个Free Functor了。
- trait Functor[F[_]] {
- def map[A,B](fa: F[A])(f: A => B): F[B]
- }
- object Functor {
- def apply[F[_]: Functor]: Functor[F] = implicitly[Functor[F]]
- }
- trait Monad[M[_]] {
- def unit[A](a: A): M[A]
- def flatMap[A,B](ma: M[A])(f: A => M[B]): M[B]
- def map[A,B](ma: M[A])(f: A => B) = flatMap(ma)(a => unit(f(a)))
- }
- object Monad {
- def apply[M[_]: Monad]: Monad[M] = implicitly[Monad[M]]
- }
- trait Yoneda[F[_],A] { yo =>
- def apply[B](f: A => B): F[B]
- def run: F[A] = apply(a => a) //无需Functor实例就可以将Yoneda转变成F[A]
- def toCoyoneda: Coyoneda[F,A] = new Coyoneda[F,A] { //转Coyoneda无需Functor
- type I = A
- def fi = yo.run
- def k(i: A) = i
- }
- def map[B](f: A => B): Yoneda[F,B] = new Yoneda[F,B] { //纯粹的函数组合 map fusion
- def apply[C](g: B => C): F[C] = yo( f andThen g)
- }
- }
- trait Coyoneda[F[_],A] { coyo =>
- type I
- def fi: F[I]
- def k(i: I): A
- def run(implicit F: Functor[F]): F[A] = //Coyoneda转F需要F Functor实例
- F.map(fi)(k)
- def toYoneda(implicit F: Functor[F]): Yoneda[F,A] = new Yoneda[F,A] { //转Yoneda需要Functor
- def apply[B](f: A => B): F[B] = F.map(fi)(k _ andThen f)
- }
- def map[B](f: A => B): Coyoneda[F,B] = new Coyoneda[F,B] {
- type I = coyo.I
- def fi = coyo.fi
- def k(i: I) = f(coyo k i)
- }
- }
- object Yoneda {
- def apply[F[_]: Functor,A](fa: F[A]) = new Yoneda[F,A] { //F转Yoneda需要Functor
- def apply[B](f: A => B): F[B] = Functor[F].map(fa)(f)
- }
- implicit def yonedaFunctor[F[_]] = new Functor[({type l[x] = Yoneda[F,x]})#l] {
- def map[A,B](ya: Yoneda[F,A])(f: A => B) = ya map f
- }
- }
- object Coyoneda {
- def apply[F[_],A](fa: F[A]): Coyoneda[F,A] = new Coyoneda[F,A] {
- type I = A //把F[A]升格成Coyoneda, F无须为Functor
- def fi = fa
- def k(a: A) = a
- }
- implicit def coyonedaFunctor[F[_]] = new Functor[({type l[x] = Coyoneda[F,x]})#l] {
- def map[A,B](ca: Coyoneda[F,A])(f: A => B) = ca map f //Coyoneda本身就是Functor
- }
- }
以上值得注意的是:F[A]可以直接升格等于Coyoneda,而Coyoneda是个Functor。换句话说我们把F[A]升格到Coyoneda就可以当Functor来用了。
我们的目的是把任何F[A]变成Free Monad,那么我们就需要有一个用Coyoneda产生的Free:
- trait Free[F[_],A] {
- private case class FlatMap[B](a: Free[F,A], f: A => Free[F,B]) extends Free[F,B]
- def unit(a: A): Free[F,A] = Return(a)
- def flatMap[B](f: A => Free[F,B])(implicit F: Functor[F]): Free[F,B] = this match {
- case Return(a) => f(a)
- case Suspend(k) => Suspend(F.map(k)(a => a flatMap f))
- case FlatMap(b,g) => FlatMap(b, g andThen (_ flatMap f))
- }
- def map[B](f: A => B)(implicit F: Functor[F]): Free[F,B] = flatMap(a => Return(f(a)))
- def resume(implicit F: Functor[F]): Either[F[Free[F,A]],A] = this match {
- case Return(a) => Right(a)
- case Suspend(k) => Left(k)
- case FlatMap(a,f) => a match {
- case Return(b) => f(b).resume
- case Suspend(k) => Left(F.map(k)(_ flatMap f))
- case FlatMap(b,g) => FlatMap(b, g andThen (_ flatMap f)).resume
- }
- }
- def foldMap[G[_]](f: (F ~> G))(implicit F: Functor[F], G: Monad[G]): G[A] = resume match {
- case Right(a) => G.unit(a)
- case Left(k) => G.flatMap(f(k))(_ foldMap f)
- }
- }
- case class Return[F[_],A](a: A) extends Free[F,A]
- case class Suspend[F[_],A](ffa: F[Free[F,A]]) extends Free[F,A]
- object Free {
- import scalaz.Unapply
- /** A free monad over the free functor generated by `S` */
- type FreeC[S[_], A] = Free[({type f[x] = Coyoneda[S, x]})#f, A]
- /** Suspends a value within a functor in a single step. Monadic unit for a higher-order monad. */
- def liftF[S[_], A](value: => S[A])(implicit S: Functor[S]): Free[S, A] =
- Suspend(S.map(value)(Return[S, A]))
- /** A version of `liftF` that infers the nested type constructor. */
- def liftFU[MA](value: => MA)(implicit MA: Unapply[Functor, MA]): Free[MA.M, MA.A] =
- liftF(MA(value))(MA.TC)
- /** A free monad over a free functor of `S`. */
- def liftFC[S[_], A](s: S[A]): FreeC[S, A] =
- liftFU(Coyoneda(s))
- /** Interpret a free monad over a free functor of `S` via natural transformation to monad `M`. */
- def runFC[S[_], M[_], A](sa: FreeC[S, A])(interp: S ~> M)(implicit M: Monad[M]): M[A] =
- sa.foldMap[M](new (({type λ[α] = Coyoneda[S, α]})#λ ~> M) {
- def apply[A](cy: Coyoneda[S, A]): M[A] =
- M.map(interp(cy.fi))(cy.k)
- })
- }
我们把前面推导出来的Free搬过来。然后在Free companion object里增加了FreeC类型:
type FreeC[S[_],A] = Free[({type f[x] = Coyoneda[F,x]})#f, A]
这个可以说是一个由Coyoneda产生的Free。
现在我们要想办法把S[A]升格成FreeC:liftFC[S[_],A](s: S[A]): FreeC[S,A],这里需要先把S[A]升格成Coyoneda:Coyoneda(s)。
由于Coyoneda[S,A]是个多层嵌入类型。我们在liftFU函数中需要借用scalaz的Unapply类型来分解出Coyoneda, S[A]然后施用在liftF;
def liftF[S[_],A](sa: S[A])(implicit S: Functor[S]),这里的S就是Coyoneda。
Interpreter沿用了foldMap但是调整了转换源目标类型 Functor >>> Coyoneda。其它如Trampoline机制维持不变。
现在我们可以直接用任何F[A]来产生Free了。先试试上面的那个Console。这个Console不是个Functor:
- trait Console[A]
- case object GetLine extends Console[String]
- case class PutLine(line: String) extends Console[Unit]
- import Free._
- implicit def liftConsole[A](ca: Console[A]): FreeC[Console,A] = liftFC(ca)
- //> liftConsole: [A](ca: ch13.ex11.Console[A])ch13.ex11.Free.FreeC[ch13.ex11.Co
- //| nsole,A]
- for {
- _ <- PutLine("What is your first name ?")
- first <- GetLine
- _ <- PutLine("What is your last name ?")
- last <- GetLine
- _ <- PutLine(s"Hello, $first $last !")
- } yield () //> res0: ch13.ex11.Free[[x]ch13.ex11.Coyoneda[ch13.ex11.Console,x],Unit] = Sus
- //| pend(ch13.ex11$Coyoneda$$anon$4@50f8360d)
可以使用Free的Monadic语言了。下面再试试Interpreter部分:
- val ioprg = for {
- _ <- PutLine("What is your first name ?")
- first <- GetLine
- _ <- PutLine("What is your last name ?")
- last <- GetLine
- _ <- PutLine(s"Hello, $first $last !")
- } yield () //> ioprg : ch13.ex11.Free[[x]ch13.ex11.Coyoneda[ch13.ex11.Console,x],Unit] =
- //| Suspend(ch13.ex11$Coyoneda$$anon$4@13c78c0b)
- type Id[A] = A
- implicit val idMonad = new Monad[Id] {
- def unit[A](a: A) = a
- def flatMap[A,B](fa: A)(f: A => B): B = f(fa)
- } //> idMonad : ch13.ex11.Monad[ch13.ex11.Id] = ch13.ex11$$anonfun$main$1$$anon$
- //| 10@12843fce
- object RealConsole extends (Console ~> Id) {
- def apply[A](ca: Console[A]): A = ca match {
- case GetLine => readLine
- case PutLine(l) => println(l)
- }
- }
- Free.runFC(ioprg)(RealConsole) //> What is your first name ?/
- case class State[S,A](runState: S => (A,S)) {
- def map[B](f: A => B) = State[S,B](s => {
- val (a1,s1) = runState(s)
- (f(a1),s1)
- })
- def flatMap[B](f: A => State[S,B]) = State[S,B](s => {
- val (a1,s1) = runState(s)
- f(a1).runState(s1)
- })
- }
- case class InOutLog(inLog: List[String], outLog: List[String])
- type LogState[A] = State[InOutLog, A]
- implicit val logStateMonad = new Monad[LogState] {
- def unit[A](a: A) = State(s => (a, s))
- def flatMap[A,B](sa: LogState[A])(f: A => LogState[B]) = sa flatMap f
- } //> logStateMonad : ch13.ex11.Monad[ch13.ex11.LogState] = ch13.ex11$$anonfun$m
- //| ain$1$$anon$11@3dd3bcd
- object MockConsole extends(Console ~> LogState) {
- def apply[A](c: Console[A]): LogState[A] = State(
- s => (c,s) match {
- case (GetLine, InOutLog(in,out)) => (in.head, InOutLog(in.tail, out))
- case (PutLine(l), InOutLog(in,out)) => ((),InOutLog(in, l :: out))
- })
- }
- val s = Free.runFC(ioprg)(MockConsole) //> s : ch13.ex11.LogState[Unit] = State(<function1>)
- val ls = s.runState(InOutLog(List("Tiger","Chan"),List()))
- //> ls : (Unit, ch13.ex11.InOutLog) = ((),InOutLog(List(),List(Hello, Tiger Ch
- //| an !, What is your last name ?, What is your first name ?)))
泛函编程(33)-泛函IO:Free Functor - Coyoneda的更多相关文章
- 泛函编程(5)-数据结构(Functional Data Structures)
编程即是编制对数据进行运算的过程.特殊的运算必须用特定的数据结构来支持有效运算.如果没有数据结构的支持,我们就只能为每条数据申明一个内存地址了,然后使用这些地址来操作这些数据,也就是我们熟悉的申明变量 ...
- 泛函编程(32)-泛函IO:IO Monad
由于泛函编程非常重视函数组合(function composition),任何带有副作用(side effect)的函数都无法实现函数组合,所以必须把包含外界影响(effectful)副作用不纯代码( ...
- 泛函编程(38)-泛函Stream IO:IO Process in action
在前面的几节讨论里我们终于得出了一个概括又通用的IO Process类型Process[F[_],O].这个类型同时可以代表数据源(Source)和数据终端(Sink).在这节讨论里我们将针对Proc ...
- 泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink
上期我们讨论了IO处理过程:Process[I,O].我们说Process就像电视信号盒子一样有输入端和输出端两头.Process之间可以用一个Process的输出端与另一个Process的输入端连接 ...
- 泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
IO处理可以说是计算机技术的核心.不是吗?使用计算机的目的就是希望它对输入数据进行运算后向我们输出计算结果.所谓Stream IO简单来说就是对一串按序相同类型的输入数据进行处理后输出计算结果.输入数 ...
- 泛函编程(30)-泛函IO:Free Monad-Monad生产线
在上节我们介绍了Trampoline.它主要是为了解决堆栈溢出(StackOverflow)错误而设计的.Trampoline类型是一种数据结构,它的设计思路是以heap换stack:对应传统递归算法 ...
- 泛函编程(28)-粗俗浅解:Functor, Applicative, Monad
经过了一段时间的泛函编程讨论,始终没能实实在在的明确到底泛函编程有什么区别和特点:我是指在现实编程的情况下所谓的泛函编程到底如何特别.我们已经习惯了传统的行令式编程(imperative progra ...
- 泛函编程(27)-泛函编程模式-Monad Transformer
经过了一段时间的学习,我们了解了一系列泛函数据类型.我们知道,在所有编程语言中,数据类型是支持软件编程的基础.同样,泛函数据类型Foldable,Monoid,Functor,Applicative, ...
- 泛函编程(25)-泛函数据类型-Monad-Applicative
上两期我们讨论了Monad.我们说Monad是个最有概括性(抽象性)的泛函数据类型,它可以覆盖绝大多数数据类型.任何数据类型只要能实现flatMap+unit这组Monad最基本组件函数就可以变成Mo ...
随机推荐
- lua使用io.open跨平台文件夹遍历匹配查找
-- Desc :实现在LUA_PATH中的lua文件中遍历寻找没用到PNG_PATH路径下的png图片,并将其打印出来. -- Date :12:49:28 2014-09-04 1 print(& ...
- Atitit atiuse软件系列
Atitit atiuse软件系列 1.1. Atian inputmethod 输入法 方言与多语言多文字支持 (au)1 1.2. File searcher 文件搜索器,支持压缩文件与正则表达 ...
- Atitit 微信支付 支付结果通用通知
Atitit 微信支付 支付结果通用通知 Wechat hto sh ma 返回页面return_url - 熊佳佳的博客 d ,only notyfi url-... 接口链接 该链接是通过[统一 ...
- Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Which of the following statement(s) is(are) correct?
Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Whi ...
- python入门学习课程推荐
最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...
- windows phone 存储图片 数据库写法
byte[] _DiseaseImage; [Column(DbType = "Image", UpdateCheck = UpdateCheck.Never)] //保证图像超出 ...
- 再谈collections模块defaultdict()和namedtuple()
defaultdict()和namedtuple()是collections模块里面2个很实用的扩展类型.一个继承自dict系统内置类型,一个继承自tuple系统内置类型.在扩展的同时都添加了额外的很 ...
- SQL*Loader之CASE8
CASE8 1. SQL脚本 [oracle@node3 ulcase]$ cat ulcase8.sql set termout off rem host write sys$output &quo ...
- bootstrap插件思路整理
知识有时也需温故知新嘛,本次做一次bs插件梳理. $.support.transition 通过判断自定义元素是否支持WebkitTransition.MozTransition.OTransitio ...
- Floyd算法(一)之 C语言详解
本章介绍弗洛伊德算法.和以往一样,本文会先对弗洛伊德算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 弗洛伊德算法介绍 2. 弗洛伊德算法图解 3 ...