Category
A category consists of: objects, morphisms(or arrows / maps) between the objects.
- for ever three
- the following axioms hold:
- associativity: if f: a -> b, g: b -> c, h: c -> d then h (gf) = (hg) f;
- identity: for every object, there exists an identity morphism (
): x -> x
Group
A group is a set G together with a binary operation on G(定义一个集合和集合上的二元关系), has denoted "", that combines any two elements a and b to form an element of G, denoted
(closure). such that the following requirements (group axoims) are satisfied:
- associativity: for all a, b, c in G, one has
- identity element:
- there exists an element e in G such that, for every a in G, one has
and
(这种元和其他元素结合时,不会改变元素)
- such an element is unique, it is called the identity element(幺元/单位元) of the group.
- there exists an element e in G such that, for every a in G, one has
- inverse element
- for each a in G, there exists an element b in G, such that
and
, where e is the identity element.
- for each a, the element b is unqiue, it is called the inverse of a.
- for each a in G, there exists an element b in G, such that
如果仅满足封闭性和结合律,则称G是一个半群;如果仅满足封闭性、结合律并且有幺元,则称G是一个含幺半群(monoid,例子如:非负整数和加法、正整数和乘法)。
Functor
A functor is a mapping between categories that:
- associate each object X in category C to an object F(X) in category D.
- associate each morphism: f: X -> Y in C to a morphism F(f): F(X) -> F(Y) in D such that the following two conditions hold (that is, functors must preserve identity morphisms and composition of morphisms) :
for every object X in C,
for all morphisms f: X -> Y and g: Y -> Z in C.
函子描述的是范畴间的映射,函数描述的是类型间的映射。自函子是一个将范畴映射到自身的函子,Identity函子是自函子的一种特例,即对范畴中的元素和关系不做任何改变。自函数就是把类型映射到自身类型,Identity函数是自函数的一种特例,它接收什么参数就返回什么参数,所以入参和返回值不仅类型一致,值也相同。
函子范畴:以范畴C到D的函子为对象,以函子间的自然变换(将一个函子变为另一个函子)为(函子间的)态射,所构成的范畴。
Monad
"A monad is just a monoid in the category of endofunctors"
A monad is a type of endofunctor (from a category to that same category). The two morphisms unit and join must exist for every object for a given monad (The normal way of defining a monad in category theory is to give unit and join, whereas Haskell programmers like to give return and bind(>>=), we can recover join and bind from each other). Any time we have some kind of structure M and a natural way of taking any object X into M(X), as well as a way of taking M(M(X)) into M(X), we probably have a monad.
在Haskell中,自函子范畴(object是自函子,morphism是自函子间的自然变换)下的幺半群是针对类别为 * -> * 的自函子(比如Maybe、[]这种自函子)而言的,而这里的二元操作是 join,单位元是 return:
-- 单位律
join · return == id
-- 结合律
join · ( join · join ) == ( join · join ) · join
在Haskell中存在名为Hask的范畴, 该范畴的对象就是Haskell里的所有类型;态射就是Haskell里的一般函数(function),如 func :: Int -> Bool 将对象 int 映射为对象 bool;态射的组合就是函数的组合,在 Haskell中函数通过点号(.)进行组合。