An Identity Map is a special kind of endomap in which each element is mapped to itself.
functional notation,
f(John) = John
f(Mary) = Mary
f(Sam) = Sam
Coded examples,
identity.hs said:-- Set A
type A = [String]
-- Identity map on Set A
identityMap :: A -> A
identityMap xs = xs
-- Test the identity map
input :: A
input = ["John", "Mary", "Sam"]
output :: A
output = identityMap input
main :: IO ()
main = print output
-- Output: ["John","Mary","Sam"]
identity.py said:# Set A
A = ['John', 'Mary', 'Sam']
# Identity map on Set A
def identityMap(xs):
return xs
# Test the identity map
output = identityMap(A)
print(output)
# Output: ['John', 'Mary', 'Sam']
In Category Theory we often don't care about the details of any given object, as such we do not concern ourselves with Internal Diagrams but instead External Diagrams which emphasize the mappings between objects instead of the mappings between members of objects.
A --f--> B
A --g--> A
A --I--> A
External diagrams are more important because the number of objects and mappings we typically deal with are more than a few, as such keeping track of the internal mappings is complicated and more importantly not worth the effort given it doesn't lead to better results.
Composition of Maps :: two maps are combined to obtain a third map
Ex. Given the previous sets A and B we can imagine a situation in which each person of A wants to serve their favorite person in their favorite breakfast in B.
As such there will be two mappings, mapping g that represents the rule that maps members of A to their favorite members of A, and mapping f that maps members of A to their favorite breakfast.
Internal Diagram,
External Diagram,
A --g--> A --f--> B
functional notation,
f(g(John)) = f(Mary) = coffee
f(g(Mary)) = f(John) = eggs
f(g(Sam)) = f(Mary) = coffee
Via the functional notation we see that a mapping f is equivalent to a mapping of f following g, which will be denoted at f.g.
As such, given the problem of this example we could solve the problem with this single mapping f.g which takes us straight from the set A to set B while satisfying our goal described in the problem.
Internal Diagram,
External Diagram,
A --f.g--> B
Coded examples,
composition.hs said:-- Function f maps members of set A to members of set B
f :: String -> String
f "John" = "eggs"
f "Mary" = "coffee"
f "Sam" = "coffee"
-- Function g maps members of set A to themselves
g :: String -> String
g "John" = "Mary"
g "Mary" = "John"
g "Sam" = "Mary"
-- Composition function
compose :: (a -> a) -> (a -> b) -> a -> b
compose g f x = f (g x)
h = compose g f
main = do
-- Print the composition of g and f applied to "John"
putStrLn (h "John")
-- Print the composition of g and f applied to "Mary"
putStrLn (h "Mary")
-- Print the composition of g and f applied to "Sam"
putStrLn (h "Sam")
composition.py said:# Set A
A = ['John', 'Mary', 'Sam']
# Endomorphism on Set A
def g(x):
if x == 'John':
return 'Mary'
elif x == 'Mary':
return 'John'
else:
return 'Mary'
# Morphism from Set A to Set B
def f(x):
if x == 'John':
return 'eggs'
elif x == 'Mary':
return 'coffee'
else:
return 'coffee'
# Composition f(g(A))
composition = [f(g(x)) for x in A]
print(composition)
# Output: ['coffee', 'eggs', 'coffee']
In general, if we have two maps f and g, where the domain of f is the same as the codomain of g, then we can form a Composition.
X --g--> Y --f--> Z == X --f.g--> Z