Applicative vs. monadic combinators and the free monad in Scalaz

后端 未结 3 1315
醉酒成梦
醉酒成梦 2020-12-31 11:55

A couple of weeks ago Dragisa Krsmanovic asked a question here about how to use the free monad in Scalaz 7 to avoid stack overflows in this situation (I\'ve adapted his code

3条回答
  •  旧时难觅i
    2020-12-31 12:33

    There is a principled intuition for this difference.

    The applicative operator *> evaluates its left argument only for its side effects, and always ignores the result. This is similar (in some cases equivalent) to Haskell's >> function for monads. Here's the source for *>:

    /** Combine `self` and `fb` according to `Apply[F]` with a function that discards the `A`s */
    final def *>[B](fb: F[B]): F[B] = F.apply2(self,fb)((_,b) => b)
    

    and Apply#apply2:

    def apply2[A, B, C](fa: => F[A], fb: => F[B])(f: (A, B) => C): F[C] =
      ap(fb)(map(fa)(f.curried))
    

    In general, flatMap depends on the result of the left argument (it must, as it is the input for the function in the right argument). Even though in this specific case you are ignoring the left result, flatMap doesn't know that.

    It seems likely, given your results, that the implementation for *> is optimized for the case where the result of the left argument is unneeded. However flatMap cannot perform this optimization and so each call grows the stack by retaining the unused left result.

    It's possible that this could be optimized at the compiler (scalac) or JIT (HotSpot) level (Haskell's GHC certainly performs this optimization), but for now this seems like a missed optimization opportunity.

提交回复
热议问题