purescript-debugged icon indicating copy to clipboard operation
purescript-debugged copied to clipboard

Instance for Data.Lazy

Open Woody88 opened this issue 5 years ago • 7 comments

Hi!

It would be nice if there was an instance for Data.Lazy. I was thinking maybe just use D.opaque_ "function" as the Repr? What do you think?

P.S: Willing to make a PR, and thanks for this lib it's really cool. I am playing/learning lazy streams and this library helps a lot with visualization.

Woody88 avatar May 21 '20 14:05 Woody88

Glad to hear it! Yeah, I think adding that instance would be good. I feel like having it force the contained value and display that, deferring to its Debug instance, could be a bit more useful?

hdgarrood avatar May 21 '20 14:05 hdgarrood

In general I think forcing things during debugging is reasonable. If you really need to debug something that contains a Lazy without forcing it, you could always extract the relevant information from it first, and just display that.

hdgarrood avatar May 21 '20 14:05 hdgarrood

Oh, yes. I thought that debug would be acting as our toString type of function. I was comparing it a bit too much with the Rust version. I guess that if one wants to see it normally in the repl they can just use the show function.

Because I want to be able to see how the values get evaluated one by one I prefer to see the <function>. My current solution should work for other people though, what I did is just wrap the Data.Lazy in a newtype and defined my own Debug instance.

Anyhow, I can add the instance for Lazy forcing the evaluation, if you will accept PR?

Woody88 avatar May 21 '20 15:05 Woody88

Sounds good! Come to think of it, another option would be to actually show whether the thing is forced or not, and only display it if it has been forced. That's very dodgy under normal circumstances, but for debugging it's probably acceptable.

hdgarrood avatar May 21 '20 15:05 hdgarrood

I don't really mind which, actually, we can always change it later.

hdgarrood avatar May 21 '20 15:05 hdgarrood

Great!

How can I tell if a lazy value has been evaluated or not? For example,

https://github.com/purescript/purescript-lists/blob/62900a56f6864638c952575dfd211a1cc55be7da/src/Data/List/Lazy/Types.purs#L30-L35.

The Cons needs a List which accepts a Lazy (Step a). Would we need another data constructor in data List a = ... say Evaluated a? Or should we extend the Lazy data type?

Woody88 avatar May 22 '20 11:05 Woody88

Looking at the implementation: https://github.com/purescript/purescript-lazy/blob/master/src/Data/Lazy.js it seems like a Lazy value is just a function which, when evaluated, forces the value if necessary and then returns it. So it looks like there's no way of asking a Lazy value whether or not it has been evaluated. I'm not sure if this would be considered a good addition to the API; in general it should probably not be possible to ask a Lazy value whether it has been forced yet.

hdgarrood avatar May 26 '20 21:05 hdgarrood