Category Theory for Functional Programming

Category Theory is called “The Mathematics of Mathematics“, also the “Abstract Nonsense“.


Key Motivations for Category Theory 范畴 :
1. Programming is Math.
2. Object-Oriented is based on Set Theory which has 2 weaknesses:
◇ Set has contradiction: The famous “Russell’s Barber Paradox”.



Data Immutability for Concurrent Processing : OO can’t control the mutable state of objects, making debugging impossible.

Category (“cat“) has 3 properties:
1. Objects
eg. Set, List, Group, Vector Space, anything…
2. Arrows (“Morphism”, between Objects) which are Associative
eg. function, sheaf, etc
3. Identity Object

Note: If the Identity is “0” or “Nothing”, then it is called Free Category.

Extensions :
1. “Cat” = Category of categories, is also a category.
2. Functor 函子 = Arrows between Categories.
3. Monoid = A Category with ONLY 1 Object.



Monoid (么半群) is a very powerful concept (used in Natural Language Processing) — basically it is a Group with No ‘I‘nverse (Mo‘No‘-‘I‘-d).



Note: In the pure Functional Programming Language Haskell:
Monad (单子) = Monoid with endo-functor.
Endo- = to itself (自己) eg. “f : A -> A”


Recommended :
1. The Best Book on Category Theory : Lawvere et al, Cambridge (2009, 2nd Edition)


2. A humorous book on Category Theory by Dr. Eugenia Cheng (2016): (found in Singapore NLB Loan book #501.1 CHE)


Mathematics: The Next Generation

Historical Backgroud:

Math evolves since antiquity, from Babylon, Egypt 5,000 years ago, through Greek, China, India 3,000 years ago, then the Arabs in the 10th century taught the Renaissance Europeans the Hindu-Arabic numerals and Algebra, Math progressed at a condensed rapid pace ever since: complex numbers to solve cubic equations in 16th century Italy, followed by the 17 CE French Cartersian Analytical Geometry, Fermat’s Number Theory,…, finally by the 19 CE to solve quintic equations of degree 5 and above, a new type of Abstract Math was created by a French genius 19-year-old Evariste Galois in “Group Theory”. The “Modern Math” was born since, it quickly develops into over 4,000 sub-branches of Math, but the origin of Math is still the same eternal truth.

Math Education Flaw: 本末倒置 Put the cart before the horse.

Math has been taught wrongly since young, either is boring, or scary, or mechanically (calculating).

This lecture by Queen Mary College (U. London) Prof Cameron is one of the rare Mathematician changing that pedagogy. Math is a “Universal Language of Truths” with unambiguous, logical syntax which transcends over eternity.

I like the brilliant idea of making the rigorous Math foundation compulsory for all S.T.E.M. (Science, Technology, Engineering, Math) undergraduate students. Prof S.S. Chern 陈省身 (Wolf Prize) after retirement in Nankai University (南开大学, 天津, China) also made basic “Abstract Algebra” course compulsory for all Chinese S.T.E.M. undergraduates in 2000s.

The foundations Prof Cameron teaches are centered around 4 Math Objects:

1. SET 集合
– Set is the founding block of the 20th century Modern Math, hitherto introduced into the world’s university textbooks by the French “Bourbaki” school (André Weil et al) after WW1.

Note: The last “Bourbaki” grand master Grothendieck proposed to replace Set by Category. That will be the next century Math for future Artificial Intelligence Era, aka “The 4th Human Revolution”.

– A vision first proposed by the German Gottingen School’s greatest Math Educator Felix Klein, who said Functions can be visualised in graphs, so it is the best tool to learn mathematical abstractness.

– The German mathematician Leopold Kronecker, who once wrote that “God made the integers; all else is the work of man.”

– The universe is composed of numbers in “NZQRC” (ie Natural numbers, Integers, Rationals, Reals, Complex numbers). After C (Complex), no more further split of new numbers. Why?

4. Proofs
– reading and debugging proofs.

Example 1: Proof by Contradiction, aka Reductio ad Absurdum (Euclid’s Proof on Infinitely Many Prime Numbers)


Challenge the proof: Why ?
Induction intuitively by:


Example 2: Proof by Logic

By Reasoning (which is unconscious), most would get “2 & A” (wrong answer)

By Logic (using consciousness), then you can proof …
Correct Answer: 2 and B
Test on all 3 Truth cases below in Truth Table:
p = front side
q = back side


Recommended The Best Book on Abstract Algebra by Prof. Peter J. Cameron

What exactly is a Limit ?

\displaystyle\lim_{x\to a}f(x) = L \iff
\forall  \varepsilon >0, \exists \delta >0 such that
\boxed{0<|x-a|<\delta} \implies |f(x)-L|< \varepsilon

The above scary ‘epsilon-delta’ definition of “Limit” by the French mathematician Cauchy in 19th century is the standard rigorous definition in today’s Analysis textbooks.

It was not taught in my Cambridge GCE A-Level Pure Math in 1970s (still true today), but every French Baccalaureate Math student (Terminale,  equivalent to JC 2 or Pre-U 2) knows it by heart. A Cornell University Math Dean recalled how he was told by his high-school teacher to memorise it — even though he did not fully understand — the “epsilon-delta” definition by “chanting”:

“for all epsilon, there is a delta ….”

(French: Quelque soit epsilon, il existe un delta …)

In this video, I am glad someone like Prof N. Wildberger recognised its “flaws”  albeit rigorous, by suggesting another more intuitive definition:
◇ Cauchy’s “flaw”: ambiguous
Finding a certain \delta = f (\varepsilon )
too counter-intuitive to grasp the idea by most university math students.

Intuitive Alternative:
\boxed { \displaystyle { \lim_{n \to \infty} P(n) = A} } \iff
Find any 2 natural numbers m (“Start“), k (“Scale“) such that:
\boxed { \text {For } m \leq n,  \frac {-k}{n} \leq P(n) - A \leq \frac {k}{n} }

A Simple Analogy in Life:
Let P(n) = Any Person’s lifespan of age n
m = ‘Start’ Age to retire, say 60
k = ‘Scale’ of interval (in years, eg. 1 year)
A = Limit of a person’s lifespan, say 80 (male) or 85 (female)

As we grow older (n increases), from a certain “Start” point (m), our lifespan P(n) approaches the limit A, plus or minus k/n (years).

Math Foundations

MathFoundations (all videos):

This is the Felix Klein’s Vision of Elementary Math from an Advanced Standpoint“.

All the Math we learn are taught as such by teachers and professors, but why so? what are the foundations ? These 200 videos answer them !

Good for students to appreciate Math and, hopefully, they will love the Math subject after viewing most of these 200 great videos.

Video 1: Natural Number This should be taught in kindergartens to 3-year-old kids.

◇ What is number ? (strings of 1s),
◇ Equal, bigger, smaller concepts are “pairing up” (1-to-1 mapping) two strings of 1s.
◇ Don’t teach the kids how to write first 12345…, without prior building these mathematical foundational concepts.

Video 2: Arithemetic
Distributive Law:
k x (m+n) = (k x m) + (k x n) = k x m + k x n

Convention: ‘x’ (or its reverse ‘÷’) takes precedent over ‘+’ (or its reverse ‘-‘).
(先 x ÷ 后 + -)

Video 31: Affine Geometry (仿射几何)

Video 32: Geometry Education in Primary Schools
◇ Use Grid paper to visualize concrete objects (line, square, triangle, circle…)
◇ Arithmetic is closely linked to Geometry which has been ignored in schools in the last 3 decades since the introduction of Modern Math (vector algebra).

Video 34: Signed Area of Polygon

Video 106: What exactly is a Limit

French Flawed Elite Higher Education: Classes Préparatoires (CP)

3 top Classes Préparatoires (CP) (Baccalaureate + 2 years, equivalent to Bachelor of Math & Science) supply more than 50% of the elite Ecole Polytechnique (X) & Ecole Normale Supérieure (ENS) (Masters degree in Engineering or Business) students each year:
1. Lycée Louis-Le-Grande (LLG) – Paris
2. Lycée Privé Sainte-Geneviève – Paris (Versailles)
3. Lycée Henri IV – Paris

These students come from families in the rich Parisian regions, whose parents and grand-parents are also the alumni of X or ENS — this education phenomenon is called “In-Breeding”, or in Mathematical parlence “Closure”. The flaw of this “Social Immobility” is that “The rich gets richer, the poor gets poorer“, because poor provincial French families have no resources to send their children to these top “CPs” to better prepare for the elite Grandes Ecoles. Although the “Concours” (Entrance Exams 科举) is meritocratic and fair, the preparation for “Concours” in the 2-year excellent top “CP” is not a level-playing field.

This Concours which was inherited from ancient China had already manifested for thousand years in the flawed elitism of Chinese “Mandarin” bureaucracy (官僚制度) — the top scholars were from the same closed circles of elite families, either mandarin parents or rich merchants. China had few major revolutions initiated by failed “Concours” ( 科举) scholars of poor provincial sons, notably “黄巢”之乱 (Hwang Chao Revolution) in 9 CE which brought down the glorious Tang (唐) Dynasty, the 洪秀全 (太平天国 Taiping Revolution, 19 CE) also preluded 40 years before the demise of Qing (清) Dynasty. In certain resemblance, the poor farmer’s non-university-graduate son Mao ZeDong launched the “Cultural Revolution” to purge the “Smelly Elite Scholars” (臭老九) inherited from the “Old China”.

It is no surprise for France, after the glorious 30-year “5th Republique” (1950 – 1980), to see so many social unrests – “non-elite” university graduate high unemployment, Arabic-African Muslim immigrant descendents’ riots, ISIS terrorism …, while the mostly-white Grandes Ecoles elites monopolise the top social echelons in government, civil service, big state and private enterprises. The previous French President faced strong resistance from the elites to introduce “diversity” (ie reserve certain student quota for poor family children and minority races) in these top Grandes Ecoles.

Circle in Different Representations

1-Dimensional Objects:
Affine Line: {\mathbb {A}^1}
Circle: {\mathbb {S}^1}

Six Representations of a Circle: {\mathbb {S}^1}
1) Euclidean Geometry (O-level Math)
Unit Circle : x^2 + y^2 = 1

2) Curve: (A-level Math)
Transcendental Parameterization :
\boxed { e(\theta) = (\cos \theta, \sin  \theta) \qquad  0 \leq \theta \leq 2\pi }

Rational Parameterisation :
\boxed { e(h) = \left(\frac {1-h^2} {1+h^2}  \: , \: \frac {2h} {1+h^2}\right) \quad \text { h any number or } \infty }


3) Affine Plane (French Baccalaureate – equivalent A-level – Math) {\mathbb {A}^2}
1-Dim Sub-spaces = Projective Lines thru’ Origin

4) Polygonal Representation (Undergraduate Math)

5) Identifying Intervals: (closed loop) (Undergraduate Math)

6) \text {Translation } (\tau, {\tau}^{-1}) \text { on a Line } {\mathbb {A}^1}
(Honors Year Undergraduate / Graduate Math)

[Using Quotient Group Notation]:
\boxed {  {\mathbb {S}^1} \simeq {\mathbb {A}^1 } \Big/ { \langle \tau , {\tau}^{-1} \rangle} }
{\mathbb {S}^1} = \text { Space of all orbits}


Are Circle and Line the same 1-dimensional object, i.e. are they Homeomorphic (同胚) in Topology ?

Answer: To be continued in the next blog “Homeomorphism