RE: Typesafe MRef with a regular monad
You can't overwrite an entry with a value of a different type, because the keys are typed! Any more than you can overwrite an IORef with a value of a different type. S | -----Original Message----- | From: Ralf Hinze [mailto:ralf@informatik.uni-bonn.de] | Sent: 06 June 2003 15:01 | To: Simon Peyton-Jones; Tim Sweeney; haskell@haskell.org; Ashley Yakeley | Subject: Re: Typesafe MRef with a regular monad | | Am Freitag, 6. Juni 2003 15:47 schrieb Simon Peyton-Jones: | > Yes, one *could* use dynamic types. But the check would always succeed! | | Why is that? If I overwrite an entry with a value of a different type, | then the check fails. I am certainly missing something ... | | Cheers, Ralf |
Am Freitag, 6. Juni 2003 16:09 schrieb Simon Peyton-Jones:
You can't overwrite an entry with a value of a different type, because the keys are typed! Any more than you can overwrite an IORef with a value of a different type. S
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.
module TypedFM where import Prelude hiding (lookup) import qualified Prelude import Dynamics
data FM k = FM [(k, Dynamic)] data Key k a = Key k (Type a)
empty :: FM k empty = FM []
insert :: (Typable a) => FM k -> k -> a -> (FM k, Key k a) insert (FM bs) k a = (FM ((k, Dyn rep a) : bs), Key k rep)
lookup :: (Eq k) => FM k -> Key k a -> Maybe a lookup (FM bs) (Key k rep) = case Prelude.lookup k bs of Nothing -> Nothing Just dy -> cast dy rep
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? Cheers, Ralf
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-)
Keith Wansbrough <Keith.Wansbrough@cl.cam.ac.uk> wrote in article <E19PLTP-0002fT-00@wisbech.cl.cam.ac.uk> in gmane.comp.lang.haskell.general:
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.
Discussing this with Oleg, I realized that this signature is not sound. (fm1, key) = insert empty 42 undefined value_in = 1 :: Int fm2 = update fm1 key value_in Just value_out = lookup fm2 key :: Char -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig * "Harry Potter is a sexist neo-conservative autocrat." -- Pierre Bruno, Liberation (cf. ISBN 1-85984-666-1) * Return junk mail in the postage-paid response envelope included. * Insert spanners randomly in unjust capitalist machines.
participants (4)
-
Keith Wansbrough -
Ken Shan -
Ralf Hinze -
Simon Peyton-Jones