[Haskell-cafe] Inheritance and Wrappers

Daniel Fischer daniel.is.fischer at googlemail.com
Mon Jan 31 21:24:54 CET 2011


On Monday 31 January 2011 20:58:02, MattMan wrote:
> tldr: Can I make arbitrary instances of one class instantiate another
> without using wrappers?
>
> I'm new to Haskell, and am trying to implement some simple typeclasses
> for doing algebra.  For example I have type class (simplified for the
> sake of argument)
>
> class AbGroup a where
>     add :: a -> a -> a
>
> I would like any type instantiating Num to also be an abelian group:
>
> instance (Num a) => AbGroup a where
>  add i j = i+j,
>
> but this doesn't compile because a is a variable, not a type
> constructor(or something).  The problem is fixed, in a sense, If I
> introduce a wrapper class
>
> data Wrapper a = Wrap a
> instance (Num a) => AbGroup (Wrapper a) where
>      add (Wrap i) (Wrap j) = Wrap(i+j)
>
> However, this is clumsy.  Is there something else I can do?  Thanks!

Not really. You could enable the FexibleInstances extension, then your 
first instance would compile. But it would mean 'every type is an instance 
of AbGroup, and if you try to use it with something that isn't also a Num, 
it won't compile, also you can't have any other instances', not what you 
want. You could then also enable OverlappingInstances, which would allow 
you to write other instances, but that extension is widely regarded as 
dangerous (have to confess, I forgot what the dangers were, one was that 
instance selection doesn't always do what you want/expect).

Wrapper instances or specific instances are probably the best way (specific 
instances to be generated by a preprocessor or TH).



More information about the Haskell-Cafe mailing list