prettyprinter icon indicating copy to clipboard operation
prettyprinter copied to clipboard

Use `Foldable` instead of list?

Open andreasabel opened this issue 2 years ago • 1 comments

I see that some functions, like concatWith work for any Foldable instead of just for list.

Is there a reason why the other list functions are not generalized to Foldable? E.g.

hsep :: [Doc ann] -> Doc ann

I am trying to be progressive, using Data.List.NonEmpty and other list variants in my code base, but then I always have to sprinkle an extra toList in.

Unless there are reasons (like breaking type inference), I think all such list functions should be generalized to Foldable. (Even if type inference is broken for legacy users, such a generalization could maybe be pushed in a major-major version bump like 2.0...)

Update: I found a prior discussion on this, but maybe time is ripe for a revision:

  • https://github.com/quchen/prettyprinter/issues/12

andreasabel avatar Jan 19 '23 07:01 andreasabel

Are there any experience reports from APIs that were generalized from List to Foldable?

What I don't like about Foldable are the instances for tuples, Either, Maybe and so on. I frankly doubt that users would want to express something like hsep x where x is one of those "weird" instances. I expect that users would inadvertently use one these instances anyway and be confused. :/

There's another class that has instances for List, NonEmpty, Vector, etc is IsList. IsList wasn't made for this purpose, so this is probably a bad idea. But what exactly would be the problem?

sjakobi avatar Nov 30 '25 09:11 sjakobi