What Makes Functional and Object-oriented Programming Equal

http://codinghelmet.com/articles/what-makes-functional-and-object-oriented-programming-equal

Comparison of 4 Functional Programming (FP : F#, Lisp/Clojure Haskell, Scala,… ) concepts in Object-Oriented (OO: C#, C++, Java,… ) :

[Not covered here] : There are other FP techniques lacking in OO: Functor (FoldMap), Monad, etc.

1. Function:

2. Closure (variable binding)

3. Currying

4. Function Composition

Conclusion:

A Programmer’s Regret: Neglecting Math at University – Adenoid Adventures

Advanced Programming needs Advanced Math: eg.

Video Game Animation: Verlet Integration

AI: Stats, Probability, Calculus, Linear Algebra

Search Engine : PageRank: Linear Algebra

Abstraction in Program “Polymorphism” : Monoid, Category, Functor, Monad

Program “Proof” : Propositions as Types, HoTT

https://awalterschulze.github.io/blog/post/neglecting-math-at-university/

Abstraction: Monoid, Category

Category

A Functional Programmer’s Guide to Homotopy Type Theory (HoTT)

Since April 2019 until I re-visit this Youtube video on 12 August 2019, I can now totally understand his speech after a pause of 4 months by viewing other related Youtube (below prerequisite) videos on Category Theory, Type Theory, Homotopy Type Theory.

That is the technique of self-study:

  1. First go through the whole video,
  2. Don’t understand? view other related simpler videos.
  3. Repeat 1.

Prerequisite knowledge:

  1. Homotopy
  2. Type Theory
  3. Homotopy Type Theory
  4. Bijection = Isomorphism
  5. Functional Programming in Category Theory Concept: Monad & Applicative

Two Key Takeaway Points:

  1. In the Homotopy “Space” : Programs are points in the space, Paths are Types.
  2. “Univalence Axiom” : Paths Induce Bijection, vice versa.

Knowing Monads Through The Category Theory

https://dev.to/juaneto/knowing-monads-through-the-category-theory-1mea

While Mathematicians like to talk non-sensical abstract idea, Informaticians want to know how to apply the idea concretely:

Mathematical Parlance:

Monad = Monoid +Endofunctor

Monoid = Identity + Associative

Endo-functor = functor between 2 same categories

IT Parlance:

Monad is a ‘function’ to wrap the ‘side effects’ (exception errors, I/O,… ) so that function composition in ‘pipeline‘ chained operation sequence is still possible in pure FP (Functional Programming, which forbids side-effects).

Some common Monads: ‘Maybe’, ‘List’, ‘Reader’…
This allows monads to simplify a wide range of problems, like handling potential undefined values (with the Maybe monad), or keeping values within a flexible, well-formed list (using the List monad). With a monad, a programmer can turn a complicated sequence of functions into a succinct pipeline that abstracts away additional data management, control flow, or side-effects.[2][3]

Exploring Monads in Scala Collections

https://blog.redelastic.com/a-guide-to-scala-collections-exploring-monads-in-scala-collections-ef810ef3aec3

Alejandro Serrano: Category Theory Through Functional Programming

(Part 1/3) – λC 2017

What is Category ?

Objects

Morphism (Arrows )

Rule1: Associative

Rule 2: Identity

A <– C –> B

Product of Categories : A x B

Unique

Sum of Categories: A + B

Unique

(Either a b)

Co-Product

Reverse all arrows.

Unique

Functor F: C-> D

Mapping of all objects (A, B) in categories C,D

Mapping of arrows f
f : A -> B
Ff : FA -> FB (preservation)
F Id = Id
F (f。g) = Ff。Fg

Example:

Constant C -> F
FC = k
Ff = Id

Arrow Functor F: C -> D

For any object A in C,

F A = D -> A
(Functional Type is also Type)

Functors compose !

Category of categories:

Objects: categories

Arrows : Functors

Haskell Category (Hask) is always Endo-Functor, ie Category Hask to itself.

Mapping of arrows.

Mapping of Objects = predefined

(Part 2/3) – λC 2017

(part 3/3) – λC 2017

Higher Order Function

[Source] https://www.quora.com/What-are-the-mathematical-explanations-for-higher-order-functions-Functional-programming/answer/Bartosz-Milewski?share=d265e0ac&srid=ZyHj

As Tikhon Jelvis explained in his response, functions map sets to sets, and functions themselves form sets. This is the essence of the untyped lambda calculus. Unfortunately, untyped lambda calculus suffers from the Kleene–Rosser paradox (later simplified to Curry’s paradox).

This paradox can be removed by introducing types, as in the typed lambda calculus. Simple types are equivalent to sets, but  in order to pass a function as an argument to another function (or return one), we have to give this function a type. To really understand what a function type is, you need to look into category theory.

The categorical model for the typed lambda calculus is a category in which objects are types and morphism are functions. So if you want to have higher order functions, you have to be able to represent morphisms as objects — in other words, create a type for functions. This is possible only if the category is cartesian closed. In such a category you can define product types and exponential types. The latter correspond to function types.

So that’s a mathematical explanation for higher order.

BM Category Theory : Motivation and Philosophy

Object-Oriented  has 2 weaknesses for Concurrency and Parallel programming : 

  1. Hidden Mutating States; 
  2. Data Sharing.

Category Theory (CT): a higher abstraction of all different Math structures : Set , Logic, Computing math, Algebra… =>

\boxed {\text {CT reveals the way how our brain works by analysing, reasoning about structures !}}

Our brain works by:  1) Abstraction 2) Composition 3) Identity (to identify)

What is a Category ? 
1) Abstraction:

  •  Objects
  • Morphism (Arrow)

2) Composition: Associative 
3) Identity

Notes: 

  • Small  Category with “Set” as object. 
  • Large Category without Set as object.
  • Morphism is a Set : “Hom” Set.


Example in Programming
:

  • Object : Types Set
  • Morphism : Function “Sin” converts degree to R: \sin \frac {\pi}{2} = 1

Note: We just look at the Category “Types Set” from external Macroview, “forget ” what it contains, we only know the “composition” (Arrows) between the Category “Type Set”, also “forget” what these Arrows (sin,cosin, tgt, etc) actually are, we only study these arrows’ behavior (Associativity).

2.1 : Function of Set, Morphism of Category

Set: A function is 

  • Surjective (greek: epic / epimorphism 满射),
  • Injective (greek : monic / monomorphism 单射)

Category:  [Surjective]

g 。f = h 。f
=> g = h (Right Cancellation )

2.2 Monomorphism 

f 。g = f 。h
=> g = h
(Left cancellation)

\boxed { \text {Epimorphism + Monomorphism =? Isomorphism }}

NOT Necessary !! Reason ( click here): 

In Haskell, 2 foundation Types: Void, Unit

Void = False
Unit ( ) = True

Functions : absurd, unit
absurd :: Void -> a (a = anything)
unit :: a -> ()

[to be continued 3.1 ….]