How are mutable arrays implemented in Haskell? How are mutable arrays implemented in Haskell? arrays arrays

How are mutable arrays implemented in Haskell?


How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell

They are implemented via primitive operations in the runtime system for memory reads and writes.

The safety of the side effecting action of destructively writing to memory is ensured via the use of monads to linearize access to the mutable state.

Looking at the primitive package for Haskell arrays (in IO or ST), you can see that the implementations is in terms of GHC's primops:

-- | Create a new mutable array of the specified size and initialise all-- elements with the given value.newArray :: PrimMonad m => Int -> a -> m (MutableArray (PrimState m) a)newArray (I# n#) x = primitive   (\s# -> case newArray# n# x s# of             (# s'#, arr# #) -> (# s'#, MutableArray arr# #))-- | Read a value from the array at the given index.readArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> m areadArray (MutableArray arr#) (I# i#) = primitive (readArray# arr# i#)-- | Write a value to the array at the given index.writeArray :: PrimMonad m => MutableArray (PrimState m) a -> Int -> a -> m ()writeArray (MutableArray arr#) (I# i#) x = primitive_ (writeArray# arr# i# x)

That is, in terms of:

  • newArray#
  • readArray#
  • writeArray#

which are primitive (hardware accelerated ;) services for operating on memory provided by the language runtime.

Mechanisms for giving type safety to destructive memory effects were introduced to Haskell by the Launchbury and Peyton-Jones paper, Lazy Functional State Threads, which introduces the ST monad and primitives for mutable arrays.


As something of an aside, please keep in mind that "implemented using monads", as can be done for various control structures, is not really the same thing as "side effects isolated by monadic operations on an opaque type", as with IO or ST, where the properties of the monad merely ensure that pure code remains so.

The mutable data is provided as a runtime primitive, as Don Stewart explains; the only thing "implemented with monads" here is type safety.


They are implemented in the same way as in an imperative language; i.e., in-place update. The type system will guarantee that you can't do anything "bad" with them.