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

Add elimNS? #143

Open
ocharles opened this issue Aug 18, 2021 · 2 comments
Open

Add elimNS? #143

ocharles opened this issue Aug 18, 2021 · 2 comments

Comments

@ocharles
Copy link

ocharles commented Aug 18, 2021

I dunno if this is too specific, but I found myself digging for this in the sop-core documentation, and eventually gave up and just wrote this myself:

elimNS :: All Top xs => NP (I -.-> K a) xs -> NS I xs -> a
elimNS handlers s = hcollapse $ hap handlers s

Along with

iToK :: (a -> b) -> (I -.-> K b) a
iToK f = fn $ K . f . unI

This lets me essentially pattern match on an NS:

            elimNS $ iToK throwIO
                  :* iToK pure
                  :* Nil

Maybe there is already a way to do this with sop-core?

@kosmikus
Copy link
Member

kosmikus commented Sep 9, 2021

Thanks for the suggestion.

iToK exists as mapIK.

elimNS does, I think, not exist, even though it's easy to define. Nevertheless, perhaps it should exist. One question is how much it should be generalised. Certainly, there's no need to use I. The type

All Top xs => NP (f -.-> K a) xs -> NS f xs -> a

just works.

However, even the type

(SListIN h xs, HCollapse h, HAp h) => Prod h (f -.-> K a) xs -> h f xs -> CollapseTo h a

works, which means it could then be used at all of these types

All Top xs => NP (f -.-> K a) xs -> NS f xs -> a
All Top xs => NP (f -.-> K a) xs -> NP f xs -> [a]
All2 Top xss => POP (f -.-> K a) xss -> SOP f xss -> [a]
All2 Top xss => POP (f -.-> K a) xss -> POP f xss -> [[a]]

Is elim then still a good name?

In principle, I'm happy to add more functions, but naming is always hard, and if people end up defining their own function in the end anyway, despite them existing, I'm unsure how much is won by adding it.

@phadej
Copy link
Contributor

phadej commented Sep 9, 2021

iToK exists as mapIK.

It's not, compare:

iToK :: (a -> b) -> (I -.-> K b) a
mapIK :: (a -> b) -> I a -> K b c

easy to define using mapIK though:

*Data.SOP> :t fn . mapIK
fn . mapIK :: (a -> b) -> (-.->) I (K b) a

EDIT: (iToK = coerce also works, but you'll need to give it a type-signature if it's not obvious from the context :)

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