## Type class

In computer science, a type class is a type system construct that supports parametric polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and a type variable a, and means that a can only be instantiated to a type whose members support the overloaded operations associated with T.

fmap执行的操作就叫做lift。同时，fmap也写作<$> class Functor (f :: * -> *) where fmap :: (a -> b) -> f a -> f b -- 类型类的实例 instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing  另外，Functor遵循两个定律： fmap id = id -- 1st functor law: id是单元运算符 fmap (g . f) = fmap g . fmap f -- 2nd functor law: 组合函数交换律  ## Applicative 如图所示，Applicative 更进一步，它的第一个参数也变成了一个 Functor，不过盒子里面放的东西是函数 a -> b。如果在上一小节里的第一个参数是个空FunctorApplicative 也知道如何处理。 class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b instance Applicative Maybe where pure = Just (Just f) <*> (Just x) = Just (f x) _ <*> _ = Nothing  方法<*>也称作liftA 同样，Applicative 也有它应该满足的定律： pure id <*> v = v -- Identity pure f <*> pure x = pure (f x) -- Homomorphism u <*> pure y = pure ($ y) <*> u              -- Interchange
pure (.) <*> u <*> v <*> w = u <*> (v <*> w) -- Composition


Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function >>= (pronounced “bind”) to do this.

class Monad m where
return :: a -> m a
(>>=)  :: m a -> (a -> m b) -> m b

return :: a -> Maybe a
return x  = Just x

(>>=)  :: Maybe a -> (a -> Maybe b) -> Maybe b
m >>= g = case m of
Nothing -> Nothing
Just x  -> g x


m >>= return     =  m                        -- right unit
return x >>= f   =  f x                      -- left unit

(m >>= f) >>= g  =  m >>= (\x -> f x >>= g)  -- associativity


ApplicativeMonad的超类（superclass）。所以，Monad即是Applicative也是Functor

## Category

范畴论（category theory）里的范畴，是对象（objects）、映射（morphisms）和映射的组态（configure）的集合（collection）。

## Optics

Optics 翻译是光学，允许在数据类型里存取数据。另外翻到一篇天书 Categories of Optics

## Algebraic data type

Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e., tagged or disjoint unions, coproduct types or variant types).

The values of a product type typically contain several values, called fields. All values of that type have the same combination of field types. The set of all possible values of a product type is the set-theoretic product, i.e., the Cartesian product, of the sets of all possible values of its field types.

The values of a sum type are typically grouped into several classes, called variants. A value of a variant type is usually created with a quasi-functional entity called a constructor. Each variant has its own constructor, which takes a specified number of arguments with specified types. The set of all possible values of a sum type is the set-theoretic sum, i.e., the disjoint union, of the sets of all possible values of its variants. Enumerated types are a special case of sum types in which the constructors take no arguments, as exactly one value is defined for each constructor.

Values of algebraic types are analyzed with pattern matching, which identifies a value by its constructor or field names and extracts the data it contains.

## Parametric Polymorphism

Parametric Polymorphism is a way to define types or functions that are generic over other types.

The genericity can be expressed by using type variables for the parameter type, and by a mechanism to explicitly or implicitly replace the type variables with concrete types when necessary.

## Product

Y可能不等于X1 x X2，因为坐标轴可能不是正交的。

## Monoid

monoid，孤立点是一个拥有以下两个的单形范畴（monoidal category，二元外积值域是本范畴的范畴）里的一个对象：

• 对象外积的是自身
• 单元映射的是自身