Search code examples
haskellghci

How to maintain -interactive-print after :load or :reload in ghci?


I'm using -interactive-print to print the current time after each line evaluated in ghci:

(from ~/.ghci):

import qualified Text.Show.TimePrint
:set -interactive-print=Text.Show.TimePrint.timePrint

It works until I try to :load a file:

$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude Text.Show.TimePrint> 1+2
3
20:08:42
Prelude Text.Show.TimePrint> :l file.hs
[1 of 1] Compiling Main             ( file.hs, interpreted )
Ok, modules loaded: Main.
*Main Text.Show.TimePrint> 1+2
3

I saw this ticket here that describes the problem: https://ghc.haskell.org/trac/ghc/ticket/11159; the proposed solution is to put it in a "registered package", which I don't exactly understand. I put it in Text.Show, is that not quite right? Thanks.

(The code for the package installed to do TimePrint:)

module Text.Show.TimePrint (timePrint) where

import System.IO
import Data.Time

getTime :: IO String
getTime = do
    now <- getCurrentTime
    return (formatTime defaultTimeLocale "%T" now)

timePrint :: Show a => a -> IO ()
timePrint a = do
    s <- getTime
    putStrLn $ show a ++ "\n" ++ s

Solution

  • You are using GHC 7.10.3; the ticket you linked says that this is fixed after 7.10.3. So you will need to upgrade your GHC to something newer.