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'?