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

add Show instances for Free and Cofree

Open sedram opened this issue 6 years ago • 15 comments

For instance, Haskell has Show1 f => Show1 (Free f) and (Show1 f, Show a) => Show (Free f a) and similarly for Cofree. The implementation depends on my other issue to introduce Show 1 .

sedram avatar Nov 16 '19 22:11 sedram

I'm interested in implementing show instances for cofree, maybe to start with only for some f. But currently I currently cant contribute since there are no instructions on how to develop in this project, and the tests are failing for me.

I would get 80% out of this feature if it was implemented only for a handfull of functors (List, Array, Maybe etc) since it allows people to play with it in the repl.

Neppord avatar Feb 12 '22 09:02 Neppord

What tests are failing?

JordanMartinez avatar Feb 13 '22 00:02 JordanMartinez

@JordanMartinez all of them, so probably i don't set up the project properly

Neppord avatar Feb 18 '22 14:02 Neppord

today i wrote a general show function for Cofree:

showCofree :: forall f a. Functor f => Show (f String) => Show a => Cofree f a -> String
showCofree cofree = execWriter do
    tell "("
    tell $ show $ Cofree.head cofree
    tell " :< "
    tell "("
    tell $ show $ map showCofree $ Cofree.tail cofree
    tell ")"
    tell ")"

Is it ok to make this the default show for Cofree?

Neppord avatar Feb 18 '22 14:02 Neppord

I don't think we should be using execWriter. Care to share why you implemented it that way?

JordanMartinez avatar Feb 19 '22 14:02 JordanMartinez

I was just the easiest way for me to create a string this complex, very similar to a string builder in kotlin or java.

I can replace it with <> instead

Neppord avatar Feb 19 '22 15:02 Neppord

Replacing it with <> would be better. However, I think this code isn't currently stack-safe.

JordanMartinez avatar Feb 19 '22 15:02 JordanMartinez

Yes, and it also don't give a correct output, it will have some extra " in the output.

Neppord avatar Feb 19 '22 15:02 Neppord

Replacing it with <> would be better. However, I think this code isn't currently stack-safe.

Stack safety would be nice, but it's worth noting it's not going to be possible to make this instance fully safe - cofree structures can be infinite so there's going to be an aspect of danger in calling this however it's implemented. I'm not sure, but that might be why it isn't provided in the first place. We should add a note on the instance warning about that probably.

garyb avatar Feb 19 '22 16:02 garyb

I'm not sure, but that might be why it isn't provided in the first place.

Ah, I hadn't thought about that. In that case, it might be better not to add such support. Show is already an anti-pattern in many ways.

JordanMartinez avatar Feb 21 '22 14:02 JordanMartinez

I was thinking a bit more about this the other day - we could write a function that prints it to an optional depth limit, and then implement the default show using some preset limit perhaps.

Or we could just not worry about it for now :smile: - although infinite cofrees are possible, I'm not sure if it's necessarily super common. It's also a possibility of most types featuring a Lazy really.

garyb avatar Feb 21 '22 14:02 garyb

For me it's utility for working with the reple, when trying things out, and when writing tests, then spec needs a show instance.

Neppord avatar Feb 21 '22 16:02 Neppord

we could write a function that prints it to an optional depth limit, and then implement the default show using some preset limit perhaps.

This sounds like a more reasonable tradeoff. The downside is you can't use Show, but it does enable a safer way to print something.

JordanMartinez avatar Feb 21 '22 17:02 JordanMartinez

This sounds like a more reasonable tradeoff. The downside is you can't use Show, but it does enable a safer way to print something.

That seems like it doesn't solve the original use case then (use in the REPL). We have a Show instance for lazy lists, I don't see how Cofree is any different.

natefaubion avatar Feb 21 '22 17:02 natefaubion

Ah, I didn't know this was being used in the REPL (missed that comment when I was writing a reply to Gary). Then, yeah, I think this should be implemented.

JordanMartinez avatar Feb 21 '22 22:02 JordanMartinez