Haskell: Printing TextEncoding

Haskell is new here.

  $ ghc --version
 The Glorious Glasgow Haskell Compilation System, version 6.12.1

When trying to debug a strange locale-related error in a third-party Haskell program, I try to print the standard encoding:

import System.IO main = do print localeEncoding 

But he fails:

  $ ghc -o printlocale main.hs
 main.hs: 4: 2:
     No instance for (Show TextEncoding)
       arising from a use of `print 'at main.hs: 4: 2-21
     Possible fix: add an instance declaration for (Show TextEncoding)
     In the expression: print localeEncoding
     In the expression: do {print localeEncoding}
     In the definition of `main ': main = do {print localeEncoding}

My google-fu is failing. What am I missing?

+4
source share
1 answer

To print the value of a type in Haskell, the type must be an instance of the Show class.

 localeEncoding :: TextEncoding 

and TextEncoding is not an instance of Show.

The TextEncoding type is actually an existential type that stores encoding and decoding methods:

 data TextEncoding = forall dstate estate . TextEncoding { mkTextDecoder :: IO (TextDecoder dstate), mkTextEncoder :: IO (TextEncoder estate) } 

Since these are functions, there is no reasonable way to show them. The current localeEncoding is determined using iconv through the C function nl_langinfo.

So, TextEncoding as such is not a visible type, so you cannot print it. However, you can create new values โ€‹โ€‹of this type through mkTextEncoding. For instance. to create utf8 environment:

 mkTextEncoding "UTF-8" 

We might consider requesting a function to store a language representation using TextEncoding so that this shortcut can be printed. However, this is currently not possible.

+5
source

All Articles