Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Scala have an IO Monad?

I'm wondering why Scala does not have an IO Monad like Haskell.

So, in Scala the return type of method readLine is String whereas in Haskell the comparable function getLine has the return type IO String.

There is a similar question about this topic, but its answer it not satisfying:

Using IO is certainly not the dominant style in scala.

Can someone explain this a bit further? What was the design decision for not including IO Monads to Scala?

like image 705
kiritsuku Avatar asked Feb 18 '12 23:02

kiritsuku


People also ask

What is IO monad Scala?

IO Monad is simply a Monad which: Allows you to safely manipulate effects. Transform the effects into data and further manipulate it before it actually gets evaluated.

Is Scala Option A monad?

We've used Option as an example above because Scala's Option type is a monad, which we will cover in more detail soon.

Is IO a monad?

The IO type constructor provides a way to represent actions as Haskell values, so that we can manipulate them with pure functions. In the Prologue chapter, we anticipated some of the key features of this solution. Now that we also know that IO is a monad, we can wrap up the discussion we started there.

Is IO monad pure?

“The IO monad does not make a function pure. It just makes it obvious that it's impure.”


1 Answers

Because Scala is not pure (and has no means to enforce that a function is pure, like D has) and allows side effects. It interoperates closely with Java (e.g. reuses big parts of the Java libraries). Scala is not lazy, so there is no problem regarding execution order like in Haskell (e.g. no need for >> or seq). Under these circumstances introducing the IO Monad would make life harder without gaining much.

But if you really have applications where the IO monad has significant advantages, nothing stops you from writing your own implementation or to use scalaz. See e.g. http://apocalisp.wordpress.com/2011/12/19/towards-an-effect-system-in-scala-part-2-io-monad/

[Edit]

Why wasn't it done as a lazy and pure language?

This would have been perfectly possible (e.g. look at Frege, a JVM language very similar to Haskell). Of course this would make the Java interoperability more complicate, but I don't think this is the main reason. I think a lazy and pure language is a totally cool thing, but simply too alien to most Java programmers, which are the target audience of Scala. Scala was designed to cooperate with Java's object model (which is the exact opposite of pure and lazy), allowing functional and mixed functional-OO programming, but not enforcing it (which would have chased away almost all Java programmers). In fact there is no point in having yet another completely functional language: There is Haskell, Erlang, F# (and other MLs) and Clojure (and other Schemes / Lisps), which are all very sophisticated, stable and successful, and won't be easily replaced by a newcomer.

like image 67
Landei Avatar answered Oct 18 '22 09:10

Landei