Proposal: Debug.Trace.traceWith

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: Debug.Trace.traceWith

Kris Nuttycombe
When using Debug.Trace for debugging, a very common operation is to prefer to trace only some subset or function of an intermediate result. As a consequence, I propose adding the following function to Debug.Trace:

traceWith :: (Show b) => (a -> b) -> a -> a
traceWith f a = trace (show $ f a) a

While it's trivial to define, I have found this to be one of the most useful functions in my trace-based debugging toolkit, hence my proposal that it be added to base. It generalizes `traceShowId` in a meaningful and useful fashion. 

As this is the first such proposal I've put forth, please let me know if I'm doing anything wrong with it!

Thanks,

Kris Nuttycombe

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

Theodore Lief Gannon
Hearty +1 one to this. Note:

traceShowId = traceWith id
traceShow   = traceWith . const


On Fri, Feb 9, 2018 at 5:08 PM, Kris Nuttycombe <[hidden email]> wrote:
When using Debug.Trace for debugging, a very common operation is to prefer to trace only some subset or function of an intermediate result. As a consequence, I propose adding the following function to Debug.Trace:

traceWith :: (Show b) => (a -> b) -> a -> a
traceWith f a = trace (show $ f a) a

While it's trivial to define, I have found this to be one of the most useful functions in my trace-based debugging toolkit, hence my proposal that it be added to base. It generalizes `traceShowId` in a meaningful and useful fashion. 

As this is the first such proposal I've put forth, please let me know if I'm doing anything wrong with it!

Thanks,

Kris Nuttycombe

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries



_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

David Feuer
In reply to this post by Kris Nuttycombe
Other another bike-shed color:

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f = traceWith (show . f)

But on the other hand, pointfree.io tells me that you can write

trace =<< f
trace =<< show . f

so I'm not sure how urgent these are.

On Feb 9, 2018 8:08 PM, "Kris Nuttycombe" <[hidden email]> wrote:
When using Debug.Trace for debugging, a very common operation is to prefer to trace only some subset or function of an intermediate result. As a consequence, I propose adding the following function to Debug.Trace:

traceWith :: (Show b) => (a -> b) -> a -> a
traceWith f a = trace (show $ f a) a

While it's trivial to define, I have found this to be one of the most useful functions in my trace-based debugging toolkit, hence my proposal that it be added to base. It generalizes `traceShowId` in a meaningful and useful fashion. 

As this is the first such proposal I've put forth, please let me know if I'm doing anything wrong with it!

Thanks,

Kris Nuttycombe

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries



_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

Michael Sloan
In reply to this post by Kris Nuttycombe
I often define

tracer prefix x = trace (prefix ++ ": " ++ show x) x

A bit arbitrary with the choice of ": ", but quite convenient. The with variant would be

tracerWith prefix f x = trace (prefix ++ ": " ++ show (f x)) x

On Feb 9, 2018 5:09 PM, "Kris Nuttycombe" <[hidden email]> wrote:
When using Debug.Trace for debugging, a very common operation is to prefer to trace only some subset or function of an intermediate result. As a consequence, I propose adding the following function to Debug.Trace:

traceWith :: (Show b) => (a -> b) -> a -> a
traceWith f a = trace (show $ f a) a

While it's trivial to define, I have found this to be one of the most useful functions in my trace-based debugging toolkit, hence my proposal that it be added to base. It generalizes `traceShowId` in a meaningful and useful fashion. 

As this is the first such proposal I've put forth, please let me know if I'm doing anything wrong with it!

Thanks,

Kris Nuttycombe

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries



_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

Kris Nuttycombe
On Sat, Feb 10, 2018 at 6:48 AM, Michael Sloan <[hidden email]> wrote:
I often define

tracer prefix x = trace (prefix ++ ": " ++ show x) x

I also have `traceWithPrefix :: (Show b) -> String -> (a -> b) -> a -> a` defined, for the same reason, so I absolutely support adding that as well.
 

A bit arbitrary with the choice of ": ", but quite convenient. The with variant would be

tracerWith prefix f x = trace (prefix ++ ": " ++ show (f x)) x

The definition I use is simply 

traceWithPrefix p f a = trace (p ++ show (f a)) a


On Fri, Feb 9, 2018 at 7:07 PM, David Feuer <[hidden email]> wrote:
Other another bike-shed color:

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f = traceWith (show . f)

Is there some situation where the `Show String` instance wouldn't be in scope? If `b` is `String` it works out just fine, so the first definition you gave here seems superfluous.
 
But on the other hand, pointfree.io tells me that you can write

trace =<< f
trace =<< show . f

 
That's... remarkably clever, so clever that it never occurred to me in 4 years of writing Haskell. :) I see your point, but jumping to the `Monad Reader` instance is a bit surprising. At very least, maybe this approach ought to be documented in the module. Thanks for pointing it out!

So, a question for all: if I were to want to create a pull request to add these functions (traceWith and traceWithPrefix) where would I go to do so?

Thanks,

Kris


On Feb 9, 2018 5:09 PM, "Kris Nuttycombe" <[hidden email]> wrote:
When using Debug.Trace for debugging, a very common operation is to prefer to trace only some subset or function of an intermediate result. As a consequence, I propose adding the following function to Debug.Trace:

traceWith :: (Show b) => (a -> b) -> a -> a
traceWith f a = trace (show $ f a) a

While it's trivial to define, I have found this to be one of the most useful functions in my trace-based debugging toolkit, hence my proposal that it be added to base. It generalizes `traceShowId` in a meaningful and useful fashion. 

As this is the first such proposal I've put forth, please let me know if I'm doing anything wrong with it!

Thanks,

Kris Nuttycombe

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries




_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

David Feuer
On Feb 10, 2018 11:53 AM, "Kris Nuttycombe" <[hidden email]> wrote:

On Fri, Feb 9, 2018 at 7:07 PM, David Feuer <[hidden email]> wrote:
Other another bike-shed color:

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f = traceWith (show . f)

Is there some situation where the `Show String` instance wouldn't be in scope? If `b` is `String` it works out just fine, so the first definition you gave here seems superfluous.

If the strings have non-ASCII characters, show will mangle them.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

David Feuer
Also, if you're pretty-printing something to use with trace, show will knock off the alignment and such.

On Feb 10, 2018 12:09 PM, "David Feuer" <[hidden email]> wrote:
On Feb 10, 2018 11:53 AM, "Kris Nuttycombe" <[hidden email]> wrote:

On Fri, Feb 9, 2018 at 7:07 PM, David Feuer <[hidden email]> wrote:
Other another bike-shed color:

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f = traceWith (show . f)

Is there some situation where the `Show String` instance wouldn't be in scope? If `b` is `String` it works out just fine, so the first definition you gave here seems superfluous.

If the strings have non-ASCII characters, show will mangle them.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Debug.Trace.traceWith

Kris Nuttycombe
In reply to this post by David Feuer
Ah, that makes sense, I forgot about the escaping issues.

On Sat, Feb 10, 2018 at 9:09 AM, David Feuer <[hidden email]> wrote:
On Feb 10, 2018 11:53 AM, "Kris Nuttycombe" <[hidden email]> wrote:

On Fri, Feb 9, 2018 at 7:07 PM, David Feuer <[hidden email]> wrote:
Other another bike-shed color:

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f = traceWith (show . f)

Is there some situation where the `Show String` instance wouldn't be in scope? If `b` is `String` it works out just fine, so the first definition you gave here seems superfluous.

If the strings have non-ASCII characters, show will mangle them.


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: [core libraries] Re: Proposal: Debug.Trace.traceWith

Joachim Breitner-2
In reply to this post by David Feuer
Hi,

Am Freitag, den 09.02.2018, 22:07 -0500 schrieb David Feuer:
> Other another bike-shed color:
>
> traceWith :: (a -> String) -> a -> a
>
> traceShowWith :: Show b => (a -> b) -> a -> a
> traceShowWith f = traceWith (show . f)

so many bike-shed colors! Maybe an indication that not all of them are
needed, but we should rather be better at pointing to useful, concice
and non-obvoius idioms?

> But on the other hand, pointfree.io tells me that you can write
>
> trace =<< f
> trace =<< show . f
>
> so I'm not sure how urgent these are.

Nifty! If we add this prominently to the docs in Data.Trace, wouldn’t
that suit us better?

Cheers,
Joachim

--
Joachim Breitner
  [hidden email]
  http://www.joachim-breitner.de/

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

signature.asc (849 bytes) Download Attachment