Created
July 23, 2014 21:41
-
-
Save cheecheeo/7e54e193419158e4b7fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ATComposeProductSum where | |
import Control.Applicative | |
import Data.Traversable | |
import Data.Functor.Compose | |
import Data.Functor.Product | |
import Data.Functor.Sum | |
import Data.Functor.Identity | |
instance (Applicative f, Applicative g) => Applicative (Sum f g) where | |
pure x = InR (pure x) | |
InL f <*> InL x = InL (f <*> x) | |
InR f <*> InR x = InR (f <*> x) | |
-- | Applicative Composes | |
-- >>> pure (+) <*> (Compose (replicate 10 (Just 41))) <*> pure 1 | |
-- Compose [Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42,Just 42] | |
-- | |
-- Traversable Composes | |
-- >>> traverse (Identity . length) (Compose (replicate 10 (Just "foo"))) | |
-- Identity (Compose [Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3,Just 3]) | |
-- | |
-- Applicative Sums | |
-- >>> pure (+) <*> Pair (Just 41) (replicate 10 41) <*> pure 1 | |
-- Pair (Just 42) [42,42,42,42,42,42,42,42,42,42] | |
-- | |
-- Traversable Sums | |
-- >>> traverse (Identity . length) (Pair (Just "foo") (replicate 10 "foo")) | |
-- Identity (Pair (Just 3) [3,3,3,3,3,3,3,3,3,3]) | |
-- | |
-- Applicative Products | |
-- >>> (InL (Just (+1))) <*> InL (Just 41) :: Sum Maybe [] Integer | |
-- InL (Just 42) | |
-- >>> pure (+1) <*> InR (replicate 10 41) :: Sum Maybe [] Integer | |
-- InR [42,42,42,42,42,42,42,42,42,42] | |
-- | |
-- Traversable Products | |
-- >>> traverse (Identity . (+1)) (InL (Just 41)) :: Identity (Sum Maybe [] Integer) | |
-- Identity (InL (Just 42)) | |
-- >>> traverse (Identity . (+1)) (InR (replicate 10 41)) :: Identity (Sum Maybe [] Integer) | |
-- Identity (InR [42,42,42,42,42,42,42,42,42,42]) | |
true = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment