Show for String 的实例是怎么写的?

How is the instance of Show for String written?

我有一个关于定义类型 classes 实例的基本问题。我以 Show typeclass 为例,我只考虑 class 中的函数 show。像 Bool 这样的具体类型的 Show 实例很简单

instance Show Bool where
  show x = {function of x here}

但对于 String 则不是:

instance Show String where
  show x = {function of x here}

产生一个可以理解的错误

Illegal instance declaration for ‘Formatter String’
  (All instance types must be of the form (T t1 ... tn)
   where T is not a synonym.
   Use TypeSynonymInstances if you want to disable this.)
In the instance declaration for ‘Formatter String’

当然不允许以下内容:

instance Show [Char] where
  show x = {function of x here}

我可以定义一个新类型

newtype String2 = String2 String 
instance Formatter String2 where
  format (String2 x) = {function of x here}

但是不允许我显示 "test",因为我可以在 Haskell 中显示。

我缺少class类型的什么基本特征?

Show类型类实际上有3个成员函数,showshowsPrecshowList。在 Show Char 的实例中,showList 函数被重载以输出引号并将所有字母拼在一起,没有分隔符:

来自 GHC.Show:

instance  Show Char  where
    showsPrec _ '\'' = showString "'\''"
    showsPrec _ c    = showChar '\'' . showLitChar c . showChar '\''

    showList cs = showChar '"' . showLitString cs . showChar '"'

其中 showLitString 定义为:

showLitString :: String -> ShowS
-- | Same as 'showLitChar', but for strings
-- It converts the string to a string using Haskell escape conventions
-- for non-printable characters. Does not add double-quotes around the
-- whole thing; the caller should do that.
-- The main difference from showLitChar (apart from the fact that the
-- argument is a string not a list) is that we must escape double-quotes
showLitString []         s = s
showLitString ('"' : cs) s = showString "\\"" (showLitString cs s)
showLitString (c   : cs) s = showLitChar c (showLitString cs s)

所以没有Show String实例,只是Show Char具体定义了如何在[Char]值上调用show