Typesafe MRef with a regular monad
Simon Peyton-Jones
simonpj@microsoft.com
Fri, 6 Jun 2003 14:23:02 +0100
Oh bother, I forgot to add that you can of course insert a new value
with an old key (suitably typed) and have it overwrite. Else, as you
say, there would not be much point.
Maybe it'd be better to have a separate key-construction function=09
newKey :: k -> Key k a
instead of having insert return a key. =20
S
| -----Original Message-----
| From: Ralf Hinze [mailto:ralf@informatik.uni-bonn.de]
| Sent: 06 June 2003 14:12
| To: Simon Peyton-Jones; Tim Sweeney; haskell@haskell.org; Ashley
Yakeley
| Subject: Re: Typesafe MRef with a regular monad
|=20
| > A more concrete way to formulate a problem that I believe to be
| > equivalent is this. Implement the following interface
| >
| > module TypedFM where
| > data FM k -- Abstract; finite map indexed by keys
| > of type k
| > data Key k a -- Abstract; a key of type k, indexing a
| > value of type a
| >
| > empty :: FM k
| > insert :: Ord k =3D> FM k -> k -> a -> (FM k, Key k a)
| > lookup :: Ord k =3D> FM k -> Key k a -> Maybe a
| >
| > The point is that the keys are typed, like Refs are. But the finite
map
| > FM is only parameterised on k, not a, so it can contain (key,value)
| > pairs of many different types.
| >
| > I don't think this can be implemented in Haskell, even with
| > existentials. But the interface above is completely well typed, and
can
| > be used to implement lots of things. What I *don't* like about it
is
| > that it embodies the finite-map implementation, and there are too
many
| > different kinds of finite maps.
|=20
| Here is a Haskell 98 implementation:
|=20
| > module TypedFM
| > where
|=20
| > data FM k =3D FM
| > data Key k a =3D Key k a
|=20
| > empty =3D FM
| > insert FM k a =3D (FM, Key k a)
| > lookup FM (Key k a) =3D Just a
|=20
| Cheers, Ralf
|=20