Typesafe MRef with a regular monad
Keith Wansbrough
Keith.Wansbrough@cl.cam.ac.uk
Mon, 09 Jun 2003 13:10:02 +0100
Ralf Hinze writes:
> Why is that? Ok, here is my second implementation. It uses the
> Dynamic module from our HW2002 paper. A key is a pair consisting
> of the actual key and a type representation.
[..]
> > update :: (Typable b) => FM k -> Key k a -> b -> (FM k, Key k b)
> > update (FM bs) (Key k _) b = (FM ((k, Dyn rep b) : bs), Key k rep)
>
> Does this fit the bill?
No, because update shouldn't return a new key, it should allow reuse
of the same key. Restating Simon PJ's original signature, and adding
update:
module TypedFM where
data FM k -- Abstract; finite map indexed bykeys 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 => FM k -> k -> a -> (FM k, Key k a)
lookup :: Ord k => FM k -> Key k a -> Maybe a
update :: Ord k => FM k -> Key k a -> a -> FM k
If updating gives you a new key, then you might as well just store the
value in the key. Instead, you keep the same key; and so you'd better
remain type-compatible.
--KW 8-)