How can I make typeclass about convertibility?

Viewed 153
class Convertible a b where
    convert :: a -> b

instance {-# OVERLAPPABLE #-} (Convertible a b, Convertible b c) => Convertible a c where
    convert = convert . convert             -- this is illegal


data Complex = Complex Float Float;
data Matrix2D = Matrix2D Float Float Float Float;
data Vector2D = Vector2D Float Float;

instance Convertible Complex Matrix2D where
    convert (Complex r i) =
        Matrix2D r (-i) i r

instance Convertible Complex Vector2D where
    convert (Complex r i) =
        Vector2D r i

instance Convertible Vector2D Complex where
    convert (Vector2D x y) =
        Complex x y

instance Convertible Float Matrix2D where
    convert x =
        Matrix2D x 0 0 x

I'm making Convertible typeclass for practice. It is typeclass about convertibility between a to b. The definition of convert is illegal because type b is could not be deduced from function call. How can I make Convertible works / or make similar typeclass?

0 Answers
Related