Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
If you have a Functor f with an instance Monoid a => Monoid (f a), f is Applicative!
{-# LANGUAGE TypeOperators, ScopedTypeVariables, TupleSections #-}
import Data.Constraint
import Data.Monoid
import Control.Applicative
pureDefault :: forall f a. Functor f
=> (Monoid () :- Monoid (f ()))
-> a -> f a
pureDefault (Sub Dict) a = a <$ (mempty :: f ())
apDefault :: forall f a b. Functor f
=> (Monoid (First (a -> b), First a) :- Monoid (f (First (a -> b), First a)))
-> f (a -> b) -> f a -> f b
apDefault (Sub Dict) fab fa = fmap (\(First (Just f), First (Just a)) -> f a) $
fmap (\f -> (First (Just f), mempty)) fab `mappend` fmap (\a -> (mempty, First (Just a))) fa
-- Example (Dual is just the identity applicative)
pfDual :: Monoid a :- Monoid (Dual a)
pfDual = Sub Dict
instance Functor Dual where
fmap f (Dual a) = Dual (f a)
instance Applicative Dual where
pure = pureDefault pfDual
(<*>) = apDefault pfDual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.