I'd like a variant of show
(let's call it label
) that acts just like show
, except that it doesn't wrap String
s in " "
or Char
s in ' '
. Examples:
> label 5
"5"
> label "hello"
"hello"
> label 'c'
"c"
I tried implementing this manually, but I ran into some walls. Here is what I tried:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Label where
class (Show a) => Label a where
label :: a -> String
instance Label [Char] where
label str = str
instance Label Char where
label c = [c]
-- Default case
instance Show a => Label a where
label x = show x
However, because the default case's class overlaps instance Label [Char]
and instance Label Char
, those types don't work with the label
function.
Is there a library function that provides this functionality? If not, is there a workaround to get the above code to work?