I am trying to declare my own data with the appropriate conversion as a class. My code is as follows:
data SomeData = SInteger Integer | SElse deriving Show class Some a where toSome :: a -> SomeData instance Some Int where toSome = SInteger . toInteger main :: IO () main = print $ toSome 3
But the GHC (7.0.3) is angry and says:
Ambiguous type variable `a0' in the constraints: (Some a0) arising from a use of `toSome' at minimal_broken.hs:11:16-21 (Num a0) arising from the literal `3' at minimal_broken.hs:11:23 Probable fix: add a type signature that fixes these type variable(s)
Explicit type signing (e.g. 3 :: Int) fixes the problem, but it is very inconvenient.
The standard "Show" works fine, and in accordance with the manual, he said exactly the same.
Why does the standard Show work, but my class does not work? Did I miss something?
PS: The explicit "default (Int)" does not allow this.
source share