FP的范畴论

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 (id_{x}): x -> x 

Group

A group is a set together with a binary operation on G(定义一个集合和集合上的二元关系), has denoted "\cdot", that combines any two elements and to form an element of G, denoted a \cdot b (closure). such that the following requirements (group axoims) are satisfied:

  • associativity: for all a, b, c in G, one has (a\cdot b)\cdot c = a\cdot \left ( b\cdot c \right )
  • identity element
    • there exists an element in G such that, for every in G, one has e \cdot a = a and a\cdot e =e (这种元和其他元素结合时,不会改变元素)
    • such an element is unique, it is called the identity element(幺元/单位元) of the group.
  • inverse element
    • for each in G, there exists an element in G, such that a\cdot b=e and b\cdot a=e, where e is the identity element.
    • for each a, the element is unqiue, it is called the inverse of a.

如果仅满足封闭性和结合律,则称G是一个半群;如果仅满足封闭性、结合律并且有幺元,则称G是一个含幺半群(monoid,例子如:非负整数和加法、正整数和乘法)。

Functor

A functor is a mapping between categories that:

  • associate each object X in category to an object F(X) in category D.
  • associate each morphismf: X -> Y in to a morphism F(f): F(X) -> F(Y) in such that the following two conditions hold (that is, functors must preserve identity morphisms and composition of morphisms) :
    • F(id_{x}) = id_{F(X)} for every object in C, 
    • F(g f) = F(g) F(f) 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 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中函数通过点号(.)进行组合。

Reference

Monads 和 Monoids

Haskell 和 Category Theory

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值