simulating dynamic dispatch

Hal Daume III hdaume@ISI.EDU
Fri, 21 Mar 2003 14:29:12 -0800 (PST)


Excellent paper!  I have started using some of the techniques already!

But, back to the real question :).  I now no longer think such a thing is
possible.  Essentially what I want (given 'cast :: Typeable a, Typeable b
=> a -> Maybe b' [1]), is to write something like:

test :: forall a r . Typeable a =>
        (forall b . (Typeable b, Foo b) => b -> r) ->
        a -> Maybe r
test f a = liftM f (cast a)

but then 'b' is ambiguous.  I'm not sure there is any way around
this.  Can someone help?

 - Hal

[1] is there any reason why parens are requires around class
contexts?  i've always found this odd...

--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume

On Fri, 21 Mar 2003, Simon Peyton-Jones wrote:

> You might also find the 'cast' function in Section 3 of "Scrap your
> boilerplate" useful.
> http://research.microsoft.com/~simonpj/papers/hmap/
> I'm not certain, but it has the right smell.
> Simon
> 
> | -----Original Message-----
> | From: oleg@pobox.com [mailto:oleg@pobox.com]
> | Sent: 21 March 2003 04:19
> | To: hdaume@ISI.EDU; haskell@haskell.org
> | Subject: Re: simulating dynamic dispatch
> | 
> | 
> | > i'm hoping to be able to simulate a sort of dynamic dispatch based
> on
> | > class instances.
> | 
> | It seems you want to dispatch based not on a type but on the
> | constraint of a type.
> | 
> | You code almost worked. Here's the a bit updated and working version.
> | 
> | class Foo a where { foo :: a -> Bool }
> | class Bar a where { bar :: a -> Bool }
> | 
> | data FB = forall a . Foo a => MkFoo a | forall a . Bar a => MkBar a
> | 
> | instance Foo FB where
> |     foo (MkFoo x) = foo x
> | 
> | instance Bar FB where
> |     bar (MkBar x) = bar x
> | 
> | -- some instances for the test
> | instance Foo Int where
> |     foo x = x == 0
> | 
> | instance Bar Char where
> |     bar x = x == 'a'
> | 
> | 
> | test x = case x of
> |           (MkFoo a) -> Just $ foo a
> |           (MkBar a) -> Just $ bar a
> | --	  _         -> Nothing
> | 
> | 
> | -- *Main> test $ MkFoo (0::Int)
> | -- Just True
> | -- *Main> test $ MkFoo (10::Int)
> | -- Just False
> | -- *Main> test $ MkBar 'a'
> | -- Just True
> | -- *Main> test $ MkBar 'b'
> | -- Just False
> | 
> | _______________________________________________
> | Haskell mailing list
> | Haskell@haskell.org
> | http://www.haskell.org/mailman/listinfo/haskell
>