[Haskell-cafe] Trouble with types

Konstantin Vladimirov konstantin.vladimirov at gmail.com
Tue Dec 25 00:11:34 EST 2007


The essence of trouble may be given by code:

module TypeTrouble where

class FirstClass a where
    firstFunction :: (SecondClass b) => a -> b

class SecondClass a where
    secondFunction :: a -> Double

data FirstData = FirstData Double

data SecondData = SecondData Double

instance SecondClass SecondData where
    secondFunction (SecondData d) = d

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

I need, the firstFunction of FirstClass types to return a value of a
SecondClass type. For example SecondData for FirstData, but for some
FirstClass ThirdData, some SecondClass FourthData, etc.

GHC 6.8.1 produces an error:

Couldn't match expected type `b' against inferred type `SecondData'
`b' is a rigid type variable bound by
the type signature for `firstFunction' at typetrouble.hs:4:31
In the expression: SecondData d
In the definition of `firstFunction':
firstFunction (FirstData d) = SecondData d
In the definition for method `firstFunction'

How can I make this idea work?

With best regards, Konstantin

More information about the Haskell-Cafe mailing list