Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-cafe] trace

0 views
Skip to first unread message

michael rice

unread,
Dec 24, 2009, 3:31:58 PM12/24/09
to haskel...@haskell.org
Can someone provide a simple example of tracing a function.

Michael

Daniel Fischer

unread,
Dec 24, 2009, 3:55:49 PM12/24/09
to haskel...@haskell.org
Am Donnerstag 24 Dezember 2009 21:31:34 schrieb michael rice:
> Can someone provide a simple example of tracing a function.
>
> Michael

Is

import Debug.Trace

infixl 0 `debug`

debug = flip trace

dfib :: Int -> Integer
dfib =
let fib 0 = 0
fib 1 = 1
fib n = dfib (n-2) + dfib (n-1) `debug` "eval fib " ++ show n
in (map fib [0 .. ] !!)


*MFib> dfib 12
eval fib 12
eval fib 10
eval fib 8
eval fib 6
eval fib 4
eval fib 2
eval fib 3
eval fib 5
eval fib 7
eval fib 9
eval fib 11
144

the kind of example you're looking for?

michael rice

unread,
Dec 24, 2009, 4:22:12 PM12/24/09
to haskel...@haskell.org, Daniel Fischer
Hmm... here are the functions I was looking to trace, the second one being an example from Scheme text "Concrete Abstractions" that I rewrote after seeing the first. Compared to the CL/Scheme memoization code, the Haskell seems like, how shall I put this, a sleight of hand, so much so that I'm driven to look behind the scenes to try to understand what is occurring. I remember that someone said, pattern matching is strict and LET is lazy, so I know the trick depends on laziness, but knowing that and understanding it are still a world apart.

Does tracing a function *always* require memoizing it?

Michael

memo_fib :: Int -> Integer
memo_fib =


�� let fib 0 = 0
������ fib 1 = 1

������ fib n = memoized_fib (n-2) + memoized_fib (n-1)
�� in� (map fib [0..] !!)

memo_walk_count :: Int -> Integer
memo_walk_count =
�� let walk_count 0 = 1
������ walk_count 1 = 1
������ walk_count n = memo_walk_count (n-2) + memo_walk_count (n-1)
�� in (map walk_count [0..] !!)

--- On Thu, 12/24/09, Daniel Fischer <daniel.i...@web.de> wrote:

From: Daniel Fischer <daniel.i...@web.de>
Subject: Re: [Haskell-cafe] trace
To: haskel...@haskell.org
Cc: "michael rice" <now...@yahoo.com>
Date: Thursday, December 24, 2009, 3:52 PM

#yiv1755006913 p, #yiv1755006913 li {white-space:pre-wrap;}

Daniel Fischer

unread,
Dec 24, 2009, 5:06:30 PM12/24/09
to michael rice, haskel...@haskell.org
Am Donnerstag 24 Dezember 2009 22:21:50 schrieb michael rice:
> Hmm... here are the functions I was looking to trace, the second one being
> an example from Scheme text "Concrete Abstractions" that I rewrote after
> seeing the first. Compared to the CL/Scheme memoization code, the Haskell
> seems like, how shall I put this, a sleight of hand, so much so that I'm
> driven to look behind the scenes to try to understand what is occurring. I
> remember that someone said, pattern matching is strict and LET is lazy, so
> I know the trick depends on laziness, but knowing that and understanding it
> are still a world apart.
>
> Does tracing a function *always* require memoizing it?

No, of course not. That was just a use of tracing I knew where it was. You can put calls
to trace more or less everywhere, like

mySine x = sin x `debug` "Calling sine " ++ show x

(note: using debug = flip trace instead of trace needs fewer parentheses and makes
commenting out the tracing code easier).
Of course, tracing can be particularly enlightening for recursive functions.

0 new messages