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.
Perhaps you want some variant of:
traceShow :: (Show a) => a -> b -> b
traceShow = trace . show
defined in Debug.Trace
The constraint "calls show if the value has one, otherwise returns "--no show--" or something" is a hard one. You'll need overlapping (and incoherent) instances, I think, to define a default Show for all types (Perhaps via unsafeCoerce, or via vacuum).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With