[Haskell-cafe] Trouble with types

Antoine Latter aslatter at gmail.com
Tue Dec 25 00:46:45 EST 2007


On Dec 25, 2007 12:11 AM, Konstantin Vladimirov
<konstantin.vladimirov at gmail.com> wrote:
>
> class FirstClass a where
>    firstFunction :: (SecondClass b) => a -> b
>

<snip!>

>
> instance FirstClass FirstData where
>    firstFunction (FirstData d) = SecondData d

The problem is that the type of firstFunction as producing a result of
type 'b', where 'b' is *any* type inahbiting the typeclass
"SecondClass."

Your definition of firstFunction can produce only *one* of the
inhabitants of "SecondClass".  You'd think this would be okay, seeing
as there is only one member of the typeclass, but because anyone can
come along and add a new typeclass instance, you're definition of
"firstFunction" needs to account for that.

If you're type-classes only ever have one instance, maybe it's easier
to not use them, and go with:

> convert :: FirstData -> SecondData
> convert (FirstData d) = SecondData d

But I'm not sure if that suggestion is at all helpful.

-Antoine


More information about the Haskell-Cafe mailing list