Opened 3 years ago

Closed 2 years ago

#7626 closed feature request (fixed)

Add common utility variants of trace to Debug.Trace

Reported by: chrisseaton Owned by:
Priority: normal Milestone: 7.8.1
Component: libraries/base Version: 7.6.1
Keywords: trace Cc: malaquias@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

As discussed on the libraries list.

http://www.haskell.org/pipermail/libraries/2013-January/019319.html

'traceId' traces a value, and then returns that same value. This avoids the common case of 'trace a a'.

    traceId :: String -> String
    traceId a = trace a a

'traceM' runs 'trace' and returns unit in an arbitrary monad. This is useful for writing traces in do-notation, as each can sit on their own line.

    traceM :: (Monad m) => String -> m ()
    traceM string = trace string $ return ()

For example:

    ... = do
        x <- ...
        traceM $ "x: " ++ show x
        y <- ...
        ...

That application of 'traceM' there can be temporarily commented out with a simple line comment.

Finally, equivalents of 'traceShow' for those two functions.

    traceShowId :: (Show a) => a -> a
    traceShowId a = trace (show a) a
    traceShowM :: (Show a, Monad m) => a -> m ()
    traceShowM = traceM . show

Advantages:

I use these functions in my code all the time. When I discussed it on the libraries list there were several enthusiastic +1s of people doing the same.

Disadvantages:

The documentation in the patch makes it clear that traceM does not actually add a trace action to the monad you're using, which is a potential point of confusion (http://www.haskell.org/pipermail/libraries/2013-January/019322.html). I had originally suggested that 'traceIO == traceM', but it turns out this is not so.

Henning Thielemann suggested that 'traceM' was better achieved with directly using 'trace' and 'printf' (http://www.haskell.org/pipermail/libraries/2013-January/019320.html). Of course, any function can be replaced with its RHS, and whether or not the user uses 'show' or 'printf' or whatever is orthogonal.

I haven't written any tests, as it's all output generating code, so I'm really not sure how that's tested in GHC - sorry.

Status of patch:

Applies against base 3b51b741f45d4d85e79b2128d00479ce230f72f2 and compiles with GHC 388e1e825f79f2d16536fc583a48e5ce9c191b06. I've run a full GHC compile cycle with it without problem. Includes documentation.

Attachments (1)

tracem.diff (1.7 KB) - added by chrisseaton 3 years ago.
Patch

Download all attachments as: .zip

Change History (4)

Changed 3 years ago by chrisseaton

Patch

comment:1 Changed 2 years ago by romildo

  • Cc malaquias@… added

comment:2 Changed 2 years ago by igloo

  • difficulty set to Unknown
  • Milestone set to 7.8.1
  • Status changed from new to patch

comment:3 Changed 2 years ago by igloo

  • Resolution set to fixed
  • Status changed from patch to closed

Applied, thanks

Note: See TracTickets for help on using tickets.