How to hack GHCi (or Hugs) so that it prints Unicode chars unescaped?

前端 未结 7 1903
情话喂你
情话喂你 2020-12-05 00:26

Look at the problem: Normally, in the interactive Haskell environment, non-Latin Unicode characters (that make a part of the results) are printed escaped, even if the locale

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 01:05

    Option 1 (bad):

    Modify this line of code:

    https://github.com/ghc/packages-base/blob/ba98712/GHC/Show.lhs#L356

    showLitChar c s | c > '\DEL' =  showChar '\\' (protectEsc isDec (shows (ord c)) s)
    

    And recompile ghc.

    Option 2 (lots of work):

    When GHCi type checks a parsed statement it ends up in tcRnStmt which relies on mkPlan (both in https://github.com/ghc/ghc/blob/master/compiler/typecheck/TcRnDriver.lhs). This attempts to type check several variants of the statement that was typed in including:

    let it = expr in print it >> return [coerce HVal it]
    

    Specifically:

    print_it  = L loc $ ExprStmt (nlHsApp (nlHsVar printName) (nlHsVar fresh_it))
                                          (HsVar thenIOName) placeHolderType
    

    All that might need to change here is printName (which binds to System.IO.print). If it instead bound to something like printGhci which was implemented like:

    class ShowGhci a where
        showGhci :: a -> String
        ...
    
    -- Bunch of instances?
    
    instance ShowGhci Char where
        ...  -- The instance we want to be different.
    
    printGhci :: ShowGhci a => a -> IO ()
    printGhci = putStrLn . showGhci
    

    Ghci could then change what is printed by bringing different instances into context.

提交回复
热议问题