Reference types

Olaf Chitil
Tue, 05 Feb 2002 16:33:59 +0000

I applaud the simplification. Getting rid of multi parameter classes and
functional dependencies is a Good Thing.

Unfortunately I am worried about the extensibility of the new scheme.
You replace two types, IORef a and STRef s a by a single type:

>         data Ref m a    -- References in monad m, values of type a

I suppose the implementation of the two original types is actually the
same, so that they can be merged. The m is a phantom type argument.

How, however, can I now define an instance of RefMonad for my own Monad?

>         class RefMonad m where
>           newRef   :: a -> m (Ref m a)
>           readRef  :: Ref m a -> m a
>           writeRef :: Ref m a -> a -> m ()

If I remember Koen Claessen correctly, it is actually impossible to
define your own references in Haskell, even inefficient ones, without
builtin references.

Nonetheless, I might want for example to define a GUI monad, with an
embedded IO monad. 

data GUI a = GUI (State -> IO (State,a))

Then I would like to define the newRef, readRef, writeRef operations on
Ref GUI a in terms of the operations on Ref IO a. But I don't see any
way to do that. Probably some kind of type conversion between various
Ref m a types would be needed; the existence of such a type conversion
function probably leads to other nasty problems...

Any solutions?

 Dept. of Computer Science, The University of York, York YO10 5DD, UK. 
 Tel: +44 1904 434756; Fax: +44 1904 432767