Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Last active November 21, 2018 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Icelandjack/9e6902690a244286843164e554a8c3db to your computer and use it in GitHub Desktop.
Save Icelandjack/9e6902690a244286843164e554a8c3db to your computer and use it in GitHub Desktop.
Monoidal natural transformations / Applicative morphisms / Applicative functor morphism / Applicative transformation (used in Data.Foldable)

https://mail.haskell.org/pipermail/haskell-cafe/2010-November/086055.html

http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms

http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms

https://arxiv.org/pdf/1202.2919.pdf

eta (pure c) = pure c
eta (f <*> x) = eta f <*> eta x

Applicative (Sum f g): https://gist.github.com/Icelandjack/78542ddbdb622a48d8e60e14510cd7dd

Examples

In addition to these we have some uninteresting ones.

I don't think there are any transformations Maybe ~> (m, ) but there is (a unique?) one

a :: (m, ) ~> Maybe
a (_, m) = Just m
@Icelandjack
Copy link
Author

pure for [] and ZipList is

pure @[]      a = [a]
pure @ZipList a = ZipList (repeat a)

so a natural transformation must take [()] to ZipList (repeat ()).. infinite lists, seems to follow laws but not interesting

b :: [] ~> ZipList 
b xs = ZipList (concatMap repeat xs)

@Icelandjack
Copy link
Author

Icelandjack commented Jun 1, 2017

c :: NonEmpty ~> []
c = toList

d :: [] ~> Compose Maybe NonEmpty
d []     = Compose $ Nothing
d (x:xs) = Compose $ Just (x:|xs)

e :: Either e ~> []
e = toList

f :: Maybe ~> []
f = toList

@Icelandjack
Copy link
Author

g :: a -> Maybe ~> Either a
g val = maybe (Left val) Right

@Icelandjack
Copy link
Author

Icelandjack commented Jun 1, 2017

Either : (->) -> (~>)

h :: (a -> a') -> (Either a ~> Either a')
h = first

?: This is a monoidal natural transformation when a -> a' is a monoid homomorphism?

i :: (a -> a') -> ((,) a ~> (,) a')
i = first 

@Icelandjack
Copy link
Author

runAp f is a monoidal natural transformation for all f, this is exciting :)

@Icelandjack
Copy link
Author

Icelandjack commented Jun 1, 2017

According to https://arxiv.org/pdf/1202.2919.pdf

join' :: Monad m => Compose m m ~> m
join' (Compose mma) = join mma

cmp to

(Functor f, Applicative g) 
  => f ~> Compose f g
(Applicative f)
  => g ~> Compose f g

@Icelandjack
Copy link
Author

Icelandjack commented Jun 1, 2017

In categories monoidal wrt the Day convolution, the exponential

data Exp :: (* -> *) -> (* -> *) -> (* -> *) where
  Exp :: (forall xx. f xx -> g (a, xx)) -> Exp f g a

has a Caley representation

data Rep :: (* -> *) -> (* -> *) where
  Rep :: (forall xx. f xx -> f (a, xx)) -> Rep f a
  deriving Functor

instance Functor f => Applicative (Rep f) where
  pure = Rep . fmap . (,)

  Exp f <*> Exp a = Exp (fmap g . a . f) where
    g (x, (f, c)) = (f x, c)

then http://usuarios.fceia.unr.edu.ar/~mauro/pubs/Notions_of_Computation_as_Monoids_ext.pdf claims rep is monoidal

rep :: Applicative f => f ~> Rep f
rep x = Rep (\y -> pure (,) <*> x <*> y)

@Icelandjack
Copy link
Author

? Monad morphism implies applicative morphism

@Icelandjack
Copy link
Author

A Uni ed View of Monadic and Applicative Non-determinism

@Icelandjack
Copy link
Author

A Uni ed View of Monadic and Applicative Non-determinism

Ideal monad / idealized monad / unital monads

Control.Applicative.Lift + https://www.reddit.com/r/haskell/comments/6ac1x2/idealised_monads_or_monad_instance_for_lift_f/

data UM m a = Pure a | Impure (m a)

class Functor m => Unital m where
  join :: m (UM m a) -> UM m a

@Icelandjack
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment