Scalaz(35)- Free :运算-Trampoline,say NO to StackOverflowError

   在前面几次讨论中我们介绍了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的递归算法问题:

def 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+1)
    } yield ((s,a) :: xn)
  ).eval(1).reverse                               //> zipIndex: [A](xa: List[A])List[(Int, A)]

zipIndex(1 |-> 10)                                //> 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(1 |-> 10000)                             //> 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(0 |-> 100)                                  //> res0: Boolean = true
isEven(0 |-> 10000)                               //> 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(1 |-> 123001)                                //> 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(1 |-> 123001).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(1 |-> 123001).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+1, s)}//> incr: => scalaz.State[Int,Int]
incr.replicateM(10000).eval(0) take 10            //> java.lang.StackOverflowError

import scalaz.Free.Trampoline
incr.lift[Trampoline].replicateM(100000).eval(0).run.take(10)
                                                  //> 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 + 1)
    } yield (s,a) :: xn
  ).lift[Trampoline]).eval(1).run.reverse         //> safeZipIndex: [A](xa: List[A])List[(Int, A)]
  
  safeZipIndex(1 |-> 1000).take(10)               //> 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(1 |-> 10000).take(10)   //> 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程序的可靠性。







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值