Hacker News new | ask | show | jobs
by thesz 1114 days ago
You can use eventlog traces, from Debug.Trace [1]. You can (traceEvent $ "look: " ++show bazinga) everywhere you need and then stare at the log to your heart content.

[1] https://hackage.haskell.org/package/base-4.18.0.0/docs/Debug...

No need for extensions, just compile and run your program slightly differently. That's the power of declarative languages.

2 comments

Not everything is tracing and debugging, sometimes you really need to output intermediate results for "normal", "production" purposes. One could still abuse Debug::Trace, but that would really be ugly.

I also object to that "everywhere". It is far easier to just dump an extra 'print' line somewhere inside a for-loop than into a `foldl (*) 1 $ map (+ 3) [17, 11, 19, 23]`. And that is an easy one...

With eventlog you have lightweight profiling and logging tool for "normal", "production" purposes. You can correlate different metrics of your program with your messages. This is not an abuse of Debug.Trace (notice the dot), it is normal state of affairs, regularly used and RTS is optimized for that use case.

I develop with Haskell professionally. That foldl example of yours is pretty rare and usually dealt with the QuickCheck [1], mother of all other quickchecks. Usually, the trace will be outside of the foldl application, but you can have it there in the foldl argument, of course.

[1] https://hackage.haskell.org/package/QuickCheck

Eventlog is just `unsafePerfemIO*`, so you could just use that instead and not "hide" it and feel better about that.

The "correct" answer to `foldl` would be `scanl` and printing the result of it.

https://hackage.haskell.org/package/base-4.18.0.0/docs/Prelu...

Eventlog traces are RTS calls wrapped into unsafePerformIO, you are right. The trace part of eventlog is optimized for, well, tracing and is very, very lightweight. It is also safe from races, whereas simple unsafePerformIO (putStrLn $ "did you meant that? " ++ show (a,b,c)) is not.

In my opinion, eventlog traces make much better logging than almost anything I've seen.

Right now, developing with C++, I miss the power of Haskell's RTS.

The point I was trying to make was, that if all you want/need is a `putStr`, just use `unsafePerformIO`.

Haskell's (GHC's) Eventlog is nice, but a binary format and not comparable to text output.

> I develop with Haskell professionally. That foldl example of yours is pretty rare and usually dealt with the QuickCheck [1], mother of all other quickchecks. Usually, the trace will be outside of the foldl application, but you can have it there in the foldl argument, of course.

So actually not everywhere. And QuickCheck does something else entirely.

You missed the word "usually". You really, really do not need a print within the body of a loop of any tightness. But you can have it.

The foldl example of yours should be split into a property checking and controlling for expected properties of the input. The first part is done via quickcheck and second part usually is done with assertions and/or traces.

But nothing preclude you from having your trace there, inside foldl argument. This is clearly wrong place to have it, but still you can have it there.

So I repeat, you can have your traceEvents everywhere.

I can't tell if you are trying to defend those languages or just piling up absurdities on the previous post in the style of "yes, and ..." improv.
I am trying to offer counterpoint to what seems to me as an unjust critique from a person who, at first sight, does not know much about Haskell.

Also, a link to a useful library is not a bad thing for anyone curious about Haskell. Thus, there's a bit of education there.

If it looks like improv, I am here every evening till Friday.