by idnefju · 7y ago · 0 comments
> “The IO monad does not make a function pure. It just makes it obvious that it’s impure.”
https://groups.google.com/forum/#!topic/scala-debate/xYlUlQA...
fn() + fn()
// ----
val a = fn()
a + a
Are the two programs above equivalent? For a function returning IO, they are. For a function that's side effectful, they are not.(As an exercise for the reader, think of our function here returning Future/Promise instead of an IO, how would that change the program?)
This is a definition of purity that you can work with. The trap that people fall in is to talk about fiction essentially. What is "meaningful" anyway? If you don't give it a definition, is it meaningful to talk about what's meaningful?
I agree that a function returning IO is opaque. You can't reinterpret it, it's no longer a plan that you can change the interpreter for, but something concrete. There are of course better abstractions for dealing with effects in a way as to make them less opaque ... like the Free Monad, or Eff.
Many of them have been expressed in Scala as well.
In a typical IO function, `fn()` only gives me 10% of the code in `fn`: the part up to the first bind. To get the rest, I need to `do` it, and this is where purity is no longer meaningful:
a <- fn "a"
b <- fn "b"
Can I swap these two lines ? The IO monad tells me there might be consequences, but what are they ? Another example: foo fn
Will `foo` trigger the IO effects of `fn` zero, one or more times ?I believe these are meaningful questions, and yet, a program built from pure functions can answer them no better than an imperative program.
https://typelevel.org/cats-effect/datatypes/io.html
Also see my comment here: