A common concept you see often in multiple instances.
Numbers: 1,2,3,... 3400 BC, real numbers 760 BC
& More things can be understood as numbers
& More operator to manipulate them.
Numbers | Set Theory (∞)/Abstract Algebra/Topology |
---|---|
\(\mathbb{N}\): \((+,0)\) | Semigroups |
\(\mathbb{Z}\): \((+,0,\times,1)\) | Rings |
\(\mathbb{Q}\) | Fields |
\(\mathbb{R}\) | Complete Fields (topology) |
\(\mathbb{C}\) | Algebræ |
Modules,Vector Spaces, Monoids, ... |
& More general: more things are sets.
& More precise: clear distinction between concepts.
Numbers | Sets | Categories |
---|---|---|
\(\mathbb{N}\): \((+,0)\) | Semigroups | ? |
\(\mathbb{Z}\): \((+,0,\times,1)\) | Rings | ? |
\(\mathbb{Q}\) | Fields | ? |
\(\mathbb{R}\) | Complete Fields (topology) | ? |
\(\mathbb{C}\) | Algebræ | ? |
Modules,Vector Spaces, Monoids, ... | ? |
Gate between different scientific fields
& More general: more things are Categories.
& More precise: better distinction between concepts.
Young field: 1942–45, Samuel Eilenberg & Saunders Mac Lane
Natural Abstractions: pointers, variables, loop, Objects, Classes...
Representation: a data structure changing other time.
Natural abstraction: higher level functions & equations
4 + ["foo",27]
forbidden
["foo",27]
forbidden
data Maybe a = Just a | Nothing
[Just 32,Nothing,Just 12] :: [Maybe Integer]
Natural abstraction: Polymorphic higher level functions.
mappend
(<>) = `mappend`
"abc" <> "def" = "abcdef"
("abc","xyz") <> ("ABC","XYZ") = ("abcABC","xyzXYZ")
3 <> 4 = ERROR which law? + or *
-- Monoid (N,+)
type Sum = Sum {getSum :: a}
(<>+) = getSum (Sum x <> Sum y)
3 <>+ 4 = 7
-- Monoid (N,*)
type Product = Product {getProduct :: a}
(<>*) = getProduct (Product x <> Product y)
3 <>* 4 = 12
(>>=)
Example: (>>=)
with [a]
and Maybe a
data Maybe a = Just a | Nothing
-- Maybe : Maybe Int >>= Int -> Maybe (Int -> Int) >>= (Int -> Int) -> Maybe Int
(Just 2) >>= \x -> (Just (\z->z*z)) >>= \f -> Just (f x) = Just 4
Nothing >>= \x -> (Just (\z->z*z)) >>= \f -> Just (f x) = Nothing
-- Lists: [a] : [Int] >>= Int -> [Int -> Int] >>= (Int -> Int) -> [Int]
[1,2] >>= \x -> [(+10),(+20)] >>= \f -> [f x] = [11,21,12,22]
[] >>= \x -> [(+10),(+20)] >>= \f -> [f x] = []
Impure | Choose the data structure, find an algorithm. |
Untyped Pure | Choose the data structure, find an equation. |
Typed Pure | Choose the Types and their laws, find the right operator |
A way of representing things and ways to go between things.
A Category \(\mathcal{C}\) is defined by:
\(\ob{\mathcal{C}}\) is a collection
\(\hom{A,B}\) is a collection
Composition (∘): \(f:A→B, g:B→C\) $$g∘f:A\rightarrow C$$
for all \(X\), there is an \(\id_X\), s.t. for all \(f:A→B\):
Composition is associative:
Two path with the same source and destination are equal.
(++)
"" ++ u = u = u ++ ""
(u ++ v) ++ w = u ++ (v ++ w)
each Monoid \((M,e,◎): \ob{M}=\{∙\},\hom{M}=M,\circ = ◎\)
one object
Examples: (Integer,0,+)
, (Integer,1,*)
, (Strings,"",++)
, (Lists,[],++)
, ...
each preorder \((P,≤): \ob{P}={P},\hom{x,y}=\{{x≤y}\} ⇔ x≤y,f_{y,z} \circ f_{x,y} = f_{x,z} \)
At most one morphism between two objects.Any set \(E: \ob{E}=E, \hom{x,y}=\{x\} ⇔ x=y \)
Only identities ; not so interesting
A functor is a mapping between two categories. Let \(\C\) and \(\D\) be two categories. A functor \(\F\) from \(\C\) to \(\D\):
A endofunctor for \(\C\) is a functor \(F:\C→\C\).
Categories and functors form a category: \(\Cat\)
Category \(\Hask\):
(.)
Haskell function composition
Forget glitches because of undefined
.
Functor for Haskell language is a type F
which belong to the type class Functor
It must therefore implement an fmap
function.
The couple (F,fmap)
is then a real functor in the categorical sense for \(\Hask\) if
for any x :: F a
:
fmap id x = x
fmap (f.g) x= (fmap f . fmap g) x
data Maybe a = Just a | Nothing
instance Functor Maybe where
fmap :: (a -> b) -> (Maybe a -> Maybe b)
fmap f (Just a) = f a
fmap f Nothing = Nothing
fmap (+1) (Just 1) == 2
fmap (+1) Nothing == Nothing
fmap head (Just [1,2,3]) == Just 1
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]
data F a = Cons Char a | Nil
-- examples :
-- Cons 'c' 32 :: F Int
-- Cons 'c' (\x -> x*x) :: F (Int -> Int)
-- Cons 'c' (Cons 'a' (\x -> x*x)) :: F (F (Int -> Int))
-- Cons 'c' (Cons 'c' Nil) :: F (F (F))
-- note String is the fixed point of F: F(F(F(...)))
instance Functor F where
fmap :: (a -> b) -> [a] -> [b]
fmap f (Cons c x) = Cons c (f x)
fmap f Nil = Nil
fmap (+1) (Cons 'c' 3) == Cons 'c' 4
fmap (+1) Nil == Nil
fmap head (Cons 'c' [1,2,3])== Cons 'c' 1
Haskell functor can be seen as boxes containing all Haskell types and functions. Haskell types is fractal:
Haskell Functors are:
/
#