[Haskell-cafe] Can sub-classes inheriting instances of parent class?
Szekeres István
szekeres at iii.hu
Sat Jul 25 18:02:43 EDT 2009
Hi,
I'm trying to do something like the following:
class Foo a where
doit :: (String -> IO ()) -> a -> IO ()
instance Foo Int where
doit f = f . show
instance Foo Char where
doit f = f . (:[])
So now I have a "doit" function for Ints and Chars, that takes a function
and does the IO with the given function. Now I could define function p as
p :: (Foo a) => a -> IO ()
p = doit putStrLn
so I could do
main = do
p 3
p 'X'
in main, but I would like to compeltely hide the fact from p that it somehow
belongs to the Foo class, so I would like to do something like this:
class Foo a => Bar a where
p2 :: a -> IO ()
p2 = doit putStrln
but, though Foo Int and Foo Char are defined, Bar Int and Bar Char are not
defined automatically, so given this class definition I will not be able to
call
p2 3
and
p2 'X'
Is there still some way to do something like this?
Thanks,
Istvan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090725/a300e6b4/attachment.html
More information about the Haskell-Cafe
mailing list