
Hello, I am using "import Debug.Trace" to debug some code but it is not working as expected. I believe this is due to lazy evaluation. But I cannot see how I can circumvent it. To exemplify I have the following function: tagFun :: (POSTags -> Tag) -> POSTags -> POSTags tagFun f corpus = Z.foldlz' tag Z.empty corpus where tag acc z = Z.insert newTag acc where (token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z) newTag = ( token, correctTag, proposed ) If I place tracing at: tag acc z = trace ("tag "++token++" = "++ show newTag) Z.insert newTag acc then it works ok. However the lines (token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z) don't work. They only appear once. I assume they should appear with every element in the zipper. Can anyone explain why? TIA, Hugo F.

On Monday 07 November 2011, 09:41:24, Hugo Ferreira wrote:
Hello,
I am using "import Debug.Trace" to debug some code but it is not working as expected. I believe this is due to lazy evaluation. But I cannot see how I can circumvent it. To exemplify I have the following function:
tagFun :: (POSTags -> Tag) -> POSTags -> POSTags tagFun f corpus = Z.foldlz' tag Z.empty corpus where tag acc z = Z.insert newTag acc where (token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z) newTag = ( token, correctTag, proposed )
If I place tracing at:
tag acc z = trace ("tag "++token++" = "++ show newTag) Z.insert newTag acc
then it works ok. However the lines
(token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z)
don't work. They only appear once. I assume they should appear with every element in the zipper. Can anyone explain why?
You're compiling with optimisations, aren't you? You need the trace strings to depend on the input, otherwise the optimiser sees no reason to repeat the work. (The constant part is lifted out of the lambda, a consequence of unsafePerformIO.) The line `trace "cursor " Z.cursor z' actually shouldn't print multiple traces even without optimisations, since it's (trace "cursor " Z.cursor) z so the trace applies to the evaluation of the function Z.cursor, which should only be done once.

On 11/07/2011 01:47 PM, Daniel Fischer wrote:
On Monday 07 November 2011, 09:41:24, Hugo Ferreira wrote:
Hello,
I am using "import Debug.Trace" to debug some code but it is not working as expected. I believe this is due to lazy evaluation. But I cannot see how I can circumvent it. To exemplify I have the following function:
tagFun :: (POSTags -> Tag) -> POSTags -> POSTags tagFun f corpus = Z.foldlz' tag Z.empty corpus where tag acc z = Z.insert newTag acc where (token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z) newTag = ( token, correctTag, proposed )
If I place tracing at:
tag acc z = trace ("tag "++token++" = "++ show newTag) Z.insert newTag acc
then it works ok. However the lines
(token, correctTag, _proposdTag) = trace "cursor " Z.cursor z proposed = trace "proposed tag" (f z)
don't work. They only appear once. I assume they should appear with every element in the zipper. Can anyone explain why?
You're compiling with optimisations, aren't you?
Not that I am aware of. I did not set up any compilation flags in the cabal file.
You need the trace strings to depend on the input, otherwise the optimiser sees no reason to repeat the work. (The constant part is lifted out of the lambda, a consequence of unsafePerformIO.)
Ok, I understand now. Should have appended some output to the trace output string (thats what the working trace does).
The line `trace "cursor " Z.cursor z' actually shouldn't print multiple traces even without optimisations, since it's
(trace "cursor " Z.cursor) z
Ooops, thats a bug 8-(
so the trace applies to the evaluation of the function Z.cursor, which should only be done once.
Ok, think I got it. Thank you, Hugo F.

On Monday 07 November 2011, 17:11:10, Hugo Ferreira wrote:
You're compiling with optimisations, aren't you?
Not that I am aware of. I did not set up any compilation flags in the cabal file.
cabal defaults to compiling with -O unless otherwise specified ($ cabal build -v2 emits the command lines used to build, if you want to check), so you most likely do.

On 11/07/2011 04:56 PM, Daniel Fischer wrote:
On Monday 07 November 2011, 17:11:10, Hugo Ferreira wrote:
You're compiling with optimisations, aren't you?
Not that I am aware of. I did not set up any compilation flags in the cabal file.
cabal defaults to compiling with -O unless otherwise specified ($ cabal build -v2 emits the command lines used to build, if you want to check), so you most likely do.
Correct. I confirmed this. Thanks, Hugo F.
participants (2)
-
Daniel Fischer
-
Hugo Ferreira