[Haskell-cafe] Type-class conditional behavior

Stephen Tetley 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:

http://haskell.org/ghc/docs/7.0-latest/html/users_guide/data-type-extensions.html#gadt

  data Showable where
    MkShowable :: Show a => a -> Showable



More information about the Haskell-Cafe mailing list