<div class="gmail_quote">2009/4/25 Thomas Hartman <span dir="ltr">&lt;<a href="mailto:tphyahoo@gmail.com">tphyahoo@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
In the program below, can someone explain the following debugger output to me?<br>
<br>
After :continue, shouldn&#39;t I hit the f breakpoint two more times?<br>
Why do I only hit the f breakpoint once?<br>
Is this a problem in the debugger?<br>
<br>
thartman@ubuntu:~/haskell-learning/debugger&gt;cat debugger.hs<br>

<br>
-- try this:<br>
-- ghci debugger.hs<br>
-- &gt; :break f<br>
-- &gt; :trace t<br>
-- &gt; :history -- should show you that f was called from h<br>
t = h . g . f $ &quot;hey!&quot;<br>
t2 = h . g . f $ &quot;heh!&quot;<br>
t3 = h . g . f $ &quot;wey!&quot;<br>
<br>
f = (&quot;f -- &quot; ++)<br>
g = (&quot;g -- &quot; ++)<br>
h = (&quot;h -- &quot; ++)<br>
<br>
ts = do<br>
putStrLn $ t<br>
putStrLn $ t2<br>
putStrLn $ t3</blockquote><div><br></div><div>What you are observing is really an artifact of the way breakpoints are attached to definitions in the debugger, and the way that GHCi evaluates code.</div><div><br></div><div>
f is clearly a function, but its definition style is a so-called &quot;pattern binding&quot;. The body contains no free (lambda bound) variables, so it is also a constant. GHCi arranges for f to be evaluated at most once. The breakpoint associated with the definition of f is fired if and when that evaluation takes place. Thus, in your case it fires exactly once.</div>
<div><br></div><div>You can re-write f to use a so-called &quot;function binding&quot; instead, by eta-expansion (introduce a new fresh variable, and apply the function to it on both sides):</div><div><br></div><div> f x = (&quot;f -- &quot; ++) x</div>
<div><br></div><div>This denotes the same function, but the breakpoint on f works differently. In this case, a breakpoint attached to f will fire whenever an application of f is reduced. If you write it this way you will see that the program stops three times instead of one.</div>
<div><br></div><div>You might ask: if both definitions denote the same function, why does the debugger behave differently? The short answer is that the debugger in GHCi is an operational debugger, so it exposes some of the operational details which may be invisible in a denotational semantics. In this case it revels that GHCi treats the two definitions of f differently.</div>
<div><br></div><div>Cheers,</div><div>Bernie.</div><div><br></div><div> </div></div>