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:

// Java
class Pair<X1, X2> {
private final X1 x1;
private final X2 x2; public Pair(X1 x1, X2 x2) {
this.x1 = x1;
this.x2 = x2;
} public X1 getX1() {
return x1;
} public X2 getX2() {
return x2;
}
}

There must be a unique f which is constructed by:

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

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.

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

So what could be a counterexample? Here is:

// Java
class Pair<X1, X2> {
private final X1 x1;
private final X2 x2; public Pair(X1 x1, X2 x2) {
this.x1 = x1;
this.x2 = x2;
} public X1 getX1() {
return 1;
} public X2 getX2() {
return 2;
}
}

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?

// Java
class Pair<X1, X2> {
private final X1 x1;
private final X2 x2; public Pair(T x1, U x2) {
this.x1 = x1;
this.x2 = x2;
} public T getX1() {
return x1 + 1;
} public T getX2() {
return x2 + 2;
}
}

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:

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

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:

// Java
class Either<X1, X2> {
private final Optional<X1> x1;
private final Optional<X2> x2; private Either(Optional<X1> x1, Optional<X2> x2) {
this.x1 = x1;
this.x2 = x2;
} public static Either<X1, X2> left(X1 x1) {
return new Either(Optional.of(x1), Optional.absent());
} public static Either<X1, X2> right(X2 x2) {
return new Either(Optional.absent(), Optional.of(x2));
} public Optional<T> getX1() {
return x1;
} public Optional<U> getX2() {
return x2;
}
}

There must be a unique f which is constructed by:

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

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.

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

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. mac pfctl / centos iptables 使用

    mac使用pfctl 为了测试zk client的重连功能,需要模拟zk client与zk server网络连接出现问题的情况,经过查询资料发现可以使用防火墙阻止zk server启动端口上的流量实 ...

  2. 2. 2A03简介

    2A03简介 1.CPU 1.1 内部寄存器 1.累加寄存器A(Accumulator):8位寄存器,用于同算术逻辑单元(ALU)共同完成各种算术逻辑运算,它既为ALU提供原始操作数又担任存放ALU运 ...

  3. DynamoDB

    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html Creating a  New  Table I ...

  4. LINUX日常操作二

    参见:Linux日常操作一  selinux 开启和关闭 一.查看SELinux状态:1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled ...

  5. Spring+MVC Controller层接收App端请求的中文参数乱码问题。

    在正文之前,说明下Filter的作用: 过滤器顾名思义就是进行过滤的,可以实现代码的定向执行和预处理.通俗点说法filter相当于加油站,request是条路,response是条路,目的地是serv ...

  6. VB 读取列表文件名

    Private Sub Command1_Click()Dim d As String, s As String If Dir(App.Path & "\down", vb ...

  7. ES6 学习之旅

    最常用的ES6特性 1.let, const 2.class, extends, super 3.arrow function (箭头函数) 4.template string (用反引号(`)来标识 ...

  8. jq无法获取ng-repeat元素,如何控制ng-repeat元素显示与隐藏?

    之前都是在做微信小程序的页面,最近做一些html页面,页面也没什么效果,就弄了几个点击事件,控制一些元素的显示与隐藏.后面用angular来写这些页面,然后就遇到了问题,就是用ng-repeat生成的 ...

  9. 20155312张竞予 Exp1 PC平台逆向破解(5)M

    Exp1 PC平台逆向破解(5)M 目录 实验内容 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发get ...

  10. O365 Manager Plus帮助台委派功能一览表

    O365 Manager Plus帮助台委派介绍 虽然Office 365允许您在全球任何地方工作,但它提供的管理功能十分不足.当一个组织分布在多个国家/地区时,一个管理员很难单独管理所有用户和邮箱. ...