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.
2. Closure (variable binding)
4. Function Composition
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
Abstraction: Monoid, Category
Functional Programming has the following key styles:
1) Lambda function:
2) Map, Filter, Reduce
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:
- First go through the whole video,
- Don’t understand? view other related simpler videos.
- Repeat 1.
- Type Theory
- Homotopy Type Theory
- Bijection = Isomorphism
- Functional Programming in Category Theory Concept: Monad & Applicative
Two Key Takeaway Points:
- In the Homotopy “Space” : Programs are points in the space, Paths are Types.
- “Univalence Axiom” : Paths Induce Bijection, vice versa.
While Mathematicians like to talk non-sensical abstract idea, Informaticians want to know how to apply the idea concretely:
Monad = Monoid +Endofunctor
Monoid = Identity + Associative
Endo-functor = functor between 2 same categories
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.
Exploring Monads in Scala Collections
- The curse of Immutability in Functional Programming – no “Looping” (recursion ok), no Date, no Random, …no I/O …
- Monad is the Savior of “No Side Effect: IO Monads
Promise of Monads (A)
Promise of Monads (B)
(Part 1/3) – λC 2017
What is Category ?
Morphism (Arrows )
Rule 2: Identity
A <– C –> B
Product of Categories : A x B
Sum of Categories: A + B
(Either a b)
Reverse all arrows.
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
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:
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
- Pure Function
- Partial Function
- Total Function
3) Higher–Order Function, Closure
As 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 (later simplified to .
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.
WhatsApp is written in Erlang – a Functional Programming Language. It supports 900 million users worldwide with only 50 programmers.
Part 1: Compare 3 paradigms:
- Object- Oriented
- Functional Programming
Introduction to Haskell
Part 2: Example – The 8 Queens Problem
Note: A simpler Haskell coding here.
Object-Oriented has 2 weaknesses for Concurrency and Parallel programming :
- Hidden Mutating States;
- Data Sharing.
Category Theory (CT): a higher abstraction of all different Math structures : Set , Logic, Computing math, Algebra… =>
Our brain works by: 1) Abstraction 2) Composition 3) Identity (to identify)
What is a Category ?
- Morphism (Arrow)
2) Composition: Associative
- 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:
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).
Set: A function is
- Surjective (greek: epic / epimorphism 满射),
- Injective (greek : monic / monomorphism 单射)
g 。f = h 。f
=> g = h (Right Cancellation )
f 。g = f 。h
=> g = h (Left cancellation)
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 ….]
Functional Programming, especially Haskell, requires the Math foundation in Abstract Algebra, and Category.