[RFC] Support Unicode characters in instance Show String

Viktor Dukhovni ietf-dane at dukhovni.org
Sun Jul 11 04:01:11 UTC 2021


On Thu, Jul 08, 2021 at 06:11:28PM +0800, Kai Ma wrote:

> 
> It's proposed here to change the Show instance of String, to achieve the following output:
> 
>     ghci> print "Hello, 世界”
>     "Hello, 世界”
>     
>     ghci> print "Hello, мир”
>     "Hello, мир”
>     
>     ghci> print "Hello, κόσμος”
>     "Hello, κόσμος”
>     
>     ghci> "Hello, 世界”      
>     “Hello, 世界”
>     
>     ghci> "😀” 
>     “😀"

Another possibility is to extend the `Show` class with two new methods
and their default implementations:

    class Show where
        ...
        showsPrecUnicode :: Int -> a -> ShowS
        showsPrecUnicode = showsPrec

        showListUnicode :: [a] -> ShowS
        showListUnicode = showList

    showUnicode :: Show a => a -> String
    showUnicode x = showsPrecUnicode 0 x ""

at which point a small number of classes can override `showUnicode`
and `showListUnicode`:

    instance Show a => Show [a] where
        showsPrec _      = showList
        showsPrecUnicode = showListUnicode

    instance Show Char where
        showsPrecUnicode = ... -- Unicode char
        showListUnicode  = ... -- Unicode string 

    instance Show Text where
        showsPrecUnicode = ... -- Unicode text

Once these are implemented, "ghci" can be modified to instead used
`showUnicode`, rather than `show`, with no new incompatibilities
elsewhere.

We can also introduce `uprint = putStrLn . showUnicode`, ...

This would still require explicit opt-in to use the Unicode show,
but it would be available for all `Show` instances, and used by
default in "ghci".

It would still be a good idea to implement `Render`, which is a related
but separate concern.

-- 
    Viktor.


More information about the Libraries mailing list