generics-sop icon indicating copy to clipboard operation
generics-sop copied to clipboard

Add `elimNS`?

Open ocharles opened this issue 2 years ago • 2 comments

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?

ocharles avatar Aug 18 '21 17:08 ocharles

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.

kosmikus avatar Sep 09 '21 14:09 kosmikus

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 :)

phadej avatar Sep 09 '21 15:09 phadej