Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good name for the natural transformation `forall a. m a -> (Identity ⊕ m) a`?

Tags:

haskell

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?

like image 954
rampion Avatar asked Aug 27 '15 12:08

rampion


2 Answers

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.

like image 111
Ørjan Johansen Avatar answered Oct 19 '22 17:10

Ørjan Johansen


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.

like image 2
Iceland_jack Avatar answered Oct 19 '22 18:10

Iceland_jack