 # [Category Theory via C# series]

Category theory is a theoretical framework to describe abstract structures and relations in mathematics, first introduced by Samuel Eilenberg and Saunders Mac Lane in 1940s. It examines mathematical concepts and properties in an abstract way, by formalizing them as collections of items and their relations. Category theory is abstract, and called "general abstract nonsense" by Norman Steenrod; It is also general, therefore widely applied in many areas in mathematics, physics, and computer science, etc. For programming, category theory is the algebraic theory of types and functions, and also the rationale and foundation of LINQ and any functional programming. This chapter discusses category theory and its important concepts, including category, morphism, natural transform, monoid, functor, and monad, etc. These general abstract concepts will be demonstrated with intuitive diagrams and specific C# and LINQ examples. These knowledge also helps building a deep understanding of functional programming in C# or other languages, since any language with types and functions is a category-theoretic structure.

# Category and category laws

In category theory, a category C is a algebraic structure consists of the following 3 kinds of mathematical entities:

And these entities must satisfy the following 2 category laws:

To make above abstract definitions intuitive, a category can be represented by the following interface:

```public interface ICategory<TObject, TMorphism>
{
IEnumerable<TObject> Objects { get; }

TMorphism Compose(TMorphism morphism2, TMorphism morphism1);

TMorphism Id(TObject @object);
}```

A simple example of category is the category of integers, where the collection of objects are all integers, and the collection of morphisms are ≤ (less than or equal to) relations, from an integer either to itself, or to another integer greater than or equal to it, for example: m1: 0 → 1 (0 ≤ 1), m2: 1 → 10 (1 ≤ 10), etc. Regarding the transitivity of inequality, the ≤ morphisms can be composed, for example, m1: 0 → 1 (0 ≤ 1) and m2: 1 → 10 (1 ≤ 10) can be composed to another morphism (m2 ∘ m1): 0 → 10 (0 ≤ 10).  Apparently, the above composition is associative, foe example: ((1 ≤ 10) ∘ (0 ≤ 1)) ∘ (-1 ≤ 0) ≡ -1 ≤ 10 ≡ (1 ≤ 10) ∘ ((0 ≤ 1) ∘ (-1 ≤ 0)). And for each integer X, there is an identity morphism idX: X → X (X ≤ X), and (Y ≤ Y) ∘ (X ≤ Y) ≡ X ≤ Y ≡ (X ≤ Y) ∘ (X ≤ X). So the category laws are satisfied. In C#, integer can be represented by int, and the morphism of ≤ relation can be represented by a BinaryExpression of node type LessThanOrEqual, so the category can be represented as:

```public class Int32Category : ICategory<int, BinaryExpression>
{
public IEnumerable<int> Objects
{
get
{
for (int int32 = int.MinValue; int32 <= int.MaxValue; int32++)
{
yield return int32;
}
}
}

public BinaryExpression Compose(BinaryExpression morphism2, BinaryExpression morphism1) =>
Expression.LessThanOrEqual(morphism2.Left, morphism1.Right); // (Y <= Z) ∘ (X <= Y) => X <= Z.

public BinaryExpression Id(int @object) =>
Expression.LessThanOrEqual(Expression.Constant(@object), Expression.Constant(@object)); // X <= X.
}```

# DotNet category

.NET can also be viewed as a category of types and functions, called DotNet:

• ob(DotNet): the collection of objects in DotNet category are .NET types, like string (System.String), int (System.Int32), bool (System.Boolean), etc.
• hom(DotNet): the collection of morphisms in DotNet category are .NET pure functions between the input type (source object) to the output type (target object), like int.Parse: string → int, DateTime.IsLeapYear: int → bool, etc.
• ∘: in DotNet category, the composition operation of morphisms is the composition of functions.

As already discussed in lambda calculus chapter, function composition is associative, and the unit function Id is the identity morphism:

```public static partial class Functions
{
public static Func<TSource, TResult> o<TSource, TMiddle, TResult>(
this Func<TMiddle, TResult> function2, Func<TSource, TMiddle> function1) =>
value => function2(function1(value));

public static TSource Id<TSource>(T value) => value;
}```

So that the category laws are satisfied. The DotNet category can be represented as:

```public partial class DotNetCategory : ICategory<Type, Delegate>
{
public IEnumerable<Type> Objects =>
SelfAndReferences(typeof(DotNetCategory).Assembly)
.SelectMany(assembly => assembly.GetExportedTypes());

public Delegate Compose(Delegate morphism2, Delegate morphism1) =>
// return (Func<TSource, TResult>)Functions.Compose<TSource, TMiddle, TResult>(
//    (Func<TMiddle, TResult>)morphism2, (Func<TSource, TMiddle>)morphism1);
(Delegate)typeof(Linq.FuncExtensions).GetMethod(nameof(Linq.FuncExtensions.o))
.MakeGenericMethod( // TSource, TMiddle, TResult.
morphism1.Method.GetParameters().Single().ParameterType,
morphism1.Method.ReturnType,
morphism2.Method.ReturnType)
.Invoke(null, new object[] { morphism2, morphism1 });

public Delegate Id(Type @object) => // Functions.Id<TSource>
typeof(Functions).GetMethod(nameof(Functions.Id)).MakeGenericMethod(@object)
.CreateDelegate(typeof(Func<,>).MakeGenericType(@object, @object));

private static IEnumerable<Assembly> SelfAndReferences(
Assembly self, HashSet<Assembly> selfAndReferences = null)
{
selfAndReferences = selfAndReferences ?? new HashSet<Assembly>();
{
self.GetReferencedAssemblies().ForEach(reference =>
return selfAndReferences;
}
return Enumerable.Empty<Assembly>(); // Circular or duplicate reference.
}
}```

In DotNet category, each object is a type represented by System.Type, so Objects method queries all available types in current assembly, and also recursively query all available assemblies in all reference assemblies. And each morphism is a function from one type to another, which can be represented by System.Delegate, so the composition is just to call the o operator with 2 Delegate instances.

• Hello
I think there is a typo in:
> A composition operation of morphisms, denoted ∘. For m1: X → Y and m2: Y → Z, their composition is also a morphism (m2∘ m1): Y → Z. Here the name of m1 of m2 also implies the order. m2 ∘ m1 can be read as m2 after m1
the statement
> their composition is also a morphism (m2∘ m1): Y → Z
should be rewritten
> their composition is also a morphism (m2∘ m1): X → Z

• This is an invaluable article.
Thanks.

• This is an invaluable article.
Thanks.

• In the Functions class

public static TSource Id<TSource>(T value) => value;

T should be TSource right?

--------------------------
In the DotNetCategory

(Delegate)typeof(Linq.FuncExtensions).GetMethod(nameof(Linq.FuncExtensions.o))

Should be

(Delegate)typeof(Functions).GetMethod(nameof(Functions.o))

• It looks like .NET code snippets are not generating proper HTML anymore.

• A pedido de amigos os resultados anteriores foram incluídos nesta página.
Deu no poste.
Segunda-Feira 2020 O resultado do jogo do bicho,
deu no poste desta Domingo,
segue abaixo para apuração.
Pesquise sempre por
“jogo do bicho portalbrasil.

• Deu no Poste ? Resultados Jogo do Bicho das 11 horas, 14 horas, 16 horas, 18 horas, 21 horas.
Mostramos-lhe a tabela de resultados do jogo do bicho.

• Amazon.com/mytv - enter the 6 digit amazon mytv code you receive at screen at www.amazon.com/mytv to regiter your device. contact amazon support for hel