I am now only including the haskell code, however I have the same examples written in Python and C++. If you want copies hmu on discord.
At this point we have everything we need to define a category.
A Category C consists of
* a collection of objects A, B, C, . . .
* a collection of arrows f, g, h, . . .
* Maps A --f--> B, . . .
* Identity Maps A --I--> A
* Composites of Maps (Composition) A --g--> B --f--> C, A --f.g--> C
Note that we have previously defined and given examples of the two key mappings that a Category must consist of, Identities and Composites.
Identity maps are morphisms from an object to itself while composites are a combination of two morphisms.
All categories C have the following two properties,
(1) Identity Property :
For every morphism f : A --> B in a category C the following equation holds, f.I_A = f and I_B.f = f
Stated differently,
If A --I_A--> A --g--> B, then A --g.I_A=g--> B
If A --f--> B --I_B--> B, then A --I_b.f=f--> B
Notice that the identity map and the identity law are not the same thing, the identity law is a property of composite maps while the identity map is a kind of morphism.
Internal diagrams,
We can check this property using a program,
In the following example we have a set A = {Alice, Peach, Tryptamine, Chapo, Med, ChallengeSeeker}
The map g is an identity map g : A->A, it maps all elements in A to themselves in A.
The map g is a endomap f: A->A, it maps all elements in A to the person they enjoy sex with.
The program shows that the identity law holds for the composition.
identity.hs said:-- Define the set A
type A = String
-- Define the mappings for f, g
f :: A -> A
f "Alice" = "Peach"
f "Peach" = "Alice"
f "Tryptamine" = "ChallengeSeeker"
f "Chapo" = "ChallengeSeeker"
f "Med" = "Chapo"
f "ChallengeSeeker" = "Tryptamine"
g :: A -> A
g "Alice" = "Alice"
g "Peach" = "Peach"
g "Tryptamine" = "Tryptamine"
g "Chapo" = "Chapo"
g "Med" = "Med"
g "ChallengeSeeker" = "ChallengeSeeker"
-- Define the identity function
identity :: A -> A
identity x = x
-- Check that the identity law holds for f
testIdentityF :: Bool
testIdentityF = all (\x -> (f (identity x)) == f x && (identity (f x)) == f x) ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]
-- Check that the identity law holds for g
testIdentityG :: Bool
testIdentityG = all (\x -> (g (identity x)) == g x && (identity (g x)) == g x) ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]
main :: IO ()
main = do
if testIdentityF
then putStrLn "The identity law holds for f."
else putStrLn "The identity law does not hold for f."
if testIdentityG
then putStrLn "The identity law holds for g."
else putStrLn "The identity law does not hold for g."-- Ouput: Identity law holds for f, Identity law holds for g
Given three morphisms f, g, and h in a category C the following must hold, (f.g).h = g.(f.h)
That is the order in which a mapping happens does not matter in so far as the sequence of mappings does not change.
Stated differently,
If A --f--> B --g--> C --h--> D, then A --h.(g.f)=(h.g).f--> D
Internal diagrams,
We can check this property via a program,
In the following example we have a set A={"Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"}
The map f is an endomap f : A->A, it maps each person to the person they enjoy sex with.
The map g is an endomap g : A->A, it maps each person to their favorite person.
The map h is an endomap h : A->A, it maps each person to their worst enemy.
The program checks that the composition satisfies the law of associativity.
associativity.hs said:-- Define the set A
setA = ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]
-- Define the map f
f "Alice" = "Peach"
f "Peach" = "Alice"
f "Tryptamine" = "ChallengeSeeker"
f "Chapo" = "ChallengeSeeker"
f "Med" = "Chapo"
f "ChallengeSeeker" = "Tryptamine"
-- Define the map g
g "Alice" = "Peach"
g "Peach" = "Alice"
g "Tryptamine" = "Tryptamine"
g "Chapo" = "ChallengeSeeker"
g "Med" = "Chapo"
g "ChallengeSeeker" = "ChallengeSeeker"
-- Define the map h
h "Alice" = "Alice"
h "Peach" = "Peach"
h "Tryptamine" = "Tryptamine"
h "Chapo" = "Med"
h "Med" = "ChallengeSeeker"
h "ChallengeSeeker" = "ChallengeSeeker"
-- Define a helper function to compose two maps
compose f g x = f (g x)
-- Compose the maps and show that they are associative
main = do
-- f(g(h(x))) = (f.g.h) x
let fgh = compose (compose f g) h
-- (f.g.h) x = f(g(h x))
let fghx = fgh "Alice"
-- (f.g)(h x) = f(g(h x))
let fgh' = compose f g
let fghx' = fgh' (h "Alice")
putStrLn $ "Both results are same, Associativity holds-- Output : Both results are the same, Associativity holds