Message Turncoat in a DM to get moderator attention

Users Online(? lurkers):
Posts: 2266
0 votes RE: Wanna study Category Theory?

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,

Posted Image

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
 (2) Associative Property :
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,

Posted Image

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
 
last edit on 1/10/2023 7:18:57 PM
Posts: 2266
1 votes RE: Wanna study Category Theory?

Fundamentally a category C is just a composition of mappings where the Identity and Associative properties hold. As such, Category Theory is just a language to talk about compositions with said properties. We can ask how powerful such a language is which comes down to how many things can be described as compositions. From a model-theoretic perspective CT is considered complete, which is to say all of mathematics can be expressed in terms of Categories, or rather, compositions.

We now can check to see if the example for the associative law forms a valid category,

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 forms a category and that the laws of identity and associativity hold.  

category.hs said:
import qualified Data.Set as Set

-- Define the set A
type A = String

-- Define the set of elements in A
elements :: Set.Set A
elements = Set.fromList ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]

-- Define the maps f, g, and h
f, g, h :: A -> A
f "Alice" = "Peach"
f "Peach" = "Alice"
f "Tryptamine" = "ChallengeSeeker"
f "Chapo" = "ChallengeSeeker"
f "Med" = "Chapo"
f "ChallengeSeeker" = "Tryptamine"


g "Alice" = "Peach"
g "Peach" = "Alice"
g "Tryptamine" = "Tryptamine"
g "Chapo" = "ChallengeSeeker"
g "Med" = "Chapo"
g "ChallengeSeeker" = "ChallengeSeeker"

h "Alice" = "Alice"
h "Peach" = "Peach"
h "Tryptamine" = "Tryptamine"
h "Chapo" = "Med"
h "Med" = "ChallengeSeeker"
h "ChallengeSeeker" = "ChallengeSeeker"

-- Define a function to compose maps
(.>) :: (a -> b) -> (b -> c) -> (a -> c)
(f .> g) x = g (f x)

-- Check that the maps are closed under composition
eq3 :: (A -> A) -> (A -> A) -> Bool
eq3 f g = all (\x -> (x `Set.member` elements) && (f x `Set.member` elements) && (g (f x) `Set.member` elements)) elements

testClosed :: (A -> A) -> (A -> A) -> Bool
testClosed f g = eq3 f g

-- Check that the associativity law holds
eq2 :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
eq2 f g h = all (\x -> (f .> (g .> h)) x == ((f .> g) .> h) x) elements

testAssociativityLaw :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
testAssociativityLaw f g h = eq2 f g h

-- Define the identity map
identity :: A -> A
identity x = x

-- Check that the identity law holds
eq :: (A -> A) -> (A -> A) -> Bool
eq f g = all (\x -> f x == g x) elements

testIdentityLaw :: (A -> A) -> Bool
testIdentityLaw f = eq (f .> identity) f && eq (identity .> f) f

-- Check if our category laws hold
isCategory = all (\x -> testClosed f g) elements &&
    all (\x -> testClosed f h) elements &&
    all (\x -> testClosed g h) elements &&
    all (\x -> testAssociativityLaw f g h) elements &&
    testIdentityLaw f && testIdentityLaw g && testIdentityLaw h

testCategory :: IO ()
testCategory = do
    if isCategory
        then putStrLn "Forms a valid category."
        else putStrLn "Does not form a valid category."

main :: IO ()
main = testCategory

-- Output : Forms a valid category.
Posts: 2479
1 votes RE: Wanna study Category Theory?

h "Alice" = "Alice"
h "Peach" = "Peach"
h "Tryptamine" = "Tryptamine"
h "Chapo" = "Med"
h "Med" = "ChallengeSeeker"
h "ChallengeSeeker" = "ChallengeSeeker

I picked up your mnemonic right away

while cute if it helps you remember, I am NOT Chapo’s worst enemy.

This kind of belies some lack of creativity or something 🤷🏻‍♀️because Alice, Peach, Tryptamine, Challengeseeker have themselves as worst enemy. 

last edit on 1/10/2023 7:56:47 PM
Posts: 4519
0 votes RE: Wanna study Category Theory?

Heh, it's as though their identity is their own worst enemy?

Thrall to the Wire of Self-Excited Circuit.
Posts: 2266
0 votes RE: Wanna study Category Theory?

Heh, it's as though their identity is their own worst enemy?

That's the idea. 

Posts: 2759
0 votes RE: Wanna study Category Theory?

Heh, it's as though their identity is their own worst enemy?

That's the idea. 

 Who is my worst enemy in your opinion?

🌺🐀 🌺
Posts: 2266
0 votes RE: Wanna study Category Theory?
Delora said: 

Heh, it's as though their identity is their own worst enemy?

That's the idea. 

 Who is my worst enemy in your opinion?

 Situations in which you can't find a can opener but need one. 

Posts: 2266
0 votes RE: Wanna study Category Theory?

Here I will provide intuition around the code, but given category.hs includes tests for associativity and identity we can ignore the other two programs.   

import qualified Data.Set as Set

-- Define the set A
type A = String

-- Define the set of elements in A
elements :: Set.Set A
elements = Set.fromList ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]

-- Define the maps f, g, and h
f, g, h :: A -> A
f "Alice" = "Peach"
f "Peach" = "Alice"
f "Tryptamine" = "ChallengeSeeker"
f "Chapo" = "ChallengeSeeker"
f "Med" = "Chapo"
f "ChallengeSeeker" = "Tryptamine"


g "Alice" = "Peach"
g "Peach" = "Alice"
g "Tryptamine" = "Tryptamine"
g "Chapo" = "ChallengeSeeker"
g "Med" = "Chapo"
g "ChallengeSeeker" = "ChallengeSeeker"

h "Alice" = "Alice"
h "Peach" = "Peach"
h "Tryptamine" = "Tryptamine"
h "Chapo" = "Med"
h "Med" = "ChallengeSeeker"
h "ChallengeSeeker" = "ChallengeSeeker"

-- Define a function to compose maps
(.>) :: (a -> b) -> (b -> c) -> (a -> c)
(f .> g) x = g (f x)

-- Check that the maps are closed under composition
eq3 :: (A -> A) -> (A -> A) -> Bool
eq3 f g = all (\x -> (x `Set.member` elements) && (f x `Set.member` elements) && (g (f x) `Set.member` elements)) elements

testClosed :: (A -> A) -> (A -> A) -> Bool
testClosed f g = eq3 f g

-- Check that the associativity law holds
eq2 :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
eq2 f g h = all (\x -> (f .> (g .> h)) x == ((f .> g) .> h) x) elements

testAssociativityLaw :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
testAssociativityLaw f g h = eq2 f g h

-- Define the identity map
identity :: A -> A
identity x = x

-- Check that the identity law holds
eq :: (A -> A) -> (A -> A) -> Bool
eq f g = all (\x -> f x == g x) elements

testIdentityLaw :: (A -> A) -> Bool
testIdentityLaw f = eq (f .> identity) f && eq (identity .> f) f

-- Check if our category laws hold
isCategory = all (\x -> testClosed f g) elements &&
    all (\x -> testClosed f h) elements &&
    all (\x -> testClosed g h) elements &&
    all (\x -> testAssociativityLaw f g h) elements &&
    testIdentityLaw f && testIdentityLaw g && testIdentityLaw h

testCategory :: IO ()
testCategory = do
    if isCategory
        then putStrLn "Forms a valid category."
        else putStrLn "Does not form a valid category."

main :: IO ()
main = testCategory

-- Output : Forms a valid category.

 

-- Define the set A
type A = String

 This defines a new type called A as a string. 

elements :: Set.Set A

Here we declare a variable named 'elements' as a set containing strings. 

Set.Set is a data structure representing sets that we are calling from the module import qualified Data.Set as Set

Set.Set A means that this Set called elements contains members of type A which were defined as strings.  

elements = Set.fromList ["Alice", "Peach", "Tryptamine", "Chapo", "Med", "ChallengeSeeker"]

We assign the strings Alice, Peach, Tryptamine, Chapo, Med, and ChallengeSeeker to the variable elements. 

This assignment is carried out be the fromList function which too is located in the Data.Set module. 

fromList is a function that creates sets from a designated list of members. 

f, g, h :: A -> A

Defines the functions f, g, and h as endomaps. 

Each function takes a single argument of type A, that is a string, and returns a value of type A, a string. 

I will not be going over the description of rules for each of these functions given I went over them in the previous coding examples. Just recall what each function is to represent and that the rules themselves are established via the method of pattern matching.  

(.>) :: (a -> b) -> (b -> c) -> (a -> c)

Here we define a function named (.>) that represents the compose of f, g, and h.  

The compose is defined to take in two functions (a->b) and (b->c) and returns a function (a->c). 

The first function takes an argument a and returns an argument b, the second function takes an argument b and returns an argument c. Finally the function (a->c) takes an argument a and returns an argument c. 

The function (a->c) is the composition of the functions (a->b) and (b->c), hence given these functions we (.>) returns the compose (a->c).  

(f .> g) x = g (f x)

Applies the function f to the argument x and then applies the function  g to the result of f x. 

eq3 :: (A -> A) -> (A -> A) -> Bool

Defines a function eq3 that takes in two functions (A->A) and (A->A) and returns a type Bool value. 

eq3 f g = all (\x -> (x `Set.member` elements) && (f x `Set.member` elements) && (g (f x) `Set.member` elements)) elements

Instantiates the rule for eq3 which checks to see if the value of X is in the set elements, f x is in the set elements, and g f x is in the set of elements. If this predicate is true, then eq3 is set to true, if false it is set to false. Lets break down the code. 

eq3 f g, the function eq3 takes in the arguments f and g which are functions (A->A) and (A->A). 

This equals an all() function, which takes in a predicate function and a set of arguments. 

The predicate that all() takes in is the lambda function, defined via \x -> x `Set.member` elements) && (f x `Set.member` elements) && (g (f x) `Set.member` elements).

For this to return true that argument x must be true in all cases, that is x must be in the set, f x must be in the set, and the compose g(f(x)) must be in the set. All come from the set elements. 

Posts: 2266
0 votes RE: Wanna study Category Theory?

 

testClosed :: (A -> A) -> (A -> A) -> Bool
testClosed f g = eq3 f g

Just as before we define a function called testClosed that takes in two functions (A->A) and (A->A) as arguments and returns a Bool type. 

testClosed checks if the composition of the functions f g preserves the membership of the elements by calling eq3 with the arguments f g. 

Here we are seeing if composition is closed, which means that for any two mappings in a category, the composition of those mappings is also a mapping in the category.  

eq2 :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
eq2 f g h = all (\x -> (f .> (g .> h)) x == ((f .> g) .> h) x) elements

First we define eq2 as a function that takes in three endomaps as arguments and returns a bool type. 

The rule of eq2 takes in the functions f g h and checks a predicate, if the predicate is true it is set to true if false it is set to false. 

The predicate is \x -> (f .> (g .> h)) x == ((f .> g) .> h) is a lambda function that checks if the composition f (g h) is equivalent to the composition (f g) h. 

The all() function checks that all x in elements satisfy the predicate. 

testAssociativityLaw :: (A -> A) -> (A -> A) -> (A -> A) -> Bool
testAssociativityLaw f g h = eq2 f g h

Just like with the test of closure we define a function for associativity that takes in three arguments and returns a bool. 

testAssociativity checks whether the functions is associative be calling eq2 with the parameters f g h. 

identity :: A -> A
identity x = x

 We define identity the same way we have before.  

eq :: (A -> A) -> (A -> A) -> Bool
eq f g = all (\x -> f x == g x) elements

Defines a function eq that takes in the functions (A->A) and (A->A) as arguments and returns a bool type. 

The all() function checks the predicate  \x -> f x == g x for all x in elements. The predicate determines if f x and g x is true pointwise (contains the same elements). If this is not true are composition can be be invalid in certain cases. 

testIdentityLaw :: (A -> A) -> Bool
testIdentityLaw f = eq (f .> identity) f && eq (identity .> f) f

You should get the idea of defining functions at this point so I will stop explaining that going forward. 

The testIdentity function tests a predicate between two eq() functions which we just defined. For this to return true f.I_f and I_f.f both need to be true.  

isCategory = all (\x -> testClosed f g) elements &&
all (\x -> testClosed f h) elements &&
all (\x -> testClosed g h) elements &&
all (\x -> testAssociativityLaw f g h) elements &&
testIdentityLaw f && testIdentityLaw g && testIdentityLaw h

 Finally we check if a set of functions form a category. 

This is done by testing multiple predicates that take in each of our individual tests for composition, identity, and associativity and determining if they are all true. If they are all true then the variable isCategory is given a bool type True. 

isCategory is implicitly a bool type given its value is determined by a predicate. 

These predicates are defined in the same way are other predicates were defined, lambda functions applied over all x in elements. 

The rest of the code runs the tests and returns a statement telling you if the set of functions forms a category. 

Posts: 195
0 votes RE: Wanna study Category Theory?

How is this used to solve real problems?

This site contains NSFW material. To view and use this site, you must be 18+ years of age.