Haskell Side Effects & IO
The cluster discusses Haskell's handling of side effects, purity in functional programming, and the IO monad's role in explicitly managing I/O without violating referential transparency.
Activity Over Time
Top Contributors
Keywords
Sample Comments
The reconciliation is simple: Monads don't allow side effects in pure functions (in Haskell). I know it's a very popular idea but it's also just wrong.(Yet another in the long list of Very Popular But Also Just Wrong ideas surrounding this not-that-complicated concept.)What the monadic interface to the IO type (very carefully phrased) in Haskell allows is building up a value to be interpreted at run time with a convenient API that looks very like it is an imper
Haskell doesn't prevent side effects. Otherwise, you wouldn't be able to do I/O. Instead, Haskell requires you to be explicit about side effects through Monads.I think about it this way: imperative languages allow side effects by default, but allow you to write side-effect free code. Purely functional languages don't use side-effects by default, but allow you to write code with side-effects.
Haskell isn't non-side-effecting. It's just a bit more sane and structured about when and how.
That's only if you don't count the IO itself as a side-effect.Does a language like Haskell give you the choice? If so,would it be idiomatic?
Same as Haskell and IO, really. Every interesting language has side-effects, Haskell just forces you to be explicit about where you're using them.
Not exactly. When using the I/O monad in a pure language like Haskell, your code doesn't directly have side effects, instead you're building a container (a monad) of instructions of what side effect having operations to do, and what should be done with the results. The Haskell runtime then takes this monad and unwraps it layer by layer, each layer executing the side effect having instruction and using the results of the side effects as input for unwrapping the next layer.You co
If Haskell I/O is pure, why can't it be intermingled with other pure functions?
OK, I think there's a fundamental misunderstanding at play here, of what purity, effects and monads are.Koka's effects system is monadic! Crazy huh? Well look: function main() { bind(foo, bar) } function foo() { println("Returning 3") return 3 } function bar(x) { println("Adding 1 to my argument and printing") y = x + 1 println(y) } function bind(x : (
That's allowed in the IO monad, and, really, it's allowed. The idea in Haskell is to have many contexts where side effects are not allowed so that in those contexts your code must be pure, and pure code is easier to write tests for and reason about.
Haskell lets you do side effects. The difference is that the type system carefully controls them.