Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing a Principled Approach to IO Monads in Scala

Developing a Principled Approach to IO Monads in Scala

The IO system in every programming language known today is crucial to a computer program's interaction with the outside world. For a long time, Java's approach to programming IO allows the programmer flexibility in interacting with the external systems; Scala, in this language's design, also allows the developer to apply a similar approach. However, when the software starts to dabble in multicore programming (e.g. starting threads to run tasks; these tasks start to execute IO) together with IO, then the situation becomes foggy. The developer should be aware that there is indeed another way to apply a principled approach to developing concurrent code which might or might not interact with IO. This talk will cover examples of how "traditional" multicore + IO code written in Scala can be re-written using another approach via the adaptation of the IO monad (found in the Haskell GHC) for Scala.

07a47618d92530d0136588d5adf46a62?s=128

Raymond Tay

October 04, 2019
Tweet

Transcript

  1. Developing a principled approach to programming IO in Scala Raymond

    Tay
  2. A little about me

  3. A little bit more about me Contributed to cats, dotty,

    eff- monad, akka, akka-http, scala compiler.
  4. Effects What is it? Anything but you are probably thinking

    about side-effect(s). Controlling how side-effects are effected is the purpose of the IO Monad.
  5. Effects (Scala 2)

  6. What is needed … - Sequencing computations - Stack safety

    - Support evaluation modes - Lazy - Asynchronous - Strict
  7. What is needed … - Sequencing computations - Stack safety

    - Support evaluation modes - Lazy - Asynchronous - Strict
  8. Stack execution

  9. Trampoline

  10. What is needed … - Sequencing computations - Stack safety

    - Support evaluation modes - Lazy - Asynchronous - Strict
  11. Getting started with IO

  12. What exactly is IO? A value of type IO[A] is

    a computation which, when evaluated, can perform effects before returning a value of type A IO values are pure, immutable values and thus preserves referential transparency, being usable in functional programming. An IO is a data structure that represents just a description of a side effectful computation.
  13. IO Typeclass Hierarchy

  14. Asynchronous & Cancelable

  15. Asynchronous & Cancelable in action ===== Start =======> => Concurrent-[scala-execution-context-global-180]

    Yes:5 => Concurrent-[scala-execution-context-global-178] Yes:2 => Concurrent-[scala-execution-context-global-181] Yes:4 Task-5 is no longer active canceled Task-3 is no longer active canceled Task-4 is no longer active canceled Task-6 is no longer active canceled => Concurrent-[scala-execution-context-global-179] Yes:3 Task-2 is no longer active canceled Task-8 is no longer active canceled Task-7 is no longer active canceled Task-9 is no longer active canceled Task-10 is no longer active canceled Task-1 is no longer active canceled … => Concurrent-[scala-execution-context-global-179] Yes:8 Left(Hello!_2)
  16. Asynchronous & Non-Cancelable

  17. Asynchronous & Non-Cancelable ===== Start =======> => Async-[scala-execution-context-global-179] Yes:2 =>

    Async-[scala-execution-context-global-179] Yes:3 … => Async-[scala-execution-context-global-179] Yes:4 => Async-[scala-execution-context-global-179] Yes:7 => Async-[scala-execution-context-global-179] Yes:8 Left(Hello!_2) ===== End =======>
  18. Cancelable

  19. Cancelable

  20. Error Handling

  21. Resource management

  22. Safely Acquire & Release

  23. Concurrent Modelling

  24. Concurrent Datatypes (I) - Buffered Channel

  25. Encoding the Datatype

  26. Modelling Concurrent Algos

  27. Buffered Channel in Action

  28. Concurrent Datatypes (II) - Skip Channel

  29. Skip Channel in action

  30. Logging with Cats-effect

  31. Setting the stage…

  32. Logging …

  33. Do I have to do this every time ?

  34. Gotchas

  35. Stack safety

  36. Cancellation assumes no-blocking

  37. Just because it compiles …

  38. Thank You JCConf 2019 ! @RaymondTayBL @https:/ /www.linkedin.com/in/ raymondtayboonleong

  39. References => code repo : https://github.com/raymondtay/JCConf_2019 => cats effect :

    https://typelevel.org/cats-effect => cats effect tutorial : https://typelevel.org/cats- effect/tutorial/tutorial.html => monix : http://monix.io/ => fs2 : https://fs2.io => log4cats : https://christopherdavenport.github.io/ log4cats/ => concurrent Haskell paper : https://www.microsoft.com/ en-us/research/wp-content/uploads/1996/01/concurrent- haskell.pdf