1

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.

4
  • Most likely this would be better fit for langdev.stackexchange.com or softwareengineering.stackexchange.com ... Historical/design questions usually considered too broad for SO. Nov 16 at 22:11
  • @AlexeiLevenkov thanks. I don't think I mean it 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. Historical/design suggests that there was also a choice in the matter, which I wasn't aware of
    – joel
    Nov 16 at 22:45
  • There are no mutations from the user's point of view. If you had a data Op = Assign Int | Output and a program = [Assign 1, Assign 2, Output, Assign 42, Output] then you could undoubtedly create a function that takes program and outputs [2,42]. Does that mean Assign 2 has a side effect? No. Does it mean Output returns a different value every time? No. But still, if you create a list of them you can still somehow kinda-sorta program as if you have mutable state. Monads are basically fancy ways of creating (potentially infinite) sequences of such steps. Nov 17 at 1:06
  • @joel, I think your follow-up comment is key to understanding what you're asking here, so I've copied it into your question.
    – K. A. Buhr
    Nov 17 at 1:33

1 Answer 1

4

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.

7
  • I'm familiar with pretending to mutate variables with the state monad, but from the ST docs it looks like this is primarily a "mutation" monad. Is it any different from a state monad that's compiled to in-place mutation?
    – joel
    Nov 16 at 22:46
  • Yes, it's different. The ST monad supports compiler primitives that mutate memory locations (see the code for STRef). As far as I know, the State monad is never compiled to code that mutates state in memory, at least not with GHC. It would be better to think of ST as "real" mutation, with IO = ST + I/O operations.
    – K. A. Buhr
    Nov 16 at 23:01
  • @joel Try to implement newSTRef using State instead of ST (i.e. newStateRef :: YourChoiceOfContextIDon'tMind s => State s (StateRef a)) and you will see that they are quite different indeed. Nov 16 at 23:05
  • @joel In particular, even if some State computation were sometimes optimised to execute by mutation in place, that would be almost accidental. The API of State just deals in values, and indeed it's quite valid for earlier state values to still have references after "changes", so it obviously can't always be implemented as mutation. ST has an API that explicitly deals in references to mutable storage; you specify the mutations, they're not an accidental effect of optimisations.
    – Ben
    Nov 17 at 3:21
  • 1
    @joel Implementation detail. You can't (or aren't supposed to be able to) actually do IO in ST. runST is supposed to be a pure function.
    – HTNW
    Nov 18 at 2:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.