[Haskell-cafe] type classes
Jules Bean
jules at jellybean.co.uk
Fri Dec 14 09:48:19 EST 2007
Peter Padawitz wrote:
> I'd like to define several instances of the same type class with the
> same type variable instance. Only method instances differ. How can I do
> this without writing copies of the type class?
newtypes and modules have both been suggested.
I have another suggestion:
Don't!
Don't use typeclasses.
The only useful thing about typeclasses is that they are a kind of
type-indexed family of dictionaries. If you don't want to use the type
indexin, then don't use classes. Just use your own kind of dictionary.
E.g., instead of:
class Foo a where { bar :: a -> Int; baz :: a -> String }
instance Foo Double ...
instance Foo Double ... -- bother, I wanted a different Double instance!
you should just have:
data Foo a = Foo { bar :: a -> Int, baz :: a -> String }
foo1 :: Foo Double
foo1 = Foo { ... }
foo2 :: Foo Double
foo2 = Foo { ... }
-- now I can have as many 'instances' for the same type as I want!
Jules
More information about the Haskell-Cafe
mailing list