Flipped function application

Andreas Abel andreas.abel
Sat Oct 12 05:01:20 UTC 2013


When thinking about flipped function application, maybe it is useful
to look at the big picture, i.e., what is the corresponding notion of
composition, and what are the functorial, applicative, and monadic
versions.

Here is a comprehensive list of existent (H) and envisioned (W,M)
application and composition operators.

   H = have already
   W = want
   M = missing, but unclear if we want it

"Backward pipelining" is the "non-flipped" application style, and
"forward pipelining is the flipped application style.  I have colored
the forward pipelining operators in |>, &, and # for an instance, but 
feel
free to try your own coloring...

Backward pipelining
-------------------

1. Pure
H  ($) :: (a -> b) -> a -> b
H  (.) :: (b -> c) -> (a -> b) -> a -> c
    (g . f) a = g $ f a

2. Functorial
H  (<$>) :: (a -> b) -> m a -> m b
W  (<.>) :: (b -> c) -> (a -> m b) -> a -> m c  -- missing, but useful!
    (g <.> f) a = g <$> f a

3. Applicative
H  (<*>) :: m (a -> b) -> m a -> m b
M  (<.*>) :: m (b -> c) -> (a -> m b) -> a -> m c -- missing.  Never 
used this.
    (g <.*> f) a = g <*> f a

4. Monadic
H  (=<<) :: (a -> m b) -> m a -> m b
H  (<=<) :: (b -> m c) -> (a -> m b) -> a -> m c
    (g <=< f) a = g =<< f a

Forward pipelining  (colored |>)    [My preference]
------------------

1. Pure
W  (|>) :: a -> (a -> b) -> b
W  (.>) :: (a -> b) -> (b -> c) -> a -> c
    a |> (f .> g) = (a |> f) |> g

2. Functorial
W  (|>>)  :: m a -> (a -> b) -> m b  -- useful!
M  (.>>) :: (a -> m b) -> (b -> c) -> a -> m c
    a |> (f .>> g) = (a |> f) |>> g

3. Applicative
H  (<**>)  :: m a -> m (a -> b) -> m b
M  (<.**>) :: (a -> m b) -> m (b -> c) -> a -> m c
    a |> (f <.**> g) = (a |> f) <**> g

4. Monadic
H  (>>=) :: m a -> (a -> m b) -> m b
H  (>=>) :: (a -> m b) -> (b -> m c) -> a -> m c
    a |> (f >=> g) = (a |> f) >>= g

Forward pipelining  (colored &)
------------------

1. Pure
W  (&)  :: a -> (a -> b) -> b
W  (.&) :: (a -> b) -> (b -> c) -> a -> c
    a & (f .& g) = (a & f) & g

2. Functorial
W  (<&>)  :: m a -> (a -> b) -> m b  -- useful!
M  (<.&>) :: (a -> m b) -> (b -> c) -> a -> m c
    a & (f <.&> g) = (a & f) <&> g

3. Applicative
H  (<**>)  :: m a -> m (a -> b) -> m b
M  (<.**>) :: (a -> m b) -> m (b -> c) -> a -> m c
    a & (f <.**> g) = (a & f) <**> g

4. Monadic
H  (>>=) :: m a -> (a -> m b) -> m b
H  (>=>) :: (a -> m b) -> (b -> m c) -> a -> m c
    a & (f >=> g) = (a & f) >>= g

Forward pipelining  (colored #)
------------------

1. Pure
W  (#) :: a -> (a -> b) -> b
W  (.#) :: (a -> b) -> (b -> c) -> a -> c
    a # (f .# g) = (a # f) # g

2. Functorial
W  (<#>)  :: m a -> (a -> b) -> m b  -- useful!
M  (<.#>) :: (a -> m b) -> (b -> c) -> a -> m c
    a # (f <.#> g) = (a # f) <#> g

3. Applicative
H  (<**>)  :: m a -> m (a -> b) -> m b
M  (<.**>) :: (a -> m b) -> m (b -> c) -> a -> m c
    a # (f <.**> g) = (a # f) <**> g

4. Monadic
H  (>>=) :: m a -> (a -> m b) -> m b
H  (>=>) :: (a -> m b) -> (b -> m c) -> a -> m c
    a # (f >=> g) = (a # f) >>= g


On 2013-10-12 03:35, wren ng thornton wrote:
> On 10/11/13 5:13 AM, Heinrich Apfelmus wrote:
>> In my opinion, only (#) looks "right" for this purpose, though.
>
> FWIW, so long as we're bikeshedding names, I agree that (#) is mich
> better than the alternatives. It also has some history from ML-land.
> The big issue is how to get it to play nicely with -XMagicHash

-- 
Andreas Abel  <><     Du bist der geliebte Mensch.

Theoretical Computer Science, University of Munich 
http://www.tcs.informatik.uni-muenchen.de/~abel/



More information about the Libraries mailing list