Difference between revisions of "Don't fear the Monad"

From PKC
Jump to navigation Jump to search
m (Text replacement - "{{#ev:youtube|hP3ENPc8Jf0 |width=900 |height=500 }} " to "{{#widget:YouTube |id= hP3ENPc8Jf0 |width= 900 |height=500 }}")
 
(79 intermediate revisions by one other user not shown)
Line 1: Line 1:
==Synopsis of Beckman's Tutorial on Monad==
#[[Don't fear the Monad#Monoid_is_the_way_to_build_complexity_from_simplicity....28start_at_1010.29|Monad is the way to build complexity from simplicity]]
#[[Don't fear the Monad#A Monoid is a collection of things, plus a rule for combining the things, where that (one) rule, obeys some (custom defined) rules ...(start at 1265)|Monad is ruled by one customizable rule that rules them all]]
#[[Don't fear the Monad#Broken symmetry causes confusion (with Monad) (start at 2400)|Monad is hard to learn because of a broken symmetry]]
The idea of [[Monad]] can be traced back to the mathematical structure: [[Monoid]], which is just an algebra with one element only. This mathematical structure provides a basis to reduce complexity. As Beckman says:
[[Monoid]] helps to guarantee you to build a software with [[one and only one]] type ...(start at 1126)
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1126&end=1230}}
<noinclude>
=Universality:You can convert any function into a table lookup... , and the [[function]] is just [[data]] (start at 355)=
=Universality:You can convert any function into a table lookup... , and the [[function]] is just [[data]] (start at 355)=
{{#ev:youtube|ZhuHCtR3xq8||||
Note:
A look up table is a [[Key–value pair]], and every [[Key–value pair]] is act of mapping an input to an output.
In [[PKC]], all [[Page]]s should be modeled in some form of Key-Value pairs, and represent a function. This will make the overall architecture of [[PKC]] to fit into the design pattern of [[Monad]].
 
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=355&end=365}}
|start=355&end=365}}


=[[Monad]] in terms of [[Function]]s, [[Monoid]]s, [[Monad]]s...(starting at 455)=
=[[Monad]] in terms of [[Function]]s, [[Monoid]]s, [[Monad]]s...(starting at 455)=
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=472&end=500}}
|start=472&end=500}}


=[[Monoid]] is '''the way''' to build complexity from simplicity...(start at 1010)=
=[[Monoid]] is '''the way''' to build complexity from simplicity...(start at 1010)=
Simple in Brian's terms means small. I assume this means a smaller vocabulary, in the [[Monoid|monoidal]] case, only one type, so in the size of type vocabulary, it is small.
Simple in Brian's terms means small. I assume this means a smaller vocabulary, in the [[Monoid|monoidal]] case, only one type, so in the size of type vocabulary, it is small.
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1010&end=1080}}
|start=1010&end=1080}}


=[[Monoid]] helps to guarantee you to build a software with [[one and only one type]] ...(start at 1126)=
=[[Monoid]] helps to guarantee you to build a software with [[one and only one]] type ...(start at 1126)=
Create a generic compatibility operator, that allows you to create other functions through composition, ... if one follows this discipline (of being monorail), that you cannot make mistake of creating types that are not the same.
Create a generic '''compatibility operator''', that allows you to create anything in your set of types, ... that are guaranteed to be in the same type, ... functions through [[compositionality|composition]], ... if one follows this discipline (of being monorail), that you cannot make mistakes of creating types that are not the same.
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1126&end=1230}}
|start=1126&end=1230}}
=[[Monoid]] helps to guarantee you to build a software with [[one and only one type]] ...(start at 1126)=
Create a generic '''compatibility operator''', that allows you to create anything in your set of types, ... that are guaranteed to be in the same type, ... functions through composition, ... if one follows this discipline (of being monorail), that you cannot make mistake of creating types that are not the same.
{{#ev:youtube|ZhuHCtR3xq8||||
|start=1126&end=1230}}


=A [[Monoid]] is a collection of things, plus a rule for combining the things, where that (one) rule, obeys some (custom defined) rules ...(start at 1265)=
=A [[Monoid]] is a collection of things, plus a rule for combining the things, where that (one) rule, obeys some (custom defined) rules ...(start at 1265)=
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1267&end=1298}}
|start=1267&end=1298}}


=The Punch Line: Functions under composition form a [[Monoid]]...(start at 1542)=
=The Punch Line: Functions under composition form a [[Monoid]]...(start at 1542)=
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1542&end=1580}}
|start=1542&end=1580}}


=As long as types line up, functional composition make sense...(start at 1570)=
=As long as types line up, functional composition make sense...(start at 1570)=
You don't need to know [[Category Theory]] to be fully conversant in this ([[Monoidal]]) language of functional composition.  
You don't need to know [[Category Theory]] to be fully conversant in this ([[Monoidal]]) language of functional composition.  
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=1570&end=1637}}
|start=1570&end=1637}}


=The Slogan: Compositionality is '''the way''' to control complexity...(start at 2095)=
==The Slogan: Compositionality is '''the way''' to control complexity...(start at 2095)==
#Side effects are complicated. Examples include: Input/Output, Concurrency, Shared Memory, Continuation, Exceptions, Interactions with SQL Databases, Exceptions, ... yuck... If I can get you to [[associativity]]...
#Side effects are complicated. Examples include: Input/Output, Concurrency, Shared Memory, Continuation, Exceptions, Interactions with SQL Databases, Exceptions, ... yuck... If I can get you to [[associativity]]...
#[https://www.infoq.com/interviews/erik-meijer-linq/ LINQ] designed by Eric Meijer, is based on [[Monad]].
#[https://www.infoq.com/interviews/erik-meijer-linq/ LINQ] designed by Eric Meijer, is based on [[Monad]].
#Functions under composition is a [[Monoid]], therefore puts you in a position where you cannot make mistake.
#Functions under [[compositionality|composition]] is a [[Monoid]], therefore puts you in a position where you cannot make mistake.
{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=2095&end=2135}}
|start=2095&end=2135}}


Line 47: Line 93:
  \a -> (f a) >>= \a -> (g a)
  \a -> (f a) >>= \a -> (g a)


{{#ev:youtube|ZhuHCtR3xq8||||
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=2210&end=2245}}
|start=2210&end=2245}}


=This is where people got confused with [[Monad]](start at 2400)=
=Broken symmetry causes confusion (with [[Monad]]) (start at 2400)=
The following definition shows a structure asymmetry...
The following definition shows a structure asymmetry...
  M a >>= a -> M a
  M a >>= a -> M a
The LHS and RHS across the <code>>>=</code> operator has two different types.
The LHS and RHS across the <code>>>=</code> operator has two different types.
{{#ev:youtube|ZhuHCtR3xq8||||
In order to restore the symmetry, we need to put a <math>\lambda</math> expression (<code>\a ->  M a</code>)  in front of the overall structure, to get back the world of compositions. We just have to put one little <math>\lambda</math> from the outside, the entire expression is back to our compositional universe. It is '''almost''' exactly the same as a [[Monoid]].
|start=2400&end=2430}}
\a -> ( M a >>= a -> M a )
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=2400&end=2442}}
 
==The Definition of [[Monad]]:  (start at 2442)==
The [[function]]s <code>\a -> M a</code> live in a Monoid and this [[data]]: <code>M a</code>, live in a [[Monad]], '''that's the definition'''. ... (because) we want [[compositionality]], for the same reasons we mentioned before, we want our functions to live in a Monoid, but we need this extra data, so that we can do concurrency, side effects, I/O, whatever else ... (to make programs useful).
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=2442&end=2477}}
 
=Why [[Monad]]?: Allow functions to be combined in arbitrary ways, while obeying [[Meta-Rule|meta-rule]]s  (start at 2620)=
The bind operator <code>>>=</code> has to obey the [[Meta-Rule|meta-rule]]. It has to obey [[associativity]] (and [[unit]]). (Associativity is also a order-preserving rule, which is related to the notion of time, sequence, and other time-like, entropy-increasing. additive kinds of properties.)
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=2620&end=2662}}
 
=Industry Applications of [[Monad]]?: LINQ passes [[function]]s as [[data]] in C#  (start at 3665)=
[[wikipedia:F Sharp (programming language)|F#]] is the middle ground, which has the performance profile of [[wikipedia:C Sharp (programming language)|C#]], but will be immediately comfortable to people who are familiar with [[wikipedia:Haskell (programming language)|Haskell]]...
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=3665&end=3705}}
 
=Top-down and Bottom-up: The saga since 1947, 1950s (start at 3735)=
The world was split into two camps:
#Bottom-up languages: Fortran, C, C++, C#, Pascal, ...
#Top-down languages: Lisp, ML, Haskell, ...
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=3735&end=3800}}
 
=[[Monad]] saving our complexity quagmire(start at 3900)=
{{#widget:YouTube
|id= hP3ENPc8Jf0
|width= 900
|height=500
}}|ZhuHCtR3xq8||||
|start=3900&end=4000}}


=To restore Symmetry:  (start at 2420)=
</noinclude>
{{#ev:youtube|ZhuHCtR3xq8||||
|start=2420&end=2450}}

Latest revision as of 05:56, 13 January 2024

Synopsis of Beckman's Tutorial on Monad

  1. Monad is the way to build complexity from simplicity
  2. Monad is ruled by one customizable rule that rules them all
  3. Monad is hard to learn because of a broken symmetry

The idea of Monad can be traced back to the mathematical structure: Monoid, which is just an algebra with one element only. This mathematical structure provides a basis to reduce complexity. As Beckman says:

Monoid helps to guarantee you to build a software with one and only one type ...(start at 1126)

|ZhuHCtR3xq8|||| |start=1126&end=1230}}


Universality:You can convert any function into a table lookup... , and the function is just data (start at 355)

Note:

A look up table is a Key–value pair, and every Key–value pair is act of mapping an input to an output.

In PKC, all Pages should be modeled in some form of Key-Value pairs, and represent a function. This will make the overall architecture of PKC to fit into the design pattern of Monad.

|ZhuHCtR3xq8|||| |start=355&end=365}}

Monad in terms of Functions, Monoids, Monads...(starting at 455)

|ZhuHCtR3xq8|||| |start=472&end=500}}

Monoid is the way to build complexity from simplicity...(start at 1010)

Simple in Brian's terms means small. I assume this means a smaller vocabulary, in the monoidal case, only one type, so in the size of type vocabulary, it is small. |ZhuHCtR3xq8|||| |start=1010&end=1080}}

Monoid helps to guarantee you to build a software with one and only one type ...(start at 1126)

Create a generic compatibility operator, that allows you to create anything in your set of types, ... that are guaranteed to be in the same type, ... functions through composition, ... if one follows this discipline (of being monorail), that you cannot make mistakes of creating types that are not the same. |ZhuHCtR3xq8|||| |start=1126&end=1230}}

A Monoid is a collection of things, plus a rule for combining the things, where that (one) rule, obeys some (custom defined) rules ...(start at 1265)

|ZhuHCtR3xq8|||| |start=1267&end=1298}}

The Punch Line: Functions under composition form a Monoid...(start at 1542)

|ZhuHCtR3xq8|||| |start=1542&end=1580}}

As long as types line up, functional composition make sense...(start at 1570)

You don't need to know Category Theory to be fully conversant in this (Monoidal) language of functional composition. |ZhuHCtR3xq8|||| |start=1570&end=1637}}

The Slogan: Compositionality is the way to control complexity...(start at 2095)

  1. Side effects are complicated. Examples include: Input/Output, Concurrency, Shared Memory, Continuation, Exceptions, Interactions with SQL Databases, Exceptions, ... yuck... If I can get you to associativity...
  2. LINQ designed by Eric Meijer, is based on Monad.
  3. Functions under composition is a Monoid, therefore puts you in a position where you cannot make mistake.

|ZhuHCtR3xq8|||| |start=2095&end=2135}}

A New Operator: Haskell calls this Bind, I call it Shove (start at 2210)

It can be expressed as follow:

\a -> (f a) >>= \a -> (g a)

|ZhuHCtR3xq8|||| |start=2210&end=2245}}

Broken symmetry causes confusion (with Monad) (start at 2400)

The following definition shows a structure asymmetry...

M a >>= a -> M a

The LHS and RHS across the >>= operator has two different types. In order to restore the symmetry, we need to put a expression (\a -> M a) in front of the overall structure, to get back the world of compositions. We just have to put one little from the outside, the entire expression is back to our compositional universe. It is almost exactly the same as a Monoid.

\a -> ( M a >>= a -> M a )

|ZhuHCtR3xq8|||| |start=2400&end=2442}}

The Definition of Monad: (start at 2442)

The functions \a -> M a live in a Monoid and this data: M a, live in a Monad, that's the definition. ... (because) we want compositionality, for the same reasons we mentioned before, we want our functions to live in a Monoid, but we need this extra data, so that we can do concurrency, side effects, I/O, whatever else ... (to make programs useful). |ZhuHCtR3xq8|||| |start=2442&end=2477}}

Why Monad?: Allow functions to be combined in arbitrary ways, while obeying meta-rules (start at 2620)

The bind operator >>= has to obey the meta-rule. It has to obey associativity (and unit). (Associativity is also a order-preserving rule, which is related to the notion of time, sequence, and other time-like, entropy-increasing. additive kinds of properties.) |ZhuHCtR3xq8|||| |start=2620&end=2662}}

Industry Applications of Monad?: LINQ passes functions as data in C# (start at 3665)

F# is the middle ground, which has the performance profile of C#, but will be immediately comfortable to people who are familiar with Haskell... |ZhuHCtR3xq8|||| |start=3665&end=3705}}

Top-down and Bottom-up: The saga since 1947, 1950s (start at 3735)

The world was split into two camps:

  1. Bottom-up languages: Fortran, C, C++, C#, Pascal, ...
  2. Top-down languages: Lisp, ML, Haskell, ...

|ZhuHCtR3xq8|||| |start=3735&end=3800}}

Monad saving our complexity quagmire(start at 3900)

|ZhuHCtR3xq8|||| |start=3900&end=4000}}