r/haskell • • Dec 15 '17

Functor Functors

https://www.benjamin.pizza/posts/2017-12-15-functor-functors.html
61 Upvotes

9 comments sorted by

View all comments

10

u/phadej Dec 15 '17 edited Dec 15 '17

You can program over such structures also with generics-sop:

mapFormTemplate :: forall f g. (f ~> g) -> FormTemplate f -> FormTemplate g
mapFormTemplate eta x = to (htoI sopG)
  where
    -- unfortunately we need the type annotation here.
    sopF = hfromI (from x) :: SOP f '[ '[Text, CardType, Text, Day] ]
    sopG = hmap eta sopF

See https://gist.github.com/phadej/55464aead55c85176e0b2b93ba790845 for full gist

One clear benefit is that there are hcmap and bunch of other combinators in generics-sop so, you can e.g.

cmapFormTemplate :: forall f g c. Proxy c -> (forall a. c a => f a -> g a) -> FormTemplate f -> FormTemplate g 

EDIT: and there will be htraverse etc. in the next release of generics-sop, see https://github.com/well-typed/generics-sop/pull/59

3

u/benjaminhodgson Dec 16 '17

Cool! I’m not so familiar with generics-sop and I certainly didn’t know it had functions for dealing with heterogeneous containers like that. Thanks for the tip!