12

A well-known alternative formulation of Applicative (see, e.g., Typeclassopedia) is

class Functor f => Monoidal f where
  unit :: f ()
  pair :: f a -> f b -> f (a, b)

This leads to laws that look more like typical identity and associativity laws than what you get from Applicative, but only when you work through pair-reassociating isomorphisms. Thinking about this a few weeks ago, I came up with two other formulations that avoid this problem.

class Functor f => Fapplicative f where
  funit :: f (a -> a)
  fcomp :: f (b -> c) -> f (a -> b) -> f (a -> c)

class Functor f => Capplicative f where
  cunit :: Category (~>) => f (a ~> a)
  ccomp :: Category (~>) => f (b ~> c) -> f (a ~> b) -> f (a ~> c)

It's easy to implement Capplicative using Applicative, Fapplicative using Capplicative, and Applicative using Fapplicative, so these all have equivalent power.

The identity and associativity laws are entirely obvious. But Monoidal needs a naturality law, and these must as well. How might I formulate them? Also: Capplicative seems to suggest an immediate generalization:

class (Category (~>), Functor f) => Appish (~>) f where
  unit1 :: f (a ~> a)
  comp1 :: f (b ~> c) -> f (a ~> b) -> f (a ~> c)

I am a bit curious about whether this (or something similar) is good for something.

dfeuer
  • 48,079
  • 5
  • 63
  • 167
  • 1
    That category is called [Static](https://hackage.haskell.org/package/semigroupoids-5.2/docs/Data-Semigroupoid-Static.html). – Julia Path Jul 23 '17 at 23:36

1 Answers1

1

This is a really neat idea!

I think the free theorem for fcomp is

fcomp (fmap (post .) u) (fmap (. pre) v) = fmap (\f -> post . f . pre) (fcomp u v)
Sjoerd Visscher
  • 11,840
  • 2
  • 47
  • 59
  • If that proves sufficient (I'll give it a try when I get a chance), it suggests that the `Functor` constraint on `Appish` might be weakened while still getting something "sensible". Rather than `fmap`, this law could be expressed with a `dimap`-like `(c ~> d) -> (a ~> b) -> f (b ~> c) -> f (a ~> d)`. – dfeuer Jul 25 '17 at 00:20
  • This discussion seems related: https://stackoverflow.com/questions/24668313/arrows-are-exactly-equivalent-to-applicative-functors – Sjoerd Visscher Jul 25 '17 at 05:52