Leis em Haskell =========== Applicative =========== type Applicative :: (* -> *) -> Constraint class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b liftA2 :: (a -> b -> c) -> f a -> f b -> f c (*>) :: f a -> f b -> f b (<*) :: f a -> f b -> f a {-# MINIMAL pure, ((<*>) | liftA2) #-} Identity ~~~~~~~~ pure id <*> v = v Homomorphism ~~~~~~~~~~~~ pure f <*> pure x = pure (f x) Interchange ~~~~~~~~~~~ u <*> pure y = pure ($ y) <*> u Composition ~~~~~~~~~~~ pure (.) <*> u <*> v <*> w = u <*> (v <*> w) fmap ~~~~ (consequĂȘncia das demais) f <$> x = pure f <*> x =========== Alternative =========== import Control.Applicative type Alternative :: (* -> *) -> Constraint class Applicative f => Alternative f where empty :: f a (<|>) :: f a -> f a -> f a some :: f a -> f [a] many :: f a -> f [a] {-# MINIMAL empty, (<|>) #-} "monoide!" ~~~~~~~~~~ empty <|> u = u u <|> empty = u u <|> (v <|> w) = (u <|> v) <|> w some/many ~~~~~~~~~ some v = (:) <$> v <*> many v many v = some v <|> pure [] =========== Traversable =========== type Traversable :: (* -> *) -> Constraint class (Functor t, Foldable t) => Traversable t where traverse :: Applicative f => (a -> f b) -> t a -> f (t b) sequenceA :: Applicative f => t (f a) -> f (t a) mapM :: Monad m => (a -> m b) -> t a -> m (t b) sequence :: Monad m => t (m a) -> m (t a) {-# MINIMAL traverse | sequenceA #-} Identidade ~~~~~~~~~~ import Data.Functor.Identity traverse Identity = Identity Composta ~~~~~~~~ import Data.Functor.Compose traverse (Compose . fmap g . f) = Compose . fmap (traverse g) . traverse f Naturalidade ~~~~~~~~~~~~ para qualquer t :: (Applicative f, Applicative g) => f a -> g a que seja "homomorfismo de Applicative": t . traverse f = traverse (t . f) "homomorfismo de Applicative"?? t :: (Applicative f, Applicative g) => f a -> g a t (pure x) = pure x t (f <*> x) = t f <*> t x