Reference types

Simon Peyton-Jones simonpj@microsoft.com
Thu, 7 Feb 2002 01:19:06 -0800


Wow.  One innocent message...

| Oh no, please don't do this! I use the RefMonad class, but=20
| *without* the dependency r -> m. Why not? Because I want to=20
| manipulate (for example) STRefs in monads built on top of the=20
| ST monad via monad transformers. So I use the same reference=20
| type with *many different* monads! Your change would make=20
| this impossible.

I don't think so.


There were really two parts to my message:

1.  Have a single built-in type (Ref), rather than two (IORef and
STRef).
I don't see how that can be anything other than a Good Thing, can it?
The primitive operations over Refs are still non-overloaded, as before:
	newIORef :: a -> IO (Ref IO a)
	newSTRef :: a -> ST s (Ref (ST s) a)
	...etc...

2.  Have a RefMonad type class to overload the new, read, write
operations over Refs.   But nothing in my message precludes also
having a RefMonad2 class with two parameters, and whatever=20
functional dependencies (or lack of them) that you like

	class RefMonad2 r m where
	   new :: a -> m (r a)
	   read :: r a -> m a
	   write :: r a -> a -> m ()

	instance RefMonad2 (Ref IO) IO where ...
	instance RefMonad2 (Ref (ST s)) (ST s) where ...

So then it's only a question of who gets the name "RefMonad"!
(Incidentally, so far as I know, RefMonad isn't in any of the existing
libraries.)


So I conclude:
	(1) is a win=09
	(2) is a question of what we name the new (and undoubtedly
	useful) class that I tendentiously called RefMonad

Or am I missing something?

Simon