Write stack safe non-tailrec recursive functions

Write stack safe non-tailrec recursive functions

Recrusive function is cool, but it sometimes cause stack overflow in naive implementation. I'll explain how to write any recursive functions in stack safe way.

75086cfa4a46d4aa47deb38b409bd9cd?s=128

Jun Tomioka

June 29, 2019
Tweet

Transcript

  1. Write stack safe non-tailrec recursive functions @jooohn1234

  2. Jun Tomioka @jooohn1234 Software Engineer at M3, Inc.

  3. Recursion is cool

  4. Recursive data structures

  5. Recursive functions

  6. Recursive functions • Describe WHAT it is rather than HOW

    it’d be done
  7. Recursive functions • Describe WHAT it is rather than HOW

    it’d be done Cool! FP Programmers
  8. Stack Overflow

  9. Recursive functions • Tend to cause StackOverflowException

  10. Tail recursion • Make functions tailrec to avoid StackOverflow

  11. Tail recursion • Make functions tailrec to avoid StackOverflow Cool!

    FP Programmers
  12. Tail recursion • Make functions tailrec to avoid StackOverflow

  13. Tail recursion • Make functions tailrec to avoid StackOverflow It’s…

    cool! FP Programmers
  14. Tail recursion? • Make functions tailrec to avoid StackOverflow …

    but can we?
  15. Tail recursion? • Make functions tailrec to avoid StackOverflow …

    but can we?
  16. Tail recursion? • Make functions tailrec to avoid StackOverflow …

    but can we? It’s… still cool I believe! FP Programmers
  17. Tail recursion? • How many unique paths from S to

    G are there? S G
  18. Tail recursion? • Can you make this recursive function tailrec?

  19. Tail recursion? • Can you make this recursive function tailrec?

    ・・・ FP Programmers
  20. Give up

  21. Tail-recursion’s limitation • It tends to be difficult to convert

    recursive functions that depend on results of multiple recursive calls.
  22. Tail-recursion’s limitation • It tends to be difficult to convert

    recursive functions that depend on results of multiple recursive calls.
  23. Tail-recursion’s limitation • It tends to be difficult to convert

    recursive functions that depend on results of multiple recursive calls. Monad! FP Programmers
  24. The Monad version of recursion • This function still describes

    the problem’s structure intuitively, doesn’t it?
  25. The Monad version of recursion • This function still describes

    the problem’s structure intuitively, doesn’t it? Cool! FP Programmers
  26. The Monad version of recursion • Of course just rewriting

    the recursive function with Monad doesn’t mean it is stack safe.
  27. Trampoline

  28. Trampoline • > a trampoline is a loop that iteratively

    invokes thunk-returning functions ◦ https://en.wikipedia.org/wiki/Trampoline_(computing) #High-level_programming ◦ “Trampoline” in computer science has a lot of meanings. This is just one of them.
  29. Trampoline • A trampoline loop evaluates values one by one

    until it returns final result. • We can write this evaluation loop as tailrec! Eval evaluate Eval Result OR loop
  30. Eval * We typically implement “Defer”, “Suspend” or something like

    that to avoid stack overflow when building the Eval.
  31. Eval * We typically implement “Defer”, “Suspend” or something like

    that to avoid stack overflow when building the Eval. Represents single value
  32. Eval * We typically implement “Defer”, “Suspend” or something like

    that to avoid stack overflow when building the Eval. Has following evaluation
  33. Eval: trampoline

  34. Eval: trampoline

  35. [Optional] Memoization • This is not the essence, but memoization

    to reduce running time • In many cases, we can make such “What it is” style of recursive functions faster by using memoization
  36. None
  37. None
  38. Beautiful FP Programmers

  39. Run with Id

  40. Run with Eval

  41. Run with Eval Cool! FP Programmers

  42. Let’s implement stack safe foldRight with Eval

  43. Let’s implement stack safe foldRight with Eval

  44. Let’s implement stack safe foldRight with Eval !!! FP Programmers

  45. Let’s implement stack safe foldRight with Eval This recursive call

    of loop causes stack overflow before stack safe trampoline evaluation
  46. Use Defer to defer Eval instance construction

  47. Use Defer to defer building Eval instance

  48. Use Defer to defer building Eval instance Stack safe! FP

    Programmers
  49. Recap • Recursive functions is cool especially when it describes

    ”what” rather than ”how” • We can apply the trampoline technique to avoid stack overflow when tail-recursion is not effective
  50. Thank you!