Is there anything in Haskell resembling the following type class?
class Mergeable (f :: Type -> Type -> Type) where
merge :: f a b -> f c d -> f (a, c) (Either b d)
In particular, imagine there is a Site
type indexed on some value and a route:
data Site a r
And we want to "merge" two sites, such that both their data (value) is kept in memory while supporting either of their routes.
instance Mergeable Site where
merge site1 site2 = ...
There is another type, called RouteEncoder a r
with the same type shape. So I figured I ought to look for a common pattern here ...
EDIT: Full type definitions of Site and RouteEncoder as requested:
data Site a r = Site
{ siteName :: Text,
siteRender ::
Some CLI.Action ->
RouteEncoder a r ->
a ->
r ->
Asset LByteString,
-- | Thread that will patch the model over time.
siteModelData :: ModelRunner a,
siteRouteEncoder :: RouteEncoder a r
}
type RouteEncoder a r = PartialIsoEnumerableWithCtx a FilePath r
-- | An Iso that is not necessarily surjective; as well as takes an (unchanging)
-- context value.
--
-- Parse `s` into (optional) `a` which can always be converted to a `s`. The `a`
-- can be enumerated finitely. `ctx` is used to all functions.
-- TODO: Is this isomrophic to `Iso (ctx, a) (Maybe a) s (ctx, s)` (plus, `ctx -> [a]`)?
data PartialIsoEnumerableWithCtx ctx s a
= PartialIsoEnumerableWithCtx (ctx -> a -> s, ctx -> s -> Maybe a, ctx -> [a])
The code with full context can be seen in this PR: https://github.com/srid/ema/pull/81/files (see also PartialIsoFunctor
which class probably should be simplified as well).
Site
andRouteEncoder
?Site
transforms if you transforma
andr
. It looks like it might be a profunctor--contravariant inr
and covariant ina
?