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.

Raymond Tay

October 04, 2019
Tweet

Other Decks in Programming

Transcript

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

    eff- monad, akka, akka-http, scala compiler.
  2. 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.
  3. What is needed … - Sequencing computations - Stack safety

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

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

    - Support evaluation modes - Lazy - Asynchronous - Strict
  6. 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.
  7. 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)
  8. 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 =======>
  9. 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