[Haskell-cafe] Type-class conditional behavior
ntung at ntung.com
Sun May 8 07:14:27 CEST 2011
I'd like to write a function "maybeShow :: a -> Maybe String", which
runs "show" if its argument is of class Show.
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 => ...".
Of course any replies are welcome, but I do need something implemented
and stable. If there are GHC-compatible hacks, even an "unsafeShow :: a ->
String", that'd be great. I'd also prefer not to branch on all types which
could possibly be maybeShow's argument.
(Concretely, if I have "newtype AVFunctor a b c = AVF (a (AV b) (AV
c))", then the Arrow class declaration forces all types, c.f. variable b, to
be potential variables of type AV),
class (Category a) => Arrow a where
arr :: (b -> c) -> a b c
Thanks very much,
Nicholas — https://ntung.com — CS major @ UC Berkeley
p.s. I posted this question on StackOverflow if you care to get brownie
points there, http://goo.gl/PrmYW
p.s. 2 -- if there is a general "dump var" function in ghci, which does more
than ":info", I'd love to know :)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe