[Haskell-cafe] Reverse Show instance
Ryan Ingram
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
> incorrect?)
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110519/514f3af6/attachment.htm>
More information about the Haskell-Cafe
mailing list