simulating dynamic dispatch
Hal Daume III
hdaume@ISI.EDU
Thu, 20 Mar 2003 14:42:41 -0800 (PST)
hi all,
i'm hoping to be able to simulate a sort of dynamic dispatch based on
class instances. basically a function which takes a value and depending
on what classes it is an instance of, does something. a simple example
might be a 'maybeShow' function which sematically looks like:
> maybeShow x
> | typeOf x `instanceOf` Show = show x
> | otherwise = "?"
I've been trying for a while to simulate something along the lines of:
> class Foo a where { foo :: a -> Bool }
> class Bar a where { bar :: a -> Bool }
> foo x
> | typeOf x `instanceOf` Foo = Just (foo x)
> | typeOf x `instanceOf` Bar = Just (bar x)
> | otherwise = Nothing
and I tried using dynamics but those require instances of typeable on
universally quantified types, which doesn't work out too well in practice.
also, we can do class sums using existential types:
> data FB = forall a . Foo a => MkFoo a | forall a . Bar a => MkBar a
and i tried something like:
class FooBar a where
wasFoo :: a -> Maybe FB -- will be MkFoo
wasBar :: a -> Maybe FB -- will be MkBar
wasFoo _ = Nothing
wasBar _ = Nothing
instance Foo a => FooBar a where
wasFoo a = Just (MkFoo a)
instance Bar a => FooBar a where
wasBar a = Just (MkBar a)
but this complains about duplicate instance declarations (for obvious
reasons).
i'm hoping one of the type class gurus out there can help me.
Thanks in advance!
--
Hal Daume III | hdaume@isi.edu
"Arrest this man, he talks in maths." | www.isi.edu/~hdaume