I want to use Debug.Trace.trace to print something which I know is a Show. Just like I would do in Python etc.
One solution is to add “Show a =>” to the signature of the function where I want to put the trace, and to any function calling it, etc.
But it would had been much nicer if I could use some debugShow function which calls show if the value has one, otherwise returns "--no show--" or something.
Here’s my failed attempt to define a DebugShow (GHC rejects "Duplicate instance declarations"):
{-# LANGUAGE FlexibleInstances, UndecidableInstances, OverlappingInstances #-}
class DebugShow a where
debugShow :: a -> String
instance Show a => DebugShow a where
debugShow = show
instance DebugShow a where
debugShow = const "--no show--"
Some kind of “unsafe cast” would also solve my problem.
Any advice?
Note – This is only for debugging purposes. I’m not using this in any finished code.
I’m not absolutely certain of this, but I think this is impossible without adding a class context to the entire call chain between the use site and the call site where each type variable is determined. The reason for this is operational: at least in GHC, each class is implemented by a dictionary. So at the use site we need a
Showdictionary foraif one exists at all. But to get this we need it to have been passed down from the site whereawas determined, and this requires something to be in the signature of all the intermediate functions.