Product and Sum in Category Theory
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 objectsX1
andX2
. A product ofX1
andX2
is an objectX
(often denotedX1 × X2
) together with a pair of morphismsπ1 : X → X1
,π2 : X → X2
that satisfy the following universal property: for every objectY
and pair of morphismsf1 : Y → X1
,f2 : Y → X2
there exists a unique morphismf
: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 letX1
andX2
be objects in that category. An object is called the coproduct of these two objects, writtenX1 ∐ X2
orX1 ⊕ X2
or sometimes simplyX1 + X2
, if there exist morphismsi1 : X1 → X1 ∐ X2
andi2 : X2 → X1 ∐ X2
satisfying a universal property: for any objectY
and morphismsf1 : X1 → Y
andf2 : X2 → Y
, there exists a unique morphismf : X1 ∐ X2 → Y
such thatf1 = f ∘ i1
andf2 = 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的更多相关文章
- Category Theory: 01 One Structured Family of Structures
Category Theory: 01 One Structured Family of Structures 这次看来要放弃了.看了大概三分之一.似乎不能够让注意力集中了.先更新吧. 群的定义 \( ...
- 【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 ...
- [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; ...
- Spring学习笔记2——创建Product对象,并在其中注入一个Category对象
第一步:创建Product类.在Product类中有对Category对象的set和get方法 package com.spring.cate; public class Product { priv ...
- Web API开发实例——对产品Product进行增删改查
1.WebApi是什么 ASP.NET Web API 是一种框架,用于轻松构建可以由多种客户端(包括浏览器和移动设备)访问的 HTTP 服务.ASP.NET Web API 是一种用于在 .NET ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- <<Differential Geometry of Curves and Surfaces>>笔记
<Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...
- 对话机器学习大神Yoshua Bengio(下)
对话机器学习大神Yoshua Bengio(下) Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun ...
- <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== ...
随机推荐
- LAB8 android
妈的,标签名字能改成自己的名字,我也是个神人嘞. 明明是去掉两个括号,怎么变成3个了,醉了. 点组件,attribute,可以修改对应的值.非常直观?. content_mail.XML要设置ID才能 ...
- 51-python3 pandas读写excel
转载自:https://blog.csdn.net/brink_compiling/article/details/76890198?locationNum=7&fps=1 0. 前言Pyth ...
- webpack4.0
1. webpack 刚开始是js的模块打包,现在是一个任何模块打包工具 可以识别 CommonJS引入规范 CMD AMD 2. commonJS: module.exports r ...
- Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {}: medium is already associated with the current state of machine uuid {}返回 代码: VBOX_E_OBJECT_IN_USE (0x80BB000C)
详细的错误信息如下: Cannot attach medium 'D:\program\VirtualBox\VBoxGuestAdditions.iso' {83b35b10-8fa2-4b81-8 ...
- Chart Parser 中 Earley's 算法的应用
1. 基本概念 1.1 状态 state 上下文无关文法规则 圆点 · (左边是已分析的,右边是未分析的:点在最右端表示完成状态,否则为未完成状态) 状态的起止位置 1.2 基本操作/算子 opera ...
- jmeter javamail 邮件格式再优化(由详情——>改为统计)
前言:之前扩展的ant—jmeter支持邮件附件形式上传以及邮件内容的html文件格式. 如图: 由于邮件的内容格式是详情信息,也就是说直观的显示的是case,但由于case的增加,邮件内容越来越大! ...
- c# maiform父窗体改变动态的gridew 奇偶行变色的快捷方法
无需在每个usercontrol里边单个指定控件内gridview 隔行换色.只需要在主窗体内改成统一就好了 做到这点要明白.gridcontrol 是usercontrol 的子控件 , grid ...
- docker--centos镜像安装tomcat jdk1.8 mysql部署java web项目
一.下载centos7标准镜像及安装mysql5.7 在centos安装mysql5.7 二.安装jdk 1.查询可用jdk版本 yum search java|grep jdk 2.根据搜索到的jd ...
- koa 学习1
1.搭建环境; npm init -f 2.出现错误 npm ERR!Windows_NT 6.1.7601 解决方法: npm config set proxy null npm install ...
- 26. pt-summary
pt-summary # Percona Toolkit System Summary Report ###################### Date | 2018-11-23 10:48:51 ...