add Show instances for Free and Cofree
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 .
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.
What tests are failing?
@JordanMartinez all of them, so probably i don't set up the project properly
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?
I don't think we should be using execWriter. Care to share why you implemented it that way?
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
Replacing it with <> would be better. However, I think this code isn't currently stack-safe.
Yes, and it also don't give a correct output, it will have some extra " in the output.
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.
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.
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.
For me it's utility for working with the reple, when trying things out, and when writing tests, then spec needs a show instance.
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.
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.
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.