在前面几次讨论中我们介绍了Free是个产生Monad的最基本结构。它的原理是把一段程序(AST)一连串的运算指令(ADT)转化成数据结构存放在内存里,这个过程是个独立的功能描述过程。然后另一个独立运算过程的Interpreter会遍历(traverse)AST结构,读取结构里的运算指令,实际运行指令。这里的重点是把一连串运算结构化(reify)延迟运行,具体实现方式是把Monad的连续运算方法flatMap转化成一串Suspend结构(case class),把运算过程转化成创建(construct)Suspend过程。flatMap的表现形式是这样的:flatMap(a => flatMap(b => flatMap(c => ....))),这是是明显的递归算法,很容易产生堆栈溢出异常(StackOverflow Exception),无法保证程序的安全运行,如果不能有效解决则FP编程不可行。Free正是解决这个问题的有效方法,因为它把Monad的递归算法flatMap转化成了一个创建数据结构实例的过程。每创建一个Suspend,立即完成一个运算。我们先用个例子来证明Monad flatMap的递归算法问题:

 ef zipIndex[A](xa: List[A]): List[(Int,A)] =
xa.foldLeft(State.state[Int,List[(Int,A)]](List()))(
(acc,a) => for {
xn <- acc
s <- get[Int]
_ <- put[Int](s+)
} yield ((s,a) :: xn)
).eval().reverse //> zipIndex: [A](xa: List[A])List[(Int, A)] zipIndex( |-> ) //> res6: List[(Int, Int)] = List((1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10))
zipIndex( |-> ) //> java.lang.StackOverflowError

在这个例子里我们使用了State Monad。我们知道for-comprehension就是flatMap链条,是一种递归算法,所以当zipIndex针对大List时就产生了StackOverflowError。

我们提到过用Trampoline可以heap换stack,以遍历数据结构代替递归运算来实现运行安全。那么什么是Trampoline呢?

 sealed trait Trampoline[+A]
case class Done[A](a: A) extends Trampoline[A]
case class More[A](k: () => Trampoline[A]) extends Trampoline[A]

Trampoline就是一种数据结构。它有两种状态:Done(a: A)代表结构内存放了一个A值,More(k: ()=>Trampoline[A])代表结构内存放的是一个Function0函数,就是一个运算Trampoline[A]。

我们先试个递归算法例子:

 def isEven(xa: List[Int]): Boolean =
xa match {
case Nil => true
case h :: t => isOdd(t)
} //> isEven: (xa: List[Int])Boolean
def isOdd(xa: List[Int]): Boolean =
xa match {
case Nil => false
case h :: t => isEven(t)
} //> isOdd: (xa: List[Int])Boolean isOdd( |-> ) //> res0: Boolean = true
isEven( |-> ) //> java.lang.StackOverflowError

可以看到isEven和isOdd这两个函数相互递归调用,最终用大点的List就产生了StackOverflowError。

现在重新调整一下函数isEven和isOdd的返回结构类型:从Boolean换成Trampoline,意思是从返回一个结果值变成返回一个数据结构:

 def even(xa: List[Int]): Trampoline[Boolean] =
xa match {
case Nil => Done(true)
case h :: t => More(() => odd(t))
} //> even: (xa: List[Int])Exercises.trampoline.Trampoline[Boolean]
def odd(xa: List[Int]): Trampoline[Boolean] =
xa match {
case Nil => Done(false)
case h :: t => More(() => even(t))
} //> odd: (xa: List[Int])Exercises.trampoline.Trampoline[Boolean] even( |-> ) //> res0: Exercises.trampoline.Trampoline[Boolean] = More(<function0>)

现在我们获得了一个在heap上存放了123001个元素的数据结构More(<function0>)。这是一个在内存heap上存放的过程,并没有任何实质运算。
现在我们需要一个方法来遍历这个返回的结构,逐个运行结构中的function0:

 sealed trait Trampoline[+A] {
final def runT: A =
this match {
case Done(a) => a
case More(k) => k().runT
}
} even( |-> ).runT //> res0: Boolean = false

由于这个runT是个尾递归(Tail Call Elimination TCE)算法,所以没有出现StackOverflowError。

实际上scalaz也提供了Trampoline类型:scalaz/Free.scala

  /** A computation that can be stepped through, suspended, and paused */
type Trampoline[A] = Free[Function0, A]
...
object Trampoline extends TrampolineInstances { def done[A](a: A): Trampoline[A] =
Free.Return[Function0,A](a) def delay[A](a: => A): Trampoline[A] =
suspend(done(a)) def suspend[A](a: => Trampoline[A]): Trampoline[A] =
Free.Suspend[Function0, A](() => a)
}

Trampoline就是Free[S,A]的一种特例。S == Function0,或者说Trampoline就是Free针对Function0生成的Monad,因为我们可以用Free.Return和Free.Suspend来实现Done和More。我们可以把scalaz的Trampoline用在even,odd函数里:

 import scalaz.Free.Trampoline
def even(xa: List[Int]): Trampoline[Boolean] =
xa match {
case Nil => Trampoline.done(true)
case h :: t => Trampoline.suspend(odd(t))
} //> even: (xa: List[Int])scalaz.Free.Trampoline[Boolean]
def odd(xa: List[Int]): Trampoline[Boolean] =
xa match {
case Nil => Trampoline.done(false)
case h :: t => Trampoline.suspend(even(t))
} //> odd: (xa: List[Int])scalaz.Free.Trampoline[Boolean] even( |-> ).run //> res0: Boolean = false

语法同我们自定义的Trampoline差不多。

那么我们能不能把Trampoline用在上面的哪个zipIndex函数里来解决StackOverflowError问题呢?zipIndex里造成问题的Monad是个State Monad,我们可以用State.lift把State[S,A升格成StateT[Trampoline,S,A]。先看看这个lift函数:scalaz/StateT.scala

 def lift[M[_]: Applicative]: IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] = new IndexedStateT[({type λ[α]=M[F[α]]})#λ, S1, S2, A] {
def apply(initial: S1): M[F[(S2, A)]] = Applicative[M].point(self(initial))
}

这个函数的功能等于是:State.lift[Trampoline] >>> StateT[Tarmpoline,S,A]。先看另一个简单例子:

 def incr: State[Int, Int] =  State {s => (s+, s)}//> incr: => scalaz.State[Int,Int]
incr.replicateM().eval() take //> java.lang.StackOverflowError import scalaz.Free.Trampoline
incr.lift[Trampoline].replicateM().eval().run.take()
//> res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

上面这个例子也使用了State Monad:函数incr返回的是State,这时用replicateM(10000).eval(0)对重复对10000个State进行运算时产生了StackOverflowError。我们跟着用lift把incr返回类型变成StateT[Trampoline,S,A],这时replicateM(10000).eval(0)的作用就是进行结构转化了(State.apply:Trampoline[(S,A)]),再用Trampoline.run作为Interpreter遍历结构进行运算。用lift升格Trampoline后解决了StackOverflowError。

我们试着调整一下zipIndex函数:

 def safeZipIndex[A](xa: List[A]): List[(Int,A)] =
(xa.foldLeft(State.state[Int,List[(Int,A)]](List()))(
(acc,a) => for {
xn <- acc
s <- get[Int]
_ <- put(s + )
} yield (s,a) :: xn
).lift[Trampoline]).eval().run.reverse //> safeZipIndex: [A](xa: List[A])List[(Int, A)] safeZipIndex( |-> ).take() //> res2: List[(Int, Int)] = List((1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10))

表面上来看结果好像是正确的。试试大一点的List:

  safeZipIndex( |-> ).take()   //> java.lang.StackOverflowError
//| at scalaz.IndexedStateT$$anonfun$flatMap$1.apply(StateT.scala:62)
//| at scalaz.IndexedStateT$$anon$10.apply(StateT.scala:95)
//| at scalaz.IndexedStateT$$anonfun$flatMap$1.apply(StateT.scala:62)
...

还是StackOverflowError,看错误提示是State.flatMap造成的。看来迟点还是按照incr的原理把foldLeft运算阶段结果拆分开来分析才行。

以上我们证明了Trampoline可以把连续运算转化成创建数据结构,以heap内存换stack,能保证递归算法运行的安全。因为Trampoline是Free的一个特例,所以Free的Interpreter也就可以保证递归算法安全运行。现在可以得出这样的结论:FP就是Monadic Programming,就是用Monad来编程,我们应该尽量用Free来生成Monad,用Free进行编程以保证FP程序的可靠性。

Scalaz(35)- Free :运算-Trampoline,say NO to StackOverflowError的更多相关文章

  1. Scalaz(53)- scalaz-stream: 程序运算器-application scenario

    从上面多篇的讨论中我们了解到scalaz-stream代表一串连续无穷的数据或者程序.对这个数据流的处理过程就是一个状态机器(state machine)的状态转变过程.这种模式与我们通常遇到的程序流 ...

  2. Scalaz(47)- scalaz-stream: 深入了解-Source

    scalaz-stream库的主要设计目标是实现函数式的I/O编程(functional I/O).这样用户就能使用功能单一的基础I/O函数组合成为功能完整的I/O程序.还有一个目标就是保证资源的安全 ...

  3. Scalaz(44)- concurrency :scalaz Future,尚不完整的多线程类型

    scala已经配备了自身的Future类.我们先举个例子来了解scala Future的具体操作: import scala.concurrent._ import ExecutionContext. ...

  4. Smarty数学运算

    数学运算可以直接应用到变量 Example 3-5. math examples 例 3-5.数学运算的例子   {$foo+1} {$foo*$bar} {* some more complicat ...

  5. C/C+小记

    1.struct与typedef struct struct Student{int a;int b}stu1; //定义名为Student的结构体,及一个Student变量stu1 struct { ...

  6. java实现随机四则运算

    使用JAVA编程语言,独立完成一个包含3到5个数字的四则运算练习,软件基本功能要求如下: 程序可接收一个输入参数n,然后随机产生n道加减乘除练习题,每个数字在 0 和 100 之间,运算符在3个到5个 ...

  7. kotlin递归&尾递归优化

    递归: 对于递归最经典的应用当然就是阶乘的计算啦,所以下面用kotlin来用递归实现阶乘的计算: 编译运行: 那如果想看100的阶乘是多少呢? 应该是结果数超出了Int的表述范围,那改成Long型再试 ...

  8. Scalaz(50)- scalaz-stream: 安全的无穷运算-running infinite stream freely

    scalaz-stream支持无穷数据流(infinite stream),这本身是它强大的功能之一,试想有多少系统需要通过无穷运算才能得以实现.这是因为外界的输入是不可预料的,对于系统本身就是无穷的 ...

  9. Scalaz(56)- scalaz-stream: fs2-安全运算,fs2 resource safety

    fs2在处理异常及资源使用安全方面也有比较大的改善.fs2 Stream可以有几种方式自行引发异常:直接以函数式方式用fail来引发异常.在纯代码里隐式引发异常或者在运算中引发异常,举例如下: /函数 ...

随机推荐

  1. H5常用代码:适配方案5

    此方案跟方案4是同一原理,也是通过REM实现的,能单独归类出一个方案,是因为它有一定的实用价值,当你遇到追求完美,追求到一像素的UI或者产品时,那此方案将解决你的困境. 方案5主要是用来解决一像素边框 ...

  2. Atitti 图像处理 图像混合 图像叠加 blend 原理与实现

    Atitti 图像处理 图像混合 图像叠加 blend 原理与实现 混合模式 编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 混合模式是图像处理技术中的一个技术名词,不 ...

  3. salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载

    目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新公司,主要做的就是salesforce,不过当时想要看一些相关资料确实比较难.为了避免想要零基础学习的人 ...

  4. PHP两种redirect

    PHP两种redirect redirect header('Location: /admin_data.php'); exit(); redirect `echo "<script& ...

  5. struts之动态方法调用使用通配符

    一.DMI动态方法调用的其中一种改变form表单中action属性的方式已经讲过了.还有两种,一种是改变struts.xml配置文件中action标签中的method属性,来指定执行不同的方法处理不同 ...

  6. PHP 对象 “==” 与 “===”

    php中对象在内存中的存储方式与java等其他面向对象语言类似,$a = new Person();在内存中表现为$a是堆区中new Person()中的引用 这样当: $a = new Person ...

  7. IO流-文本IO\读写二进制数据

    文本IO 一.简述 OutputStreamWriter类使用选定的编码方式吧Unicode字符流转换为字节流,InputStreamReader类将包含字节的输入流转为可以产生Unicode字符的读 ...

  8. 邻接表有向图(三)之 Java详解

    前面分别介绍了邻接表有向图的C和C++实现,本文通过Java实现邻接表有向图. 目录 1. 邻接表有向图的介绍 2. 邻接表有向图的代码说明 3. 邻接表有向图的完整源码 转载请注明出处:http:/ ...

  9. c语言读取字符在记事本中出现次数

    程序使用说明: 此程序可以统计出名称为1.txt的文件中/出现的次数 但是仅支持单个英文字母和标点符号查询,不支持汉字. 在本程序生成的exe根目录下放一个1.txt, 即可查询出该字符在1.txt记 ...

  10. angularjs中 *.min.js.map 404的问题

    初次使用AngularJS,在chrom调试的时候,出现如下问题: GET http://localhost:63342/luosuo/visitor/js/lib/angular-animate.m ...