Reputation: 459
I have a project for Uni to write a compiler (in Haskell) for a simple made-up imperative language. One of the requirements is printing debug statements on entering a function call, leaving a function and assigning variables.
Printing messages when entering functions is easy, I just use Debug.trace, eg:
functionValue = trace "Entering function" (evaluateFunction functionArguments)
The same process applies when assigning to variables. What I can't figure out is how to print when returning from a function call and have the output timed correctly with the other outputs. Every attempt I've made so far has resulted in "Leaving function" being printed immediately after "Entering function" - I need the function's internal debug statements (assigning and nested function calls) to be printed before "Leaving function" is printed.
My imperative habits tell me that I need a way to force execution of (evaluateFunction functionArguments) before the leave-function output, but this seems impossible and wrong in Haskell.
Example output I get now:
Entering main function...
Leaving main function...
Entering fn1 function...
Leaving fn1 function...
Assigning value1 to A.
Assigning value2 to C.
Entering fn2 function...
Leaving fn2 function...
Assigning value3 to B.
Assigning value4 to C.
Same program's output the way I need it to look:
Entering main function...
Entering fn1 function...
Assigning value1 to A.
Leaving fn1 function...
Assigning value2 to C.
Entering fn2 function...
Assigning value3 to B.
Assigning value4 to C.
Leaving fn2 function...
Leaving main function...
So, what's Haskell idiom for 'run myFunctionWithTraces then print myString'?
Upvotes: 2
Views: 1401
Reputation: 459
[The code was unreadable in a comment so I posted another answer as a comment to Cat Plus Plus]
I've finally threaded the IO monad through all my code, but this solution doesn't quite work - I need to get return values (ie. IO (value)) out of my functions.
do
putStrLn $ "Entering " ++ name
f
putStrLn $ "Leaving " ++ name
The above snippet will return IO () (the empty IO monad). So I modified it to be:
do
putStrLn $ "Entering " ++ name
returnVal <- f
putStrLn $ "Leaving " ++ name
return (returnVal)
But this is now printing:
inside function actions... ... ... Entering function Leaving function
Edit: The incorrect output was getting was my fault: I accidentally put result <- f
before putStrLn "Entering...
Upvotes: 1
Reputation: 129764
If you want to immediately print traces, you can lift the function to IO monad, and put it between two putStr
s, e.g.
trace :: String -> IO () -> IO ()
trace name f = do
putStrLn $ "Entering " ++ name
f
putStrLn $ "Leaving " ++ name
And then:
main = trace "main" $ do
fn1
fn2
fn1 = trace "fn1" $ do
return ()
fn2 = trace "fn2" $ do
return ()
This also can be done purely, with the Writer
monad (i.e. don't print, but just accumulate debugging output as you go). trace
would then look more like this:
trace :: String -> Writer String () -> Writer String ()
trace name f = do
tell $ "Entering " ++ name ++ "\n"
f
tell $ "Leaving " ++ name ++ "\n"
and with additional step of unwrapping the debug output with runWriter
or execWriter
.
Edit: generalising trace
to IO a
is not too difficult:
trace :: String -> IO a -> IO a
trace name f = do
putStrLn $ "Entering " ++ name
ret <- f
putStrLn $ "Leaving " ++ name
return ret
main = trace "main" $ do
a <- fn1
b <- fn2
print $ a + b
fn1 = trace "fn1" $ do
return 42
fn2 = trace "fn2" $ do
return 69
Upvotes: 5