<div dir="ltr">What do you call these functions? I&#39;ll put them all into a patch and open a feature request for them all in one go.<div><br></div><div>Chris</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On 21 January 2013 22:40, Roman Cheplyaka <span dir="ltr">&lt;<a href="mailto:roma@ro-che.info" target="_blank">roma@ro-che.info</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
While we&#39;re at it, the trace functions I miss are<br>
<br>
  \x -&gt; trace x x<br>
<br>
and<br>
<br>
  \x -&gt; trace (show x) x<br>
<br>
Roman<br>
<br>
* Andreas Abel &lt;<a href="mailto:andreas.abel@ifi.lmu.de">andreas.abel@ifi.lmu.de</a>&gt; [2013-01-21 23:31:24+0100]<br>
<div class="HOEnZb"><div class="h5">&gt; +1.  I also had to define traceM for the same purposes. --Andreas<br>
&gt;<br>
&gt; On 21.01.13 6:41 PM, Chris Seaton wrote:<br>
&gt; &gt;Yes, I suppose that traceIO does not have the semantics I assumed.<br>
&gt; &gt;Still, I think it is useful to have a trace that one can easily insert<br>
&gt; &gt;into an arbitrary monad. Here&#39;s how I use it:<br>
&gt; &gt;<br>
&gt; &gt;--------<br>
&gt; &gt;<br>
&gt; &gt;import Debug.Trace<br>
&gt; &gt;<br>
&gt; &gt;main :: IO ()<br>
&gt; &gt;main = putStrLn $ show foo<br>
&gt; &gt;<br>
&gt; &gt;foo :: Maybe Int<br>
&gt; &gt;foo = do<br>
&gt; &gt;     x &lt;- bar 14<br>
&gt; &gt;     traceM $ show x<br>
&gt; &gt;     y &lt;- bar 2<br>
&gt; &gt;     traceM $ show y<br>
&gt; &gt;     return $ x + y<br>
&gt; &gt;<br>
&gt; &gt;bar :: Int -&gt; Maybe Int<br>
&gt; &gt;bar x = Just $ 2*x<br>
&gt; &gt;<br>
&gt; &gt;traceM :: (Monad m) =&gt; String -&gt; m ()<br>
&gt; &gt;traceM message = trace message $ return ()<br>
&gt; &gt;<br>
&gt; &gt;----------<br>
&gt; &gt;<br>
&gt; &gt;I think it is cleaner and more obvious than without the abstraction.<br>
&gt; &gt;Plus it is very easy to comment out. It is really good for list<br>
&gt; &gt;comprehensions written in do notation, as I often want to peek at<br>
&gt; &gt;intermediate values of those. I know I always add it to my projects, so<br>
&gt; &gt;I thought it may be wanted in base.<br>
&gt; &gt;<br>
&gt; &gt;As Henning Thielemann said, you can use printf or whatever with it, but<br>
&gt; &gt;I think that is an orthogonal issue.<br>
&gt; &gt;<br>
&gt; &gt;Regards,<br>
&gt; &gt;<br>
&gt; &gt;Chris<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;On 21 January 2013 17:09, Herbert Valerio Riedel &lt;<a href="mailto:hvr@gnu.org">hvr@gnu.org</a><br>
&gt; &gt;&lt;mailto:<a href="mailto:hvr@gnu.org">hvr@gnu.org</a>&gt;&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt;    Chris Seaton &lt;<a href="mailto:chris@chrisseaton.com">chris@chrisseaton.com</a> &lt;mailto:<a href="mailto:chris@chrisseaton.com">chris@chrisseaton.com</a>&gt;&gt;<br>
&gt; &gt;    writes:<br>
&gt; &gt;<br>
&gt; &gt;     &gt; I use printf-style debugging a lot, so I am always adding and<br>
&gt; &gt;    removing<br>
&gt; &gt;     &gt; applications of trace. There is the Debug.Trace.traceIO function<br>
&gt; &gt;    that makes<br>
&gt; &gt;     &gt; this easy to do in the IO monad (it just applies hPutStrLn<br>
&gt; &gt;    stderr), but is<br>
&gt; &gt;     &gt; that specialisation to IO unnecessary?<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; I find myself always using this utility function:<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; traceM :: (Monad m) =&gt; String -&gt; m ()<br>
&gt; &gt;     &gt; traceM message = trace message $ return ()<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; Which can be used to implement traceIO.<br>
&gt; &gt;     &gt;<br>
&gt; &gt;     &gt; traceIO :: String -&gt; IO ()<br>
&gt; &gt;     &gt; traceIO = traceM<br>
&gt; &gt;<br>
&gt; &gt;    btw, that wouldn&#39;t have the same semantics as the existing<br>
&gt; &gt;    `Debug.Trace.traceIO` which is more or less something similiar to a<br>
&gt; &gt;    `hPutStrLn stderr` whose side-effect gets triggered at monad-execution<br>
&gt; &gt;    time, whereas the side-effect of `traceM` occurs at monad-construction<br>
&gt; &gt;    time; consider the following program:<br>
&gt; &gt;<br>
&gt; &gt;    --8&lt;---------------cut here---------------start-------------&gt;8---<br>
&gt; &gt;    import Control.Monad<br>
&gt; &gt;    import Debug.Trace<br>
&gt; &gt;<br>
&gt; &gt;    traceM :: (Monad m) =&gt; String -&gt; m ()<br>
&gt; &gt;    traceM message = trace message $ return ()<br>
&gt; &gt;<br>
&gt; &gt;    traceIO&#39; :: String -&gt; IO ()<br>
&gt; &gt;    traceIO&#39; = traceM<br>
&gt; &gt;<br>
&gt; &gt;    main = replicateM_ 5 $ do<br>
&gt; &gt;              trace1<br>
&gt; &gt;              trace2<br>
&gt; &gt;       where<br>
&gt; &gt;         trace1 = traceIO&#39; &quot;trace1&quot;<br>
&gt; &gt;         trace2 = traceIO  &quot;trace2&quot;<br>
&gt; &gt;    --8&lt;---------------cut here---------------end---------------&gt;8---<br>
&gt; &gt;<br>
&gt; &gt;    when run via runghc (or compiled with -O0) for GHC 7.6, this emits<br>
&gt; &gt;<br>
&gt; &gt;    --8&lt;---------------cut here---------------start-------------&gt;8---<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace2<br>
&gt; &gt;    --8&lt;---------------cut here---------------end---------------&gt;8---<br>
&gt; &gt;<br>
&gt; &gt;    only when using -O1 or -O2 the output results in<br>
&gt; &gt;<br>
&gt; &gt;    --8&lt;---------------cut here---------------start-------------&gt;8---<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    trace1<br>
&gt; &gt;    trace2<br>
&gt; &gt;    --8&lt;---------------cut here---------------end---------------&gt;8---<br>
&gt; &gt;<br>
&gt; &gt;    (I&#39;m guessing this due to `trace1` being inlined for -O1/-O2 -- but I<br>
&gt; &gt;    haven&#39;t checked)<br>
&gt; &gt;<br>
&gt; &gt;    cheers,<br>
&gt; &gt;       hvr<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;_______________________________________________<br>
&gt; &gt;Libraries mailing list<br>
&gt; &gt;<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
&gt; &gt;<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
&gt; &gt;<br>
&gt;<br>
&gt; --<br>
&gt; Andreas Abel  &lt;&gt;&lt;      Du bist der geliebte Mensch.<br>
&gt;<br>
&gt; Theoretical Computer Science, University of Munich<br>
&gt; Oettingenstr. 67, D-80538 Munich, GERMANY<br>
&gt;<br>
&gt; <a href="mailto:andreas.abel@ifi.lmu.de">andreas.abel@ifi.lmu.de</a><br>
&gt; <a href="http://www2.tcs.ifi.lmu.de/~abel/" target="_blank">http://www2.tcs.ifi.lmu.de/~abel/</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Libraries mailing list<br>
&gt; <a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</div></div></blockquote></div><br></div>