Notebook
class Semigroup m where (<>) :: m -> m -> m
(x <> y) <> z ≡ x <> (y <> z)
sconcat :: NonEmpty a -> a stimes :: Integral b => b -> a -> a
instance Semigroup [a] where (<>) = (++)
newtype Sum a = Sum { getSum :: a } newtype Product a = Product { getProduct :: a } instance Num a => Semigroup (Sum a) where Sum x <> Sum y = Sum (x + y) instance Num a => Semigroup (Product a) where Product x <> Product y = Product (x * y)
newtype Max a = Max { getMax :: a } -- max newtype Min a = Min { getMin :: a } -- min newtype Any = Any { getAny :: Bool } -- || newtype All = All { getAll :: Bool } -- &&
class Semigroup m => Monoid m where mempty :: m
1. (x <> y) <> z ≡ x <> (y <> z) 2. x <> mempty ≡ x 3. mempty <> x ≡ x
class Monoid a where mempty :: a mappend :: a -> a -> a mconcat :: [a] -> a
1. mappend x (mappend y z) = mappend (mappend x y) 2. mappend mempty x = x 3. mappend x mempty = x
instance Monoid [a] where mempty = [] l1 `mappend` l2 = l1 ++ l2
instance Monoid a => Monoid (Maybe a) where mempty = Nothing Nothing `mappend` x = x x `mappend` Nothing = x Just x `mappend` Just y = Just (x `mappend` y)
instance (Monoid a, Monoid b) => Monoid (a,b) where mempty = ( mempty, mempty) (a1,b1) `mappend` (a2,b2) = (a1 `mappend` a2, b1 `mappend` b2)
instance Monoid Ordering where mempty = EQ LT `mappend` _ = LT EQ `mappend` y = y GT `mappend` _ = GT
newtype Sum a = Sum { getSum :: a } -- (+) 0 newtype Product a = Product { getProduct :: a } -- (*) 1 newtype Max a = Max { getMax :: a } -- max newtype Min a = Min { getMin :: a } -- min newtype Any = Any { getAny :: Bool } -- || False newtype All = All { getAll :: Bool } -- && True
instance Semigroup a => Semigroup (Maybe a) where Nothing <> b = b a <> Nothing = a Just a <> Just b = Just (a <> b) instance Semigroup a => Monoid (Maybe a) where mempty = Nothing
newtype First a = First { getFirst :: Maybe a } deriving (Eq, Ord, Read, Show) instance Monoid (First a) where mempty = First Nothing r@(First (Just _)) `mappend` _ = r First Nothing `mappend` r = r newtype Last a = Last { getLast :: Maybe a } deriving (Eq, Ord, Read, Show) instance Monoid (Last a) where mempty = Last Nothing _ `mappend` r@(Last (Just _)) = r r `mappend` Last Nothing = r
instance Monoid b => Monoid (a -> b) where mempty _ = mempty mappend f g x = f x `mappend` g x
class Foldable t where fold :: Monoid m => t m -> m foldMap :: Monoid m => (a -> m) -> t a -> m foldr :: (a -> b -> b) -> b -> t a -> b
instance Foldable [] where foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = x `f` foldr f z xs
instance Foldable Maybe where foldr :: (a -> b -> b) -> b -> Maybe a -> b foldr _ z Nothing = z foldr f z (Just x) = f x z
class Functor f where fmap :: (a -> b) -> f a -> f b
1. fmap id ≡ id 2. fmap (f . g) ≡ fmap f . fmap g
infixl 4 <$> (<$>) :: Functor f => (a -> b) -> f a -> f b (<$>) = fmap
instance Functor Maybe where fmap :: (a -> b) -> Maybe a -> Maybe b fmap f (Just x) = fmap f Nothing =
instance Functor [] where fmap :: (a -> b) -> [a] -> [b] fmap = map
instance Functor ((->) r) where fmap :: (a -> b) -> (r -> a) -> (r -> b) fmap = (.)
class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b
1. identidad pure id <*> v ≡ v 2. composición pure (.) <*> u <*> v <*> w ≡ u <*> (v <*> w) 3. homomorfismo pure f <*> pure x ≡ pure (f x) 4. intercambio u <*> pure y ≡ pure ($ y) <*> u
instance Applicative Maybe where pure :: a -> Maybe a pure = Just (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b Nothing <*> _ = Nothing Just f <*> mx = fmap f mx
instance Applicative [] where pure :: a -> [a] pure x = [x] (<*>) :: [a -> b] -> [a] -> [b] fs <*> xs = [f x | f <- fs, x <- xs]
instance Applicative ((->) r) where pure :: a -> r -> a pure x = \_ -> x (<*>) :: (r -> a -> b) -> (r -> a) -> r -> b f <*> g = \x -> f x (g x)
liftA2 f x y = f <$> x <*> y
class Applicative f => Alternative f where empty :: f a (<|>) :: f a -> f a -> f a
empty <|> x = x x <|> empty = x (x <|> y) <|> z = x <|> (y <|> z)
instance Alternative Maybe where empty :: Maybe a empty = Nothing (<|>) :: Maybe a -> Maybe a -> Maybe a Nothing <|> r = r l <|> _ = l
instance Alternative [] where empty :: [a] empty = [] (<|>) :: [a] -> [a] -> [a] (<|>) = (++)
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)
instance Traversable Maybe where traverse :: Applicative f => (a -> f b) -> Maybe a -> f (Maybe b) traverse _ Nothing = pure Nothing traverse f (Just x) = Just <$> f x
instance Traversable [] where traverse :: Applicative f => (a -> f b) -> [a] -> f [b] traverse f = foldr consF (pure []) where consF x ys = (:) <$> f x <*> ys