I have the following Haskell code using overlapping instances; I tried to implement a function which yield the funcion's type as a String, or -- more generally speaking -- does different things for different function types:
{-# OPTIONS_GHC -fglasgow-exts #-}
{-# LANGUAGE OverlappingInstances, IncoherentInstances #-}
module Test
where
data TypeString = MKTS String
instance Show TypeString where
show (MKTS s) = s
class ShowType b c where
theType :: (b -> c) -> TypeString
instance ShowType b b where
theType _ = MKTS "b -> b"
instance ShowType b c where
theType _ = MKTS "b -> c"
instance ShowType (b, b') c where
theType _ = MKTS "(b, b') -> c"
class Problem a where
showProblem :: a -> TypeString
instance Problem (b -> c) where
showProblem f = theType f
Haskell shows the expected behavior when I type
> theType (uncurry (+))
(b,b') -> c
But: Can anyone explain the following:
> showProblem (uncurry (+))
b -> c
... and explain, how to avoid such situations where Haskell chooses a too general instance...