When to use STRef or IORef?
Asked Answered
C

2

24

What exactly is the difference between STRef and IORef and when do I use each of them? As far as I can tell they both are for mutable state so whats the point of both of them existing?

Caster answered 7/12, 2013 at 8:23 Comment(0)
S
31

You can do more things in the IO monad than in the ST monad. The latter provides mutable references, the former provides mutable references, exception catching, threads, and of course IO.

It is usually good Haskell practice to use the "weakest" or "more restricted" tool available that can solve your problem, because "weaker" tools tend to be easier to understand and analyze (another place this principle crops up in Haskell is in the Applicative versus Monad distinction).

So, if you only need mutable references, use ST. Future maintainers will be able to infer more about what your function does (and doesn't do) just by looking at the type.

An example situation in which you are forced to use IORefs (or their cousins MVars) is when having to share a mutable reference between two different execution threads.

Also keep in mind that you can escape ST (which means you can run ST computations inside pure functions) but you can't escape IO.

Softshoe answered 7/12, 2013 at 8:59 Comment(4)
The last point is, IMHO, the most important one: ST lets you safely use imperative features while providing a pure interface.Sherborne
What exactly does ST computations inside pure function look like? Can you give an example of escape?Caster
@Caster You escape ST using the runST function. The Haskellwiki page on ST has some examples: haskell.org/haskellwiki/Monad/ST See also the RankNTypes extension: fpcomplete.com/school/to-infinity-and-beyond/pick-of-the-week/…Softshoe
ST not only has mutable references, but also mutable arraysVendetta
E
10

They each provide the same functionality, but for different monads. Use IORef if you need a managed ref in IO, and STRef if you need one in ST s.

EDIT: a brief example:

import Control.Monad.ST
import Data.IORef
import Data.STRef

exampleSTRef :: ST s Int
exampleSTRef = do
  counter <- newSTRef 0
  modifySTRef counter (+ 1)
  readSTRef counter

exampleIORef :: IO Int
exampleIORef = do
  counter <- newIORef 0
  modifyIORef counter (+ 1)
  putStrLn "im in ur IO monad so i can do I/O"
  readIORef counter
Elul answered 7/12, 2013 at 8:28 Comment(2)
What's s in exampleSTRef?Prolusion
@Prolusion Check this answer for an explanation.Elul

© 2022 - 2024 — McMap. All rights reserved.