ghci 8.0.1の話。
マルチバイト文字が"\12411\12370"のようにエスケープされて表示されるが、
ghci> "ほげ"
"\12411\12370"
これを
ghci> "ほげ"
"ほげ"
と表示させるようにしたい。
まず~/MyPrint.hsを作成:
module MyPrint (myPrint, myShow) where
-- preparing for the 7.6.1
myPrint :: Show a => a -> IO ()
myPrint = putStrLn . myShow
myShow :: Show a => a -> String
myShow x = con (show x) where
  con :: String -> String
  con [] = []
  con li@(x:xs) | x == '\"' = '\"':str++"\""++(con rest)
                | x == '\'' = '\'':char:'\'':(con rest')
                | otherwise = x:con xs where
                  (str,rest):_ = reads li
                  (char,rest'):_ = reads li
シェルでaliasを設定:
(.ghciで設定する方がベターだと思うのだが、Haskell超初心者なのでその書き方も分からなかった)
alias ghci="ghci -interactive-print=MyPrint.myPrint MyPrint -i$HOME"
これで一応はできたのだが、:loadや:reloadを使うと元に戻ってしまう。
これは-interactive-printの仕様らしい:
The function can reside in any loaded module or any registered package, but only when it resides in a registered package will it survive a :cd, :add, :load, :reload or, :set.
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#using-a-custom-interactive-printing-function
ghciをカラー化するツールもあるので、それをベースにエスケープされた文字列をアンエスケープする方法もある…らしい。
https://wiki.haskell.org/GHCi_in_colour
参考
- http://stackoverflow.com/a/14461928/5209556
- haskell - How to maintain -interactive-print after :load or :reload in ghci? - Stack Overflow
- #11159 ('-interactive-print myPrint' forgotten after :load or :reload) – GHC
- haskell - How to hack GHCi (or Hugs) so that it prints Unicode chars unescaped? - Stack Overflow

