I am researching on this problem which related to MonadRef
. When look at the definition
class MonadRef r m | m -> r where
newRef :: a -> m (r a)
readRef :: r a -> m a
writeRef :: r a -> a -> m ()
I was thinking how to implement this with pure data structure but failed to find an answer. Actually, all the known standalone implementations (which does not depend on another MonadRef
)
instance TVar STM
instance IORef IO
instance (STRef s) (ST s)
require RealWorld
in pratise. Is that means we can only have MonadRef
s that are not pure?
When I tried to resolve this, I first figure out that Maybe
cannot implement MonadRef
simply because it is too simple and there is no space for it to record the required information. As a generalise of this I conclude that for any implementation the Monad
must be able to contain arbitrary number of information, simply because you can call newRef
as many times as you can.
So I considered []
but it is still a fail because the data stored within the Monad can be of any type. I don't know how to construct a container in Haskell that can store any type of data whilst still have the ability to extract the data back with proper type (maybe existential quantification can help? But I still don't know how to do so).