Why is in-place mutation represented with `IO`?

165 Views Asked by At

Why are mutable data structures and other mutability represented using IO in functional languages? I'm looking at e.g. Haskell's IORef or Idris' IOArray.

I don't think I mean this as a historical or design question. I don't quite understand why IO is suitable for mutation - or rather, why mutation becomes pure when encapsulated in IO.

1

There are 1 best solutions below

7
On

You don't need to represent them with IO. It's possible to do them in ST instead. But you can obviously represent them with IO, where any dirty side effect can be achieved. So if you're working in IO anyway, it's easiest to just do the mutations there too, this way you don't need to put any worry on welding different monads together. If you're not already working in IO, you should use ST however.