[Haskell-cafe] Re: Type question in instance of a class

Maurí­cio briqueabraque at yahoo.com
Tue Nov 18 02:25:03 EST 2008


> Perhaps you want something like:
> 
> class Complex r c | c -> r
>     where makeComplex :: r -> r -> c
>           realPart :: c -> r
>           imagPart :: c -> r
> 
> data ComplexNumber t = CN t t
> instance Complex t (ComplexNumber t)
>     where makeComplex = CN
>           realPart (CN r _) = r
>           imagPart (CN _ i) = i
> 
> newtype ComplexWithDouble = CWD (ComplexNumber Double)
>     deriving (Complex Double)
> 
> Having the parameters "backwards" is somewhat annoying, I suppose, but
> it's unavoidable if you're hoping to use generalized newtype deriving
> I believe.
> 

Great! Actually, I would never imagine that (ComplexNumber Double)
would yield a class with a single type variable... What is the logic
behind that? I know about kinds (of types). Do classes have "kinds"
of their own?

Thanks,
Maurício



More information about the Haskell-Cafe mailing list