sinまたはsinをラップしないことを除いて、と同じように動作するshow
(それを呼びましょう)のバリアントが欲しいのですが。例:label
show
String
" "
Char
' '
> label 5
"5"
> label "hello"
"hello"
> label 'c'
"c"
これを手動で実装しようとしましたが、いくつかの壁にぶつかりました。これが私が試したことです:
{-# 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
ただし、デフォルトのケースのクラスはと重複instance Label [Char]
しているためinstance Label Char
、これらの型は関数では機能しませんlabel
。
この機能を提供するライブラリ関数はありますか?そうでない場合、上記のコードを機能させるための回避策はありますか?