Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sop-core: Concatenate a POP into an NP #176

Open
turion opened this issue Dec 4, 2023 · 7 comments
Open

sop-core: Concatenate a POP into an NP #176

turion opened this issue Dec 4, 2023 · 7 comments

Comments

@turion
Copy link

turion commented Dec 4, 2023

This may be out of scope, but I use sop-core as a lean, clean, principled general purpose library for heterogeneous sums and products. As such it is great, but sometimes I'm lacking a feature. For example, concatenation. For homogeneous lists, there is (++) :: [a] -> [a] -> [a] and concat :: [[a]] -> [a], but I believe this doesn't exist in sop-core. I think it should be possible to implement with these type signatures:

type family Concat (xss :: [[k]]) :: [k] where
  ...

hconcat :: POP f xss -> NP f (Concat xss)

Maybe it's already possible to do this with the existing type families, if so, I don't know how. In that case it might be helpful to add a type alias and a function to witness this.

Either way, if you think this works and is a good addition, I'll happily send a PR.

@phadej
Copy link
Contributor

phadej commented Dec 4, 2023

This is sibling issue to #115. See general comments there.

concat_NS :: NS (NS f) xss -> NS f (Concat xss) e.g. is a reasonable thing to have. And also the reverse functions, SListI2 xss => NP f (Concat xss) -> NP (NP f) xss, and a NS variant.

@turion
Copy link
Author

turion commented Dec 4, 2023

@phadej I'd also be willing to co-maintain a sop-core-extras package with you.

@phadej
Copy link
Contributor

phadej commented Dec 4, 2023

I don't think anymore that sop-core-extras is a good idea. If a separate package were to be introduced, it would be one defining Append, Concat etc. (list related) type families (and if UnsaturatedTypeFamilies is implemented in GHC ,then Map etc higher-order type families too), and then sop-core could depend on it.

But for time being, IMO, they can be defined in sop-core too.

EDIT: e.g. optics-core defines own Append: https://hackage.haskell.org/package/optics-core-0.4.1.1/docs/Optics-Internal-Optic-TypeLevel.html#t:Append, so there is some value of having shared package for type-families, so they are not redefined in every package.

@kosmikus
Copy link
Member

kosmikus commented Dec 4, 2023

The problem is that there are so many ways to define even "simple" type families that the hope of actually being able to reuse them is nearly zero. See e.g. the "extra" case in Append in the linked optics-core which depending on use case, you may or may not want.

I'm still not extremely fond of adding more things for general-purpose programming to sop-core, but I've also already said in the past that I'm not going to block adding a few more functions if that's what everyone seems to want.

@turion
Copy link
Author

turion commented Dec 5, 2023

I understand that you prefer to add only things to sop-core that can be used in generics-sop. But maybe Concat could be used for a generic "flatten a record of records" function? If yes, and this is desired, I'll try and add this as well, possibly with your guidance.

@turion
Copy link
Author

turion commented Dec 5, 2023

The problem is that there are so many ways to define even "simple" type families that the hope of actually being able to reuse them is nearly zero.

I don't have enough experience to comment on that, I would have implemented it in the "vanilla" way, not being aware of particular downsides.

@phadej
Copy link
Contributor

phadej commented Dec 12, 2023

I needed Append and Concat for my own needs, and some other stuff so I ended up making https://hackage.haskell.org/package/defun-sop

There isn't split, unconcat or NS versions, as I'm yet not sure how I want them to look as (I try my best to avoid SListI, but sometimes it is convenient). However there are some definitions in https://github.com/phadej/defun/blob/master/defun/test/defun-tests.hs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants