Pure functions is the base of everything in programming
functions Math special kinds of relations (Sets with elements that pairs)
Set of pairs forms the cartesian product
Function Domain is the Set in equivalence The set that is connected is the codomain this also have the image
The direction is defined by possibility to exist a inverse function
- f :: a -> b
- g :: b -> a
g . f = id
f . g = id
The point between the functions are "after"
This is isomorphism
Extracting Modeling
Injective functions dont colapse as it happen in "Extracting" (Monic)
Surjective function colapse like the "Modeling" example (Isomorphism too btw) (Epic)
F :: Void -> a id :: Void -> Void
This function is named absurd Void by logic corresponds to False
( ) :: ( ) -> True unit :: a -> ( )
one :: ( ) -> Int two :: ( ) -> Int ...
Home-set: The set or collection of all morphisms from A to B for some given ordered pair (A, B) of objects from some given category
The Monoidal Category is a Category in which you can “multiply” objects using some kind of a tensor product (⊗) and which also has a special object I that is the unit of multiplication
Binaries operation defines as a base every element in the monoid Like a multiplication of natural numbers String Contatenation
A monoid can be compared to a lightly typed language
Definition: In category theory, a Kleisli category is a category naturally associated to any monad T. It is equivalent to the category of free T-algebras. The Kleisli category is one of two extremal solutions to the question Does every monad arise from an adjunction? The other extremal solution is the Eilenberg–Moore category.
Functions return ( < x >) proving in java/c++
Functional programming we don't really stops in composing functions We compose bunch of operations using function composion, but also use differents compositions making the code freedom more vast
Important definition: _Initial and terminal objects are not required to exist in a given category. However, if they do exist, they are essentially unique. Specifically, if I1 and I2 are two different initial objects, then there is a unique isomorphism between them. Moreover, if I is an initial object then any object isomorphic to I is also an initial object. The same is true for terminal objects.
Arrows in Kleisli are differente from de other category types
(m = maps type to type)
Implementation of Kleisli arrow in C category |
In general the composition are differente even if is the "same" arrow just in different domains
The Id function are also differente:
Remembering Monad is : In category theory, a branch of mathematics, a monad (also triple, triad, standard construction and fundamental construction) is a monoid in the category of endofunctors. An endofunctor is a functor mapping a category to itself, and a monad is an endofunctor together with two natural transformations required to fulfill certain coherence conditions.
definition: In mathematics, a singleton, also known as a unit set or one-point set, is a set with exactly one element. For example, the set { 0 } is a singleton whose single element is 0.
- In Category theory the singleton is a terminal object in the category Sets of sets, and no other set are terminal
- Any singleton admits a unique topological space structure (both subsets are open). These singleton topological spaces are terminal objects in the category of topological spaces and continuous functions. No other spaces are terminal in that category.
- Any singleton admits a unique group structure (the unique element serving as identity element). These singleton groups are zero objects in the category of groups and group homomorphisms. No other groups are terminal in that category.
Event Bool as set can receive 2 arrows ( True and False) but singleton just receive one.
- Furst math representation - For all object A there exists a F thats goes from A to the terminologic
Definition: In category theory, a branch of mathematics, an initial object of a category C is an object I in C such that for every object X in C, there exists precisely one morphism I → X.
Easier way to remember: Categorically initial object is as object that has an unique outgoing arrow to every other object
Disclaimer: Until now we just compare arrows to other arrows but don't compare objects to other objects
For every category there is another category that have the same objects with inverse arrow
Partition products:
Real projection definition:
It becomes exponencially complex when we think about multi-arrows, because the arrows start to point at a multidimensional image, in this case a cube pointing to one point:
In categorical products :
it's important to point that the m ° p = q1 m ° p = p1
as also told before The C and C1 are set of rules and the C of "p" and "q" are called the product of A and B
Important: Not all category have products, but in a category of sets, all sets have products, including initial objects, terminal objects, etc
Besides the product that makes projections, now what happens is injections Coproduct ( C ) is a object with a pair of injections such as any other object that has a injection from A to C' and B to C' has a unique morphism M from C to C'
C also is a perfect match from A and B images exactly like a Union and the M finds the exactly point of C' that also is part of the system.
The canonical way to describe a pair in haskell is ( a, b )
In the "Projection" that's the inverse system, the equivalent of I and J of the last image is called "Fst" and "Snd" while in the "Injection" is Left A and Right B
X :: Either Int Bool
F :: Either Int Bool -> Bool
F(Left I) = i > 0
F(Right b) = b
In other languages it's just < a, b > (Pairs)
The haskell representation for a pair of Projection
data Point = P Float Float
P { x :: Float
y :: Float
}
( a, b ) ( b, a )
swap :: ( a, b ) -> ( b, a )
swap p = ( snd p, fst p )
(( a , b ), c) ~ ( a, ( b , c))
assoc ((a, b ), c) = (a, ( b, c))
( a, () ) ~ a => a * 1 = a
mUnit ( x, () ) = x or mUnit = fst
mUnit_inv x = ( x, ())
- These are isomorph examples
Either a b ~ Either b a
( | in haskell are guardian that's basically a multi option like if else chain) data triple a b c = Left a | Right c | Middle b
Either a Void ~ a a + 0 = a
a * 0 = 0 ( a , Void ) ~ Void
a * ( b + c) = a * b + a * c ( a, Either b c ) ~ Either ( a , b ) ( a , c )
It is like a Ring, but mathmatically Ring has an inverse, what isn't the case now because we don't know the inverse of a Integer so we call it Rig.
2 = 1 + 1 We can think that 2 is a boolean and the 1 called the True or False
1 + a data Maybe a = Nothing | Just a Either ( ) a
data List a = Nil | Cons a ( List a )
Infinit list definition
Definition: A Functor is kind of mapping of objects and morphisms that preserves composition and identity. We have two Categories: A and B . In Category A we have two objects a and b with morphism f . Our Functor is a mapping of objects a and b to Fa and Fb and mapping of morphisms, in this case single morphism: f to Ff
Pattern matching definition is basically the recognition of patterns between categorys
Functors must preservate the composition and identity: Injective functors on home-sets are called Faithfull
In Haskell objects becomes types and morphisms become functions and the type constructor is the mapping of types and also functions
There is the object mapping too that is the mathematical way to the Morphism in haskell
data Maybe a = Nothin | Just a
This is Morphism to and the f in haskell are the => fmap f so:
Some haskell typing functor and examples:
The last image is the definition of the actual map function in haskell, it's a recursive function that verifies a list of T in the case, btw the Cons is a historical name for constructors
This is kinda funny too because we find the Reader function is a ( . ) actually in a logical conclusion by removing repeated f g in the Fmap.
Definition: The Reader monad is useful when you have a long chain of function invocations where only some of the functions in the chain make direct use of a piece of “shared environmental state”. This “environmental state” could be anything. It could be as simple as a primitive value or it could be a record containing configuration values your program needs in certain places. Doesn’t matter.
- Vector is a container of elements
Function can be seem as containers
- In Haskell all data types are thunks, there are functions that can be evaluated to return a value
- Data arte really functions and functions are really data
- Applying a function to a value dont obligate him to be evaluated
- Sometimes the functions will only be applyied when the value appears, that doesnt mean that it's gonna be in the beggining
Definition of thunks: _A thunk is a value that is yet to be evaluated. It is used in Haskell systems that implement non-strict semantics by lazy evaluation.
Functoriality
This representation is interesting because usually we don't think in the tail representation as a risk, but if naturally the function was [a] -> Mayba [a] there wouldn't be any problem related to crash the program in execution, because would raise an exception for empty lists
Representation of a bifunctor:
Is really game changer to start thinking that you can think of a set merge, in this case the CxD that generates a new conception in functors constructs
Applying to haskell, it's possible to project a class Bifunctor where we get a bimap that accept two functions as represented:
F is the bifunctor that gets the A and B, as a compiler it means that the F is getting two types as arguments (btw type constructor of two types)
- Basically taking two types that makes a third its a bifunctor
Easy way to go from AxB -> A' x B' And the logical representation of this is in the image
By logic if we apply the equivalent for a comparison of A and unit() it becames even easier
Bifunctor with bifunctors inside
Practical situation would be something in this scheme: {-# LANGUAGE DeriveFunctor #-} Data Maybe = ..... deriving Functor
It's like the bifuntor but the category that compares C x C in this case have one of these C inverted to get the inverse value doing the basic a -> b Sequence of the last image:
Function creation in haskell and curry:
- It's possible to a type be exponential, if we think bool -> int , it means that technically there are 2 int's as value possibility.
![[Pasted image 20230124201220.png]] Haskell equivalent:
alpha :: forall a. F a -> G a
A category that have 2 categories inside and in this case are functors categories, that also get vertical and horizontal escalability:
- [ C , D] = D^c => functor
There are vertical compositions too
Kinda like a vertical escalation, this also get's exponencial possibilities
definition: A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
Changing symbols to the next annotation prints
Proving that Identity is the same data as T
Re-watch natural transformation and monads to understand better before going to the next chapter...