Even if you are not a functional programmer, the notion of product type should be familiar to you, e.g., Pair<A, B> in Java is a product type of A and B. But the definition in category theory is not that easy to comprehend. Here is how it is defined on Wikipedia:

Let C be a category with some objects X1 and X2. A product of X1 and X2 is an object X (often denoted X1 × X2) together with a pair of morphisms π1 : X → X1, π2 : X → X2 that satisfy the following universal property: for every object Y and pair of morphisms f1 : Y → X1, f2 : Y → X2 there exists a unique morphism f : Y → X1 × X2 such that the following diagram commutes:

Why is it defined that way and how do we interpret it? Let me translate it into something that Java programmers can understand. The definition actually says, if X1 x X2 is a product type of X1 and X2 with two functions π1 : X -> X1 and π2 : X -> X2, there must be a unique function f : Y -> X1 × X2 which satisfies the property: for any value y of type Y, function f1 : Y -> X1 and a function f2 : Y -> X2, the equations π1(f(y)) == f1(y) and π2(f(y)) == f2(y) must always be true.

In other words, if I define my product type as usual like:

  1. // Java
  2. class Pair<X1, X2> {
  3. private final X1 x1;
  4. private final X2 x2;
  5. public Pair(X1 x1, X2 x2) {
  6. this.x1 = x1;
  7. this.x2 = x2;
  8. }
  9. public X1 getX1() {
  10. return x1;
  11. }
  12. public X2 getX2() {
  13. return x2;
  14. }
  15. }

There must be a unique f which is constructed by:

  1. // Java
  2. Function<Y, Pair<X1, X2>> makeF(Function<Y, X1> f1, Function<Y, X2> f2) {
  3. return (Y y) -> new Pair(f1.apply(y), f2.apply(y));
  4. }

In other words, product type guarantees that if you have a function of type Y -> X1 and a function of type Y -> X2, you must have a unique function of type Y -> X1 x X2 satisfying the property. The property can be expressed programatically as: for any y, f1 and f2, the following test must pass.

  1. // Java
  2. void testProductType(Y y, Function<Y, X1> f1, Function<Y, X2> f2) {
  3. Function<Y, Pair<X1, X2>> f = makeF(f1, f2);
  4. assert(f.apply(y).getX1() == f1.apply(y));
  5. assert(f.apply(y).getX2() == f2.apply(y));
  6. }

So what could be a counterexample? Here is:

  1. // Java
  2. class Pair<X1, X2> {
  3. private final X1 x1;
  4. private final X2 x2;
  5. public Pair(X1 x1, X2 x2) {
  6. this.x1 = x1;
  7. this.x2 = x2;
  8. }
  9. public X1 getX1() {
  10. return 1;
  11. }
  12. public X2 getX2() {
  13. return 2;
  14. }
  15. }

With this wrong definition of product type, you cannot possibly construct such a f which satisfies the universal property, i.e., there are always some cases which can make the test fail.

If you think it is done, here comes the tricky part, is the type below a product type?

  1. // Java
  2. class Pair<X1, X2> {
  3. private final X1 x1;
  4. private final X2 x2;
  5. public Pair(T x1, U x2) {
  6. this.x1 = x1;
  7. this.x2 = x2;
  8. }
  9. public T getX1() {
  10. return x1 + 1;
  11. }
  12. public T getX2() {
  13. return x2 + 2;
  14. }
  15. }

Intuition may tell you it is not a product type, but by definition of product type in the category theory, it actually is. Why? Because you can define a unique f satisfying the property:

  1. // Java
  2. Function<Y, Pair<X, Y>> makeF(Function<Y, X1> f1, Function<Y, X2> f2) {
  3. return (Y y) -> new Pair(f1.apply(y) - 1, f2.apply(y) - 2);
  4. }

What this means is that, the two product types are equivalent in category theory. This is because category theory defines equivalence by structure, if two things have the same structure, they are considered the same thing.

Then, what about sum type (a.k.a coproduct type)? The definition in category theory is:

Let C be a category and let X1 and X2 be objects in that category. An object is called the coproduct of these two objects, written X1 ∐ X2 or X1 ⊕ X2 or sometimes simply X1 + X2, if there exist morphisms i1 : X1 → X1 ∐ X2 and i2 : X2 → X1 ∐ X2 satisfying a universal property: for any object Y and morphisms f1 : X1 → Y and f2 : X2 → Y, there exists a unique morphism f : X1 ∐ X2 → Y such that f1 = f ∘ i1 and f2 = f ∘ i2. That is, the following diagram commutes:

From program perspective, the definition says, if X1 ∐ X2 is a sum type of X1 and X2 with two functions i1 : X1 -> X1 ∐ X2 and i2 : X2 → X1 ∐ X2, there must be a unique function f : X1 ∐ X2 -> Y which satisfies the property: for any value y : Y, function f1 : X1 -> Y and function f2 : X2 -> Y, the equations f(i1(y)) == f1(y) and f(i2(y)) == f2(y) must always be true.

If I define sum type as below:

  1. // Java
  2. class Either<X1, X2> {
  3. private final Optional<X1> x1;
  4. private final Optional<X2> x2;
  5. private Either(Optional<X1> x1, Optional<X2> x2) {
  6. this.x1 = x1;
  7. this.x2 = x2;
  8. }
  9. public static Either<X1, X2> left(X1 x1) {
  10. return new Either(Optional.of(x1), Optional.absent());
  11. }
  12. public static Either<X1, X2> right(X2 x2) {
  13. return new Either(Optional.absent(), Optional.of(x2));
  14. }
  15. public Optional<T> getX1() {
  16. return x1;
  17. }
  18. public Optional<U> getX2() {
  19. return x2;
  20. }
  21. }

There must be a unique f which is constructed by:

  1. // Java
  2. Function<Either<X1, X2>, Y> makeF(Function<X1, Y> f1, Function<X2, Y> f2) {
  3. return (Either<X1, X2> e) -> e.getX1().isPresent() ? f1.apply(e.getX1().get()) : f2.apply(e.getX2().get());
  4. }

In other words, sum type guarantees that if you have a function of type X1 -> Y and a function of type X2 -> Y, you must have a unique function of type X1 ∐ X2 -> Y satisfying the property. The property can be verified programatically as: for any x1, x2, f1, f2 the following tests must pass.

  1. // Java
  2. void testSumType(X1 x1, X2 x2, Function<X1, Y> f1, Function<X2, Y> f2) {
  3. assert(f.apply(Either.left(x1)) == f1.apply(x1));
  4. assert(f.apply(Either.left(x2)) == f2.apply(x2));
  5. }

To sum up, category theory defines product and sum type by requiring them to be able to construct such a function which satisfies a universal property.

Product and Sum in Category Theory的更多相关文章

  1. Category Theory: 01 One Structured Family of Structures

    Category Theory: 01 One Structured Family of Structures 这次看来要放弃了.看了大概三分之一.似乎不能够让注意力集中了.先更新吧. 群的定义 \( ...

  2. 【leetcode】1281. Subtract the Product and Sum of Digits of an Integer

    题目如下: Given an integer number n, return the difference between the product of its digits and the sum ...

  3. [Leetcode] 5279. Subtract the Product and Sum of Digits of an Integer

    class Solution { public int subtractProductAndSum(int n) { int productResult = 1; int sumResult = 0; ...

  4. Spring学习笔记2——创建Product对象,并在其中注入一个Category对象

    第一步:创建Product类.在Product类中有对Category对象的set和get方法 package com.spring.cate; public class Product { priv ...

  5. Web API开发实例——对产品Product进行增删改查

    1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...

  6. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  7. <<Differential Geometry of Curves and Surfaces>>笔记

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

  8. 对话机器学习大神Yoshua Bengio(下)

    对话机器学习大神Yoshua Bengio(下) Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun ...

  9. <Differential Geometry of Curves and Surfaces>(by Manfredo P. do Carmo) Notes

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

随机推荐

  1. 数据结构树之AVL树(平衡二叉树)

    一 什么是AVL树(平衡二叉树): AVL树本质上是一颗二叉查找树,但是它又具有以下特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.在AVL树中任何节 ...

  2. linux 网络管理的三种方式

    修改网络IP的三种方式 1.修改配置文件 1.1dhcp自动获取 配置文件地址/etc/sysconfig/network-scripts TYPE=Ethernet  #类型=以太网 PROXY_M ...

  3. CentOS7 安装VNC

    系统环境:CentOS Linux release 7.6.1810Kernel:3.10.0-957.el7.x86_64系统现状:最小化安装,没有安装任何图形支持软件 安装图形化支持 不建议安装G ...

  4. python脚本执行报错整理

    people = [ {'name':'alex','age':1000}, {'name':'wuxie','age':100}, {'name':'wangcanghai','age':9000} ...

  5. google搜索引擎正确打开姿势

    Google搜索引擎 原文来自黑白之道微信公众号       https://mp.weixin.qq.com/s/Ey_ODP_mG00of5DPwcQtfg   这里之所以要介绍google搜索引 ...

  6. 选择文件,显示其路径在ListBox控件里

    private void btnSelect_Click(object sender, EventArgs e)        {            lbxFiles.Items.Clear(); ...

  7. react-01

    比较了半天VUE.Angular.React,最终选择React,下面从几个例子开始React的学习,先从单个的index.html,引用react.js开始 一.最简单的纯JS的代码 <!DO ...

  8. Jquery 数组操作大全【转载】

    转载于:https://www.jb51.net/article/43164.htm 1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍 jQuery 对象的 ...

  9. Linq语言,由红色部分可直接代替绿色(List,dictionary)

    /// <summary> /// 获取最近5分钟缓存的车量 /// </summary> /// <param name="carNo">&l ...

  10. 口试C#概念

    C#概念 装箱拆箱: 值类型与引用类型:值类型:System.ValueType(继承自System.Object)引用类型:System.Object 反射:反射提供一种编程方式,让程序员可以在程序 ...