I found myself writing a bunch of functions with the signature
a -> Either longComplicatedType (m longComplicatedType)
So I decided I needed an alias
type SomeAlias m a = Either a (m a)
making it just a natural transformation on the functor m, isomorphic to forall a. m a -> (Identity ⊕ m) a.
At first I was tempted to name it MaybeN or MaybeF, as it either used the functor m or nothing. But Maybe a is isomorphic to 1 ⊕ a, and Identity isn't the terminal object in the category of endofunctors, Proxy is, so MaybeN f a should be Either (Proxy a) (f a).
Is there an existing name for forall a. m a -> (Identity ⊕ m) a I can steal from somewhere else? Failing that, is there a more elegant name than IdentityOr?
This appears to be isomorphic to InR from Data.Functor.Sum with f = Identity and g = m:
data Sum f g a = InL (f a) | InR (g a)
There was quite a bit of bikeshedding when the libraries committee chose those names, however; you may find some other alternatives there.
What you ask for exists under the name Lift
data Lift g a = Pure a | Other (g a)
This could be defined as a type synonym in terms of Sum and Identity
data Sum :: (k -> Type) -> (k -> Type) -> (k -> Type) where
InL :: f a -> (Sum f g) a
InR :: g a -> (Sum f g) a
newtype Identity :: Type -> Type where
Identity :: a -> Identity a
type Lift g a = (Sum Identity g) a
but this will not give you an Applicative or Alternative instance.
Sum f g is only an applicative under very specific circumstances (given a monoidal natural transformation forall xx. (Applicative g, Applicative f) => g xx -> f xx) (more info: Abstracting with Applicatives and Constructing Applicative Functors). This exists for
mnt :: forall xx. Applicative f => Identity xx -> f xx
mnt (Identity x) = pure x
and Lift g is that special case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With