How to avoid stack overflow when using scalaz's free monad?

亡梦爱人 提交于 2020-01-01 09:13:40

问题


I had previously thought that part of the goal of the implementation was to avoid this very problem, so maybe I'm doing something obviously dumb?

Here is some code:

    // Stack overflow
import scalaz._

sealed trait Command[T]
case class Wait(ms: Long) extends Command[Unit]

case object Evaluator extends (Command ~> Id.Id) {
  override def apply[T](cmd: Command[T]) = cmd match {
    case Wait(t)  => Thread.sleep(t)
  }
}

object Api {
  def sleep(ms: Long): Free.FreeC[Command, Unit] = Free.liftFC(Wait(ms))
}

val sleep: Free.FreeC[Command, Unit] =
  Api.sleep(1).flatMap { _ => sleep }

Free.runFC(sleep)(Evaluator)

Note: I realize this is silly :) In practice, my command class has many commands, and I have a command which does this same loop...basically, poll some state, if true abort, if false, keep waiting.

I want to avoid the stack overflow that this causes... I THOUGHT this was already trampolined, but I guess I need to manually do it again? Is there a clean way to do it within the free monad way of thinking?

Update:

Thinking further on this, I think the issue isn't the sleep Free Monad but rather the Id.Id monad taht we bind into on evaluation... so I tried something like:

case object Evaluator2 extends (Command ~> ({ type t[x] = Free[Id.Id, x] })#t) {
  override def apply[T](cmd: Command[T]) = cmd match {
    case Wait(t)  => Thread.sleep(t); Free.liftF[Id.Id, Unit](())
  }
}

Free.runFC[Command, ({ type t[x] = Free[Id.Id, x] })#t, Unit](sleep)(Evaluator2)(Free.freeMonad[Id.Id])

But the problem with this is that it will only evaluate one step. Ideally I would like runFC to block until some condition is satisfied (or in this case, to loop forever until I kill it, but without a stack overflow)


回答1:


The Id monad is not trampolined. You end up in an infinite mutual recursion between the bind method for the Id monad and the foldMap method for the free monad. Use Trampoline or Task instead of Id.




回答2:


Since @Apocalisp's answer, there has been addition of BindRec typeclass and foldMapRec method that can be used for stack-safe evaluation directly to Id (or any other "tail-recursive" monad). For details, read Stack Safety for Free.



来源:https://stackoverflow.com/questions/29660067/how-to-avoid-stack-overflow-when-using-scalazs-free-monad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!