Is there a Haskell library providing (or assisting in writing) generic isomorphism between a sum type and an associated heterogenous sum type?
Take the following sum type,
data TR = TR_Index | TR_Inner R
Now we associate it with a heterogenous sum NS I xs
(where the 'xs' is defined by a type-class; see MotleyRouteSubRoutes
below). And then define a isomorphism to convert back and forth:
instance MotleyRoute TR where
-- `SingletonRoute s` is isomorphic to `()`
-- `PrefixedRoute s r` is isomorphic to `Const r s`
type MotleyRouteSubRoutes TR =
'[ SingletonRoute "index.html"
, PrefixedRoute "inner" R
]
motleyRouteIso =
iso
( \case
TR_Index -> Z $ I SingletonRoute
TR_Inner r -> S $ Z $ I $ PrefixedRoute r
)
( \case
Z (I SingletonRoute) -> TR_Index
S (Z (I (PrefixedRoute r))) -> TR_Inner r
S (S x) -> case x of {}
)
My goal here is to write motleyRouteIso
generically (as well as define MotleyRouteSubRoutes
generically, but that maybe outside the scope here). I was going to do it from scratch using generics-sop
, but I wonder if there already is a library that does this for me. generic-optics
provides a IsList
but that works for products not sums.
(For full context, see this PR)