3

For example:

{-# LANGUAGE UnicodeSyntax #-}

data Symbolic n
  = Constant n
  | Variable String
  | Symbolic n :+ Symbolic n
  | Symbolic n :* Symbolic n
  | Symbolic n :◁ Symbolic n
  deriving (Show)

This code loaded successfully in GHCi.

Then I input:

Constant 2 :* Variable "a"

That's OK.

But when I input:

Constant 2 :◁ Variable "a"

*** Exception: : hPutChar: invalid argument (invalid character)

Does this mean that Haskell does not support Unicode symbol constructor?

How to make it support Unicode symbol constructor?

Environment: GHCi 8.6.3 (WinGHCi) Windows 7

Thanks.

New observation:

It works when ◁ appeared in source file, but you can not input ◁ in REPL (I use WinGHCi).

chansey
  • 1,266
  • 9
  • 20
  • 4
    Unicode output on windows terminals is a persistent and annoying problem - the correct answer depends on which combination of shell (i.e. powershell or cmd.com) and terminal (e.g. ConEmu, Console2, the default windows one whatever that is called?) you're running (it's not really Haskell specific but most windows programs are built to either deal with or sidestep this issue, while most of the GHC people are on various *nixes). If you don't have a particularly fancy setup something as simple as running `chcp 65001` before ghci might work – Cubic Mar 26 '19 at 10:55
  • [See also.](https://stackoverflow.com/q/10779149/791604) – Daniel Wagner Mar 26 '19 at 11:53

2 Answers2

5

The problem is not with your code, or constructors, but simply that your environment is not set up so that Haskell can print unicode characters. Try

Prelude> putStrLn "\9731"

and you should see the same problem.

I am not an expert on Unicode and Windows, but presumably you can fix this by setting a LANG=C.utf8 environment variable, or similar.

Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
  • https://gitlab.haskell.org/ghc/ghc/issues/8118 might be relevant. And https://gitlab.haskell.org/ghc/ghc/issues/4471. And a large number of other tickets :-/ – Joachim Breitner Mar 26 '19 at 09:30
  • Thanks, but what do you mean about "by setting a LANG=C.utf8 environment variable"? Does this means "system environment variable"? (e.g: In windows7, Control Panel Home -> System Properties -> Environment Variables -> System variables) Or other configuration of Haskell? Sorry I'm not very familiar with Haskell. (PS: I have set System variables LANG=C.utf8, but doesn't work) – chansey Mar 26 '19 at 09:38
  • And I am not familiar with Windows, sorry. I hope someone else can help you out here. – Joachim Breitner Mar 26 '19 at 09:56
0

Following @Cubic, this issue can be workaround by running chcp 65001 before GHCi.

I use stack in windows 7 command line:

X:\you-path> chcp 65001

X:\you-path> stack ghci

*Main> Constant 2 :◁ Variable "a"

Constant 2 :◁�� Variable "a"

It works.

chansey
  • 1,266
  • 9
  • 20