[Haskell-cafe] Type-class conditional behavior
stephen.tetley at gmail.com
Sun May 8 08:58:50 CEST 2011
On 8 May 2011 06:14, Nicholas Tung <ntung at ntung.com> wrote:
> Dear all,
> I'd like to write a function "maybeShow :: a -> Maybe String", which
> runs "show" if its argument is of class Show.
I'm pretty sure this is not readily possible - there might be some
hack through Typeable but that would oblige but Show and Typeable
constraints on the type of "a".
> The context and motivation for this are as follows. I have a GADT type
> which encapsulates abstract-value computation (or constants or error codes),
> a snippet of which is below.
> data AV t where
> AVLeft :: AV a -> AV (Either a b)
> This is used to implement an arrow transformer, and due to Arrows
> mapping all Haskell functions, I cannot put some kind of qualification on
> the constructor, like "AVLeft :: Show a => ...".
Yes you can, from the GHC docs:
data Showable where
MkShowable :: Show a => a -> Showable
More information about the Haskell-Cafe