Search code examples
haskelltracestderrfrege

Frege trace not printing


As the title says, for some reason, messages passed to the trace (well, a variant of which) function don't show up properly when debugging functions. Simply flushing stdout/stderr doesn't seem to do anything, either.

-- Makes it more like Haskell's trace
debug :: String -> α -> α
debug msg f = const f $ trace msg

-- dummy function
polyA :: (Num α) => α
polyA = debug "polyA\n" 0

-- another dummy function
polyB :: (Num α) => α
polyB = debug "polyB\n" polyA

main :: IO ()
main = do println (polyB :: Int    )
          println (polyB :: Int    )
          println (polyB :: Integer)

Output is just

0
0

with nothing visible in stderr (normally represented by red text in Eclipse's console).


Solution

  • As const doesn't use the second argument, trace doesn't get invoked. You could use seq or pattern match.

    If you change the debug function to this:

    debug msg f = trace msg `seq` f
    

    or to this:

    debug msg f | trace msg = undefined
                | otherwise = f
    

    It still won't print anything due to flushing so if you change the main to flush stderr:

    main :: IO ()
    main = do println (polyB :: Int    )
              println (polyB :: Int    )
              println (polyB :: Integer)
              stderr.flush
    

    It would work and it prints all the debug messages at the end:

    frege> main
    0
    0
    0
    polyA
    polyB
    polyA
    polyB
    polyA
    polyB
    ()
    

    As Ingo mentioned, we could also use traceLn to have it flushed automatically when the function is called.