简体   繁体   中英

Applicative laws for alternative class formulations

A well-known alternative formulation of Applicative (see, eg, 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM