I’ve read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the “type” Array itself should be defined, they only gave definitions for the functions using monads to access or modify this type.
How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell ?! (such as STUArray and MArray)
I’ve read many research papers on this topic, and they usually argue that arrays
Share
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
primitivepackage for Haskell arrays (inIOorST), you can see that the implementations is in terms of GHC’s primops:That is, in terms of:
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
STmonad and primitives for mutable arrays.