开发者

Defining a function a -> String, which works for types without Show?

开发者 https://www.devze.com 2023-03-27 02:42 出处:网络
I\'d like to def开发者_如何学JAVAine a function which can \"show\" values of any type, with special behavior for types which actually do define a Show instance:

I'd like to def开发者_如何学JAVAine a function which can "show" values of any type, with special behavior for types which actually do define a Show instance:

magicShowCast :: ?

debugShow :: a -> String
debugShow x = case magicShowCast x of
    Just x' -> show x'
    Nothing -> "<unprintable>"

This would be used to add more detailed information to error messages when something goes wrong:

-- needs to work on non-Showable types
assertEq :: Eq a => a -> a -> IO ()
assertEq x y = when (x /= y)
    (throwIO (AssertionFailed (debugShow x) (debugShow y)))

data CanShow = CanShow1
             | CanShow 2
    deriving (Eq, Show)

data NoShow = NoShow1
            | NoShow2
    deriving (Eq)

-- AssertionFailed "CanShow1" "CanShow2"
assertEq CanShow1 CanShow2

-- AssertionFailed "<unprintable>" "<unprintable>"
assertEq NoShow1 NoShow2

Is there any way to do this? I tried using various combinations of GADTs, existential types, and template haskell, but either these aren't enough or I can't figure out how to apply them properly.


The real answer: You can't. Haskell intentionally doesn't define a generic "serialize to string" function, and being able to do so without some type class constraint would violate parametricity all over town. Dreadful, just dreadful.

If you don't see why this poses a problem, consider the following type signature:

something :: (a, a) -> b -> a

How would you implement this function? The generic type means it has to be either const . fst or const . snd, right? Hmm.

something (x,y) z = if debugShow z == debugShow y then y else x
> something ('a', 'b') ()
'a'
> something ('a', 'b') 'b'
'b'

Oooooooops! So much for being able to reason about your program in any sane way. That's it, show's over, go home, it was fun while it lasted.


The terrible, no good, unwise answer: Sure, if you don't mind shamelessly cheating. Did I mention that example above was an actual GHCi session? Ha, ha.

import Control.Exception
import Control.Monad
import GHC.Vacuum

debugShow :: a -> String
debugShow = show . nameGraph . vacuumLazy

assertEq :: Eq a => a -> a -> IO ()
assertEq x y = when (x /= y) . throwIO . AssertionFailed $ 
    unlines ["assertEq failed:", '\t':debugShow x, "=/=", '\t':debugShow y]

data NoShow = NoShow1
            | NoShow2
    deriving (Eq)
> assertEq NoShow1 NoShow2
*** Exception: assertEq failed:
    [("|0",["NoShow1|1"]),("NoShow1|1",[])]
=/=
    [("|0",["NoShow2|1"]),("NoShow2|1",[])]

Oh. Ok. That looks like a fantastic idea, doesn't it.

Anyway, this doesn't quite give you what you want, since there's no obvious way to fall back to a proper Show instance when available. On the other hand, this lets you do a lot more than show can do, so perhaps it's a wash.

Seriously, though. Don't do this in actual, working code. Ok, error reporting, debugging, logging... that makes sense. But otherwise, it's probably very ill-advised.


I asked this question a while ago on the haskell-cafe list, and the experts said no. Here are some good responses,

  • http://www.haskell.org/pipermail/haskell-cafe/2011-May/091744.html
  • http://www.haskell.org/pipermail/haskell-cafe/2011-May/091746.html

The second one mentions GHC advanced overlap, but my experience was that it doesn't really work.

For your particular problem, I'd introduce a typeclass

class MaybeShow a where mshow :: a -> String

make anything that is showable do the logical thing

instance Show a => MaybeShow a where mshow = show

and then, if you have a fixed number of types which wouldn't be showable, say

instance MaybeShow NotShowableA where mshow _ = "<unprintable>"

of course you could abstract it a little,

class NotShowable a
instance NotShowable a => MaybeShow a where mshow _ = "<unprintable>"
instance NotShowable NotShowableA -- etc.


You shouldn't be able to. The simplest way to implement type classes is by having them compile into an extra parameter

 foo :: Show s => a -> s

turns into

 foo :: show -> a -> s

The program just passess around the type class instances (like v-tables in C++) as ordinary data. This is why you can trivially use things that look not just like multiple dispatch in OO languages, but can dispatch off the return type.

The problem is that a signature

 foo :: a -> String

has no way of getting the implementation of Show that goes for a in cases when it has one.

You might be able to get something like this to work in particular implementations, with the correct language extensions (overlapping instance, etc) on, but I havent tried it

 class MyShow a where
   myShow :: a -> String

 instance (Show a) => MyShow a where
   myShow = show

 instance MyShow a where
   myShow = ...

one trick that might help is enable type families. It can let you write code like

 instance (a' ~ a, Show a') => MyShow a

which can sometimes help you get code past the compiler that it doesn't think looks okay.

0

精彩评论

暂无评论...
验证码 换一张
取 消