for each a
, ([a],[],++)
+(Integer,0,+)
, (Integer,1,*)
,
+(Strings,"",++)
, for each a
, ([a],[],++)
-Degenerated Categories: Preorders
-
-each preorder \((P,≤)\):
+Degenerated Categories: Preorders \((P,≤)\)
- \(\ob{P}={P}\),
- \(\hom{x,y}=\{x≤y\} ⇔ x≤y\),
- \((y≤z) \circ (x≤y) = (x≤z) \)
-At most one morphism between two objects.
+At most one morphism between two objects.
@@ -403,14 +399,11 @@ such that for each \(f:A→B\):
Only identities
-Categorical Property
+Categorical Properties
Any property which can be expressed in term of category, objects, morphism and composition.
-- isomorphism: \(f:A→B\) which can be "undone" i.e.
-
\(∃g:B→A\), \(g∘f=id_A\) & \(f∘g=id_B\)
-
in this case, \(A\) & \(B\) are isomorphic.
- - Dual: \(\D\) is \(\C\) with reversed morphisms.
+
- Dual: \(\D\) is \(\C\) with reversed morphisms.
- Initial: \(Z\in\ob{\C}\) s.t. \(∀Y∈\ob{\C}, \#\hom{Z,Y}=1\)
Unique ("up to isormophism")
- Terminal: \(T\in\ob{\C}\) s.t. \(T\) is initial in the dual of \(\C\)
@@ -419,6 +412,11 @@ such that for each \(f:A→B\):
+Isomorph
+isomorphism: (f:A→B) which can be "undone" i.e.
\(∃g:B→A\), \(g∘f=id_A\) & \(f∘g=id_B\)
in this case, \(A\) & \(B\) are isomorphic.
+In Category Theory, = is generally ≌. For example in commutative diagrams.
+
+
Functor
A functor is a mapping between two categories.
@@ -438,17 +436,17 @@ A functor \(\F\) from \(
Functor Example (ob → ob)
-
+
Functor Example (hom → hom)
-
+
Functor Example
-
+
Endofunctors
@@ -500,18 +498,14 @@ A functor \(\F\) from \(
Haskell Kinds
In Haskell some types can take type variable(s). Typically: [a]
.
-data Tree a = Node a [Tree a]
-data CTree a b = CNode a [b]
-Types have kind; The kind is to type what type is to function. Kind are the types for types (so meta).
+Types have kinds; The kind is to type what type is to function. Kind are the types for types (so meta).
Int, Char :: *
-[], Maybe, Tree :: * -> *
-CTree :: * -> * -> *
-[Int], Maybe Char, Tree [Int] :: *
-CTree [Int] :: * -> *
+[], Maybe :: * -> *
+(,) :: * -> * -> *
+[Int], Maybe Char, Maybe [Int] :: *
Haskell Types
-We can make function that can work for all type parameter. Such function can only work with the topology induced by the type. We know such function won't work on the elements.
Sometimes, the type determine a lot about the function★:
fst :: (a,b) -> a -- Only one choice
snd :: (a,b) -> b -- Only one choice
@@ -530,12 +524,12 @@ addOne l = map (+1) l
Haskell Functor vs \(\Hask\) Functor
-Functor for Haskell language is a type F :: * -> *
which belong to the type class Functor
.
-It must implement fmap :: (a -> b) -> (F a -> F b)
.
+
A Haskell Functor is a type F :: * -> *
which belong to the type class Functor
; thus instantiate
+fmap :: (a -> b) -> (F a -> F b)
.
& F
: \(\ob{\Hask}→\ob{\Hask}\)
& fmap
: \(\hom{\Hask}→\hom{\Hask}\)
-
The couple (F,fmap)
is a functor in the categorical sense for \(\Hask\) if for any x :: F a
:
+The couple (F,fmap)
is a \(\Hask\) endofunctor if for any x :: F a
:
fmap id x = x
fmap (f.g) x= (fmap f . fmap g) x
@@ -556,22 +550,20 @@ fmap head (Just [1,2,3]) == Just 1
Haskell Functors Example: List
-
-instance Functor ([]) where
+instance Functor ([]) where
fmap :: (a -> b) -> [a] -> [b]
fmap = map
fmap (+1) [1,2,3] == [2,3,4]
fmap (+1) [] == []
-fmap head [[1,2,3],[4,5,6]] == [1,4]
-
+fmap head [[1,2,3],[4,5,6]] == [1,4]
Haskell Functors for the programmer
Functor
is a type class used for types that can be mapped over.
- Containers:
[]
, Trees, Map, HashMap...
-- Smart containers:
+
- "Feature Type":
Maybe a
: help to handle absence of a
.
Ex: safeDiv x 0 ⇒ Nothing
Either String a
: help to handle errors
Ex: reportDiv x 0 ⇒ Left "Division by 0!"
@@ -633,7 +625,7 @@ Haskell types is fractal:
Also Functor inside \(\Hask\)
-\(\mathtt{[a]}∈\ob{\Hask}\) but is also a complete category. The same can be said for Int
.
+\(\mathtt{[a]}∈\ob{\Hask}\) but is also a category. Idem for Int
.
length
is a Functor from the category [a]
to the cateogry Int
:
- \(\ob{\mathtt{[a]}}=\{∙\}\)
@@ -653,26 +645,27 @@ Haskell types is fractal:
Category of \(\Hask\) Endofunctors
-
+
Category of Functors
If \(\C\) is small (\(\hom{\C}\) is a set). All functors from \(\C\) to some category \(\D\) form the category \(\mathrm{Func}(\C,\D)\).
-
- \(\ob{\mathrm{Func}(\C,\D)}\): Functors \(F:\C→\D\)
-- \(\hom{\mathrm{Func}(\C,\D)}\): natural transformations
-
-- η familly \(η_X\in\hom{\D}\) for \(X\in\ob{\C}\) s.t.
-- ex: between Haskel functors;
F a -> G a
Rearragement functions only.
-
+- \(\hom{\mathrm{Func}(\C,\D)}\): natural transformations
- ∘: Functor composition
\(\mathrm{Func}(\C,\C)\) is the category of endofunctors of \(\C\).
+
+Let \(F\) and \(G\) be two functors from \(\C\) to \(\D\).
+ A natural transformation: familly η ; \(η_X\in\hom{\D}\) for \(X\in\ob{\C}\) s.t.
+ex: between Haskell functors; F a -> G a
Rearragement functions only.
+
+
-data Tree a = Empty | Node a [Tree a]
+data Tree a = Empty | Node a [Tree a]
toTree :: [a] -> Tree a
toTree [] = Empty
toTree (x:xs) = Node x [toTree xs]
@@ -687,7 +680,7 @@ toTree (x:xs) = Node x [toTree xs]
-data Tree a = Empty | Node a [Tree a]
+data Tree a = Empty | Node a [Tree a]
toList :: Tree a -> [a]
toList Empty = []
toList (Node x l) = [x] ++ concat (map toList l)
@@ -702,7 +695,7 @@ toList (Node x l) = [x] ++ concat (map toList l)
-toMaybe :: [a] -> Maybe a
+toMaybe :: [a] -> Maybe a
toMaybe [] = Nothing
toMaybe (x:xs) = Just x
@@ -716,7 +709,7 @@ toMaybe (x:xs) = Just x
-mToList :: Maybe a -> [a]
+mToList :: Maybe a -> [a]
mToList Nothing = []
mToList Just x = [x]
@@ -997,7 +990,7 @@ depth = cata phi where
-
+
-
+