-2

The Haskell typeclass show does not support unicode but the rest of the language does. I was unable to find a standard alternative typeclass for unicode. Does Haskell have a version of show for unicode?

This property is described in "How to make Haskell or ghci able to show Chinese characters and run Chinese characters named scripts?"

Community
  • 1
  • 1
44701
  • 397
  • 4
  • 10
  • 4
    Can you clarify what you mean by this? `instance Show Snowman where show _ = "☃"` certainly works in Haskell. – Ian Henry Jan 14 '16 at 01:50
  • You have misunderstood the answer to the linked question. The Show instance **for String** uses only printable ASCII characters **by choice** (in order to ensure the output is viewable in any terminal, I assume). That has nothing to do with the Show typeclass itself. You are perfectly free to define your own Show instances that produce non-ASCII output. – Reid Barton Jan 14 '16 at 02:44
  • 1
    @ReidBarton, the relevant instance is the one for `Char`. – dfeuer Jan 14 '16 at 02:47
  • Hi @Harpo it is uncivilly. You are delete question. I have spent much time to to answer your questions here http://stackoverflow.com/questions/35426726/does-the-hiredis-redis-library-create-its-own-thread-for-async-callbacks Question It is very difficult and interesting for people. People votes your quetion. Not each man can understand and solve that problem. It does not show you as mistake. Do not be shy. It show you as qualified developer asks dificult question. – oklas Feb 16 '16 at 10:23
  • The problem is real. Try entering `show 'α'` in repl. It outputs `"'\\945'"`. The same for `"α"`. Makes using Show for debugging hard if your data contains non-english text. – kolen Jan 04 '20 at 03:49

1 Answers1

4

I think this question is ill-posed. In show :: (Show a) => a -> String, String is a type synonym for [Char] and Char is a smart enough data type to represent any Unicode character.

The character type Char is an enumeration whose values represent Unicode (or equivalently ISO/IEC 10646) characters (see http://www.unicode.org/ for details). This set extends the ISO 8859-1 (Latin-1) character set (the first 256 characters), which is itself an extension of the ASCII character set (the first 128 characters). A character literal in Haskell has type Char.

This is the kind of power in simplicity you get when you choose simple data types and may come as a surprise to people immigrating from other languages where the default string type can only represent up to ASCII or UTF-16 or whatever else garbage.

So in that sense show already is the Unicode version of show. Whether or not String is an efficient representation of a Unicode, well that's another question entirely.

hao
  • 10,138
  • 1
  • 35
  • 50
  • I am not so certain as you about the nature of HarpoRoeder's misunderstanding; especially the clause "but the rest of the language does [support unicode]" makes me think he may not necessarily have fallen into the "`Char` is ASCII-only" trap. – Daniel Wagner Jan 14 '16 at 02:11
  • This is wrong. Show supports a subset of string. http://stackoverflow.com/a/14039878/5725848 – 44701 Jan 14 '16 at 02:19
  • 2
    @HarpoRoeder, the `show` and `showList` methods of the `Show` instance for `Char` escapes some characters. That has nothing to do with `show` or `showList` proper, and there is no restriction on what characters the instances you write produce. – dfeuer Jan 14 '16 at 02:45