[Haskell-cafe] Reverse Show instance
ryani.spam at gmail.com
Fri May 20 03:17:31 CEST 2011
Think of it this way:
-- Here is some data representing the typeclass 'Show'
data ShowDict a = ShowD (a -> String)
show :: ShowDict a -> a -> String
show (ShowD f) a = f a
-- Here's a sample implementation for Strings
showString :: ShowDict String
showString = ShowD (\s -> "\"" ++ escape s ++ "\"") where
escape = concatMap escapeChar
escapeChar '\\' = "\\\\"
escapeChar '"' = "\\\""
escapeChar c = [c]
-- Here's an implementation for pairs that uses the implementation for each
piece of the pair
showPair :: ShowDict a -> ShowDict b -> ShowDict (a,b)
showPair (ShowD sa) (ShowD sb) = ShowD (\(a,b) -> "(" ++ sa a ++ ", " ++ sb
b ++ ")")
-- Here is what you are asking for
implementMe :: ShowDict (a,b) -> ShowDict a
implementMe = ????
On Thu, May 19, 2011 at 2:08 PM, Andrew Coppin
<andrewcoppin at btinternet.com>wrote:
> Cannot deduce (Show x) from context (Show (x, y)).
> Cannot deduce (Show y) from context (Show (x, y)).
> Um... seriously?
> From Prelude, we have
> Show x, Show y => Show (x, y)
> So clearly it works in the forward direction. But apparently not in the
> reverse direction.
> Is this a bug or a feature? (I.e., is there some obscure possibility I
> haven't thought of which means that doing the reverse inference would be
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe