<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Dixin's Blog</title><link>https://weblogs.asp.net:443/dixin/</link><description>Microsoft Most Valuable Professional (CodingOnWheels.com) &amp; Photographer (PicturesOnWheels.com). Code examples: GitHub.com/Dixin/Blog.</description><item><title>C# Functional Programming In-Depth (14) Asynchronous Function</title><link>https://weblogs.asp.net:443/dixin/functional-csharp-asynchronous-function</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Functional%20C%23" target="_blank"&gt;C# functional programming in-depth series&lt;/a&gt;]&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Asynchronous function can improve the responsiveness and scalability of the application and service. C# 5.0 introduces asynchronous function to greatly simplify the async programming model.&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151486"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Task, Task&amp;lt;TResult&amp;gt; and asynchrony&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The C# async programming model uses System.Threading.Tasks.Task to represent async operation without output, and uses System.Threading.Tasks.Task&amp;lt;TResult&amp;gt; to represent async operation with TResult output:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;: &lt;span&gt;IAsyncResult&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;Task(&lt;span&gt;Action &lt;/span&gt;action); &lt;span&gt;// Wraps () &amp;ndash;&amp;gt; void function.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;Start();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;Wait();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TaskStatus &lt;/span&gt;Status { &lt;span&gt;get&lt;/span&gt;; } &lt;span&gt;// Created, WaitingForActivation, WaitingToRun, Running, WaitingForChildrenToComplete, RanToCompletion, Canceled, Faulted.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;IsCanceled { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;IsCompleted { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;IsFaulted { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;AggregateException &lt;/span&gt;Exception { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;ContinueWith(&lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;gt; continuationAction);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; ContinueWith&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;, &lt;span&gt;TResult&lt;/span&gt;&amp;gt; continuationFunction);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; : &lt;span&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;Task(&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; function); &lt;span&gt;// Wraps () &amp;ndash;&amp;gt; TResult function.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;Result { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;ContinueWith(&lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;&amp;gt; continuationAction);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TNewResult&lt;/span&gt;&amp;gt; ContinueWith&amp;lt;&lt;span&gt;TNewResult&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;, &lt;span&gt;TNewResult&lt;/span&gt;&amp;gt; continuationFunction);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Task can be constructed with () &amp;ndash;&amp;gt;void function, and Task&amp;lt;TResult&amp;gt; can be constructed with () &amp;ndash;&amp;gt; TResult function. They can be started by calling the Start method. A () &amp;ndash;&amp;gt; void function or () &amp;ndash;&amp;gt; TResult function runs synchronously on the thread. In contrast, a task runs asynchronously, and does not block the current thread. Its status can be queried by the Status, IsCanceled, IsCompleted, IsFaulted properties. A task can be waited by calling its Wait method, which blocks the current thread until the task is completed successfully, or fails, or is cancelled. For Task&amp;lt;TResult&amp;gt;, when the underlying async operation is completed successfully, the result is available through Result property. For Task or Task&amp;lt;TResult&amp;gt;, if the underlying async operation fails with exception, the exception is available through the Exception property. A task can be chained with another async continuation operation by calling the ContinueWith methods. When the task finishes running, the specified continuation starts running asynchronously. If the task already finishes running when its ContinueWith method is called, then the specified continuation immediately starts running. The following example constructs and starts a task to read a file, and chains another continuation task to write the contents to another file:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;a name="OLE_LINK8"&gt;&lt;/a&gt;&lt;span&gt;internal static partial class &lt;/span&gt;&lt;span&gt;Functions&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;internal static void &lt;/span&gt;&lt;span&gt;ConstructTask(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Thread&lt;span&gt;.CurrentThread.ManagedThreadId.WriteLine(); &lt;/span&gt;&lt;span&gt;// 10&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt; task = &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;Task&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;&amp;gt;(() =&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Thread&lt;span&gt;.CurrentThread.ManagedThreadId.WriteLine(); &lt;/span&gt;&lt;span&gt;// 8&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return &lt;/span&gt;File&lt;span&gt;.ReadAllText(readPath);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;task.Start();&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task &lt;span&gt;continuationTask = task.ContinueWith(antecedentTask =&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Thread&lt;span&gt;.CurrentThread.ManagedThreadId.WriteLine(); &lt;/span&gt;&lt;span&gt;// 9&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;object&lt;/span&gt;&lt;span&gt;.ReferenceEquals(antecedentTask, task).WriteLine(); &lt;/span&gt;&lt;span&gt;// True&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;if &lt;/span&gt;&lt;span&gt;(antecedentTask.IsFaulted)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;antecedentTask.Exception.WriteLine();&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;File&lt;span&gt;.WriteAllText(writePath, antecedentTask.Result);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;});&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continuationTask.Wait();&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link8;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;As async operations, when tasks are started, the wrapped functions are by default scheduled to runtime&amp;rsquo;s thread pool to execute, so that their thread ids are different from the caller thread id.&lt;/span&gt;&lt;/p&gt;
&lt;div style="border-width: 1pt medium; border-style: solid none; border-color: windowtext currentcolor; padding: 1pt 0in; margin-right: 7.1pt; margin-left: 7.1pt; mso-element: para-border-div;"&gt;&lt;span style="font-size: 12pt;"&gt; &lt;/span&gt;
&lt;p class="NoteTipCaution" style="margin: 0.25in 0in 24pt; padding: 0in; line-height: 14pt;"&gt;&lt;span style="font-size: 13pt;"&gt;In .NET Framework, Task also implements System.Threading.IThreadPoolWorkItem and IDisposable interfaces. Its usage is the same as in .NET Core. Do not bother disposing tasks.&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size: 12pt;"&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Task also provides Run methods to construct and automatically start tasks:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;: &lt;span&gt;IAsyncResult&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;Run(&lt;span&gt;Action &lt;/span&gt;action);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; Run&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; function);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Now compare the following functions:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static void &lt;/span&gt;&lt;span&gt;Write(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;path, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;contents) =&amp;gt; &lt;/span&gt;&lt;span&gt;File&lt;/span&gt;&lt;span&gt;.WriteAllText(path, contents);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static string &lt;/span&gt;Read(&lt;span&gt;string &lt;/span&gt;path) =&amp;gt; &lt;span&gt;File&lt;/span&gt;.ReadAllText(path);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;WriteAsync(&lt;span&gt;string &lt;/span&gt;path, &lt;span&gt;string &lt;/span&gt;contents) =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;.Run(() =&amp;gt; &lt;span&gt;File&lt;/span&gt;.WriteAllText(path, contents));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;ReadAsync(&lt;span&gt;string &lt;/span&gt;path) =&amp;gt; &lt;span&gt;Task&lt;/span&gt;.Run(() =&amp;gt; &lt;span&gt;File&lt;/span&gt;.ReadAllText(path));&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Write without output and Read with string output run synchronously. WriteAsync with Task output and ReadAsync with Task&amp;lt;string&amp;gt; output run asynchronously, where Task can be viewed as future void, and Task&amp;lt;TResult&amp;gt; can be viewed as future TResult result. Here WriteAsync and ReadAsync become async by simply offloading the operations to thread pool. This is for demonstration purpose, and does not bring any scalability improvement. A better implementation is discussed later.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal &lt;/span&gt;static void CallReadWrite(string path, string contents)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Write(path, contents); // Blocking.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// When the underlying write operation is done, the call is completed with no result.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string result = Read(path); // Blocking.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// When the underlying read operation is done, the call is completed with result available.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task writeTask = WriteAsync(path, contents); // Non-blocking.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// When the task is constructed and started, the call is completed immediately.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// The underlying write operation is scheduled, and will be completed in the future with no result.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task&amp;lt;string&amp;gt; readTask = ReadAsync(path); // Non-blocking.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// When the task is constructed and started, the call is completed immediately.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// The underlying read operation is scheduled, and will be completed in the future with result available.&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;When Write is called, its execution blocks the current thread. When the writing operation is done synchronously, it does not output any result, and then the caller thread can continue execution. Similarly, when Read is called, its execution blocks the current thread too. When the reading operation is done synchronously, it outputs the result, so that the result is available to the caller and the caller can continue execution. When WriteAsync is called, it calls Task.Run to construct a Task instance with the writing operation, start the task, then immediately outputs the task to the caller. Then the caller can continue without being blocked by the writing operation execution. By default, the writing operation is scheduled to thread pool, when it is done, the writing operation outputs no result, and the task&amp;rsquo;s Status is updated. Similarly, when ReadAsync is called, it also calls Task.Run to construct a Task&amp;lt;string&amp;gt; instance with the reading operation, start the task, then immediately outputs the task to the caller. Then the caller can continue without being blocked by the reading operation execution. By default, the reading operation is also scheduled to thread pool, when it is done, the reading operation has a result, and the task&amp;rsquo;s Status is updated, with the result available through the Result property.&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151487"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Named async function&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;By default, named async function&amp;rsquo;s output type is Task or Task&amp;lt;TResult&amp;gt;, and has an Async or AsyncTask postfix in the name as the convention. The following example is a file read and write workflow of sync function calls:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static void &lt;/span&gt;&lt;span&gt;ReadWrite(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;contents = Read(readPath);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Write(writePath, contents);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The same logic can be implemented by calling the async version of functions:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;ReadWriteAsync(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;contents = &lt;span&gt;await &lt;/span&gt;ReadAsync(readPath);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;WriteAsync(writePath, contents);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Here await keyword is used for each async function call, and the code structure remains the same as the sync workflow. When await keyword is used in function body, the async modifier is required for that function&amp;rsquo;s signature. Regarding the workflow outputs no result, the async function outputs Task (future void). This ReadWriteAsync function calls async functions, itself is also async function, since it has the async modifier and Task output. When ReadWriteAsync is called, it works the same way as ReadAsync and WriteAsync. it does not block its caller, and immediately outputs a task to represent the scheduled read and write workflow.&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;So the await keyword can be viewed as virtually waiting for the task&amp;rsquo;s underlying async operation to finish. If the task fails, exception is thrown. If the task is completed successfully, the continuation right after the await expression is called back. If the task has a result, await expression can extract the result. Therefore, the async workflow keeps the same looking of sync workflow. There is no ContinueWith call needed to build the continuation. The following example is a more complex database query workflow of sync function calls, with an int value as the query result:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;a name="OLE_LINK9"&gt;&lt;/a&gt;&lt;span&gt;internal static int &lt;/span&gt;&lt;span&gt;Query(&lt;/span&gt;&lt;span&gt;DbConnection &lt;/span&gt;&lt;span&gt;connection, &lt;/span&gt;&lt;span&gt;StreamWriter &lt;/span&gt;&lt;span&gt;logWriter) // Output int.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;connection.Open(); &lt;span&gt;// Output void.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;DbCommand &lt;/span&gt;command = connection.CreateCommand())&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;command.CommandText = &lt;span&gt;"SELECT 1;"&lt;/span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;DbDataReader &lt;/span&gt;reader = command.ExecuteReader()) &lt;span&gt;// Output DbDataReader.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(reader.Read()) &lt;span&gt;// Output bool.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;)reader[0];&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;throw new &lt;/span&gt;&lt;span&gt;InvalidOperationException&lt;/span&gt;(&lt;span&gt;"Failed to call sync functions."&lt;/span&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;catch &lt;/span&gt;(&lt;span&gt;SqlException &lt;/span&gt;exception)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;logWriter.WriteLine(exception.ToString()); &lt;span&gt;// Output void.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;throw new &lt;/span&gt;&lt;span&gt;InvalidOperationException&lt;/span&gt;(&lt;span&gt;"Failed to call sync functions."&lt;/span&gt;, exception);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link9;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Here the DbConnection.Open, DbCommand.ExecuteReader, DbDataReader.Read, StreamWriter.WriteLine functions have async version provided as DbConnection.OpenAsync, DbCommand.ExecuteReaderAsync, DbDataReader.ReadAsync, StreamWriter.WriteLineAsync. They output either Task or Task&amp;lt;TResult&amp;gt;. With the async and await keywords, it easy to call these async functions:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;a name="OLE_LINK11"&gt;&lt;/a&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; QueryAsync(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DbConnection &lt;/span&gt;&lt;span&gt;connection, &lt;/span&gt;&lt;span&gt;StreamWriter &lt;/span&gt;&lt;span&gt;logWriter) &lt;/span&gt;&lt;span&gt;// Output Task&amp;lt;int&amp;gt; instead of int.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;connection.OpenAsync(); &lt;span&gt;// Output Task instead of void.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;DbCommand &lt;/span&gt;command = connection.CreateCommand())&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;command.CommandText = &lt;span&gt;"SELECT 1;"&lt;/span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;DbDataReader &lt;/span&gt;reader = &lt;span&gt;await &lt;/span&gt;command.ExecuteReaderAsync()) &lt;span&gt;// Output Task&amp;lt;DbDataReader&amp;gt; instead of DbDataReader.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;await &lt;/span&gt;reader.ReadAsync()) &lt;span&gt;// Output Task&amp;lt;bool&amp;gt; instead of bool.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;)reader[0];&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;throw new &lt;/span&gt;&lt;span&gt;InvalidOperationException&lt;/span&gt;(&lt;span&gt;"Failed to call async functions."&lt;/span&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;catch &lt;/span&gt;(&lt;span&gt;SqlException &lt;/span&gt;exception)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;logWriter.WriteLineAsync(exception.ToString()); &lt;span&gt;// Output Task instead of void.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;throw new &lt;/span&gt;&lt;span&gt;InvalidOperationException&lt;/span&gt;(&lt;span&gt;"Failed to call async functions."&lt;/span&gt;, exception);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-bookmark: ole_link11;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Again, the async workflow persists the same code structure as the sync workflow, including try-catch, using, if statements, etc. Without this syntax, it is a lot more complex to call ContinueWith and manually build above workflow. Regarding the async function has an int result, its output type is Task&amp;lt;int&amp;gt; (future int).&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The above Write and Read functions calls File.WriteAllText and File.ReadAllText to execute sync I/O operation, which are internally implemented by calling StreamWriter.Write and StreamReader.ReadToEnd. Now with the async and await keywords, WriteAsync and ReadAsync can be reimplemented as real async I/O (assuming async I/O &lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;is actually supported by the underlying operating system) by calling StreamWriter.WriteAsync and StreamReader.ReadToEndAsync:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;WriteAsync(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;path, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;contents)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// File.WriteAllText implementation:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// using (StreamWriter writer = new StreamWriter(new FileStream(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: FileMode.Create, access: FileAccess.Write,&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: FileShare.Read, bufferSize: 4096, useAsync: false)))&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// {&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;writer.Write(contents);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// }&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;StreamWriter &lt;/span&gt;writer = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;StreamWriter&lt;/span&gt;(&lt;span&gt;new &lt;/span&gt;&lt;span&gt;FileStream&lt;/span&gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: &lt;span&gt;FileMode&lt;/span&gt;.Create, access: &lt;span&gt;FileAccess&lt;/span&gt;.Write,&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: &lt;span&gt;FileShare&lt;/span&gt;.Read, bufferSize: 4096, useAsync: &lt;span&gt;true&lt;/span&gt;)))&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;writer.WriteAsync(contents);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;ReadAsync(&lt;span&gt;string &lt;/span&gt;path)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// File.ReadAllText implementation:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// using (StreamReader reader = new StreamReader(new FileStream(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: FileMode.Open, access: FileAccess.Read, &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: FileShare.Read, bufferSize: 4096, useAsync: false)))&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// {&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return reader.ReadToEnd();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// }&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;StreamReader &lt;/span&gt;reader = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;StreamReader&lt;/span&gt;(&lt;span&gt;new &lt;/span&gt;&lt;span&gt;FileStream&lt;/span&gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: &lt;span&gt;FileMode&lt;/span&gt;.Open, access: &lt;span&gt;FileAccess&lt;/span&gt;.Read,&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: &lt;span&gt;FileShare&lt;/span&gt;.Read, bufferSize: 4096, useAsync: &lt;span&gt;true&lt;/span&gt;)))&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return await &lt;/span&gt;reader.ReadToEndAsync();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;There is one special scenario where async function has void output &amp;ndash; async function as event handler. For example, ObservableCollection&amp;lt;T&amp;gt; has a CollectionChanged event:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Collections.ObjectModel&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public class &lt;/span&gt;&lt;span&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt; : &lt;span&gt;Collection&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;, &lt;span&gt;INotifyCollectionChanged&lt;/span&gt;, &lt;span&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public event &lt;/span&gt;&lt;span&gt;NotifyCollectionChangedEventHandler &lt;/span&gt;CollectionChanged;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;System.Collections.Specialized&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;a name="OLE_LINK7"&gt;&lt;/a&gt;&lt;a name="OLE_LINK6"&gt;&lt;/a&gt;&lt;a name="OLE_LINK5"&gt;&lt;/a&gt;&lt;span style="mso-bookmark: ole_link6;"&gt;&lt;span style="mso-bookmark: ole_link7;"&gt;&lt;span&gt;// (object, NotifyCollectionChangedEventArgs) &amp;ndash;&amp;gt; void.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;public delegate void &lt;/span&gt;&lt;span&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;object &lt;/span&gt;sender, &lt;span&gt;NotifyCollectionChangedEventArgs &lt;/span&gt;e);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;This event requires its handler to be a function of type (object, NotifyCollectionChangedEventArgs) &amp;ndash;&amp;gt; void. So, when defining an async function as the above event&amp;rsquo;s handler, that async function has void output instead of Task:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private static readonly &lt;/span&gt;StringBuilder &lt;span&gt;Logs = &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;StringBuilder&lt;span&gt;();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private static readonly &lt;/span&gt;StringWriter &lt;span&gt;LogWriter = &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;StringWriter&lt;span&gt;(logs);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;// (object, NotifyCollectionChangedEventArgs) &amp;ndash;&amp;gt; void.&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private static async void &lt;/span&gt;&lt;span&gt;CollectionChangedAsync(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;object &lt;/span&gt;&lt;span&gt;sender, &lt;/span&gt;NotifyCollectionChangedEventArgs &lt;span&gt;e) =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;await &lt;/span&gt;&lt;span&gt;LogWriter.WriteLineAsync(e.Action.ToString());&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static void Add&lt;/span&gt;&lt;span&gt;EventHandler()&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;ObservableCollection&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt; collection = &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;ObservableCollection&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;collection.CollectionChanged += CollectionChangedAsync;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;collection.Add(1); &lt;/span&gt;&lt;span&gt;// Fires CollectionChanged event.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The await keyword works with task from async function as well as any Task and Task&amp;lt;TResult&amp;gt; instance:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal &lt;/span&gt;static async Task AwaitTasks(string path)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task&amp;lt;string&amp;gt; task1 = ReadAsync(path);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string contents = await task1;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: string contents = await ReadAsync(path);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task task2 = WriteAsync(path, contents);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;await task2;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: await WriteAsync(path, contents);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task task3 = Task.Run(() =&amp;gt; { });&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;await task3;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: await Task.Run(() =&amp;gt; { });&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task&amp;lt;int&amp;gt; task4 = Task.Run(() =&amp;gt; 0);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int result = await task4;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: int result = await Task.Run(() =&amp;gt; 0);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task task5 = Task.Delay(TimeSpan.FromSeconds(10));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;await task5;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: await Task.Delay(TimeSpan.FromSeconds(10));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task&amp;lt;int&amp;gt; task6 = Task.FromResult(result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;result = await task6;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Equivalent to: result = await Task.FromResult(result);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;If a task is never started, apparently it never finishes running. The code after its await expression is never called back:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;HotColdTasks(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;path)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;hotTask = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;(() =&amp;gt; { });&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;hotTask.Start();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;hotTask;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;hotTask.Status.WriteLine();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;coldTask = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;(() =&amp;gt; { });&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;coldTask;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;coldTask.Status.WriteLine(); &lt;span&gt;// Never execute.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Task not started yet is called cold task, and task already started is called hot task. As a convention, any function with task output should always output a hot task. All APIs in .NET Standard follow this convention.&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151488"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Awaitable-awaiter pattern&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;C# compiles the await expression with the awaitable-awaiter pattern. Besides Task and Task&amp;lt;TResult&amp;gt;, the await keyword can be used with any awaitable type. An awaitable type has a GetAwaiter instance or extension method to output an awaiter. An awaiter type implements System.Runtime.CompilerServices.INotifyCompletion interface, also has an IsCompleted property with bool output, and a GetResult instance method with or without output. The following IAwaitable and IAwaiter interfaces demonstrate the awaitable-awaiter pattern for operation with no result:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public interface &lt;/span&gt;&lt;span&gt;IAwaitable&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;IAwaiter &lt;span&gt;GetAwaiter();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public interface &lt;/span&gt;IAwaiter &lt;span&gt;: &lt;/span&gt;INotifyCompletion&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;bool &lt;/span&gt;&lt;span&gt;IsCompleted { &lt;/span&gt;&lt;span&gt;get&lt;/span&gt;&lt;span&gt;; }&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;void &lt;/span&gt;&lt;span&gt;GetResult(); &lt;/span&gt;&lt;span&gt;// No result.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;And the following IAwaitable&amp;lt;TResult&amp;gt; and IAwaiter&amp;lt;TResult&amp;gt; interfaces demonstrate the awaitable-awaiter pattern for operations with a result:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public interface &lt;/span&gt;&lt;span&gt;IAwaitable&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;TResult&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;IAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;GetAwaiter();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public interface &lt;/span&gt;&lt;span&gt;IAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;: &lt;span&gt;INotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;bool &lt;/span&gt;IsCompleted { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;GetResult(); &lt;span&gt;// TResult result.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;And INotifyCompletion interface has a single OnCompleted method to chain a continuation:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Runtime.CompilerServices&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public interface &lt;/span&gt;&lt;span&gt;INotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;void &lt;/span&gt;OnCompleted(&lt;span&gt;Action &lt;/span&gt;continuation);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Here is how Task and Task&amp;lt;TResult&amp;gt; implement the awaitable-awaiter pattern. Task can be virtually viewed as implementation of IAwaitable, it has a GetAwaiter instance method outputting System.Runtime.CompilerServices.TaskAwaiter, which can be virtually viewed as implementation of IAwaiter; Similarly, Task&amp;lt;TResult&amp;gt; can be virtually viewed as implementation of IAwaitable&amp;lt;TResult&amp;gt;, it has a GetAwaiter method outputting System.Runtime.CompilerServices.TaskAwaiter&amp;lt;TResult&amp;gt;, which can be virtually viewed as implementation of IAwaiter&amp;lt;TResult&amp;gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;: &lt;span&gt;IAsyncResult&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TaskAwaiter &lt;/span&gt;GetAwaiter();&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; : &lt;span&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TaskAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; GetAwaiter();&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;System.Runtime.CompilerServices&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public struct &lt;/span&gt;&lt;span&gt;TaskAwaiter &lt;/span&gt;: &lt;span&gt;ICriticalNotifyCompletion&lt;/span&gt;, &lt;span&gt;INotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;IsCompleted { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;GetResult(); &lt;span&gt;// No result.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;OnCompleted(&lt;span&gt;Action &lt;/span&gt;continuation);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public struct &lt;/span&gt;&lt;span&gt;TaskAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; : &lt;span&gt;ICriticalNotifyCompletion&lt;/span&gt;, &lt;span&gt;INotifyCompletion&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;IsCompleted { &lt;span&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;GetResult(); &lt;span&gt;// TResult result.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;OnCompleted(&lt;span&gt;Action &lt;/span&gt;continuation);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Any other type can be used with the await keyword, as long as the awaitable-awaiter pattern is implemented. Take delegate type Action as example, a GetAwaiter method can be easily implemented as its extension method, by reusing above TaskAwaiter:&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="margin: 6pt 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;public static &lt;/span&gt;TaskAwaiter &lt;span&gt;GetAwaiter(&lt;/span&gt;&lt;span&gt;this &lt;/span&gt;Action &lt;span&gt;action) =&amp;gt; &lt;/span&gt;Task&lt;span&gt;.Run(action).GetAwaiter();&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Similarly, this pattern can be implemented for Func&amp;lt;TResult&amp;gt;, by reusing TaskAwaiter&amp;lt;TResult&amp;gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public static &lt;/span&gt;TaskAwaiter&lt;span&gt;&amp;lt;&lt;/span&gt;TResult&lt;span&gt;&amp;gt; GetAwaiter&amp;lt;&lt;/span&gt;TResult&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;this &lt;/span&gt;Func&lt;span&gt;&amp;lt;&lt;/span&gt;TResult&lt;span&gt;&amp;gt; function) =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task&lt;span&gt;.Run(function).GetAwaiter();&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Now the await keyword can be directly used with a function of Action type or Func&amp;lt;TResult&amp;gt; type:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;AwaitFunctions(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;read = () =&amp;gt; &lt;span&gt;File&lt;/span&gt;.ReadAllText(readPath);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;contents = &lt;span&gt;await &lt;/span&gt;read;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Action &lt;/span&gt;write = () =&amp;gt; &lt;span&gt;File&lt;/span&gt;.WriteAllText(writePath, contents);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;write;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151489"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Async state machine&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;As fore mentioned, with async and await keywords, an async function outputs a task immediately, so it is non-blocking. At compile time, the workflow of an async function is compiled to an async state machine. At runtime, when this async function is called, it just starts that generated async state machine , and immediately outputs a task representing the workflow in the async state machine. To demonstrate this, define the following async methods:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt; Async&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;T &lt;/span&gt;&lt;span&gt;value)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;value1 = Start(value);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;result1 = &lt;span&gt;await &lt;/span&gt;Async1(value1);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;value2 = Continuation1(result1);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;result2 = &lt;span&gt;await &lt;/span&gt;Async2(value2);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;value3 = Continuation2(result2);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;result3 = &lt;span&gt;await &lt;/span&gt;Async3(value3);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;result = Continuation3(result3);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;Start&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; value;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt; Async1&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; &lt;span&gt;Task&lt;/span&gt;.Run(() =&amp;gt; value);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;Continuation1&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; value;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt; Async2&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; &lt;span&gt;Task&lt;/span&gt;.FromResult(value);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;Continuation2&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; value;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt; Async3&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; &lt;span&gt;Task&lt;/span&gt;.Run(() =&amp;gt; value);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;Continuation3&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value) =&amp;gt; value;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;After compilation, the async modifier is gone. The async function becomes a normal function to start an async state machine:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;AsyncStateMachine&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;AsyncStateMachine&lt;/span&gt;&lt;span&gt;&amp;lt;&amp;gt;))]&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt; CompiledAsync&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;(&lt;span&gt;T &lt;/span&gt;value)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;AsyncStateMachine&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;asyncStateMachine = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;AsyncStateMachine&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;()&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Value = value,&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Builder = &lt;span&gt;AsyncTaskMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;T&lt;/span&gt;&amp;gt;.Create(),&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;State = -1 &lt;span&gt;// -1 means start.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;};&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;asyncStateMachine.Builder.Start(&lt;span&gt;ref &lt;/span&gt;asyncStateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;asyncStateMachine.Builder.Task;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;And the generated async state machine is a structure in release build, and a class in debug build:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;CompilerGenerated&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;[&lt;span&gt;StructLayout&lt;/span&gt;(&lt;span&gt;LayoutKind&lt;/span&gt;.Auto)]&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private struct &lt;/span&gt;&lt;span&gt;AsyncStateMachine&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;: &lt;span&gt;IAsyncStateMachine&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public int &lt;/span&gt;State;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;AsyncTaskMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; Builder;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;Value;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private &lt;/span&gt;&lt;span&gt;TaskAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; awaiter;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;void &lt;/span&gt;&lt;span&gt;IAsyncStateMachine&lt;/span&gt;.MoveNext()&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;switch &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.State)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;case &lt;/span&gt;-1: &lt;span&gt;// Start code from the beginning to the 1st await.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Workflow begins.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;value1 = Start(&lt;span&gt;this&lt;/span&gt;.Value);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.awaiter = Async1(value1).GetAwaiter();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.awaiter.IsCompleted)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async1 is already completed, immediately execute the continuation.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;goto case &lt;/span&gt;0;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.State = 0;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async1 is not completed, specify the continuation as its callback.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.AwaitUnsafeOnCompleted(&lt;span&gt;ref this&lt;/span&gt;.awaiter, &lt;span&gt;ref this&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Later when the task returned by Async1 is completed, it calls back MoveNext, where State is 0.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;case &lt;/span&gt;0: &lt;span&gt;// Continuation code from after the 1st await to the 2nd await.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// The task returned by Async1 is completed. The result is available immediately through GetResult.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result1 = &lt;span&gt;this&lt;/span&gt;.awaiter.GetResult();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;value2 = Continuation1(result1);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.awaiter = Async2(value2).GetAwaiter();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.awaiter.IsCompleted)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async2 is already completed, immediately execute the continuation.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;goto case &lt;/span&gt;1;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.State = 1;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async2 is not completed, specify the continuation as its callback.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.AwaitUnsafeOnCompleted(&lt;span&gt;ref this&lt;/span&gt;.awaiter, &lt;span&gt;ref this&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Later when the task returned by Async2 is completed, it calls back MoveNext, where State is 1.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;case &lt;/span&gt;1: &lt;span&gt;// Continuation code from after the 2nd await to the 3rd await.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// The task returned by Async2 is completed. The result is available immediately through GetResult.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result2 = &lt;span&gt;this&lt;/span&gt;.awaiter.GetResult();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;value3 = Continuation2(result2);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.awaiter = Async3(value3).GetAwaiter();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.awaiter.IsCompleted)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async3 is already completed, immediately execute the continuation.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;goto case &lt;/span&gt;2;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.State = 2;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// If the task returned by Async3 is not completed, specify the continuation as its callback.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.AwaitUnsafeOnCompleted(&lt;span&gt;ref this&lt;/span&gt;.awaiter, &lt;span&gt;ref this&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Later when the task returned by Async3 is completed, it calls back MoveNext, where State is 1.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;case &lt;/span&gt;2: &lt;span&gt;// Continuation code from after the 3rd await to the end.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// The task returned by Async3 is completed. The result is available immediately through GetResult.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result3 = &lt;span&gt;this&lt;/span&gt;.awaiter.GetResult();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;result = Continuation3(result3);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.State = -2; &lt;span&gt;// -2 means end.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.SetResult(result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Workflow ends.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;catch &lt;/span&gt;(&lt;span&gt;Exception &lt;/span&gt;exception)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.State = -2; &lt;span&gt;// -2 means end.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.SetException(exception);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[&lt;span&gt;DebuggerHidden&lt;/span&gt;]&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;void &lt;/span&gt;&lt;span&gt;IAsyncStateMachine&lt;/span&gt;.SetStateMachine(&lt;span&gt;IAsyncStateMachine &lt;/span&gt;asyncStateMachine) =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Builder.SetStateMachine(asyncStateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The generated async state machine is a finite state machine:&lt;/span&gt;&lt;/p&gt;
&lt;p align="center" class="Figure" style="margin: 12pt 0in; page-break-after: avoid;"&gt;&lt;span style="mso-no-proof: yes;"&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Functional-C-13-Pure-Function_F89E/clip_image002_2.gif"&gt;&lt;img width="547" height="257" title="clip_image002" style="border: 0px currentcolor; display: inline; background-image: none;" alt="clip_image002" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Functional-C-13-Pure-Function_F89E/clip_image002_thumb.gif" border="0" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The workflow is compiled into a switch statement in its MoveNext method. The original workflow is split to 4 parts by the 3 await keywords, and compiled to 4 cases of the switch statement. The parameter of the workflow is compiled as a field of the state machine, so it can be accessed by the workflow inside MoveNext. When the state machine is initialized, its initial state is &amp;ndash;1, which means to start. Once the state machine is started, MoveNext is called, and the case &amp;ndash;1 block is executed, which has the code from the beginning of the workflow to the first await expression, which is compiled to a GetAwaiter call. If the awaiter is already completed, then the continuation should immediate be executed by going to case 0 block directly; If the awaiter is not completed, the continuation (MoveNext call with next state 0) is specified as the awaiter&amp;rsquo;s callback when it is completed in the future. In either case, when code in case 0 block is executed, the previous awaiter is already completed, and its result is immediately available through its GetResult method. The execution goes on in the same pattern, until the last block of case 2 is executed.&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151490"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Runtime context capture&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;At runtime, when executing an await expression, if the awaited task is not completed yet, the continuation is scheduled as callback. As a result, the continuation can be executed by a thread different from initial caller thread. By default, the initial thread&amp;rsquo;s runtime context information is captured, and are reused by the callback thread to execute the continuation. To demonstrate this, the above awaitable-awaiter pattern for Action can be re-implemented with the following custom awaiter:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public static &lt;/span&gt;IAwaiter &lt;span&gt;GetAwaiter(&lt;/span&gt;&lt;span&gt;this &lt;/span&gt;Action &lt;span&gt;action) =&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new &lt;/span&gt;ActionAwaiter&lt;span&gt;(&lt;/span&gt;Task&lt;span&gt;.Run(action));&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public class &lt;/span&gt;ActionAwaiter &lt;span&gt;: &lt;/span&gt;IAwaiter&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;private readonly &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;SynchronizationContext&lt;span&gt;, &lt;/span&gt;TaskScheduler&lt;span&gt;, &lt;/span&gt;ExecutionContext&lt;span&gt;) runtimeContext;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;private readonly &lt;/span&gt;Task &lt;span&gt;task;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;ActionAwaiter(&lt;/span&gt;Task &lt;span&gt;task) =&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.task, this.runtimeContext) = (task, RuntimeContext.Capture());&lt;a name="OLE_LINK1"&gt;&lt;/a&gt; // Capture runtime context when initialized.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public bool &lt;/span&gt;&lt;span&gt;IsCompleted =&amp;gt; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.task.IsCompleted;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;&lt;span&gt;GetResult() =&amp;gt; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.task.Wait();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;&lt;span&gt;OnCompleted(&lt;/span&gt;Action &lt;span&gt;continuation) =&amp;gt; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.task.ContinueWith(task =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.runtimeContext.Execute(continuation));&lt;a name="OLE_LINK2"&gt;&lt;/a&gt;// Execute continuation on captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;When the awaiter is constructed, it captures the runtime context information, including System.Threading.SynchronizationContext, System.Threading.Tasks.TaskScheduler, and System.Threading.ExecutionContext of current thread. Then in OnCompleted, when the continuation is called back, it is executed with the previously captured runtime context information. The custom awaiter can be implemented for Func&amp;lt;TResult&amp;gt; in the same pattern:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public static IAwaiter&amp;lt;TResult&amp;gt; GetAwaiter&amp;lt;TResult&amp;gt;(this Func&amp;lt;TResult&amp;gt; function) =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new FuncAwaiter&amp;lt;TResult&amp;gt;(Task.Run(function));&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public class FuncAwaiter&amp;lt;TResult&amp;gt; : IAwaiter&amp;lt;TResult&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private readonly (SynchronizationContext, TaskScheduler, ExecutionContext) runtimeContext =&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;RuntimeContext.Capture();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private readonly Task&amp;lt;TResult&amp;gt; task;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public FuncAwaiter(Task&amp;lt;TResult&amp;gt; task) =&amp;gt; (this.task, this.runtimeContext) = (task, RuntimeContext.Capture()); // Capture runtime context when initialized.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public bool IsCompleted =&amp;gt; this.task.IsCompleted;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public TResult GetResult() =&amp;gt; this.task.Result;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void OnCompleted(Action continuation) =&amp;gt; this.task.ContinueWith(task =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.runtimeContext.Execute(continuation)); // Execute continuation on captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The following is a basic implementation of runtime context capture and resume:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public static class &lt;/span&gt;&lt;span&gt;RuntimeContext&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;SynchronizationContext&lt;span&gt;, &lt;/span&gt;TaskScheduler&lt;span&gt;, &lt;/span&gt;ExecutionContext&lt;span&gt;) Capture() =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/span&gt;SynchronizationContext&lt;span&gt;.Current, &lt;/span&gt;TaskScheduler&lt;span&gt;.Current, &lt;/span&gt;ExecutionContext&lt;span&gt;.Capture());&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;public static void &lt;/span&gt;&lt;span&gt;Execute(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this &lt;/span&gt;&lt;span&gt;(&lt;/span&gt;SynchronizationContext&lt;span&gt;, &lt;/span&gt;TaskScheduler&lt;span&gt;, &lt;/span&gt;ExecutionContext&lt;span&gt;) runtimeContext, &lt;/span&gt;Action &lt;span&gt;continuation)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;var &lt;/span&gt;&lt;span&gt;(synchronizationContext, taskScheduler, executionContext) = runtimeContext;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;if &lt;/span&gt;&lt;span&gt;(synchronizationContext != &lt;/span&gt;&lt;span&gt;null &lt;/span&gt;&lt;span&gt;&amp;amp;&amp;amp; synchronizationContext.GetType() != &lt;/span&gt;&lt;span&gt;typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;SynchronizationContext&lt;span&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;if &lt;/span&gt;&lt;span&gt;(synchronizationContext == &lt;/span&gt;SynchronizationContext&lt;span&gt;.Current)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionContext.Run(continuation);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionContext.Run(() =&amp;gt; synchronizationContext.Post(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;d: state =&amp;gt; continuation(), state: &lt;/span&gt;&lt;span&gt;null&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;if &lt;/span&gt;&lt;span&gt;(taskScheduler != &lt;/span&gt;&lt;span&gt;null &lt;/span&gt;&lt;span&gt;&amp;amp;&amp;amp; taskScheduler != &lt;/span&gt;TaskScheduler&lt;span&gt;.Default)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task &lt;span&gt;continuationTask = &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;Task&lt;span&gt;(continuation);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continuationTask.Start(taskScheduler);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionContext.Run(continuation);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;private static void &lt;/span&gt;&lt;span&gt;Run(&lt;/span&gt;&lt;span&gt;this &lt;/span&gt;ExecutionContext &lt;span&gt;executionContext, &lt;/span&gt;Action &lt;span&gt;continuation)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;if &lt;/span&gt;&lt;span&gt;(executionContext != &lt;/span&gt;&lt;span&gt;null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;ExecutionContext&lt;span&gt;.Run(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;executionContext: executionContext, callback: state =&amp;gt; continuation(), state: &lt;/span&gt;&lt;span&gt;null&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continuation();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;When Capture is called, it captures a 3-tuple of SynchronizationContext, TaskScheduler, and ExecutionContext When the continuation is executed, first the previously captured SynchronizationContext is checked. If a specialized SynchronizationContext is captured and it is different from current SynchronizationContext, then the continuation is executed with the captured SynchronizationContext and ExecutionContext. When there is no specialized SynchronizationContext captured, then the TaskScheduler is checked. If a specialized TaskScheduler is captured, it is used to schedule the continuation as a task. For all the other cases, the continuation is executed with the captured ExecutionContext.&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Task and Task&amp;lt;TResult&amp;gt; provides a ConfigureAwait method to specify whether the continuation is marshalled to the previously captured runtime context:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;: &lt;span&gt;IAsyncResult&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;ConfiguredTaskAwaitable &lt;/span&gt;ConfigureAwait(&lt;span&gt;bool &lt;/span&gt;continueOnCapturedContext);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public partial class &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; : &lt;span&gt;Task&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;ConfiguredTaskAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; ConfigureAwait(&lt;span&gt;bool &lt;/span&gt;continueOnCapturedContext);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;To demonstrate the runtime context capture, define a custom task scheduler, which simply start a background thread to execute each task:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public class &lt;/span&gt;&lt;span&gt;BackgroundThreadTaskScheduler &lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span&gt;TaskScheduler&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;protected override &lt;/span&gt;IEnumerable&lt;span&gt;&amp;lt;&lt;/span&gt;Task&lt;span&gt;&amp;gt; GetScheduledTasks() =&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw new &lt;/span&gt;NotImplementedException&lt;span&gt;();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;protected override void &lt;/span&gt;&lt;span&gt;QueueTask(&lt;/span&gt;Task &lt;span&gt;task) =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;new &lt;/span&gt;Thread&lt;span&gt;(() =&amp;gt; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.TryExecuteTask(task)) { IsBackground = &lt;/span&gt;&lt;span&gt;true &lt;/span&gt;&lt;span&gt;}.Start();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;protected override bool &lt;/span&gt;&lt;span&gt;TryExecuteTaskInline(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task &lt;span&gt;task, &lt;/span&gt;&lt;span&gt;bool &lt;/span&gt;&lt;span&gt;taskWasPreviouslyQueued) =&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;&lt;span&gt;.TryExecuteTask(task);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The following async function has 2 await expressions, where ConfigureAwait is called with different bool values:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;ConfigureRuntimeContextCapture(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TaskScheduler &lt;/span&gt;taskScheduler1 = &lt;span&gt;TaskScheduler&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;string &lt;/span&gt;contents = &lt;span&gt;await &lt;/span&gt;ReadAsync(readPath).ConfigureAwait(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continueOnCapturedContext: &lt;span&gt;true&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Equivalent to: await ReadAsync(readPath);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Continuation is executed with captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TaskScheduler &lt;/span&gt;taskScheduler2 = &lt;span&gt;TaskScheduler&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;object&lt;/span&gt;.ReferenceEquals(taskScheduler1, taskScheduler2).WriteLine(); &lt;span&gt;// True&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;await &lt;/span&gt;WriteAsync(writePath, contents).ConfigureAwait(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continueOnCapturedContext: &lt;span&gt;false&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Continuation is executed without captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;TaskScheduler &lt;/span&gt;taskScheduler3 = &lt;span&gt;TaskScheduler&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;object&lt;/span&gt;.ReferenceEquals(taskScheduler1, taskScheduler3).WriteLine(); &lt;span&gt;// False&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;To demonstrate the task scheduler capture, call the above async function by specifying the custom task scheduler:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;CallConfigureContextCapture(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;gt; task = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;gt;(() =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ConfigureRuntimeContextCapture(readPath, writePath));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;task.Start(&lt;span&gt;new &lt;/span&gt;&lt;span&gt;BackgroundThreadTaskScheduler&lt;/span&gt;());&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;task.Unwrap(); &lt;span&gt;// Equivalent to: await await task;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Here since async function ConfigureRuntimeContextCapture outputs Task, so the task constructed with () -&amp;gt; Task function is of type Task&amp;lt;Task&amp;gt;. Similarly, if task is constructed with () -&amp;gt; Task&amp;lt;TResult&amp;gt; function, the constructed task is of type Task&amp;lt;Task&amp;lt;TResult&amp;gt;&amp;gt;. For this scenario, Unwrap extension methods are provided to convert nested task to normal task:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static class &lt;/span&gt;&lt;span&gt;TaskExtensions&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;Unwrap(&lt;span&gt;this &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;gt; task);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; Unwrap&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(&lt;span&gt;this &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;&amp;gt; task);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;When start executing ConfigureRuntimeContextCapture, the initial task scheduler is specified to be the custom task scheduler, BackgroundThreadTaskScheduler. In the first await expression, ConfigureAwait is called with true, so that the runtime context information is captured and the continuation is executed with the captured runtime context information. This is the default behaviour, so calling ConfigureAwait with true is equivalent to not calling ConfigureAwait at all. As a result, the first continuation is executed with the same custom task scheduler. In the second await expression, ConfigureAwait is called with false, so the runtime context information is not captured. As a result, the second continuation is executed with the default task scheduler, System.Threading.Tasks.ThreadPoolTaskScheduler.&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The runtime context capture can be also demonstrated by SynchronizationContext. SynchronizationContext is inherited by different implementations in different application models, for example:&lt;/span&gt;&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ASP.NET: System.Web.&lt;span class="MsoHyperlink"&gt;AspNetSynchronizationContext&lt;/span&gt;&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;WPF: System.Windows.Threading.&lt;span class="MsoHyperlink"&gt;DispatcherSynchronizationContext&lt;/span&gt;&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;WinForms: System.Windows.Forms.&lt;span class="MsoHyperlink"&gt;WindowsFormsSynchronizationContext&lt;/span&gt;&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Windows Universal: System.Threading.WinRTCoreDispatcherBasedSynchronizationContext&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Xamarin.Android: Android.App.SyncContext&lt;/p&gt;
&lt;p class="Bullet" style="margin: 6pt 43.1pt 8pt 46.75pt; line-height: 16pt; text-indent: -17.85pt; mso-list: l8 level1 lfo3;"&gt;&lt;span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"&gt;&lt;span style="mso-list: ignore;"&gt;&amp;middot;&lt;span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"&gt;&lt;span style="font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Xamarin.iOS: UIKit.UIKitSynchronizationContext&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Take Windows Universal application as example. In Visual Studio on Windows, create a Windows Universal application, add a button to its UI:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="Code" style="margin: 6pt 0in 8pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;Button &lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;Name&lt;/span&gt;&lt;span&gt;="Button" &lt;/span&gt;&lt;span&gt;Content&lt;/span&gt;&lt;span&gt;="Button" &lt;/span&gt;&lt;span&gt;HorizontalAlignment&lt;/span&gt;&lt;span&gt;="Center" &lt;/span&gt;&lt;span&gt;VerticalAlignment&lt;/span&gt;&lt;span&gt;="Center" &lt;/span&gt;&lt;span&gt;Click&lt;/span&gt;&lt;span&gt;="ButtonClick" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;In the code behind, implement the Click event handler as an async function:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private async void &lt;/span&gt;&lt;span&gt;ButtonClick(&lt;/span&gt;&lt;span&gt;object &lt;/span&gt;&lt;span&gt;sender, &lt;/span&gt;&lt;span&gt;RoutedEventArgs &lt;/span&gt;&lt;span&gt;e)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;SynchronizationContext &lt;/span&gt;synchronizationContext1 = &lt;span&gt;SynchronizationContext&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;ExecutionContext &lt;/span&gt;executionContext1 = &lt;span&gt;ExecutionContext&lt;/span&gt;.Capture();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;await &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;.Delay(&lt;span&gt;TimeSpan&lt;/span&gt;.FromSeconds(1)).ConfigureAwait(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continueOnCapturedContext: &lt;span&gt;true&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Equivalent to: await Task.Delay(TimeSpan.FromSeconds(1));&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Continuation is executed with captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;SynchronizationContext &lt;/span&gt;synchronizationContext2 = &lt;span&gt;SynchronizationContext&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Debug&lt;/span&gt;.WriteLine(synchronizationContext1 == synchronizationContext2); &lt;span&gt;// True&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Button.Background = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;SolidColorBrush&lt;/span&gt;(&lt;span&gt;Colors&lt;/span&gt;.Blue); &lt;span&gt;// UI update works.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;await &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;.Delay(&lt;span&gt;TimeSpan&lt;/span&gt;.FromSeconds(1)).ConfigureAwait(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;continueOnCapturedContext: &lt;span&gt;false&lt;/span&gt;);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Continuation is executed without captured runtime context.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;SynchronizationContext &lt;/span&gt;synchronizationContext3 = &lt;span&gt;SynchronizationContext&lt;/span&gt;.Current;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Debug&lt;/span&gt;.WriteLine(synchronizationContext1 == synchronizationContext3); &lt;span&gt;// False&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.Button.Background = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;SolidColorBrush&lt;/span&gt;(&lt;span&gt;Colors&lt;/span&gt;.Yellow); &lt;span&gt;// UI update fails.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Exception: The application called an interface that was marshalled for a different thread.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;For Windows Universal application, the SynchronizationContext is only available for the UI thread, and application UI can only be updated with UI thread&amp;rsquo;s SynchronizationContext. When the button is clicked, the UI thread calls the async function ButtonClick, so the UI thread&amp;rsquo;s SynchronizationContext is captured. Similar to the previous example, when ConfigureAwait is called with true, the continuation is executed with the previously captured SynchronizationContext, so the continuation can update the UI successfully. When ConfigureAwait is called with true, the continuation is not executed with the SynchronizationContext captured from UI thread, and it fails to update the UI and throws exception.&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151491"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Generalized async function output type and async method builder&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Since C# 7, async function is supported to have any awaitable output type, as long as it has an async method builder specified. Take Func&amp;lt;TResult&amp;gt; is already awaitable with the previously defined GetAwaiter extension method as example, it is already awaitable with the previously defined GetAwaiter extension method, and can be used in await expression just like task. However, it cannot be used with async modifier to be async function output type just like task. To make Func&amp;lt;TResult&amp;gt; output type of async function, the following FuncAwaitable&amp;lt;TResult&amp;gt; type is defined as a wrapper of Func&amp;lt;TResult&amp;gt;. This wrapper is an awaitable type, its GetAwaiter instance method reuses previously defined FuncAwater&amp;lt;TResult&amp;gt; as its awaiter:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;AsyncMethodBuilder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;AsyncFuncAwaitableMethodBuilder&lt;/span&gt;&lt;span&gt;&amp;lt;&amp;gt;))]&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public class &lt;/span&gt;&lt;span&gt;FuncAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;: &lt;span&gt;IAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private readonly &lt;/span&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; function;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;FuncAwaitable(&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; function) =&amp;gt; &lt;span&gt;this&lt;/span&gt;.function = function;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;IAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; GetAwaiter() =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new &lt;/span&gt;&lt;span&gt;FuncAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(&lt;span&gt;Task&lt;/span&gt;.Run(&lt;span&gt;this&lt;/span&gt;.function));&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;This wrapper type is associated with its async method builder with System.Runtime.CompilerServices.AsyncMethodBuilderAttribute. The async method builder for FuncAwaitable&amp;lt;TResult&amp;gt; is implemented as:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;public class &lt;/span&gt;&lt;span&gt;AsyncFuncAwaitableMethodBuilder&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;TResult&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private &lt;/span&gt;&lt;span&gt;AsyncTaskMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; taskMethodBuilder;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private bool &lt;/span&gt;hasResult;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;private bool &lt;/span&gt;useBuilder;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public static &lt;/span&gt;&lt;span&gt;AsyncFuncAwaitableMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; Create() =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;&lt;span&gt;AsyncFuncAwaitableMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;()&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;taskMethodBuilder = &lt;span&gt;AsyncTaskMethodBuilder&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;.Create()&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;public void &lt;/span&gt;Start&amp;lt;&lt;span&gt;TStateMachine&lt;/span&gt;&amp;gt;(&lt;span&gt;ref &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;stateMachine)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;where &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;: &lt;span&gt;IAsyncStateMachine &lt;/span&gt;=&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.Start(&lt;span&gt;ref &lt;/span&gt;stateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;SetStateMachine(&lt;span&gt;IAsyncStateMachine &lt;/span&gt;stateMachine) =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.SetStateMachine(stateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;SetResult(&lt;span&gt;TResult &lt;/span&gt;result)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.useBuilder)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.SetResult(result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.result = result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.hasResult = &lt;span&gt;true&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;public void &lt;/span&gt;SetException(&lt;span&gt;Exception &lt;/span&gt;exception) =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this&lt;/span&gt;.taskMethodBuilder.SetException(exception);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;FuncAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; Task&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(&lt;span&gt;this&lt;/span&gt;.hasResult)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;TResult &lt;/span&gt;result = &lt;span&gt;this&lt;/span&gt;.result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return new &lt;/span&gt;&lt;span&gt;FuncAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(() =&amp;gt; result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.useBuilder = &lt;span&gt;true&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;task = &lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.Task;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return new &lt;/span&gt;&lt;span&gt;FuncAwaitable&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;(() =&amp;gt; task.Result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;&lt;span&gt;public void &lt;/span&gt;AwaitOnCompleted&amp;lt;&lt;span&gt;TAwaiter&lt;/span&gt;, &lt;span&gt;TStateMachine&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ref &lt;/span&gt;&lt;span&gt;TAwaiter &lt;/span&gt;awaiter, &lt;span&gt;ref &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;stateMachine)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;where &lt;/span&gt;&lt;span&gt;TAwaiter &lt;/span&gt;: &lt;span&gt;INotifyCompletion &lt;/span&gt;&lt;span&gt;where &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;: &lt;span&gt;IAsyncStateMachine&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.useBuilder = &lt;span&gt;true&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.AwaitOnCompleted(&lt;span&gt;ref &lt;/span&gt;awaiter, &lt;span&gt;ref &lt;/span&gt;stateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public void &lt;/span&gt;AwaitUnsafeOnCompleted&amp;lt;&lt;span&gt;TAwaiter&lt;/span&gt;, &lt;span&gt;TStateMachine&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;ref &lt;/span&gt;&lt;span&gt;TAwaiter &lt;/span&gt;awaiter, &lt;span&gt;ref &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;stateMachine)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;where &lt;/span&gt;&lt;span&gt;TAwaiter &lt;/span&gt;: &lt;span&gt;ICriticalNotifyCompletion &lt;/span&gt;&lt;span&gt;where &lt;/span&gt;&lt;span&gt;TStateMachine &lt;/span&gt;: &lt;span&gt;IAsyncStateMachine&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.useBuilder = &lt;span&gt;true&lt;/span&gt;;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;this&lt;/span&gt;.taskMethodBuilder.AwaitUnsafeOnCompleted(&lt;span&gt;ref &lt;/span&gt;awaiter, &lt;span&gt;ref &lt;/span&gt;stateMachine);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Now the FuncAwaitable&amp;lt;TResult&amp;gt; type can be output type of async function, just like task:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;FuncAwaitable&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt; &lt;a name="OLE_LINK3"&gt;&lt;/a&gt;AsyncFunctionWithFuncAwaitable&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;T &lt;/span&gt;&lt;span&gt;value)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;.Delay(&lt;span&gt;TimeSpan&lt;/span&gt;.FromSeconds(1));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;value;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Its compilation is in the same pattern as async function with task output. The only difference is, in the generated async state machine, the builder field become the specified AsyncFuncAwaitableMethodBuilder&amp;lt;TResult&amp;gt;, instead of the AsyncTaskMethodBuilder&amp;lt;TResult&amp;gt; for task. And apparently, this async function can be called in the await expression since its output type FuncAwaitable&amp;lt;TResult&amp;gt; is awaitable:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;CallAsyncFunctionWithFuncAwaitable&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;T &lt;/span&gt;&lt;span&gt;value)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;T &lt;/span&gt;result = &lt;span&gt;await &lt;/span&gt;&lt;span&gt;AsyncFunctionWith&lt;/span&gt;FuncAwaitable(value);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;h2 style="margin: 0.25in 0in 17pt; line-height: 19pt;"&gt;&lt;a name="_Toc511151492"&gt;&lt;/a&gt;&lt;span style="font-size: 18pt;"&gt;ValueTask&amp;lt;TResult&amp;gt; and performance&lt;/span&gt;&lt;/h2&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;With the generalized async function output type support, Microsoft also provides a System.Threading.Tasks.ValueTask&amp;lt;TResult&amp;gt; awaitable structure in the System.Threading.Tasks.Extensions NuGet package:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;namespace &lt;/span&gt;&lt;span&gt;System.Threading.Tasks&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[&lt;span&gt;AsyncMethodBuilder&lt;/span&gt;(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;AsyncValueTaskMethodBuilder&lt;/span&gt;&amp;lt;&amp;gt;))]&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;[&lt;span&gt;StructLayout&lt;/span&gt;(&lt;span&gt;LayoutKind&lt;/span&gt;.Auto)]&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public struct &lt;/span&gt;&lt;span&gt;ValueTask&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; : &lt;span&gt;IEquatable&lt;/span&gt;&amp;lt;&lt;span&gt;ValueTask&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt;&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;ValueTask(&lt;span&gt;TResult &lt;/span&gt;result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;ValueTask(&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; task);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;public &lt;/span&gt;&lt;span&gt;ValueTaskAwaiter&lt;/span&gt;&amp;lt;&lt;span&gt;TResult&lt;/span&gt;&amp;gt; GetAwaiter();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;// Other members.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Its awaiter is System.Threading.Tasks.ValueTaskAwaiter&amp;lt;TResult&amp;gt;, and its async method builder is System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder&amp;lt;TResult&amp;gt;, which are provided in the same NuGet package. So ValueTask&amp;lt;TResult&amp;gt; can be used with both await expression and async function. As a value type, it can be allocated and deallocated on stack, with better performance than reference type Task&amp;lt;TResult&amp;gt;. Also, unlike Task&amp;lt;TResult&amp;gt; as a wrapper of Func&amp;lt;TResult&amp;gt; operation, ValueTask&amp;lt;TResult&amp;gt; can be a wrapper of either Func&amp;lt;TResult&amp;gt; operation or TResult result. So ValueTask&amp;lt;TResult&amp;gt; can improve the performance for async function that may have result available before any async operation. The following example downloads data from the specified URI:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;private static readonly &lt;/span&gt;&lt;span&gt;Dictionary&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;byte&lt;/span&gt;&lt;span&gt;[]&amp;gt; Cache = &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;new &lt;/span&gt;&lt;span&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;, &lt;span&gt;byte&lt;/span&gt;[]&amp;gt;(&lt;span&gt;StringComparer&lt;/span&gt;.OrdinalIgnoreCase);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;byte&lt;/span&gt;[]&amp;gt; DownloadAsyncTask(&lt;span&gt;string &lt;/span&gt;uri)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// All code compiled to async state machine. When URI is cached, async state machine is still started.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;if &lt;/span&gt;(Cache.TryGetValue(uri, &lt;span&gt;out byte&lt;/span&gt;[] cachedResult))&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;cachedResult;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;HttpClient &lt;/span&gt;httpClient = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;HttpClient&lt;/span&gt;())&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;byte&lt;/span&gt;[] result = &lt;span&gt;await &lt;/span&gt;httpClient.GetByteArrayAsync(uri);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Cache.Add(uri, result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;It first checks the cache, if the data is already cached for the specified URI, then it outputs the cached data directly, no async operation is needed. However, at compile time, since the function has the async modifier, the entire function body, including the if statement, is compiled into an async state machine. At runtime, a task is always allocated on the heap and should be garbage collected, and the async state machine is always started, even when the result is available in the cache and no async operation is needed. With ValueTask&amp;lt;TResult&amp;gt;, this can be easily optimized:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static &lt;/span&gt;&lt;span&gt;ValueTask&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;byte&lt;/span&gt;&lt;span&gt;[]&amp;gt; DownloadAsyncValueTask(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;uri)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Not compiled to async state machine. When URI is cached, no async state machine is started.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;Cache.TryGetValue(uri, &lt;span&gt;out byte&lt;/span&gt;[] cachedResult)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;? &lt;span&gt;new &lt;/span&gt;&lt;span&gt;ValueTask&lt;/span&gt;&amp;lt;&lt;span&gt;byte&lt;/span&gt;[]&amp;gt;(cachedResult)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: &lt;span&gt;new &lt;/span&gt;&lt;span&gt;ValueTask&lt;/span&gt;&amp;lt;&lt;span&gt;byte&lt;/span&gt;[]&amp;gt;(DownloadAsync());&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;async &lt;/span&gt;&lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;byte&lt;/span&gt;[]&amp;gt; DownloadAsync()&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Compiled to async state machine&lt;a name="OLE_LINK10"&gt;&lt;/a&gt;. When URI is not cached, async state machine is started.&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;HttpClient &lt;/span&gt;httpClient = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;HttpClient&lt;/span&gt;())&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;byte&lt;/span&gt;[] result = &lt;span&gt;await &lt;/span&gt;httpClient.GetByteArrayAsync(uri);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Cache.Add(uri, result);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return &lt;/span&gt;result;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Now the function becomes a sync function with awaitable ValueTask&amp;lt;TResult&amp;gt; output. When the result is available in the cache, the async local function is not called, so there is no async operation or async state machine involved, and there is no task allocated on heap. The async operation is encapsulated in the async local function, which is compiled to async state machine, and is only involved when the result is not available in the cache. As a result, the performance can be improved, especially when the cache is hit frequently.&lt;/span&gt;&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;a name="_Toc511151493"&gt;&lt;/a&gt;&lt;span style="font-size: 20pt;"&gt;Anonymous async function&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="BodyTextFirst" style="margin: 0in 0in 0pt; line-height: 16pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The async and await keywords can be used with the lambda expression syntax for anonymous function. Just like named async function, anonymous async function&amp;rsquo;s output type is task:&lt;/span&gt;&lt;span lang="EN-IN" style="mso-fareast-font-family: 等线;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task &lt;/span&gt;&lt;span&gt;AsyncAnonymousFunction(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;, &lt;span&gt;Task&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;&amp;gt;readAsync = &lt;span&gt;async &lt;/span&gt;path =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;StreamReader &lt;/span&gt;reader = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;StreamReader&lt;/span&gt;(&lt;span&gt;new &lt;/span&gt;&lt;span&gt;FileStream&lt;/span&gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: &lt;span&gt;FileMode&lt;/span&gt;.Open, access: &lt;span&gt;FileAccess&lt;/span&gt;.Read,&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: &lt;span&gt;FileShare&lt;/span&gt;.Read, bufferSize: 4096, useAsync: &lt;span&gt;true&lt;/span&gt;)))&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;return await &lt;/span&gt;reader.ReadToEndAsync();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;string&lt;/span&gt;, &lt;span&gt;string&lt;/span&gt;, &lt;span&gt;Task&lt;/span&gt;&amp;gt;writeAsync = &lt;span&gt;async &lt;/span&gt;(path, contents) =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using &lt;/span&gt;(&lt;span&gt;StreamWriter &lt;/span&gt;writer = &lt;span&gt;new &lt;/span&gt;&lt;span&gt;StreamWriter&lt;/span&gt;(&lt;span&gt;new &lt;/span&gt;&lt;span&gt;FileStream&lt;/span&gt;(&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;path: path, mode: &lt;span&gt;FileMode&lt;/span&gt;.Create, access: &lt;span&gt;FileAccess&lt;/span&gt;.Write,&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;share: &lt;span&gt;FileShare&lt;/span&gt;.Read, bufferSize: 4096, useAsync: &lt;span&gt;true&lt;/span&gt;)))&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;writer.WriteAsync(contents);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;};&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;result = &lt;span&gt;await &lt;/span&gt;readAsync(readPath);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await &lt;/span&gt;writeAsync(writePath, result);&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The above async lambda expressions are compiled as methods of closure class, in the same pattern as normal sync lambda expressions.&lt;/span&gt;&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;Since task can be constructed with anonymous function with any output type, it can be constructed with anonymous async function with task output too:&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task ConstructTaskWithAsyncAnonymousFunction(&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt; text-indent: 9.75pt;"&gt;Task&amp;lt;Task&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;&amp;gt; task1 = &lt;span&gt;new &lt;/span&gt;Task&amp;lt;Task&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span&gt;async &lt;/span&gt;() =&amp;gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;await &lt;/span&gt;ReadAsync(readPath));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;task1.Start(); &lt;span&gt;// Cold task needs to be started.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;contents = &lt;span&gt;await &lt;/span&gt;task1.Unwrap(); &lt;span&gt;// Equivalent to: string contents = await await task1;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task&amp;lt;Task&amp;gt; task2 = &lt;span&gt;new &lt;/span&gt;Task&amp;lt;Task&amp;gt;(&lt;span&gt;async &lt;/span&gt;() =&amp;gt; &lt;span&gt;await &lt;/span&gt;WriteAsync(writePath, &lt;span&gt;null&lt;/span&gt;));&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;task2.Start(); &lt;span&gt;// Cold task needs to be started.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;await &lt;/span&gt;task2.Unwrap(); &lt;span&gt;// Equivalent to: await await task2;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 17.85pt; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;The first task is constructed with anonymous async function of type () &amp;ndash;&amp;gt; Task&amp;lt;string&amp;gt;, so the constructed task is of type Task&amp;lt;Task&amp;lt;string&amp;gt;&amp;gt;. Similarly, the second task is constructed with anonymous async function of type () &amp;ndash;&amp;gt; Task, so the constructed task is of type Task&amp;lt;Task&amp;gt;. As fore mentioned, nested task can be unwrapped and awaited. &lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal static async &lt;/span&gt;&lt;span&gt;Task RunAsync&lt;a name="OLE_LINK4"&gt;&lt;/a&gt;WithAutoUnwrap(&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;readPath, &lt;/span&gt;&lt;span&gt;string &lt;/span&gt;&lt;span&gt;writePath)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Task&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt; task1 = Task.Run(&lt;span&gt;async &lt;/span&gt;() =&amp;gt; &lt;span&gt;await &lt;/span&gt;ReadAsync(readPath)); &lt;span&gt;// Automatically unwrapped.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;string &lt;/span&gt;contents = &lt;span&gt;await &lt;/span&gt;task1; &lt;span&gt;// Hot task.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;Task task2 = Task.Run(&lt;span&gt;async &lt;/span&gt;() =&amp;gt; &lt;span&gt;await &lt;/span&gt;WriteAsync(writePath, contents)); &lt;span&gt;// Automatically unwrapped.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;await &lt;/span&gt;task2; &lt;span&gt;// Hot task.&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;span style="font-size: 20pt;"&gt;Asynchrnous sequence: IAsyncEnumerable&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;In C# 8.0, an async function can return a sequence of values using the return type IAsyncEnumerable&amp;lt;T&amp;gt;:&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;namespace System.Collections.Generic&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public interface IAsyncEnumerable&amp;lt;out T&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;IAsyncEnumerator&amp;lt;T&amp;gt; GetAsyncEnumerator(CancellationToken cancellationToken = default);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public interface IAsyncEnumerator&amp;lt;out T&amp;gt; : IAsyncDisposable&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;T Current { get; }&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ValueTask&amp;lt;bool&amp;gt; MoveNextAsync();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;The following example define a async function that generates a sequence of values by using yield statement:&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span&gt;internal&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;async&lt;/span&gt; IAsyncEnumerable&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt;DownloadAsync(IEnumerable&amp;lt;Uri&amp;gt; uris)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;using&lt;/span&gt; WebClient webClient = &lt;span&gt;new&lt;/span&gt; WebClient();&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;foreach&lt;/span&gt; (Uri uri &lt;span&gt;in&lt;/span&gt; uris)&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;string&lt;/span&gt; webPage = &lt;span&gt;await&lt;/span&gt; webClient.DownloadStringTaskAsync(uri);&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;yield&lt;/span&gt; &lt;span&gt;return&lt;/span&gt; webPage;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;}&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;The following example consumes an async sequence by pulling the values asynchronously using async foreach statement:&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;internal&lt;/span&gt;&lt;span style="mso-fareast-language: en-in;"&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;async&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; PrintDownloadAsync(IEnumerable&amp;lt;Uri&amp;gt; uris)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;IAsyncEnumerable&amp;lt;&lt;span&gt;string&lt;/span&gt;&amp;gt; webPages = DownloadAsync(uris);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span&gt;await&lt;/span&gt; &lt;span&gt;foreach&lt;/span&gt; (&lt;span&gt;string&lt;/span&gt; webPage &lt;span&gt;in&lt;/span&gt; webPages)&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Trace.WriteLine(webPage);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;In above 2 async functions, the code are both compiled to a state macine similar to async functions returning a task.&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;span style="font-size: 20pt;"&gt;async using declaration: IAsyncDispose&lt;/span&gt;&lt;/h1&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;In C# 8.0, an instance can be decared with async using keywords, if its type implements System.IAsyncDispose interface:&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;namespace System&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public interface IAsyncDisposable&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ValueTask DisposeAsync();&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;For example, FileStream type implements IAsyncDispose:&lt;/p&gt;
&lt;p class="CodeCxSpFirst" style="margin: 6pt 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;internal&lt;span&gt;&lt;/span&gt;static&lt;span&gt;&lt;/span&gt;async&lt;span&gt;&lt;/span&gt;void&lt;span&gt; AsyncUsing(&lt;/span&gt;string&lt;span&gt; file)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;{&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="mso-fareast-language: en-in;"&gt;await&lt;span&gt;&lt;/span&gt;using&lt;span&gt; FileStream fileStream = File.OpenRead(file);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpMiddle" style="margin: 0in 0in 0pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Trace.WriteLine(fileStream.Length);&lt;/span&gt;&lt;/p&gt;
&lt;p class="CodeCxSpLast" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span style="mso-fareast-language: en-in;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;The code in the above async function is also compiled to a state machine.&lt;/p&gt;
&lt;h1 style="margin: 0.25in 0in 17pt; line-height: 21pt;"&gt;&lt;span style="font-size: 20pt;"&gt;Summary&lt;/span&gt;&lt;/h1&gt;
&lt;p align="justify" class="MsoBodyText" style="margin: 0in 0in 0pt; line-height: 16pt; text-indent: 0in; -ms-text-justify: inter-ideograph;"&gt;&lt;span lang="EN-IN"&gt;C# supports async function as a simplified asynchronous programming model. In C#, async operation is represented by Task or Task&amp;lt;TResult&amp;gt;. A task can be async function output type, and can be used in await expression. Besides task, any type following the awaitable-awaiter pattern can be used in await expression. Async function is compiled to async state machine. It can capture the caller&amp;rsquo;s runtime context, and execute the continuation code with the captured context. C# generalizes async function output type with async method builder support, and a ValueTask&amp;lt;TResult&amp;gt; type is provided to improve async function performance. C# also support async local function and async anonymous function.&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0in 0in 8pt; line-height: 12pt;"&gt;&lt;span lang="EN-IN" style="mso-ansi-language: en-in;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Tue, 04 Mar 2025 12:58:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/functional-csharp-asynchronous-function</guid><category>.NET</category><category>C#</category><category>C# 5.0</category><category>LINQ</category><category>LINQ via C#</category><category>C# Features</category><category>Functional Programming</category><category>Functional C#</category></item><item><title>Functional Programming and LINQ via C#</title><link>https://weblogs.asp.net:443/dixin/fp-csharp</link><description>&lt;p&gt;&lt;img width="1200" height="800" title="ezgif-1-93576e9d87" style="border: 0px currentcolor; display: inline; background-image: none;" alt="ezgif-1-93576e9d87" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/72436fc4c617_10FA4/ezgif-1-93576e9d87_3.jpg" border="0" /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#acclaim"&gt;Acclaim&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#contentsataglance"&gt;Contents at a Glance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#contents"&gt;Table of Contents&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is a book on functional programming and LINQ programming via C# language. It discusses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Functional programming via C# in-depth&lt;/li&gt;
&lt;li&gt;Use functional LINQ to work with local data and cloud data&lt;/li&gt;
&lt;li&gt;The underlying mathematics theories of functional programming and LINQ, including Lambda Calculus and Category Theory&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="acclaim"&gt;Acclaim&lt;/h1&gt;
&lt;p&gt;Microsoft:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;An excellent book for those of us who need to get in-depth understanding on LINQ and functional programming with latest C# language. The author made sure this book includes the latest and cross-platform knowledge for the language, the framework, as well as the underlying mathematical theories.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Hongfei Guo&lt;br /&gt;Partner Group Engineering Manager at Microsoft&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This book explains practical and in-depth material clearly, concisely and accurately to the areas of the C# language, functional programming, and LINQ on .NET Framework and .NET Core. This is a great book for anyone wanting to understand the whys and hows behind these important technologies.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Samer Boshra&lt;br /&gt;Principal Software Development Engineer at Microsoft&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This is a great book for developers who want to go functional programming. It's one-stop shopping for serious developers who have to get up to speed with LINQ and functional programming quickly and in-depth. I'll keep this book on my desk not on my bookshelf.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Roshan Kommusetty&lt;br /&gt;Principal Software Engineering Manager at Microsoft&lt;/strong&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This is a great book for C# developers, it covers both basic C# programming concepts for the beginners new to the .NET world, and C# advanced constructs for experienced .NET programmers. The book is up to date, talks C# 7.0 new language features and demonstrates how you can use them for functional programming. Thanks for the awesome work!&amp;rdquo; &lt;br /&gt;&lt;strong&gt;Mark Zhou&lt;br /&gt;Principal Software Engineering Manager at Microsoft&amp;nbsp; &lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;I like the way the author presented the detailed knowledge with a lot of examples. As a data scientist with statistics background in a number of industries, I can pick up C# programming and LINQ quickly when I followed the book. The book was concise and easy to read. It was a pleasant experience for me to spend my time emerging myself in the book in the sunshine weekday afternoon.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Xue Liu&lt;br /&gt;Senior Data Scientist at Microsoft&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Functional Programming and LINQ in C# language, have been fully and clearly unraveled in this book, with many practical examples.&amp;nbsp; The author has not saved any effort to go beyond scratching the surface of C# language and has successfully explained the magic behind the scene. This book is a must-have for anyone who wants to understand functional programming using C#.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Jie Mei&lt;br /&gt;Data &amp;amp; Applied Scientist at Microsoft&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Academy and more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;This book provides comprehensive and in-depth information about the C# functional programming and LINQ technologies to application developers on both .NET Framework and .NET Core. The detailed text and wealth of examples will give a developer a clear and solid understanding of C# language, functional programming and using LINQ to work with different data domains.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Dong Si&lt;br /&gt;Assistant Professor, Department of Computer Science, University of Washington, Bothell&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This book offers a comprehensive, in-depth, yet easy-to-understand tutorial to functional C# programming and LINQ. Filled with detailed explanations and real-world examples, this book is highly valuable for beginners and experienced developers alike.&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Shuang Zhao&lt;br /&gt;Assistant Professor, Department of Computer Science, University of California, Irvine&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This excellent book is an in-depth and also readable exploration of C# functional programming and LINQ programming. It covers .NET Framework and .NET Core in great detail.&amp;rdquo; &lt;br /&gt;&lt;strong&gt;Yang Sha&lt;br /&gt;Engineering Manager at Google&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Great book! It takes a hands-on approach to LINQ and functional programming in an easy to understand format. I would highly recommend this book to developers looking to develop expertise in C#, functional programming, and LINQ.&amp;rdquo;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;strong&gt;Himanshu Lal&lt;br /&gt;Software Engineering Manager at Facebook&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This is a great book that combines practical examples with in-depth analysis of LINQ and functional programming in C#. Dixin leverages his expertise in .NET to provide a well written tutorial on the effective use of LINQ and an overview of the theoretical principles behind it. A must read for anyone working on these technologies!&amp;rdquo; &lt;strong&gt;&lt;br /&gt;Dimitrios Soulios&lt;br /&gt;Director at Goldman Sachs&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id="contentsataglance"&gt;Contents at a Glance&lt;/h1&gt;
&lt;p&gt;The contents are organized as the following chapters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Part 1 Code &lt;/b&gt;- covers functional programming via C#, and fundamentals of LINQ.
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Chapter 1 Functional programming and LINQ paradigm&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;What is LINQ, how LINQ uses language to work with many different data domains.&lt;/li&gt;
&lt;li&gt;Programming paradigm, imperative vs. declarative programming, object-oriented vs. functional programming.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chapter 2 Functional programming in depth&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;C# fundamentals for beginners.&lt;/li&gt;
&lt;li&gt;Aspects of functional programming via C#, including function type, named/anonymous/local function, closure, lambda, higher-order function, currying, partial application, first class function, function composition, query expression, covariance/contravariance, immutability, tuple, purity, async function, pattern matching, etc., including how C# is processed at compile time and runtime.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Part 2 Data&lt;/b&gt; - covers how to use functional LINQ to work with different data domains in the real world, and how LINQ works internally.
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Chapter 3 LINQ to Objects&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;How to use functional LINQ queries to work with objects, covering all LINQ and Ix.&lt;/li&gt;
&lt;li&gt;How the LINQ to Objects query methods are implemented, how to implement useful custom LINQ queries.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chapter 4 LINQ to XML&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;How to modeling XML data, and use functional LINQ queries to work with XML data.&lt;/li&gt;
&lt;li&gt;How to use the other LINQ to XML APIs to manipulate XML data.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chapter 5 Parallel LINQ&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;How to use parallelized functional LINQ queries to work with objects.&lt;/li&gt;
&lt;li&gt;Performance analysis for parallel/sequential LINQ queries.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chapter 6 Entity Framework/Core and LINQ to Entities&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;How to model database with object-relational mapping, and use functional LINQ queries to work with relational data in database.&lt;/li&gt;
&lt;li&gt;How the C# LINQ to Entities queries are implemented to work with database.&lt;/li&gt;
&lt;li&gt;How to change data in database, and handle concurrent conflicts.&lt;/li&gt;
&lt;li&gt;Performance tips and asynchrony.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Part 3 Theories&lt;/b&gt; - demystifies the abstract mathematics theories, which are the rationale and foundations of LINQ and functional programming.
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Chapter 7 Lambda Calculus via C#&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;Core concepts of lambda calculus, bound and free variables, reduction (&amp;alpha;-conversion, &amp;beta;-reduction, &amp;eta;-conversion), etc.&lt;/li&gt;
&lt;li&gt;How to use lambda functions to represent values, data structures and computation, including Church Boolean, Church numbers, Church pair, Church list, and their operations.&lt;/li&gt;
&lt;li&gt;Combinators and combinatory logic, including SKI combinator calculus, fixed point combinator for function recursion, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Chapter 8 Category Theory via C#&lt;/b&gt;
&lt;ul&gt;
&lt;li&gt;Core concepts of category theory, including category, object, morphism, monoid, functor, natural transformation, applicative functor, monad, and their laws.&lt;/li&gt;
&lt;li&gt;How these concepts are applied in functional programming and LINQ.&lt;/li&gt;
&lt;li&gt;How to manage I/O, state, exception handling, shared environment, logging, and continuation, etc., in functional programming.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tutorial delivers highly reusable knowledge:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It covers C# language in depth, which can be generally applied in any programming paradigms besides functional programming.&lt;/li&gt;
&lt;li&gt;It is a cross platform tutorial, covering both .NET Framework for Windows and .NET Core for Windows, Mac, Linux.&lt;/li&gt;
&lt;li&gt;It demonstrates both usage and implementation of LINQ for mainstream data domains, which also enables developer to use the LINQ technologies for other data domains, or build custom LINQ APIs for specific data scenarios.&lt;/li&gt;
&lt;li&gt;It also demystifies the abstract mathematics knowledge for functional programming, which applies to general functional programming, so it greatly helps developers understanding any other functional languages too.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a fun of functional programming, LINQ, C#, and .NET technologies, hope this helps.&lt;/p&gt;
&lt;h1 id="contents"&gt;Table of Contents&lt;/h1&gt;
&lt;p&gt;All code examples are available on GitHub: https://github.com/Dixin/CodeSnippets.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Introducing%20LINQ" target="_blank"&gt;Functional programming and LINQ paradigm&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp-introduction" target="_blank"&gt;Cross platform C# and .NET&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Introducing cross platform .NET, C# and LINQ
&lt;ul&gt;
&lt;li&gt;.NET Framework, C#, and LINQ&lt;/li&gt;
&lt;li&gt;.NET Core, UWP, Mono, Xamarin and Unity&lt;/li&gt;
&lt;li&gt;.NET Standard&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Introducing this book
&lt;ul&gt;
&lt;li&gt;Book structure&lt;/li&gt;
&lt;li&gt;Code examples&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Start coding
&lt;ul&gt;
&lt;li&gt;Start coding with Visual Studio (Windows)&lt;/li&gt;
&lt;li&gt;Start coding with Visual Studio Code (Windows, macOS and Linux)&lt;/li&gt;
&lt;li&gt;Start coding with Visual Studio for Mac (macOS)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/introducing-linq-3-waht-is-functional-programming" target="_blank"&gt;Programming paradigms and functional programming&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Programming paradigms&lt;/li&gt;
&lt;li&gt;Imperative programming vs. declarative programming&lt;/li&gt;
&lt;li&gt;Object-oriented programming vs. functional programming&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/introducing-linq-2-what-is-linq" target="_blank"&gt;LINQ to data sources&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;One language for different data domains
&lt;ul&gt;
&lt;li&gt;LINQ to Objects&lt;/li&gt;
&lt;li&gt;Parallel LINQ&lt;/li&gt;
&lt;li&gt;LINQ to XML&lt;/li&gt;
&lt;li&gt;LINQ to DataSets&lt;/li&gt;
&lt;li&gt;LINQ to Entities&lt;/li&gt;
&lt;li&gt;LINQ to SQL&lt;/li&gt;
&lt;li&gt;LINQ to NoSQL (LINQ to CosmosDB)&lt;/li&gt;
&lt;li&gt;LINQ to JSON&lt;/li&gt;
&lt;li&gt;LINQ to Twitter&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Sequential query vs. parallel query&lt;/li&gt;
&lt;li&gt;Local query vs. remote query&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Functional%20C%23" target="_blank"&gt;Functional programming in depth&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-fundamentals" target="_blank"&gt;C# language basics&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Types and members
&lt;ul&gt;
&lt;li&gt;Types and members&lt;/li&gt;
&lt;li&gt;Built-in types&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Reference type vs. value type
&lt;ul&gt;
&lt;li&gt;ref local variable and immutable ref local variable&lt;/li&gt;
&lt;li&gt;Array and stack-allocated array&lt;/li&gt;
&lt;li&gt;Default value&lt;/li&gt;
&lt;li&gt;ref structure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Static class&lt;/li&gt;
&lt;li&gt;Partial type&lt;/li&gt;
&lt;li&gt;Interface and implementation
&lt;ul&gt;
&lt;li&gt;IDisposable interface and using declaration&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Generic type
&lt;ul&gt;
&lt;li&gt;Type parameter&lt;/li&gt;
&lt;li&gt;Type parameter constraints&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Nullable value type&lt;/li&gt;
&lt;li&gt;Auto property&lt;/li&gt;
&lt;li&gt;Property initializer&lt;/li&gt;
&lt;li&gt;Object initializer&lt;/li&gt;
&lt;li&gt;Collection initializer&lt;/li&gt;
&lt;li&gt;Index initializer&lt;/li&gt;
&lt;li&gt;Null coalescing operator&lt;/li&gt;
&lt;li&gt;Null conditional operator&lt;/li&gt;
&lt;li&gt;throw expression&lt;/li&gt;
&lt;li&gt;Exception filter&lt;/li&gt;
&lt;li&gt;String interpolation&lt;/li&gt;
&lt;li&gt;nameof operator&lt;/li&gt;
&lt;li&gt;Digit separator and leading underscore&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-function-type-and-delegate" target="_blank"&gt;Named function and function polymorphism&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Constructor, static constructor and finalizer&lt;/li&gt;
&lt;li&gt;Static method and instance method&lt;/li&gt;
&lt;li&gt;Extension method&lt;/li&gt;
&lt;li&gt;More named functions&lt;/li&gt;
&lt;li&gt;Function polymorphisms
&lt;ul&gt;
&lt;li&gt;Ad hoc polymorphism: method overload&lt;/li&gt;
&lt;li&gt;Parametric polymorphism: generic method
&lt;ul&gt;
&lt;li&gt;Type argument inference&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Static import&lt;/li&gt;
&lt;li&gt;Partial method&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-local-function-and-closure" target="_blank"&gt;Local function and closure&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Local function&lt;/li&gt;
&lt;li&gt;Closure
&lt;ul&gt;
&lt;li&gt;Outer variable&lt;/li&gt;
&lt;li&gt;Implicit reference&lt;/li&gt;
&lt;li&gt;Static local function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-function-parameter-and-return-value" target="_blank"&gt;Function input and output&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Input by copy vs. input by alias (ref parameter)
&lt;ul&gt;
&lt;li&gt;Input by immutable alias (in parameter)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Output parameter (out parameter) and out variable
&lt;ul&gt;
&lt;li&gt;Discarding out variable&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Parameter array&lt;/li&gt;
&lt;li&gt;Positional argument vs. named argument&lt;/li&gt;
&lt;li&gt;Required parameter vs. optional parameter&lt;/li&gt;
&lt;li&gt;Caller information parameter&lt;/li&gt;
&lt;li&gt;Output by copy vs. output by alias
&lt;ul&gt;
&lt;li&gt;Output by immutable alias&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-local-function-and-closure" target="_blank"&gt;Delegate: Function type, instance, and group&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Delegate type as function type
&lt;ul&gt;
&lt;li&gt;Function type&lt;/li&gt;
&lt;li&gt;Generic delegate type&lt;/li&gt;
&lt;li&gt;Unified built-in delegate types&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Delegate instance as function instance&lt;/li&gt;
&lt;li&gt;Delegate instance as function group
&lt;ul&gt;
&lt;li&gt;Event and event handler&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-anonymous-function-and-lambda-expression" target="_blank"&gt;Anonymous function and lambda expression&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Anonymous method&lt;/li&gt;
&lt;li&gt;Lambda expression as anonymous function
&lt;ul&gt;
&lt;li&gt;IIFE (Immediately-invoked function expression)&lt;/li&gt;
&lt;li&gt;Closure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Expression bodied function member&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-function-as-data-and-expression-tree" target="_blank"&gt;Expression tree: Function as data&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Lambda expression as expression tree
&lt;ul&gt;
&lt;li&gt;Metaprogramming: function as abstract syntax tree&lt;/li&gt;
&lt;li&gt;.NET expressions&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Compile expression tree at runtime
&lt;ul&gt;
&lt;li&gt;Traverse expression tree&lt;/li&gt;
&lt;li&gt;Expression tree to CIL at runtime&lt;/li&gt;
&lt;li&gt;Expression tree to executable function at runtime&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Expression tree and LINQ remote query&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-higher-order-function-currying-and-first-class-function" target="_blank"&gt;Higher-order function, currying and first class function&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;First order function vs. higher-order function
&lt;ul&gt;
&lt;li&gt;Convert first-order function to higher-order function&lt;/li&gt;
&lt;li&gt;Lambda operator =&amp;gt; associativity&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Curry function
&lt;ul&gt;
&lt;li&gt;Uncurry function&lt;/li&gt;
&lt;li&gt;Partial applying function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;First-class function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-function-composition-and-method-chaining" target="_blank"&gt;Function composition and chaining&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Forward composition vs. backward composition&lt;/li&gt;
&lt;li&gt;Forward piping&lt;/li&gt;
&lt;li&gt;Method chaining and fluent interface&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-query-expression" target="_blank"&gt;LINQ query expression&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Syntax and compilation&lt;/li&gt;
&lt;li&gt;Query expression pattern&lt;/li&gt;
&lt;li&gt;LINQ query expression
&lt;ul&gt;
&lt;li&gt;Forward piping with LINQ&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Query expression vs. query method&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-covariance-and-contravariance" target="_blank"&gt;Covariance and contravariance&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Subtyping and type polymorphism&lt;/li&gt;
&lt;li&gt;Variances of non-generic function type&lt;/li&gt;
&lt;li&gt;Variances of generic function type&lt;/li&gt;
&lt;li&gt;Variances of generic interface&lt;/li&gt;
&lt;li&gt;Variances of generic higher-order function type&lt;/li&gt;
&lt;li&gt;Covariance of array&lt;/li&gt;
&lt;li&gt;Variances in .NET and LINQ&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-immutability-anonymous-type-and-tuple" target="_blank"&gt;Immutability, anonymous type and tuple&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Immutable value
&lt;ul&gt;
&lt;li&gt;Constant local&lt;/li&gt;
&lt;li&gt;Enumeration&lt;/li&gt;
&lt;li&gt;using declaration and foreach statement&lt;/li&gt;
&lt;li&gt;Immutable alias (immutable ref local variable)&lt;/li&gt;
&lt;li&gt;Function&amp;rsquo;s immutable input and immutable output&lt;/li&gt;
&lt;li&gt;Range variable in LINQ query expression&lt;/li&gt;
&lt;li&gt;this reference for class&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Immutable state (immutable type)
&lt;ul&gt;
&lt;li&gt;Constant field&lt;/li&gt;
&lt;li&gt;Immutable class with readonly instance field&lt;/li&gt;
&lt;li&gt;Immutable structure (readonly structure)&lt;/li&gt;
&lt;li&gt;Immutable anonymous type
&lt;ul&gt;
&lt;li&gt;Local variable type inference&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Immutable tuple vs. mutable tuple
&lt;ul&gt;
&lt;li&gt;Construction, element name and element inference&lt;/li&gt;
&lt;li&gt;Deconstruction&lt;/li&gt;
&lt;li&gt;Tuple assignment&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Immutable collection vs. readonly collection&lt;/li&gt;
&lt;li&gt;Shallow immutability vs. deep immutability&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-pure-function" target="_blank"&gt;Pure function&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Pure function vs. impure function
&lt;ul&gt;
&lt;li&gt;Referential transparency and side effect free&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Purity in .NET&lt;/li&gt;
&lt;li&gt;Purity in LINQ&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-asynchronous-function" target="_blank"&gt;Asynchronous function&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Task, Task&amp;lt;TResult&amp;gt; and asynchrony&lt;/li&gt;
&lt;li&gt;Named async function&lt;/li&gt;
&lt;li&gt;Awaitable-awaiter pattern&lt;/li&gt;
&lt;li&gt;Async state machine&lt;/li&gt;
&lt;li&gt;Runtime context capture&lt;/li&gt;
&lt;li&gt;Generalized async return type and async method builder
&lt;ul&gt;
&lt;li&gt;ValueTask&amp;lt;TResult&amp;gt; and performance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Anonymous async function&lt;/li&gt;
&lt;li&gt;Asynchronous sequence: IAsyncEnumerable&amp;lt;T&amp;gt;&lt;/li&gt;
&lt;li&gt;async using declaration: IAsyncDispose&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/functional-csharp-pattern-matching" target="_blank"&gt;Pattern matching&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is expression&lt;/li&gt;
&lt;li&gt;switch statement and switch expression&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/LINQ%20to%20Objects" target="_blank"&gt;LINQ to Objects: Querying objects in memory&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-local-sequential-query" target="_blank"&gt;Local sequential LINQ query&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Iteration pattern and foreach statement&lt;/li&gt;
&lt;li&gt;IEnumerable&amp;lt;T&amp;gt; and IEnumerator&amp;lt;T&amp;gt;
&lt;ul&gt;
&lt;li&gt;foreach loop vs. for loop&lt;/li&gt;
&lt;li&gt;Non-generic sequence vs. generic sequence&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;LINQ to Objects queryable types&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-query-methods-operators-and-query-expressions" target="_blank"&gt;LINQ to Objects standard queries and query expressions&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Sequence queries
&lt;ul&gt;
&lt;li&gt;Generation: Empty , Range, Repeat, DefaultIfEmpty&lt;/li&gt;
&lt;li&gt;Filtering (restriction): Where, OfType, where&lt;/li&gt;
&lt;li&gt;Mapping (projection): Select, SelectMany, from, let, select&lt;/li&gt;
&lt;li&gt;Grouping: GroupBy, group, by, into&lt;/li&gt;
&lt;li&gt;Join
&lt;ul&gt;
&lt;li&gt;Inner join: Join, SelectMany, join, on, equals&lt;/li&gt;
&lt;li&gt;Outer join: GroupJoin, join, into, on, equals&lt;/li&gt;
&lt;li&gt;Cross join: SelectMany, Join, from select, join, on, equals&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Concatenation: Concat&lt;/li&gt;
&lt;li&gt;Set: Distinct, Union, Intersect, Except&lt;/li&gt;
&lt;li&gt;Convolution: Zip&lt;/li&gt;
&lt;li&gt;Partitioning: Take, Skip, TakeWhile, SkipWhile&lt;/li&gt;
&lt;li&gt;Ordering: OrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse, orderby, ascending, descending, into&lt;/li&gt;
&lt;li&gt;Conversion: Cast, AsEnumerable&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Collection queries
&lt;ul&gt;
&lt;li&gt;Conversion: ToArray, ToList, ToDictionary, ToLookup&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Value queries
&lt;ul&gt;
&lt;li&gt;Element: First, FirstOrDefault, Last, LastOrDefault, ElementAt, ElementAtOrDefault, Single, SingleOrDefault&lt;/li&gt;
&lt;li&gt;Aggregation: Aggregate, Count, LongCount, Min, Max, Sum, Average&lt;/li&gt;
&lt;li&gt;Quantifier: All, Any, Contains&lt;/li&gt;
&lt;li&gt;Equality: SequenceEqual&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Queries in other languages&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-generator" target="_blank"&gt;Generator&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Implementing iterator pattern&lt;/li&gt;
&lt;li&gt;Generating sequence and iterator&lt;/li&gt;
&lt;li&gt;Yield statement and generator&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-deferred-execution-lazy-evaluation-and-eager-evaluation" target="_blank"&gt;Deferred execution, lazy evaluation and eager Evaluation&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Immediate execution vs. Deferred execution
&lt;ul&gt;
&lt;li&gt;Cold IEnumerable&amp;lt;T&amp;gt; vs. hot IEnumerable&amp;lt;T&amp;gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Lazy evaluation vs. eager evaluation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-query-methods-implementation" target="_blank"&gt;LINQ to Objects internals: Standard queries implementation&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Argument check and deferred execution&lt;/li&gt;
&lt;li&gt;Collection queries
&lt;ul&gt;
&lt;li&gt;Conversion: ToArray, ToList, ToDictionary, ToLookup&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Sequence queries
&lt;ul&gt;
&lt;li&gt;Conversion: Cast, AsEnumerable&lt;/li&gt;
&lt;li&gt;Generation: Empty , Range, Repeat, DefaultIfEmpty&lt;/li&gt;
&lt;li&gt;Filtering (restriction): Where, OfType&lt;/li&gt;
&lt;li&gt;Mapping (projection): Select, SelectMany&lt;/li&gt;
&lt;li&gt;Grouping: GroupBy&lt;/li&gt;
&lt;li&gt;Join: SelectMany, Join, GroupJoin&lt;/li&gt;
&lt;li&gt;Concatenation: Concat&lt;/li&gt;
&lt;li&gt;Set: Distinct, Union, Intersect, Except&lt;/li&gt;
&lt;li&gt;Convolution: Zip&lt;/li&gt;
&lt;li&gt;Partitioning: Take, Skip, TakeWhile, SkipWhile&lt;/li&gt;
&lt;li&gt;Ordering: OrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Value queries
&lt;ul&gt;
&lt;li&gt;Element: First, FirstOrDefault, Last, LastOrDefault, ElementAt, ElementAtOrDefault, Single, SingleOrDefault&lt;/li&gt;
&lt;li&gt;Aggregation: Aggregate, Count, LongCount, Min, Max, Sum, Average&lt;/li&gt;
&lt;li&gt;Quantifier: All, Any, Contains&lt;/li&gt;
&lt;li&gt;Equality: SequenceEqual&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-interactive-extensions-ix" target="_blank"&gt;Advanced queries in Microsoft Interactive Extensions (Ix)&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Sequence queries
&lt;ul&gt;
&lt;li&gt;Generation: Defer, Create, Return, Repeat&lt;/li&gt;
&lt;li&gt;Filtering: IgnoreElements, DistinctUntilChanged&lt;/li&gt;
&lt;li&gt;Mapping: SelectMany, Scan, Expand&lt;/li&gt;
&lt;li&gt;Concatenation: Concat, StartWith&lt;/li&gt;
&lt;li&gt;Set: Distinct&lt;/li&gt;
&lt;li&gt;Partitioning: TakeLast, SkipLast&lt;/li&gt;
&lt;li&gt;Conversion: Hide&lt;/li&gt;
&lt;li&gt;Buffering: Buffer, Share, Publish, Memoize&lt;/li&gt;
&lt;li&gt;Exception: Throw, Catch, Finally, OnErrorResumeNext, Retry&lt;/li&gt;
&lt;li&gt;Imperative: If, Case, Using, While, DoWhile, Generate, For&lt;/li&gt;
&lt;li&gt;Iteration: Do&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Value queries
&lt;ul&gt;
&lt;li&gt;Aggregation: Min, Max, MinBy, MaxBy&lt;/li&gt;
&lt;li&gt;Quantifiers: isEmpty&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Void queries
&lt;ul&gt;
&lt;li&gt;Iteration: ForEach&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-objects-custom-query-methods" target="_blank"&gt;Building custom queries&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Sequence queries (deferred execution)
&lt;ul&gt;
&lt;li&gt;Generation: Create, RandomInt32, RandomDouble, FromValue, FromValues, EmptyIfNull&lt;/li&gt;
&lt;li&gt;Filtering: Timeout&lt;/li&gt;
&lt;li&gt;Concatenation: Join, Append, Prepend, AppendTo, PrependTo&lt;/li&gt;
&lt;li&gt;Partitioning: Subsequence&lt;/li&gt;
&lt;li&gt;Exception: Catch, Retry&lt;/li&gt;
&lt;li&gt;Comparison: OrderBy, OrderByDescending, ThenBy, ThenByDescending, GroupBy, Join, GroupJoin, Distinct, Union, Intersect, Except&lt;/li&gt;
&lt;li&gt;List: Insert, Remove, RemoveAll, RemoveAt&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Collection queries
&lt;ul&gt;
&lt;li&gt;Comparison: ToDictionary, ToLookup&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Value queries
&lt;ul&gt;
&lt;li&gt;List: IndexOf, LastIndexOf&lt;/li&gt;
&lt;li&gt;Aggregation: PercentileExclusive, PercentileInclusive, Percentile&lt;/li&gt;
&lt;li&gt;Quantifiers: IsNullOrEmpty, IsNotNullOrEmpty&lt;/li&gt;
&lt;li&gt;Comparison: Contains, SequenceEqual&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Void queries
&lt;ul&gt;
&lt;li&gt;Iteration: ForEach&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/LINQ%20to%20XML" target="_blank"&gt;LINQ to XML: Querying XML&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-xml-1-modeling-xml" target="_blank"&gt;Modeling XML&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Imperative vs. declarative paradigm&lt;/li&gt;
&lt;li&gt;Types, conversions and operators&lt;/li&gt;
&lt;li&gt;Read and deserialize XML&lt;/li&gt;
&lt;li&gt;Serialize and write XML&lt;/li&gt;
&lt;li&gt;Deferred construction&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-xml-2-query-methods" target="_blank"&gt;LINQ to XML standard queries&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Navigation&lt;/li&gt;
&lt;li&gt;Ordering&lt;/li&gt;
&lt;li&gt;Comparison&lt;/li&gt;
&lt;li&gt;More useful queries&lt;/li&gt;
&lt;li&gt;XPath
&lt;ul&gt;
&lt;li&gt;Generate XPath expression&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/linq-to-xml-3-manipulating-xml" target="_blank"&gt;Manipulating XML&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Clone&lt;/li&gt;
&lt;li&gt;Adding, deleting, replacing, updating, and events&lt;/li&gt;
&lt;li&gt;Annotation&lt;/li&gt;
&lt;li&gt;Validating XML with XSD&lt;/li&gt;
&lt;li&gt;Transforming XML with XSL&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Parallel%20LINQ" target="_blank"&gt;Parallel LINQ: Querying objects in parallel&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/parallel-linq-1-local-parallel-query-and-visualization" target="_blank"&gt;Parallel LINQ query and visualization&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Parallel query vs. sequential query&lt;/li&gt;
&lt;li&gt;Parallel query execution&lt;/li&gt;
&lt;li&gt;Visualizing parallel query execution
&lt;ul&gt;
&lt;li&gt;Using Concurrency Visualizer&lt;/li&gt;
&lt;li&gt;Visualizing sequential and parallel LINQ queries&lt;/li&gt;
&lt;li&gt;Visualizing chaining query methods&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/parallel-linq-2-partitioning" target="_blank"&gt;Parallel LINQ internals: data partitioning&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Partitioning and load balancing
&lt;ul&gt;
&lt;li&gt;Range partitioning&lt;/li&gt;
&lt;li&gt;Chunk partitioning&lt;/li&gt;
&lt;li&gt;Hash partitioning&lt;/li&gt;
&lt;li&gt;Stripped partitioning&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Implement custom partitioner
&lt;ul&gt;
&lt;li&gt;Static partitioner&lt;/li&gt;
&lt;li&gt;Dynamic partitioner&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/parallel-linq-3-query-methods" target="_blank"&gt;Parallel LINQ standard queries&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Query settings
&lt;ul&gt;
&lt;li&gt;Cancellation&lt;/li&gt;
&lt;li&gt;Degree of parallelism&lt;/li&gt;
&lt;li&gt;Execution mode&lt;/li&gt;
&lt;li&gt;Merge the values&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Ordering
&lt;ul&gt;
&lt;li&gt;Preserving the order&lt;/li&gt;
&lt;li&gt;Order and correctness&lt;/li&gt;
&lt;li&gt;Orderable partitioner&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Aggregation
&lt;ul&gt;
&lt;li&gt;Commutativity, associativity and correctness&lt;/li&gt;
&lt;li&gt;Partitioning and merging&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/parallel-linq-4-performance" target="_blank"&gt;Parallel query performance&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Sequential query vs. parallel query&lt;/li&gt;
&lt;li&gt;CPU bound operation vs. IO bound operation&lt;/li&gt;
&lt;li&gt;Factors to impact performance&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Entity%20Framework" target="_blank"&gt;Entity Framework/Core and LINQ to Entities: Querying relational data&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-1-remote-query" target="_blank"&gt;Remote LINQ query&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Entity Framework and Entity Framework Core&lt;/li&gt;
&lt;li&gt;SQL database&lt;/li&gt;
&lt;li&gt;Remote query vs. local query&lt;/li&gt;
&lt;li&gt;Function vs. expression tree&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-2-modeling-database-object-relational-mapping" target="_blank"&gt;Modeling database with object-relational mapping&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Data types&lt;/li&gt;
&lt;li&gt;Database
&lt;ul&gt;
&lt;li&gt;Connection resiliency and execution retry strategy&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Tables&lt;/li&gt;
&lt;li&gt;Relationships
&lt;ul&gt;
&lt;li&gt;One-to-one&lt;/li&gt;
&lt;li&gt;One-to-many&lt;/li&gt;
&lt;li&gt;Many-to-many&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Views&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-3-logging-and-tracing-queries" target="_blank"&gt;Logging and tracing LINQ to Entities queries&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Application side logging&lt;/li&gt;
&lt;li&gt;Database side tracing with Extended Events&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-4-query-methods" target="_blank"&gt;LINQ to Entities standard queries&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Sequence queries
&lt;ul&gt;
&lt;li&gt;Generation: DefaultIfEmpty&lt;/li&gt;
&lt;li&gt;Filtering (restriction): Where, OfType&lt;/li&gt;
&lt;li&gt;Mapping (projection): Select&lt;/li&gt;
&lt;li&gt;Grouping: GroupBy&lt;/li&gt;
&lt;li&gt;Join
&lt;ul&gt;
&lt;li&gt;Inner join: Join, SelectMany, GroupJoin, Select&lt;/li&gt;
&lt;li&gt;Outer join: GroupJoin, Select, SelectMany&lt;/li&gt;
&lt;li&gt;Cross join and self join: SelectMany, Join&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Concatenation: Concat&lt;/li&gt;
&lt;li&gt;Set: Distinct, Union, Intersect, Except&lt;/li&gt;
&lt;li&gt;Partitioning: Take, Skip&lt;/li&gt;
&lt;li&gt;Ordering: OrderBy, ThenBy, OrderByDescending, ThenByDescending&lt;/li&gt;
&lt;li&gt;Conversion: Cast, AsQueryable&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Value queries
&lt;ul&gt;
&lt;li&gt;Element: First, FirstOrDefault, Single, SingleOrDefault&lt;/li&gt;
&lt;li&gt;Aggregation: Count, LongCount, Min, Max, Sum, Average&lt;/li&gt;
&lt;li&gt;Quantifier: All, Any, Contains&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-5-query-translation-implementation" target="_blank"&gt;LINQ to Entities internals: Query translation implementation&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Code to LINQ expression tree
&lt;ul&gt;
&lt;li&gt;IQueryable&amp;lt;T&amp;gt; and IQueryProvider&lt;/li&gt;
&lt;li&gt;Standard remote queries&lt;/li&gt;
&lt;li&gt;Building LINQ to Entities abstract syntax tree&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;.NET expression tree to database expression tree
&lt;ul&gt;
&lt;li&gt;Database query abstract syntax tree&lt;/li&gt;
&lt;li&gt;Compiling LINQ expressions to database expressions&lt;/li&gt;
&lt;li&gt;Compiling LINQ queries&lt;/li&gt;
&lt;li&gt;Compiling .NET API calls&lt;/li&gt;
&lt;li&gt;Remote API call vs. local API call&lt;/li&gt;
&lt;li&gt;Compile database functions and operators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Database expression tree to database query language
&lt;ul&gt;
&lt;li&gt;SQL generator and SQL command&lt;/li&gt;
&lt;li&gt;Generating SQL from database expression tree&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-6-query-data-loading" target="_blank"&gt;Loading query data&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Deferred execution
&lt;ul&gt;
&lt;li&gt;Iterator pattern&lt;/li&gt;
&lt;li&gt;Lazy evaluation vs. eager evaluation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Explicit loading&lt;/li&gt;
&lt;li&gt;Eager loading&lt;/li&gt;
&lt;li&gt;Lazy loading
&lt;ul&gt;
&lt;li&gt;The N + 1 problem&lt;/li&gt;
&lt;li&gt;Disabling lazy loading&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-7-data-changes-and-transactions" target="_blank"&gt;Manipulating relational data: Data change and transaction&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Repository pattern and unit of work pattern&lt;/li&gt;
&lt;li&gt;Tracking entities and changes
&lt;ul&gt;
&lt;li&gt;Tracking entities&lt;/li&gt;
&lt;li&gt;Tracking entity changes and property changes&lt;/li&gt;
&lt;li&gt;Tracking relationship changes&lt;/li&gt;
&lt;li&gt;Enabling and disabling tracking&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Change data
&lt;ul&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;li&gt;Update&lt;/li&gt;
&lt;li&gt;Delete&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Transaction
&lt;ul&gt;
&lt;li&gt;Transaction with connection resiliency and execution strategy&lt;/li&gt;
&lt;li&gt;EF Core transaction&lt;/li&gt;
&lt;li&gt;ADO.NET transaction&lt;/li&gt;
&lt;li&gt;Transaction scope&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/entity-framework-core-and-linq-to-entities-8-optimistic-concurrency" target="_blank"&gt;Resolving optimistic concurrency&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Detecting concurrent conflicts&lt;/li&gt;
&lt;li&gt;Resolving concurrent conflicts
&lt;ul&gt;
&lt;li&gt;Retaining database values (database wins)&lt;/li&gt;
&lt;li&gt;Overwriting database values (client wins)&lt;/li&gt;
&lt;li&gt;Merging with database values&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Saving changes with concurrent conflict handling&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;!--&lt;li&gt;
        &lt;h1&gt;LINQ to SQL&lt;/h1&gt;
        &lt;ol&gt;
        &lt;li&gt;
        &lt;h2&gt;Object-Relational Mapping&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Create C# models from SQL schema
            &lt;li&gt;Implement the mapping
            &lt;li&gt;Customize the mapping
            &lt;li&gt;
            Work with model
            &lt;ul&gt;
                &lt;li&gt;Partial class
                &lt;li&gt;Partial method
                &lt;li&gt;INotifyPropertyChanging and INotifyPropertyChanged interfaces&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;Create SQL schema from C# models&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;IQueryable&amp;lt;T&amp;gt;&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;IQueryable and IQueryable&amp;lt;T&amp;gt;
            &lt;li&gt;IQueryable and IQueryable&amp;lt;T&amp;gt; extensions
            &lt;li&gt;Table&amp;lt;T&amp;gt;
            &lt;li&gt;IEnumerable&amp;lt;T&amp;gt; extensions vs. IQueryable&amp;lt;T&amp;gt; extensions&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Expression Tree&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Anonymous method vs. expression tree
            &lt;li&gt;Expression tree for LINQ to SQL
            &lt;li&gt;Translate expression tree to SQL
            &lt;li&gt;Expression tree types
            &lt;li&gt;Expression tree for DLR
            &lt;li&gt;Visualize expression tree while debugging&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Data Retrieving Via Query Methods&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Supported and Unsupported query methods
            &lt;li&gt;
            Restriction
            &lt;ul&gt;
                &lt;li&gt;WHERE
                &lt;li&gt;AND / OR
                &lt;li&gt;LIKE
                &lt;li&gt;IN
                &lt;li&gt;IS / NOT / NULL&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Projection
            &lt;ul&gt;
                &lt;li&gt;SELECT
                &lt;li&gt;Explicitly construct entity
                &lt;li&gt;CASE&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Ordering
            &lt;ul&gt;
                &lt;li&gt;ORDER BY / ASC / DESC&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Join
            &lt;ul&gt;
                &lt;li&gt;Natural join
                &lt;li&gt;INNER JOIN
                &lt;li&gt;
                OUTER JOIN
                &lt;ul&gt;
                    &lt;li&gt;Association&lt;/li&gt;
                &lt;/ul&gt;
                &lt;li&gt;CROSS JOIN
                &lt;li&gt;Self JOIN&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Grouping and aggregation
            &lt;ul&gt;
                &lt;li&gt;GROUP BY / aggregate functions
                &lt;li&gt;HAVING&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Set
            &lt;ul&gt;
                &lt;li&gt;DISTINCT
                &lt;li&gt;UNION
                &lt;li&gt;EXISTS
                &lt;li&gt;NOT EXISTS&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Partitioning
            &lt;ul&gt;
                &lt;li&gt;TOP
                &lt;li&gt;ROW_NUMBER()
                &lt;li&gt;BETWEEN … AND&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Concatenation
            &lt;ul&gt;
                &lt;li&gt;UNION ALL&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Qualifiers
            &lt;ul&gt;
                &lt;li&gt;CASE / EXISTS&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;Other query methods&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Remote And Local Method Call&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Remote method call
            &lt;li&gt;Local method call
            &lt;li&gt;Remote method recognition&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Working With Deferred Execution&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Deferred execution and DataContext
            &lt;li&gt;
                Deferred execution and eager loading
                &lt;ul&gt;
                    &lt;li&gt;DataLoadOptions.LoadWith()
                    &lt;li&gt;DataLoadOptions.AssociateWith()
                    &lt;li&gt;DataContext.DeferredLoadingEnabled&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Data Changing&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;
            Object Identity
            &lt;ul&gt;
                &lt;li&gt;Identity of entity objects
                &lt;li&gt;Identity and DataContext
                &lt;li&gt;Identity of projected objects (non-entity objects)&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Track changes
            &lt;ul&gt;
                &lt;li&gt;State change
                &lt;li&gt;Association change
                &lt;li&gt;Change set&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Submit changes
            &lt;ul&gt;
                &lt;li&gt;
                INSERT
                &lt;ul&gt;
                    &lt;li&gt;Work with IDENTITY column&lt;/li&gt;
                &lt;/ul&gt;
                &lt;li&gt;UPDATE
                &lt;li&gt;DELETE&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;Read-only DataContext&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Transaction&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Implementing transaction (BEGIN / COMMIT / ROLLBACK)
            &lt;li&gt;Default transaction
            &lt;li&gt;Custom transactions&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Concurrent conflict&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;Conflicts in concurrent data access
            &lt;li&gt;
            Optimistic concurrency control
            &lt;ul&gt;
                &lt;li&gt;Update check
                &lt;li&gt;Time stamp&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Handle ChangeConflictException
            &lt;ul&gt;
                &lt;li&gt;Merge changes to resolve conflict&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;More about concurrency&lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
        &lt;h2&gt;Implementing LINQ to SQL Provider&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;
            The provider model
            &lt;ul&gt;
                &lt;li&gt;Implement IQueryable&amp;lt;T&amp;gt; and IOrderedQueryable&amp;lt;T&amp;gt;
                &lt;li&gt;Implement IQueryProvider&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Query method internals
            &lt;ul&gt;
                &lt;li&gt;Deferred execution methods
                &lt;li&gt;Eager execution methods&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
            Work together
            &lt;ul&gt;
                &lt;li&gt;Query a collection of items (deferred execution)
                &lt;li&gt;Query a single item (eager execution)
                &lt;li&gt;Aggregate (eager execution)
                &lt;li&gt;SQL translating and executing&lt;/li&gt;
            &lt;/ul&gt;
            &lt;li&gt;
                LINQ Providers
                &lt;ul&gt;
                    &lt;li&gt;Built-in IQueryable LINQ Providers
                    &lt;li&gt;LINQ to Everything
                    &lt;li&gt;LINQ to Objects provider&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
        &lt;li&gt;
            &lt;h2&gt;Performance&lt;/h2&gt;
            &lt;ul&gt;
                &lt;li&gt;O/R mapping overhead
                &lt;li&gt;
                Data retrieving overhead
                &lt;ul&gt;
                    &lt;li&gt;Compiled query
                    &lt;li&gt;Static SQL / stored procedures without translating&lt;/li&gt;
                &lt;/ul&gt;
                &lt;li&gt;
                Data changing overhead
                &lt;ul&gt;
                    &lt;li&gt;Bulk deleting / updating&lt;/li&gt;
                &lt;/ul&gt;
                &lt;li&gt;
                    Query plan overhead
                    &lt;ul&gt;
                        &lt;li&gt;.NET 3.5
                        &lt;li&gt;.NET 4.0&lt;/li&gt;
                    &lt;/ul&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
        &lt;/li&gt;
        &lt;/ol&gt;--&gt;&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C#: The foundation of all functional programming&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-c-1-fundamentals" target="_blank"&gt;Basics&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Expression
&lt;ul&gt;
&lt;li&gt;Bound variable vs. free variable&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Reductions
&lt;ul&gt;
&lt;li&gt;&amp;alpha;-conversion (alpha-conversion)&lt;/li&gt;
&lt;li&gt;&amp;beta;-reduction (beta-reduction)&lt;/li&gt;
&lt;li&gt;&amp;eta;-conversion (eta-conversion)&lt;/li&gt;
&lt;li&gt;Normal order&lt;/li&gt;
&lt;li&gt;Applicative order&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Function composition
&lt;ul&gt;
&lt;li&gt;Associativity&lt;/li&gt;
&lt;li&gt;Unit&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-c-2-boolean-and-logic" target="_blank"&gt;Church encoding: Function as boolean and logic&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Church encoding&lt;/li&gt;
&lt;li&gt;Church Boolean&lt;/li&gt;
&lt;li&gt;Logical operators&lt;/li&gt;
&lt;li&gt;Conversion between Church Boolean and System.Boolean&lt;/li&gt;
&lt;li&gt;If&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-csharp-3-numeral-arithmetic-and-predicate" target="_blank"&gt;Church encoding: Function as numeral, arithmetic and predicate&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Church numerals&lt;/li&gt;
&lt;li&gt;Increase and decrease&lt;/li&gt;
&lt;li&gt;Arithmetic operators&lt;/li&gt;
&lt;li&gt;Predicate and relational operators
&lt;ul&gt;
&lt;li&gt;Attempt of recursion&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Conversion between Church numeral and System.UInt32&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-csharp-4-tuple-and-signed-numeral" target="_blank"&gt;Church encoding: Function as tuple and signed numeral&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Church pair (2-tuple)
&lt;ul&gt;
&lt;li&gt;Tuple operators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;N-tuple&lt;/li&gt;
&lt;li&gt;Signed numeral
&lt;ul&gt;
&lt;li&gt;Arithmetic operators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-csharp-5-list" target="_blank"&gt;Church encoding: Function as list&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Tuple as list node
&lt;ul&gt;
&lt;li&gt;List operators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Aggregation function as list node
&lt;ul&gt;
&lt;li&gt;List operators&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Model everything&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-csharp-6-combinatory-logic" target="_blank"&gt;Combinatory logic&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Combinator&lt;/li&gt;
&lt;li&gt;SKI combinator calculus
&lt;ul&gt;
&lt;li&gt;SKI compiler: compile lambda calculus expression to SKI calculus combinator&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Iota combinator calculus&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-csharp-7-fixed-point-combinator-and-recursion" target="_blank"&gt;Fixed point combinator and recursion&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Normal order fixed point combinator (Y combinator) and recursion&lt;/li&gt;
&lt;li&gt;Applicative order fixed point combinator (Z combinator) and recursion&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/lambda-calculus-via-c-sharp-24-undecidability-of-equivalence" target="_blank"&gt;Undecidability of equivalence&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Halting problem&lt;/li&gt;
&lt;li&gt;Equivalence problem&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h1&gt;&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C#: The essentials and design of LINQ&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-1-fundamentals" target="_blank"&gt;Basics: Category and morphism&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Category and category laws&lt;/li&gt;
&lt;li&gt;DotNet category&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-2-monoid" target="_blank"&gt;Monoid&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Monoid and monoid laws&lt;/li&gt;
&lt;li&gt;Monoid as category&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-3-functor-and-linq-to-functors" target="_blank"&gt;Functor and LINQ to Functors&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Functor and functor laws
&lt;ul&gt;
&lt;li&gt;Endofunctor&lt;/li&gt;
&lt;li&gt;Type constructor and higher-kinded type&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;LINQ to Functors
&lt;ul&gt;
&lt;li&gt;Built-in IEnumerable&amp;lt;&amp;gt; functor&lt;/li&gt;
&lt;li&gt;Functor pattern of LINQ&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;More LINQ to Functors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-4-natural-transformation" target="_blank"&gt;Natural transformation&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Natural transformation and naturality&lt;/li&gt;
&lt;li&gt;Functor Category
&lt;ul&gt;
&lt;li&gt;Endofunctor category&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-5-bifunctor" target="_blank"&gt;Bifunctor&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Bifunctor&lt;/li&gt;
&lt;li&gt;Monoidal category&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-6-monoidal-functor-and-applicative-functor" target="_blank"&gt;Monoidal functor and applicative functor&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Monoidal functor
&lt;ul&gt;
&lt;li&gt;IEnumeable&amp;lt;&amp;gt; monoidal functor&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Applicative functor
&lt;ul&gt;
&lt;li&gt;IEnumeable&amp;lt;&amp;gt; applicative functor&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Monoidal functor vs. applicative functor&lt;/li&gt;
&lt;li&gt;More Monoidal functors and applicative functors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-7-monad-and-linq-to-monads" target="_blank"&gt;Monad and LINQ to Monads&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Monad&lt;/li&gt;
&lt;li&gt;LINQ to Monads and monad laws
&lt;ul&gt;
&lt;li&gt;Built-in IEnumerable&amp;lt;&amp;gt; monad&lt;/li&gt;
&lt;li&gt;Monad laws and Kleisli composition&lt;/li&gt;
&lt;li&gt;Kleisli category&lt;/li&gt;
&lt;li&gt;Monad pattern of LINQ&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Monad vs. monoidal/applicative functor&lt;/li&gt;
&lt;li&gt;More LINQ to Monads&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h2&gt;&lt;a href="https://weblogs.asp.net/dixin/category-theory-via-csharp-8-more-linq-to-monads" target="_blank"&gt;Advanced LINQ to Monads&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;IO monad&lt;/li&gt;
&lt;li&gt;State monad&lt;/li&gt;
&lt;li&gt;Try monad&lt;/li&gt;
&lt;li&gt;Reader monad&lt;/li&gt;
&lt;li&gt;Writer monad&lt;/li&gt;
&lt;li&gt;Continuation monad&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;</description><pubDate>Sun, 02 Mar 2025 03:16:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/fp-csharp</guid><category>.NET</category><category>C#</category><category>Functional Programming</category><category>LINQ</category><category>LINQ via C#</category><category>.NET Core</category><category>.NET Standard</category><category>C# 8.0</category></item><item><title>Understanding (all) JavaScript module formats and tools</title><link>https://weblogs.asp.net:443/dixin/understanding-all-javascript-module-formats-and-tools</link><description>&lt;p&gt;&lt;/p&gt;
&lt;!-- omit in toc --&gt;
&lt;p&gt;When you build an application with JavaScript, you always want to modularize your code. However, JavaScript language was initially invented for simple form manipulation, with no built-in features like module or namespace. In years, tons of technologies are invented to modularize JavaScript. This article discusses all mainstream terms, patterns, libraries, syntax, and tools for JavaScript modules.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#iife-module-javascript-module-pattern"&gt;IIFE module: JavaScript module pattern&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#iife-immediately-invoked-function-expression"&gt;IIFE: Immediately invoked function expression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#import-mixins"&gt;Import mixins&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#revealing-module-javascript-revealing-module-pattern"&gt;Revealing module: JavaScript revealing module pattern&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#cjs-module-commonjs-module-or-nodejs-module"&gt;CJS module: CommonJS module, or Node.js module&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#amd-module-asynchronous-module-definition-or-requirejs-module"&gt;AMD module: Asynchronous Module Definition, or RequireJS module&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#dynamic-loading"&gt;Dynamic loading&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#amd-module-from-commonjs-module"&gt;AMD module from CommonJS module&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#umd-module-universal-module-definition-or-umdjs-module"&gt;UMD module: Universal Module Definition, or UmdJS module&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#umd-for-both-amd-requirejs-and-native-browser"&gt;UMD for both AMD (RequireJS) and native browser&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#umd-for-both-amd-requirejs-and-commonjs-nodejs"&gt;UMD for both AMD (RequireJS) and CommonJS (Node.js)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#es-module-ecmascript-2015-or-es6-module"&gt;ES module: ECMAScript 2015, or ES6 module&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#es-dynamic-module-ecmascript-2020-or-es11-dynamic-module"&gt;ES dynamic module: ECMAScript 2020, or ES11 dynamic module&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#system-module-systemjs-module"&gt;System module: SystemJS module&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#dynamic-module-loading"&gt;Dynamic module loading&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#webpack-module-bundle-from-cjs-amd-es-modules"&gt;Webpack module: bundle from CJS, AMD, ES modules&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#babel-module-transpile-from-es-module"&gt;Babel module: transpile from ES module&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#babel-with-systemjs"&gt;Babel with SystemJS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#typescript-module-transpile-to-cjs-amd-es-system-modules"&gt;TypeScript module: Transpile to CJS, AMD, ES, System modules&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#internal-module-and-namespace"&gt;Internal module and namespace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#conclusion"&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;IIFE module: JavaScript module pattern&lt;/h1&gt;
&lt;p&gt;In the browser, defining a JavaScript variable is defining a global variable, which causes pollution across all JavaScript files loaded by the current web page:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define global variables.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-comment"&gt;// Use global variables.&lt;/span&gt; increase(); reset();&lt;/div&gt;
&lt;p&gt;To avoid global pollution, an anonymous function can be used to wrap the code:&lt;/p&gt;
&lt;div&gt;(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// ...&lt;/span&gt; });&lt;/div&gt;
&lt;p&gt;Apparently, there is no longer any global variable. However, defining a function does not execute the code inside the function.&lt;/p&gt;
&lt;h2&gt;IIFE: Immediately invoked function expression&lt;/h2&gt;
&lt;p&gt;To execute the code inside a function &lt;code&gt;f&lt;/code&gt;, the syntax is function call &lt;code&gt;()&lt;/code&gt; as &lt;code&gt;f()&lt;/code&gt;. To execute the code inside an anonymous function &lt;code&gt;(() =&amp;gt; {})&lt;/code&gt;, the same function call syntax &lt;code&gt;()&lt;/code&gt; can be used as &lt;code&gt;(() =&amp;gt; {})()&lt;/code&gt;:&lt;/p&gt;
&lt;div&gt;(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// ...&lt;/span&gt; })();&lt;/div&gt;
&lt;p&gt;This is called an IIFE (Immediately invoked function expression). So a basic module can be defined in this way:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define IIFE module.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; iifeCounterModule = &lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;(&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;increase&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count, &lt;span class="hljs-attr"&gt;reset&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); } }; })(); &lt;span class="hljs-comment"&gt;// Use IIFE module.&lt;/span&gt; iifeCounterModule.increase(); iifeCounterModule.reset();&lt;/div&gt;
&lt;p&gt;It wraps the module code inside an IIFE. The anonymous function returns an object, which is the placeholder of exported APIs. Only 1 global variable is introduced, which is the module name (or namespace). Later the module name can be used to call the exported module APIs. This is called the module pattern of JavaScript.&lt;/p&gt;
&lt;h2&gt;Import mixins&lt;/h2&gt;
&lt;p&gt;When defining a module, some dependencies may be required. With IIFE module pattern, each dependent module is a global variable. The dependent modules can be directly accessed inside the anonymous function, or they can be passed as the anonymous function&amp;rsquo;s arguments:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define IIFE module with dependencies.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; iifeCounterModule = &lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;(dependencyModule1, dependencyModule2&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;increase&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count, &lt;span class="hljs-attr"&gt;reset&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); } }; })(dependencyModule1, dependencyModule2);&lt;/div&gt;
&lt;p&gt;The early version of popular libraries, like jQuery, followed this pattern. (The latest version of jQuery follows the UMD module, which is explained later in this article.)&lt;/p&gt;
&lt;h1&gt;Revealing module: JavaScript revealing module pattern&lt;/h1&gt;
&lt;p&gt;The revealing module pattern is named by Christian Heilmann. This pattern is also an IIFE, but it emphasizes defining all APIs as local variables inside the anonymous function:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define revealing module.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; revealingCounterModule = &lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;(&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { increase, reset }; })(); &lt;span class="hljs-comment"&gt;// Use revealing module.&lt;/span&gt; revealingCounterModule.increase(); revealingCounterModule.reset();&lt;/div&gt;
&lt;p&gt;With this syntax, it becomes easier when the APIs need to call each other.&lt;/p&gt;
&lt;h1&gt;CJS module: CommonJS module, or Node.js module&lt;/h1&gt;
&lt;p&gt;CommonJS, initially named ServerJS, is a pattern to define and consume modules. It is implemented by Node,js. By default, each &lt;code&gt;.js&lt;/code&gt; file is a CommonJS module. A &lt;code&gt;module&lt;/code&gt; variable and an &lt;code&gt;exports&lt;/code&gt; variable are provided for a module (a file) to expose APIs. And a &lt;code&gt;require&lt;/code&gt; function is provided to load and consume a module. The following code defines the counter module in CommonJS syntax:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define CommonJS module: commonJSCounterModule.js.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule2"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; exports.increase = increase; exports.reset = reset; &lt;span class="hljs-comment"&gt;// Or equivalently:&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = { increase, reset };&lt;/div&gt;
&lt;p&gt;The following example consumes the counter module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use CommonJS module.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; { increase, reset } = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./commonJSCounterModule"&lt;/span&gt;); increase(); reset(); &lt;span class="hljs-comment"&gt;// Or equivelently:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; commonJSCounterModule = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./commonJSCounterModule"&lt;/span&gt;); commonJSCounterModule.increase(); commonJSCounterModule.reset();&lt;/div&gt;
&lt;p&gt;At runtime, Node.js implements this by wrapping the code inside the file into a function, then passes the &lt;code&gt;exports&lt;/code&gt; variable, &lt;code&gt;module&lt;/code&gt; variable, and &lt;code&gt;require&lt;/code&gt; function through arguments.&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define CommonJS module: wrapped commonJSCounterModule.js.&lt;/span&gt; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports, require, module, __filename, __dirname&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule2"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = { increase, reset }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports; }).call(thisValue, exports, &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;, filename, dirname); &lt;span class="hljs-comment"&gt;// Use CommonJS module.&lt;/span&gt; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports, require, module, __filename, __dirname&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; commonJSCounterModule = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./commonJSCounterModule"&lt;/span&gt;); commonJSCounterModule.increase(); commonJSCounterModule.reset(); }).call(thisValue, exports, &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;, filename, dirname);&lt;/div&gt;
&lt;h1&gt;AMD module: Asynchronous Module Definition, or RequireJS module&lt;/h1&gt;
&lt;p&gt;AMD (Asynchronous Module Definition &lt;a href="https://github.com/amdjs/amdjs-api"&gt;https://github.com/amdjs/amdjs-api&lt;/a&gt;), is a pattern to define and consume module. It is implemented by RequireJS library &lt;a href="https://requirejs.org/"&gt;https://requirejs.org/&lt;/a&gt;. AMD provides a &lt;code&gt;define&lt;/code&gt; function to define module, which accepts the module name, dependent modules&amp;rsquo; names, and a factory function:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define AMD module.&lt;/span&gt; define(&lt;span class="hljs-string"&gt;"amdCounterModule"&lt;/span&gt;, [&lt;span class="hljs-string"&gt;"dependencyModule1"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"dependencyModule2"&lt;/span&gt;], (dependencyModule1, dependencyModule2) =&amp;gt; { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { increase, reset }; });&lt;/div&gt;
&lt;p&gt;It also provides a &lt;code&gt;require&lt;/code&gt; function to consume module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use AMD module.&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;([&lt;span class="hljs-string"&gt;"amdCounterModule"&lt;/span&gt;], amdCounterModule =&amp;gt; { amdCounterModule.increase(); amdCounterModule.reset(); });&lt;/div&gt;
&lt;p&gt;The AMD &lt;code&gt;require&lt;/code&gt; function is totally different from the CommonJS &lt;code&gt;require&lt;/code&gt; function. AMD &lt;code&gt;require&lt;/code&gt; accept the names of modules to be consumed, and pass the module to a function argument.&lt;/p&gt;
&lt;h2&gt;Dynamic loading&lt;/h2&gt;
&lt;p&gt;AMD&amp;rsquo;s &lt;code&gt;define&lt;/code&gt; function has another overload. It accepts a callback function, and pass a CommonJS-like &lt;code&gt;require&lt;/code&gt; function to that callback. Inside the callback function, &lt;code&gt;require&lt;/code&gt; can be called to dynamically load the module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use dynamic AMD module.&lt;/span&gt; define(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;require&lt;/span&gt; =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dynamicDependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dynamicDependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule2"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { increase, reset }; });&lt;/div&gt;
&lt;h2&gt;AMD module from CommonJS module&lt;/h2&gt;
&lt;p&gt;The above &lt;code&gt;define&lt;/code&gt; function overload can also passes the &lt;code&gt;require&lt;/code&gt; function as well as &lt;code&gt;exports&lt;/code&gt; variable and &lt;code&gt;module&lt;/code&gt; to its callback function. So inside the callback, CommonJS syntax code can work:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define AMD module with CommonJS code.&lt;/span&gt; define(&lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, exports, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// CommonJS code.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule2"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; exports.increase = increase; exports.reset = reset; }); &lt;span class="hljs-comment"&gt;// Use AMD module with CommonJS code.&lt;/span&gt; define(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;require&lt;/span&gt; =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// CommonJS code.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; counterModule = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"amdCounterModule"&lt;/span&gt;); counterModule.increase(); counterModule.reset(); });&lt;/div&gt;
&lt;h1&gt;UMD module: Universal Module Definition, or UmdJS module&lt;/h1&gt;
&lt;p&gt;UMD (Universal Module Definition, &lt;a href="https://github.com/umdjs/umd"&gt;https://github.com/umdjs/umd&lt;/a&gt;) is a set of tricky patterns to make your code file work in multiple environments.&lt;/p&gt;
&lt;h3 id="umd-for-both-amd-requirejs-and-native-browser"&gt;UMD for both AMD (RequireJS) and native browser&lt;/h3&gt;
&lt;p&gt;For example, the following is a kind of UMD pattern to make module definition work with both AMD (RequireJS) and native browser:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define UMD module for both AMD and browser.&lt;/span&gt; (&lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;root, factory&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// Detects AMD/RequireJS"s define function.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; define === &lt;span class="hljs-string"&gt;"function"&lt;/span&gt; &amp;amp;&amp;amp; define.amd) { &lt;span class="hljs-comment"&gt;// Is AMD/RequireJS. Call factory with AMD/RequireJS"s define function.&lt;/span&gt; define(&lt;span class="hljs-string"&gt;"umdCounterModule"&lt;/span&gt;, [&lt;span class="hljs-string"&gt;"deependencyModule1"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"dependencyModule2"&lt;/span&gt;], factory); } &lt;span class="hljs-keyword"&gt;else&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// Is Browser. Directly call factory.&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Imported dependencies are global variables(properties of window object).&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Exported module is also a global variable(property of window object)&lt;/span&gt; root.umdCounterModule = factory(root.deependencyModule1, root.dependencyModule2); } })(&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; self !== &lt;span class="hljs-string"&gt;"undefined"&lt;/span&gt; ? self : &lt;span class="hljs-keyword"&gt;this&lt;/span&gt;, (deependencyModule1, dependencyModule2) =&amp;gt; { &lt;span class="hljs-comment"&gt;// Module code goes here.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { increase, reset }; });&lt;/div&gt;
&lt;p&gt;It is more complex but it is just an IIFE. The anonymous function detects if AMD&amp;rsquo;s &lt;code&gt;define&lt;/code&gt; function exists.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If yes, call the module factory with AMD&amp;rsquo;s &lt;code&gt;define&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;If not, it calls the module factory directly. At this moment, the &lt;code&gt;root&lt;/code&gt; argument is actually the browser&amp;rsquo;s &lt;code&gt;window&lt;/code&gt; object. It gets dependency modules from global variables (properties of &lt;code&gt;window&lt;/code&gt; object). When &lt;code&gt;factory&lt;/code&gt; returns the module, the returned module is also assigned to a global variable (property of &lt;code&gt;window&lt;/code&gt; object).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;UMD for both AMD (RequireJS) and CommonJS (Node.js)&lt;/h2&gt;
&lt;p&gt;The following is another kind of UMD pattern to make module definition work with both AMD (RequireJS) and CommonJS (Node.js):&lt;/p&gt;
&lt;div&gt;(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;define&lt;/span&gt; =&amp;gt;&lt;/span&gt; define(&lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, exports, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// Module code goes here.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"dependencyModule2"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.export = { increase, reset }; }))(&lt;span class="hljs-comment"&gt;// Detects module variable and exports variable of CommonJS/Node.js.&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Also detect the define function of AMD/RequireJS.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt; === &lt;span class="hljs-string"&gt;"object"&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; define !== &lt;span class="hljs-string"&gt;"function"&lt;/span&gt; ? &lt;span class="hljs-comment"&gt;// Is CommonJS/Node.js. Manually create a define function.&lt;/span&gt; factory =&amp;gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = factory(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, exports, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;) : &lt;span class="hljs-comment"&gt;// Is AMD/RequireJS. Directly use its define function.&lt;/span&gt; define);&lt;/div&gt;
&lt;p&gt;Again, don&amp;rsquo;t be scared. It is just another IIFE. When the anonymous function is called, its argument is evaluated. The argument evaluation detects the environment (check the module variable and &lt;code&gt;exports&lt;/code&gt; variable of CommonJS/Node.js, as well as the &lt;code&gt;define&lt;/code&gt; function of AMD/RequireJS).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the environment is CommonJS/Node.js, the anonymous function&amp;rsquo;s argument is a manually created &lt;code&gt;define&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;If the environment is AMD/RequireJS, the anonymous function&amp;rsquo;s argument is just AMD&amp;rsquo;s &lt;code&gt;define&lt;/code&gt; function. So when the anonymous function is executed, it is guaranteed to have a working &lt;code&gt;define&lt;/code&gt; function. Inside the anonymous function, it simply calls the &lt;code&gt;define&lt;/code&gt; function to create the module.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;ES module: ECMAScript 2015, or ES6 module&lt;/h1&gt;
&lt;p&gt;After all the module mess, in 2015, JavaScript&amp;rsquo;s spec version 6 introduces one more different module syntax. This spec is called ECMAScript 2015 (ES2015), or ECMAScript 6 (ES6). The main syntax is the import keyword and the export keyword. The following example uses new syntax to demonstrate ES module&amp;rsquo;s named import/export and default import/export:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define ES module: esCounterModule.js or esCounterModule.mjs.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule1 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./dependencyModule1.mjs"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule2 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./dependencyModule2.mjs"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// Named export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-comment"&gt;// Or default export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;default&lt;/span&gt; { increase, reset };&lt;/div&gt;
&lt;p&gt;To use this module file in browser, add a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag and specify it is a module: &lt;code&gt;&amp;lt;script type="module" src="esCounterModule.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;. To use this module file in Node.js, rename its extension from &lt;code&gt;.js&lt;/code&gt; to &lt;code&gt;.mjs&lt;/code&gt;.&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use ES module.&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Browser: &amp;lt;script type="module" src="esCounterModule.js"&amp;gt;&amp;lt;/script&amp;gt; or inline.&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Server: esCounterModule.mjs&lt;/span&gt; &lt;span class="hljs-comment"&gt;// Import from named export.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; { increase, reset } &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./esCounterModule.mjs"&lt;/span&gt;; increase(); reset(); &lt;span class="hljs-comment"&gt;// Or import from default export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; esCounterModule &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./esCounterModule.mjs"&lt;/span&gt;; esCounterModule.increase(); esCounterModule.reset();&lt;/div&gt;
&lt;p&gt;For browser, &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;&amp;rsquo;s &lt;code&gt;nomodule&lt;/code&gt; attribute can be used for fallback:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-tag"&gt;&amp;lt;&lt;span class="hljs-name"&gt;script&lt;/span&gt; &lt;span class="hljs-attr"&gt;nomodule&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;span class="actionscript"&gt; alert(&lt;span class="hljs-string"&gt;"Not supported."&lt;/span&gt;); &lt;/span&gt;&lt;span class="hljs-tag"&gt;&amp;lt;/&lt;span class="hljs-name"&gt;script&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;h1&gt;ES dynamic module: ECMAScript 2020, or ES11 dynamic module&lt;/h1&gt;
&lt;p&gt;In 2020, the latest JavaScript spec version 11 is introducing a built-in function &lt;code&gt;import&lt;/code&gt; to consume an ES module dynamically. The &lt;code&gt;import&lt;/code&gt; function returns a &lt;code&gt;promise&lt;/code&gt;, so its &lt;code&gt;then&lt;/code&gt; method can be called to consume the module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use dynamic ES module with promise APIs, import from named export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;).then(&lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;{ increase, reset }&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { increase(); reset(); }); &lt;span class="hljs-comment"&gt;// Or import from default export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;).then(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;dynamicESCounterModule&lt;/span&gt; =&amp;gt;&lt;/span&gt; { dynamicESCounterModule.increase(); dynamicESCounterModule.reset(); });&lt;/div&gt;
&lt;p&gt;By returning a &lt;code&gt;promise&lt;/code&gt;, apparently, &lt;code&gt;import&lt;/code&gt; function can also work with the &lt;code&gt;await&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use dynamic ES module with async/await.&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;async&lt;/span&gt; () =&amp;gt; { &lt;span class="hljs-comment"&gt;// Import from named export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; { increase, reset } = &lt;span class="hljs-keyword"&gt;await&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;); increase(); reset(); &lt;span class="hljs-comment"&gt;// Or import from default export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dynamicESCounterModule = &lt;span class="hljs-keyword"&gt;await&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;); dynamicESCounterModule.increase(); dynamicESCounterModule.reset(); })();&lt;/div&gt;
&lt;p&gt;The following is the compatibility of import/dynamic import/export, from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img alt="import compatibility" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/JavaScript-moduels_11325/image_2.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="export compatibility" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/JavaScript-moduels_11325/image_4.png" /&gt;&lt;/p&gt;
&lt;h1&gt;System module: SystemJS module&lt;/h1&gt;
&lt;p&gt;SystemJS is a library that can enable ES module syntax for older ES. For example, the following module is defined in ES 6syntax:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define ES module.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule1 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./dependencyModule1.js"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule2 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./dependencyModule2.js"&lt;/span&gt;; dependencyModule1.api1(); dependencyModule2.api2(); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// Named export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count }; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-comment"&gt;// Or default export:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;default&lt;/span&gt; { increase, reset }&lt;/div&gt;
&lt;p&gt;If the current runtime, like an old browser, does not support ES6 syntax, the above code cannot work. One solution is to transpile the above module definition to a call of SystemJS library API, &lt;code&gt;System.register&lt;/code&gt;:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define SystemJS module.&lt;/span&gt; System.register([&lt;span class="hljs-string"&gt;"./dependencyModule1.js"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"./dependencyModule2.js"&lt;/span&gt;], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports_1, context_1&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule1_js_1, dependencyModule2_js_1, count, increase, reset; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; __moduleName = context_1 &amp;amp;&amp;amp; context_1.id; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [ &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;dependencyModule1_js_1_1&lt;/span&gt;) &lt;/span&gt;{ dependencyModule1_js_1 = dependencyModule1_js_1_1; }, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;dependencyModule2_js_1_1&lt;/span&gt;) &lt;/span&gt;{ dependencyModule2_js_1 = dependencyModule2_js_1_1; } ], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ dependencyModule1_js_1.default.api1(); dependencyModule2_js_1.default.api2(); count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// Named export:&lt;/span&gt; exports_1(&lt;span class="hljs-string"&gt;"increase"&lt;/span&gt;, increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count }; exports_1(&lt;span class="hljs-string"&gt;"reset"&lt;/span&gt;, reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); };); &lt;span class="hljs-comment"&gt;// Or default export:&lt;/span&gt; exports_1(&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;, { increase, reset }); } }; });&lt;/div&gt;
&lt;p&gt;So that the import/export new ES6 syntax is gone. The old API call syntax works for sure. This transpilation can be done automatically with Webpack, TypeScript, etc., which are explained later in this article.&lt;/p&gt;
&lt;h2&gt;Dynamic module loading&lt;/h2&gt;
&lt;p&gt;SystemJS also provides an import function for dynamic import:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use SystemJS module with promise APIs.&lt;/span&gt; System.import(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;).then(&lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;dynamicESCounterModule&lt;/span&gt; =&amp;gt;&lt;/span&gt; { dynamicESCounterModule.increase(); dynamicESCounterModule.reset(); });&lt;/div&gt;
&lt;h1&gt;Webpack module: bundle from CJS, AMD, ES modules&lt;/h1&gt;
&lt;p&gt;Webpack is a bundler for modules. It transpiles combined CommonJS module, AMD module, and ES module into a single harmony module pattern, and bundle all code into a single file. For example, the following 3 files define 3 modules in 3 different syntaxes:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Define AMD module: amdDependencyModule1.js&lt;/span&gt; define(&lt;span class="hljs-string"&gt;"amdDependencyModule1"&lt;/span&gt;, () =&amp;gt; { &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; api1 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { api1 }; }); &lt;span class="hljs-comment"&gt;// Define CommonJS module: commonJSDependencyModule2.js&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./amdDependencyModule1"&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; api2 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; dependencyModule1.api1(); exports.api2 = api2; &lt;span class="hljs-comment"&gt;// Define ES module: esCounterModule.js.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule1 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./amdDependencyModule1"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule2 &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./commonJSDependencyModule2"&lt;/span&gt;; dependencyModule1.api1(); dependencyModule2.api2(); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;default&lt;/span&gt; { increase, reset }&lt;/div&gt;
&lt;p&gt;And the following file consumes the counter module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Use ES module: index.js&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; counterModule &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./esCounterModule"&lt;/span&gt;; counterModule.increase(); counterModule.reset();&lt;/div&gt;
&lt;p&gt;Webpack can bundle all the above file, even they are in 3 different module systems, into a single file &lt;code&gt;main.js&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;root
&lt;ul&gt;
&lt;li&gt;dist
&lt;ul&gt;
&lt;li&gt;main.js (Bundle of all files under src)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;src
&lt;ul&gt;
&lt;li&gt;amdDependencyModule1.js&lt;/li&gt;
&lt;li&gt;commonJSDependencyModule2.js&lt;/li&gt;
&lt;li&gt;esCounterModule.js&lt;/li&gt;
&lt;li&gt;index.js&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;webpack.config.js&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since Webpack is based on Node.js, Webpack uses CommonJS module syntax for itself. In &lt;code&gt;webpack.config.js&lt;/code&gt;:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;const&lt;/span&gt; path = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;'path'&lt;/span&gt;); &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = { &lt;span class="hljs-attr"&gt;entry&lt;/span&gt;: &lt;span class="hljs-string"&gt;'./src/index.js'&lt;/span&gt;, &lt;span class="hljs-attr"&gt;mode&lt;/span&gt;: &lt;span class="hljs-string"&gt;"none"&lt;/span&gt;, &lt;span class="hljs-comment"&gt;// Do not optimize or minimize the code for readability.&lt;/span&gt; output: { &lt;span class="hljs-attr"&gt;filename&lt;/span&gt;: &lt;span class="hljs-string"&gt;'main.js'&lt;/span&gt;, &lt;span class="hljs-attr"&gt;path&lt;/span&gt;: path.resolve(__dirname, &lt;span class="hljs-string"&gt;'dist'&lt;/span&gt;), }, };&lt;/div&gt;
&lt;p&gt;Now run the following command to transpile and bundle all 4 files, which are in different syntax:&lt;/p&gt;
&lt;div&gt;npm install webpack webpack-cli --save-dev npx webpack --config webpack.config.js&lt;/div&gt;
&lt;p&gt;AS a result, Webpack generates the bundle file &lt;code&gt;main.js&lt;/code&gt;. The following code in &lt;code&gt;main.js&lt;/code&gt; is reformatted, and variables are renamed, to improve readability:&lt;/p&gt;
&lt;div&gt;(&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;modules&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// webpackBootstrap&lt;/span&gt; &lt;span class="hljs-comment"&gt;// The module cache&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; installedModules = {}; &lt;span class="hljs-comment"&gt;// The require function&lt;/span&gt; &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;require&lt;/span&gt;(&lt;span class="hljs-params"&gt;moduleId&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Check if module is in cache&lt;/span&gt; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (installedModules[moduleId]) { &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; installedModules[moduleId].exports; } &lt;span class="hljs-comment"&gt;// Create a new module (and put it into the cache)&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt; = installedModules[moduleId] = { &lt;span class="hljs-attr"&gt;i&lt;/span&gt;: moduleId, &lt;span class="hljs-attr"&gt;l&lt;/span&gt;: &lt;span class="hljs-literal"&gt;false&lt;/span&gt;, &lt;span class="hljs-attr"&gt;exports&lt;/span&gt;: {} }; &lt;span class="hljs-comment"&gt;// Execute the module function&lt;/span&gt; modules[moduleId].call(&lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports, &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;); &lt;span class="hljs-comment"&gt;// Flag the module as loaded&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.l = &lt;span class="hljs-literal"&gt;true&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// Return the exports of the module&lt;/span&gt; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports; } &lt;span class="hljs-comment"&gt;// expose the modules object (__webpack_modules__)&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.m = modules; &lt;span class="hljs-comment"&gt;// expose the module cache&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.c = installedModules; &lt;span class="hljs-comment"&gt;// define getter function for harmony exports&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.d = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports, name, getter&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (!&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.o(exports, name)) { &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.defineProperty(exports, name, { &lt;span class="hljs-attr"&gt;enumerable&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt;, &lt;span class="hljs-attr"&gt;get&lt;/span&gt;: getter }); } }; &lt;span class="hljs-comment"&gt;// define __esModule on exports&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.r = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Symbol&lt;/span&gt; !== &lt;span class="hljs-string"&gt;'undefined'&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-built_in"&gt;Symbol&lt;/span&gt;.toStringTag) { &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.defineProperty(exports, &lt;span class="hljs-built_in"&gt;Symbol&lt;/span&gt;.toStringTag, { &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: &lt;span class="hljs-string"&gt;'Module'&lt;/span&gt; }); } &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.defineProperty(exports, &lt;span class="hljs-string"&gt;'__esModule'&lt;/span&gt;, { &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt; }); }; &lt;span class="hljs-comment"&gt;// create a fake namespace object&lt;/span&gt; &lt;span class="hljs-comment"&gt;// mode &amp;amp; 1: value is a module id, require it&lt;/span&gt; &lt;span class="hljs-comment"&gt;// mode &amp;amp; 2: merge all properties of value into the ns&lt;/span&gt; &lt;span class="hljs-comment"&gt;// mode &amp;amp; 4: return value when already ns object&lt;/span&gt; &lt;span class="hljs-comment"&gt;// mode &amp;amp; 8|1: behave like require&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.t = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;value, mode&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (mode &amp;amp; &lt;span class="hljs-number"&gt;1&lt;/span&gt;) value = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(value); &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (mode &amp;amp; &lt;span class="hljs-number"&gt;8&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; value; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; ((mode &amp;amp; &lt;span class="hljs-number"&gt;4&lt;/span&gt;) &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; value === &lt;span class="hljs-string"&gt;'object'&lt;/span&gt; &amp;amp;&amp;amp; value &amp;amp;&amp;amp; value.__esModule) &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; value; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; ns = &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.create(&lt;span class="hljs-literal"&gt;null&lt;/span&gt;); &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.r(ns); &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.defineProperty(ns, &lt;span class="hljs-string"&gt;'default'&lt;/span&gt;, { &lt;span class="hljs-attr"&gt;enumerable&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt;, &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: value }); &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (mode &amp;amp; &lt;span class="hljs-number"&gt;2&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; value != &lt;span class="hljs-string"&gt;'string'&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;for&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;var&lt;/span&gt; key &lt;span class="hljs-keyword"&gt;in&lt;/span&gt; value) &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.d(ns, key, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;key&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; value[key]; }.bind(&lt;span class="hljs-literal"&gt;null&lt;/span&gt;, key)); &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ns; }; &lt;span class="hljs-comment"&gt;// getDefaultExport function for compatibility with non-harmony modules&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.n = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;module&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; getter = &lt;span class="hljs-built_in"&gt;module&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.__esModule ? &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;getDefault&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;[&lt;span class="hljs-string"&gt;'default'&lt;/span&gt;]; } : &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;getModuleExports&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;; }; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.d(getter, &lt;span class="hljs-string"&gt;'a'&lt;/span&gt;, getter); &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; getter; }; &lt;span class="hljs-comment"&gt;// Object.prototype.hasOwnProperty.call&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.o = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;object, property&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.prototype.hasOwnProperty.call(object, property); }; &lt;span class="hljs-comment"&gt;// __webpack_public_path__&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.p = &lt;span class="hljs-string"&gt;""&lt;/span&gt;; &lt;span class="hljs-comment"&gt;// Load entry module and return exports&lt;/span&gt; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.s = &lt;span class="hljs-number"&gt;0&lt;/span&gt;); })([ &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;module, exports, require&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.r(exports); &lt;span class="hljs-comment"&gt;// Use ES module: index.js.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; esCounterModule = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-number"&gt;1&lt;/span&gt;); esCounterModule[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].increase(); esCounterModule[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].reset(); }, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;module, exports, require&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.r(exports); &lt;span class="hljs-comment"&gt;// Define ES module: esCounterModule.js.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; amdDependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.n(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-number"&gt;2&lt;/span&gt;)); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; commonJSDependencyModule2 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;.n(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-number"&gt;3&lt;/span&gt;)); amdDependencyModule1.a.api1(); commonJSDependencyModule2.a.api2(); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; exports[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;] = { increase, reset }; }, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;module, exports, require&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; result; !&lt;span class="hljs-function"&gt;(&lt;span class="hljs-params"&gt;result = ((&lt;/span&gt;) =&amp;gt;&lt;/span&gt; { &lt;span class="hljs-comment"&gt;// Define AMD module: amdDependencyModule1.js&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; api1 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { api1 }; }).call(exports, &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, exports, &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;), result !== &lt;span class="hljs-literal"&gt;undefined&lt;/span&gt; &amp;amp;&amp;amp; (&lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = result)); }, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;module, exports, require&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Define CommonJS module: commonJSDependencyModule2.js&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-number"&gt;2&lt;/span&gt;); &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; api2 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; dependencyModule1.api1(); exports.api2 = api2; } ]);&lt;/div&gt;
&lt;p&gt;Again, it is just another IIFE. The code of all 4 files is transpiled to the code in 4 functions in an array. And that array is passed to the anonymous function as an argument.&lt;/p&gt;
&lt;h1&gt;Babel module: transpile from ES module&lt;/h1&gt;
&lt;p&gt;Babel is another transpiler to convert ES6+ JavaScript code to the older syntax for the older environment like older browsers. The above counter module in ES6 import/export syntax can be converted to the following babel module with new syntax replaced:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Babel.&lt;/span&gt; &lt;span class="hljs-built_in"&gt;Object&lt;/span&gt;.defineProperty(exports, &lt;span class="hljs-string"&gt;"__esModule"&lt;/span&gt;, { &lt;span class="hljs-attr"&gt;value&lt;/span&gt;: &lt;span class="hljs-literal"&gt;true&lt;/span&gt; }); exports[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;] = &lt;span class="hljs-keyword"&gt;void&lt;/span&gt; &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;_interopRequireDefault&lt;/span&gt;(&lt;span class="hljs-params"&gt;obj&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; obj &amp;amp;&amp;amp; obj.__esModule ? obj : { &lt;span class="hljs-string"&gt;"default"&lt;/span&gt;: obj }; } &lt;span class="hljs-comment"&gt;// Define ES module: esCounterModule.js.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule1 = _interopRequireDefault(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./amdDependencyModule1"&lt;/span&gt;)); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule2 = _interopRequireDefault(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./commonJSDependencyModule2"&lt;/span&gt;)); dependencyModule1[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api1(); dependencyModule2[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api2(); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; exports[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;] = { &lt;span class="hljs-attr"&gt;increase&lt;/span&gt;: increase, &lt;span class="hljs-attr"&gt;reset&lt;/span&gt;: reset };&lt;/div&gt;
&lt;p&gt;And here is the code in &lt;code&gt;index.js&lt;/code&gt; which consumes the counter module:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Babel.&lt;/span&gt; &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; &lt;span class="hljs-title"&gt;_interopRequireDefault&lt;/span&gt;(&lt;span class="hljs-params"&gt;obj&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; obj &amp;amp;&amp;amp; obj.__esModule ? obj : { &lt;span class="hljs-string"&gt;"default"&lt;/span&gt;: obj }; } &lt;span class="hljs-comment"&gt;// Use ES module: index.js&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; esCounterModule = _interopRequireDefault(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./esCounterModule.js"&lt;/span&gt;)); esCounterModule[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].increase(); esCounterModule[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].reset();&lt;/div&gt;
&lt;p&gt;This is the default transpilation. Babel can also work with other tools.&lt;/p&gt;
&lt;h2&gt;Babel with SystemJS&lt;/h2&gt;
&lt;p&gt;SystemJS can be used as a plugin for Babel:&lt;/p&gt;
&lt;div&gt;npm install --save-dev @babel/plugin-transform-modules-systemjs&lt;/div&gt;
&lt;p&gt;And it should be added to the Babel configuration &lt;code&gt;babel.config.json&lt;/code&gt;:&lt;/p&gt;
&lt;div&gt;{ &lt;span class="hljs-string"&gt;"plugins"&lt;/span&gt;: [&lt;span class="hljs-string"&gt;"@babel/plugin-transform-modules-systemjs"&lt;/span&gt;], &lt;span class="hljs-string"&gt;"presets"&lt;/span&gt;: [ [ &lt;span class="hljs-string"&gt;"@babel/env"&lt;/span&gt;, { &lt;span class="hljs-string"&gt;"targets"&lt;/span&gt;: { &lt;span class="hljs-string"&gt;"ie"&lt;/span&gt;: &lt;span class="hljs-string"&gt;"11"&lt;/span&gt; } } ] ] }&lt;/div&gt;
&lt;p&gt;Now Babel can work with SystemJS to transpile CommonJS/Node.js module, AMD/RequireJS module, and ES module:&lt;/p&gt;
&lt;div&gt;npx babel src --out-&lt;span class="hljs-built_in"&gt;dir&lt;/span&gt; lib&lt;/div&gt;
&lt;p&gt;The result is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;root
&lt;ul&gt;
&lt;li&gt;lib
&lt;ul&gt;
&lt;li&gt;amdDependencyModule1.js (Transpiled with SystemJS)&lt;/li&gt;
&lt;li&gt;commonJSDependencyModule2.js (Transpiled with SystemJS)&lt;/li&gt;
&lt;li&gt;esCounterModule.js (Transpiled with SystemJS)&lt;/li&gt;
&lt;li&gt;index.js (Transpiled with SystemJS)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;src
&lt;ul&gt;
&lt;li&gt;amdDependencyModule1.js&lt;/li&gt;
&lt;li&gt;commonJSDependencyModule2.js&lt;/li&gt;
&lt;li&gt;esCounterModule.js&lt;/li&gt;
&lt;li&gt;index.js&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;babel.config.json&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now all the ADM, CommonJS, and ES module syntax are transpiled to SystemJS syntax:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// Transpile AMD/RequireJS module definition to SystemJS syntax: lib/amdDependencyModule1.js.&lt;/span&gt; System.register([], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_export, _context&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Define AMD module: src/amdDependencyModule1.js&lt;/span&gt; define(&lt;span class="hljs-string"&gt;"amdDependencyModule1"&lt;/span&gt;, () =&amp;gt; { &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; api1 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { }; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { api1 }; }); } }; }); &lt;span class="hljs-comment"&gt;// Transpile CommonJS/Node.js module definition to SystemJS syntax: lib/commonJSDependencyModule2.js.&lt;/span&gt; System.register([], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_export, _context&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule1, api2; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Define CommonJS module: src/commonJSDependencyModule2.js&lt;/span&gt; dependencyModule1 = &lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./amdDependencyModule1"&lt;/span&gt;); api2 = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; dependencyModule1.api1(); exports.api2 = api2; } }; }); &lt;span class="hljs-comment"&gt;// Transpile ES module definition to SystemJS syntax: lib/esCounterModule.js.&lt;/span&gt; System.register([&lt;span class="hljs-string"&gt;"./amdDependencyModule1"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"./commonJSDependencyModule2"&lt;/span&gt;], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_export, _context&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule1, dependencyModule2, count, increase, reset; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_amdDependencyModule&lt;/span&gt;) &lt;/span&gt;{ dependencyModule1 = _amdDependencyModule.default; }, &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_commonJSDependencyModule&lt;/span&gt;) &lt;/span&gt;{ dependencyModule2 = _commonJSDependencyModule.default; }], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Define ES module: src/esCounterModule.js.&lt;/span&gt; dependencyModule1.api1(); dependencyModule2.api2(); count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; _export(&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;, { increase, reset }); } }; }); &lt;span class="hljs-comment"&gt;// Transpile ES module usage to SystemJS syntax: lib/index.js.&lt;/span&gt; System.register([&lt;span class="hljs-string"&gt;"./esCounterModule"&lt;/span&gt;], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_export, _context&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; esCounterModule; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;_esCounterModuleJs&lt;/span&gt;) &lt;/span&gt;{ esCounterModule = _esCounterModuleJs.default; }], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-comment"&gt;// Use ES module: src/index.js&lt;/span&gt; esCounterModule.increase(); esCounterModule.reset(); } }; });&lt;/div&gt;
&lt;h1&gt;TypeScript module: Transpile to CJS, AMD, ES, System modules&lt;/h1&gt;
&lt;p&gt;TypeScript supports all JavaScript syntax, including the ES6 module syntax &lt;a href="https://www.typescriptlang.org/docs/handbook/modules.html"&gt;https://www.typescriptlang.org/docs/handbook/modules.html&lt;/a&gt;. When TypeScript transpiles, the ES module code can either be kept as ES6, or transpiled to other formats, including CommonJS/Node.js, AMD/RequireJS, UMD/UmdJS, or System/SystemJS, according to the specified transpiler options in &lt;code&gt;tsconfig.json&lt;/code&gt;:&lt;/p&gt;
&lt;div&gt;{ &lt;span class="hljs-string"&gt;"compilerOptions"&lt;/span&gt;: { &lt;span class="hljs-string"&gt;"module"&lt;/span&gt;: &lt;span class="hljs-string"&gt;"ES2020"&lt;/span&gt;, &lt;span class="hljs-comment"&gt;// None, CommonJS, AMD, System, UMD, ES6, ES2015, ES2020, ESNext.&lt;/span&gt; } }&lt;/div&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-comment"&gt;// TypeScript and ES module.&lt;/span&gt; &lt;span class="hljs-comment"&gt;// With compilerOptions: { module: "ES6" }. Transpile to ES module with the same import/export syntax.&lt;/span&gt; &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; dependencyModule &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; &lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;; dependencyModule.api(); &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count }; &lt;span class="hljs-comment"&gt;// With compilerOptions: { module: "CommonJS" }. Transpile to CommonJS/Node.js module:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; __importDefault = (&lt;span class="hljs-keyword"&gt;this&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;this&lt;/span&gt;.__importDefault) || &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;mod&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (mod &amp;amp;&amp;amp; mod.__esModule) ? mod : { &lt;span class="hljs-string"&gt;"default"&lt;/span&gt;: mod }; }; exports.__esModule = &lt;span class="hljs-literal"&gt;true&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule_1 = __importDefault(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;)); dependencyModule_1[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api(); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; exports.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; &lt;span class="hljs-comment"&gt;// With compilerOptions: { module: "AMD" }. Transpile to AMD/RequireJS module:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; __importDefault = (&lt;span class="hljs-keyword"&gt;this&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;this&lt;/span&gt;.__importDefault) || &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;mod&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (mod &amp;amp;&amp;amp; mod.__esModule) ? mod : { &lt;span class="hljs-string"&gt;"default"&lt;/span&gt;: mod }; }; define([&lt;span class="hljs-string"&gt;"require"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"exports"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;require, exports, dependencyModule_1&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; exports.__esModule = &lt;span class="hljs-literal"&gt;true&lt;/span&gt;; dependencyModule_1 = __importDefault(dependencyModule_1); dependencyModule_1[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api(); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; exports.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; }); &lt;span class="hljs-comment"&gt;// With compilerOptions: { module: "UMD" }. Transpile to UMD/UmdJS module:&lt;/span&gt; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; __importDefault = (&lt;span class="hljs-keyword"&gt;this&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;this&lt;/span&gt;.__importDefault) || &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;mod&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; (mod &amp;amp;&amp;amp; mod.__esModule) ? mod : { &lt;span class="hljs-string"&gt;"default"&lt;/span&gt;: mod }; }; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;factory&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt; === &lt;span class="hljs-string"&gt;"object"&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports === &lt;span class="hljs-string"&gt;"object"&lt;/span&gt;) { &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; v = factory(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;, exports); &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (v !== &lt;span class="hljs-literal"&gt;undefined&lt;/span&gt;) &lt;span class="hljs-built_in"&gt;module&lt;/span&gt;.exports = v; } &lt;span class="hljs-keyword"&gt;else&lt;/span&gt; &lt;span class="hljs-keyword"&gt;if&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt; define === &lt;span class="hljs-string"&gt;"function"&lt;/span&gt; &amp;amp;&amp;amp; define.amd) { define([&lt;span class="hljs-string"&gt;"require"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"exports"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;], factory); } })(&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;require, exports&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; exports.__esModule = &lt;span class="hljs-literal"&gt;true&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule_1 = __importDefault(&lt;span class="hljs-built_in"&gt;require&lt;/span&gt;(&lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;)); dependencyModule_1[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api(); &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; exports.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; }); &lt;span class="hljs-comment"&gt;// With compilerOptions: { module: "System" }. Transpile to System/SystemJS module:&lt;/span&gt; System.register([&lt;span class="hljs-string"&gt;"./dependencyModule"&lt;/span&gt;], &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;exports_1, context_1&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-meta"&gt; "use strict"&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; dependencyModule_1, count, increase; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; __moduleName = context_1 &amp;amp;&amp;amp; context_1.id; &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; { &lt;span class="hljs-attr"&gt;setters&lt;/span&gt;: [ &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;dependencyModule_1_1&lt;/span&gt;) &lt;/span&gt;{ dependencyModule_1 = dependencyModule_1_1; } ], &lt;span class="hljs-attr"&gt;execute&lt;/span&gt;: &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ dependencyModule_1[&lt;span class="hljs-string"&gt;"default"&lt;/span&gt;].api(); count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; exports_1(&lt;span class="hljs-string"&gt;"increase"&lt;/span&gt;, increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }); } }; });&lt;/div&gt;
&lt;p&gt;The ES module syntax supported in TypeScript was called external modules.&lt;/p&gt;
&lt;h2&gt;Internal module and namespace&lt;/h2&gt;
&lt;p&gt;TypeScript also has a &lt;code&gt;module&lt;/code&gt; keyword and a &lt;code&gt;namespace&lt;/code&gt; keyword &lt;a href="https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#pitfalls-of-namespaces-and-modules"&gt;https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#pitfalls-of-namespaces-and-modules&lt;/a&gt;. They were called internal modules:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;module&lt;/span&gt; Counter { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; } &lt;span class="hljs-keyword"&gt;namespace&lt;/span&gt; Counter { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; { count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; }&lt;/div&gt;
&lt;p&gt;They are both transpiled to JavaScript objects:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;var&lt;/span&gt; Counter; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;Counter&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; Counter.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; Counter.reset = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-built_in"&gt;console&lt;/span&gt;.log(&lt;span class="hljs-string"&gt;"Count is reset."&lt;/span&gt;); }; })(Counter || (Counter = {}));&lt;/div&gt;
&lt;p&gt;TypeScript module and namespace can have multiple levels by supporting the &lt;code&gt;.&lt;/code&gt; separator:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;module&lt;/span&gt; Counter.Sub { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; } &lt;span class="hljs-keyword"&gt;namespace&lt;/span&gt; Counter.Sub { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; }&lt;/div&gt;
&lt;p&gt;The the sub module and sub namespace are both transpiled to object&amp;rsquo;s property:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;var&lt;/span&gt; Counter; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;Counter&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; Sub; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;Sub&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; Sub.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; })(Sub = Counter.Sub || (Counter.Sub = {})); })(Counter|| (Counter = {}));&lt;/div&gt;
&lt;p&gt;TypeScript module and namespace can also be used in the &lt;code&gt;export&lt;/code&gt; statement:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;module&lt;/span&gt; Counter { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;module&lt;/span&gt; Sub { &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; } } &lt;span class="hljs-keyword"&gt;module&lt;/span&gt; Counter { &lt;span class="hljs-keyword"&gt;let&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;namespace&lt;/span&gt; Sub { &lt;span class="hljs-keyword"&gt;export&lt;/span&gt; &lt;span class="hljs-keyword"&gt;const&lt;/span&gt; increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt; =&amp;gt;&lt;/span&gt; ++count; } }&lt;/div&gt;
&lt;p&gt;The transpilation is the same as submodule and sub-namespace:&lt;/p&gt;
&lt;div&gt;&lt;span class="hljs-keyword"&gt;var&lt;/span&gt; Counter; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;Counter&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; count = &lt;span class="hljs-number"&gt;0&lt;/span&gt;; &lt;span class="hljs-keyword"&gt;var&lt;/span&gt; Sub; (&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;Sub&lt;/span&gt;) &lt;/span&gt;{ Sub.increase = &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;function&lt;/span&gt; (&lt;span class="hljs-params"&gt;&lt;/span&gt;) &lt;/span&gt;{ &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; ++count; }; })(Sub = Counter.Sub || (Counter.Sub = {})); })(Counter || (Counter = {}));&lt;/div&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Welcome to JavaScript, which has so much drama - 10+ systems/formats just for modularization/namespace:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;IIFE module: JavaScript &lt;strong&gt;module pattern&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Revealing module: JavaScript &lt;strong&gt;revealing module pattern&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CJS module&lt;/strong&gt;: CommonJS module, or Node.js module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AMD module&lt;/strong&gt;: Asynchronous Module Definition, or RequireJS module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;UMD module&lt;/strong&gt;: Universal Module Definition, or UmdJS module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ES module&lt;/strong&gt;: ECMAScript 2015, or ES6 module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ES dynamic module&lt;/strong&gt;: ECMAScript 2020, or ES11 dynamic module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;System module&lt;/strong&gt;: SystemJS module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Webpack module&lt;/strong&gt;: transpile and bundle of CJS, AMD, ES modules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Babel module&lt;/strong&gt;: transpile ES module&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TypeScript module&lt;/strong&gt; and namespace&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Fortunately, now JavaScript has standard built-in language features for modules, and it is supported by Node.js and all the latest modern browsers. For the older environments, you can still code with the new ES module syntax, then use Webpack/Babel/SystemJS/TypeScript to transpile to older or compatible syntax.&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sun, 09 Feb 2025 18:15:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/understanding-all-javascript-module-formats-and-tools</guid><category>JavaScript</category><category>TypeScript</category><category>Web</category><category>Webpack</category><category>Babel</category><category>CommonJS</category><category>Node.js</category><category>AMD</category><category>UMD</category></item><item><title>Port Microsoft Concurrency Visualizer SDK to .NET Standard and NuGet</title><link>https://weblogs.asp.net:443/dixin/port-microsoft-concurrency-visualizer-sdk-to-net-standard-and-nuget</link><description>&lt;p&gt;I uploaded a NuGet package of Microsoft Concurrency Visualizer SDK: &lt;a href="https://www.nuget.org/packages/ConcurrencyVisualizer/" target="_blank"&gt;ConcurrencyVisualizer&lt;/a&gt;. &lt;a href="https://docs.microsoft.com/en-us/visualstudio/profiling/concurrency-visualizer" target="_blank"&gt;Microsoft Concurrency Visualizer&lt;/a&gt; is an extension tool for Visual Studio. It is a great tool for performance profiling and multithreading execution visualization. It also has &lt;g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="21" data-gr-id="21"&gt;a &lt;/g&gt;&lt;a href="https://docs.microsoft.com/en-us/visualstudio/profiling/concurrency-visualizer-sdk" target="_blank"&gt;&lt;g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" id="21" data-gr-id="21"&gt;SDK&lt;/g&gt; library&lt;/a&gt; to be invoked by code and draw markers and spans in the timeline. I used it to visualize sequential LINQ and Parallel LINQ (PLINQ) execution in my Functional Programming and LINQ tutorials. For example, array.Where(&amp;hellip;).Select(&amp;hellip;) sequential LINQ query and array.AsParallel().Where(&amp;hellip;).Select(&amp;hellip;) Parallel LINQ query can be visualized as following spans:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb11_thumb[6].png"&gt;&lt;img width="800" height="600" title="image_thumb11_thumb[6]" style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" alt="image_thumb11_thumb[6]" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb11_thumb[6]_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The Concurrency Visualizer is available in Visual Studio Marketplace:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb[4].png"&gt;&lt;img width="882" height="443" title="image_thumb[4]" style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" alt="image_thumb[4]" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb[4]_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And the SDK library is available for .NET Framework, and can be installed from &lt;g class="gr_ gr_23 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="23" data-gr-id="23"&gt;menu&lt;/g&gt; item:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb1[4].png"&gt;&lt;img width="709" height="249" title="image_thumb1[4]" style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" alt="image_thumb1[4]" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb1[4]_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This adds a reference to local assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\4hhyuhoo.ghy\SDK\Managed\4.0\Microsoft.ConcurrencyVisualizer.Markers.dll. For your convenience, I have made a NuGet package:&lt;/p&gt;
&lt;pre&gt;Install-Package ConcurrencyVisualizer&lt;/pre&gt;
&lt;p&gt;It makes your code more &lt;g class="gr_ gr_22 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="22" data-gr-id="22"&gt;potable&lt;/g&gt;. I have also ported the code to .NET Standard, so now the SDK can work with .NET Framework, .NET Core, etc.:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb2[4].png"&gt;&lt;img width="736" height="317" title="image_thumb2[4]" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image_thumb2[4]" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb2[4]_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now the Concurrency Visualizer tool can visualize markers and spans of .NET Core correctly.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb4_thumb[4].png"&gt;&lt;img width="800" height="600" title="image_thumb4_thumb[4]" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image_thumb4_thumb[4]" src="https://aspblogs.blob.core.windows.net/media/dixin/Open-Live-Writer/Port-Microsoft-Co.NET-Standard-and-NuGet_E96/image_thumb4_thumb[4]_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Fri, 24 Jan 2025 18:27:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/port-microsoft-concurrency-visualizer-sdk-to-net-standard-and-nuget</guid><category>Concurrency VIsualizer</category><category>Visual Studio</category><category>.NET</category><category>.NET Core</category><category>.NET Standard</category><category>NuGet</category><category>SDK</category></item><item><title>TransientFaultHandling.Core: Retry library for .NET Core/.NET Standard</title><link>https://weblogs.asp.net:443/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard</link><description>&lt;p&gt;TransientFaultHandling.Core is retry library for transient error handling. It is ported from Microsoft Enterprise Library&amp;rsquo;s TransientFaultHandling library, a library widely used with .NET Framework. The retry pattern APIs are ported to .NET Core/.NET Standard, with outdated configuration API updated, and new retry APIs added for convenience.&lt;/p&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;With this library, the old code of retry logic based on Microsoft Enterprise Library can be ported to .NET Core/.NET Standard without modification:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;ITransientErrorDetectionStrategy transientExceptionDetection = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;MyDetection();
RetryStrategy retryStrategy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;FixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1));
RetryPolicy retryPolicy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;RetryPolicy(transientExceptionDetection, retryStrategy);&lt;/span&gt;&lt;span style="color: black;"&gt;
string result = retryPolicy.ExecuteAction(() =&amp;gt; webClient.DownloadString(&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;));
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With this library, it is extremely easy to detect transient exception and implement retry logic. For example, the following code downloads a string, if the exception thrown is transient (a WebException), it retries up to 5 times, and it waits for 1 second between retries:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry.FixedInterval(
    () =&amp;gt; webClient.DownloadString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;),
    isTransient: exception =&amp;gt; exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;WebException,
    retryCount: 5, retryInterval: TimeSpan.FromSeconds(1));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Fluent APIs are also provided for even better readability:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry
    .WithIncremental(retryCount: 5, initialInterval: TimeSpan.FromSeconds(1),
        increment: TimeSpan.FromSeconds(1))
    .Catch&amp;lt;OperationCanceledException&amp;gt;()
    .Catch&amp;lt;WebException&amp;gt;(exception =&amp;gt;
        exception.Response &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;HttpWebResponse response &amp;amp;&amp;amp; response.StatusCode == HttpStatusCode.RequestTimeout)
    .ExecuteAction(() =&amp;gt; webClient.DownloadString(&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It also supports JSON/XML/INI configuration:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;{
  &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryStrategy"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
    &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"name1"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"fastFirstRetry"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"true"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryCount"&lt;/span&gt;&lt;span style="color: black;"&gt;: 5,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryInterval"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.1"
    &lt;/span&gt;&lt;span style="color: black;"&gt;},
    &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"name2"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"fastFirstRetry"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"true"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryCount"&lt;/span&gt;&lt;span style="color: black;"&gt;: 55,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"initialInterval"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.2"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"increment"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.3"
    &lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;
  }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Document&lt;/h1&gt;
&lt;p&gt;&lt;a title="https://weblogs.asp.net/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard" href="https://weblogs.asp.net/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard"&gt;https://weblogs.asp.net/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;Source&lt;/h1&gt;
&lt;p&gt;&lt;a title="https://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core" href="https://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core"&gt;https://github.com/Dixin/EnterpriseLibrary.TransientFaultHandling.Core&lt;/a&gt; (Partially ported from &lt;a href="https://github.com/MicrosoftArchive/transient-fault-handling-application-block" target="_blank"&gt;Topaz&lt;/a&gt;, with additional new APIs and updated configuration APIs).&lt;/p&gt;
&lt;h1&gt;NuGet installation&lt;/h1&gt;
&lt;p&gt;It can be installed through &lt;a href="https://www.nuget.org/packages/EntityFramework.Functions"&gt;NuGet&lt;/a&gt; using .NET CLI:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;dotnet add package EnterpriseLibrary.TransientFaultHandling.Core&lt;/p&gt;
&lt;p&gt;dotnet add package TransientFaultHandling.Caching&lt;/p&gt;
&lt;p&gt;dotnet add package TransientFaultHandling.Configuration&lt;/p&gt;
&lt;p&gt;dotnet add package TransientFaultHandling.Data&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Or in Visual Studio NuGet Package Manager Console:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Install-Package EnterpriseLibrary.TransientFaultHandling.Core&lt;/p&gt;
&lt;p&gt;Install-Package TransientFaultHandling.Caching&lt;/p&gt;
&lt;p&gt;Install-Package TransientFaultHandling.Configuration&lt;/p&gt;
&lt;p&gt;Install-Package TransientFaultHandling.Data&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;Backward compatibility with Enterprise Library&lt;/h1&gt;
&lt;p&gt;This library provides maximum backward compatibility with Microsoft Enterprise Library&amp;rsquo;s TransientFaultHandling (aka Topaz) for .NET Framework:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you have code using EnterpriseLibrary.TransientFaultHandling, you can port your code to use EnterpriseLibrary.TransientFaultHandling.Core, without any modification.&lt;/li&gt;
&lt;li&gt;If you have code using EnterpriseLibrary.TransientFaultHandling.Caching, you can port your code to use TransientFaultHandling.Caching, without any modification.&lt;/li&gt;
&lt;li&gt;If you have code using EnterpriseLibrary.TransientFaultHandling.Data, you can port your code to use TransientFaultHandling.Data, without any modification.&lt;/li&gt;
&lt;li&gt;If you have code and configuration based on EnterpriseLibrary.TransientFaultHandling.Configuration, you have to change your code and configuration to use TransientFaultHandling.Configuration. The old XML configuration infrastructure based on .NET Framework is outdated. You need to replace the old XML format with new XML/JSON/INI format configuration supported by .NET Core/.NET Standard.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;How to use the APIs&lt;/h1&gt;
&lt;p&gt;For retry pattern, please read Microsoft&amp;rsquo;s &lt;a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/retry" target="_blank"&gt;introduction in Cloud Design Patterns&lt;/a&gt;. For the introduction of transient fault handling, read Microsoft&amp;rsquo;s &lt;a href="https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn440719(v=pandp.60)" target="_blank"&gt;Perseverance, Secret of All Triumphs: Using the Transient Fault Handling Application Block&lt;/a&gt; and Microsoft Azure Architecture Center&amp;rsquo;s &lt;a href="https://docs.microsoft.com/en-us/azure/architecture/best-practices/transient-faults" target="_blank"&gt;Best practice - Transient fault handling&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Object-oriented APIs from Enterprise Library&lt;/h2&gt;
&lt;p&gt;Enterprise Library existing APIs follows an object-oriented design. For the details, please see Microsoft&amp;rsquo;s &lt;a href="https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn170426(v=pandp.60)" target="_blank"&gt;API reference&lt;/a&gt; and ebook &lt;a href="http://go.microsoft.com/fwlink/p/?linkid=290904" target="_blank"&gt;Developer's Guide to Microsoft Enterprise Library&lt;/a&gt;&amp;lsquo;s Chapter 4, Using the Transient Fault Handling Application Block. Here is a brief introduction.&lt;/p&gt;
&lt;p&gt;First, ITransientErrorDetectionStrategy interface must be implemented. It has a single method IsTransient to detected if the thrown exception is transient and retry should be executed.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MyDetection &lt;/span&gt;&lt;span style="color: black;"&gt;: ITransientErrorDetectionStrategy
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;IsTransient(Exception exception) =&amp;gt; 
        exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;OperationCanceledException;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Second, a retry strategy must be defined to specify how the retry is executed, like retry count, retry interval, etc.. a retry strategy must inherit RetryStrategy abstract class. There are 3 built-in retry strategies: FixedInterval, Incremental, ExponentialBackoff.&lt;/p&gt;
&lt;p&gt;Then a retry policy (RetryPolicy class) must be instantiated with a retry strategy and an ITransientErrorDetectionStrategy interface. a retry policy has an ExecuteAction method to execute the specified synchronous function, and an ExecuteAsync method to execute a\the specified async function. It also has a Retrying event. When the executed sync/async function throws an exception, if the exception is detected to be transient and max retry count is not reached, then it waits for the specified retry interval, and then it fires the Retrying event, and execute the specified sync/async function again.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;RetryStrategy retryStrategy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;FixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1));

RetryPolicy retryPolicy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;RetryPolicy(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;MyDetection(), retryStrategy);
retryPolicy.Retrying += (sender, args) =&amp;gt;
    Console.WriteLine(&lt;/span&gt;&lt;span style="color: maroon;"&gt;$@"&lt;/span&gt;&lt;span style="color: black;"&gt;{args.CurrentRetryCount}&lt;/span&gt;&lt;span style="color: maroon;"&gt;: &lt;/span&gt;&lt;span style="color: black;"&gt;{args.LastException}&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);

&lt;/span&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;&lt;span style="color: black;"&gt;(WebClient webClient = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;WebClient())
{
    string result1 = retryPolicy.ExecuteAction(() =&amp;gt; webClient.DownloadString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;));
    string result2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;retryPolicy.ExecuteAsync(() =&amp;gt; webClient.DownloadStringTaskAsync(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;));
}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;New functional APIs: single function call for retry&lt;/h2&gt;
&lt;p&gt;The above object-oriented API design is very inconvenient. New static functions Retry.FixedInterval, Retry.Incremental, Retry.ExponentialBackoff are added to implement retry with a single function call. For example:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry.FixedInterval(
    () =&amp;gt; webClient.DownloadString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;),
    isTransient: exception =&amp;gt; exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;OperationCanceledException,
    retryCount: 5, retryInterval: TimeSpan.FromSeconds(1),
    retryingHandler: (sender, args) =&amp;gt;
        Console.WriteLine(&lt;/span&gt;&lt;span style="color: maroon;"&gt;$@"&lt;/span&gt;&lt;span style="color: black;"&gt;{args.CurrentRetryCount}&lt;/span&gt;&lt;span style="color: maroon;"&gt;: &lt;/span&gt;&lt;span style="color: black;"&gt;{args.LastException}&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;));

&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;Retry.IncrementalAsync(
    () =&amp;gt; webClient.DownloadStringTaskAsync(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;),
    isTransient: exception =&amp;gt; exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;OperationCanceledException,
    retryCount: 5, initialInterval: TimeSpan.FromSeconds(1), increment: TimeSpan.FromSeconds(2));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;These sync and async functions are very convenient because only the first argument (action to execute) is required. All the other arguments are optional. And a function can be defined inline to detect transient exception, instead of defining a type to implement an interface:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Treat any exception as transient. Use default retry count, default interval. No event handler.
&lt;/span&gt;&lt;span style="color: black;"&gt;Retry.FixedInterval(() =&amp;gt; webClient.DownloadString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;));

&lt;/span&gt;&lt;span style="color: green;"&gt;// Treat any exception as transient. Specify retry count. Use default initial interval, default increment. No event handler.
&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;Retry.IncrementalAsync(
    () =&amp;gt; webClient.DownloadStringTaskAsync(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"https://DixinYan.com"&lt;/span&gt;&lt;span style="color: black;"&gt;),
    retryCount: 10);&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;New fluent APIs for retry&lt;/h2&gt;
&lt;p&gt;For better readability, new fluent APIs are provided:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry
    .WithFixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1))
    .Catch(exception =&amp;gt;
        exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;OperationCanceledException ||
        exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;HttpListenerException httpListenerException &amp;amp;&amp;amp; httpListenerException.ErrorCode == 404)
    .HandleWith((sender, args) =&amp;gt;
        Console.WriteLine(&lt;/span&gt;&lt;span style="color: maroon;"&gt;$@"&lt;/span&gt;&lt;span style="color: black;"&gt;{args.CurrentRetryCount}&lt;/span&gt;&lt;span style="color: maroon;"&gt;: &lt;/span&gt;&lt;span style="color: black;"&gt;{args.LastException}&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;))
    .ExecuteAction(() =&amp;gt; MyTask());&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The HandleWith call adds an event handler to the Retying event. It is optional:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry
    .WithFixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1))
    .Catch(exception =&amp;gt;
        exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;OperationCanceledException ||
        exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: black;"&gt;HttpListenerException httpListenerException &amp;amp;&amp;amp; httpListenerException.ErrorCode == 404)&lt;/span&gt;&lt;span style="color: black;"&gt;
    .ExecuteAction(() =&amp;gt; MyTask());&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Catch method has a generic overload. The above code is equivalent to:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry
    .WithFixedInterval(retryCount: 5, retryInterval: TimeSpan.FromSeconds(1))
    .Catch&amp;lt;OperationCanceledException&amp;gt;()
    .Catch&amp;lt;HttpListenerException&amp;gt;(exception =&amp;gt; exception.ErrorCode == 404)
    .ExecuteAction(() =&amp;gt; MyTask());&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following code &amp;ldquo;catches&amp;rdquo; any exception as transient:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Retry
    .WithIncremental(retryCount: 5, increment: TimeSpan.FromSeconds(1)) &lt;/span&gt;&lt;span style="color: green;"&gt;// Use default initial interval.
    &lt;/span&gt;&lt;span style="color: black;"&gt;.Catch() &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to: .Catch&amp;lt;Exception&amp;gt;()
    &lt;/span&gt;&lt;span style="color: black;"&gt;.ExecuteAction(() =&amp;gt; MyTask());&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Old XML configuration for retry&lt;/h2&gt;
&lt;p&gt;Ditched the following old XML format from .NET Framework:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515;"&gt;xml &lt;/span&gt;&lt;span style="color: red;"&gt;version&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;encoding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;utf-8&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;section &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;RetryPolicyConfiguration&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Configuration&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;RetryPolicyConfiguration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fixedInterval &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;FixedIntervalDefault&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;maxRetryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;retryInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;00:00:00.1&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;incremental &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;IncrementalIntervalDefault&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;maxRetryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;initialInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;00:00:00.01&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;retryIncrement&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;00:00:00.05&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;exponentialBackoff &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;ExponentialIntervalDefault&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;maxRetryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;minBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;100&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;maxBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;1000&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;deltaBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;100&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;RetryPolicyConfiguration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;These old XML infrastructures are outdated. Use new XML/JSON/INI format configuration supported by .NET Standard/.NET Core.&lt;/p&gt;
&lt;h2&gt;New XML/JSON/INI configuration for retry&lt;/h2&gt;
&lt;p&gt;Please install TransientFaultHandling.Configuration package. The following is an example JSON configuration file app.json. It has 3 retry strategies, a FixedInterval retry strategy, a Incremental retry strategy, and an ExponentialBackoff retry strategy:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;{
  &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryStrategy"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
    &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"name1"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"fastFirstRetry"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"true"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryCount"&lt;/span&gt;&lt;span style="color: black;"&gt;: 5,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryInterval"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.1"
    &lt;/span&gt;&lt;span style="color: black;"&gt;},
    &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"name2"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"fastFirstRetry"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"true"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryCount"&lt;/span&gt;&lt;span style="color: black;"&gt;: 55,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"initialInterval"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.2"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"increment"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.3"
    &lt;/span&gt;&lt;span style="color: black;"&gt;},
    &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"name3"&lt;/span&gt;&lt;span style="color: black;"&gt;: {
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"fastFirstRetry"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"true"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"retryCount"&lt;/span&gt;&lt;span style="color: black;"&gt;: 555,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"minBackoff"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.4"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"maxBackoff"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.5"&lt;/span&gt;&lt;span style="color: black;"&gt;,
      &lt;/span&gt;&lt;span style="color: #2e75b6;"&gt;"deltaBackoff"&lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"00:00:00.6"
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
  }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The same configuration file app.xml in XML format:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515;"&gt;xml &lt;/span&gt;&lt;span style="color: red;"&gt;version&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;&lt;span style="color: black;"&gt;" &lt;/span&gt;&lt;span style="color: red;"&gt;encoding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;UTF-8&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;name1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;true&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;5&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.1&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;name2&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;true&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;55&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;initialInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.2&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;initialInterval&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;increment&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.3&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;increment&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;name3&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;true&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;555&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryCount&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;minBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.4&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;minBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;maxBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.5&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;maxBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;deltaBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;00:00:00.6&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;deltaBackoff&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;retryStrategy&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And app.ini file in INI format:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af;"&gt;[retryStrategy:name1]
&lt;/span&gt;&lt;span style="color: blue;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: black;"&gt;=true
&lt;/span&gt;&lt;span style="color: blue;"&gt;retryCount&lt;/span&gt;&lt;span style="color: black;"&gt;=5
&lt;/span&gt;&lt;span style="color: blue;"&gt;retryInterval&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.1

&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;[retryStrategy:name2]
&lt;/span&gt;&lt;span style="color: blue;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: black;"&gt;=true
&lt;/span&gt;&lt;span style="color: blue;"&gt;retryCount&lt;/span&gt;&lt;span style="color: black;"&gt;=55
&lt;/span&gt;&lt;span style="color: blue;"&gt;initialInterval&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.2
&lt;/span&gt;&lt;span style="color: blue;"&gt;increment&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.3

&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;[retryStrategy:name3]
&lt;/span&gt;&lt;span style="color: blue;"&gt;fastFirstRetry&lt;/span&gt;&lt;span style="color: black;"&gt;=true
&lt;/span&gt;&lt;span style="color: blue;"&gt;retryCount&lt;/span&gt;&lt;span style="color: black;"&gt;=5555
&lt;/span&gt;&lt;span style="color: blue;"&gt;minBackoff&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.4
&lt;/span&gt;&lt;span style="color: blue;"&gt;maxBackoff&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.5
&lt;/span&gt;&lt;span style="color: blue;"&gt;deltaBackoff&lt;/span&gt;&lt;span style="color: black;"&gt;=00:00:00.6&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;These configurations can be easily loaded and deserialized into retry strategy instances:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IConfiguration configuration = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;ConfigurationBuilder()
    .AddJsonFile(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"app.json"&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: green;"&gt;// or AddXml("app.xml") or AddIni("app.ini")
    &lt;/span&gt;&lt;span style="color: black;"&gt;.Build();

IDictionary&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, RetryStrategy&amp;gt; retryStrategies = configuration.GetRetryStrategies();
&lt;/span&gt;&lt;span style="color: green;"&gt;// or retryStrategies = configuration.GetRetryStrategies("yourConfigurationSectionKey");
// The default configuration section key is "retryStrategy".&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The GetRetryStrategies extension method returns a dictionary of key value pairs, where each key is the specified name of retry strategy, and each value is the retry strategy instance. Here the first key is &amp;ldquo;name1&amp;rdquo;, the first value is a FixedInterval retry strategy instance. The second key is &amp;ldquo;anme2&amp;rdquo;, the second value is Incremental retry strategy instance. The third key is &amp;ldquo;name3&amp;rdquo;, the third value is ExponentialBackoff retry strategy instance. This extension method can also accept custom configuration section key, and a function to create instance of custom retry strategy type.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;retryStrategies = configuration.GetRetryStrategies(
    key: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"yourConfigurationSectionKey"&lt;/span&gt;&lt;span style="color: black;"&gt;,
    getCustomRetryStrategy: configurationSection =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: red;"&gt;MyRetryStrategyType&lt;/span&gt;&lt;span style="color: black;"&gt;(...));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The other generic overload can filter the specified retry strategy type:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;FixedInterval retryStrategy = configuration.GetRetryStrategies&amp;lt;FixedInterval&amp;gt;().Single().Value;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It still returns a dictionary, which only has the specified type of retry strategies.&lt;/p&gt;
&lt;h1&gt;TransientFaultHandling.Data.Core: SQL Server support&lt;/h1&gt;
&lt;p&gt;Since 2.1.0, both Microsoft.Data.SqlClient and System.Data.SqlClient are supported. A API breaking change is introduced for this. If you are using the latest Microsoft.Data.SqlClient, no code change is needed. If you are using the legacy System.Data.SqlClient, the following types are renamed with a Legacy suffix:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ReliableSqlConnection &amp;ndash;&amp;gt; ReliableSqlConnection&lt;strong&gt;Legacy&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;SqlDatabaseTransientErrorDetectionStrategy &amp;ndash;&amp;gt; SqlDatabaseTransientErrorDetectionStrategy&lt;strong&gt;Legacy&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;SqlAzureTransientErrorDetectionStrategy &amp;ndash;&amp;gt; SqlAzureTransientErrorDetectionStrategy&lt;strong&gt;Legacy&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can either rename these types or add the using directives:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;&lt;span style="color: black;"&gt;ReliableSqlConnection = Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.ReliableSqlConnectionLegacy;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;&lt;span style="color: black;"&gt;SqlDatabaseTransientErrorDetectionStrategy = Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.SqlDatabaseTransientErrorDetectionStrategyLegacy;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;&lt;span style="color: black;"&gt;SqlAzureTransientErrorDetectionStrategy = Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure.SqlAzureTransientErrorDetectionStrategyLegacy;&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;History&lt;/h1&gt;
&lt;p&gt;This library follows the &lt;a href="http://semver.org/"&gt;http://semver.org&lt;/a&gt; standard for semantic versioning.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1.0.0: Initial release. Ported EnterpriseLibrary.TransientFaultHandling from .NET Framework to .NET Core/.NET Standard.&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;1.1.0: Add functional APIs for retry.&lt;/li&gt;
&lt;li&gt;1.2.0: Add functional APIs for retry.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;2.0.0: Add fluent APIs for retry. Ported EnterpriseLibrary.TransientFaultHandling.Caching from .NET Framework to .NET Core/.NET Standard. Ported EnterpriseLibrary.TransientFaultHandling.Data from .NET Framework to .NET Core/.NET Standard. Redesigned/reimplemented EnterpriseLibrary.TransientFaultHandling.Configuration with JSON in .NET Core/.NET Standard.&lt;/li&gt;
&lt;li&gt;2.1.0: Add support for Microsoft.Data.SqlClient. Now both Microsoft.Data.SqlClient and System.Data.SqlClient are supported.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Wed, 22 Jan 2025 18:09:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/transientfaulthandling-core-retry-library-for-net-core-net-standard</guid><category>C#</category><category>.NET</category><category>.NET Core</category><category>.NET Standard</category><category>Retry</category><category>Exception Handling</category><category>Functional Programming</category></item><item><title>Category Theory via C# (8) Advanced LINQ to Monads</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-8-more-linq-to-monads</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;Monad is a powerful structure, with the LINQ support in C# language, monad enables chaining operations to build fluent workflow, which can be pure. With these features, monad can be used to manage I/O, state changes, exception handling, shared environment, logging/tracing, and continuation, etc., in the functional paradigm.&lt;/p&gt;
&lt;h1&gt;IO monad&lt;/h1&gt;
&lt;p&gt;IO is impure. As already demonstrated, the Lazy&amp;lt;&amp;gt; and Func&amp;lt;&amp;gt; monads can build purely function workflows consists of I/O operations. The I/O is produced only when the workflows is started. So the Func&amp;lt;&amp;gt; monad is also called IO monad (Again, Lazy&amp;lt;T&amp;gt; is just a wrapper of Func&amp;lt;T&amp;gt; factory function, so Lazy&amp;lt;&amp;gt; and Func&amp;lt;&amp;gt; can be viewed as equivalent.). Here, to be more intuitive, rename Func&amp;lt;&amp;gt; to IO&amp;lt;&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// IO: () -&amp;gt; T
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;();&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Func&amp;lt;T&amp;gt; or IO&amp;lt;T&amp;gt; is just a wrapper of T. Generally, the difference is, if a value T is obtained, effect is already produced; and if a Func&amp;lt;T&amp;gt; or IO&amp;lt;T&amp;gt; function wrapper is obtained, the effect can be delayed to produce, until explicitly calling this function to pull the wrapped T value. The following example is a simple comparison:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IOExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static string &lt;/span&gt;&lt;span style="color: black;"&gt;Impure()
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;filePath = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine();
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath);
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent;
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Pure()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; filePath = () =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; fileContent = () =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath());
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent;
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;IO()
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;ioResult1 = Impure(); &lt;/span&gt;&lt;span style="color: green;"&gt;// IO is produced.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ioResultWrapper = Pure(); &lt;/span&gt;&lt;span style="color: green;"&gt;// IO is not produced.&lt;span style="color: green;"&gt;

        &lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;ioResult2 = ioResultWrapper(); &lt;/span&gt;&lt;span style="color: green;"&gt;// IO is produced.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;IO&amp;lt;&amp;gt; monad is just Func&amp;lt;&amp;gt; monad:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IOExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (IO&amp;lt;TSource&amp;gt;, TSource -&amp;gt; IO&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; IO&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            () =&amp;gt;
            {
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value = source();
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value, selector(value)());
            };

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; IO&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; IO&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; () =&amp;gt; value;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (IO&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; IO&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            source.SelectMany(value =&amp;gt; selector(value).IO(), (value, result) =&amp;gt; result);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The (SelectMany, Wrap, Select) operations are defined so that the LINQ functor syntax (single from clause) and monad syntax (multiple from clauses) are enabled. The let clause is also enabled by Select, which provides great convenience.&lt;/p&gt;
&lt;p&gt;Some I/O operations, like above Console.ReadLine: () &amp;ndash;&amp;gt; string, and File.ReadAllText: string &amp;ndash;&amp;gt; string, returns a value T that can be wrapped IO&amp;lt;T&amp;gt;. There are other I/O operations that return void, like Console.WriteLine: string &amp;ndash;&amp;gt; void, etc. Since C# compiler does not allow void to be used as type argument of IO&amp;lt;void&amp;gt;, these operations can be viewed as returning a Unit value, which can be wrapped as IO&amp;lt;Uint&amp;gt;. The following methods help wrap IO&amp;lt;T&amp;gt; functions from I/O operations with or without return value:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; IO&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function) =&amp;gt;
    () =&amp;gt; function();

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; IO(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Action &lt;/span&gt;&lt;span style="color: black;"&gt;action) =&amp;gt;
    () =&amp;gt;
    {
        action();
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return default&lt;/span&gt;&lt;span style="color: black;"&gt;;
    };&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now the I/O workflow can be build as purely function LINQ query:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"File path:"&lt;/span&gt;&lt;span style="color: black;"&gt;)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;string&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"File encoding:"&lt;/span&gt;&lt;span style="color: black;"&gt;)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;string&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;encoding = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName)
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;string&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"File content:"&lt;/span&gt;&lt;span style="color: black;"&gt;)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit4 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(fileContent)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;.
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent.Length; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;result = query(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;IO&amp;lt;&amp;gt; monad works with both synchronous and asynchronous I/O operations. The async version of IO&amp;lt;T&amp;gt; is just IO&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;, and the async version of IO&amp;lt;Unit&amp;gt; is just IO&amp;lt;Task&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task &lt;/span&gt;&lt;span style="color: black;"&gt;WorkflowAsync()
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;using &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;HttpClient &lt;/span&gt;&lt;span style="color: black;"&gt;httpClient = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;HttpClient&lt;/span&gt;&lt;span style="color: black;"&gt;())
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IO&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"URI:"&lt;/span&gt;&lt;span style="color: black;"&gt;)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;. 
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;uri &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;string&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"File path:"&lt;/span&gt;&lt;span style="color: black;"&gt;)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Unit&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;string&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;downloadStreamTask &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: blue;"&gt;async &lt;/span&gt;&lt;span style="color: black;"&gt;() =&amp;gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;httpClient.GetStreamAsync(uri)) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Task&amp;lt;Stream&amp;gt;&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;writeFileTask &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: blue;"&gt;async &lt;/span&gt;&lt;span style="color: black;"&gt;() =&amp;gt; 
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;downloadStreamTask).CopyToAsync(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.Create(filePath))) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Task&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;messageTask &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;IO(&lt;/span&gt;&lt;span style="color: blue;"&gt;async &lt;/span&gt;&lt;span style="color: black;"&gt;() =&amp;gt;
                                {
                                    &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;writeFileTask;
                                    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"Downloaded &lt;/span&gt;&lt;span style="color: black;"&gt;{uri} &lt;/span&gt;&lt;span style="color: #a31515;"&gt;to &lt;/span&gt;&lt;span style="color: black;"&gt;{filePath}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);
                                }) &lt;/span&gt;&lt;span style="color: green;"&gt;// IO&amp;lt;Task&amp;gt;.
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;messageTask; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;query(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;State monad&lt;/h1&gt;
&lt;p&gt;In object-oriented programming, there is the state pattern to handle state changes. In functional programming, state change can be modeled with pure function. For pure function TSource &amp;ndash;&amp;gt; TResult, its state-involved version can be represented as a Tuple&amp;lt;TSource, TState&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;TResult, TState&amp;gt; function, which accepts some input value along with some input state, and returns some output value and some output state. This function can remains pure, because it can leave the input state unchanged, then either return the same old state, or create a new state and return it. To make this function monadic, break up the input tuple and curry the function to TSource &amp;ndash;&amp;gt; (TState &amp;ndash;&amp;gt; Tuple&amp;lt;TResult, TState&amp;gt;). Now the returned TState &amp;ndash;&amp;gt; Tuple&amp;lt;TResult, TState&amp;gt; function type can be given an alias called State:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// State: TState -&amp;gt; ValueTuple&amp;lt;T, TState&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;Value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;State) &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;state);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to fore mentioned Tuple&amp;lt;,&amp;gt; and Func&amp;lt;,&amp;gt; types, the above open generic type State&amp;lt;,&amp;gt; can be viewed as a type constructor of kind * &amp;ndash;&amp;gt; * &amp;ndash;&amp;gt; *. After partially applied with a first type argument TState, State&amp;lt;TState,&amp;gt; becomes a * &amp;ndash;&amp;gt; * type constructor. If it can be a functor and monad, then above stateful function becomes a monadic selector TSource &amp;ndash;&amp;gt; State&amp;lt;TState, TResult&amp;gt;. So the following (SelectMany, Wrap, Select) methods can be defined for State&amp;lt;TState,&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;StateExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (State&amp;lt;TState, TSource&amp;gt;, TSource -&amp;gt; State&amp;lt;TState, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; State&amp;lt;TState, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            oldState =&amp;gt;
            {
                (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;Value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;State) value = source(oldState);
                (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector &lt;/span&gt;&lt;span style="color: black;"&gt;Value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;State) result = selector(value.Value)(value.State);
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;newState = result.State;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(resultSelector(value.Value, result.Value), newState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output new state.
            &lt;/span&gt;&lt;span style="color: black;"&gt;};

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; State&amp;lt;TState, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; State&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt;
        oldState =&amp;gt; (value, oldState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output old state.

    // Select: (State&amp;lt;TState, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; State&amp;lt;TState, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            oldState =&amp;gt;
            {
                (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;Value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;State) value = source(oldState);
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;newState = value.State;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(selector(value.Value), newState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output new state.
            &lt;/span&gt;&lt;span style="color: black;"&gt;};
            &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to:            
            // source.SelectMany(value =&amp;gt; selector(value).State&amp;lt;TState, TResult&amp;gt;(), (value, result) =&amp;gt; result);
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;SelectMany and Select return a function that accepts an old state and outputs new state, State method returns a function that outputs the old state. Now this State&amp;lt;TState,&amp;gt; delegate type is the state monad, so a State&amp;lt;TState, T&amp;gt; function can be viewed as a wrapper of a T value, and this T value can be unwrapped in the monad workflow, with the from value in source syntax. State&amp;lt;TState, T&amp;gt; function also wraps the state information. To get/set the TState state in the monad workflow, the following GetState/SetState functions can be defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// GetState: () -&amp;gt; State&amp;lt;TState, TState&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() =&amp;gt;
    oldState =&amp;gt; (oldState, oldState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output old state.

// SetState: TState -&amp;gt; State&amp;lt;TState, Unit&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SetState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TState &lt;/span&gt;&lt;span style="color: black;"&gt;newState) =&amp;gt;
    oldState =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, newState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output new state.&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here GetState returns a State&amp;lt;TState, TState&amp;gt; function wrapping the state as value, so that the state can be extracted in the monad workflow with the same syntax that unwraps the value. SetState returns a State&amp;lt;TState, Unit&amp;gt; function, which ignores the old state, and wrap no value (represented by Unit) and outputs the the specified new value to the monad workflow. Generally, the state monad workflow can be demonstrated as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;initialState = &lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(initialState);
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;newState = &lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(newState);
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;resetState = &lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(resetState);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1 = oldState =&amp;gt; (1, oldState);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2 = oldState =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, newState);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source3 = &lt;/span&gt;&lt;span style="color: #a31515;"&gt;'@'&lt;/span&gt;&lt;span style="color: black;"&gt;.State&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(); &lt;/span&gt;&lt;span style="color: green;"&gt;// oldState =&amp;gt; 2, oldState).

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;[]&amp;gt; query =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1 &lt;/span&gt;&lt;span style="color: green;"&gt;// source1: State&amp;lt;string, int&amp;gt; = initialState =&amp;gt; (1, initialState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;state1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// GetState&amp;lt;int&amp;gt;(): State&amp;lt;string, string&amp;gt; = initialState =&amp;gt; (initialState, initialState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2 &lt;/span&gt;&lt;span style="color: green;"&gt;// source2: State&amp;lt;string, bool&amp;gt;3 = initialState =&amp;gt; (true, newState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;state2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// GetState&amp;lt;int&amp;gt;(): State&amp;lt;string, string&amp;gt; = newState =&amp;gt; (newState, newState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SetState(resetState) &lt;/span&gt;&lt;span style="color: green;"&gt;// SetState(resetState): State&amp;lt;string, Unit&amp;gt; = newState =&amp;gt; (default, resetState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;state3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// GetState(): State&amp;lt;string, string&amp;gt; = resetState =&amp;gt; (resetState, resetState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source3 &lt;/span&gt;&lt;span style="color: green;"&gt;// source3: State&amp;lt;string, char&amp;gt; = resetState =&amp;gt; (@, resetState).
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select new string&lt;/span&gt;&lt;span style="color: black;"&gt;[] { state1, state2, state3 }; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;[] Value, &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;State) result = query(initialState); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query with initial state.
    &lt;/span&gt;&lt;span style="color: black;"&gt;result.Value.WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// initialState newState resetState
    &lt;/span&gt;&lt;span style="color: black;"&gt;result.State.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Final state: resetState
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The state monad workflow is a State&amp;lt;TState, T&amp;gt; function, which is of type TState &amp;ndash;&amp;gt; Tuple&amp;lt;T, TState&amp;gt;. To execute the workflow, it must be called with a TState initial state. At runtime, when the workflow executes, the first operation in the workflow, also a TState &amp;ndash;&amp;gt; Tuple&amp;lt;T, TState&amp;gt; function, is called with the workflow&amp;rsquo;s initial state, and returns a output value and a output state; then the second operation, once again another TState &amp;ndash;&amp;gt; Tuple&amp;lt;T, TState&amp;gt; function, is called with the first operation&amp;rsquo;s output state, and outputs another output value and another output state; and so on. In this chaining, each operation function can wither return its original input state, or return a new state. This is how state changes through a workflow of pure functions.&lt;/p&gt;
&lt;p&gt;Take the factorial function as example. The factorial function can be viewed as a recursive function with a state &amp;ndash; the current product of the current recursion step, and apparently take the initial state (product) is 1. To calculate the factorial of 5, the recursive steps can be modeled as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(Value: 5, State: 1) =&amp;gt; (Value: 4, State: 1 * 5)&lt;/li&gt;
&lt;li&gt;(Value: 4, State: 1 * 5) =&amp;gt; (Value: 3, State: 1 * 5 * 4)&lt;/li&gt;
&lt;li&gt;(Value: 3, State: 1 * 5 * 4) =&amp;gt; (Value: 3, State: 1 * 5 * 4)&lt;/li&gt;
&lt;li&gt;(Value: 2, State: 1 * 5 * 4 * 3) =&amp;gt; (Value: 2, State: 1 * 5 * 4 * 3)&lt;/li&gt;
&lt;li&gt;(Value: 1, State: 1 * 5 * 4 * 3 * 2) =&amp;gt; (Value: 1, State: 1 * 5 * 4 * 3 * 2)&lt;/li&gt;
&lt;li&gt;(Value: 0, State: 1 * 5 * 4 * 3 * 2 * 1) =&amp;gt; (Value: 0, State: 1 * 5 * 4 * 3 * 2 * 1)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When the current integer becomes 0, the recursion terminates, and the final state (product) is the factorial result. So this recursive function is of type Tuple&amp;lt;int, int&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;int, int&amp;gt;. As fore mentioned, it can be curried to int &amp;ndash;&amp;gt; (int &amp;ndash;&amp;gt; Tuple&amp;lt;int, int&amp;gt;), which is equivalent to int &amp;ndash;&amp;gt; State&amp;lt;int, int&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// FactorialState: uint -&amp;gt; (uint -&amp;gt; (uint, uint))
// FactorialState: uint -&amp;gt; State&amp;lt;unit, uint&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; FactorialState(&lt;/span&gt;&lt;span style="color: blue;"&gt;uint &lt;/span&gt;&lt;span style="color: black;"&gt;current) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;state &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;uint, uint&amp;gt;.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;product = state
    &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;next = current - 1U
    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;current &amp;gt; 0U
        ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SetState(product * current) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;unit, Unit&amp;gt;.
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;FactorialState(next) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;uint, uint&amp;gt;.
            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;next)
        : next.State&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;uint, uint&amp;gt;.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static uint &lt;/span&gt;&lt;span style="color: black;"&gt;Factorial(&lt;/span&gt;&lt;span style="color: blue;"&gt;uint &lt;/span&gt;&lt;span style="color: black;"&gt;uInt32)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = FactorialState(uInt32); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;query(1).State; &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query, with initial state: 1.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Another example is Enumerable.Aggregate query method, which accepts an IEnumerable&amp;lt;TSource&amp;gt; sequence, a TAccumulate seed, and a TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate function. Aggregate calls the accumulation function over the seed and all the values in the sequence. The aggregation steps can also be modeled as recursive steps, where each step&amp;rsquo;s state is the current accumulate result and the unused source values. Take source sequence { 1, 2, 3, 4, 5 }, seed 0, and function + as example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(Value: +, State: (0, { 1, 2, 3, 4 })) =&amp;gt; (Value: +, State: (0 + 1, { 2, 3, 4 }))&lt;/li&gt;
&lt;li&gt;(Value: +, State: (0 + 1, { 2, 3, 4 })) =&amp;gt; (Value: +, State: (0 + 1 + 2, { 3, 4 }))&lt;/li&gt;
&lt;li&gt;(Value: +, State: (0 + 1 + 2, { 3, 4 })) =&amp;gt; (Value: +, State: (0 + 1 + 2 + 3, { 4 }))&lt;/li&gt;
&lt;li&gt;(Value: +, State: (0 + 1 + 2 + 3, { 4 })) =&amp;gt; (Value: +, State: (0 + 1 + 2 + 3 + 4, { }))&lt;/li&gt;
&lt;li&gt;(Value: +, State: (0 + 1 + 2 + 3 + 4, { })) =&amp;gt; (Value: +, State: (0 + 1 + 2 + 3 + 4, { }))&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When the current source sequence in the state is empty, all source values are applied to the accumulate function, the recursion terminates, and the aggregation result in in the final state. So the recursive function is of type Tuple&amp;lt;TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate, Tuple&amp;lt;TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;&amp;gt;&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate, Tuple&amp;lt;TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;&amp;gt;&amp;gt;. Again, it can be curried to (TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate) &amp;ndash;&amp;gt; (Tuple&amp;lt;TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate, Tuple&amp;lt;TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;&amp;gt;&amp;gt;), which is equivalent to (TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate) &amp;ndash;&amp;gt; State&amp;lt;Tuple&amp;lt;TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;&amp;gt;, TAccumulate &amp;ndash;&amp;gt; TSource &amp;ndash;&amp;gt; TAccumulate&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// AggregateState: (TAccumulate -&amp;gt; TSource -&amp;gt; TAccumulate) -&amp;gt; ((TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;) -&amp;gt; (TAccumulate -&amp;gt; TSource -&amp;gt; TAccumulate, (TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;)))
// AggregateState: TAccumulate -&amp;gt; TSource -&amp;gt; TAccumulate -&amp;gt; State&amp;lt;(TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;), TAccumulate -&amp;gt; TSource -&amp;gt; TAccumulate&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;), &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; AggregateState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; func) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;state &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;)&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;(TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;), (TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;)&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;accumulate = state.Item1 &lt;/span&gt;&lt;span style="color: green;"&gt;// TAccumulate.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;source = state.Item2.Share() &lt;/span&gt;&lt;span style="color: green;"&gt;// IBuffer&amp;lt;TSource&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;&lt;span style="color: black;"&gt;sourceIterator = source.GetEnumerator() &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerator&amp;lt;TSource&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;sourceIterator.MoveNext()
            ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SetState((func(accumulate, sourceIterator.Current), source.AsEnumerable())) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;(TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;), Unit&amp;gt;.
                &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;AggregateState(func) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;(TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;), Func&amp;lt;TAccumulate, TSource, TAccumulate&amp;gt;&amp;gt;.
                &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;func)
            : func.State&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;), &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;(TAccumulate, IEnumerable&amp;lt;TSource&amp;gt;), Func&amp;lt;TAccumulate, TSource, TAccumulate&amp;gt;&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate &lt;/span&gt;&lt;span style="color: black;"&gt;Aggregate&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate &lt;/span&gt;&lt;span style="color: black;"&gt;seed, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; func)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;), &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; query =
        AggregateState(func); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;query((seed, source)).State.Item1; &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query, with initial state (seed, source).
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In each recursion step, if the source sequence in the current state in not empty, the source sequence needs to be split. The first value is used to call the accumulation function, and the other values are put into output state, which is passed to the next recursion step. So there are multiple pulling operations for the source sequence: detecting if it is empty detection, pulling first value, and pulling the rest values. To avoid multiple iterations for the same source sequence, here the Share query method from Microsoft Ix (Interactive Extensions) library is called, so that all the pulling operations share the same iterator.&lt;/p&gt;
&lt;p&gt;The stack&amp;rsquo;s Pop and Push operation can be also viewed as state processing. The Pop method of stack requires no input, and out put the stack&amp;rsquo;s top value T, So Pop can be viewed of type Unit &amp;ndash;&amp;gt; T. In contrast, stack&amp;rsquo;s Push method accepts a value, set the value to the top of the stack, and returns no output, so Push can be viewed of type T &amp;ndash;&amp;gt; Unit. The stack&amp;rsquo;s values are different before and after the Pop and Push operations, so the stack itself can be viewed as the state of the Pop and Push operation. If the values in a stack is represented as a IEnumerable&amp;lt;T&amp;gt; sequence, then Pop can be remodeled as Tuple&amp;lt;Unit, IEnumerable&amp;lt;T&amp;gt;&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;Unit, IEnumerable&amp;lt;T&amp;gt;&amp;gt;, which can be curried to Unit &amp;ndash;&amp;gt; State&amp;lt;IEnumerable&amp;lt;T&amp;gt;, T&amp;gt;; and Push can be remodeled as Tuple&amp;lt;T, IEnumerable&amp;lt;T&amp;gt;&amp;gt; &amp;ndash;&amp;gt; Tuple&amp;lt;Unit, IEnumerable&amp;lt;T&amp;gt;&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// PopState: Unit -&amp;gt; (IEnumerable&amp;lt;T&amp;gt; -&amp;gt; (T, IEnumerable&amp;lt;T&amp;gt;))
// PopState: Unit -&amp;gt; State&amp;lt;IEnumerable&amp;lt;T&amp;gt;, T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; PopState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt;
    oldStack =&amp;gt;
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; newStack = oldStack.Share();
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(newStack.First(), newStack); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output new state.
    &lt;/span&gt;&lt;span style="color: black;"&gt;};

&lt;/span&gt;&lt;span style="color: green;"&gt;// PushState: T -&amp;gt; (IEnumerable&amp;lt;T&amp;gt; -&amp;gt; (Unit, IEnumerable&amp;lt;T&amp;gt;))
// PushState: T -&amp;gt; State&amp;lt;IEnumerable&amp;lt;T&amp;gt;, Unit&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; PushState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt;
    oldStack =&amp;gt;
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; newStack = oldStack.Concat(value.Enumerable());
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, newStack); &lt;/span&gt;&lt;span style="color: green;"&gt;// Output new state.
    &lt;/span&gt;&lt;span style="color: black;"&gt;};&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now the stack operations can be a state monad workflow. Also, GetState can get the current values of the stack, and SetState can reset the values of stack:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Stack()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; initialStack = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="color: black;"&gt;.Repeat(0, 5);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;State&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; query =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PopState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, int&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PushState(1) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, Unit&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PushState(2) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, Unit&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;stack &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;GetState&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, IEnumerable&amp;lt;int&amp;gt;&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SetState(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="color: black;"&gt;.Range(0, 5)) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, Unit&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PopState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, int&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PopState&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, int&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit4 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;PushState(5) &lt;/span&gt;&lt;span style="color: green;"&gt;// State&amp;lt;IEnumerable&amp;lt;int&amp;gt;, Unit&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;stack; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; State) result = query(initialStack); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query with initial state.
    &lt;/span&gt;&lt;span style="color: black;"&gt;result.Value.WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 0 0 0 1 2
    &lt;/span&gt;&lt;span style="color: black;"&gt;result.State.WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 5
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Exception monad&lt;/h1&gt;
&lt;p&gt;As previously demonstrated, the Optional&amp;lt;&amp;gt; monad can handle the case that any operation of the workflow may not produce a valid result, in a . When an operation succeeds to return a valid result, the next operation executes. If all operations succeed, the entire workflow has a valid result. Option&amp;lt;&amp;gt; monad&amp;rsquo;s handling is based on operation&amp;rsquo;s return result. What if the operation fails with exception? To work with operation exceptions in a purely functional paradigm, the following Try&amp;lt;&amp;gt; structure can be defined, which is just Optional&amp;lt;&amp;gt; plus exception handling and store:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public readonly struct &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; factory;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: black;"&gt;Try(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; factory) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(() =&amp;gt;
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;try
            &lt;/span&gt;&lt;span style="color: black;"&gt;{
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;factory();
            }
            &lt;/span&gt;&lt;span style="color: blue;"&gt;catch &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;&lt;span style="color: black;"&gt;exception)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, exception);
            }
        });

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;Value
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;get
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.HasException)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;InvalidOperationException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;)} &lt;/span&gt;&lt;span style="color: #a31515;"&gt;object must have a value."&lt;/span&gt;&lt;span style="color: black;"&gt;);
            }
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory.Value.Item1;
        }
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;&lt;span style="color: black;"&gt;Exception =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory.Value.Item2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public bool &lt;/span&gt;&lt;span style="color: black;"&gt;HasException =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.Exception != &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static implicit operator &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (value, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Try&amp;lt;T&amp;gt; represents an operation, which either succeeds with a result, or fail with an exception. Its SelectMany method is also in the same pattern as Optional&amp;lt;&amp;gt;&amp;rsquo;s SelectMany, so that when an operation (source) succeeds without exception, the next operation (returned by selector) executes:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TryExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Try&amp;lt;TSource&amp;gt;, TSource -&amp;gt; Try&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Try&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt;
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(source.HasException)
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, source.Exception);
                }
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; result = selector(source.Value);
                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(result.HasException)
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, result.Exception);
                }
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(resultSelector(source.Value, result.Value), (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;);
            });

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Try&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Try&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; value;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (Try&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Try&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            source.SelectMany(value =&amp;gt; selector(value).Try(), (value, result) =&amp;gt; result);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The operation of throwing an exception can be represented with a Try&amp;lt;T&amp;gt; with the specified exception:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Throw&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;&lt;span style="color: black;"&gt;exception) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, exception));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;For convenience, Try&amp;lt;T&amp;gt; instance can be implicitly wrapped from a T value. And the following method also helps wrap a Func&amp;lt;T&amp;gt; operation:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Try&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (function(), (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to IO&amp;lt;&amp;gt; monad, an function operation (() &amp;ndash;&amp;gt; void) without return result can be viewed as a function returning Unit (() &amp;ndash;&amp;gt; Unit):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Try(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Action &lt;/span&gt;&lt;span style="color: black;"&gt;action) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt;
    {
        action();
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;);
    });&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To handle the exception from an operation represented by Try&amp;lt;T&amp;gt;, just check the HasException property, filter the exception, and process it. The following Catch method handles the specified exception type:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Catch&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TException&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TException&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; handler, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TException&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; when = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TException &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;&lt;span style="color: black;"&gt;=&amp;gt; 
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt;
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(source.HasException &amp;amp;&amp;amp; source.Exception &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TException &lt;/span&gt;&lt;span style="color: black;"&gt;exception &amp;amp;&amp;amp; exception != &lt;/span&gt;&lt;span style="color: blue;"&gt;null
                &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;amp;&amp;amp; (when == &lt;/span&gt;&lt;span style="color: blue;"&gt;null &lt;/span&gt;&lt;span style="color: black;"&gt;|| when(exception)))
            {
                source = handler(exception);
            }
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;source.HasException ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, source.Exception) : (source.Value, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;);
        });&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The evaluation of the Try&amp;lt;T&amp;gt; source, and the execution of handler, are both deferred. And the following Catch overload handles all exception types:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Catch&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; handler, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; when = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt;
        Catch&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;, handler, when);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the Finally method just call a function to process the Try&amp;lt;T&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult &lt;/span&gt;&lt;span style="color: black;"&gt;Finally&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; finally) =&amp;gt; finally(&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static void &lt;/span&gt;&lt;span style="color: black;"&gt;Finally&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Action&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; finally) =&amp;gt; finally(&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;source&lt;/span&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The operation of throwing an exception can be represented with a Try&amp;lt;T&amp;gt; instance wrapping the specified exception:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TryExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Throw&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Exception &lt;/span&gt;&lt;span style="color: black;"&gt;exception) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, exception));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following is an example of throwing exception:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; TryStrictFactorial(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;? value)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(value == &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;Throw&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentNullException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(value)));
    }
    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(value &amp;lt;= 0)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;Throw&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(value), value, &lt;/span&gt;&lt;span style="color: #a31515;"&gt;"Argument should be positive."&lt;/span&gt;&lt;span style="color: black;"&gt;));
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(value == 1)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;1;
    }
    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;value.Value * TryStrictFactorial(value - 1).Value;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the the following is an example of handling exception:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static string &lt;/span&gt;&lt;span style="color: black;"&gt;Factorial(&lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;value)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;?&amp;gt; stringToNullableInt32 = @string =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNullOrEmpty(@string) ? &lt;/span&gt;&lt;span style="color: blue;"&gt;default &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToInt32(@string);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Try&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;nullableInt32 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Try(() =&amp;gt; stringToNullableInt32(value)) &lt;/span&gt;&lt;span style="color: green;"&gt;// Try&amp;lt;int32?&amp;gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;TryStrictFactorial(nullableInt32) &lt;/span&gt;&lt;span style="color: green;"&gt;// Try&amp;lt;int&amp;gt;.
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Try(() =&amp;gt; result.WriteLine()) &lt;/span&gt;&lt;span style="color: green;"&gt;// Try&amp;lt;Unit&amp;gt;.
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;query
        .Catch(exception =&amp;gt; &lt;/span&gt;&lt;span style="color: green;"&gt;// Catch all and rethrow.
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            exception.WriteLine();
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;Throw&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(exception);
        })
        .Catch&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentNullException&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(exception =&amp;gt; 1) &lt;/span&gt;&lt;span style="color: green;"&gt;// When argument is null, factorial is 1.
        &lt;/span&gt;&lt;span style="color: black;"&gt;.Catch&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
            when: exception =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;.Equals(exception.ActualValue, 0),
            handler: exception =&amp;gt; 1) &lt;/span&gt;&lt;span style="color: green;"&gt;// When argument is 0, factorial is 1.
        &lt;/span&gt;&lt;span style="color: black;"&gt;.Finally(result =&amp;gt; result.HasException &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
            &lt;/span&gt;&lt;span style="color: black;"&gt;? result.Exception.Message : result.Value.ToString());
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Reader monad&lt;/h1&gt;
&lt;p&gt;The Func&amp;lt;T,&amp;gt; functor is also monad. In contrast to Func&amp;lt;&amp;gt; monad, a factory function that only outputs a value, Func&amp;lt;T,&amp;gt; can also read input value from the environment. So Fun&amp;lt;T,&amp;gt; monad is also called reader monad, or environment monad. To be intuitive, rename Func&amp;lt;T,&amp;gt; to Reader&amp;lt;TEnvironment,&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Reader: TEnvironment -&amp;gt; T
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment &lt;/span&gt;&lt;span style="color: black;"&gt;environment);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And its (SelectMany, Wrap, Select) methods are straightforward:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ReaderExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Reader&amp;lt;TEnvironment, TSource&amp;gt;, TSource -&amp;gt; Reader&amp;lt;TEnvironment, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Reader&amp;lt;TEnvironment, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            environment =&amp;gt;
            {
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value = source(environment);
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value, selector(value)(environment));
            };

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Reader&amp;lt;TEnvironment, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Reader&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; 
        environment =&amp;gt; value;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (Reader&amp;lt;TEnvironment, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Reader&amp;lt;TEnvironment, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            source.SelectMany(value =&amp;gt; selector(value).Reader&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEnvironment&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(), (value, result) =&amp;gt; result&lt;/span&gt;&lt;span style="color: black;"&gt;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There are scenarios of accessing input value from shared environment, like reading the configurations, dependency injection, etc. In the following example, the operations are dependents of the configurations, so these operations can be modeled using Reader&amp;lt;ICongiguration,&amp;gt; monad:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IConfiguration&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; DownloadHtml(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Uri &lt;/span&gt;&lt;span style="color: black;"&gt;uri) =&amp;gt;
    configuration =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: green;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IConfiguration&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ConverToWord(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo &lt;/span&gt;&lt;span style="color: black;"&gt;htmlDocument, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo &lt;/span&gt;&lt;span style="color: black;"&gt;template) =&amp;gt;
    configuration =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: green;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IConfiguration&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; UploadToOneDrive(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo &lt;/span&gt;&lt;span style="color: black;"&gt;file) =&amp;gt;
    configuration =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: green;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IConfiguration &lt;/span&gt;&lt;span style="color: black;"&gt;configuration, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Uri &lt;/span&gt;&lt;span style="color: black;"&gt;uri, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo &lt;/span&gt;&lt;span style="color: black;"&gt;template)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Reader&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IConfiguration&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; query =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;htmlDocument &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;DownloadHtml(uri) &lt;/span&gt;&lt;span style="color: green;"&gt;// Reader&amp;lt;IConfiguration, FileInfo&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;wordDocument &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;ConverToWord(htmlDocument, template) &lt;/span&gt;&lt;span style="color: green;"&gt;// Reader&amp;lt;IConfiguration, FileInfo&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;unit &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;UploadToOneDrive(wordDocument) &lt;/span&gt;&lt;span style="color: green;"&gt;// Reader&amp;lt;IConfiguration, Unit&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;(htmlDocument, wordDocument); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FileInfo&lt;/span&gt;&lt;span style="color: black;"&gt;) result = query(configuration); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The workflow is also a Reader&amp;lt;ICongiguration, T&amp;gt; function. To execute the workflow, it must read the required configuration input. Then all operation in the workflow execute sequentially by reading the same configuration input.&lt;/p&gt;
&lt;h1&gt;Writer monad&lt;/h1&gt;
&lt;p&gt;Writer is a function that returns a computed value along with a stream of additional content, so this function is of type () &amp;ndash;&amp;gt; Tuple&amp;lt;T, TContent&amp;gt;. In the writer monad workflow, each operation&amp;rsquo;s additional output content is merged with the next operation&amp;rsquo;s additional output content, so that when the entire workflow is executed, all operations&amp;rsquo; additional output content are merged as the workflow&amp;rsquo;s final additional output content. Each merge operation accepts 2 TContent instances, and result another TContent instance. It is a binary operation and can be implemented by monoid&amp;rsquo;s multiplication: TContent ⊙ TContent &amp;ndash;&amp;gt; TContent. So writer can be represented by a () &amp;ndash;&amp;gt; Tuple&amp;lt;T, TContent&amp;gt; function along with a IMonoid&amp;lt;TContent&amp;gt; monoid:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;WriterBase&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContent&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContentMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : IMonoid&amp;lt;TContent&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: black;"&gt;TContentMonoid : IMonoid&amp;lt;TContent&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color: black;"&gt;Lazy&amp;lt;(TContent, T)&amp;gt; lazy;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;protected &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;WriterBase&lt;/span&gt;&lt;span style="color: black;"&gt;(Func&amp;lt;(TContent, T)&amp;gt; writer)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;Lazy&amp;lt;(TContent, T)&amp;gt;(writer);
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: black;"&gt;TContent Content =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy.Value.Item1;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: black;"&gt;T Value =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy.Value.Item2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;TContent Multiply(TContent value1, TContent value2) =&amp;gt; TContentMonoid.Multiply(value1, value2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;TContent Unit =&amp;gt; TContentMonoid.Unit;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The most common scenario of outputting additional content, is tracing and logging, where the TContent is a sequence of log entries. A sequence of log entries can be represented as IEnumerable&amp;lt;T&amp;gt;, so the fore mentioned (IEnumerable&amp;lt;T&amp;gt;, Enumerable.Concat&amp;lt;T&amp;gt;, Enumerable.Empty&amp;lt;T&amp;gt;()) monoid can be used:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEntry&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : WriterBase&amp;lt;IEnumerable&amp;lt;TEntry&amp;gt;, T, EnumerableConcatMonoid&amp;lt;TEntry&amp;gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;(Func&amp;lt;(IEnumerable&amp;lt;TEntry&amp;gt;, T)&amp;gt; writer) : &lt;/span&gt;&lt;span style="color: blue;"&gt;base&lt;/span&gt;&lt;span style="color: black;"&gt;(writer) { }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;(T value) : &lt;/span&gt;&lt;span style="color: blue;"&gt;base&lt;/span&gt;&lt;span style="color: black;"&gt;(() =&amp;gt; (Unit, value)) { }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to State&amp;lt;TState,&amp;gt; and Reader&amp;lt;TEnvironment,&amp;gt;, here Writer&amp;lt;TEntry,&amp;gt; can be monad with the following (SelectMany, Wrap, Select) methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;WriterExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Writer&amp;lt;TEntry, TSource&amp;gt;, TSource -&amp;gt; Writer&amp;lt;TEntry, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Writer&amp;lt;TEntry, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TResult&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEntry&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TSource&amp;gt; source,
        Func&amp;lt;TSource, Writer&amp;lt;TEntry, TSelector&amp;gt;&amp;gt; selector,
        Func&amp;lt;TSource, TSelector, TResult&amp;gt; resultSelector) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TResult&amp;gt;(() =&amp;gt;
            {
                Writer&amp;lt;TEntry, TSelector&amp;gt; result = selector(source.Value);
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(Tutorial.CategoryTheory.Writer&amp;lt;TEntry, TSource&amp;gt;.Multiply(source.Content, result.Content),
                    resultSelector(source.Value, result.Value));
            });

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Writer&amp;lt;TEntry, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TSource&amp;gt; Writer&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEntry&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;TSource value) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TSource&amp;gt;(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (Writer&amp;lt;TEnvironment, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Writer&amp;lt;TEnvironment, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TResult&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TEntry&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;Writer&amp;lt;TEntry, TSource&amp;gt; source, Func&amp;lt;TSource, TResult&amp;gt; selector) =&amp;gt;
            source.SelectMany(value =&amp;gt; selector(value).Writer&amp;lt;TEntry, TResult&amp;gt;(), (value, result) =&amp;gt; result);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Most commonly, each operation in the workflow logs string message. So the following method is defined to construct a writer instance from a value and a string log factory:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; LogWriter&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; logFactory) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (logFactory(value).Enumerable(), value));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The previous Fun&amp;lt;&amp;gt; monad workflow now can output logs for each operation:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Writer&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine().LogWriter(value =&amp;gt;
                                        &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"File path: &lt;/span&gt;&lt;span style="color: black;"&gt;{value}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: green;"&gt;// Writer&amp;lt;string, string&amp;gt;.
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine().LogWriter(value =&amp;gt;
                                        &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"Encoding name: &lt;/span&gt;&lt;span style="color: black;"&gt;{value}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: green;"&gt;// Writer&amp;lt;string, string&amp;gt;.
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName).LogWriter(value =&amp;gt;
                                        &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"Encoding: &lt;/span&gt;&lt;span style="color: black;"&gt;{value}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: green;"&gt;// Writer&amp;lt;string, Encoding&amp;gt;.
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding).LogWriter(value =&amp;gt;
                                        &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"File content length: &lt;/span&gt;&lt;span style="color: black;"&gt;{value.Length}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: green;"&gt;// Writer&amp;lt;string, string&amp;gt;.
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Value; &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;query.Content.WriteLines(&lt;/span&gt;&lt;span style="color: black;"&gt;);
    &lt;/span&gt;&lt;span style="color: green;"&gt;// File path: D:\File.txt
    // Encoding name: utf-8
    // Encoding: System.Text.UTF8Encoding
    // File content length: 76138
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Continuation monad&lt;/h1&gt;
&lt;p&gt;In program, a function can return the result value, so that some other continuation function can use that value; or a function can take a continuation function as parameter, after it computes the result value, it calls back the continuation function with that value:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CpsExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Sqrt: int -&amp;gt; double
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static double &lt;/span&gt;&lt;span style="color: black;"&gt;Sqrt(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SqrtWithCallback: (int, double -&amp;gt; TContinuation) -&amp;gt; TContinuation
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation &lt;/span&gt;&lt;span style="color: black;"&gt;SqrtWithCallback&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; continuation) =&amp;gt;
            continuation(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The former is style is called direct style, and the latter is called continuation-passing style (CPS). Generally, for a TSource &amp;ndash;&amp;gt; TResult function, its CPS version can accept a TResult &amp;ndash;&amp;gt; TContinuation continuation function, so the CPS function is of type (TSource, TResult &amp;ndash;&amp;gt; TContinuation) &amp;ndash;&amp;gt; TContinuation. Again, just like the state monad, the CPS function can be curried to TSource &amp;ndash;&amp;gt; ((TResult &amp;ndash;&amp;gt; TContinuation) &amp;ndash;&amp;gt; TContinuation)&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SqrtWithCallback: int -&amp;gt; (double -&amp;gt; TContinuation) -&amp;gt; TContinuation
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SqrtWithCallback&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32) =&amp;gt;
    continuation =&amp;gt; continuation(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now the returned (TResult &amp;ndash;&amp;gt; TContinuation) &amp;ndash;&amp;gt; TContinuation function type can be given an alias Cps:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cps: (T -&amp;gt; TContinuation&amp;gt;) -&amp;gt; TContinuation
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; continuation);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that the above function can be renamed as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SqrtCps: int -&amp;gt; Cps&amp;lt;TContinuation, double&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SqrtCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32) =&amp;gt;
    continuation =&amp;gt; continuation(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The CPS function becomes TSource &amp;ndash;&amp;gt; Cps&amp;lt;TContinuation, TResult&amp;gt;, which is a monadic selector function. Just like State&amp;lt;TState,&amp;gt;, here Cps&amp;lt;TContinuation,&amp;gt; is the continuation monad. Its (SelectMany, Wrap, Select) methods can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CpsExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Cps&amp;lt;TContinuation, TSource&amp;gt;, TSource -&amp;gt; Cps&amp;lt;TContinuation, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Cps&amp;lt;TContinuation, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            continuation =&amp;gt; source(value =&amp;gt;
                selector(value)(result =&amp;gt;
                    continuation(resultSelector(value, result))));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Cps&amp;lt;TContinuation, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt;
        continuation =&amp;gt; continuation(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (Cps&amp;lt;TContinuation, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Cps&amp;lt;TContinuation, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            source.SelectMany(value =&amp;gt; selector(value).Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(), (value, result) =&amp;gt; result);
            &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to:
            // continuation =&amp;gt; source(value =&amp;gt; continuation(selector(value)));
            // Or:
            // continuation =&amp;gt; source(continuation.o(selector));
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;A more complex example is sum of squares. The CPS version of sum and square are straightforward. If direct style of square operation of type int &amp;ndash;&amp;gt; int, and the direct style of sum operation is (int, int) &amp;ndash;&amp;gt; int, then their CPS versions are just of type int &amp;ndash;&amp;gt; Cps&amp;lt;TContinuation, int&amp;gt;, and (int, int) &amp;ndash;&amp;gt; Cps&amp;lt;TContinuation, int&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SquareCps: int -&amp;gt; Cps&amp;lt;TContinuation, int&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SquareCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;x) =&amp;gt;
    continuation =&amp;gt; continuation(x * x);

&lt;/span&gt;&lt;span style="color: green;"&gt;// SumCps: (int, int) -&amp;gt; Cps&amp;lt;TContinuation, int&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SumCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;x, &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;y) =&amp;gt;
    continuation =&amp;gt; continuation(x + y);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then CPS version of sum of square can be implemented with them:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SumOfSquaresCps: (int, int) -&amp;gt; Cps&amp;lt;TContinuation, int&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SumOfSquaresCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt;
    continuation =&amp;gt;
        SquareCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(a)(squareOfA =&amp;gt;
        SquareCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(b)(squareOfB =&amp;gt;
        SumCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(squareOfA, squareOfB)(continuation)));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This is not intuitive. But the continuation monad can help. A Cps&amp;lt;TContinuation, T&amp;gt; function can be viewed as a monad wrapper of T value. So T value can be unwrapped from Cps&amp;lt;TContinuation, T&amp;gt; with the LINQ from clause:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SumOfSquaresCpsLinq&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;squareOfA &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SquareCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(a) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, int&amp;gt;.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;squareOfB &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SquareCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(b) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, int&amp;gt;.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;sum &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;SumCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(squareOfA, squareOfB) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, int&amp;gt;.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;sum;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the following is a similar example of fibonacci:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; FibonacciCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;uint &lt;/span&gt;&lt;span style="color: black;"&gt;uInt32) =&amp;gt;
    uInt32 &amp;gt; 1
        ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;a &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;FibonacciCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(uInt32 - 1U)
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;b &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;FibonacciCps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(uInt32 - 2U)
            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;a + b)
        : uInt32.Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to:
    // continuation =&amp;gt; uInt32 &amp;gt; 1U
    //    ? continuation(FibonacciCps&amp;lt;int&amp;gt;(uInt32 - 1U)(Id) + FibonacciCps&amp;lt;int&amp;gt;(uInt32 - 2U)(Id))
    //    : continuation(uInt32);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Generally, a direct style function can be easily converted to CPS function &amp;ndash; just pass the direct style function&amp;rsquo;s return value to a continuation function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function) =&amp;gt;
    continuation =&amp;gt; continuation(function());&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now the previous workflows can be represented in CPS too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; continuation)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Cps&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName)) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, Encoding&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Cps&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding)) &lt;/span&gt;&lt;span style="color: green;"&gt;// Cps&amp;lt;TContinuation, string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TContinuation &lt;/span&gt;&lt;span style="color: black;"&gt;result = query(continuation); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In the workflow, each operation&amp;rsquo;s continuation function is its next operation. When the workflow executes, each operation computes its return value, then calls back its next operation with its return value. When the last operation executes, it calls back the workflow&amp;rsquo;s continuation function.&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Tue, 31 Dec 2024 13:13:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-8-more-linq-to-monads</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>LINQ via C#</category><category>Monads</category></item><item><title>Category Theory via C# (7) Monad and LINQ to Monads</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-7-monad-and-linq-to-monads</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Monad&lt;/h1&gt;
&lt;p&gt;As fore mentioned endofunctor category can be monoidal (the entire category. Actually, an endofunctor In the endofunctor category can be monoidal too. This kind of endofunctor is called monad. Monad is another important algebraic structure in category theory and LINQ. Formally, &lt;a href="http://en.wikipedia.org/wiki/Monad_(category_theory)" target="_blank"&gt;monad&lt;/a&gt; is an endofunctor equipped with 2 natural transformations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Monoid multiplication ◎ or &amp;mu;, which a natural transformation ◎: F(F) &amp;rArr; F, which means, for each object X, ◎ maps F(F(X)) to F(X). For convenience, this mapping operation is also denoted F ◎ F &amp;rArr; F.&lt;/li&gt;
&lt;li&gt;Monoid unit &amp;eta;, which is a natural transformation &amp;eta;: I &amp;rArr; F. Here I is the identity functor, which maps each object X to X itself. For each X, there is &amp;eta; maps I(X) to F(X). Since I(X) is just X, &amp;eta; can also be viewed as mapping: X &amp;rarr; F(X).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So monad F is a monoid (F, ◎, &amp;eta;) in the category of endofunctors. Apparently it must preserve the monoid laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Associativity preservation &amp;alpha;: (F ◎ F) ◎ F &amp;equiv; F ◎ (F ◎ F)&lt;/li&gt;
&lt;li&gt;Left unit preservation &amp;lambda;: &amp;eta; ◎ F &amp;equiv; F, and right unit preservation &amp;rho;: F &amp;equiv; F ◎ &amp;eta;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So that, the following diagram commutes:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-6-Monad-and-LINQ-t_1486B/image_2.png"&gt;&lt;img width="674" height="399" title="image" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-6-Monad-and-LINQ-t_1486B/image_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In DotNet category, monad can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public partial interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;&lt;/span&gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// From IFunctor&amp;lt;TMonad&amp;lt;&amp;gt;&amp;gt;:
    // Select: (TSource -&amp;gt; TResult) -&amp;gt; (TMonad&amp;lt;TSource&amp;gt; -&amp;gt; TMonad&amp;lt;TResult&amp;gt;)
    // Func&amp;lt;TMonad&amp;lt;TSource&amp;gt;, TMonad&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);

    // Multiply: TMonad&amp;lt;TMonad&amp;lt;TSource&amp;gt;&amp;gt; -&amp;gt; TMonad&amp;lt;TSource&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper);
        
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: TSource -&amp;gt; TMonad&amp;lt;TSource&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;LINQ to Monads and monad laws&lt;/h1&gt;
&lt;h2&gt;Built-in IEnumerable&amp;lt;&amp;gt; monad&lt;/h2&gt;
&lt;p&gt;The previously discussed IEnumerable&amp;lt;&amp;gt; functor is a built-in monad, it is straightforward to implement its (Multiply, Unit) method pair:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IMonad&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: IEnumerable&amp;lt;IEnumerable&amp;lt;TSource&amp;gt;&amp;gt; -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;sourceWrapper)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;value;
            }
        }
    }

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;value;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The monoid unit &amp;eta; is exactly the same as the Wrap method for monoidal functor. It is easy to verify the above implementation preserves the monoid laws:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonoidLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 };

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: source.Wrap().Multiply().Wrap().Multiply() == source.Wrap().Wrap().Multiply().Multiply().
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Enumerable().Multiply().Enumerable().Multiply().WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Enumerable().Enumerable().Multiply().Multiply().WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    // Left unit preservation: Unit(source).Multiply() == f.
    &lt;/span&gt;&lt;span style="color: black;"&gt;Unit(source).Multiply().WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    // Right unit preservation: source == source.Select(Unit).Multiply().
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Select(Unit).Multiply().WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As discussed in LINQ to Object chapter, for IEnumerable&amp;lt;&amp;gt;, there is already a query method SelectMany providing the same ability to flatten hierarchy an IEnumerable&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; sequence to an IEnumerable&amp;lt;T&amp;gt; sequence. Actually, monad can be alternatively defined with SelectMany and &amp;eta;/Wrap:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;
{&lt;/span&gt;&lt;span style="color: green;"&gt;
    // SelectMany: (TMonad&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TMonad&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; TMonad&amp;lt;TResult&amp;gt;
    &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Wrap&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the alternative implementation is very similar:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IMonad&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (IEnumerable&amp;lt;TSource&amp;gt;, TSource -&amp;gt; IEnumerable&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value))
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value, result);
            }
        }
    }

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Enumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;value;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above 2 versions of monad definition are equivalent. First, the (SelectMany, Wrap) methods can be implemented with the (Select, Multiply, Unit) methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (Select, Multiply, Unit) implements (SelectMany, Wrap).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (IEnumerable&amp;lt;TSource&amp;gt;, TSource -&amp;gt; IEnumerable&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
             &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value)
                     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value, result))).Multiply();
            &lt;/span&gt;&lt;span style="color: green;"&gt;// Compiled to:
            // source.Select(value =&amp;gt; selector(value).Select(result =&amp;gt; resultSelector(value, result))).Multiply();

    // Wrap: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Enumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit(value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the (Select, Multiply, Unit) methods can be implemented with (SelectMany, Wrap) methods too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (SelectMany, Wrap) implements (Select, Multiply, Unit).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Select: (TSource -&amp;gt; TResult) -&amp;gt; (IEnumerable&amp;lt;TSource&amp;gt; -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;value.Enumerable()
            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result;
            &lt;/span&gt;&lt;span style="color: green;"&gt;// source.SelectMany(Enumerable, (result, value) =&amp;gt; value);

    // Multiply: IEnumerable&amp;lt;IEnumerable&amp;lt;TSource&amp;gt;&amp;gt; -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;source &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;sourceWrapper
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;value;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value);

    // Unit: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; value.Enumerable();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So monad support is built-in in the C# language. As discussed in the LINQ query expression pattern part, SelectMany enables multiple from clauses, which can chain operations together to build a workflow, for example:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source1,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source2,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source3,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source4)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source3()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value4 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source4(value1, value2, value3)
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;value4; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;query.WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here N + 1 from clauses are compiled to N SelectMany fluent calls:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CompiledWorkflow&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source1,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source2,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source3,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source4)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = source1()
        .SelectMany(value1 =&amp;gt; source2(), (value1, value2) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;{ Value1 = value1, Value2 = value2 })
        .SelectMany(result2 =&amp;gt; source3(), (result2, value3) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;{ Result2 = result2, Value3 = value3 })
        .SelectMany(
            result3 =&amp;gt; source4(result3.Result2.Value1, result3.Result2.Value2, result3.Value3),
            (result3, value4) =&amp;gt; value4); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;query.WriteLines(); &lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In LINQ, if monad&amp;rsquo;s SelectMany implements deferred execution, then monad enables imperative programming paradigm (a sequence of commands) in a purely functional way. In above LINQ query definition, the calls to the commands are not executed. When trying to pull results from the LINQ query, the workflow stars, and the commands executes sequentially.&lt;/p&gt;
&lt;h2&gt;Monad law and Kleisli composition&lt;/h2&gt;
&lt;p&gt;Regarding monad (F, ◎, &amp;eta;) can be redefined as (F, SelectMany, Wrap), the monoid laws now can be expressed by SelectMany and Wrap too, which are called monad laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Associativity law: SelectMany is the associative operator, since it is equivalent to Multiply.&lt;/li&gt;
&lt;li&gt;Left unit law and right unit law: Wrap is the unit &amp;eta;, since it is identical to Unit.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonadLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new string&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;'*'&lt;/span&gt;&lt;span style="color: black;"&gt;, int32);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector1 = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new double&lt;/span&gt;&lt;span style="color: black;"&gt;[] { int32 / 2D, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32) };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector2 =
        @double =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new string&lt;/span&gt;&lt;span style="color: black;"&gt;[] { @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.0"&lt;/span&gt;&lt;span style="color: black;"&gt;), @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;) };
    &lt;/span&gt;&lt;span style="color: blue;"&gt;const int &lt;/span&gt;&lt;span style="color: black;"&gt;Value = 5;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity: source.SelectMany(selector1).SelectMany(selector2) == source.SelectMany(value =&amp;gt; selector1(value).SelectMany(selector2)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(value)
     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector2(result1)
     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result2).WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0 0.00 0.0 0.00
    // 0.5 0.50 1.0 1.00
    // 1.0 1.00 1.4 1.41
    // 1.5 1.50 1.7 1.73
    // 2.0 2.00 2.0 2.00
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(value)
                     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector2(result1)
                     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result2)
     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0 0.00 0.0 0.00
    // 0.5 0.50 1.0 1.00
    // 1.0 1.00 1.4 1.41
    // 1.5 1.50 1.7 1.73
    // 2.0 2.00 2.0 2.00
    // Left unit: value.Wrap().SelectMany(selector) == selector(value).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Value.Enumerable()
     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value)
     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// * * * * *
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector(Value).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// * * * * *
    // Right unit: source == source.SelectMany(Wrap).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
     &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;value.Enumerable()
     &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;However, the monad laws are not intuitive. The Kleisli composition ∘ can help. For 2 monadic selector functions that can be passed to SelectMany,are also called Kleisli functions like s&lt;sub&gt;1&lt;/sub&gt;: TSource &amp;ndash;&amp;gt; TMonad&amp;lt;TMiddle&amp;gt; and s&lt;sub&gt;2&lt;/sub&gt;: TMiddle &amp;ndash;&amp;gt; TMonad&amp;lt;TResult&amp;gt;, their Kleisli composition is still a monadic selector (s&lt;sub&gt;2&lt;/sub&gt; ∘ s&lt;sub&gt;1&lt;/sub&gt;): TSource &amp;ndash;&amp;gt; TMonad&amp;lt;TResult&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; o&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;( &lt;/span&gt;&lt;span style="color: green;"&gt;// After.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector2,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector1) =&amp;gt;
        value =&amp;gt; selector1(value).SelectMany(selector2, (result1, result2) =&amp;gt; result2);
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to:
        // value =&amp;gt; selector1(value).Select(selector2).Multiply();&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Or generally:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; o&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;( &lt;/span&gt;&lt;span style="color: green;"&gt;// After.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector2,
        &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector1) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            value =&amp;gt; selector1(value).SelectMany&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;(selector2, (result1, result2) =&amp;gt; result2);
            &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to:
            &lt;/span&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// value =&amp;gt; selector1(value).Select(selector2).Multiply();
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now above monad laws can be expressed by monadic selectors and Kleisli composition:&lt;/p&gt;
&lt;ul&gt;&lt;!--StartFragment--&gt;
&lt;li&gt;Associativity law: the Kleisli composition of monadic selectors is now the monoid multiplication, it is associative. For monadic selectors s&lt;sub&gt;1&lt;/sub&gt;, s&lt;sub&gt;2&lt;/sub&gt;, s&lt;sub&gt;3&lt;/sub&gt;, there is (s&lt;sub&gt;3&lt;/sub&gt; ∘ s&lt;sub&gt;2&lt;/sub&gt;) ∘ s&lt;sub&gt;1&lt;/sub&gt; = s&lt;sub&gt;3&lt;/sub&gt; ∘ (s&lt;sub&gt;2&lt;/sub&gt; ∘ s&lt;sub&gt;1&lt;/sub&gt;).&lt;/li&gt;
&lt;li&gt;Left unit law and right unit law: Wrap is still the monoid unit &amp;eta;, it is of type TSource &amp;ndash;&amp;gt; TMonad&amp;lt;TSource&amp;gt;, so it can also be viewed as a monadic selector too. For monadic selector s, there is &amp;eta; ∘ s = s and s = s ∘ &amp;eta;.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;KleisliComposition()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector1 =
        boolean =&amp;gt; boolean ? &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 } : &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 5, 6, 7, 8, 9 };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector2 = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new double&lt;/span&gt;&lt;span style="color: black;"&gt;[] { int32 / 2D, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32) };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector3 =
        @double =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new string&lt;/span&gt;&lt;span style="color: black;"&gt;[] { @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.0"&lt;/span&gt;&lt;span style="color: black;"&gt;), @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;) };

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity: selector3.o(selector2).o(selector1) == selector3.o(selector2.o(selector1)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector3.o(selector2).o(selector1)(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0 0.00 0.0 0.00
    // 0.5 0.50 1.0 1.00
    // 1.0 1.00 1.4 1.41
    // 1.5 1.50 1.7 1.73
    // 2.0 2.00 2.0 2.00
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector3.o(selector2.o(selector1))(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0 0.00 0.0 0.00
    // 0.5 0.50 1.0 1.00
    // 1.0 1.00 1.4 1.41
    // 1.5 1.50 1.7 1.73
    // 2.0 2.00 2.0 2.00
    // Left unit: Unit.o(selector) == selector.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; leftUnit = Enumerable;
    leftUnit.o(selector1)(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    // Right unit: selector == selector.o(Unit).
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 5 6 7 8 9
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; rightUnit = Enumerable;
    selector1.o(rightUnit)(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 5 6 7 8 9
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Kleisli category&lt;/h2&gt;
&lt;p&gt;With monad and Kleisli composition, a new kind of category called Kleisli category can be defined. Given a monad (F, ◎, &amp;eta;) in category C, there is a Kleisli category of F, denoted C&lt;sub&gt;F&lt;/sub&gt;:&lt;/p&gt;
&lt;ul&gt;&lt;!--StartFragment--&gt;
&lt;li&gt;Its objects ob(C&lt;sub&gt;F&lt;/sub&gt;) are ob(C), all objects in C.&lt;/li&gt;
&lt;li&gt;Its morphisms hom(C&lt;sub&gt;F&lt;/sub&gt;) are Kleisli morphisms. A Kleisli morphisms m from object X to object Y is m: X &amp;rarr; F(Y). In DotNet, the Kleisli morphisms are above monadic selector functions.&lt;/li&gt;
&lt;li&gt;The composition of Kleisli morphisms is the above Kleisli composition.&lt;/li&gt;
&lt;li&gt;The identity Kleisli morphism is &amp;eta; of the monad, so that &amp;eta;&lt;sub&gt;X&lt;/sub&gt;: X &amp;rarr; F(X).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-6-Monad-and-LINQ-t_1486B/image_4.png"&gt;&lt;img width="882" height="352" title="image" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-6-Monad-and-LINQ-t_1486B/image_thumb_1.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;!--EndFragment--&gt;As already demonstrated, Kleisli composition and &amp;eta; satisfy the category associativity law and identity law.&lt;/p&gt;
&lt;h2&gt;Monad pattern of LINQ&lt;/h2&gt;
&lt;p&gt;So LINQ SelectMany query&amp;rsquo;s quintessential mathematics is monad. Generally, in DotNet category, a type is a monad if:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This type is an open generic type definition, which can be viewed as type constructor of kind * &amp;ndash;&amp;gt; *, so that it maps a concrete type to another concrete monad-wrapped type.&lt;/li&gt;
&lt;li&gt;It is equipped with the standard LINQ query method SelectMany, which can be either instance method or extension method.&lt;/li&gt;
&lt;li&gt;The implementation of SelectMany satisfies the monad laws, so that the monad&amp;rsquo;s monoid structure is preserved.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As &lt;a href="https://www.linkedin.com/in/brianbeckman" target="_blank"&gt;Brian Beckman&lt;/a&gt; said in &lt;a href="http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads" target="_blank"&gt;this Channel 9 video&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;LINQ is monad. It is very carefully designed by &lt;a href="http://en.wikipedia.org/wiki/Erik_Meijer_(computer_scientist)" target="_blank"&gt;Erik Meijer&lt;/a&gt; so that it is monad.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="http://ericlippert.com/" target="_blank"&gt;Eric Lippert&lt;/a&gt; also &lt;a href="http://stackoverflow.com/questions/4683506/are-there-any-connections-between-haskell-and-linq" target="_blank"&gt;mentioned&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The LINQ syntax is designed specifically to make operations on the sequence monad feel natural, but in fact the implementation is more general; what C# calls "SelectMany" is a slightly modified form of the "Bind" operation on an arbitrary monad.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;On the other hand, to enable the monad LINQ query expression (multiple from clauses with select clause) for a type does not require that type to be strictly a monad. This LINQ workflow syntax can be enabled for any generic or non generic type as long as it has such a SelectMany method, which can be virtually demonstrated as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;span style="color: #2b91af;"&gt; &lt;/span&gt;&lt;span style="color: green;"&gt;// Non generic TMonad can work too.&lt;/span&gt;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; operation1,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; operation2,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; operation3,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; operation4,
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T4&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;
{
    &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: green;"&gt;/* T1 */ &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: green;"&gt;/* TMonad&amp;lt;T1&amp;gt; */ &lt;/span&gt;&lt;span style="color: black;"&gt;operation1()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: green;"&gt;/* T2 */ &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: green;"&gt;/* TMonad&amp;lt;T1&amp;gt; */ &lt;/span&gt;&lt;span style="color: black;"&gt;operation2()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: green;"&gt;/* T3 */ &lt;/span&gt;&lt;span style="color: black;"&gt;value3 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: green;"&gt;/* TMonad&amp;lt;T1&amp;gt; */ &lt;/span&gt;&lt;span style="color: black;"&gt;operation3()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: green;"&gt;/* T4 */ &lt;/span&gt;&lt;span style="color: black;"&gt;value4 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: green;"&gt;/* TMonad&amp;lt;T1&amp;gt; */ &lt;/span&gt;&lt;span style="color: black;"&gt;operation4()
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: green;"&gt;/* TResult */ &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value1, value2, value3, value4); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}
&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Monad vs. monoidal/applicative functor&lt;/h1&gt;
&lt;p&gt;Monad is monoidal functor and applicative functor. Monads&amp;rsquo; (SelectMany, Wrap) methods implement monoidal functor&amp;rsquo;s Multiply and Unit methods, and applicative functor&amp;rsquo;s (Apply, Wrap) methods. This can be virtually demonstrated as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonadExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (SelectMany, Wrap) implements (Multiply, Unit).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: (TMonad&amp;lt;T1&amp;gt;, TMonad&amp;lt;T2&amp;gt;) =&amp;gt; TMonad&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2
            &lt;/span&gt;&lt;span style="color: blue;"&gt;select (&lt;/span&gt;&lt;span style="color: black;"&gt;value1, value2);
            &lt;/span&gt;&lt;span style="color: green;"&gt;// source1.SelectMany(value1 =&amp;gt; source2 (value1, value2) =&amp;gt; value1.ValueTuple(value2));

    // Unit: Unit -&amp;gt; TMonad&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;(
        &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt; unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt; unit&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;.Wrap();
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonadExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (SelectMany, Wrap) implements (Apply, Wrap).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (TMonad&amp;lt;TSource -&amp;gt; TResult&amp;gt;, TMonad&amp;lt;TSource&amp;gt;) -&amp;gt; TMonad&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, 
        &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;TMonad&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;selector &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper
            &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
            &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value);
            &lt;/span&gt;&lt;span style="color: green;"&gt;// selectorWrapper.SelectMany(selector =&amp;gt; source, (selector, value) =&amp;gt; selector(value));

    // Monad's Wrap is identical to applicative functor's Wrap.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;If monad is defined with the (Multiply, Unit) methods, they implement monoidal functor&amp;rsquo;s Multiply and Unit methods, and applicative functor&amp;rsquo;s (Apply, Wrap) methods too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonadExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Monad (Multiply, Unit) implements monoidal functor (Multiply, Unit).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: (TMonad&amp;lt;T1&amp;gt;, TMonad&amp;lt;T2&amp;gt;) =&amp;gt; TMonad&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1
             &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2
                     &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select (&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;value1, value2))).Multiply();
            &lt;/span&gt;&lt;span style="color: green;"&gt;// source1.Select(value1 =&amp;gt; source2.Select(value2 =&amp;gt; (value1, value2))).Multiply();

    // Unit: Unit -&amp;gt; TMonad&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(Unit unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt; 
        &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&amp;gt;.Unit&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&amp;gt;(unit);
}

&lt;/span&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonadExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Monad (Multiply, Unit) implements applicative functor (Apply, Wrap).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (TMonad&amp;lt;TSource -&amp;gt; TResult&amp;gt;, TMonad&amp;lt;TSource&amp;gt;) -&amp;gt; TMonad&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;Func&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, 
        &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source)  &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; :&lt;/span&gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            (&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;selector &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper
             &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                     &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;selector(value))).Multiply();
            &lt;/span&gt;&lt;span style="color: green;"&gt;// selectorWrapper.Select(selector =&amp;gt; source.Select(value =&amp;gt; selector(value))).Multiply();

    // Wrap: TSource -&amp;gt; TMonad&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Wrap&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt; &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the monad definition can be updated to implement monoidal functor and applicative functor too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public partial interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonad&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;
{
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;More LINQ to Monads&lt;/h1&gt;
&lt;p&gt;Many other open generic type definitions provided by .NET can be monad. Take Lazy&amp;lt;&amp;gt; functor as example, first, apparently it is a type constructor of kind * &amp;ndash;&amp;gt; *. Then, its SelectMany query method can be defined as extension method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Lazy&amp;lt;T&amp;gt; : IMonad&amp;lt;Lazy&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Lazy&amp;lt;Lazy&amp;lt;TSource&amp;gt; -&amp;gt; Lazy&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper) =&amp;gt;
        sourceWrapper.SelectMany(Id, False);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: TSource -&amp;gt; Lazy&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Lazy(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Lazy&amp;lt;TSource&amp;gt;, TSource -&amp;gt; Lazy&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Lazy&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt; 
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; resultSelector(source.Value, selector(source.Value).Value));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Its Wrap method has been implemented previously, as a requirement of applicative functor. The following is an example of chaining operations into a workflow with Lazy&amp;lt;&amp;gt; monad:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine)
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine)
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName))
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding))
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Value; &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Since SelectMany implements deferred execution, the above LINQ query is pure and the workflow is deferred. When the query is executed by calling Lazy&amp;lt;&amp;gt;.Value, the workflow is started.&lt;/p&gt;
&lt;p&gt;Func&amp;lt;&amp;gt; functor is also monad, with the following SelectMany:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T&amp;gt; : IMonad&amp;lt;Func&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Func&amp;lt;Func&amp;lt;T&amp;gt; -&amp;gt; Func&amp;lt;T&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper) =&amp;gt; 
        sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; Func&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Func(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Func&amp;lt;TSource&amp;gt;, TSource -&amp;gt; Func&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Func&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt; () =&amp;gt;
        {
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value = source();
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;resultSelector(value, selector(value)());
        };
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the workflow is similar to Lazy&amp;lt;&amp;gt; monad&amp;rsquo;s workflow, because Lazy&amp;lt;T&amp;gt; is just a wrapper of Func&amp;lt;T&amp;gt; factory function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine)
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine)
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName))
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding))
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The Optional&amp;lt;&amp;gt; monad is monad too, with the following SelectMany:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;OptionalExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Optional&amp;lt;T&amp;gt; : IMonad&amp;lt;Optional&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Optional&amp;lt;Optional&amp;lt;TSource&amp;gt; -&amp;gt; Optional&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper) =&amp;gt;
        sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: TSource -&amp;gt; Optional&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Optional(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Optional&amp;lt;TSource&amp;gt;, TSource -&amp;gt; Optional&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Optional&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt;
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(source.HasValue)
                {
                    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; result = selector(source.Value);
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(result.HasValue)
                    {
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, resultSelector(source.Value, result.Value));
                    }
                }
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;);
            });
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The LINQ workflow of Optional&amp;lt;&amp;gt; monad is also pure and deferred, where each operation in the chaining is an Optional&amp;lt;T&amp;gt; instance:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;input;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNullOrWhiteSpace(input = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine())
            ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) : (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, input))
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNullOrWhiteSpace(input = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine())
            ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) : (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, input))
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt;
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;try
                &lt;/span&gt;&lt;span style="color: black;"&gt;{
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName));
                }
                &lt;/span&gt;&lt;span style="color: blue;"&gt;catch &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="color: black;"&gt;)
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;);
                }
            })
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.Exists(filePath)
            ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding)) : (&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;))
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(query.HasValue) &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;{
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Value;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Optional&amp;lt;&amp;gt; covers the scenario that each operation of the workflow may not have invalid result. When an operation has valid result (Optional&amp;lt;T&amp;gt;.HasValue returns true), its next operation executes. And when all all the operations have valid result, the entire workflow has a valid query result.&lt;/p&gt;
&lt;p&gt;The ValueTuple&amp;lt;&amp;gt; functor is also monad. Again, its SelectMany cannot defer the call of selector, just like its Select:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T, TResult&amp;gt; : IMonad&amp;lt;ValueTuple&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: ValueTuple&amp;lt;T, ValueTuple&amp;lt;T, TSource&amp;gt; -&amp;gt; ValueTuple&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;)) sourceWrapper) =&amp;gt;
        sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Unit: TSource -&amp;gt; ValueTuple&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (ValueTuple&amp;lt;T, TSource&amp;gt;, TSource -&amp;gt; ValueTuple&amp;lt;T, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; ValueTuple&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;) SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            (source.Item1, resultSelector(source.Item2, selector(source.Item2).Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So its workflow is the immediate execution version of Lazy&amp;lt;&amp;gt; monad&amp;rsquo;s workflow:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Workflow()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;filePath &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine())
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encodingName &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadLine())
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;encoding &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Encoding&lt;/span&gt;&lt;span style="color: black;"&gt;.GetEncoding(encodingName))
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;File&lt;/span&gt;&lt;span style="color: black;"&gt;.ReadAllText(filePath, encoding))
                                   &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;fileContent; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Item1; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The Task&amp;lt;&amp;gt; functor is monad too. Once again, its SelectMany is immediate and impure, just like its Select:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TaskExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Task&amp;lt;T&amp;gt; : IMonad&amp;lt;Task&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Task&amp;lt;Task&amp;lt;T&amp;gt; -&amp;gt; Task&amp;lt;T&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; sourceWrapper) =&amp;gt;
        sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.

    // Unit: TSource -&amp;gt; Task&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Task(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (Task&amp;lt;TSource&amp;gt;, TSource -&amp;gt; Task&amp;lt;TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; Task&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            resultSelector(&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;source, &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;selector(&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;source)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the following LINQ workflow with Task&amp;lt;&amp;gt; monad is also immediate and impure:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static async &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt; &lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;WorkflowAsync&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;uri)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;response &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;HttpClient&lt;/span&gt;&lt;span style="color: black;"&gt;().GetAsync(uri) &lt;/span&gt;&lt;span style="color: green;"&gt;// Return Task&amp;lt;HttpResponseMessage&amp;gt;.
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;stream &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;response.Content.ReadAsStreamAsync() &lt;/span&gt;&lt;span style="color: green;"&gt;// Return Task&amp;lt;Stream&amp;gt;.
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;text &lt;/span&gt;&lt;span style="color: blue;"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;StreamReader&lt;/span&gt;&lt;span style="color: black;"&gt;(stream).ReadToEndAsync() &lt;/span&gt;&lt;span style="color: green;"&gt;// Return Task&amp;lt;string&amp;gt;.
                         &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;text; &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;query; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is easy to verify all the above SelectMany methods satisfy the monad laws, and all the above (Multiply, Unit) methods preserve the monoid laws. However, not any SelectMany or (Multiply, Unit) methods can automatically satisfy those laws. Take the ValueTuple&amp;lt;T,&amp;gt; functor as example, here are its SelectMany and (Multiply, Unit):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T, TResult&amp;gt; : IMonad&amp;lt;ValueTuple&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: ValueTuple&amp;lt;T, ValueTuple&amp;lt;T, TSource&amp;gt; -&amp;gt; ValueTuple&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;)) sourceWrapper) =&amp;gt;
        sourceWrapper.SelectMany(source =&amp;gt; source, (source, value) =&amp;gt; value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Unit: TSource -&amp;gt; ValueTuple&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(value);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// SelectMany: (ValueTuple&amp;lt;T, TSource&amp;gt;, TSource -&amp;gt; ValueTuple&amp;lt;T, TSelector&amp;gt;, (TSource, TSelector) -&amp;gt; TResult) -&amp;gt; ValueTuple&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;) SelectMany&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; selector,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSelector&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; resultSelector) =&amp;gt;
            (source.Item1, resultSelector(source.Item2, selector(source.Item2).Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above (Multiply, Unit) implementations cannot preserve the monoid left unit law:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonoidLaws()
{
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;) source = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;&lt;span style="color: black;"&gt;, 1);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: source.Wrap().Multiply().Wrap().Multiply() == source.Wrap().Wrap().Multiply().Multiply().
    &lt;/span&gt;&lt;span style="color: black;"&gt;source
        .ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;()
        .Multiply()
        .ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;()
        .Multiply()
        .WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    &lt;/span&gt;&lt;span style="color: black;"&gt;source
        .ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;()
        .ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;))&amp;gt;()
        .Multiply()
        .Multiply()
        .WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    // Left unit preservation: Unit(f).Multiply() == source.
    &lt;/span&gt;&lt;span style="color: black;"&gt;Unit&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(source).Multiply().WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    // Right unit preservation: source == source.Select(Unit).Multiply().
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Select(Unit&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;).Multiply().WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1)
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the above SelectMany implementation breaks the left unit monad law too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonadLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;&lt;span style="color: black;"&gt;, 1);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector = int32 =&amp;gt; (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"b"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #a31515;"&gt;'@'&lt;/span&gt;&lt;span style="color: black;"&gt;);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector1 = int32 =&amp;gt; (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"c"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selector2 = @double =&amp;gt; (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"d"&lt;/span&gt;&lt;span style="color: black;"&gt;, @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;));
    &lt;/span&gt;&lt;span style="color: blue;"&gt;const int &lt;/span&gt;&lt;span style="color: black;"&gt;Value = 5;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity: source.SelectMany(selector1).SelectMany(selector2) == source.SelectMany(value =&amp;gt; selector1(value).SelectMany(selector2)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(value)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector2(result1)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result2).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1.00)
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(value) &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector2(result1) &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result2)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1.00)
    // Left unit: value.Wrap().SelectMany(selector) == selector(value).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;Value.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, @)
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector(Value).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (b, @)
    // Right unit: source == source.SelectMany(Wrap).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;result &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;value.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;result).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1)
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Thu, 26 Dec 2024 13:13:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-7-monad-and-linq-to-monads</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>LINQ via C#</category><category>Monads</category></item><item><title>Category Theory via C# (6) Monoidal Functor and Applicative Functor</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-6-monoidal-functor-and-applicative-functor</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Monoidal functor&lt;/h1&gt;
&lt;p&gt;Given monoidal categories (C, &amp;otimes;, I&lt;sub&gt;C&lt;/sub&gt;) and (D, ⊛, I&lt;sub&gt;D&lt;/sub&gt;), a strong lax monoidal functor is a functor F: C &amp;rarr; D equipped with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Monoid binary multiplication operation, which is a natural transformation &amp;phi;: F(X) ⊛ F(Y) &amp;rArr; F(X &amp;otimes; Y)&lt;/li&gt;
&lt;li&gt;Monoid unit, which is a morphism &amp;iota;: I&lt;sub&gt;D&lt;/sub&gt; &amp;rarr; F(I&lt;sub&gt;C&lt;/sub&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;F preserves the monoid laws in D:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Associativity law is preserved with D&amp;rsquo;s associator &amp;alpha;&lt;sub&gt;D&lt;/sub&gt;:&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/Untitled-4.fw_thumb1_4.png"&gt;&lt;img width="640" height="346" title="Untitled-4.fw_thumb1" style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="Untitled-4.fw_thumb1" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/Untitled-4.fw_thumb1_thumb_1.png" border="0" /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Left unit law is preserved with D&amp;rsquo;s left unitor &amp;lambda;&lt;sub&gt;D&lt;/sub&gt;:&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/image_thumb_2.png"&gt;&lt;img width="640" height="248" title="image_thumb" style="margin: 0px; border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/image_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&amp;nbsp; &lt;br /&gt;and right unit law is preserved with D&amp;rsquo;s right unitor &amp;rho;&lt;sub&gt;D&lt;/sub&gt;:&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/Untitled-3..fw_thumb_2.png"&gt;&lt;img width="640" height="267" title="Untitled-3..fw_thumb" style="margin: 0px; border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="Untitled-3..fw_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/8aeb52ea4130_CA43/Untitled-3..fw_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this tutorial, strong lax monoidal functor is called monoidal functor for short. In DotNet category, monoidal functors are monoidal endofunctors. In the definition, (C, &amp;otimes;, I&lt;sub&gt;C&lt;/sub&gt;) and (D, ⊛, I&lt;sub&gt;D&lt;/sub&gt;) are both (DotNet, ValueTuple&amp;lt;,&amp;gt;, Unit), so monoidal functor can be IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;
{
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// From IFunctor&amp;lt;TMonoidalFunctor&amp;lt;&amp;gt;&amp;gt;:
    &lt;/span&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// &lt;/span&gt;&lt;span style="color: green;"&gt;Select: (TSource -&amp;gt; TResult) -&amp;gt; (TMonoidalFunctor&amp;lt;TSource&amp;gt; -&amp;gt; TMonoidalFunctor&amp;lt;TResult&amp;gt;)
    // Func&amp;lt;TMonoidalFunctor&amp;lt;TSource&amp;gt;, TMonoidalFunctor&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);

    // Multiply: TMonoidalFunctor&amp;lt;T1&amp;gt; x TMonoidalFunctor&amp;lt;T2&amp;gt; -&amp;gt; TMonoidalFunctor&amp;lt;T1 x T2&amp;gt;
    // Multiply: ValueTuple&amp;lt;TMonoidalFunctor&amp;lt;T1&amp;gt;, TMonoidalFunctor&amp;lt;T2&amp;gt;&amp;gt; -&amp;gt; TMonoidalFunctor&amp;lt;ValueTuple&amp;lt;T1, T2&amp;gt;&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; bifunctor);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; TMonoidalFunctor&amp;lt;Unit&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Multiply accepts a ValueTuple&amp;lt;IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;&amp;gt; bifunctor, which is literally a 2-tuple (IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;). For convenience, the explicit ValueTuple&amp;lt;,&amp;gt; parameter can be represented by an implicit tuple, a pair of parameters. So the monoidal functor definition is equivalent to:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;
{&lt;/span&gt;&lt;span style="color: green;"&gt;
    // Multiply: TMonoidalFunctor&amp;lt;T1&amp;gt; x TMonoidalFunctor&amp;lt;T2&amp;gt; -&amp;gt; TMonoidalFunctor&amp;lt;T1 x T2&amp;gt;
    // Multiply: (TMonoidalFunctor&amp;lt;T1&amp;gt;, TMonoidalFunctor&amp;lt;T2&amp;gt;) -&amp;gt; TMonoidalFunctor&amp;lt;(T1, T2)&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2)&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; source2); &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit&lt;/span&gt;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; TMonoidalFunctor&amp;lt;Unit&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;IEnumerable&amp;lt;&amp;gt; monoidal functor&lt;/h2&gt;
&lt;p&gt;IEnumerable&amp;lt;&amp;gt; functor is a monoidal functor. Its Multiply method can be implemented as its extension method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: IEnumerable&amp;lt;T1&amp;gt; x IEnumerable&amp;lt;T2&amp;gt; -&amp;gt; IEnumerable&amp;lt;T1 x T2&amp;gt;
    // Multiply: ValueTuple&amp;lt;IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;&amp;gt; -&amp;gt; IEnumerable&amp;lt;ValueTuple&amp;lt;T1, T2&amp;gt;&amp;gt;
    // Multiply: (IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;) -&amp;gt; IEnumerable&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) &lt;/span&gt;&lt;span style="color: green;"&gt;// Implicit tuple.
    &lt;/span&gt;&lt;span style="color: black;"&gt;{
        &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1 &lt;/span&gt;&lt;span style="color: black;"&gt;value1 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2 &lt;/span&gt;&lt;span style="color: black;"&gt;value2 &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;(value1, value2);
            }
        }
    }

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; IEnumerable&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;unit;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now extension method Multiply can be used as a infix operator. It can be verified that the above Multiply and Unit implementations preserve the monoid laws by working with associator, left unitor and right unitor of DotNet monoidal category:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// using static Dixin.Linq.CategoryTheory.DotNetCategory;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonoidalFunctorLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; unit = Unit();
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1 };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new char&lt;/span&gt;&lt;span style="color: black;"&gt;[] { &lt;/span&gt;&lt;span style="color: #a31515;"&gt;'@'&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #a31515;"&gt;'#' &lt;/span&gt;&lt;span style="color: black;"&gt;};
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source3 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new bool&lt;/span&gt;&lt;span style="color: black;"&gt;[] { &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;};
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 };

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: source1.Multiply(source2).Multiply(source3).Select(Associator) == source1.Multiply(source2.Multiply(source3)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source1.Multiply(source2).Multiply(source3).Select(Associator).WriteLines();
        &lt;/span&gt;&lt;span style="color: green;"&gt;// (0, (@, True)) (0, (@, False)) (0, (#, True)) (0, (#, False))
        // (1, (@, True)) (1, (@, False)) (1, (#, True)) (1, (#, False))
    &lt;/span&gt;&lt;span style="color: black;"&gt;source1.Multiply(source2.Multiply(source3)).WriteLines();
        &lt;/span&gt;&lt;span style="color: green;"&gt;// (0, (@, True)) (0, (@, False)) (0, (#, True)) (0, (#, False))
        // (1, (@, True)) (1, (@, False)) (1, (#, True)) (1, (#, False))
    // Left unit preservation: unit.Multiply(source).Select(LeftUnitor) == source.
    &lt;/span&gt;&lt;span style="color: black;"&gt;unit.Multiply(source).Select(LeftUnitor).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    // Right unit preservation: source == source.Multiply(unit).Select(RightUnitor).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Multiply(unit).Select(RightUnitor).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;How could these methods be useful? Remember functor&amp;rsquo;s Select method enables selector working with value(s) wrapped by functor:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Selector1Arity(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; xs)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = x =&amp;gt; x &amp;gt; 0;
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply selector with xs.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; applyWithXs = xs.Select(selector);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Select can be viewed as applying 1 arity selector (a TSource &amp;ndash;&amp;gt; TResult function) with TFunctor&amp;lt;TSource&amp;gt;. For a N arity selector, to have it work with value(s) wrapped by functor, first curry it, so that it can be viewed as 1 arity function. In the following example, the (T1, T2, T3) &amp;ndash;&amp;gt; TResult selector is curried to T1 &amp;ndash;&amp;gt; (T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult) function, so that it can be viewed as only have 1 parameter, and can work with TFunctor&amp;lt;T1&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;SelectorNArity(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; xs, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ys, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; zs)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = (x, y, z) =&amp;gt; x + y + z &amp;gt; 0;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Curry selector.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; curriedSelector = 
        selector.Curry(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 1 arity: x =&amp;gt; (y =&amp;gt; z =&amp;gt; x + y + z &amp;gt; 0)
    // Partially apply selector with xs.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; applyWithXs = xs.Select(curriedSelector);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So partially applying the T1 &amp;ndash;&amp;gt; (T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult) selector with TFunctor&amp;lt;T1&amp;gt; returns TFunctor&amp;lt;T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult&amp;gt;, where the T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult function is wrapped by the TFunctor&amp;lt;&amp;gt; functor. To further apply TFunctor&amp;lt;T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult&amp;gt; with TFunctor&amp;lt;T2&amp;gt;, Multiply can be called:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;    // Partially apply selector with ys.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; multiplyWithYs = applyWithXs.Multiply(ys);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; applyWithYs = multiplyWithYs.Select(product =&amp;gt;
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; partialAppliedSelector = product.Item1;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;long &lt;/span&gt;&lt;span style="color: black;"&gt;y = product.Item2;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;partialAppliedSelector(y);
    });&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The result of Multiply is TFunctor&amp;lt;(T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult, T2)&amp;gt;, where each T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult function is paired with each T2 value, so that each function can be applied with each value, And TFunctor&amp;lt;(T2 &amp;ndash;&amp;gt; T3 &amp;ndash;&amp;gt; TResult, T2)&amp;gt; is mapped to TFunctor&amp;lt;(T3 &amp;ndash;&amp;gt; TResult)&amp;gt;, which can be applied with TFunctor&amp;lt;T3&amp;gt; in the same way:&lt;/p&gt;
&lt;pre class="code"&gt;    &lt;span style="color: green;"&gt;// Partially apply selector with zs.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; multiplyWithZs = applyWithYs.Multiply(zs);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; applyWithZs = multiplyWithZs.Select(product =&amp;gt;
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; partialAppliedSelector = product.Item1;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;double &lt;/span&gt;&lt;span style="color: black;"&gt;z = product.Item2;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;partialAppliedSelector(z);
    });
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Multiply enables applying functor-wrapped functions (TFunctor&amp;lt;T &amp;ndash;&amp;gt; TResult&amp;gt;) with functor-wrapped values (TFunctor&amp;lt;TSource&amp;gt;), which returns functor-wrapped results (TFunctor&amp;lt;TResult&amp;gt;). Generally, the Multiply and Select calls can be encapsulated as the following Apply method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Apply: (IEnumerable&amp;lt;TSource -&amp;gt; TResult&amp;gt;, IEnumerable&amp;lt;TSource&amp;gt;) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
        selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that the above N arity selector application becomes:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Apply(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; xs, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ys, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; zs)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = (x, y, z) =&amp;gt; x + y + z &amp;gt; 0;
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Partially apply selector with xs.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; applyWithXs = xs.Select(selector.Curry());
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Partially apply selector with ys.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; applyWithYs = applyWithXs.Apply(ys);
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Partially apply selector with zs.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; applyWithZs = applyWithYs.Apply(zs);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Applicative functor&lt;/h1&gt;
&lt;p&gt;A functor, with the above ability to apply functor-wrapped functions with functor-wrapped values, is also called applicative functor. The following is the definition of applicative functor:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;
    &lt;span style="color: blue;"&gt;where&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// From: IFunctor&amp;lt;TApplicativeFunctor&amp;lt;&amp;gt;&amp;gt;:
    // Select: (TSource -&amp;gt; TResult) -&amp;gt; (TApplicativeFunctor&amp;lt;TSource&amp;gt; -&amp;gt; TApplicativeFunctor&amp;lt;TResult&amp;gt;)
    // Func&amp;lt;TApplicativeFunctor&amp;lt;TSource&amp;gt;, TApplicativeFunctor&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);

    // Apply: (TApplicativeFunctor&amp;lt;TSource -&amp;gt; TResult&amp;gt;, TApplicativeFunctor&amp;lt;TSource&amp;gt; -&amp;gt; TApplicativeFunctor&amp;lt;TResult&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; TApplicativeFunctor&amp;lt;TSource&amp;gt;
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Wrap&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And applicative functor must satisfy the applicative laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Functor preservation: applying function is equivalent to applying functor-wrapped function&lt;/li&gt;
&lt;li&gt;Identity preservation: applying functor-wrapped identity function, is equivalent to doing nothing.&lt;/li&gt;
&lt;li&gt;Composition preservation: functor-wrapped functions can be composed by applying.&lt;/li&gt;
&lt;li&gt;Homomorphism: applying functor-wrapped function with functor-wrapped value, is equivalent to functor-wrapping the result of applying that function with that value.&lt;/li&gt;
&lt;li&gt;Interchange: when applying functor-wrapped functions with a functor-wrapped value, the functor-wrapped functions and the functor-wrapped value can interchange position.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;IEnumerable&amp;lt;&amp;gt; applicative functor&lt;/h2&gt;
&lt;p&gt;IEnumerable&amp;lt;&amp;gt; functor is a applicative functor. Again, these methods are implemented as extension methods. And for IEnumerable&amp;lt;&amp;gt;, the Wrap method is called Enumerable to be intuitive:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (IEnumerable&amp;lt;TSource -&amp;gt; TResult&amp;gt;, IEnumerable&amp;lt;TSource&amp;gt;) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value);
            }
        }
    }

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Enumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;value;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It can be verified that the above Apply and Wrap (Enumerable) implementations satisfy the applicative laws:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;ApplicativeLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper1 =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;[] { int32 =&amp;gt; int32 / 2D, int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32) };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper2 =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;[] { @double =&amp;gt; @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.0"&lt;/span&gt;&lt;span style="color: black;"&gt;), @double =&amp;gt; @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;) };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; o =
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(Linq.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions&lt;/span&gt;&lt;span style="color: black;"&gt;.o).Curry();
    &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value = 5;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor preservation: source.Select(selector) == selector.Wrap().Apply(source).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Select(selector).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 1.4142135623731 1.73205080756888 2
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector.Enumerable().Apply(source).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 1.4142135623731 1.73205080756888 2
    // Identity preservation: Id.Wrap().Apply(source) == source.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;.Id).Enumerable().Apply(source).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    // Composition preservation: o.Wrap().Apply(selectorWrapper2).Apply(selectorWrapper1).Apply(source) == selectorWrapper2.Apply(selectorWrapper1.Apply(source)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;o.Enumerable().Apply(selectorWrapper2).Apply(selectorWrapper1).Apply(source).WriteLines();
        &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0  0.5  1.0  1.5  2.0
        // 0.0  1.0  1.4  1.7  2.0 
        // 0.00 0.50 1.00 1.50 2.00
        // 0.00 1.00 1.41 1.73 2.00
    &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper2.Apply(selectorWrapper1.Apply(source)).WriteLines();
        &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.0  0.5  1.0  1.5  2.0
        // 0.0  1.0  1.4  1.7  2.0 
        // 0.00 0.50 1.00 1.50 2.00
        // 0.00 1.00 1.41 1.73 2.00
    // Homomorphism: selector.Wrap().Apply(value.Wrap()) == selector(value).Wrap().
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector.Enumerable().Apply(value.Enumerable()).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 2.23606797749979
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value).Enumerable().WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 2.23606797749979
    // Interchange: selectorWrapper.Apply(value.Wrap()) == (selector =&amp;gt; selector(value)).Wrap().Apply(selectorWrapper).
    &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper1.Apply(value.Enumerable()).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 2.5 2.23606797749979
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(function =&amp;gt; function(value)).Enumerable().Apply(selectorWrapper1)
        .WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 2.5 2.23606797749979
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Monoidal functor vs. applicative functor&lt;/h1&gt;
&lt;p&gt;The applicative functor definition is actually equivalent to above monoidal functor definition. First, applicative functor&amp;rsquo;s Apply and Wrap methods can be implemented by monoidal functor&amp;rsquo;s Multiply and Unit methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (IEnumerable&amp;lt;TSource -&amp;gt; TResult&amp;gt;, IEnumerable&amp;lt;TSource&amp;gt;) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; IEnumerable&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Enumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;On the other hand, monoidal functor&amp;rsquo;s Multiply and Unit methods can be implemented by applicative functor&amp;rsquo;s Apply and Wrap methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: IEnumerable&amp;lt;T1&amp;gt; x IEnumerable&amp;lt;T2&amp;gt; -&amp;gt; IEnumerable&amp;lt;T1 x T2&amp;gt;
    // Multiply: (IEnumerable&amp;lt;T1&amp;gt;, IEnumerable&amp;lt;T2&amp;gt;) -&amp;gt; IEnumerable&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;.Create).Curry().Enumerable().Apply(source1).Apply(source2);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; IEnumerable&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; unit.Enumerable();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Generally, for any applicative functor, its (Apply, Wrap) method pair can implement the (Multiply, Unit) method pair required as monoidal functor, and vice versa. This can be virtually demonstrated as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonoidalFunctorExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (Multiply, Unit) implements (Apply, Wrap).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (TMonoidalFunctor&amp;lt;TSource -&amp;gt; TResult&amp;gt;, TMonoidalFunctor&amp;lt;TSource&amp;gt;) -&amp;gt; TMonoidalFunctor&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) 
        &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            selectorWrapper&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;.Multiply(source&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;).Select(product &lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;=&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; TMonoidalFunctor&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Wrap&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) 
        &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;&lt;span style="color: blue;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: #2b91af;"&gt;TMonoidalFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.U&lt;/span&gt;nit().Select(unit &lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;=&amp;gt; value);
}

&lt;/span&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicativeFunctorExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// (Apply, Wrap) implements (Multiply, Unit).
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: TApplicativeFunctor&amp;lt;T1&amp;gt; x TApplicativeFunctor&amp;lt;T2&amp;gt; -&amp;gt; TApplicativeFunctor&amp;lt;T1 x T2&amp;gt;
    // Multiply: (TApplicativeFunctor&amp;lt;T1&amp;gt;, TApplicativeFunctor&amp;lt;T2&amp;gt;) -&amp;gt; TApplicativeFunctor&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) 
        &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;.Create&lt;/span&gt;&lt;span style="color: black;"&gt;).Curry().Wrap().Apply(source1&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;).Apply(source2&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; TApplicativeFunctor&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt;(Unit unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IApplicativeFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TApplicativeFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; =&amp;gt; unit&lt;/span&gt;&lt;span style="color: black;"&gt;.Wrap();
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;More Monoidal functors and applicative functors&lt;/h1&gt;
&lt;p&gt;The Lazy&amp;lt;&amp;gt;, Func&amp;lt;&amp;gt;, Func&amp;lt;T,&amp;gt; functors are also monoidal/applicative functors:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Lazy&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;Lazy&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Lazy&amp;lt;T1&amp;gt; x Lazy&amp;lt;T2&amp;gt; -&amp;gt; Lazy&amp;lt;T1 x T2&amp;gt;
    // Multiply: (Lazy&amp;lt;T1&amp;gt;, Lazy&amp;lt;T2&amp;gt;) -&amp;gt; Lazy&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(() =&amp;gt; (source1.Value, source2.Value));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; Lazy&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; unit);
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Lazy&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;Lazy&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (Lazy&amp;lt;TSource -&amp;gt; TResult&amp;gt;, Lazy&amp;lt;TSource&amp;gt;) -&amp;gt; Lazy&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Lazy&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Lazy&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;Func&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Func&amp;lt;T1&amp;gt; x Func&amp;lt;T2&amp;gt; -&amp;gt; Func&amp;lt;T1 x T2&amp;gt;
    // Multiply: (Func&amp;lt;T1&amp;gt;, Func&amp;lt;T2&amp;gt;) -&amp;gt; Func&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        () =&amp;gt; (source1(), source2());

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; Func&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; () =&amp;gt; unit;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;Func&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (Func&amp;lt;TSource -&amp;gt; TResult&amp;gt;, Func&amp;lt;TSource&amp;gt;) -&amp;gt; Func&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Func&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Func&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T, TResult&amp;gt; : IMonoidalFunctor&amp;lt;Func&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Func&amp;lt;T, T1&amp;gt; x Func&amp;lt;T, T2&amp;gt; -&amp;gt; Func&amp;lt;T, T1 x T2&amp;gt;
    // Multiply: (Func&amp;lt;T, T1&amp;gt;, Func&amp;lt;T, T2&amp;gt;) -&amp;gt; Func&amp;lt;T, (T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        value =&amp;gt; (source1(value), source2(value));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; Func&amp;lt;T, Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; _ =&amp;gt; unit;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T, TResult&amp;gt; : IApplicativeFunctor&amp;lt;Func&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (Func&amp;lt;T, TSource -&amp;gt; TResult&amp;gt;, Func&amp;lt;T, TSource&amp;gt;) -&amp;gt; Func&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Func&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Func&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;OptionalExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Optional&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;Optional&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Optional&amp;lt;T1&amp;gt; x Optional&amp;lt;T2&amp;gt; -&amp;gt; Optional&amp;lt;T1 x T2&amp;gt;
    // Multiply: (Optional&amp;lt;T1&amp;gt;, Optional&amp;lt;T2&amp;gt;) -&amp;gt; Optional&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(() =&amp;gt; source1.HasValue &amp;amp;&amp;amp; source2.HasValue
            ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, (source1.Value, source2.Value))
            : (&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;)));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Unit: Unit -&amp;gt; Optional&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, unit));
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;OptionalExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Optional&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;Optional&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (Optional&amp;lt;TSource -&amp;gt; TResult&amp;gt;, Optional&amp;lt;TSource&amp;gt;) -&amp;gt; Optional&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Wrap: TSource -&amp;gt; Optional&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Optional&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The ValueTuple&amp;lt;&amp;gt; and Task&amp;lt;&amp;gt; functors are monoidal/applicative functors too. Notice their Multiply/Apply methods cannot defer the execution, and Task&amp;lt;&amp;gt;&amp;rsquo;s Multiply/Apply methods are impure.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;ValueTuple&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: ValueTuple&amp;lt;T1&amp;gt; x ValueTuple&amp;lt;T2&amp;gt; -&amp;gt; ValueTuple&amp;lt;T1 x T2&amp;gt;
    // Multiply: (ValueTuple&amp;lt;T1&amp;gt;, ValueTuple&amp;lt;T2&amp;gt;) -&amp;gt; ValueTuple&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;((source1.Item1, source2.Item1)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Unit: Unit -&amp;gt; ValueTuple&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(unit);
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;ValueTuple&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (ValueTuple&amp;lt;TSource -&amp;gt; TResult&amp;gt;, ValueTuple&amp;lt;TSource&amp;gt;) -&amp;gt; ValueTuple&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Wrap: TSource -&amp;gt; ValueTuple&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}
&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TaskExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Task&amp;lt;T&amp;gt; : IMonoidalFunctor&amp;lt;Task&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: Task&amp;lt;T1&amp;gt; x Task&amp;lt;T2&amp;gt; -&amp;gt; Task&amp;lt;T1 x T2&amp;gt;
    // Multiply: (Task&amp;lt;T1&amp;gt;, Task&amp;lt;T2&amp;gt;) -&amp;gt; Task&amp;lt;(T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2) =&amp;gt;
        ((&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;source1), (&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;source2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.

    // Unit: Unit -&amp;gt; Task&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Unit(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; System.Threading.Tasks.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;.FromResult(unit);
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TaskExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Task&amp;lt;T&amp;gt; : IApplicativeFunctor&amp;lt;Task&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (Task&amp;lt;TSource -&amp;gt; TResult&amp;gt;, Task&amp;lt;TSource&amp;gt;) -&amp;gt; Task&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; selectorWrapper, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.

    // Wrap: TSource -&amp;gt; Task&amp;lt;TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Task&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is easy to verify all the above (Multiply, Unit) method pairs preserve the monoid laws, and all the above (Apply, Wrap) method pairs satisfy the applicative laws. However, not any (Multiply, Unit) or any (Apply, Wrap) can automatically satisfy the laws. Take the ValueTuple&amp;lt;T,&amp;gt; functor as example:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T1, T2 : IMonoidalFunctor&amp;lt;ValueTuple&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply: ValueTuple&amp;lt;T, T1&amp;gt; x ValueTuple&amp;lt;T, T2&amp;gt; -&amp;gt; ValueTuple&amp;lt;T, T1 x T2&amp;gt;
    // Multiply: (ValueTuple&amp;lt;T, T1&amp;gt;, ValueTuple&amp;lt;T, T2&amp;gt;) -&amp;gt; ValueTuple&amp;lt;T, (T1, T2)&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)) Multiply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;) source1, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;) source2) =&amp;gt;
        (source1.Item1, (source1.Item2, source2.Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Unit: Unit -&amp;gt; ValueTuple&amp;lt;Unit&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;) Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;unit = &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;, unit);
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T, TResult&amp;gt; : IApplicativeFunctor&amp;lt;ValueTuple&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Apply: (ValueTuple&amp;lt;T, TSource -&amp;gt; TResult&amp;gt;, ValueTuple&amp;lt;T, TSource&amp;gt;) -&amp;gt; ValueTuple&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;) Apply&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;) selectorWrapper, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) source) =&amp;gt;
            selectorWrapper.Multiply(source).Select(product =&amp;gt; product.Item1(product.Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // Wrap: TSource -&amp;gt; ValueTuple&amp;lt;T, TSource&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; Unit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;().Select(unit =&amp;gt; value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above (Multiply, Unit) implementations cannot preserve the left unit law:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;MonoidalFunctorLaws()
{
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;) source = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;&lt;span style="color: black;"&gt;, 1);
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;) unit = Unit&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;();
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;) source1 = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"b"&lt;/span&gt;&lt;span style="color: black;"&gt;, 2);
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;char&lt;/span&gt;&lt;span style="color: black;"&gt;) source2 = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"c"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #a31515;"&gt;'@'&lt;/span&gt;&lt;span style="color: black;"&gt;);
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;) source3 = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"d"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: source1.Multiply(source2).Multiply(source3).Select(Associator) == source1.Multiply(source2.Multiply(source3)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source1.Multiply(source2).Multiply(source3).Select(Associator).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (b, (2, (@, True)))
    &lt;/span&gt;&lt;span style="color: black;"&gt;source1.Multiply(source2.Multiply(source3)).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (b, (2, (@, True)))
    // Left unit preservation: unit.Multiply(source).Select(LeftUnitor) == source.
    &lt;/span&gt;&lt;span style="color: black;"&gt;unit.Multiply(source).Select(LeftUnitor).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    // Right unit preservation: source == source.Multiply(unit).Select(RightUnitor).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Multiply(unit).Select(RightUnitor).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1)
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the above (Apply, Wrap) implementation breaks all applicative laws:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;ApplicativeLaws()
{
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;) source = (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"a"&lt;/span&gt;&lt;span style="color: black;"&gt;, 1);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32);
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;) selectorWrapper1 = 
        (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"b"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32)));
    (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;) selectorWrapper2 =
        (&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"c"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(@double =&amp;gt; @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;)));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; o = 
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(Linq.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions&lt;/span&gt;&lt;span style="color: black;"&gt;.o).Curry();
    &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value = 5;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor preservation: source.Select(selector) == selector.Wrap().Apply(source).
    &lt;/span&gt;&lt;span style="color: black;"&gt;source.Select(selector).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (a, 1)
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;().Apply(source).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    // Identity preservation: Id.Wrap().Apply(source) == source.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;.Id).ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;().Apply(source).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1)
    // Composition preservation: o.Curry().Wrap().Apply(selectorWrapper2).Apply(selectorWrapper1).Apply(source) == selectorWrapper2.Apply(selectorWrapper1.Apply(source)).
    &lt;/span&gt;&lt;span style="color: black;"&gt;o.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;()
        .Apply(selectorWrapper2).Apply(selectorWrapper1).Apply(source).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 1.00)
    &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper2.Apply(selectorWrapper1.Apply(source)).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (c, 1.00)
    // Homomorphism: selector.Wrap().Apply(value.Wrap()) == selector(value).Wrap().
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;().Apply(value.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 2.23606797749979)
    &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value).ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;().WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 2.23606797749979)
    // Interchange: selectorWrapper.Apply(value.Wrap()) == (selector =&amp;gt; selector(value)).Wrap().Apply(selectorWrapper).
    &lt;/span&gt;&lt;span style="color: black;"&gt;selectorWrapper1.Apply(value.ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (b, 2.23606797749979)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(function =&amp;gt; function(value))
        .ValueTuple&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;().Apply(selectorWrapper1).WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// (, 2.23606797749979)
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Wed, 25 Dec 2024 13:12:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-6-monoidal-functor-and-applicative-functor</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>LINQ via C#</category><category>Applicative Functors</category><category>Monadal Functors</category></item><item><title>Category Theory via C# (5) Bifunctor</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-5-bifunctor</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Bifunctor&lt;/h1&gt;
&lt;p&gt;A functor is the mapping from 1 object to another object, with a &amp;ldquo;Select&amp;rdquo; ability to map 1 morphism to another morphism. A &lt;a href="http://en.wikipedia.org/wiki/Functor#Bifunctors_and_multifunctors" target="_blank"&gt;bifunctor&lt;/a&gt; (binary functor), as the name implies, is the mapping from 2 objects and from 2 morphisms. Giving category C, D and E, bifunctor F from category C, D to E is a structure-preserving morphism from C, D to E, denoted F: C &amp;times; D &amp;rarr; E:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-9-Functor-Category_8A55/image1.png"&gt;&lt;img width="640" height="744" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-9-Functor-Category_8A55/image1_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;F maps objects X &amp;isin; ob(C), Y &amp;isin; ob(D) to object F(X, Y) &amp;isin; ob(E)&lt;/li&gt;
&lt;li&gt;F also maps morphisms m&lt;sub&gt;C&lt;/sub&gt;: X &amp;rarr; X&amp;rsquo; &amp;isin; hom(C), m&lt;sub&gt;D&lt;/sub&gt;: Y &amp;rarr; Y&amp;rsquo; &amp;isin; hom(D) to morphism m&lt;sub&gt;E&lt;/sub&gt;: F(X, Y) &amp;rarr; F(X&amp;rsquo;, Y&amp;rsquo;) &amp;isin; hom(E)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In DotNet category, bifunctors are binary endofunctors, and can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IBifunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TBifunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;,&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TBifunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;,&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IBifunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TBifunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;,&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TBifunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TBifunctor&lt;span style="color: #2b91af;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The most intuitive built-in bifunctor is ValueTuple&amp;lt;,&amp;gt;. Apparently ValueTuple&amp;lt;,&amp;gt; can be viewed as a type constructor of kind * &amp;ndash;&amp;gt; * &amp;ndash;&amp;gt; *, which accepts 2 concrete types to and return another concrete type. Its Select implementation is also straightforward:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T1, T2&amp;gt; : IBifunctor&amp;lt;ValueTuple&amp;lt;,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Bifunctor Select: (TSource1 -&amp;gt; TResult1, TSource2 -&amp;gt; TResult2) -&amp;gt; (ValueTuple&amp;lt;TSource1, TSource2&amp;gt; -&amp;gt; ValueTuple&amp;lt;TResult1, TResult2&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2) =&amp;gt; source =&amp;gt;
            Select(source, selector1, selector2);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ-like Select: (ValueTuple&amp;lt;TSource1, TSource2&amp;gt;, TSource1 -&amp;gt; TResult1, TSource2 -&amp;gt; TResult2) -&amp;gt; ValueTuple&amp;lt;TResult1, TResult2&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2) =&amp;gt;
            (selector1(source.Item1), selector2(source.Item2));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;However, similar to ValueTuple&amp;lt;&amp;gt; functor&amp;rsquo;s Select method, ValueTuple&amp;lt;,&amp;gt; bifunctor&amp;rsquo;s Select method has to call selector1 and selector2 immediately. To implement deferred execution, the following Lazy&amp;lt;,&amp;gt; bifunctor can be defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; lazy;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: black;"&gt;Lazy(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; factory) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(factory);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1 &lt;/span&gt;&lt;span style="color: black;"&gt;Value1 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy.Value.Item1;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2 &lt;/span&gt;&lt;span style="color: black;"&gt;Value2 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy.Value.Item2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public override string &lt;/span&gt;&lt;span style="color: black;"&gt;ToString() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.lazy.Value.ToString();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Lazy&amp;lt;,&amp;gt; is simply the lazy version of ValueTuple&amp;lt;,&amp;gt;. Jut like Lazy&amp;lt;&amp;gt;, Lazy&amp;lt;,&amp;gt; can be constructed with a factory function, so that the call to selector1 and selector2 are deferred:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Lazy&amp;lt;T1, T2&amp;gt; : IBifunctor&amp;lt;Lazy&amp;lt;,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Bifunctor Select: (TSource1 -&amp;gt; TResult1, TSource2 -&amp;gt; TResult2) -&amp;gt; (Lazy&amp;lt;TSource1, TSource2&amp;gt; -&amp;gt; Lazy&amp;lt;TResult1, TResult2&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2) =&amp;gt; source =&amp;gt;
            Select(source, selector1, selector2);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ-like Select: (Lazy&amp;lt;TSource1, TSource2&amp;gt;, TSource1 -&amp;gt; TResult1, TSource2 -&amp;gt; TResult2) -&amp;gt; Lazy&amp;lt;TResult1, TResult2&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1,
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (selector1(source.Value1), selector2(source.Value2)));
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Monoidal category&lt;/h1&gt;
&lt;p&gt;With the help of bifunctor, &lt;a href="http://en.wikipedia.org/wiki/Monoidal_category" target="_blank"&gt;monoidal category&lt;/a&gt; can be defined. A &lt;a href="http://en.wikipedia.org/wiki/Monoidal_category" target="_blank"&gt;monoidal category&lt;/a&gt; is a category C equipped with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A bifunctor &amp;otimes; as the monoid binary multiplication operation: bifunctor &amp;otimes; maps 2 objects in C to another object in C, denoted C &amp;otimes; C &amp;rarr; C, which is also called the &lt;a href="http://en.wikipedia.org/wiki/Tensor_product" target="_blank"&gt;monoidal product&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Tensor_product"&gt;tensor product&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;An unit object I &amp;isin; ob(C) as the monoid unit, also called tensor unit&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For (C, &amp;otimes;, I) to be a monoid, it also needs to be equipped with the following natural transformations, so that the monoid laws are satisfied:&lt;/p&gt;
&lt;ul&gt;&lt;!--StartFragment--&gt;
&lt;li&gt;Associator &amp;alpha;&lt;sub&gt;X, Y, Z&lt;/sub&gt;: (X &amp;otimes; Y) &amp;otimes; Z &amp;rArr; X &amp;otimes; (Y &amp;otimes; Z) for the associativity law, where X, Y, Z &amp;isin; ob(C)&lt;/li&gt;
&lt;li&gt;Left unitor &amp;lambda;&lt;sub&gt;X&lt;/sub&gt;: I &amp;otimes; X &amp;rArr; X for the left unit law, and right unitor &amp;rho;&lt;sub&gt;X&lt;/sub&gt;: X &amp;otimes; I &amp;rArr; X for the right unit law, where X &amp;isin; ob(C)&lt;/li&gt;
&lt;li&gt;&lt;!--EndFragment--&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following monoid triangle identity and pentagon identity diagrams still commute for monoidal category:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-5-Bifunctor_548F/image_thumb12_thumb_2.png"&gt;&lt;img width="640" height="252" title="image_thumb12_thumb" style="margin: 0px; border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image_thumb12_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-5-Bifunctor_548F/image_thumb12_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-5-Bifunctor_548F/Untitled-2.fw_thumb_thumb_2.png"&gt;&lt;img width="640" height="376" title="Untitled-2.fw_thumb_thumb" style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="Untitled-2.fw_thumb_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-5-Bifunctor_548F/Untitled-2.fw_thumb_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here for monoidal category, the above ⊙ (general multiplication operator) becomes &amp;otimes; (bifunctor).&lt;/p&gt;
&lt;p&gt;Monoidal category can be simply defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoidalCategory&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TObject&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMorphism&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ICategory&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TObject&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMorphism&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TObject&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; { }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;DotNet category is monoidal category, with the most intuitive bifunctor ValueTuple&amp;lt;,&amp;gt; as the monoid multiplication, and Unit type as the monoid unit:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;DotNetCategory &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoidalCategory&amp;lt;Type, Delegate&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Type Multiply(Type value1, Type value2) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(ValueTuple&amp;lt;,&amp;gt;).MakeGenericType(value1, value2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Type Unit =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(Unit);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To have (DotNet, ValueTuple&amp;lt;,&amp;gt;, Unit) satisfy the monoid laws, the associator, left unitor and right unitor are easy to implement:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;DotNetCategory
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associator: (T1 x T2) x T3 -&amp;gt; T1 x (T2 x T3)
    // Associator: ValueTuple&amp;lt;ValueTuple&amp;lt;T1, T2&amp;gt;, T3&amp;gt; -&amp;gt; ValueTuple&amp;lt;T1, ValueTuple&amp;lt;T2, T3&amp;gt;&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;)) Associator&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;), &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;) product) =&amp;gt;
        (product.Item1.Item1, (product.Item1.Item2, product.Item2));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LeftUnitor: Unit x T -&amp;gt; T
    // LeftUnitor: ValueTuple&amp;lt;Unit, T&amp;gt; -&amp;gt; T
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;LeftUnitor&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;) product) =&amp;gt; product.Item2;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// RightUnitor: T x Unit -&amp;gt; T
    // RightUnitor: ValueTuple&amp;lt;T, Unit&amp;gt; -&amp;gt; T
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;RightUnitor&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;) product) =&amp;gt; product.Item1;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sun, 15 Dec 2024 13:11:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-5-bifunctor</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>Categories</category><category>LINQ via C#</category><category>Bifunctors</category><category>Monadal Categories</category></item><item><title>Category Theory via C# (4) Natural Transformation</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-4-natural-transformation</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Natural transformation and naturality&lt;/h1&gt;
&lt;p&gt;If F: C &amp;rarr; D and G: C &amp;rarr; D are both functors from categories C to category D, the mapping from F to G is called &lt;a href="http://en.wikipedia.org/wiki/Natural_transformation" target="_blank"&gt;natural transformation&lt;/a&gt; and denoted &amp;alpha;: F &amp;rArr; G. &amp;alpha;: F &amp;rArr; G is actually family of morphisms from F to G, For each object X in category C, there is a specific morphism &amp;alpha;&lt;sub&gt;X&lt;/sub&gt;: F(X) &amp;rarr; G(X) in category D, called the component of &amp;alpha; at X. For each morphism m: X &amp;rarr; Y in category C and 2 functors F: C &amp;rarr; D, G: C &amp;rarr; D, there is a naturality square in D:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_2.png"&gt;&lt;img width="826" height="748" title="image" style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In another word, for m: X &amp;rarr; Y in category C, there must be &amp;alpha;&lt;sub&gt;Y&lt;/sub&gt; ∘ F(m) &amp;equiv; G(m) ∘ &amp;alpha;&lt;sub&gt;X&lt;/sub&gt; , or equivalently &amp;alpha;&lt;sub&gt;Y&lt;/sub&gt; ∘ Select&lt;sub&gt;F&lt;/sub&gt;(m) &amp;equiv; Select&lt;sub&gt;G&lt;/sub&gt;(m) ∘ &amp;alpha;&lt;sub&gt;X &lt;/sub&gt;in category D.&lt;/p&gt;
&lt;p&gt;In DotNet category, the following ToLazy&amp;lt;&amp;gt; generic method transforms Func&amp;lt;&amp;gt; functor to Lazy&amp;lt;&amp;gt; functor:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NaturalTransformations
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// ToLazy: Func&amp;lt;&amp;gt; -&amp;gt; Lazy&amp;lt;&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToLazy&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(function);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Apparently, for above natural transformation: ToLazy&amp;lt;&amp;gt;: Func&amp;lt;&amp;gt; &amp;rArr; Lazy&amp;lt;&amp;gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;for each specific object T, there is an object Func&amp;lt;T&amp;gt;, an object Lazy&amp;lt;T&amp;gt;, and a morphism ToFunc&amp;lt;T&amp;gt;: Func&amp;lt;T&amp;gt; &amp;rarr; Lazy&amp;lt;T&amp;gt;.&lt;/li&gt;
&lt;li&gt;For each specific morphism selector: TSource &amp;rarr; TResult, there is a naturality square, which consists of 4 morphisms:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;ToLazy&amp;lt;TResult&amp;gt;: Func&amp;lt;TResult&amp;gt; &amp;rarr; Lazy&amp;lt;TResult&amp;gt;, which is the component of ToLazy&amp;lt;&amp;gt; at TResult&lt;/li&gt;
&lt;li&gt;FuncExtensions.Select(selector): Func&amp;lt;TSource&amp;gt; &amp;rarr; Func&amp;lt;TResult&amp;gt;&lt;/li&gt;
&lt;li&gt;LazyExtensions.Select(selector): Lazy&amp;lt;TSource&amp;gt; &amp;rarr; Lazy&amp;lt;TResult&amp;gt;&lt;/li&gt;
&lt;li&gt;ToLazy&amp;lt;TSource&amp;gt;: Func&amp;lt;TSource&amp;gt; &amp;rarr; Lazy&amp;lt;TSource&amp;gt;, which is the component of ToLazy&amp;lt;&amp;gt; at TSource&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_4.png"&gt;&lt;img width="1028" height="741" title="image" style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_thumb_1.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The following example is a simple naturality square that commutes for ToLazy&amp;lt;&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Naturality()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32).ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Naturality square:
    // ToFunc&amp;lt;string&amp;gt;.o(LazyExtensions.Select(selector)) == FuncExtensions.Select(selector).o(ToFunc&amp;lt;int&amp;gt;)
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; funcStringToLazyString = ToLazy&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; funcInt32ToFuncString = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions&lt;/span&gt;&lt;span style="color: black;"&gt;.Select(selector);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; leftComposition = funcStringToLazyString.o(funcInt32ToFuncString);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; lazyInt32ToLazyString = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions&lt;/span&gt;&lt;span style="color: black;"&gt;.Select(selector);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; funcInt32ToLazyInt32 = ToLazy&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; rightComposition = lazyInt32ToLazyString.o(funcInt32ToLazyInt32);

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; funcInt32 = () =&amp;gt; 2;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazyString = leftComposition(funcInt32);
    lazyString.Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 1.41
    &lt;/span&gt;&lt;span style="color: black;"&gt;lazyString = rightComposition(funcInt32);
    lazyString.Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 1.41
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the following are a few more examples of natural transformations:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// ToFunc: Lazy&amp;lt;T&amp;gt; -&amp;gt; Func&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToFunc&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazy) =&amp;gt; () =&amp;gt; lazy.Value;

&lt;/span&gt;&lt;span style="color: green;"&gt;// ToEnumerable: Func&amp;lt;T&amp;gt; -&amp;gt; IEnumerable&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToEnumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;function();
}

&lt;/span&gt;&lt;span style="color: green;"&gt;// ToEnumerable: Lazy&amp;lt;T&amp;gt; -&amp;gt; IEnumerable&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToEnumerable&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazy)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;lazy.Value;
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Functor Category&lt;/h1&gt;
&lt;p&gt;Now there are functors, and mappings between functors, which are natural transformations. Naturally, they lead to category of functors. Given 2 categories C and D, there is a &lt;a href="http://en.wikipedia.org/wiki/Functor_category" target="_blank"&gt;functor category&lt;/a&gt;, denoted D&lt;sup&gt;C&lt;/sup&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Its objects ob(D&lt;sup&gt;C&lt;/sup&gt;) are the functors from category C to D .&lt;/li&gt;
&lt;li&gt;Its morphisms hom(D&lt;sup&gt;C&lt;/sup&gt;) are the natural transformations between those functors.&lt;/li&gt;
&lt;li&gt;The composition of natural transformations &amp;alpha;: F &amp;rArr; G and &amp;beta;: G &amp;rArr; H, is natural transformations (&amp;beta; ∘ &amp;alpha;): F &amp;rArr; H.&lt;/li&gt;
&lt;li&gt;The identity natural transformation id&lt;sub&gt;F&lt;/sub&gt;: F &amp;rArr; F maps each functor to itself&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_thumb1_2.png"&gt;&lt;img width="640" height="576" title="image_thumb1" style="margin: 0px; border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image_thumb1" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image_thumb1_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Regarding the category laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Associativity law: As fore mentioned, natural transformation&amp;rsquo;s components are morphisms in D, so natural transformation composition in D&lt;sup&gt;C&lt;/sup&gt; can be viewed as morphism composition in D: (&amp;beta; ∘ &amp;alpha;)&lt;sub&gt;X&lt;/sub&gt;: F(X) &amp;rarr; H(X) = (&amp;beta;&lt;sub&gt;X&lt;/sub&gt;: G(X) &amp;rarr; H(X)) ∘ (&amp;alpha;&lt;sub&gt;X&lt;/sub&gt;: F(X) &amp;rarr; G(X)). Natural transformations&amp;rsquo; composition in D&lt;sup&gt;C&lt;/sup&gt; is associative, since all component morphisms&amp;rsquo; composition in D is associative&lt;/li&gt;
&lt;li&gt;Identity law: similarly, identity natural transform&amp;rsquo;s components are the id morphisms id&lt;sub&gt;F(X)&lt;/sub&gt;: F(X) &amp;rarr; F(X) in D. Identity natural transform satisfy identity law, since all its components satisfy identity law.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is an example of natural transformations composition:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// ToFunc: Lazy&amp;lt;T&amp;gt; -&amp;gt; Func&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToFunc&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazy) =&amp;gt; () =&amp;gt; lazy.Value;
&lt;/span&gt;&lt;span style="color: gray;"&gt;#endif

&lt;/span&gt;&lt;span style="color: green;"&gt;// ToOptional: Func&amp;lt;T&amp;gt; -&amp;gt; Optional&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToOptional&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, function()));

&lt;/span&gt;&lt;span style="color: green;"&gt;// ToOptional: Lazy&amp;lt;T&amp;gt; -&amp;gt; Optional&amp;lt;T&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ToOptional&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazy) =&amp;gt;
    &lt;/span&gt;&lt;span style="color: green;"&gt;// new Func&amp;lt;Func&amp;lt;T&amp;gt;, Optional&amp;lt;T&amp;gt;&amp;gt;(ToOptional).o(new Func&amp;lt;Lazy&amp;lt;T&amp;gt;, Func&amp;lt;T&amp;gt;&amp;gt;(ToFunc))(lazy);
    &lt;/span&gt;&lt;span style="color: black;"&gt;lazy.ToFunc().ToOptional();
}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Endofunctor category&lt;/h2&gt;
&lt;p&gt;Given category C, there is a endofunctors category, denoted C&lt;sup&gt;C&lt;/sup&gt;, or End(C), where the objects are the endofunctors from category C to C itself, and the morphisms are the natural transformations between those endofunctors.&lt;/p&gt;
&lt;p&gt;&lt;img width="640" height="589" title="image3_thumb" style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image3_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/public-static-partial-class-Optio.------_E2EC/image3_thumb_thumb.png" border="0" /&gt;&lt;/p&gt;
&lt;p&gt;All the functors in C# are endofunctors from DotNet category to DotNet. They are the objects of endofunctor category DotNet&lt;sup&gt;DotNet&lt;/sup&gt; or End(DotNet).&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sat, 14 Dec 2024 13:10:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-4-natural-transformation</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>Functors</category><category>Natural Transformation</category><category>Categories</category></item><item><title>Category Theory via C# (3) Functor and LINQ to Functors</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-3-functor-and-linq-to-functors</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Functor and functor laws&lt;/h1&gt;
&lt;p&gt;In category theory, &lt;a href="http://en.wikipedia.org/wiki/Functor" target="_blank"&gt;functor&lt;/a&gt; is a &lt;a href="http://en.wikipedia.org/wiki/Map_(mathematics)" target="_blank"&gt;mapping&lt;/a&gt; from category to category. Giving category C and D, functor F from category C to D is a structure-preserving morphism from C to D, denoted F: C &amp;rarr; D:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-3-Functor_DC22/image6_thumb_2.png"&gt;&lt;img width="640" height="654" title="image6_thumb" style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image6_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-3-Functor_DC22/image6_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;F maps objects in C to objects in D, for example, X, Y, Z, &amp;hellip; &amp;isin; ob(C) are mapped to F(X), F(Y), F(Z), &amp;hellip; &amp;isin; in ob(D)&lt;/li&gt;
&lt;li&gt;F also maps morphisms in C to morphisms in D, for example, m: X &amp;rarr; Y &amp;isin; hom(C) is mapped to morphism F(m): F(X) &amp;rarr; F(Y) &amp;isin; hom(D). In this tutorial, to align to C#/.NET terms, this morphism mapping capability of functor is also called &amp;ldquo;select&amp;rdquo;. so F(m) is also denoted Select&lt;sub&gt;F&lt;/sub&gt;(m).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And F must satisfy the following functor laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Composition preservation: F(m&lt;sub&gt;2&lt;/sub&gt; ∘ m&lt;sub&gt;1&lt;/sub&gt;) &amp;equiv; F(m&lt;sub&gt;2&lt;/sub&gt;) ∘ F(m&lt;sub&gt;1&lt;/sub&gt;), or Select&lt;sub&gt;F&lt;/sub&gt;(m&lt;sub&gt;2&lt;/sub&gt; ∘ m&lt;sub&gt;1&lt;/sub&gt;) &amp;equiv; Select&lt;sub&gt;F&lt;/sub&gt;(m&lt;sub&gt;2&lt;/sub&gt;) ∘ Select&lt;sub&gt;F&lt;/sub&gt;(m&lt;sub&gt;1&lt;/sub&gt;), F maps composition in C to composition in D&lt;/li&gt;
&lt;li&gt;Identity preservation: F(id&lt;sub&gt;X&lt;/sub&gt;) &amp;equiv; id&lt;sub&gt;F(X)&lt;/sub&gt;, or Select&lt;sub&gt;F&lt;/sub&gt;(id&lt;sub&gt;X&lt;/sub&gt;) &amp;equiv; id&lt;sub&gt;F(X)&lt;/sub&gt;, F maps each identity morphism in C to identity morphism in D&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-3-Functor_DC22/image3_thumb_2.png"&gt;&lt;img width="640" height="488" title="image3_thumb" style="border: 0px currentcolor; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image3_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-3-Functor_DC22/image3_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Endofunctor&lt;/h2&gt;
&lt;p&gt;When a functor F&amp;rsquo;s source category and target category are the same category C, it is called &lt;a href="http://en.wikipedia.org/wiki/Functor#Examples" target="_blank"&gt;endofunctor&lt;/a&gt;, denoted F: C &amp;rarr; C. In the DotNet category, there are endofunctors mapping objects (types) and morphisms (functions) in DotNet category to other objects and morphisms in itself. In C#, endofunctor in DotNet can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In DotNet category, objects are types, so the functor&amp;rsquo;s type mapping capability is represented by generic type TFunctor&amp;lt;&amp;gt;, which maps type T to another type TFunctor&amp;lt;T&amp;gt;. And in DotNet category, morphisms are functions, so the functor&amp;rsquo;s function mapping capability is represented by the Select method, which maps function of type TSource &amp;ndash;&amp;gt; TResult to another function of type TFunctor&amp;lt;TSource&amp;gt; &amp;ndash;&amp;gt; TFunctor&amp;lt;TResult&amp;gt;.&lt;/p&gt;
&lt;p&gt;Unfortunately, the above interface cannot be compiled, because C#/.NET do not support &lt;a href="http://en.wikipedia.org/wiki/Type_class#Higher-kinded_polymorphism" target="_blank"&gt;higher-kinded polymorphism&lt;/a&gt; for types.&lt;/p&gt;
&lt;h2&gt;Type constructor and higher-kinded type&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Kind_(type_theory)" target="_blank"&gt;Kind&lt;/a&gt; is the meta type of a type:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A concrete type has the simplest kind, denoted *. All non generic types (types without type parameters) are of kind *. Closed generic types (types with concrete type arguments) are also concrete types of kind *.&lt;/li&gt;
&lt;li&gt;An open generic type definition with type parameter can be viewed as a type constructor, which works like a function. For example,&amp;nbsp; IEnumerable&amp;lt;&amp;gt; can accept a type of kind * (like int), and return another closed type of kind * (like IEnumerable&amp;lt;int&amp;gt;), so IEnumerable&amp;lt;&amp;gt; is a type constructor, its kind is denoted * &amp;ndash;&amp;gt; *; ValueTuple&amp;lt;,&amp;gt; can accept 2 types of kind * (like string and bool), and return another closed type of kind * (like ValueTuple&amp;lt;string, bool&amp;gt;) so ValueTuple&amp;lt;,&amp;gt; is a type constructor, its kind is denoted (*, *) &amp;ndash;&amp;gt; *, or * &amp;ndash;&amp;gt; * &amp;ndash;&amp;gt; * in curried style.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In above IFunctor&amp;lt;TFunctor&amp;lt;&amp;gt;&amp;gt; generic type definition, its type parameter TFunctor&amp;lt;&amp;gt; is an open generic type of kind * &amp;ndash;&amp;gt; *. As a result, IFunctor&amp;lt;TFunctor&amp;lt;&amp;gt;&amp;gt; can be viewed as a type constructor, which works like a higher-order function, accepting a TFunctor&amp;lt;&amp;gt; type constructor of kind * &amp;ndash;&amp;gt; *, and returning a concrete type of kind *. So&amp;nbsp; IFunctor&amp;lt;TFunctor&amp;lt;&amp;gt;&amp;gt; is of kind (* &amp;ndash;&amp;gt; *) &amp;ndash;&amp;gt; *. This is called a higher-kinded type, and not supported by .NET and C# compiler. In another word, C# generic type definition does not support its type parameter to have type parameters. In C#, functor support is implemented by LINQ query comprehensions instead of type system.&lt;/p&gt;
&lt;h1&gt;LINQ to Functors&lt;/h1&gt;
&lt;h2&gt;Built-in IEnumerable&amp;lt;&amp;gt; functor&lt;/h2&gt;
&lt;p&gt;IEnumerable&amp;lt;&amp;gt; is a built-in functor of DotNet category, which can be viewed as virtually implementing above IFunctor&amp;lt;TFunctor&amp;lt;&amp;gt;&amp;gt; interface:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;IEnumerable&amp;lt;TSource&amp;gt;, IEnumerable&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);

    // Other members.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Endofunctor IEnumerable&amp;lt;&amp;gt; in DotNet category maps each T object (type) to IEnumerable&amp;lt;T&amp;gt; object (type), and its Select method maps TSource&amp;rarr; TResult morphism (function) to IEnumerable&amp;lt;TSource&amp;gt; &amp;rarr; IEnumerable&amp;lt;TResult&amp;gt; morphism (function). So its Select method is of type (TSource &amp;ndash;&amp;gt; TResult) &amp;ndash;&amp;gt; (IEnumerable&amp;lt;TSource&amp;gt; &amp;ndash;&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;), which can be uncurried to (TSource &amp;ndash;&amp;gt; TResult, IEnumerable&amp;lt;TSource&amp;gt;) &amp;ndash;&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;IEnumerable&amp;lt;TSource&amp;gt;, IEnumerable&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);
    // can be equivalently converted to:
    // IEnumerable&amp;lt;TResult&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector, IEnumerable&amp;lt;TSource&amp;gt; source);

    // Other members.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now swap the 2 parameters of the uncurried Select, then its type becomes (IEnumerable&amp;lt;TSource&amp;gt;, TSource &amp;ndash;&amp;gt; TResult) &amp;ndash;&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;IEnumerable&amp;lt;TSource&amp;gt;, IEnumerable&amp;lt;TResult&amp;gt;&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector);
    // can be equivalently converted to:
    // IEnumerable&amp;lt;TResult&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;(IEnumerable&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource, TResult&amp;gt; selector);

    // Other members.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In .NET, this equivalent version of Select is exactly the LINQ query method Select. The following is the comparison of functor Select method and LINQ Select method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableExtensions&lt;span style="color: #2b91af;"&gt; &lt;/span&gt;&lt;span style="color: green;"&gt;// IEnumerable&amp;lt;T&amp;gt; : IFunctor&amp;lt;IEnumerable&amp;lt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (IEnumerable&amp;lt;TSource&amp;gt; -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;).
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt; 
            Select(source, selector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// 1. Uncurry to Select: (TSource -&amp;gt; TResult, IEnumerable&amp;lt;TSource&amp;gt;) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;.
    // 2. Swap 2 parameters to Select: (IEnumerable&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; IEnumerable&amp;lt;TResult&amp;gt;.
    // 3. Define as LINQ extension method.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value);
        }
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the IEnumerable&amp;lt;&amp;gt; functor&amp;rsquo;s morphism mapping capability is implemented as the LINQ mapping query. As a part of the LINQ query expression pattern, functor support is built in in the C# language:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = System.Linq.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="color: black;"&gt;.Range(0, 5);
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map IEnumerable&amp;lt;int&amp;gt; to IEnumerable&amp;lt;string&amp;gt;.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;query.WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the above Select implementation satisfies the functor laws:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// using static Dixin.Linq.CategoryTheory.Functions;
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;FunctorLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new int&lt;/span&gt;&lt;span style="color: black;"&gt;[] { 0, 1, 2, 3, 4 };
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1 = int32 =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Sqrt(int32);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2 = @double =&amp;gt; @double.ToString(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"0.00"&lt;/span&gt;&lt;span style="color: black;"&gt;);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: source.Select(selector2.o(selector1)) == source.Select(selector1).Select(selector2).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector2.o(selector1)(value)).WriteLines();  &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.00 1.00 1.41 1.73 2.00
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector1(value) &lt;/span&gt;&lt;span style="color: blue;"&gt;into &lt;/span&gt;&lt;span style="color: black;"&gt;value
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector2(value)).WriteLines();  &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.00 1.00 1.41 1.73 2.00
    // Identity preservation: source.Select(Id) == Id(source).
    &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
        &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;Id(value)).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
    &lt;/span&gt;&lt;span style="color: black;"&gt;Id(source).WriteLines(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0 1 2 3 4
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Functor pattern of LINQ&lt;/h2&gt;
&lt;p&gt;So LINQ Select mapping query&amp;rsquo;s quintessential mathematics is functor. Generally, in DotNet category, a type is a functor if:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This type is an open generic type definition, which can be viewed as type constructor of kind * &amp;ndash;&amp;gt; *, so that it maps a concrete type T to another concrete functor-wrapped type.&lt;/li&gt;
&lt;li&gt;It is equipped with the standard LINQ query method Select, which can be either instance method or extension method.&lt;/li&gt;
&lt;li&gt;The implementation of Select satisfies the functor laws, so that DotNet category&amp;rsquo;s associativity law and identity law are preserved.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On the other hand, to enable the LINQ functor query expression (single from clauses with select clause) for a type does not require that type to be strictly a functor. This LINQ syntax can be enabled for any generic or non generic type with as long as it has such a Select method, , which can be virtually demonstrated as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
&lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;( &lt;/span&gt;&lt;span style="color: green;"&gt;// Non generic TFunctor can work too.
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; functor, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&amp;gt; : &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IFunctor&lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&amp;lt;&amp;gt;&amp;gt;
{
    &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TFunctor&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: green;"&gt;/* TSource */ &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: green;"&gt;/* TFunctor&amp;lt;TSource&amp;gt; */ &lt;/span&gt;&lt;span style="color: black;"&gt;functor
                              &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select&lt;/span&gt; &lt;/span&gt;&lt;span style="color: green;"&gt;/* TResult */ &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;More LINQ to Functors&lt;/h1&gt;
&lt;p&gt;Many other open generic type definitions provided by .NET can be functor. Take Lazy&amp;lt;&amp;gt; as example, first, apparently it is a type constructor of kind * &amp;ndash;&amp;gt; *. Then, its Select query method can be defined as extension method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LazyExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Lazy&amp;lt;T&amp;gt; : IFunctor&amp;lt;Lazy&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Lazy&amp;lt;TSource&amp;gt; -&amp;gt; Lazy&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ Select: (Lazy&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Lazy&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; selector(source.Value));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; 1);
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Lazy&amp;lt;int&amp;gt; to Lazy&amp;lt;string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                             &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Value; &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Func&amp;lt;&amp;gt; with 1 type parameter is also a functor with the following Select implementation:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T&amp;gt; : IFunctor&amp;lt;Func&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Func&amp;lt;TSource&amp;gt; -&amp;gt; Func&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ Select: (Func&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Func&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            () =&amp;gt; selector(source());

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = () =&amp;gt; 1;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Func&amp;lt;int&amp;gt; to Func&amp;lt;string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                             &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here Select maps TSource &amp;ndash;&amp;gt; TResult function to Func&amp;lt;TSource&amp;gt; &amp;ndash;&amp;gt; Func&amp;lt;TResult&amp;gt; function, which is straightforward. The other Func generic delegate types, like Func&amp;lt;,&amp;gt; with 2 type parameters, could be more interesting. Just like fore mentioned ValueTuple&amp;lt;,&amp;gt;, Func&amp;lt;,&amp;gt; is of kind * &amp;ndash;&amp;gt; * &amp;ndash;&amp;gt; *, and can be viewed as a type constructor accepting 2 concrete types and returning another concrete type, which is different from functor. However, if Func&amp;lt;,&amp;gt; already has a concrete type T as its first type parameter, then Func&amp;lt;T,&amp;gt; can be viewed as a partially applied type constructor of kind * &amp;ndash;&amp;gt; *, which can map one concrete type (its second type parameter) to another concrete type. So that Func&amp;lt;T,&amp;gt; is also a functor, with the following Select method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FuncExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Func&amp;lt;T, TResult&amp;gt; : IFunctor&amp;lt;Func&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Func&amp;lt;T, TSource&amp;gt; -&amp;gt; Func&amp;lt;T, TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ Select: (Func&amp;lt;T, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Func&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            value =&amp;gt; selector(source(value)); &lt;/span&gt;&lt;span style="color: green;"&gt;// selector.o(source);
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This time Select maps TSource &amp;ndash;&amp;gt; TResult function to Func&amp;lt;T, TSource&amp;gt; &amp;ndash;&amp;gt; Func&amp;lt;T, TResult&amp;gt; function. Actually, Func&amp;lt;T,&amp;gt; functor&amp;rsquo;s Select is exactly the function composition:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;input)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = value =&amp;gt; value.ToString();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map string to bool.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNullOrWhiteSpace;
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Func&amp;lt;T, string&amp;gt; to Func&amp;lt;T, bool&amp;gt;.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                          &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;result = query(input); &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.

    // Equivalent to:
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function1 = source;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function2 = selector&lt;/span&gt;&lt;span style="color: black;"&gt;;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; composition = function2.o(function1);
    result = composition(input);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;ValueTuple&amp;lt;&amp;gt; with 1 type parameter simply wraps a value. It is the eager version of Lazy&amp;lt;&amp;gt;, and it is also functor, with the following Select method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T&amp;gt; : IFunctor&amp;lt;ValueTuple&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (ValueTuple&amp;lt;TSource&amp;gt; -&amp;gt; ValueTuple&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // LINQ Select: (ValueTuple&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; ValueTuple&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(selector(source.Item1)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Unlike all the previous Select, here ValueTuple&amp;lt;&amp;gt;&amp;rsquo;s Select query method cannot implement deferred execution. To construct a ValueTuple&amp;lt;TResult&amp;gt; instance and return, selector must be called immediately to evaluate the result value.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(1);
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = int32 =&amp;gt;
        {
            &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(selector)} &lt;/span&gt;&lt;span style="color: #a31515;"&gt;is called with &lt;/span&gt;&lt;span style="color: black;"&gt;{int32}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;."&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine();
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString(int32);
        };
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Map ValueTuple&amp;lt;int&amp;gt; to ValueTuple&amp;lt;string&amp;gt;.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// selector is called with 1.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Item1; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to Func&amp;lt;T,&amp;gt;, ValueTuple&amp;lt;T,&amp;gt; is also functor, with the following Select method of immediate execution:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ValueTupleExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// ValueTuple&amp;lt;T, T2&amp;gt; : IFunctor&amp;lt;ValueTuple&amp;lt;T,&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (ValueTuple&amp;lt;T, TSource&amp;gt; -&amp;gt; ValueTuple&amp;lt;T, TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;), (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;(source, selector); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // LINQ Select: (ValueTuple&amp;lt;T, TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; ValueTuple&amp;lt;T, TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;) Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;) source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            (source.Item1, selector(source.Item2)); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;item1)
    {
        (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;) source = (item1, 1);
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = int32 =&amp;gt;
        {
            &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(selector)} &lt;/span&gt;&lt;span style="color: #a31515;"&gt;is called with &lt;/span&gt;&lt;span style="color: black;"&gt;{int32}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;."&lt;/span&gt;&lt;span style="color: black;"&gt;.WriteLine();
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString(int32);
        };
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map ValueTuple&amp;lt;T, int&amp;gt; to ValueTuple&amp;lt;T, string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;) query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
                            &lt;span style="color: green;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// selector is called with 1.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = query.Item2; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Task is also an example of functor, with the following Select method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TaskExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Task&amp;lt;T&amp;gt; : IFunctor&amp;lt;Task&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Task&amp;lt;TSource&amp;gt; -&amp;gt; Task&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.

    // LINQ Select: (Task&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Task&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static async &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            selector(&lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;source); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution, impure.

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static async &lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;MapAsync()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source = System.Threading.Tasks.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;.FromResult(1);
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Task&amp;lt;int&amp;gt; to Task&amp;lt;string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Task&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source
                             &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result = &lt;/span&gt;&lt;span style="color: blue;"&gt;await &lt;/span&gt;&lt;span style="color: black;"&gt;query; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to ValueTuple&amp;lt;&amp;gt;, above Select implementation is not deferred either. When Select is called, if the source task is already completed, the selector function is called immediately. And unlike all the previous Select methods are pure (referential transparent and side effect free), this Select use the await syntactic sugar to construct a state machine and start it immediately. So it changes state and is impure.&lt;/p&gt;
&lt;p&gt;Nullable&amp;lt;&amp;gt; is also an interesting type. It is of kind * &amp;ndash;&amp;gt; * and the following Select method can be defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NullableExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Nullable&amp;lt;T&amp;gt; : IFunctor&amp;lt;Nullable&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Nullable&amp;lt;TSource&amp;gt; -&amp;gt; Nullable&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;?, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;?&amp;gt; Select2&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: blue;"&gt;struct where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: blue;"&gt;struct &lt;/span&gt;&lt;span style="color: black;"&gt;=&amp;gt; source =&amp;gt;
            Select(source, selector); &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    // LINQ Select: (Nullable&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Nullable&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;? Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;? source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: blue;"&gt;struct where &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: blue;"&gt;struct &lt;/span&gt;&lt;span style="color: black;"&gt;=&amp;gt;
            source.HasValue ? selector(source.Value) : &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: green;"&gt;// Immediate execution.

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;? source1 = 1L;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TimeSpan&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TimeSpan&lt;/span&gt;&lt;span style="color: black;"&gt;.FromTicks;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Nullable&amp;lt;int&amp;gt; to Nullable&amp;lt;TimeSpan&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TimeSpan&lt;/span&gt;&lt;span style="color: black;"&gt;? query1 = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1
                           &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TimeSpan &lt;/span&gt;&lt;span style="color: black;"&gt;result1 = query1.Value; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.

        &lt;/span&gt;&lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt;? source2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Nullable&amp;lt;int&amp;gt; to Nullable&amp;lt;TimeSpan&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TimeSpan&lt;/span&gt;&lt;span style="color: black;"&gt;? query2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2
                           &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define and execute query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;result2 = query2.HasValue; &lt;/span&gt;&lt;span style="color: green;"&gt;// Query result.
    &lt;/span&gt;&lt;span style="color: black;"&gt;}
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In the above Select method, if the source Nullable&amp;lt;TSource&amp;gt; instance represents an actual value of TSource, that value is extracted to call selector, and the result is wrapped into another Nullable&amp;lt;TResult&amp;gt; instance to return; if the source represents null, selector is not called, and a Nullable&amp;lt;TResult&amp;gt; instance representing null is directly returned. There are 2 issues here. First, Nullable&amp;lt;&amp;gt;&amp;rsquo;s type parameter is constrained to be structures, so it can only map some objects of DotNet category (the value types). Second, the Select implementation cannot be deferred. As LINQ query method, deferred execution is always preferred whenever possible. So the following Optional&amp;lt;T&amp;gt; type can be defined to be used with any type parameter, and also be lazy:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public readonly struct &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; factory;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: black;"&gt;Optional(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt; factory = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory = factory == &lt;/span&gt;&lt;span style="color: blue;"&gt;null &lt;/span&gt;&lt;span style="color: black;"&gt;? &lt;/span&gt;&lt;span style="color: blue;"&gt;null &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;)&amp;gt;(factory);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public bool &lt;/span&gt;&lt;span style="color: black;"&gt;HasValue =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory?.Value.Item1 ?? &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;Value
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;get
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(!&lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.HasValue)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;InvalidOperationException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;)} &lt;/span&gt;&lt;span style="color: #a31515;"&gt;object must have a value."&lt;/span&gt;&lt;span style="color: black;"&gt;);
            }
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return this&lt;/span&gt;&lt;span style="color: black;"&gt;.factory.Value.Item2;
        }
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Optional&amp;lt;T&amp;gt; is still a structure just like Nullable&amp;lt;T&amp;gt;, so its instance cannot be null. Its parameter is not constrained, so it can wrap any valid or invalid value of any type, Its constructor accepts a factory function just like Lazy&amp;lt;&amp;gt;, s the evaluation of its wrapped value can be deferred. And the factory function returns a tuple of bool value and T value, where the bool value indicates if the other T value is a valid value, and that bool value can be returned by the the HasValue property.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Optional()
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32 = 1;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; nullableInt32 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(int32);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; nullableFunction = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(function); &lt;/span&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; nullableString = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Nullable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Cannot be compiled.

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; optionalInt32 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, int32));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; optionalFunction = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, function));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; optionalString = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(); &lt;/span&gt;&lt;span style="color: green;"&gt;// Equivalent to: new Optional&amp;lt;string&amp;gt;(() =&amp;gt; false, default);
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Apparently, Optional&amp;lt;&amp;gt; is a factor, and its Select can be defined with deferred execution:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;OptionalExtensions &lt;/span&gt;&lt;span style="color: green;"&gt;// Optional&amp;lt;T&amp;gt; : IFunctor&amp;lt;Optional&amp;lt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Functor Select: (TSource -&amp;gt; TResult) -&amp;gt; (Optional&amp;lt;TSource&amp;gt; -&amp;gt; Optional&amp;lt;TResult&amp;gt;)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt; source =&amp;gt;
            Select(source, selector);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// LINQ Select: (Optional&amp;lt;TSource&amp;gt;, TSource -&amp;gt; TResult) -&amp;gt; Optional&amp;lt;TResult&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; source.HasValue
                ? (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, selector(source.Value)) : (&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Map()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source1 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;, 1));
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map int to string.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Optional&amp;lt;int&amp;gt; to Optional&amp;lt;string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query1 = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source1
                                    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(query1.HasValue) &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result1 = query1.Value;
        }

        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;();
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Map Optional&amp;lt;int&amp;gt; to Optional&amp;lt;string&amp;gt;.
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Optional&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; query2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;from &lt;/span&gt;&lt;span style="color: black;"&gt;value &lt;/span&gt;&lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: black;"&gt;source2
                                    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;&lt;span style="color: black;"&gt;selector(value); &lt;/span&gt;&lt;span style="color: green;"&gt;// Define query.
        &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(query2.HasValue) &lt;/span&gt;&lt;span style="color: green;"&gt;// Execute query.
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;result2 = query2.Value;
        }
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is easy to verify all the above Select methods satisfy the functor laws. However, not any Select can automatically satisfy the functor laws. The following is a different Select implementation for Lazy&amp;lt;&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Select&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
    &lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;span style="color: blue;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And it breaks the functor because it does not preserve the identity law:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;FunctorLaws()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; lazy = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(() =&amp;gt; 1);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToString;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; selector2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;&lt;span style="color: black;"&gt;.ToDouble;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Associativity preservation: TFunctor&amp;lt;T&amp;gt;.Select(f2.o(f1)) == TFunctor&amp;lt;T&amp;gt;.Select(f1).Select(f2)
    &lt;/span&gt;&lt;span style="color: black;"&gt;lazy.Select(selector2.o(selector1)).Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0
    &lt;/span&gt;&lt;span style="color: black;"&gt;lazy.Select(selector1).Select(selector2).Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0
    // Identity preservation: TFunctor&amp;lt;T&amp;gt;.Select(Id) == Id(TFunctor&amp;lt;T&amp;gt;)
    &lt;/span&gt;&lt;span style="color: black;"&gt;lazy.Select(Id).Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0
    &lt;/span&gt;&lt;span style="color: black;"&gt;Id(lazy).Value.WriteLine(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 1
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Fri, 13 Dec 2024 13:10:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-3-functor-and-linq-to-functors</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>Functors</category></item><item><title>Category Theory via C# (2) Monoid</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-2-monoid</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;Monoid and monoid laws&lt;/h1&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Monoid_(category_theory)" target="_blank"&gt;Monoid&lt;/a&gt; is an important algebraic structure in category theory. A monoid M is a set M equipped with a binary operation ⊙ and a special element I, denoted 3-tuple (M, ⊙, I), where&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;M is a set of elements&lt;/li&gt;
&lt;li&gt;⊙ is a binary operator called multiplication, so that M ⊙ M &amp;rarr; M, which means the multiplication of 2 elements in set M always results an element in set M. This operation is also denoted &amp;mu;, so that &amp;mu;(M, M) &amp;equiv; M ⊙ M.&lt;/li&gt;
&lt;li&gt;I is a special unit element (aka neutral element, or identity element) in set M&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And the binary operator and the unit element must satisfy the following monoid laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;associative law &amp;alpha;&lt;sub&gt;X, Y, Z&lt;/sub&gt;: (X ⊙ Y) ⊙ Z &amp;equiv; X ⊙ (Y ⊙ Z), where X &amp;isin; M, Y &amp;isin; M, Z &amp;isin; M&lt;/li&gt;
&lt;li&gt;left unit law &amp;lambda;&lt;sub&gt;X&lt;/sub&gt;: I ⊙ X &amp;equiv; X, where X &amp;isin; M; and right unit law &amp;rho;&lt;sub&gt;X&lt;/sub&gt;: X &amp;equiv; X ⊙ I, where X &amp;isin; M&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;so that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the triangle identity commutes:&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/LINQ-via-C-Series-C-Functional-Programmi_921F/image_6.png"&gt;&lt;img width="640" height="252" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/LINQ-via-C-Series-C-Functional-Programmi_921F/image_thumb_2.png" border="0" /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;and the pentagon identity commutes::&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/LINQ-via-C-Series-C-Functional-Programmi_921F/Untitled-2.fw_2.png"&gt;&lt;img width="640" height="376" title="Untitled-2.fw" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="Untitled-2.fw" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/LINQ-via-C-Series-C-Functional-Programmi_921F/Untitled-2.fw_thumb.png" border="0" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In C#, the monoid definition can be represented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;static abstract &lt;/span&gt;&lt;span style="color: black;"&gt;T Multiply(T value1, T value2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;static abstract &lt;/span&gt;&lt;span style="color: black;"&gt;T Unit { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;An intuitive example is the set ℤ of all integers, with binary operator + and unit element 0. The addition of 2 integers always results another integer; Also for integers x, y ,z, there is (x + y) + z &amp;equiv; x + (y + z) and 0 + x &amp;equiv; x &amp;equiv; x + 0 (ℤ, +, 0), so that (ℤ, +, 0) is monoid. Apparently (ℤ, *, 1) is monoid too.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Int32SumMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static int &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; value1 + value2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static int &lt;/span&gt;&lt;span style="color: black;"&gt;Unit =&amp;gt; GenericIndirection.AdditiveUnit&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 0.
&lt;/span&gt;&lt;span style="color: black;"&gt;}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Int32ProductMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static int &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; value1 * value2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static int &lt;/span&gt;&lt;span style="color: black;"&gt;Unit =&amp;gt; GenericIndirection.MultiplicativeUnit&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(); &lt;/span&gt;&lt;span style="color: green;"&gt;// 1.
&lt;/span&gt;&lt;span style="color: black;"&gt;}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;GenericIndirection
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;T AdditiveUnit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: black;"&gt;T : IAdditiveIdentity&amp;lt;T, T&amp;gt; =&amp;gt; T.AdditiveIdentity;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;T MultiplicativeUnit&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;() &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: black;"&gt;T : IMultiplicativeIdentity&amp;lt;T, T&amp;gt; =&amp;gt; T.MultiplicativeIdentity;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Another monoid example is (string, string.Concat, string.Empty):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;StringConcatMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Concat(value1, value2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;&lt;span style="color: black;"&gt;Unit =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here string can be viewed as a sequence of char, and the unit string is an empty sequence. Generally, (IEnumerable&amp;lt;T&amp;gt;, Enumerable.Concat&amp;lt;T&amp;gt;, Enumerable.Empty&amp;lt;T&amp;gt;()) is a monoid:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;EnumerableConcatMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : IMonoid&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;T&amp;gt; Multiply(IEnumerable&amp;lt;T&amp;gt; value1, IEnumerable&amp;lt;T&amp;gt; value2) =&amp;gt; value1.Concat(value2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;T&amp;gt; Unit =&amp;gt; Enumerable.Empty&amp;lt;T&amp;gt;();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The set of Boolean values { true, false }, with binary operator &amp;amp;&amp;amp; and unit element true, is a monoid with only 2 element: ({true, false}, &amp;amp;&amp;amp;, true); And so is ({ true, false }, ||, false):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;BooleanAndMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; value1 &amp;amp;&amp;amp; value2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;&lt;span style="color: black;"&gt;Unit =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;BooleanOrMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; value1 || value2;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;&lt;span style="color: black;"&gt;Unit =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;A monoid with 1 single element can be defined with the special type void (System.Void):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;namespace &lt;/span&gt;&lt;span style="color: black;"&gt;System
{&lt;/span&gt;&lt;span style="color: black;"&gt;
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ComVisible&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)]
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Serializable&lt;/span&gt;&lt;span style="color: black;"&gt;]
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;StructLayout&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LayoutKind&lt;/span&gt;&lt;span style="color: black;"&gt;.Sequential, Size = 1)]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public struct &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Void
    &lt;/span&gt;&lt;span style="color: black;"&gt;{
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The set { default(void) } with the following operator and unit is a monoid:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;VoidMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;void &lt;/span&gt;&lt;span style="color: black;"&gt;value1, &lt;/span&gt;&lt;span style="color: blue;"&gt;void &lt;/span&gt;&lt;span style="color: black;"&gt;value2) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;Unit() =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;However, C# compiler does not allow void keyword or System.Void type to be used in this way, so here System.Void can be replaced with its equivalency in F#, the Microsoft.FSharp.Core.Unit type:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;namespace &lt;/span&gt;&lt;span style="color: black;"&gt;Microsoft.FSharp.Core
{&lt;/span&gt;&lt;span style="color: black;"&gt;
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CompilationMapping&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SourceConstructFlags&lt;/span&gt;&lt;span style="color: black;"&gt;.ObjectType)]
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Serializable&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IComparable
    &lt;/span&gt;&lt;span style="color: black;"&gt;{
        &lt;/span&gt;&lt;span style="color: blue;"&gt;internal &lt;/span&gt;&lt;span style="color: black;"&gt;Unit() { }

        &lt;/span&gt;&lt;span style="color: blue;"&gt;public override int &lt;/span&gt;&lt;span style="color: black;"&gt;GetHashCode() =&amp;gt; 0;

        &lt;/span&gt;&lt;span style="color: blue;"&gt;public override bool &lt;/span&gt;&lt;span style="color: black;"&gt;Equals(&lt;/span&gt;&lt;span style="color: blue;"&gt;object &lt;/span&gt;&lt;span style="color: black;"&gt;obj) =&amp;gt; 
            obj == &lt;/span&gt;&lt;span style="color: blue;"&gt;null &lt;/span&gt;&lt;span style="color: black;"&gt;|| &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LanguagePrimitives&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IntrinsicFunctions&lt;/span&gt;&lt;span style="color: black;"&gt;.TypeTestGeneric&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(obj);

        &lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IComparable&lt;/span&gt;&lt;span style="color: black;"&gt;.CompareTo(&lt;/span&gt;&lt;span style="color: blue;"&gt;object &lt;/span&gt;&lt;span style="color: black;"&gt;obj) =&amp;gt; 0;
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With an internal constructor, Unit cannot be instantiated, a Unit variable can only be default(Unit), which is null. So similarly, set { default(Unit) } with the following operator and unit element is monoid:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;UnitMonoid &lt;/span&gt;&lt;span style="color: black;"&gt;: IMonoid&amp;lt;Unit&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Unit Multiply(Unit value1, Unit value2) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Unit Unit =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;default&lt;/span&gt;&lt;span style="color: black;"&gt;;
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Monoid as category&lt;/h1&gt;
&lt;p&gt;An individual monoid (M, ⊙, I) can be a category C with 1 single object M, where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The collection of objects ob(C) is { M }, which means, category C has 1 single object, that is M.&lt;/li&gt;
&lt;li&gt;The collection of orphisms hom(C) is set M itself, which mean, each elements in set M is a morphism in category C.&lt;/li&gt;
&lt;li&gt;The composition operation ∘ of C is ⊙: since each morphism in C is each element in M, the composition of morphisms is just the multiplication of elements.&lt;/li&gt;
&lt;li&gt;The identity morphism id of C is unit element I: the identity morphism in C is the unit element in M&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this way, since M, ⊙, I satisfies the monoid laws, apparently the category laws are satisfied. In C#, this singleton category can be represented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;MonoidCategory&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMonoid&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; : ICategory&amp;lt;Type, T&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: black;"&gt;TMonoid : IMonoid&amp;lt;T&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;Type&amp;gt; Objects { &lt;/span&gt;&lt;span style="color: blue;"&gt;get &lt;/span&gt;&lt;span style="color: black;"&gt;{ &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(TMonoid); } }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;T Compose(T morphism2, T morphism1) =&amp;gt; TMonoid.Multiply(morphism1, morphism2);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;T Id(Type @object) =&amp;gt; TMonoid.Unit;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Thu, 12 Dec 2024 13:09:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-2-monoid</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>Categories</category><category>LINQ via C#</category><category>Monoid`</category></item><item><title>Category Theory via C# (1) Fundamentals</title><link>https://weblogs.asp.net:443/dixin/category-theory-via-csharp-1-fundamentals</link><description>&lt;h1&gt;[&lt;a href="http://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Category%20Theory" target="_blank"&gt;Category Theory via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;Category theory is a theoretical framework to describe abstract structures and relations in mathematics, first introduced by &lt;a href="http://en.wikipedia.org/wiki/Samuel_Eilenberg"&gt;Samuel Eilenberg&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Saunders_Mac_Lane"&gt;Saunders Mac Lane&lt;/a&gt; in 1940s. It examines mathematical concepts and properties in an abstract way, by formalizing them as collections of items and their relations. Category theory is abstract, and called "&lt;a href="http://en.wikipedia.org/wiki/Abstract_nonsense"&gt;general abstract nonsense&lt;/a&gt;" by &lt;a href="https://en.wikipedia.org/wiki/Norman_Steenrod" target="_blank"&gt;Norman Steenrod&lt;/a&gt;; It is also general, therefore widely applied in many areas in mathematics, physics, and computer science, etc. For programming, category theory is the algebraic theory of types and functions, and also the rationale and foundation of LINQ and any functional programming. This chapter discusses category theory and its important concepts, including category, morphism, natural transform, monoid, functor, and monad, etc. These general abstract concepts will be demonstrated with intuitive diagrams and specific C# and LINQ examples. These knowledge also helps building a deep understanding of functional programming in C# or other languages, since any language with types and functions is a category-theoretic structure.&lt;/p&gt;
&lt;h1&gt;Category and category laws&lt;/h1&gt;
&lt;p&gt;In category theory, a &lt;a href="http://en.wikipedia.org/wiki/Category_(mathematics)"&gt;category&lt;/a&gt; C is a &lt;a href="https://en.wikipedia.org/wiki/Algebraic_structure"&gt;algebraic structure&lt;/a&gt; consists of the following 3 kinds of mathematical entities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A collection of objects, denoted ob(C). This is not the &lt;a href="http://en.wikipedia.org/wiki/Object_(computer_science)" target="_blank"&gt;objects&lt;/a&gt; in &lt;a href="http://en.wikipedia.org/wiki/Object-oriented_programming" target="_blank"&gt;object-oriented programming paradigm&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;A collection of morphisms (relations, aka arrows or maps) between objects, denoted hom(C). A morphism m from source object X to target object Y is denoted m: X &amp;rarr; Y.&lt;/li&gt;
&lt;li&gt;A composition operation of morphisms, denoted ∘. For m&lt;sub&gt;1&lt;/sub&gt;: X &amp;rarr; Y and m&lt;sub&gt;2&lt;/sub&gt;: Y &amp;rarr; Z, their composition is also a morphism (m&lt;sub&gt;2&lt;/sub&gt;∘ m&lt;sub&gt;1&lt;/sub&gt;): Y &amp;rarr; Z. Here the name of m&lt;sub&gt;1&lt;/sub&gt; of m&lt;sub&gt;2&lt;/sub&gt; also implies the order. m&lt;sub&gt;2&lt;/sub&gt; ∘ m&lt;sub&gt;1&lt;/sub&gt; can be read as m&lt;sub&gt;2&lt;/sub&gt; after m&lt;sub&gt;1&lt;/sub&gt;.&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_4.png"&gt;&lt;img width="640" height="388" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_thumb_1.png" border="0" /&gt;&lt;/a&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_2.png"&gt;&lt;img width="640" height="238" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And these entities must satisfy the following 2 category laws:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Associative law: the composition of morphisms &lt;a href="http://en.wikipedia.org/wiki/Associativity"&gt;associative&lt;/a&gt;: For m&lt;sub&gt;1&lt;/sub&gt;: W &amp;rarr; X, m&lt;sub&gt;2&lt;/sub&gt;: X &amp;rarr; Y and m&lt;sub&gt;3&lt;/sub&gt;: Y &amp;rarr; Z, there is (m&lt;sub&gt;3&lt;/sub&gt; ∘ m&lt;sub&gt;2&lt;/sub&gt;) ∘ m&lt;sub&gt;1&amp;equiv;&lt;/sub&gt; &amp;equiv; m&lt;sub&gt;3&lt;/sub&gt; ∘ (m&lt;sub&gt;2&lt;/sub&gt; ∘ m&lt;sub&gt;1&lt;/sub&gt;). &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Identity law: for each object X, there is an &lt;a href="http://en.wikipedia.org/wiki/Identity_function"&gt;identity&lt;/a&gt; morphism: id&lt;sub&gt;x&lt;/sub&gt; : X &amp;rarr; X, and identity morphism is neutral for morphism composition. For m: X &amp;rarr; Y, there is id&lt;sub&gt;Y&lt;/sub&gt;&lt;sub&gt; &lt;/sub&gt;∘ m &amp;equiv; m &amp;equiv; m ∘ id&lt;sub&gt;X&lt;/sub&gt;.&lt;br /&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_9.png"&gt;&lt;img width="640" height="221" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_thumb_3.png" border="0" /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To make above abstract definitions intuitive, a category can be represented by the following interface:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ICategory&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TObject&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMorphism&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;static abstract &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;TObject&amp;gt; Objects { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;static abstract &lt;/span&gt;&lt;span style="color: black;"&gt;TMorphism Compose(TMorphism morphism2, TMorphism morphism1);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;static abstract &lt;/span&gt;&lt;span style="color: black;"&gt;TMorphism Id(TObject @object);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;A simple example of category is the category of integers, where the collection of objects are all integers, and the collection of morphisms are &amp;le; (less than or equal to) relations, from an integer either to itself, or to another integer greater than or equal to it, for example: m&lt;sub&gt;1&lt;/sub&gt;: 0 &amp;rarr; 1 (0 &amp;le; 1), m&lt;sub&gt;2&lt;/sub&gt;: 1 &amp;rarr; 10 (1 &amp;le; 10), etc. Regarding the transitivity of inequality, the &amp;le; morphisms can be composed, for example, m&lt;sub&gt;1&lt;/sub&gt;: 0 &amp;rarr; 1 (0 &amp;le; 1) and m&lt;sub&gt;2&lt;/sub&gt;: 1 &amp;rarr; 10 (1 &amp;le; 10) can be composed to another morphism (m&lt;sub&gt;2&lt;/sub&gt; ∘ m&lt;sub&gt;1&lt;/sub&gt;): 0 &amp;rarr; 10 (0 &amp;le; 10).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/248e0c9c0941_E1F4/image_2.png"&gt;&lt;img width="762" height="444" title="image" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/248e0c9c0941_E1F4/image_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/248e0c9c0941_E1F4/image_4.png"&gt;&lt;img width="588" height="185" title="image" style="border: 0px currentcolor; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/248e0c9c0941_E1F4/image_thumb_1.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Apparently, the above composition is associative, foe example: ((1 &amp;le; 10) ∘ (0 &amp;le; 1)) ∘ (-1 &amp;le; 0) &amp;equiv; -1 &amp;le; 10 &amp;equiv; (1 &amp;le; 10) ∘ ((0 &amp;le; 1) ∘ (-1 &amp;le; 0)). And for each integer X, there is an identity morphism id&lt;sub&gt;X&lt;/sub&gt;: X &amp;rarr; X (X &amp;le; X), and (Y &amp;le; Y) ∘ (X &amp;le; Y) &amp;equiv; X &amp;le; Y &amp;equiv; (X &amp;le; Y) ∘ (X &amp;le; X). So the category laws are satisfied. In C#, integer can be represented by int, and the morphism of &amp;le; relation can be represented by a BinaryExpression of node type LessThanOrEqual, so the category can be represented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Int32Category &lt;/span&gt;&lt;span style="color: black;"&gt;: ICategory&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, BinaryExpression&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Objects
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;get
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: blue;"&gt;for &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;int32 = &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;.MinValue; int32 &amp;lt;= &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;.MaxValue; int32++)
            {
                &lt;/span&gt;&lt;span style="color: blue;"&gt;yield return &lt;/span&gt;&lt;span style="color: black;"&gt;int32;
            }
        }
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;BinaryExpression Compose(BinaryExpression morphism2, BinaryExpression morphism1) =&amp;gt;
        Expression.LessThanOrEqual(morphism2.Left, morphism1.Right); &lt;/span&gt;&lt;span style="color: green;"&gt;// (Y &amp;lt;= Z) ∘ (X &amp;lt;= Y) =&amp;gt; X &amp;lt;= Z.

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;BinaryExpression Id(&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;&lt;span style="color: black;"&gt;@object) =&amp;gt;
        Expression.GreaterThanOrEqual(Expression.Constant(@object), Expression.Constant(@object)); &lt;/span&gt;&lt;span style="color: green;"&gt;// X &amp;lt;= X.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;DotNet category&lt;/h1&gt;
&lt;p&gt;.NET can also be viewed as a category of types and functions, called DotNet:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ob(DotNet): the collection of objects in DotNet category are .NET types, like string (System.String), int (System.Int32), bool (System.Boolean), etc.&lt;/li&gt;
&lt;li&gt;hom(DotNet): the collection of morphisms in DotNet category are .NET pure functions between the input type (source object) to the output type (target object), like int.Parse: string &amp;rarr; int, DateTime.IsLeapYear: int &amp;rarr; bool, etc.&lt;/li&gt;
&lt;li&gt;∘: in DotNet category, the composition operation of morphisms is the composition of functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As already discussed in lambda calculus chapter, function composition is associative, and the unit function Id is the identity morphism:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; o&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
        &lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function2, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TMiddle&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; function1) =&amp;gt;
            value =&amp;gt; function2(function1(value));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource &lt;/span&gt;&lt;span style="color: black;"&gt;Id&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;value) =&amp;gt; value;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that the category laws are satisfied.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_11.png"&gt;&lt;img width="640" height="313" title="image" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="image" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Category-Theory-via-C-1-Fundamentals_6A3A/image_thumb_4.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The DotNet category can be represented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;DotNetCategory &lt;/span&gt;&lt;span style="color: black;"&gt;: ICategory&amp;lt;Type, Delegate&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;IEnumerable&amp;lt;Type&amp;gt; Objects =&amp;gt; AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(assembly =&amp;gt; assembly.ExportedTypes);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Delegate Compose(Delegate morphism2, Delegate morphism1) =&amp;gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// return (Func&amp;lt;TSource, TResult&amp;gt;)Functions.Compose&amp;lt;TSource, TMiddle, TResult&amp;gt;(
        //    (Func&amp;lt;TMiddle, TResult&amp;gt;)morphism2, (Func&amp;lt;TSource, TMiddle&amp;gt;)morphism1);
        &lt;/span&gt;&lt;span style="color: black;"&gt;(Delegate)&lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(Tutorial.FuncExtensions).GetMethod(nameof(Tutorial.FuncExtensions.o))
            .MakeGenericMethod( &lt;/span&gt;&lt;span style="color: green;"&gt;// TSource, TMiddle, TResult.
                &lt;/span&gt;&lt;span style="color: black;"&gt;morphism1.Method.GetParameters().Single().ParameterType,
                morphism1.Method.ReturnType,
                morphism2.Method.ReturnType)
            .Invoke(&lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;new object&lt;/span&gt;&lt;span style="color: black;"&gt;[] { morphism2, morphism1 });

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: black;"&gt;Delegate Id(Type @object) =&amp;gt; &lt;/span&gt;&lt;span style="color: green;"&gt;// Functions.Id&amp;lt;TSource&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(Functions).GetMethod(nameof(Functions.Id)).MakeGenericMethod(@object)
            .CreateDelegate(&lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(Func&amp;lt;,&amp;gt;).MakeGenericType(@object, @object));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In DotNet category, each object is a type represented by System.Type, so Objects method queries all available types in current assembly, and also recursively query all available assemblies in all reference assemblies. And each morphism is a function from one type to another, which can be represented by System.Delegate, so the composition is just to call the o operator with 2 Delegate instances.&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sun, 01 Dec 2024 13:08:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/category-theory-via-csharp-1-fundamentals</guid><category>C#</category><category>.NET</category><category>Functional Programming</category><category>LINQ</category><category>Category Theory</category><category>Categories</category><category>LINQ via C#</category></item><item><title>Lambda Calculus via C# (8) Undecidability of Equivalence</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-c-sharp-24-undecidability-of-equivalence</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;All the previous parts demonstrated what &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus" target="_blank"&gt;lambda calculus&lt;/a&gt; can do &amp;ndash; defining functions to model the computing, applying functions to execute the computing, implementing recursion, encoding data types and data structures, etc. Lambda calculus is a powerful tool, and it is Turing complete. This part discuss some interesting problem that cannot be done with lambda calculus &amp;ndash; asserting whether 2 lambda expressions are equivalent.&lt;/p&gt;
&lt;p&gt;Assuming f&lt;sub&gt;1&lt;/sub&gt; and f&lt;sub&gt;2&lt;/sub&gt; are 2 functions, they are equivalent if for &amp;forall;x, there is f&lt;sub&gt;1&lt;/sub&gt; x &amp;equiv; f&lt;sub&gt;2&lt;/sub&gt; x. For example, the following 2 functions can &lt;a href="http://weblogs.asp.net/dixin/lambda-calculus-via-c-sharp-2-fundamentals-lambda-expression-variables-reductions"&gt;alpha-convert&lt;/a&gt; to each other:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;f1 := &amp;lambda;x.Add x 1
f2 := &amp;lambda;y.Add y 1&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Apparently they are equivalent. And they are both equivalent to:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;f3 := &amp;lambda;x.Add 1 x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;because Add is commutative. &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus#Undecidability_of_equivalence" target="_blank"&gt;Undecidability of equivalence&lt;/a&gt; means, in lambda calculus, there is no function can takes 2 lambda expressions as input, and returns True/False to indicate whether those 2 lambda expressions are equivalent or not. &lt;a href="http://www.joachim-breitner.de/various/ChurchTalk2011.pdf" target="_blank"&gt;Alonzo Church has a proof&lt;/a&gt; using &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus#Normal_forms_and_confluence" target="_blank"&gt;normal form&lt;/a&gt;. An intuitive proof can be done by viewing equivalence problem as another version of &lt;a href="http://en.wikipedia.org/wiki/Halting_problem" target="_blank"&gt;halting problem&lt;/a&gt;. Actually, Alonzo Church&amp;rsquo;s publish on equivalence is earlier (April 1936) than Alan Turing&amp;rsquo;s publish on halting problem (May 1936). To make it simple, this part discusses the undecidability of halting problem first, then discuss the undecidability of equivalence.&lt;/p&gt;
&lt;h1&gt;Halting problem&lt;/h1&gt;
&lt;p&gt;The halting problem is the problem of determining, when running an arbitrary program with an input, whether the program halts (finish running) or does not halt (run forever). For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Function Increase halts (finish running) with argument x, and returns x + 1.&lt;/li&gt;
&lt;li&gt;Function &lt;a href="http://weblogs.asp.net/dixin/lambda-calculus-via-c-sharp-20-combinators"&gt;&amp;omega; does not halt with argument &amp;omega;&lt;/a&gt;, &lt;a href="http://weblogs.asp.net/dixin/lambda-calculus-via-c-sharp-20-combinators"&gt;&amp;Omega; := &amp;omega; &amp;omega; reduces (runs) forever&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;No general algorithm can solve the halting problem for all possible program-input pairs. To prove this, first define a simple function Sequence.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Sequence := &amp;lambda;a.&amp;lambda;b.b&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When applying Sequence, the &lt;a href="http://en.wikipedia.org/wiki/Lambda_calculus#Reduction_strategies" target="_blank"&gt;reduction strategy&lt;/a&gt; matters. In normal order, both its first argument is never reduced. In this part, applicative order is always assumed - the same reduction strategy as C#. So Sequence can be viewed as - reduce (run) a then reduce (run) b sequentially, and return the reduction result of b. When applying Sequence with &amp;Omega; and another lambda expression. It reduces forever in applicative order:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Sequence &amp;Omega; x
&amp;equiv; Sequence (&amp;omega; &amp;omega;) x
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) x
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) x
&amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Because &amp;Omega; does not halt, Sequence &amp;Omega; does not halt either. In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        Sequence = value1 =&amp;gt; value2 =&amp;gt; value2;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Assume an IsHalting function exists, which takes 2 parameters f and x, and returns True/False if function f halts/does not halt with parameter x:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;IsHalting := &amp;lambda;f.&amp;lambda;x.If (/* f halts with x */) (&amp;lambda;x.True) (&amp;lambda;x.False)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then an IsNotHalting function can be defined to test whether function f does not halt with argument f (itself):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;IsNotHalting := &amp;lambda;f.If (IsHalting f f) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When a certain function f does not halt with itself, by definition IsNotHalting f returns True:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNotHalting f
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;IsHalting f f&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True))
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;False&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True))
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Remember the If function is lazy, here &amp;lambda;x.Sequence &amp;Omega; False is never reduced. When f halts with itself, the application reduces to Sequence &amp;Omega; False:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNotHalting f
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;IsHalting f f&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True))
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;True&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True))
&amp;equiv; &lt;span style="text-decoration: underline;"&gt;Sequence &amp;Omega;&lt;/span&gt; False
&amp;equiv; Sequence (&amp;omega; &amp;omega;) False
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) False
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) False
&amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As fore mentioned, Sequence &amp;Omega; does not halt. So in this case, IsNotHalting f never returns False.&lt;/p&gt;
&lt;p&gt;In C# IsHalting and IsNotHalting functions can be represented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Halting&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// IsHalting = f =&amp;gt; x =&amp;gt; True if f halts with x; otherwise, False
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static readonly &lt;/span&gt;&lt;span style="color: black;"&gt;Func&amp;lt;Func&amp;lt;T, TResult&amp;gt;, Func&amp;lt;T, Boolean&amp;gt;&amp;gt;
        IsHalting = f =&amp;gt; x =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #008dd9;"&gt;NotImplementedException&lt;/span&gt;&lt;span style="color: black;"&gt;();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// IsNotHalting = f =&amp;gt; If(IsHalting(f)(f))(_ =&amp;gt; Sequence(&amp;Omega;)(False))(_ =&amp;gt; True)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static readonly &lt;/span&gt;&lt;span style="color: black;"&gt;Func&amp;lt;SelfApplicableFunc&amp;lt;TResult&amp;gt;, Boolean&amp;gt;
        IsNotHalting = f =&amp;gt;
            If(Halting&amp;lt;SelfApplicableFunc&amp;lt;TResult&amp;gt;, TResult&amp;gt;.IsHalting(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: black;"&gt;Func&amp;lt;SelfApplicableFunc&amp;lt;TResult&amp;gt;, TResult&amp;gt;(f))(f))
                (_ =&amp;gt; Functions&amp;lt;TResult, Boolean&amp;gt;.Sequence(OmegaCombinators&amp;lt;TResult&amp;gt;.&amp;Omega;)(False))
                (_ =&amp;gt; True);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here since f can be applied with itself, it is represented with the SelfApplicableFunc&amp;lt;TResult&amp;gt; function type.&lt;/p&gt;
&lt;p&gt;It is interesting when IsNotHalting is applied with argument IsNotHalting (itself). Assume IsNotHalting halts with IsNotHalting, in another word:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsHalting IsNotHalting IsNotHalting
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;then there is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNotHalting IsNotHalting
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;IsHalting IsNotHalting IsNotHalting&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True)
&amp;equiv; If (&lt;span style="text-decoration: underline;"&gt;True&lt;/span&gt;) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True)
&amp;equiv; &lt;span style="text-decoration: underline;"&gt;Sequence &amp;Omega;&lt;/span&gt; False
&amp;equiv; Sequence (&amp;omega; &amp;omega;) False
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) False
&amp;equiv; Sequence ((&amp;lambda;x.x x) (&amp;lambda;x.x x)) False
&amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So IsNotHalting IsNotHalting is reduced to Sequence &amp;Omega; False, and is then reduced forever, which means actually IsNotHalting does not halt with IsNotHalting.&lt;/p&gt;
&lt;p&gt;On the other hand, Assume IsNotHalting does not halt with IsNotHalting, in another word:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsHalting IsNotHalting IsNotHalting
&amp;equiv; False&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;then there is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNotHalting IsNotHalting
&amp;equiv; If (IsHalting IsNotHalting IsNotHalting) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True)
&amp;equiv; If (False) (&amp;lambda;x.Sequence &amp;Omega; False) (&amp;lambda;x.True)
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So IsNotHalting IsNotHalting is reduced to True, which means IsNotHalting halts with IsNotHalting.&lt;/p&gt;
&lt;p&gt;Therefore, if IsHalting exists, it leads to IsNotHalting with the following properties:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If IsNotHalting halts with IsNotHalting, then IsNotHalting does not halt with IsNotHalting&lt;/li&gt;
&lt;li&gt;If IsNotHalting does not halt with IsNotHalting, then IsNotHalting halts with IsNotHalting.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This proves IsNotHalting and IsHalting cannot exist.&lt;/p&gt;
&lt;h1&gt;Equivalence problem&lt;/h1&gt;
&lt;p&gt;After understanding the halting problem, the equivalence problem becomes very easy to prove. Assume an AreEquivalent function exists:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;AreEquivalent := &amp;lambda;a.&amp;lambda;b.If (/* a and b are equivalent */) (&amp;lambda;x.True) (&amp;lambda;x.False)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;which takes 2 lambda expression as parameter, and returns True/False if they are/are not equivalent. Now define the following 2 functions:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;GetTrue1 := &amp;lambda;f.&amp;lambda;x.&amp;lambda;y.Sequence (f x) True
GetTrue2 := &amp;lambda;f.&amp;lambda;x.&amp;lambda;y.True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Given arbitrary function f and its argument x:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  GetTrue1 f x
&amp;equiv; &amp;lambda;y.Sequence (f x) True

  GetTrue2 f x
&amp;equiv; &amp;lambda;y.True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;For specified f and x:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if f halts with x, then, &amp;forall;y, (GetTrue1 f x y) and (GetTrue2 f x y) both always returns True. That is, partially applied functions GetTrue1 f x and GetTrue2 f x are equivalent.&lt;/li&gt;
&lt;li&gt;if f does not halt with x, then, &amp;forall;y, (GetTrue1 f x y) never returns True, and (GetTrue2 f x y) always returns True. That is, partially applied functions (GetTrue1 f x) and (GetTrue2 f x) are not equivalent.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now halting problem and equivalence problem are connected. IsHalting function can be directly defined by AreEquivalent function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;IsHalting := &amp;lambda;f.&amp;lambda;x.AreEquivalent (GetTrue1 f x) (GetTrue2 f x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The partial application (GetTrue1 f x) and (GetTrue2 f x) can be substituted as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;IsHalting := &amp;lambda;f.&amp;lambda;x.AreEquivalent (&amp;lambda;y.Sequence (f x) True) (&amp;lambda;y.True)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Equivalence&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// IsEquivalent = f1 =&amp;gt; f2 =&amp;gt; True if f1 and f2 are equivalent; otherwise, False
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static readonly &lt;/span&gt;&lt;span style="color: black;"&gt;Func&amp;lt;Func&amp;lt;T, TResult&amp;gt;, Func&amp;lt;Func&amp;lt;T, TResult&amp;gt;, Boolean&amp;gt;&amp;gt;
        IsEquivalent = f1 =&amp;gt; f2 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #008dd9;"&gt;NotImplementedException&lt;/span&gt;&lt;span style="color: black;"&gt;();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// IsHalting = f =&amp;gt; x =&amp;gt; IsEquivalent(_ =&amp;gt; Sequence(f(x))(True))(_ =&amp;gt; True)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal static readonly &lt;/span&gt;&lt;span style="color: black;"&gt;Func&amp;lt;Func&amp;lt;T, TResult&amp;gt;, Func&amp;lt;T, Boolean&amp;gt;&amp;gt;
        IsHalting = f =&amp;gt; x =&amp;gt; Equivalence&amp;lt;T, Boolean&amp;gt;.IsEquivalent(_ =&amp;gt; Functions&amp;lt;TResult, Boolean&amp;gt;.Sequence(f(x))(True))(_ =&amp;gt; True);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;If the above AreEquivalent function can be defined, then IsHalting can be defined. It is already approved that IsHalting cannot exist, so AreEquivalent cannot exist either. This demonstrates equivalence problem is just another version of halting problem. So, lambda expressions&amp;rsquo; equivalence is undecidable. The undecidability is actually a very general topic in computability theory and mathematical logic. The undecidability of halting problem and lambda calculus&amp;rsquo; undecidability of equivalence are examples of &lt;a href="http://en.wikipedia.org/wiki/Rice%27s_theorem" target="_blank"&gt;Rice's theorem&lt;/a&gt;, and also examples of &lt;a href="http://en.wikipedia.org/wiki/Kurt_G%C3%B6del" target="_blank"&gt;Kurt G&amp;ouml;del&lt;/a&gt;'s &lt;a href="http://en.wikipedia.org/wiki/G%C3%B6del's_incompleteness_theorems" target="_blank"&gt;incompleteness theorems&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Thu, 28 Nov 2024 13:07:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-c-sharp-24-undecidability-of-equivalence</guid><category>LINQ via C# Series</category><category>C#</category><category>C# 3.0</category><category>Lambda Calculus</category><category>Functional Programming</category></item><item><title>Lambda Calculus via C# (7) Fixed Point Combinator and Recursion</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-7-fixed-point-combinator-and-recursion</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;p is the &lt;a href="http://en.wikipedia.org/wiki/Fixed_point_(mathematics)" target="_blank"&gt;fixed point&lt;/a&gt; (aka invariant point) of function f &lt;a href="http://en.wikipedia.org/wiki/If_and_only_if" target="_blank"&gt;if and only if&lt;/a&gt;:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;p
&amp;equiv; f p&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Take function Math.Sqrt as example, it has 2 fix point, 0 and 1, so that 0 &amp;equiv; Math.Sqrt(0) and 1 &amp;equiv; Math.Sqrt(1).&lt;/p&gt;
&lt;p&gt;&lt;a href="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Lambda-Calculus-via-C-7-Fixed-Point-Comb_DA7/FixedPoint.fw_thumb2_thumb_2.png"&gt;&lt;img width="600" height="600" title="FixedPoint.fw_thumb2_thumb" style="border: 0px currentcolor; display: inline; background-image: none;" alt="FixedPoint.fw_thumb2_thumb" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/Lambda-Calculus-via-C-7-Fixed-Point-Comb_DA7/FixedPoint.fw_thumb2_thumb_thumb.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The above fixed point definition also leads to infinite substitution:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;p
&amp;equiv; f p
&amp;equiv; f (f p)
&amp;equiv; f (f (f p))
&amp;equiv; ...
&amp;equiv; f (f (f ... (f p) ...))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, the &lt;a href="http://en.wikipedia.org/wiki/Fixed_point_combinator" target="_blank"&gt;fixed point combinator&lt;/a&gt; Y is defined as if Y f is the fixed point of f:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(Y f)
&amp;equiv; f (Y f)
&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Normal order fixed point combinator (Y combinator) and recursion&lt;/h1&gt;
&lt;p&gt;The following &lt;a href="http://en.wikipedia.org/wiki/Fixed-point_combinator#Fixed_point_combinators_in_lambda_calculus" target="_blank"&gt;Y combinator&lt;/a&gt; is an implementation of fixed point combinator, discovered by Haskell Curry:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Y := &amp;lambda;f.(&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is called the normal order fixed point combinator:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Y f
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g))) f
&amp;equiv; (&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g))
&amp;equiv; f ((&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g)))
&amp;equiv; f (Y f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://matt.might.net/articles/compiling-up-to-lambda-calculus/"&gt;&lt;img width="640" height="480" title="y_combinator" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="y_combinator" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/3c3b4cb86227_12489/y_combinator_1.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The following is Y implemented in SKI:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Y := S (K (S I I)) (S (S (K S) K) (K (S I I)))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And just in SK:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Y := S S K (S (K (S S (S (S S K)))) K)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When Y f can also be substituted infinitely:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(Y f)
&amp;equiv; f (Y f)
&amp;equiv; f (f (Y f))
&amp;equiv; f (f (f (Y f)))
&amp;equiv; ...
&amp;equiv; f (f (f ... (f (Y f)) ...))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Knights_of_the_Lambda_Calculus" target="_blank"&gt;&lt;img width="390" height="390" title="390px-Knights_of_the_Lambda_Calculus.svg" style="border-width: 0px; padding-top: 0px; padding-right: 0px; padding-left: 0px; display: inline; background-image: none;" alt="390px-Knights_of_the_Lambda_Calculus.svg" src="https://aspblogs.blob.core.windows.net/media/dixin/Windows-Live-Writer/3c3b4cb86227_12489/390px-Knights_of_the_Lambda_Calculus.svg_3.png" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So Y can be used to implement &lt;a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)" target="_blank"&gt;recursion&lt;/a&gt;. As fore mentioned, in lambda calculus, a function cannot directly apply it self in its body. Take the factorial function as example, the factorial of n is defined recursively:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If n is greater than 0, then factorial of n is the multiplication of n and factorial of n &amp;ndash; 1&lt;/li&gt;
&lt;li&gt;if n is 0, then factorial of n is 1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So naturally:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Factorial := &amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (Factorial (n - 1)))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;However, in lambda calculus the above definition is illegal, because the self reference does not work anonymously:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (? (n - 1)))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now with the power of Y combinator, the recursion can be implemented, but still in the anonymous way. First, in above definition, just pass the reference of itself as an variable/argument:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;If the above function is called FactorialHelper, then the Factorial function can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;FactorialHelper := &amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))
Factorial := Y FactorialHelper&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the recursive Factorial is implemented anonymously:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Factorial
&amp;equiv; Y FactorialHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g))) FactorialHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.f (g g)) (&amp;lambda;g.f (g g))) (&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1))))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When Factorial is applied, according to the definition of Factorial and Y:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Factorial 3
&amp;equiv; Y FactorialHelper 3
&amp;equiv; FactorialHelper (Y FactorialHelper) 3&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here (Y FactorialHelper) can be substituted by Factorial, according to the definition. So FactorialHelper is called with Factorial and n, exactly as expected.&lt;/p&gt;
&lt;p&gt;The normal order Y combinator does not work with applicative order reduction. In applicative order, here FactorialHelper is applied with (Y FactorialHelper), so the right most argument Y FactorialHelper should be reduced first, which leads to infinite reduction:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;FactorialHelper (Y FactorialHelper) 3
&amp;equiv; FactorialHelper (FactorialHelper (Y FactorialHelper)) 3
&amp;equiv; FactorialHelper (FactorialHelper (FactorialHelper (Y FactorialHelper))) 3
&amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The normal order Y combinator only works with normal order. In normal order, here FactorialHelper is applied with (Y FactorialHelper), so the left most function FactorialHelper should be reduced first:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;FactorialHelper (Y FactorialHelper) 3
&amp;equiv; (&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (Y FactorialHelper) 3
&amp;equiv; (&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (Y FactorialHelper (n - 1)))) 3
&amp;equiv; If (3 == 0) (&amp;lambda;x.1) (&amp;lambda;x.3 * (Y FactorialHelper (3 - 1)))
&amp;equiv; If (False) (&amp;lambda;x.1) (&amp;lambda;x.3 * (Y FactorialHelper (3 - 1))
&amp;equiv; 3 * (Y FactorialHelper (3 - 1))
&amp;equiv; 3 * (FactorialHelper (Y FactorialHelper) (3 - 1))
&amp;equiv; 3 * ((&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (Y FactorialHelper) (3 - 1))
&amp;equiv; 3 * ((&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (Y FactorialHelper (n - 1)))) (3 - 1))
&amp;equiv; 3 * (If ((3 - 1) == 0) (&amp;lambda;x.1) (&amp;lambda;x.(3 - 1) * (Y FactorialHelper ((3 - 1) - 1))))
&amp;equiv; 3 * ((3 - 1) * (Y FactorialHelper ((3 - 1) - 1)))
&amp;equiv; 3 * (2 * (Y FactorialHelper ((3 - 1) - 1)))
&amp;equiv; 3 * (2 * (FactorialHelper (Y FactorialHelper) ((3 - 1) - 1)))
&amp;equiv; 3 * (2 * ((&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (Y FactorialHelper) ((3 - 1) - 1)))
&amp;equiv; 3 * (2 * ((&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (Y FactorialHelper (n - 1)))) ((3 - 1) - 1)))
&amp;equiv; 3 * (2 * (If (((3 - 1) - 1) == 0) (&amp;lambda;x.1) (&amp;lambda;x.((3 - 1) - 1) * (Y FactorialHelper (((3 - 1) - 1) - 1)))))
&amp;equiv; 3 * (2 * (((3 - 1) - 1) * (Y FactorialHelper (((3 - 1) - 1) - 1))))
&amp;equiv; 3 * (2 * (1 * (Y FactorialHelper (((3 - 1) - 1) - 1))))
&amp;equiv; 3 * (2 * (1 * (FactorialHelper (Y FactorialHelper) (((3 - 1) - 1) - 1))))
&amp;equiv; 3 * (2 * (1 * ((f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (Y FactorialHelper) (((3 - 1) - 1) - 1))))
&amp;equiv; 3 * (2 * (1 * ((n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (Y FactorialHelper (n - 1)))) (((3 - 1) - 1) - 1))))
&amp;equiv; 3 * (2 * (1 * (If ((((3 - 1) - 1) - 1) == 0) (&amp;lambda;x.1) (&amp;lambda;x.(((3 - 1) - 1) - 1) * (Y FactorialHelper ((((3 - 1) - 1) - 1) - 1))))))
&amp;equiv; 3 * (2 * (1 * 1))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the Y f infinite reduction is blocked in in normal order reduction. First, Y f is reduced to f (Y f), then the next reduction is to reduce leftmost expression f, not the the rightmost (Y f). In the above example Y FactorialHelper n:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If n is greater than 0, Y Factorial n is reduced to n * (Y Factorial (n - 1)), where Y Factorial can be further reduced, so the recursion continues.&lt;/li&gt;
&lt;li&gt;If n is 0, Y Factorial n is reduced to 1. The reduction ends, so the recursion terminates.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Y combinator is easy to implement in C#. Generally, for a recursive function f of type T -&amp;gt; TResult, its helper function accepts the T -&amp;gt; TResult function and a T value, then return TResult, so its helper function is of type (T -&amp;gt; TResult) &amp;ndash;&amp;gt; T -&amp;gt; TResult. Y can be viewed as accepting helper function and returns f. so Y is of type ((T -&amp;gt; TResult) &amp;ndash;&amp;gt; T -&amp;gt; TResult) -&amp;gt; (T -&amp;gt; TResult). So:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FixedPointCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Y = (g =&amp;gt; f(g(g)))(g =&amp;gt; f(g(g)))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Y = f =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SelfApplicableFunc&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(g =&amp;gt; f(g(g)))(g =&amp;gt; f(g(g)));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here are the types of the elements in above lambda expression:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;g: SelfApplicableFunc&amp;lt;T -&amp;gt; TResult&amp;gt;&lt;/li&gt;
&lt;li&gt;g(g): T -&amp;gt; TResult&lt;/li&gt;
&lt;li&gt;f: (T -&amp;gt; TResult) &amp;ndash;&amp;gt; T -&amp;gt; TResult&lt;/li&gt;
&lt;li&gt;f(g(g)): T =&amp;gt; TResult&lt;/li&gt;
&lt;li&gt;g =&amp;gt; f(g(g)): SelfApplicableFunc&amp;lt;T -&amp;gt; TResult&amp;gt; &amp;ndash;&amp;gt; T -&amp;gt; TResult, which is SelfApplicableFunc&amp;lt;T -&amp;gt; TResult&amp;gt; by definition&lt;/li&gt;
&lt;li&gt;(g =&amp;gt; f(g(g)))(g =&amp;gt; f(g(g))): T -&amp;gt; TResult&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For Factorial, apparently it is of function type Numeral -&amp;gt; Numeral, so FactorialHelper is of function type (Numeral -&amp;gt; Numeral) &amp;ndash;&amp;gt; Numeral -&amp;gt; Numeral:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FixedPointCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;;
&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// FactorialHelper = factorial =&amp;gt; n =&amp;gt; If(n == 0)(_ =&amp;gt; 1)(_ =&amp;gt; n * factorial(n - 1))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        FactorialHelper = factorial =&amp;gt; n =&amp;gt;
            If(n.IsZero())
                (_ =&amp;gt; One)
                (_ =&amp;gt; n.Multiply(factorial(n.Subtract(One))));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Factorial = Y(FactorialHelper);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Calling above Factorial always throws StackOverflowException, because in C# executes in applicative order. When Factorial is called, it calls normal order Y in applicative order, which causes infinite execution.&lt;/p&gt;
&lt;h1&gt;Applicative order fixed point combinator (Z combinator) and recursion&lt;/h1&gt;
&lt;p&gt;The above Y combinator does not work in C#. When reducing Y f in applicative order, the self application in expression f (g g) leads to infinite reduction, which need to be blocked. The solution is to eta convert f (g g) to &amp;lambda;x.f (g g) x. So the applicative order fixed point combinator is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Z := &amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is called Z combinator. Now reduce Z f in applicative order:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Z f
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) f
&amp;equiv; (&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)
&amp;equiv; &amp;lambda;x.f ((&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) x
&amp;equiv; &amp;lambda;x.f (Z f) x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This time Z f is not reduced to f (Z f), but reduced to the eta expanded version &amp;lambda;x.f (Z f) x, so any further reduction is blocked. Still take factorial as example:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Factorial 3
&amp;equiv; Z FactorialHelper 3
&amp;equiv; (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 3
&amp;equiv; FactorialHelper (Z FactorialHelper) 3
&amp;equiv; FactorialHelper (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 3
&amp;equiv; (&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 3
&amp;equiv; (&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1)))) 3
&amp;equiv; If (3 == 0) (&amp;lambda;x.1) (&amp;lambda;x.3 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (3 - 1)))
&amp;equiv; If (False) (&amp;lambda;x.1) (&amp;lambda;x.3 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (3 - 1)))
&amp;equiv; 3 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (3 - 1))
&amp;equiv; 3 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 2)
&amp;equiv; 3 * (FactorialHelper (Z FactorialHelper) 2)
&amp;equiv; 3 * (FactorialHelper (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 2)
&amp;equiv; 3 * ((&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 2)
&amp;equiv; 3 * ((&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1)))) 2)
&amp;equiv; 3 * (If (2 == 0) (&amp;lambda;x.1) (&amp;lambda;x.2 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (2 - 1))))
&amp;equiv; 3 * (If (False) (&amp;lambda;x.1) (&amp;lambda;x.2 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (2 - 1))))
&amp;equiv; 3 * (2 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (2 - 1)))
&amp;equiv; 3 * (2 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 1))
&amp;equiv; 3 * (2 * (FactorialHelper (Z FactorialHelper) 1))
&amp;equiv; 3 * (2 * (FactorialHelper (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 1))
&amp;equiv; 3 * (2 * ((&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 1))
&amp;equiv; 3 * (2 * ((&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1)))) 1))
&amp;equiv; 3 * (2 * (If (1 == 0) (&amp;lambda;x.1) (&amp;lambda;x.1 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (1 - 1)))))
&amp;equiv; 3 * (2 * (If (False) (&amp;lambda;x.1) (&amp;lambda;x.1 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (1 - 1)))))
&amp;equiv; 3 * (2 * (1 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (1 - 1))))
&amp;equiv; 3 * (2 * (1 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 0)))
&amp;equiv; 3 * (2 * (1 * (FactorialHelper (Z FactorialHelper) 0)))
&amp;equiv; 3 * (2 * (1 * (FactorialHelper (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 0)))
&amp;equiv; 3 * (2 * (1 * ((&amp;lambda;f.&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * (f (n - 1)))) (&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) 0)))
&amp;equiv; 3 * (2 * (1 * ((&amp;lambda;n.If (n == 0) (&amp;lambda;x.1) (&amp;lambda;x.n * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1)))) 0)))
&amp;equiv; 3 * (2 * (1 * (If (0 == 0) (&amp;lambda;x.1) (&amp;lambda;x.0 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1))))))
&amp;equiv; 3 * (2 * (1 * (If (True) (&amp;lambda;x.1) (&amp;lambda;x.0 * ((&amp;lambda;x.FactorialHelper (Z FactorialHelper) x) (n - 1))))))
&amp;equiv; 3 * (2 * (1 * 1))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, Z combinator can be implemented in the same pattern. Just eta expand f(g(g)) to x =&amp;gt; f(g(g))(x):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FixedPointCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Z = (g =&amp;gt; x =&amp;gt; f(g(g))(x))(g =&amp;gt; x =&amp;gt; f(g(g))(x))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Z = f =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SelfApplicableFunc&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(g =&amp;gt; x =&amp;gt; f(g(g))(x))(g =&amp;gt; x =&amp;gt; f(g(g))(x));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The types of the elements in above lambda expression are the same as in Y combinator, and x is of type T.&lt;/p&gt;
&lt;p&gt;Now Factorial can be defined with Z and above FactorialHelper:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FixedPointCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, System.&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// DivideByHelper = divideBy =&amp;gt; dividend =&amp;gt; divisor =&amp;gt; If(dividend &amp;gt;= divisor)(_ =&amp;gt; 1 + divideBy(dividend - divisor)(divisor))(_ =&amp;gt; 0)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; DivideByHelper = divideBy =&amp;gt; dividend =&amp;gt; divisor =&amp;gt;
            If(dividend.IsGreaterThanOrEqualTo(divisor))
                (_ =&amp;gt; One.Add(divideBy(dividend.Subtract(divisor))(divisor)))
                (_ =&amp;gt; Zero);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        DivideBy = Z(DivideByHelper);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Another recursion example is &lt;a href="http://en.wikipedia.org/wiki/Fibonacci_number" target="_blank"&gt;Fibonacci&lt;/a&gt; number. The nth Fibonacci number is defined recursively:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if n is greater than 1, then the nth Fibonacci number is the sum of the (n -1)th Fibonacci number and the (n -2)th Fibonacci number.&lt;/li&gt;
&lt;li&gt;if n is 1 or 0, then the nth Fibonacci number is n&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So naturally:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Fibonacci := &amp;lambda;n.If (n &amp;gt; 1) (&amp;lambda;x.(Fibonacci (n - 1)) + (Fibonacci (n - 2))) (&amp;lambda;x.n)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Again, the above recursive definition is illegal in lambda calculus, because the self reference does not work anonymously:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;&amp;lambda;n.If (n &amp;gt; 1) (&amp;lambda;x.(? (n - 1)) + (? (n - 2))) (&amp;lambda;x.n)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Following the same helper function pattern as FactorialHelper, a FibonacciHelper can be defined to pass the Fibonacci function as a variable/argument, then Fibonacci can be defined with Z and FibonacciHelper:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;FibonacciHelper := &amp;lambda;f.&amp;lambda;n.If (n &amp;gt; 1) (&amp;lambda;x.(f (n - 1)) + (f (n - 2))) (&amp;lambda;x.n)
Fibonacci := Z FibonacciHelper&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now Fibonacci is recursive but still can go anonymous, without any self reference:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Fibonacci
&amp;equiv; Z FibonacciHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) FibonacciHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) (&amp;lambda;f.&amp;lambda;n.If (n &amp;gt; 1) (&amp;lambda;x.(f (n - 1)) + (f (n - 2))) (&amp;lambda;x.n))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// FibonacciHelper  = fibonacci  =&amp;gt; n =&amp;gt; If(n &amp;gt; 1)(_ =&amp;gt; fibonacci(n - 1) + fibonacci(n - 2))(_ =&amp;gt; n)
&lt;/span&gt;&lt;span style="color: blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    FibonacciHelper = fibonacci =&amp;gt; n =&amp;gt;
        If(n.IsGreaterThan(One))
            (_ =&amp;gt; fibonacci(n.Subtract(One)).Add(fibonacci(n.Subtract(Two))))
            (_ =&amp;gt; n);

&lt;/span&gt;&lt;span style="color: green;"&gt;// Fibonacci = Z(FibonacciHelper)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
    Fibonacci = Z(FibonacciHelper);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Previously, in the &lt;a href="http://weblogs.asp.net/dixin/lambda-calculus-via-c-sharp-11-predicates-and-divide"&gt;Church numeral arithmetic&lt;/a&gt;, the following illegal DivideBy with self reference was temporarily used:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;DivideBy := &amp;lambda;a.&amp;lambda;b.If (a &amp;gt;= b) (&amp;lambda;x.1 + (DivideBy (a - b) b)) (&amp;lambda;x.0)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Finally, with Z, an legal DivideBy in lambda calculus can be defined, following the same helper function pattern:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;DivideByHelper := &amp;lambda;f.&amp;lambda;a.&amp;lambda;b.If (a &amp;gt;= b) (&amp;lambda;x.1 + (f (a - b) b)) (&amp;lambda;x.0)
DivideBy := Z DivideByHelper&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following is the formal version of DivideBy:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;DivideBy
&amp;equiv; Z DivideByHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) DivideByHelper
&amp;equiv; (&amp;lambda;f.(&amp;lambda;g.&amp;lambda;x.f (g g) x) (&amp;lambda;g.&amp;lambda;x.f (g g) x)) (&amp;lambda;f.&amp;lambda;a.&amp;lambda;b.If (a &amp;gt;= b) (&amp;lambda;x.1 + (f (a - b) b)) (&amp;lambda;x.0))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// DivideByHelper = divideBy =&amp;gt; dividend =&amp;gt; divisor =&amp;gt; If(dividend &amp;gt;= divisor)(_ =&amp;gt; 1 + divideBy(dividend - divisor)(divisor))(_ =&amp;gt; 0)
&lt;/span&gt;&lt;span style="color: blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
    DivideByHelper = divideBy =&amp;gt; dividend =&amp;gt; divisor =&amp;gt;
        If(dividend.IsGreaterThanOrEqualTo(divisor))
            (_ =&amp;gt; One.Add(divideBy(dividend.Subtract(divisor))(divisor)))
            (_ =&amp;gt; Zero);

&lt;/span&gt;&lt;span style="color: green;"&gt;// DivideBy = Z(DivideByHelper)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    DivideBy = Z(DivideByHelper);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following are a few examples&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Factorial(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Factorial(n);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Fibonacci(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Fibonacci(n);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;DivideBy(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;dividend, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;divisor) =&amp;gt; 
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.DivideBy(dividend)(divisor);
}

[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;FixedPointCombinatorTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;FactorialTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; factorial = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: green;"&gt;// Must have to be compiled.
        &lt;/span&gt;&lt;span style="color: black;"&gt;factorial = x =&amp;gt; x == 0 ? 1U : x * factorial(x - 1U);

        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(factorial(0U), 0U.Church().Factorial().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(factorial(1U), 1U.Church().Factorial().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(factorial(2U), 2U.Church().Factorial().Unchurch());&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(factorial(8U), 8U.Church().Factorial().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;FibonacciTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; fibonacci = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: green;"&gt;// Must have. So that fibonacci can recursively refer itself.
        &lt;/span&gt;&lt;span style="color: black;"&gt;fibonacci = x =&amp;gt; x &amp;gt; 1U ? fibonacci(x - 1) + fibonacci(x - 2) : x;

        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(fibonacci(0U), 0U.Church().Fibonacci().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(fibonacci(1U), 1U.Church().Fibonacci().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(fibonacci(2U), 2U.Church().Fibonacci().Unchurch());&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(fibonacci(8U), 8U.Church().Fibonacci().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;DivideByTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U / 1U, 1U.Church().DivideBy(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U / 2U, 1U.Church().DivideBy(2U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U / 2U, 2U.Church().DivideBy(2U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U / 1U, 2U.Church().DivideBy(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(8U / 3U, 8U.Church().DivideBy(3U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U / 8U, 3U.Church().DivideBy(8U.Church()).Unchurch());
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sat, 23 Nov 2024 13:19:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-7-fixed-point-combinator-and-recursion</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Combinators</category><category>Combinatory Logic</category><category>Fixed Point Combinator</category><category>Y Combinator</category></item><item><title>Lambda Calculus via C# (6) Combinatory Logic</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-6-combinatory-logic</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;In lambda calculus, the primitive is function, which can have free variables and bound variables. &lt;a href="http://en.wikipedia.org/wiki/Combinatory_logic" target="_blank"&gt;Combinatory logic&lt;/a&gt; was introduced by &lt;a href="http://en.wikipedia.org/wiki/Moses_Sch%C3%B6nfinkel" target="_blank"&gt;Moses Sch&amp;ouml;nfinkel&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Haskell_Curry" target="_blank"&gt;Haskell Curry&lt;/a&gt; in 1920s. It is equivalent variant lambda calculus, with combinator as primitive. A combinator can be viewed as an expression with no free variables in its body.&lt;/p&gt;
&lt;h1&gt;Combinator&lt;/h1&gt;
&lt;p&gt;The following is the simplest function definition expression, with only bound variable and no free variable:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;I := &amp;lambda;x.x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In combinatory logic it is called I (Id) combinator. The following functions are combinators too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;S := &amp;lambda;x.&amp;lambda;y.&amp;lambda;z.x z (y z)
K := &amp;lambda;x.&amp;lambda;y.x
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here S (Slider) combinator slides z to between x and y (In some materials S is called Substitution; In &lt;a href="https://www.youtube.com/watch?v=7cPtCpyBPNI" target="_blank"&gt;presentation&lt;/a&gt; of &lt;a href="http://en.wikipedia.org/wiki/Dana_Scott" target="_blank"&gt;Dana Scott&lt;/a&gt; S is called Slider), and K (Killer) combinator kills y.&lt;/p&gt;
&lt;p&gt;In C#, just leave the each combinator&amp;rsquo;s variables as dynamic:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SkiCombinators
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        S = x =&amp;gt; y =&amp;gt; z =&amp;gt; x(z)(y(z));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        K = x =&amp;gt; y =&amp;gt; x;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        I = x =&amp;gt; x;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;omega; is the self application combinator. It applies variable f to f itself:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;&amp;omega; := &amp;lambda;f.f f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Just like above f, &amp;omega; can also be applied with &amp;omega; itself, which is the definition of &amp;Omega;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;&amp;Omega; := &amp;omega; &amp;omega; &amp;equiv; (&amp;lambda;f.f f) (&amp;lambda;f.f f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here &amp;omega; is a function definition expression without free variables, and &amp;Omega; is a function application expression, which contains no free variables. For &amp;Omega;, its function application can be beta reduced forever:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(&amp;lambda;f.f f) (&amp;lambda;f.f f)
&amp;equiv; (&amp;lambda;f.f f) (&amp;lambda;f.f f)
&amp;equiv; (&amp;lambda;f.f f) (&amp;lambda;f.f f)
&amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So &amp;omega; &amp;omega; is an infinite application. &amp;Omega; is called the looping combinator.&lt;/p&gt;
&lt;p&gt;In C#, it is easy to define the type of self applicable function, like above f. Assume the function&amp;rsquo;s return type is TResult, then this function is of type input &amp;ndash;&amp;gt; TResult:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(?);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The input type is the function type itself, so it is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; self)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Above Func&amp;lt;TResult&amp;gt; is the self applicable function type. To be unambiguous with System.Func&amp;lt;TResult&amp;gt;, it can be renamed to SelfApplicableFunc&amp;lt;TResult&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult SelfApplicableFunc&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SelfApplicableFunc&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; self);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So SelfApplicableFunc&amp;lt;TResult&amp;gt; is equivalent to SelfApplicableFunc&amp;lt;TResult&amp;gt; -&amp;gt; TResult. Since f is of type SelfApplicableFunc&amp;lt;TResult&amp;gt;, f(f) returns TResult. And since &amp;omega; accept f and returns TResult. &amp;omega; is of type SelfApplicableFunc&amp;lt;TResult&amp;gt; -&amp;gt; TResult, which is the definition of SelfApplicableFunc&amp;lt;TResult&amp;gt;, so &amp;omega; is still of type SelfApplicableFunc&amp;lt;TResult&amp;gt;, &amp;omega;(&amp;omega;) is still of type TResult:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;OmegaCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SelfApplicableFunc&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        &amp;omega; = f =&amp;gt; f(f);
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult
        &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;Omega; = &amp;omega;(&amp;omega;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;SKI combinator calculus&lt;/h1&gt;
&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/SKI_combinator_calculus" target="_blank"&gt;SKI combinator calculus&lt;/a&gt; is a kind of combinatory logic. As a variant of lambda calculus, SKI combinatory logic has no general expression definition rules, or general expression reduction rules. It only has the above S, K, I combinators as the only 3 primitives, and the only 3 function application rules. It can be viewed as a reduced version of lambda calculus, and an extremely simple Turing complete language with only 3 elements: S, K, I.&lt;/p&gt;
&lt;p&gt;Take the Boolean values as a simple example. Remember in lambda calculus, True and False are defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;True := &amp;lambda;t.&amp;lambda;f.t
False := &amp;lambda;t.&amp;lambda;f.f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that when they are applied:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;True t f
&amp;equiv; (&amp;lambda;t.&amp;lambda;f.t) t f
&amp;equiv; t

  False t f
&amp;equiv; (&amp;lambda;t.&amp;lambda;f.f) t f
&amp;equiv; f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here in SKI combinator calculus, SKI combinators are the only primitives, so True and False can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;True := K
False := S K&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that when they are applied, they return the same result as the lambda calculus definition:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;True t f
&amp;equiv; K t f
&amp;equiv; t

  False t f
&amp;equiv; S K t f
&amp;equiv; K f (t f) 
&amp;equiv; f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Remember function composition is defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;(f&lt;sub&gt;2&lt;/sub&gt; ∘ f&lt;sub&gt;1&lt;/sub&gt;) x := f&lt;sub&gt;2&lt;/sub&gt; (f&lt;sub&gt;1&lt;/sub&gt; x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In SKI, the composition operator can be equivalently defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Compose := S (K S) K&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And this is how it works:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Compose f&lt;sub&gt;2&lt;/sub&gt; f&lt;sub&gt;1&lt;/sub&gt; x
&amp;equiv; S (K S) K f&lt;sub&gt;2&lt;/sub&gt; f&lt;sub&gt;1&lt;/sub&gt; x
&amp;equiv; (K S) f&lt;sub&gt;2&lt;/sub&gt; (K f&lt;sub&gt;2&lt;/sub&gt;) f&lt;sub&gt;1&lt;/sub&gt; x
&amp;equiv; S (K f&lt;sub&gt;2&lt;/sub&gt;) f&lt;sub&gt;1&lt;/sub&gt; x
&amp;equiv; (K f&lt;sub&gt;2&lt;/sub&gt;) x (f&lt;sub&gt;1&lt;/sub&gt; x)
&amp;equiv; f&lt;sub&gt;2&lt;/sub&gt; (f&lt;sub&gt;1&lt;/sub&gt; x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In lambda calculus, numerals are defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := &amp;lambda;f.&amp;lambda;x.x
1 := &amp;lambda;f.&amp;lambda;x.f x
2 := &amp;lambda;f.&amp;lambda;x.f (f x)
3 := &amp;lambda;f.&amp;lambda;x.f (f (f x))
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In SKI, numerals are equivalently defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := K I                     &amp;equiv; K I
1 := I                       &amp;equiv; I
2 := S Compose I             &amp;equiv; S (S (K S) K) I
3 := S Compose (S Compose I) &amp;equiv; S (S (K S) K) (S (S (K S) K) I)
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When these numerals are applied, they return the same results as lambda calculus definition:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;0 f x
&amp;equiv; K I f x
&amp;equiv; I x
&amp;equiv; x

  1 f x
&amp;equiv; I f x
&amp;equiv; f x

  2 f x
&amp;equiv; S Compose I f x
&amp;equiv; Compose f (I f) x
&amp;equiv; Compose f f x
&amp;equiv; f (f x)

  3 f x
&amp;equiv; S Compose (S Compose I) f x
&amp;equiv; Compose f (S Compose I f) x
&amp;equiv; Compose f (Compose f f) x
&amp;equiv; f (f (f x))

...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In SKI, the self application combinator &amp;omega; is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;&amp;omega; := S I I&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When it is applied with f, it returns f f:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  S I I f
&amp;equiv; I x (I f) 
&amp;equiv; f f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So naturally, &amp;Omega; is defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;&amp;Omega; := (S I I) (S I I)
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And it is infinite as in lambda calculus:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;S I I (S I I)
&amp;equiv; I (S I I) (I (S I I)) 
&amp;equiv; I (S I I) (S I I) 
&amp;equiv; S I I (S I I)
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Actually, I combinator can be defined with S and K in either of the following ways:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;I := S K K
I := S K S&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And they work the same:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;I x
&amp;equiv; S K K x
&amp;equiv; K x (K x)
&amp;equiv; x

  I x
&amp;equiv; S K S x
&amp;equiv; K x (S x)
&amp;equiv; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So I is just a syntactic sugar in SKI calculus.&lt;/p&gt;
&lt;p&gt;In C#, these combinators can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SkiCombinators&lt;/span&gt;&lt;span style="color: black;"&gt;;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SkiCalculus
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean
        &lt;/span&gt;&lt;span style="color: black;"&gt;True = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;(K);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean
        &lt;/span&gt;&lt;span style="color: black;"&gt;False = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;(S(K));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Compose = S(K(S))(K);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Zero = K(I);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        One = I;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Two = S(Compose)(I);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Three = S(Compose)(S(Compose)(I));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// ...

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Increase = S(Compose);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        &amp;omega; = S(I)(I);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        &amp;Omega; = S(I)(I)(S(I)(I));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        IWithSK = S(K)(K); &lt;/span&gt;&lt;span style="color: green;"&gt;// Or S(K)(S).
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;SKI compiler: compile lambda calculus expression to SKI calculus combinator&lt;/h2&gt;
&lt;p&gt;The S, K, I combinators can be composed to new combinator that equivalent to any lambda calculus expression. An arbitrary expression in lambda calculus can be converted to combinator in SKI calculus. Assume v is a variable in lambda calculus, and E is an expression in lambda calculus, the conversion ToSki is defined as:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;ToSki (v) =&amp;gt; v&lt;/li&gt;
&lt;li&gt;ToSki (E&lt;sub&gt;1&lt;/sub&gt; E&lt;sub&gt;2&lt;/sub&gt;) =&amp;gt; (ToSki (E&lt;sub&gt;1&lt;/sub&gt;) (ToSki (E&lt;sub&gt;2&lt;/sub&gt;)))&lt;/li&gt;
&lt;li&gt;ToSki (&amp;lambda;v.E) =&amp;gt; (K (ToSki (E))), if x does not occur free in E&lt;/li&gt;
&lt;li&gt;ToSki (&amp;lambda;v.v) =&amp;gt; I&lt;/li&gt;
&lt;li&gt;ToSki (&amp;lambda;v&lt;sub&gt;1&lt;/sub&gt;.&amp;lambda;v&lt;sub&gt;2&lt;/sub&gt;.E) =&amp;gt; ToSki (&amp;lambda;v&lt;sub&gt;1&lt;/sub&gt;.ToSki (&amp;lambda;v&lt;sub&gt;2&lt;/sub&gt;.E))&lt;/li&gt;
&lt;li&gt;ToSki (&amp;lambda;v.(E&lt;sub&gt;1&lt;/sub&gt; E&lt;sub&gt;2&lt;/sub&gt;)) =&amp;gt; (S (ToSki (&amp;lambda;.v.E&lt;sub&gt;1&lt;/sub&gt;)) (ToSki (&amp;lambda;v.E&lt;sub&gt;2&lt;/sub&gt;)))&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Based on these rules, a compiler can be implemented to compile a expression in lambda calculus to combinator in SKI calculus. As mentioned before, the C# lambda expression can be compiled as function, and also expression tree data representing the logic of that function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;FunctionAsData&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; idFunction = value =&amp;gt; value;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; idExpression = value =&amp;gt; value;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above idFunction and idExpression shares the same lambda expression syntax, but is executable function, while the idExpression is a abstract syntax tree data structure, representing the logic of idFunction:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Expression&amp;lt;Func&amp;lt;T, T&amp;gt;&amp;gt; (NodeType = Lambda, Type = Func&amp;lt;T, T&amp;gt;)
|_Parameters
| |_ParameterExpression (NodeType = Parameter, Type = T)
|   |_Name = "value"
|_Body
  |_ParameterExpression (NodeType = Parameter, Type = T)
    |_Name = "value"&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This metaprogramming feature provides great convenience for the conversion &amp;ndash; just build the lambda calculus expression as .NET expression tree, traverse the tree and apply the above rules, and convert the tree to another tree representing the SKI calculus combinator.&lt;/p&gt;
&lt;p&gt;A SKI calculus combinator, like above &amp;Omega; combinator (S I I) (S I I), is a composition of S, K, I. The S, K, I primitives can be represented with a constant expression:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: black;"&gt;CombinatorExpression(&lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;&lt;span style="color: black;"&gt;name) =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.Name = name;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression &lt;/span&gt;&lt;span style="color: black;"&gt;S { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(S));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression &lt;/span&gt;&lt;span style="color: black;"&gt;K { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(K));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression &lt;/span&gt;&lt;span style="color: black;"&gt;I { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(I));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public string &lt;/span&gt;&lt;span style="color: black;"&gt;Name { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType &lt;/span&gt;&lt;span style="color: black;"&gt;NodeType { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Constant;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Type &lt;/span&gt;&lt;span style="color: black;"&gt;Type { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The composition can be represented with a function application expression:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;internal &lt;/span&gt;&lt;span style="color: black;"&gt;ApplicationExpression(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;function, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;variable)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.Function = function;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;.Variable = variable;
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;Function { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;Variable { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType &lt;/span&gt;&lt;span style="color: black;"&gt;NodeType { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Invoke;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Type &lt;/span&gt;&lt;span style="color: black;"&gt;Type { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; } = &lt;/span&gt;&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the above &amp;Omega; combinator (S I I) (S I I) can be represented by the following expression tree:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;ApplicationExpression (NodeType = Invoke, Type = object)
|_Function
| |_ApplicationExpression (NodeType = Invoke, Type = object)
|   |_Function
|   | |_ApplicationExpression (NodeType = Invoke, Type = object)
|   |   |_Function
|   |   | |_CombinatorExpression (NodeType = Constant, Type = object)
|   |   |   |_Name = "S"
|   |   |_Variable
|   |     |_CombinatorExpression (NodeType = Constant, Type = object)
|   |       |_Name = "I"
|   |_Variable
|     |_CombinatorExpression (NodeType = Constant, Type = object)
|       |_Name = "I"
|_Variable
  |_ApplicationExpression (NodeType = Invoke, Type = object)
    |_Function
    | |_ApplicationExpression (NodeType = Invoke, Type = object)
    |   |_Function
    |   | |_CombinatorExpression (NodeType = Constant, Type = object)
    |   |   |_Name = "S"
    |   |_Variable
    |     |_CombinatorExpression (NodeType = Constant, Type = object)
    |       |_Name = "I"
    |_Variable
      |_CombinatorExpression (NodeType = Constant, Type = object)
        |_Name = "I"&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So in the following SkiCompiler type, the ToSki is implemented to traverse the input abstract syntax tree recursively, and apply the above conversion rules:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SkiCompiler
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;ToSki(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus)
    {
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Ignore type convertion specified in code or generated by C# compiler.
        &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus = lambdaCalculus.IgnoreTypeConvertion();

        &lt;/span&gt;&lt;span style="color: blue;"&gt;switch &lt;/span&gt;&lt;span style="color: black;"&gt;(lambdaCalculus.NodeType)
        {
            &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Constant:
                &lt;/span&gt;&lt;span style="color: green;"&gt;// 0. ToSki(S) = S, ToSki(K) = K, ToSki(I) = I.
                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(lambdaCalculus &lt;/span&gt;&lt;span style="color: blue;"&gt;is &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus;
                }
                &lt;/span&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;;

            &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Parameter:
                &lt;/span&gt;&lt;span style="color: green;"&gt;// 1. ToSki(v) = v.
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus;

            &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Invoke:
                &lt;/span&gt;&lt;span style="color: green;"&gt;// 2. ToSki(E1(E2)) = ToSki(E1)(ToSKi(E2)).
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;application = lambdaCalculus.ToApplication();
                &lt;/span&gt;&lt;span style="color: blue;"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(ToSki(application.Function), ToSki(application.Variable));

            &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda:
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression &lt;/span&gt;&lt;span style="color: black;"&gt;function = (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)lambdaCalculus;
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ParameterExpression &lt;/span&gt;&lt;span style="color: black;"&gt;variable = function.Parameters.Single();
                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;body = function.Body.IgnoreTypeConvertion();

                &lt;/span&gt;&lt;span style="color: green;"&gt;// 3. ToSki(v =&amp;gt; E) = K(ToSki(E)), if v does not occur free in E.
                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(!variable.IsFreeIn(body))
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;.K, ToSki(body));
                }

                &lt;/span&gt;&lt;span style="color: blue;"&gt;switch &lt;/span&gt;&lt;span style="color: black;"&gt;(body.NodeType)
                {
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Parameter:
                        &lt;/span&gt;&lt;span style="color: green;"&gt;// 4. ToSki(v =&amp;gt; v) = I
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(variable == (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ParameterExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)body)
                        {
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;.I;
                        }
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;;

                    &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda:
                        &lt;/span&gt;&lt;span style="color: green;"&gt;// 5. ToSki(v1 =&amp;gt; v2 =&amp;gt; E) = ToSki(v1 =&amp;gt; ToSki(v2 =&amp;gt; E)), if v1 occurs free in E.
                        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression &lt;/span&gt;&lt;span style="color: black;"&gt;bodyFunction = (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)body;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;&lt;span style="color: black;"&gt;(variable.IsFreeIn(bodyFunction.Body))
                        {
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;ToSki(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda(ToSki(bodyFunction), variable));
                        }
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;;

                    &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Invoke:
                        &lt;/span&gt;&lt;span style="color: green;"&gt;// 6. ToSki(v =&amp;gt; E1(E2)) = S(ToSki(v =&amp;gt; E1))(ToSki(v =&amp;gt; E2)).
                        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;bodyApplication = body.ToApplication();
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(
                                &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;.S,
                                ToSki(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda(bodyApplication.Function, variable))),
                            ToSki(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda(bodyApplication.Variable, variable)));
                }
                &lt;/span&gt;&lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;;
        }
        &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(lambdaCalculus));
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It calls a few helper functions:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;IgnoreTypeConvertion(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus) =&amp;gt;
    lambdaCalculus.NodeType == &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Convert
        ? ((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;UnaryExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)lambdaCalculus).Operand
        : lambdaCalculus;

&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;ToApplication(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;expression)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;switch &lt;/span&gt;&lt;span style="color: black;"&gt;(expression)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;application:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;application;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;InvocationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;invocation:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;(invocation.Expression, invocation.Arguments.Single());
    }
    &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(expression));
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;private static bool &lt;/span&gt;&lt;span style="color: black;"&gt;IsFreeIn(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ParameterExpression &lt;/span&gt;&lt;span style="color: black;"&gt;variable, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus)
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Ignore type convertion specified in code or generated by C# compiler.
    &lt;/span&gt;&lt;span style="color: black;"&gt;lambdaCalculus = lambdaCalculus.IgnoreTypeConvertion();

    &lt;/span&gt;&lt;span style="color: blue;"&gt;switch &lt;/span&gt;&lt;span style="color: black;"&gt;(lambdaCalculus.NodeType)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Invoke:
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;application = lambdaCalculus.ToApplication();
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;variable.IsFreeIn(application.Function) || variable.IsFreeIn(application.Variable);
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Lambda:
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression &lt;/span&gt;&lt;span style="color: black;"&gt;function = (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;LambdaExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)lambdaCalculus;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;variable != function.Parameters.Single() &amp;amp;&amp;amp; variable.IsFreeIn(function.Body);
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Parameter:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;variable == (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ParameterExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)lambdaCalculus;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Constant:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return false&lt;/span&gt;&lt;span style="color: black;"&gt;;
    }
    &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(lambdaCalculus));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Sometimes, in order to make the lambda calculus expression be compiled, some type information has to be added manually or automatically by C# compiler. These type conversion information is not needed, and can be removed by IgnoreTypeConvertion. In lambda expression, function invocation is compiled as InvocationExpression node with node type Invoke, which is the same as ApplicationExpression. For convenience, ToApplication unifies all Invoke nodes to ApplicationExpression. And IsFreeIn recursively test whether the specified variable occurs free in the specified lambda calculus expression.&lt;/p&gt;
&lt;p&gt;Finally, for readability, the following ToSkiString method Converts the compiled SKI calculus expression to string representation:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;&lt;span style="color: black;"&gt;ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;skiCalculus) =&amp;gt; skiCalculus.ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;);

&lt;/span&gt;&lt;span style="color: blue;"&gt;private static string &lt;/span&gt;&lt;span style="color: black;"&gt;ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;skiCalculus, &lt;/span&gt;&lt;span style="color: blue;"&gt;bool &lt;/span&gt;&lt;span style="color: black;"&gt;parentheses)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;switch &lt;/span&gt;&lt;span style="color: black;"&gt;(skiCalculus.NodeType)
    {
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Invoke:
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression &lt;/span&gt;&lt;span style="color: black;"&gt;application = (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ApplicationExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)skiCalculus;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;parentheses
                ? &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"(&lt;/span&gt;&lt;span style="color: black;"&gt;{application.Function.ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;)} {application.Variable.ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;)"
                &lt;/span&gt;&lt;span style="color: black;"&gt;: &lt;/span&gt;&lt;span style="color: #a31515;"&gt;$"&lt;/span&gt;&lt;span style="color: black;"&gt;{application.Function.ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;)} {application.Variable.ToSkiString(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)}&lt;/span&gt;&lt;span style="color: #a31515;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Parameter:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ParameterExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)skiCalculus).Name;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;case &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ExpressionType&lt;/span&gt;&lt;span style="color: black;"&gt;.Constant:
            &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: black;"&gt;((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CombinatorExpression&lt;/span&gt;&lt;span style="color: black;"&gt;)skiCalculus).Name;
    }
    &lt;/span&gt;&lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;nameof&lt;/span&gt;&lt;span style="color: black;"&gt;(skiCalculus));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following example demonstrates how to represent 2-tuple in SKI calculus combinator:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Tuple&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;
        createTupleLambda = item1 =&amp;gt; item2 =&amp;gt; f =&amp;gt; f(item1)(item2);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;createTupleSki = createTupleLambda.ToSki();
    createTupleSki.ToSkiString().WriteLine();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// S (S (K S) (S (K K) (S (K S) (S (K (S I)) (S (K K) I))))) (K (S (K K) I))
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To verify the result, a tuple can be created with x as first item, and y as the second item:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;CreateTuple x y
&amp;equiv; S (S (K S) (S (K K) (S (K S) (S (K (S I)) (S (K K) I))))) (K (S (K K) I)) x y
&amp;equiv; S (K S) (S (K K) (S (K S) (S (K (S I)) (S (K K) I)))) x (K (S (K K) I) x) y
&amp;equiv; K S x (S (K K) (S (K S) (S (K (S I)) (S (K K) I))) x) (K (S (K K) I) x) y
&amp;equiv; S (S (K K) (S (K S) (S (K (S I)) (S (K K) I))) x) (K (S (K K) I) x) y
&amp;equiv; S (K K) (S (K S) (S (K (S I)) (S (K K) I))) x y (K (S (K K) I) x y)
&amp;equiv; K K x (S (K S) (S (K (S I)) (S (K K) I)) x) y (K (S (K K) I) x y)
&amp;equiv; K (S (K S) (S (K (S I)) (S (K K) I)) x) y (K (S (K K) I) x y)
&amp;equiv; S (K S) (S (K (S I)) (S (K K) I)) x (K (S (K K) I) x y)
&amp;equiv; K S x (S (K (S I)) (S (K K) I) x) (K (S (K K) I) x y)
&amp;equiv; S (S (K (S I)) (S (K K) I) x) (K (S (K K) I) x y)
&amp;equiv; S (K (S I) x (S (K K) I x)) (K (S (K K) I) x y)
&amp;equiv; S (S I (S (K K) I x)) (K (S (K K) I) x y)
&amp;equiv; S (S I ((K K) x (I x))) (K (S (K K) I) x y)
&amp;equiv; S (S I (K (I x))) (K (S (K K) I) x y)
&amp;equiv; S (S I (K x)) (K (S (K K) I) x y)
&amp;equiv; S (S I (K x)) (S (K K) I y)
&amp;equiv; S (S I (K x)) (K K y (I y))
&amp;equiv; S (S I (K x)) (K (I y))
&amp;equiv; S (S I (K x)) (K y)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To get the first/second item of the above tuple, apply it with True/False:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Item1 (CreateTuple x y)
&amp;equiv; (CreateTuple x y) True
&amp;equiv; S (S I (K x)) (K y) True
&amp;equiv; S (S I (K x)) (K y) K
&amp;equiv; S I (K x) K (K y K)
&amp;equiv; I K (K x K) (K y K)
&amp;equiv; K (K x K) (K y K)
&amp;equiv; K x K
&amp;equiv; x

  Item2 (CreateTuple x y)
&amp;equiv; (CreateTuple x y) False
&amp;equiv; S (S I (K x)) (K y) False
&amp;equiv; S (S I (K x)) (K y) (S K)
&amp;equiv; S I (K x) (S K) (K y (S K))
&amp;equiv; I (S K) (K x (S K)) (K y (S K))
&amp;equiv; S K (K x (S K)) (K y (S K))
&amp;equiv; K y (K x (S K) y)
&amp;equiv; y&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the compiled 2-tuple SKI calculus combinator is equivalent to the lambda calculus expression.&lt;/p&gt;
&lt;p&gt;Another example is the logic operator And:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;And := &amp;lambda;a.&amp;lambda;b.a b False &amp;equiv; &amp;lambda;a.&amp;lambda;b.a b (&amp;lambda;t.&amp;lambda;f.f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So in C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;And()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        andLambda = a =&amp;gt; b =&amp;gt; a(b)((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;)(@true =&amp;gt; @false =&amp;gt; @false));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;andSki = andLambda.ToSki();
    &lt;/span&gt;&lt;span style="color: black;"&gt;andSki.ToSkiString().WriteLine();;&lt;/span&gt;&lt;span style="color: green;"&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Unfortunately, above expression tree cannot be compiled, with error CS1963: An expression tree may not contain a dynamic operation. The reason is, Boolean is the alias of Func&amp;lt;dynamic, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt;, and C# compiler does not support dynamic operations in expression tree, like calling a(b) here. At compile time, dynamic is just object, so the solution is to replace dynamic with object, and replace Boolean with object &amp;ndash;&amp;gt; object -&amp;gt; object, then the following code can be compiled:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;And()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;
        andLambda = a =&amp;gt; b =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;)a(b)((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;)(@true =&amp;gt; @false =&amp;gt; @false));
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Expression &lt;/span&gt;&lt;span style="color: black;"&gt;andSki = andLambda.ToSki();
    &lt;/span&gt;&lt;span style="color: black;"&gt;andSki.ToSkiString().WriteLine();
    &lt;/span&gt;&lt;span style="color: green;"&gt;// S (S (K S) (S (S (K S) (S (K K) I)) (K I))) (K (K (K I)))
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The compilation result can be verified in similar way:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;And True True
&amp;equiv; S (S (K S) (S (S (K S) (S (K K) I)) (K I))) (K (K (K I))) True True
&amp;equiv; S (S (K S) (S (S (K S) (S (K K) I)) (K I))) (K (K (K I))) K K
&amp;equiv; S (K S) (S (S (K S) (S (K K) I)) (K I)) K (K (K (K I)) K) K
&amp;equiv; K S K (S (S (K S) (S (K K) I)) (K I) K) (K (K (K I)) K) K
&amp;equiv; S (S (S (K S) (S (K K) I)) (K I) K) (K (K (K I)) K) K
&amp;equiv; S (S (K S) (S (K K) I)) (K I) K K (K (K (K I)) K K)
&amp;equiv; S (K S) (S (K K) I) K (K I K) K (K (K (K I)) K K)
&amp;equiv; K S K (S (K K) I K) (K I K) K (K (K (K I)) K K)
&amp;equiv; S (S (K K) I K) (K I K) K (K (K (K I)) K K)
&amp;equiv; S (K K) I K K (K I K K) (K (K (K I)) K K)
&amp;equiv; K K K (I K) K (K I K K) (K (K (K I)) K K)
&amp;equiv; K (I K) K (K I K K) (K (K (K I)) K K)
&amp;equiv; I K (K I K K) (K (K (K I)) K K)
&amp;equiv; K (K I K K) (K (K (K I)) K K)
&amp;equiv; K I K K
&amp;equiv; I K
&amp;equiv; K
&amp;equiv; True

  And True False
&amp;equiv; S (S (K S) (S (S (K S) (S (K K) I)) (K I))) (K (K (K I))) True False
&amp;equiv; S (S (K S) (S (S (K S) (S (K K) I)) (K I))) (K (K (K I))) K (S K)
&amp;equiv; (S (K S)) (S (S (K S) (S (K K) I)) (K I)) K (K (K (K I)) K) (S K)
&amp;equiv; K S K (S (S (K S) (S (K K) I)) (K I) K) (K (K (K I)) K) (S K)
&amp;equiv; S (S (S (K S) (S (K K) I)) (K I) K) (K (K (K I)) K) (S K)
&amp;equiv; S (S (K S) (S (K K) I)) (K I) K (S K) (K (K (K I)) K (S K))
&amp;equiv; S (K S) (S (K K) I) K (K I K) (S K) (K (K (K I)) K (S K))
&amp;equiv; K S K (S (K K) I K) (K I K) (S K) (K (K (K I)) K (S K))
&amp;equiv; S (S (K K) I K) (K I K) (S K) (K (K (K I)) K (S K))
&amp;equiv; S (K K) I K (S K) (K I K (S K)) (K (K (K I)) K (S K))
&amp;equiv; K K K (I K) (S K) (K I K (S K)) (K (K (K I)) K (S K))
&amp;equiv; K (I K) (S K) (K I K (S K)) (K (K (K I)) K (S K))
&amp;equiv; I K (K I K (S K)) (K (K (K I)) K (S K))
&amp;equiv; K (K I K (S K)) (K (K (K I)) K (S K))
&amp;equiv; K I K (S K)
&amp;equiv; I (S K)
&amp;equiv; S K
&amp;equiv; False

...&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Iota combinator calculus&lt;/h1&gt;
&lt;p&gt;Another interesting example of combinator logic is &lt;a href="http://en.wikipedia.org/wiki/Iota_and_Jot" target="_blank"&gt;Iota&lt;/a&gt; combinator calculus. It has only one combinator:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;&amp;iota; := &amp;lambda;f.f S K &amp;equiv; &amp;lambda;f.f (&amp;lambda;x.&amp;lambda;y.&amp;lambda;z.x z (y z)) (&amp;lambda;x.&amp;lambda;y.x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;That&amp;rsquo;s &lt;a href="https://web.archive.org/web/20150121065142/http://semarch.linguistics.fas.nyu.edu/barker/Iota/" target="_blank"&gt;the whole combinatory logic&lt;/a&gt;. It is an &lt;a href="http://en.wikipedia.org/wiki/Esoteric_programming_language" target="_blank"&gt;esoteric programming language&lt;/a&gt; with minimum element &amp;ndash; only 1 single element, but still &lt;a href="http://en.wikipedia.org/wiki/Turing-complete" target="_blank"&gt;Turing-complete&lt;/a&gt;. With Iota combinator, SKI can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;S := &amp;iota; (&amp;iota; (&amp;iota; (&amp;iota; &amp;iota;)))
K := &amp;iota; (&amp;iota; (&amp;iota; &amp;iota;))
I := &amp;iota; &amp;iota;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Iota is as Turing-complete as&amp;nbsp; SKI. For example:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;I x
&amp;equiv; &amp;iota; &amp;iota; x
&amp;equiv; (&amp;lambda;f.f S K) (&amp;lambda;f.f S K) x
&amp;equiv; (&amp;lambda;f.f S K) S K x
&amp;equiv; (S S K) K x
&amp;equiv; S K (K K) x
&amp;equiv; K x ((K K) x)
&amp;equiv; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, these combinators can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IotaCombinator
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        &amp;iota; = f =&amp;gt; f
            (&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;(x =&amp;gt; y =&amp;gt; z =&amp;gt; x(z)(y(z)))) &lt;/span&gt;&lt;span style="color: green;"&gt;// S
            &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(x =&amp;gt; y =&amp;gt; x)); &lt;/span&gt;&lt;span style="color: green;"&gt;// K
&lt;/span&gt;&lt;span style="color: black;"&gt;}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IotaCalculus
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        S = &amp;iota;(&amp;iota;(&amp;iota;(&amp;iota;(&amp;iota;))));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        K = &amp;iota;(&amp;iota;(&amp;iota;(&amp;iota;)));

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        I = &amp;iota;(&amp;iota;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Tue, 19 Nov 2024 13:06:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-6-combinatory-logic</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Combinators</category><category>Combinatory Logic</category><category>SKI</category><category>Iota</category></item><item><title>Lambda Calculus via C# (5) List</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-5-list</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;In lambda calculus and Church encoding, there are various ways to represent a list with anonymous functions.&lt;/p&gt;
&lt;h1&gt;Tuple as list node&lt;/h1&gt;
&lt;p&gt;With Church pair, it is easy to model Church list as a linked list, where each list node is a a Church pair (2-tuple) of current node&amp;rsquo;s value and the next node, So that&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;CreateListNode := &lt;span style="background: #f5f5f5; color: black;"&gt;CreateTuple = &lt;/span&gt;&amp;lambda;v.&amp;lambda;n.&amp;lambda;f.f v n
ListNode := &lt;span style="background: #f5f5f5; color: black;"&gt;Tuple = &lt;/span&gt;&amp;lambda;f.f v n
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here variable v is the value of the current node, so it is the first item of the tuple; And variable n is the next node of the current node, so it is the second item of the tuple:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Value := Item1 = &amp;lambda;l.l (&amp;lambda;v.&amp;lambda;n.v)
Next := Item2 = &amp;lambda;l.l (&amp;lambda;v.&amp;lambda;n.n)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here variable l is the list node. The C# implementation is similar to tuple and signed numeral, except ListNode&amp;lt;T&amp;gt; function type now has 1 type parameter, which is the type of its value:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// ListNode&amp;lt;T&amp;gt; is the alias of Tuple&amp;lt;T, ListNode&amp;lt;T&amp;gt;&amp;gt;.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate dynamic &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;f);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Create = value =&amp;gt; next =&amp;gt; (value, next)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        Create = value =&amp;gt; next =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;.Create(value)(next));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Value = node =&amp;gt; node.Item1()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
        Value = node =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(node).Item1();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Next = node =&amp;gt; node.Item2()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        Next = node =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(node).Item2();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Usually, when a list ends, its last node&amp;rsquo;s next node is flagged as a special null node. Here in lambda calculus, since a node is an anonymous function, the null node is also an anonymous function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Null := &amp;lambda;f.&amp;lambda;x.x
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And IsNull predicate returns a Church Boolean to indicate whether a list node is null:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsNull := &amp;lambda;l.l (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When IsNull is applied with a null node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNull Null
&amp;equiv; (&amp;lambda;l.l (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) True) (&amp;lambda;f.&amp;lambda;x.x)
&amp;equiv; (&amp;lambda;f.&amp;lambda;x.x) (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) True
&amp;equiv; (&amp;lambda;x.x) True
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And when IsNull is applied with a non-null node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  IsNull (CreateListNode 0 Null)
&amp;equiv; IsNull (&amp;lambda;f.f 0 Null)
&amp;equiv; (&amp;lambda;l.l (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) True) (&amp;lambda;f.f 0 Null)
&amp;equiv; (&amp;lambda;f.f 0 Null) (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) True
&amp;equiv; (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.False) 0 Null True
&amp;equiv; (&amp;lambda;n.&amp;lambda;x.False) Null True
&amp;equiv; (&amp;lambda;x.False) True
&amp;equiv; False&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The C# implementation is noisy because a lot of type information has to be provided. This is Null:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;
&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Null = False;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Null = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(False);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// IsNull = node =&amp;gt; node(value =&amp;gt; next =&amp;gt; _ =&amp;gt; False)(True)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
        IsNull = node =&amp;gt; node(value =&amp;gt; next =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(_ =&amp;gt; False))(True);&lt;/span&gt;&lt;span style="color: black;"&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the indexer for list can be easily defined with as a function accepts a start node and a Church numeral i as the specified index. To return the node at the specified index, just call Next function for i times from the start node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;ListNodeAt := &amp;lambda;l.&amp;lambda;i.i Next l&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
    ListNodeAt = start =&amp;gt; index =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;index(node =&amp;gt; Next(node&lt;/span&gt;&lt;span style="color: black;"&gt;))(start);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following are the extension methods wrapping the list operators:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNodeExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;Value&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Value(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Next&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Next(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsNull&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.IsNull(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ListNodeAt&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; start, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;index) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.ListNodeAt(start)(index);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the following code demonstrates how the list works:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchListTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;CreateValueNextTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1, node1.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null, node1.Next());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2, node2.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1, node2.Next());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3, node3.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node2, node3.Next());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node2.Value(), node3.Next().Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1.Value(), node3.Next().Next().Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null, node3.Next().Next().Next());
        &lt;/span&gt;&lt;span style="color: blue;"&gt;try
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null.Next();
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.Fail();
        }
        &lt;/span&gt;&lt;span style="color: blue;"&gt;catch &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;InvalidCastException &lt;/span&gt;&lt;span style="color: black;"&gt;exception)
        {
            &lt;/span&gt;&lt;span style="color: black;"&gt;exception.WriteLine();
        }
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;IsNullTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node1.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node2.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node3.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node1.Next().IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node2.Next().IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node3.Next().IsNull().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;IndexTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node3, node3.NodeAt(0U.Church()));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node2, node3.NodeAt(1U.Church()));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1, node3.NodeAt(2U.Church()));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node3.NodeAt(3U.Church()).IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: blue;"&gt;try
        &lt;/span&gt;&lt;span style="color: black;"&gt;{
            node3.NodeAt(4U.Church());
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.Fail();
        }
        &lt;/span&gt;&lt;span style="color: blue;"&gt;catch &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;InvalidCastException &lt;/span&gt;&lt;span style="color: black;"&gt;exception)
        {
            &lt;/span&gt;&lt;span style="color: black;"&gt;exception.WriteLine();
        }
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Aggregate function as list node&lt;/h1&gt;
&lt;p&gt;Remember the LINQ Aggregate query method accepting a seed and a accumulator function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate &lt;/span&gt;&lt;span style="color: black;"&gt;Aggregate&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; source, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate &lt;/span&gt;&lt;span style="color: black;"&gt;seed, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TSource&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TAccumulate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; func);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Assume seed is x, and accumulator function is f:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When source is empty, the aggregation result is x&lt;/li&gt;
&lt;li&gt;When source is { 0 }, the aggregation result is f(x, 0)&lt;/li&gt;
&lt;li&gt;When source is { 1, 0 }, the aggregation result is f(f(x, 1), 0)&lt;/li&gt;
&lt;li&gt;When source is { 2, 1, 0 }, the aggregation result is f(f(f(x, 2), 1), 0)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Church list can also be encoded with a similar Aggregate function with seed and accumulator function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;dynamic &lt;/span&gt;&lt;span style="color: black;"&gt;AggregateListNode&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic &lt;/span&gt;&lt;span style="color: black;"&gt;x, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; f);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Its type parameter T is the type of node value. And since the seed can be anything, just leave it as dynamic as usual. So the list node is of above aggregate function type (dynamic, (dynamic , T) -&amp;gt; dynamic) -&amp;gt; dynamic. After currying the aggregate function and the accumulator function, it becomes dynamic -&amp;gt; (dynamic &amp;ndash;&amp;gt; T -&amp;gt; dynamic) -&amp;gt; dynamic. So this is the function type of list node, and an alias can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Curried from: (dynamic, dynamic -&amp;gt; T -&amp;gt; dynamic) -&amp;gt; dynamic.
// AggregateListNode is the alias of: dynamic -&amp;gt; (dynamic -&amp;gt; T -&amp;gt; dynamic) -&amp;gt; dynamic.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic &lt;/span&gt;&lt;span style="color: black;"&gt;x);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And this is the creation and definition of list node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;CreateListNode := &amp;lambda;v.&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) v
ListNode := &amp;lambda;x.&amp;lambda;f.f (n x f) v&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        Create = value =&amp;gt; next =&amp;gt; x =&amp;gt; f =&amp;gt; f(next(x)(f))(value);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, here variable v is the value of current node, variable n is the next node of the current node. And variable x is the seed for aggregation, variable f is the accumulator function. The list is still modeled as a linked list, so Null is also needed to represent the end of list:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Null := &amp;lambda;x.&amp;lambda;f.x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Null is defined to call f for 0 times. For example, to create a linked list { 2, 1, 0 }, first create the last list node, with value 2 and Null as its next node:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;CreateListNode 0 Null
&amp;equiv; (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) v) 0 (&amp;lambda;x.&amp;lambda;f.x)
&amp;equiv; (&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) 0) (&amp;lambda;x.&amp;lambda;f.x)
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f ((&amp;lambda;x.&amp;lambda;f.x) x f) 0
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f x 0&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then the previous node can be created with value 1 and the above node:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;CreateListNode 1 (CreateListNode 0 Null)
&amp;equiv; CreateListNode 1 (&amp;lambda;x.&amp;lambda;f.f x 0)
&amp;equiv; (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) v) 1 (&amp;lambda;x.&amp;lambda;f.f x 0)
&amp;equiv; (&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) 1) (&amp;lambda;x.&amp;lambda;f.f x 0)
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f ((&amp;lambda;x.&amp;lambda;f.f x 0) x f) 1
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f (f x 0) 1&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the first node has value 0:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;CreateListNode 2 (CreateListNode 1 (CreateListNode 0 Null))
&amp;equiv; CreateListNode 2 (&amp;lambda;x.&amp;lambda;f.f (f x 0) 1)
&amp;equiv; (&amp;lambda;v.&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) v) 2 (&amp;lambda;x.&amp;lambda;f.f (f x 0) 1)
&amp;equiv; (&amp;lambda;n.&amp;lambda;x.&amp;lambda;f.f (n x f) 2) (&amp;lambda;x.&amp;lambda;f.f (f x 0) 1)
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f (&amp;lambda;x.&amp;lambda;f.f (f x 0) 1) x f) 2
&amp;equiv; &amp;lambda;x.&amp;lambda;f.f (f (f x 0) 1) 2&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the list nodes are represented in the same pattern as LINQ aggregation.&lt;/p&gt;
&lt;p&gt;The IsNull predicate can be defined as following:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsNull := &amp;lambda;l.l True (&amp;lambda;x.&amp;lambda;v.False)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The variable l is the list node, which is an aggregate function, and is applied with seed True and accumulate function &amp;lambda;v.&amp;lambda;x.False. When IsNull is applied with a null node, the accumulate function is not applied, and seed True is directly returned:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;IsNull Null
&amp;equiv; (&amp;lambda;l.l True (&amp;lambda;x.&amp;lambda;v.False)) (&amp;lambda;x.&amp;lambda;f.x)
&amp;equiv; (&amp;lambda;x.&amp;lambda;f.x) True (&amp;lambda;x.&amp;lambda;v.False)
&amp;equiv; (&amp;lambda;f.True) (&amp;lambda;x.&amp;lambda;v.False)
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And when IsNull is applied with a non null node, the accumulator function is applied and constantly returns False, so IsNull returns False:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;IsNull (CreateListNode 2 Null)
&amp;equiv; IsNull (&amp;lambda;x.&amp;lambda;f.f x 2)
&amp;equiv; (&amp;lambda;l.l True (&amp;lambda;x.&amp;lambda;v.False)) (&amp;lambda;x.&amp;lambda;f.f x 2)
&amp;equiv; (&amp;lambda;x.&amp;lambda;f.f x 2) True (&amp;lambda;x.&amp;lambda;v.False)
&amp;equiv; (&amp;lambda;f.f True 2) (&amp;lambda;x.&amp;lambda;v.False)
&amp;equiv; (&amp;lambda;x.&amp;lambda;v.False) True 2
&amp;equiv; False&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;
&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Null = x =&amp;gt; f =&amp;gt; x;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        IsNull = node =&amp;gt; node(True)(x =&amp;gt; value =&amp;gt; False);&lt;/span&gt;&lt;span style="color: black;"&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following function returns the value from the specified node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Value := &amp;lambda;l.l Id (&amp;lambda;x.&amp;lambda;v.v)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When Value is applied with a node, which has value v and next node n:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Value (CreateListNode v n)
&amp;equiv; Value (&amp;lambda;x.&amp;lambda;f.f (n x f) v)
&amp;equiv; (&amp;lambda;l.l Id (&amp;lambda;x.&amp;lambda;v.v)) (&amp;lambda;x.&amp;lambda;f.f (n x f) v)
&amp;equiv; (&amp;lambda;x.&amp;lambda;f.f (n x f) v) Id (&amp;lambda;x.&amp;lambda;v.v)
&amp;equiv; (&amp;lambda;f.f (n Id f) v) (&amp;lambda;x.&amp;lambda;v.v)
&amp;equiv; (&amp;lambda;x.&amp;lambda;v.v) (n Id f) v
&amp;equiv; (&amp;lambda;v.v) v
&amp;equiv; v&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Value = node =&amp;gt; node(Id)(x =&amp;gt; value =&amp;gt; value)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
    Value = node =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;node(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Id)(x =&amp;gt; value =&amp;gt; value);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;It is not very intuitive to get a node&amp;rsquo;s next node:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Next := &amp;lambda;l.&amp;lambda;x.&amp;lambda;f.l (&amp;lambda;f.x) (&amp;lambda;x.&amp;lambda;v.&amp;lambda;g.g (x f) v) (&amp;lambda;x.&amp;lambda;v.v)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Next = node =&amp;gt; x =&amp;gt; f =&amp;gt; node(_ =&amp;gt; x)(accumulate =&amp;gt; value =&amp;gt; (g =&amp;gt; g(accumulate(f))(value)))(accumulate =&amp;gt; value =&amp;gt; accumulate);
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Next = node =&amp;gt; x =&amp;gt; f =&amp;gt; node(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(_ =&amp;gt; x))(accumulate =&amp;gt; value =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(g =&amp;gt; g(accumulate(f))(value)))(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(accumulate =&amp;gt; value =&amp;gt; accumulate));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above definition is similar to the the pattern of initial version Subtract function for Church numeral. So it can be defined by shifting tuple too. Again, list node with value v and next node n is a aggregate function, it can be applied with a tuple of Null nodes as seed, and a accumulator function to swap the tuple:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(CreateListNode v n) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (&amp;lambda;x.&amp;lambda;f.f (n x f) v) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (&amp;lambda;f.f (n (Null, Null) f) v) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t) (n (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t) v
&amp;equiv; (&amp;lambda;v.Shift (CreateListNode v) (n (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)) v
&amp;equiv; Shift (CreateListNode v) (n (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Take list { n, n &amp;ndash; 1, &amp;hellip;, 2, 1, 0 } as example, assume its nodes are ListNode&lt;sub&gt;n&lt;/sub&gt;, ListNode&lt;sub&gt;n - 1&lt;/sub&gt;, &amp;hellip;, ListNode&lt;sub&gt;2&lt;/sub&gt;, ListNode&lt;sub&gt;1&lt;/sub&gt;, ListNode&lt;sub&gt;0&lt;/sub&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the last node&amp;nbsp; is: CreateListNode 0 Null&lt;/li&gt;
&lt;li&gt;the second last node is: CreateListNode 1 (CreateListNode 0 Null)&lt;/li&gt;
&lt;li&gt;the third last node is: CreateListNode 2 (CreateListNode 1 (CreateListNode 0 Null))&lt;/li&gt;
&lt;li&gt;&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now apply these nodes with above tuple seed and tuple shifting accumulator function:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;ListNode&lt;sub&gt;0&lt;/sub&gt; (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (CreateListNode 0 Null) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; Shift (CreateListNode 0) (Null (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)
&amp;equiv; Shift (CreateListNode 0) ((&amp;lambda;x.&amp;lambda;f.&amp;lambda;x) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)
&amp;equiv; Shift (CreateListNode 0) (Null, Null)
&amp;equiv; (Null, CreateListNode 0 Null)
&amp;equiv; (Null, ListNode&lt;sub&gt;0&lt;/sub&gt;)

  ListNode&lt;sub&gt;1&lt;/sub&gt; (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (CreateListNode 1 (CreateListNode 0 Null)) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; Shift (CreateListNode 1) ((CreateListNode 0 Null) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)
&amp;equiv; Shift (CreateListNode 1) (Null, Create ListNode 0 Null)
&amp;equiv; (CreateListNode 0 Null, (CreateListNode 1 (CreateListNode 0 Null))
&amp;equiv; (ListNode&lt;sub&gt;0&lt;/sub&gt;, ListNode&lt;sub&gt;1&lt;/sub&gt;)

  ListNode&lt;sub&gt;2&lt;/sub&gt; (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (CreateListNode 2 (CreateListNode 1 (CreateListNode 0 Null))) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; Shift (CreateListNode 2) ((CreateListNode 1 (CreateListNode 0 Null)) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v)) t)
&amp;equiv; Shift (CreateListNode 2) (CreateListNode 0 Null, (CreateListNode 1 (CreateListNode 0 Null))
&amp;equiv; ((CreateListNode 1 (CreateListNode 0 Null), CreateListNode 2 (CreateListNode 1 (CreateListNode 0 Null)))
&amp;equiv; (ListNode&lt;sub&gt;1&lt;/sub&gt;, ListNode&lt;sub&gt;2&lt;/sub&gt;)

...

  ListNode&lt;sub&gt;n&lt;/sub&gt; (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (ListNode&lt;sub&gt;n - 1&lt;/sub&gt;, ListNode&lt;sub&gt;n&lt;/sub&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Generally, there is:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(CreateListNode v n) (Null, Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t)
&amp;equiv; (n, Create v n)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Next can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Next := &amp;lambda;l.Item2 (l (CreateTuple Null Null) (&amp;lambda;t.&amp;lambda;v.Shift (CreateListNode v) t))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Next = node =&amp;gt; node((Null, Null))(tuple =&amp;gt; value =&amp;gt; tuple.Shift(ChurchTuple.Create(value))).Item1()
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Next = node =&amp;gt;
        ((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;)node
            (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;.Create(Null)(Null))
            (tuple =&amp;gt; value =&amp;gt; ((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;)tuple).Shift(Create(value))))
        .Item1();&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The indexer can be defined the same as above:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;ListNodeAt := &amp;lambda;l.&amp;lambda;i.i Next l&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
    ListNodeAt = start =&amp;gt; index =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;index(node =&amp;gt; Next(&lt;/span&gt;&lt;span style="color: black;"&gt;node))(start);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following are the extension methods wrapping the list operators:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNodeExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsNull&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.IsNull(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T &lt;/span&gt;&lt;span style="color: black;"&gt;Value&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Value(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Next&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node) =&amp;gt; 
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Next(node);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ListNodeAt&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; start, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;index) =&amp;gt; 
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.ListNodeAt(start)(index);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the following code demonstrate how the list works:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateListTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;CreateValueNextTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1, node1.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node1.Next().IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2, node2.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1.Value(), node2.Next().Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3, node3.Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node2.Value(), node3.Next().Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1.Value(), node3.Next().Next().Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node3.Next().Next().Next().IsNull().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;IsNullTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node1.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node2.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node3.IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node1.Next().IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node2.Next().IsNull().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsFalse(node3.Next().IsNull().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;IndexTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Null);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(2)(node1);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;AggregateListNode&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; node3 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchAggregateList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(3)(node2);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node3.Value(), node3.NodeAt(0U.Church()).Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node2.Value(), node3.NodeAt(1U.Church()).Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(node1.Value(), node3.NodeAt(2U.Church()).Value());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(node3.NodeAt(3U.Church()).IsNull().Unchurch());
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Model everything&lt;/h1&gt;
&lt;p&gt;Once again, in lambda calculus the only primitive is anonymous function. So far many data types and operations are modeled by anonymous functions, including Boolean, unsigned and signed numeral, tuple, list, logic, arithmetic (except division, which will be implemented later), predicate, etc. With these facilities, many other data types and operations can be modeled too. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Floating_point" target="_blank"&gt;Floating point number&lt;/a&gt; can be represented in the form of significand * base&lt;sup&gt;exponent&lt;/sup&gt;. In &lt;a href="https://en.wikipedia.org/wiki/IEEE_floating_point" target="_blank"&gt;IEEE 754 (aka IEC 60559)&lt;/a&gt;, floating point numbers are &lt;a href="https://www.h-schmidt.net/FloatConverter/IEEE754.html" target="_blank"&gt;represented&lt;/a&gt; as binary format (sign) significand * 2&lt;sup&gt;exponent&lt;/sup&gt; (&lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm" target="_blank"&gt;System.Single and System.Double&lt;/a&gt; in .NET), and decimal format (sign) significand * 10&lt;sup&gt;exponent&lt;/sup&gt; (System.Decimal). So either representation can be modeled with a 3-tuple of (Boolean, unsigned numeral, signed numeral).&lt;/li&gt;
&lt;li&gt;Character (System.Char in .NET) can be represented by unsigned numeral.&lt;/li&gt;
&lt;li&gt;String (System.String in .NET) can be modeled by a list of characters.&lt;/li&gt;
&lt;li&gt;Tuple and list can represent other data structures, like tree, stack, queue, etc.&lt;/li&gt;
&lt;li&gt;&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And eventually everything can be modeled with anonymous function represented by lambda expression. Actually, lambda calculus is a classic example of &lt;a href="https://en.wikipedia.org/wiki/Turing_completeness" target="_blank"&gt;Turing completeness&lt;/a&gt;. Lambda calculus is introduced by Alonzo Church before &lt;a href="https://en.wikipedia.org/wiki/Turing_machine" target="_blank"&gt;Turing machine&lt;/a&gt; was introduced by Alan Turing, and they are equivalent. Lambda calculus, as a universal model of computation, is the rationale and foundations of functional programming. Functional languages (or the functional subset of languages) can be viewed as lambda calculus with more specific syntax, and the execution of functional program can be viewed as reduction of lambda calculus expression.&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Wed, 13 Nov 2024 13:06:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-5-list</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Church Encoding</category><category>Church Lists</category></item><item><title>Lambda Calculus via C# (4) Tuple and Signed Numeral</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-4-tuple-and-signed-numeral</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;Besides modeling values like Boolean and numeral, anonymous function can also model data structures. In Church encoding, Church pair is an approach to use functions to represent a tuple of 2 items.&lt;/p&gt;
&lt;h1&gt;Church pair (2-tuple)&lt;/h1&gt;
&lt;p&gt;A tuple can be constructed with its first item x, its second item y, and a function f:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;CreateTuple := &amp;lambda;x.&amp;lambda;y.&amp;lambda;f.f x y&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So a tuple is can be created by partially applying CreateTuple with 2 items x and y:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Tuple := CreateTuple x y
       &amp;equiv; (&amp;lambda;x.&amp;lambda;y.&amp;lambda;f.f x y) x y
       &amp;equiv; &amp;lambda;f.f x y&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So a tuple is a higher-order function, which accept a function f, and apply it with its 2 items. So f accepts 2 arguments, it is in the form of &amp;lambda;x.&amp;lambda;y.E.&lt;/p&gt;
&lt;p&gt;To get the tuple&amp;rsquo;s first item x, just apply the tuple function with a specific function f, where f simply accepts 2 items and returns the first item:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Tuple (&amp;lambda;x.&amp;lambda;y.x)
&amp;equiv; (&amp;lambda;f.f x y) (&amp;lambda;x.&amp;lambda;y.x)
&amp;equiv; (&amp;lambda;x.&amp;lambda;y.x) x y
&amp;equiv; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, to get the tuple&amp;rsquo;s second item y, just apply tuple function with a specific function f, where f simply accepts 2 items and returns the first item:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Tuple (&amp;lambda;x.&amp;lambda;y.y)
&amp;equiv; (&amp;lambda;f.f x y) (&amp;lambda;x.&amp;lambda;y.y)
&amp;equiv; (&amp;lambda;x.&amp;lambda;y.y) x y
&amp;equiv; y&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the following Item1 function is defined to accept a tuple, apply the tuple function with function &amp;lambda;x.&amp;lambda;y.x, and return the tuple&amp;rsquo;s first item:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Item1 := &amp;lambda;t.t (&amp;lambda;x.&amp;lambda;y.x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Again, this is how it works:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Item1 (CreateTuple x y)
&amp;equiv; (&amp;lambda;t.t (&amp;lambda;x.&amp;lambda;y.x)) (CreateTuple x y)
&amp;equiv; (&amp;lambda;t.t (&amp;lambda;x.&amp;lambda;y.x)) (&amp;lambda;f.f x y)
&amp;equiv; (&amp;lambda;f.f x y) (&amp;lambda;x.&amp;lambda;y.x)
&amp;equiv; (&amp;lambda;x.&amp;lambda;y.x) x y
&amp;equiv; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And Item2 function can ne defined in the same way to get the tuple&amp;rsquo;s second item:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Item2 := &amp;lambda;t.t (&amp;lambda;x.&amp;lambda;y.y)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Notice functions &amp;lambda;x.&amp;lambda;y.x and &amp;lambda;x.&amp;lambda;y.y can be alpha converted to &amp;lambda;t.&amp;lambda;f.t and &amp;lambda;t.&amp;lambda;f.f, which are just Church Boolean True and False. So Item1 and Item2 can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Item1 := &amp;lambda;t.t True
Item2 := &amp;lambda;t.t False&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To implement tuple in C#, its function type needs to be identified. The tuple function accepts argument f, which is either function True of function False, so f is of function type Boolean. In the body of tuple function, f is applied, and f returns dynamic. So tuple virtually is of function type Boolean -&amp;gt; dynamic:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;

&lt;/span&gt;&lt;span style="color: green;"&gt;// Tuple is the alias of (dynamic -&amp;gt; dynamic -&amp;gt; dynamic) -&amp;gt; dynamic.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate dynamic &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;f);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt; 
        Create = item1 =&amp;gt; item2 =&amp;gt; f =&amp;gt; f(item1)(item2);

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Item1 = tuple =&amp;gt; tuple(True)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
        Item1 = tuple =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;)(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;)tuple(True);&lt;/span&gt;&lt;span style="color: #00008b;"&gt;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Item2 = tuple =&amp;gt; tuple(False)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
        Item2 = tuple =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;)(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;)tuple(False);&lt;/span&gt;&lt;span style="color: #00008b;"&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There are type conversions in Item1/Item2 functions. At compiled time, the tuple function returns dynamic, and at runtime it actually it calls True/False function to return either item1 or item2 . So the type conversions are always safe. Also notice here tuple function&amp;rsquo;s return value cannot be directly converted to T1 or T2, because of a &lt;a href="http://stackoverflow.com/questions/37392566/" target="_blank"&gt;bug of C# runtime binding layer&lt;/a&gt;. The workaround is to convert dynamic to object first, then convert to T1 or T2.&lt;/p&gt;
&lt;p&gt;The following are the extension methods for convenience:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TupleExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1 &lt;/span&gt;&lt;span style="color: black;"&gt;Item1&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Item1(tuple);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2 &lt;/span&gt;&lt;span style="color: black;"&gt;Item2&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Item2(tuple);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;For example, a point can be a tuple of 2 numerals:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Point(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;x, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;y)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; point1 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(x)(y);
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;x1 = point1.Item1();
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;y1 = point1.Item1();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Move up.
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;y2 = y1.Increase();
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; point2 = &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(x1)(y2);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Tuple operators&lt;/h2&gt;
&lt;p&gt;The Swap function accepts a tuple (x, y), swaps its first item and second item, and returns a new tuple (y, x):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Swap := &amp;lambda;t.CreateTuple (Item2 t)(Item1 t)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Apparently, Swap is of function type Tuple&amp;lt;T1, T2&amp;gt; -&amp;gt; Tuple&amp;lt;T2, T1&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Swap = tuple =&amp;gt; Create(tuple.Item2())(tuple.Item1())
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Swap = tuple =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(tuple.Item2())(tuple.Item1());&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The Shift function accepts a tuple (x, y) and a function f, and returns a new tuple (y, f y):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Shift := &amp;lambda;f.&amp;lambda;t.CreateTuple (Item2 t) (f (Item2 t))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here assume the argument tuple (x, y) is of type Tuple&amp;lt;T1, T2&amp;gt;, regarding f is applied with y, assume f returns TResult type, then f is of function type T2 -&amp;gt; TResult, so that the returned new tuple (y, f y) is of type Tuple&amp;lt;T2, TResult&amp;gt;. As a result, Shift is of type Tuple&amp;lt;T1, T2&amp;gt; -&amp;gt; (T2 -&amp;gt; TResult) -&amp;gt; Tuple&amp;lt;T2, TResult&amp;gt;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Shift = f =&amp;gt; tuple =&amp;gt; Create(tuple.Item2())(f(tuple.Item1()))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
        Shift = f =&amp;gt; tuple =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(tuple.Item2())(f(tuple.Item2()));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And their extension methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Swap&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Swap(tuple);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Shift&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; f) =&amp;gt; 
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TResult&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Shift(f)(tuple);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here Shift function can be used to define the Subtract function for Church numerals. Remember a Church numeral n can be viewed as to apply Increase n times from 0:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  n Increase 0
&amp;equiv; n&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Applying Shift with Increase and a tuple of Church numerals, it returns a new tuple of Church numerals, so this application can repeat forever:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Shift Increase (0, 0)
&amp;equiv; (0, Increase 0)
&amp;equiv; (0, 1)

  Shift Increase (0, 1)
&amp;equiv; (1, Increase 1)
&amp;equiv; (1, 2)

  Shift Increase (1, 2)
&amp;equiv; (2, Increase 2)
&amp;equiv; (2, 3)

...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In another word, partially applying Shift with Increase is a function that can be repeatedly applied with a tuple of Church numerals:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;(Shift Increase) (0, 0)                                       &amp;equiv; (Shift Increase)&lt;sup&gt;1&lt;/sup&gt; (0, 0) &amp;equiv; 1 (Shift Increase) (0, 0) 
&amp;equiv; (0, 1)

  (Shift Increase) (0, 1)
&amp;equiv; (Shift Increase) ((Shift Increase) (0, 0))
&amp;equiv; (Shift Increase) ∘ (Shift Increase) (0, 0)                    &amp;equiv; (Shift Increase)&lt;sup&gt;2&lt;/sup&gt; (0, 0) &amp;equiv; 2 (Shift Increase) (0, 0) 
&amp;equiv; (1, 2)

  (Shift Increase) (1, 2)
&amp;equiv; (Shift Increase) ((Shift Increase) ∘ (Shift Increase) (0, 0))
&amp;equiv; (Shift Increase) ∘ (Shift Increase) ∘ (Shift Increase) (0, 0) &amp;equiv; (Shift Increase)&lt;sup&gt;3&lt;/sup&gt; (0, 0) &amp;equiv; 3 (Shift Increase) (0, 0) 
&amp;equiv; (2, 3)

...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So generally:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;n (Shift Increase) (0, 0)
&amp;equiv; (n - 1, n)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As a result, to decrease n to n &amp;ndash; 1, just apply n with function (Shift Increase) and tuple (0, 0), get the result tuple (n &amp;ndash; 1, n), and return its first item:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Item1 (n (Shift Increase) (0, 0))
&amp;equiv; Item1 (n - 1, n)
&amp;equiv; n - 1&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So Decrease can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Decrease := &amp;lambda;n.Item1 (n (Shift Increase) (CreateTuple 0 0))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Decrease = n =&amp;gt; n(tuple =&amp;gt; tuple.Shift(Increase))(0, 0).Item1();
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Decrease = n =&amp;gt;
    ((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;)n
        (tuple =&amp;gt; ((&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;)tuple).Shift(Increase))&lt;/span&gt;&lt;span style="color: green;"&gt;
        &lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(Zero)(Zero)))
    .Item1();&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;N-tuple&lt;/h1&gt;
&lt;p&gt;An easy way is to model n-tuple as a 2-tuple of the first value and a (n-1)-tuple of the rest values. A 3 tuple of values 1, 2, 3 can be represented by nested 2-tuples as (a, (b, c)), a 4-tuple of values 1, 2, 3, 4 can be represented by nested 2-tuples (1, (2, (3, 4))), etc., and a n tuple of values 1, 2, 3, &amp;hellip;, n can be represented by nested 2 tuples (1, (2, (3, (&amp;hellip;(n-1, n)&amp;hellip;)))).&amp;nbsp; For example, the following is the definition of 3 tuple:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Create3Tuple := &amp;lambda;x.&amp;lambda;y.&amp;lambda;z.CreateTuple x (CreateTuple y z)

3TupleItem1 := &amp;lambda;t.Item1 t
3TupleItem2 := &amp;lambda;t.Item1 (Item2 t)
3TupleItem3 := &amp;lambda;t.Item2 (Item2 t)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And in C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate dynamic &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;out &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;f);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Create = item1 =&amp;gt; item2 =&amp;gt; item3 =&amp;gt; Create(item1)(Create(item2)(item3))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;
        Create = item1 =&amp;gt; item2 =&amp;gt; item3 =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;.Create(item1)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(item2)(item3)));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Item1 = tuple.Item1()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Item1 = tuple =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(tuple).Item1();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Item2 = tuple.Item2().Item1()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Item2 = tuple =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(tuple).Item2().Item1();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Item3 = tuple.Item2().Item2()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Item3 = tuple =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(tuple).Item2().Item2();
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TupleExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1 &lt;/span&gt;&lt;span style="color: black;"&gt;Item1&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Item1(tuple);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2 &lt;/span&gt;&lt;span style="color: black;"&gt;Item2&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Item2(tuple);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3 &lt;/span&gt;&lt;span style="color: black;"&gt;Item3&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; tuple) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T1&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T2&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;T3&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Item3(tuple);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Signed numeral&lt;/h1&gt;
&lt;p&gt;With tuple, a signed numeral (integer) can be modeled by a pair of Church numerals (natural numbers), where the first item represents the positive value, and the second item represents the negative value:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;SignedNumeral := Tuple&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;For example (1, 0) and (2, 1) models 1, (0, 2) and (1, 3) models &amp;ndash;2, (0, 0) and (1, 1) models 0, etc.:&lt;/p&gt;
&lt;pre class="code"&gt; &lt;span style="color: black;"&gt;1 := (1, 0) &amp;equiv; (2, 1) &amp;equiv; (3, 2) &amp;equiv; (4, 3) &amp;equiv; ...
 0 := (0, 0) &amp;equiv; (1, 1) &amp;equiv; (2, 2) &amp;equiv; (3, 3) &amp;equiv; ...
-2 := (0, 2) &amp;equiv; (1, 3) &amp;equiv; (2, 4) &amp;equiv; (3, 5) &amp;equiv; ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, the function type SignedNumeral is the same as Tuple, except SignedNumeral is not open generic type:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SignedNumeral is the alias of Tuple&amp;lt;Numeral, Numeral&amp;gt;.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate dynamic &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;f);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Church numeral represents natural number. So Converting a Church numeral n to signed number is easy, just make it a tuple (n, 0):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Sign := &amp;lambda;n.CreateTuple n 0&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To negate a signed numeral, just swap its positive value and negative value:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Negate := Swap&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And it is straightforward to get the positive value and the negative value from a signed number:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Positive := Item1
Negative := Item2&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Signed numeral like (4, 3), (3, 3), (3, 5) can be formatted to have at least one 0: (1, 0), (0, 0), (0, 2). For a signed number s represented by (p, n), If p &amp;gt;= n, then it is (p - n, 0), otherwise it is (0, n &amp;ndash; p):&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Format := &amp;lambda;s.If (s&lt;sub&gt;p&lt;/sub&gt; &amp;gt;=  s&lt;sub&gt;n&lt;/sub&gt;) (&amp;lambda;x.(s&lt;sub&gt;p&lt;/sub&gt; - s&lt;sub&gt;n&lt;/sub&gt;, 0)) (&amp;lambda;x.(0, s&lt;sub&gt;n&lt;/sub&gt; - s&lt;sub&gt;p&lt;/sub&gt;))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here S&lt;sub&gt;p&lt;/sub&gt; is the positive value of s, and s&lt;sub&gt;n&lt;/sub&gt; the the negative value of s.&lt;/p&gt;
&lt;p&gt;The following are these function&amp;rsquo;s C# implementation, and the extension methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Sign = n =&amp;gt; (n, 0)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Sign = n =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(n)(Zero));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Negate = signed =&amp;gt; signed.Swap()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Negate = signed =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(signed).Swap());

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Positive = signed =&amp;gt; signed.Item1()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Positive = signed =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(signed).Item1();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Negative = signed =&amp;gt; signed.Item2()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Negative = signed =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Tuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(signed).Item2();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Format = signed =&amp;gt;
    //    If(positive &amp;gt;= negative)
    //        (_ =&amp;gt; (positive - negative, 0))
    //        (_ =&amp;gt; (0, negative - positive))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;
        Format = signed =&amp;gt;
            If(signed.Positive().IsGreaterThanOrEqualTo(signed.Negative()))
                (_ =&amp;gt; signed.Positive().Subtract(signed.Negative()).Sign())
                (_ =&amp;gt; signed.Negative().Subtract(signed.Positive()).Sign().Negate());
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Sign(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Sign(n);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Negate(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Negate(signed);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Positive(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Positive(signed);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Negative(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Negative(signed);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Format(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Format(signed);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Arithmetic operators&lt;/h2&gt;
&lt;p&gt;Naturally, for signed numbers a, b:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;  a + b
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt;) + (b&lt;sub&gt;p&lt;/sub&gt;, b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; - a&lt;sub&gt;n&lt;/sub&gt;) + (b&lt;sub&gt;p&lt;/sub&gt; - b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; + b&lt;sub&gt;p&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt; + b&lt;sub&gt;n&lt;/sub&gt;)

  a - b
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt;) - (b&lt;sub&gt;p&lt;/sub&gt;, b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; - a&lt;sub&gt;n&lt;/sub&gt;) - (b&lt;sub&gt;p&lt;/sub&gt; - b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; + b&lt;sub&gt;n&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt; + b&lt;sub&gt;p&lt;/sub&gt;)

  a * b
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt;) * (b&lt;sub&gt;p&lt;/sub&gt;, b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; - a&lt;sub&gt;n&lt;/sub&gt;) * (b&lt;sub&gt;p&lt;/sub&gt; - b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; * b&lt;sub&gt;p&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; * b&lt;sub&gt;n&lt;/sub&gt;, a&lt;sub&gt;p&lt;/sub&gt; * b&lt;sub&gt;n&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; * b&lt;sub&gt;p&lt;/sub&gt;)

  a / b
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt;, a&lt;sub&gt;n&lt;/sub&gt;) / (b&lt;sub&gt;p&lt;/sub&gt;, b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; - a&lt;sub&gt;n&lt;/sub&gt;) / (b&lt;sub&gt;p&lt;/sub&gt; - b&lt;sub&gt;n&lt;/sub&gt;)
&amp;equiv; (a&lt;sub&gt;p&lt;/sub&gt; / b&lt;sub&gt;p&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; / b&lt;sub&gt;n&lt;/sub&gt;, a&lt;sub&gt;p&lt;/sub&gt; / b&lt;sub&gt;n&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; / b&lt;sub&gt;p&lt;/sub&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So in lambda calculus:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;AddSigned := &amp;lambda;a.&amp;lambda;b.Format (CreateTuple (a&lt;sub&gt;p&lt;/sub&gt; + b&lt;sub&gt;p&lt;/sub&gt;) (a&lt;sub&gt;n&lt;/sub&gt; + b&lt;sub&gt;n&lt;/sub&gt;))
SubtractSigned := &amp;lambda;a.&amp;lambda;b.Format (CreateTuple (a&lt;sub&gt;p&lt;/sub&gt; + b&lt;sub&gt;n&lt;/sub&gt;) (a&lt;sub&gt;n&lt;/sub&gt; + b&lt;sub&gt;p&lt;/sub&gt;))
MultiplySigned := &amp;lambda;a.&amp;lambda;b.Format (CreateTuple (a&lt;sub&gt;p&lt;/sub&gt; * b&lt;sub&gt;p&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; * b&lt;sub&gt;n&lt;/sub&gt;) (a&lt;sub&gt;p&lt;/sub&gt; * b&lt;sub&gt;n&lt;/sub&gt; + a&lt;sub&gt;n&lt;/sub&gt; * b&lt;sub&gt;p&lt;/sub&gt;))
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Division is more tricky because a and b&amp;rsquo;s positive and negative values can be 0. In this case, just return 0 when dividing by 0:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;DivideByIgnoreZero := &amp;lambda;a.&amp;lambda;b.If (IsZero b) (&amp;lambda;x.0) (&amp;lambda;x.DivideBy a b)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here the DivideBy function for Church numeral is used. As fore mentioned, this DivideBy function is not well defined. It is temporarily used here and will be revisited later. So division can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;DivideBySigned := &amp;lambda;a.&amp;lambda;b.Format (CreateTuple ((DivideByIgnoreZero a&lt;sub&gt;p&lt;/sub&gt; b&lt;sub&gt;p&lt;/sub&gt;) + (DivideByIgnoreZero a&lt;sub&gt;n&lt;/sub&gt; b&lt;sub&gt;n&lt;/sub&gt;)) ((DivideByIgnoreZero a&lt;sub&gt;p&lt;/sub&gt; b&lt;sub&gt;n&lt;/sub&gt;) + (DivideByIgnoreZero a&lt;sub&gt;n&lt;/sub&gt; b&lt;sub&gt;p&lt;/sub&gt;)))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following are the C# implementations and the extension methods:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Add = a =&amp;gt; b =&amp;gt; (a.Positive() + b.Positive(), a.Negative() + b.Negative()).Format()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Add = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create
                (a.Positive().Add(b.Positive()))
                (a.Negative().Add(b.Negative())))
            .Format();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Subtract = a =&amp;gt; b =&amp;gt; (a.Positive() + b.Negative(), a.Negative() + b.Positive()).Format()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Subtract = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create
                (a.Positive().Add(b.Negative()))
                (a.Negative().Add(b.Positive())))
            .Format();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply = a =&amp;gt; b =&amp;gt; (a.Positive() * b.Positive() + a.Negative() * b.Negative(), a.Positive() * b.Negative() + a.Negative() * b.Positive()).Format()
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        Multiply = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create
                (a.Positive().Multiply(b.Positive()).Add(a.Negative().Multiply(b.Negative())))
                (a.Positive().Multiply(b.Negative()).Add(a.Negative().Multiply(b.Positive()))))
            .Format();

    &lt;/span&gt;&lt;span style="color: green;"&gt;// / = dividend =&amp;gt; divisor =&amp;gt; If(divisor.IsZero())(_ =&amp;gt; 0)(_ =&amp;gt; dividend.DivideBy(divisor))
    &lt;/span&gt;&lt;span style="color: blue;"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        DivideByIgnoreZero = dividend =&amp;gt; divisor =&amp;gt;
            &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.If(divisor.IsZero())
                (_ =&amp;gt; Zero)
                (_ =&amp;gt; dividend.DivideBy(divisor));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// DivideBy = dividend =&amp;gt; divisor =&amp;gt; (dividend.Positive() / divisor.Positive() + dividend.Negative() / divisor.Negative(), dividend.Positive() / divisor.Negative() + dividend.Negative() / divisor.Positive()).Format();
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        DivideBy = dividend =&amp;gt; divisor =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create
                (DivideByIgnoreZero(dividend.Positive())(divisor.Positive()).Add(DivideByIgnoreZero(dividend.Negative())(divisor.Negative())))
                (DivideByIgnoreZero(dividend.Positive())(divisor.Negative()).Add(DivideByIgnoreZero(dividend.Negative())(divisor.Positive()))))
            .Format();
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Add(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Add(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Subtract(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Subtract(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Multiply(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;DivideBy(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;dividend, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;divisor) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.DivideBy(dividend)(divisor);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the following code demonstrate how these operators work:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchSignedNumeralTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;SignNegatePositiveNegativeTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());
        signed = signed.Negate();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());

        signed = 1U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(1U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());
        signed = signed.Negate();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(1U == signed.Negative().Unchurch());

        signed = 2U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(2U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());
        signed = signed.Negate();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(2U == signed.Negative().Unchurch());

        signed = 123U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(123U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());
        signed = signed.Negate();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(123U == signed.Negative().Unchurch());

        signed = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(12U.Church())(23U.Church()));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(12U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(23U == signed.Negative().Unchurch());
        signed = signed.Negate();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(23U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(12U == signed.Negative().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;FormatWithZeroTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;signed = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(12U.Church())(23U.Church()));
        signed = signed.Format();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(11U == signed.Negative().Unchurch());

        signed = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchTuple&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Create(23U.Church())(12U.Church()));
        signed = signed.Format();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(11U == signed.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == signed.Negative().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;AddTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;result = a.Add(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 1U.Church().Sign();
        b = 1U.Church().Sign().Negate();
        result = a.Add(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 3U.Church().Sign();
        b = 5U.Church().Sign().Negate();
        result = a.Add(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(2U == result.Negative().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;SubtractTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;result = a.Subtract(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 1U.Church().Sign();
        b = 1U.Church().Sign().Negate();
        result = a.Subtract(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(2U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 3U.Church().Sign();
        b = 5U.Church().Sign().Negate();
        result = a.Subtract(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(8U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;MultiplyTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;result = a.Multiply(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 1U.Church().Sign();
        b = 1U.Church().Sign().Negate();
        result = a.Multiply(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(1U == result.Negative().Unchurch());

        a = 3U.Church().Sign();
        b = 5U.Church().Sign().Negate();
        result = a.Multiply(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(15U == result.Negative().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;DivideByTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;a = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;b = 0U.Church().Sign();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;SignedNumeral &lt;/span&gt;&lt;span style="color: black;"&gt;result = a.DivideBy(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Negative().Unchurch());

        a = 1U.Church().Sign();
        b = 1U.Church().Sign().Negate();
        result = a.DivideBy(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(1U == result.Negative().Unchurch());

        a = 11U.Church().Sign();
        b = 5U.Church().Sign().Negate();
        result = a.DivideBy(b);
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(0U == result.Positive().Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.IsTrue(2U == result.Negative().Unchurch());
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Sun, 10 Nov 2024 13:05:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-4-tuple-and-signed-numeral</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Church Encoding</category><category>Church Pairs</category></item><item><title>Lambda Calculus via C# (3) Numeral, Arithmetic and Predicate</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-3-numeral-arithmetic-and-predicate</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;Anonymous functions can also model numerals and their arithmetic. In Church encoding, a natural number n is represented by a function that calls a given function for n times. This representation is called Church Numeral.&lt;/p&gt;
&lt;h1&gt;Church numerals&lt;/h1&gt;
&lt;p&gt;Church numerals are defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := &amp;lambda;fx.x                  &amp;equiv; &amp;lambda;f.&amp;lambda;x.x
1 := &amp;lambda;fx.f x                &amp;equiv; &amp;lambda;f.&amp;lambda;x.f x
2 := &amp;lambda;fx.f (f x)            &amp;equiv; &amp;lambda;f.&amp;lambda;x.f (f x)
3 := &amp;lambda;fx.f (f (f x))        &amp;equiv; &amp;lambda;f.&amp;lambda;x.f (f (f x))
...
n := &amp;lambda;fx.f (f ... (f x)...) &amp;equiv; &amp;lambda;f.&amp;lambda;x.f (f .u.. (f x)...)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So a Church numeral n is a &lt;a href="https://weblogs.asp.net/dixin/understanding-csharp-covariance-and-contravariance-5-higher-order-functions"&gt;higher order function&lt;/a&gt;, it accepts a function f and an argument x. When n is applied, it repeatedly applies f for n times by starting with x, and returns the result. If n is 0, f is not applied (in another word, f is applied 0 times), and x is directly returned.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 f x &amp;equiv; x
1 f x &amp;equiv; f x
2 f x &amp;equiv; f (f x)
3 f x &amp;equiv; f (f (f x))
...
n f x &amp;equiv; f (f (... (f x)...))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;According to the definition of function composition:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;f (f x) &amp;equiv; (f ∘ f) x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This definition is equivalent to compose f for n time:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := &amp;lambda;fx.x                  &amp;equiv; &amp;lambda;f.&amp;lambda;x.x                   &amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;0&lt;/sup&gt; x
1 := &amp;lambda;fx.f x                &amp;equiv; &amp;lambda;f.&amp;lambda;x.f x                 &amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;1&lt;/sup&gt; x
2 := &amp;lambda;fx.f (f x)            &amp;equiv; &amp;lambda;f.&amp;lambda;x.(f ∘ f) x           &amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;2&lt;/sup&gt; x
3 := &amp;lambda;fx.f (f (f x))        &amp;equiv; &amp;lambda;f.&amp;lambda;x.(f ∘ f ∘ f) x       &amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;3&lt;/sup&gt; x
...
n := &amp;lambda;fx.f (f ... (f x)...) &amp;equiv; &amp;lambda;f.&amp;lambda;x.(f ∘ f ∘ ... ∘ f) x &amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;n&lt;/sup&gt; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The partial application with f is the composition of f, so Church numeral n can be simply read as &amp;ndash; do something n times:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 f &amp;equiv; f&lt;sup&gt;0&lt;/sup&gt;
1 f &amp;equiv; f&lt;sup&gt;1&lt;/sup&gt;
2 f &amp;equiv; f&lt;sup&gt;2&lt;/sup&gt;
3 f &amp;equiv; f&lt;sup&gt;3&lt;/sup&gt;
...
n f &amp;equiv; f&lt;sup&gt;n&lt;/sup&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, x can be anything, so leave its type as dynamic. f can be viewed as a function accept a value x and returns something, and f can also accept its returned value again, so f is of type dynamic -&amp;gt; dynamic. And n&amp;rsquo; return type is the same as f&amp;rsquo;s return type, so n returns dynamic too. As a result, n can be virtually viewed as curried function type (dynamic -&amp;gt; dynamic) &amp;ndash;&amp;gt; dynamic -&amp;gt; dynamic, which in C# is represented by Func&amp;lt;Func&amp;lt;dynamic, dynamic&amp;gt;, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt;. Similar to the C# implementation of Church Boolean, a alias Numeral can be defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Curried from (dynamic -&amp;gt; dynamic, dynamic) -&amp;gt; dynamic.
// Numeral is the alias of (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic -&amp;gt; dynamic.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; f);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Based on the definition:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;
{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
        &lt;/span&gt;&lt;span style="color: black;"&gt;Zero = f =&amp;gt; x =&amp;gt; x;
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
        &lt;/span&gt;&lt;span style="color: black;"&gt;One = f =&amp;gt; x =&amp;gt; f(x);
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
        &lt;/span&gt;&lt;span style="color: black;"&gt;Two = f =&amp;gt; x =&amp;gt; f(f(x));
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
        &lt;/span&gt;&lt;span style="color: black;"&gt;Three = f =&amp;gt; x =&amp;gt; f(f(f(x)));

    &lt;/span&gt;&lt;span style="color: green;"&gt;// ...
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Also since n f &amp;equiv; f&lt;sup&gt;n&lt;/sup&gt;, n can be also implemented with composition of f:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
    &lt;/span&gt;&lt;span style="color: black;"&gt;OneWithComposition = f =&amp;gt; f;

&lt;/span&gt;&lt;span style="color: green;"&gt;// Two = f =&amp;gt; f o f
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
    &lt;/span&gt;&lt;span style="color: black;"&gt;TwoWithComposition = f =&amp;gt; f.o(f);

&lt;/span&gt;&lt;span style="color: green;"&gt;// Three = f =&amp;gt; f o f o f
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral
    &lt;/span&gt;&lt;span style="color: black;"&gt;ThreeWithComposition = f =&amp;gt; f.o(f).o(f);

&lt;/span&gt;&lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here the o operator is the forward composition extension method defined previously. Actually, instead of defining each number individually, Church numeral can be defined recursively by increase or decrease.&lt;/p&gt;
&lt;h1&gt;Increase and decrease&lt;/h1&gt;
&lt;p&gt;By observing the definition and code, there are some patterns when the Church numeral increases from 0 to 3. In the definitions of Church numerals:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := &amp;lambda;f.&amp;lambda;x.x
1 := &amp;lambda;f.&amp;lambda;x.f (&lt;span style="text-decoration: underline;"&gt;x)&lt;/span&gt;
2 := &amp;lambda;f.&amp;lambda;x.f (&lt;span style="text-decoration: underline;"&gt;f x&lt;/span&gt;)
3 := &amp;lambda;f.&amp;lambda;x.f (&lt;span style="text-decoration: underline;"&gt;f (f x)&lt;/span&gt;)
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The expressions in the parenthesis can be reduced from the following function applications expressions:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;0 f x &amp;equiv; &lt;span style="text-decoration: underline;"&gt;x&lt;/span&gt;
1 f x &amp;equiv; &lt;span style="text-decoration: underline;"&gt;f x&lt;/span&gt;
2 f x &amp;equiv; &lt;span style="text-decoration: underline;"&gt;f (f x)&lt;/span&gt;
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With substitution, Church numerals&amp;rsquo; definition become:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;0 := &amp;lambda;f.&amp;lambda;x.x
1 := &amp;lambda;f.&amp;lambda;x.f (0 f x)
2 := &amp;lambda;f.&amp;lambda;x.f (1 f x)
3 := &amp;lambda;f.&amp;lambda;x.f (2 f x)
...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This shows how the the Church numerals increases. Generally, given a Church numeral n, the next numeral n + 1 is &amp;lambda;f.&amp;lambda;x.f (n f x). So:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Increase := &amp;lambda;n.&amp;lambda;f.&amp;lambda;x.f (n f x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, this is:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
    Increase = n =&amp;gt; f =&amp;gt; x =&amp;gt; f(n(f)(x));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In the other way, Church numeral n is to compose f for n times:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;n f &amp;equiv; f&lt;sup&gt;n&lt;/sup&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So increasing n means to compose f for one more time:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Increase := &amp;lambda;n.&amp;lambda;f.f ∘ f&lt;sup&gt;n&lt;/sup&gt; &amp;equiv; &amp;lambda;n.&amp;lambda;f.f ∘ (n f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And in C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
    IncreaseWithComposition = n =&amp;gt; f =&amp;gt; f.o(n(f));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;To decrease a Church numeral n, when n is 0, the result is defined as 0, when n is positive, the result is n &amp;ndash; 1. The Decrease function is more complex:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Decrease := &amp;lambda;n.&amp;lambda;f.&amp;lambda;x.n (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x) Id
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When n is 0, regarding n f &amp;equiv; f&lt;sup&gt;n&lt;/sup&gt;, applying Decrease with 0 can be reduced as:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Decrease 0
&amp;equiv; &amp;lambda;f.&amp;lambda;x.0 (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x) Id
&amp;equiv; &amp;lambda;f.&amp;lambda;x.(&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;0&lt;/sup&gt; (&amp;lambda;v.x) Id
&amp;equiv; &amp;lambda;f.&amp;lambda;x.(&amp;lambda;v.x) Id
&amp;equiv; &amp;lambda;f.&amp;lambda;x.x
&amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;0&lt;/sup&gt; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The last expression is the definition of 0.&lt;/p&gt;
&lt;p&gt;When n is positive, regarding function function composition is associative, the expression n (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;u.x) can be reduced first. When n is 1, 2, 3, ...:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;1 (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;1&lt;/sup&gt; (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x)
&amp;equiv; &amp;lambda;h.h ((&amp;lambda;v.x) f)
&amp;equiv; &amp;lambda;h.h x
&amp;equiv; &amp;lambda;h.h (f&lt;sup&gt;0&lt;/sup&gt; x) 

  2 (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;2&lt;/sup&gt; (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f)) ∘ (&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;1&lt;/sup&gt; (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;h.h (f&lt;sup&gt;0&lt;/sup&gt; x))
&amp;equiv; &amp;lambda;h.h (&amp;lambda;h.h (f&lt;sup&gt;0&lt;/sup&gt; x) f)
&amp;equiv; &amp;lambda;h.h (f (f&lt;sup&gt;0&lt;/sup&gt; x))
&amp;equiv; &amp;lambda;h.h (f&lt;sup&gt;1&lt;/sup&gt; x)

  3 (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;3&lt;/sup&gt; (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f)) ∘ (&amp;lambda;g.&amp;lambda;h.h (g f))&lt;sup&gt;2&lt;/sup&gt; (&amp;lambda;v.x)
&amp;equiv; (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;h.h (f&lt;sup&gt;1&lt;/sup&gt; x))
&amp;equiv; &amp;lambda;h.h ((&amp;lambda;h.h (f&lt;sup&gt;1&lt;/sup&gt; x)) f)
&amp;equiv; &amp;lambda;h.h (f (f&lt;sup&gt;1&lt;/sup&gt; x))
&amp;equiv; &amp;lambda;h.h (f&lt;sup&gt;2&lt;/sup&gt; x)

...&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And generally:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;n (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x)
&amp;equiv; &amp;lambda;h.h (f&lt;sup&gt;n - 1&lt;/sup&gt; x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So when Decrease is applied with positive n:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Decrease n
&amp;equiv; &amp;lambda;f.&amp;lambda;x.n (&amp;lambda;g.&amp;lambda;h.h (g f)) (&amp;lambda;v.x) Id
&amp;equiv; &amp;lambda;f.&amp;lambda;x.(&amp;lambda;h.h (f&lt;sup&gt;n - 1&lt;/sup&gt; x)) Id
&amp;equiv; &amp;lambda;f.&amp;lambda;x.Id (f&lt;sup&gt;n - 1&lt;/sup&gt; x)
&amp;equiv; &amp;lambda;f.&amp;lambda;x.f&lt;sup&gt;n - 1&lt;/sup&gt; x&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The returned result is the definition of n &amp;ndash; 1. In the following C# implementation, a lot of noise of type information is involved to implement complex lambda expression:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Decrease = n =&amp;gt; f =&amp;gt; x =&amp;gt; n(g =&amp;gt; h =&amp;gt; h(g(f)))(_ =&amp;gt; x)(Id)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
    Decrease = n =&amp;gt; f =&amp;gt; x =&amp;gt; n(g =&amp;gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(h =&amp;gt; h(g(f))))(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(_ =&amp;gt; x))(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Id));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here are the actual types of the elements in above lambda expression at runtime:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;g: (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;h: dynamic -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;g(f): dynamic&lt;/li&gt;
&lt;li&gt;h(g(f)): dynamic&lt;/li&gt;
&lt;li&gt;h =&amp;gt; h(g(f)): (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;g =&amp;gt; h =&amp;gt; h(g(f)): ((dynamic -&amp;gt; dynamic) -&amp;gt; dynamic) -&amp;gt; (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;n(g =&amp;gt; h =&amp;gt; h(g(f))): ((dynamic -&amp;gt; dynamic) -&amp;gt; dynamic) -&amp;gt; (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;!--EndFragment--&gt;&lt;/li&gt;
&lt;li&gt;_ =&amp;gt; x: (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;n(g =&amp;gt; h =&amp;gt; h(g(f)))(_ =&amp;gt; x): (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;Id: dynamic -&amp;gt; dynamic&lt;/li&gt;
&lt;li&gt;n(g =&amp;gt; h =&amp;gt; h(g(f)))(_ =&amp;gt; x)(Id): dynamic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At compile time, function types must be provided for a few elements. When n is applied, C# compiler expects its first argument g =&amp;gt; h =&amp;gt; h(g(f)) to be of type dynamic =&amp;gt; dynamic. So C# compiler infers g to dynamic, but cannot infer the type of h =&amp;gt; h(g(f)), which can be expression tree or anonymous function, so the constructor call syntax is used here to specify it is a function of type (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic. Similarly, C# compiler expects n&amp;rsquo;s second argument to be dynamic, and C# compiler cannot infer the type of _ =&amp;gt; x, so the constructor syntax is used again for _ =&amp;gt; x. Also, Functions&amp;lt;dynamic&amp;gt;.Id is of Unit&amp;lt;dynamic&amp;gt; type, while at runtime a dynamic -&amp;gt; dynamic function is expected. Unit&amp;lt;dynamic&amp;gt; is alias of function type dynamic &amp;ndash;&amp;gt; dynamic, but the conversion does not happen automatically at runtime, so the constructor syntax is used once again to indicate the function type conversion.&lt;/p&gt;
&lt;p&gt;Later after introducing Church pair, a cleaner version of Decrease will be implemented.&lt;/p&gt;
&lt;h1&gt;Arithmetic operators&lt;/h1&gt;
&lt;p&gt;To implement add operation, according to the definition, Church numeral a adding Church numeral b means to apply f for a times, then apply f again for b times:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Add := &amp;lambda;a.&amp;lambda;b.&amp;lambda;f.&amp;lambda;x.b f (a f x)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With the definition of function composition, Add can be also defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Add := &amp;lambda;a.&amp;lambda;b.&amp;lambda;f.f&lt;sup&gt;a&lt;/sup&gt; ∘ f&lt;sup&gt;b&lt;/sup&gt; &amp;equiv; &amp;lambda;a.&amp;lambda;b.&amp;lambda;f.(a f) ∘ (b f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So in C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;  
    Add = a =&amp;gt; b =&amp;gt; f =&amp;gt; x =&amp;gt; b(f)(a(f)(x));

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
    AddWithComposition = a =&amp;gt; b =&amp;gt; f =&amp;gt; a(f).o(b(f));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With Increase function, Add can also be defined as increase a for b times:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;Add := &amp;lambda;a.&amp;lambda;b.b Increase a&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, there are some noise of type information again:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    AddWithIncrease = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;b(Increase)(a);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Unfortunately, the above code cannot be compiled, because b is a function of type (dynamic -&amp;gt; dynamic) -&amp;gt; dynamic x -&amp;gt; dynamic. So its first argument f must be a function of type dynamic -&amp;gt; dynamic. Here, Increase is of type Numeral -&amp;gt; Numeral, and b(Increase) cannot be compiled. The solution is to eta convert Increase to a wrapper function &amp;lambda;n.Increase n:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Add := &amp;lambda;a.&amp;lambda;b.a (&amp;lambda;n.Increase n) b&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that in C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Add = a =&amp;gt; b =&amp;gt; b(Increase)(a)
// &amp;eta; conversion:
// Add = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Increase(n))(a)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    AddWithIncrease = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Increase(n))(a);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Since a dynamic -&amp;gt; dynamic function is expected and the wrapper function n =&amp;gt; Increase(n), n inferred to be of type dynamic. Increase(n) still returns Numeral, so the wrapper function is of type dynamic -&amp;gt; Numeral. Regarding dynamic is just object, and Numeral derives from object, with support covariance in C#, the wrapper function is implicitly converted to dynamic -&amp;gt; dynamic, so calling b with the wrapper function can be compiled.&lt;/p&gt;
&lt;p&gt;Similarly, Church numeral a subtracting b can be defined as decrease a for b times, a multiplying b can be defined as adding a for b times to 0, and raising a to the power b can be defined as multiplying a for n times with 1:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;Subtract := &amp;lambda;a.&amp;lambda;b.b Decrease a
Multiply := &amp;lambda;a.&amp;lambda;b.b (Add a) 0
Power := &amp;lambda;a.&amp;lambda;b.b (Multiply a) 1&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The C# implementation are in the same pattern:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Subtract = a =&amp;gt; b =&amp;gt; b(Decrease)(a)
// &amp;eta; conversion:
// Subtract = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Decrease(n))(a)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Subtract = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Decrease(n))(a);

&lt;/span&gt;&lt;span style="color: green;"&gt;// Multiply = a =&amp;gt; b =&amp;gt; b(Add(a))(a)
// &amp;eta; conversion:
// Multiply = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Add(a)(n))(Zero)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Multiply = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Add(a)(n))(Zero);

&lt;/span&gt;&lt;span style="color: green;"&gt;// Pow = a =&amp;gt; b =&amp;gt; b(Multiply(a))(a)
// &amp;eta; conversion:
// Pow = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Multiply(a)(n))(1)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Pow = a =&amp;gt; b =&amp;gt; b(n =&amp;gt; Multiply(a)(n))(One);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Similar to Church Boolean operators, the above arithmetic operators can also be wrapped as extension method for convenience:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Increase(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Increase(n);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Decrease(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Decrease(n);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Add(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Add(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Subtract(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Subtract(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Multiply(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Multiply(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Pow(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;mantissa, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;exponent) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(mantissa)(exponent);
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Predicate and relational operators&lt;/h1&gt;
&lt;p&gt;Predicate is function returning Church Boolean. For example, the following function predicate whether a Church numeral n is 0:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsZero := &amp;lambda;n.n (&amp;lambda;x.False) True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When n is 0, (&amp;lambda;x.False) is not applied, and IsZero directly returns True. When n is positive, (&amp;lambda;x.False) is applied for n times. (&amp;lambda;x.False) always return False, so IsZero returns False. The following are the implementation and extension method:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
        IsZero = n =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;n(_ =&amp;gt; False)(True);
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsZero(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsZero(n);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With IsZero, it is easy to define functions to compare 2 Church numerals a and b. According the to definition of Decrease and Subtract, when a &amp;ndash; b is 0, a is either equal to b, or less than b. So IsLessThanOrEqualTo can be defined with IsZero and Subtract:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsLessThanOrEqualTo := &amp;lambda;a.&amp;lambda;b.IsZero (Subtract a b)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;IsGreaterThanOrEqualTo is similar:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsGreaterThanOrEqualTo := &amp;lambda;a.&amp;lambda;b.IsZero (Subtract b a)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then these 2 functions can define IsEqualTo:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsEqualTo := &amp;lambda;a.&amp;lambda;b.And (IsLessThanOrEqualTo a b) (IsGreaterThanOrEqualTo a b)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The opposite of these functions are IsGreaterThan, IsLessThan, IsNotEqual. They can be defined with Not:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;IsGreaterThan := &amp;lambda;a.&amp;lambda;b.Not (IsLessThanOrEqualTo a b)
IsLessThan := &amp;lambda;a.&amp;lambda;b.Not (IsGreaterThanOrEqualTo a b)
IsNotEqualTo := &amp;lambda;a.&amp;lambda;b.Not (IsEqualTo a b)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The following are the C# implementation of these 6 predicates:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        IsLessThanOrEqualTo = a =&amp;gt; b =&amp;gt; a.Subtract(b).IsZero();
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        IsGreaterThanOrEqualTo = a =&amp;gt; b =&amp;gt; b.Subtract(a).IsZero();
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        IsEqualTo = a =&amp;gt; b =&amp;gt; IsLessThanOrEqualTo(a)(b).And(IsGreaterThanOrEqualTo(a)(b));
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        IsGreaterThan = a =&amp;gt; b =&amp;gt; IsLessThanOrEqualTo(a)(b).Not();
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
        IsLessThan = a =&amp;gt; b =&amp;gt; IsGreaterThanOrEqualTo(a)(b).Not();
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        IsNotEqualTo = a =&amp;gt; b =&amp;gt; IsEqualTo(a)(b).Not();
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NumeralExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsLessThanOrEqualTo(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsLessThanOrEqualTo(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsGreaterThanOrEqualTo(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsGreaterThanOrEqualTo(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsEqualTo(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsEqualTo(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsGreaterThan(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsGreaterThan(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsLessThan(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsLessThan(a)(b);

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;IsNotEqualTo(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchPredicate&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNotEqualTo(a)(b);&lt;/span&gt;&lt;span style="color: blue;"&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;h2&gt;Attempt of recursion&lt;/h2&gt;
&lt;p&gt;The division of natural numbers can be defined with arithmetic and relation operators:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;a / b := if a &amp;gt;= b then 1 + (a &amp;ndash; b) / b else 0&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This is a recursive definition. If defining division in this way lambda calculus, the function name is referred in its own body:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;DivideBy := &amp;lambda;a.&amp;lambda;b.If (IsGreaterThanOrEqualTo a b) (&amp;lambda;x.Add One (DivideBy (Subtract a b) b)) (&amp;lambda;x.Zero)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;As fore mentioned, in lambda calculus, functions are anonymously by default, and names are just for readability. Here the self reference does not work with anonymous function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;&amp;lambda;a.&amp;lambda;b.If (IsGreaterThanOrEqualTo a b) (&amp;lambda;x.Add One (? (Subtract a b) b)) (&amp;lambda;x.Zero)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So the above DivideBy function definition is illegal in lambda calculus. The recursion implementation with anonymous function will be discussed later in this chapter.&lt;/p&gt;
&lt;p&gt;In C#, recursion is a basic feature, so the following self reference is supported:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// Divide = dividend =&amp;gt; divisor =&amp;gt; 
    //    If(dividend &amp;gt;= divisor)
    //        (_ =&amp;gt; 1 + DivideBy(dividend - divisor)(divisor))
    //        (_ =&amp;gt; 0);
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        DivideBy = dividend =&amp;gt; divisor =&amp;gt;
            If(dividend.IsGreaterThanOrEqualTo(divisor))
                (_ =&amp;gt; One.Add(DivideBy(dividend.Subtract(divisor))(divisor)))
                (_ =&amp;gt; Zero);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Here using static directive is used so that ChurchBoolean.If function can be called directly. DivideBy is compiled to a field definition and field initialization code in static constructor, and apparently referencing to a field in the constructor is allowed:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;;

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;CompiledChurchNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; DivideBySelfReference;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;static &lt;/span&gt;&lt;span style="color: black;"&gt;CompiledChurchNumeral()
    {
        DivideBySelfReference = dividend =&amp;gt; divisor =&amp;gt;
            If(dividend.IsGreaterThanOrEqualTo(divisor))
                (_ =&amp;gt; One.Add(DivideBySelfReference(dividend.Subtract(divisor))(divisor)))
                (_ =&amp;gt; Zero);
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The self reference also works for named function:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; DivideByMethod(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;dividend) =&amp;gt; divisor =&amp;gt;
        If(dividend.IsGreaterThanOrEqualTo(divisor))
            (_ =&amp;gt; One.Add(DivideByMethod(dividend.Subtract(divisor))(divisor)))
            (_ =&amp;gt; Zero);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The only exception is, when this function is a local variable instead of field, then the inline self reference cannot be compiled:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Inline()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; divideBy = dividend =&amp;gt; divisor =&amp;gt;
        If(dividend.IsGreaterThanOrEqualTo(divisor))
            (_ =&amp;gt; One.Add(divideBy(dividend.Subtract(divisor))(divisor)))
            (_ =&amp;gt; Zero);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The reason is, the value of the local variable is compiled before the local variable is compiled. when the anonymous function is compiled, the referenced divideBy function is not defined yet, and C# compiler gives CS0165 error: Use of unassigned local variable 'divideBy'. To resolve this problem, divideBy can be first initialized with default value null. When divideBy is initialized again with the anonymous function, it is already defined, so the lambda expression can be compiled:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;Inline()

{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; divideBy = &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;;
    divideBy = dividend =&amp;gt; divisor =&amp;gt;
        If(dividend.IsGreaterThanOrEqualTo(divisor))
            (_ =&amp;gt; One.Add(divideBy(dividend.Subtract(divisor))(divisor)))
            (_ =&amp;gt; Zero);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The above division operator DivideBy will be used temporarily. Later after introducing fixed point combinator, the division can be implemented with an anonymous function without self reference at all.&lt;/p&gt;
&lt;h1&gt;Conversion between Church numeral and System.UInt32&lt;/h1&gt;
&lt;p&gt;In .NET, natural number can be represented with unit (System.UInt32). It would be intuitive if Church numeral and uint can be converted to each other. Similar to the conversion between Church Boolean and bool, the following extension methods can be defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchEncoding
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;Church(&lt;/span&gt;&lt;span style="color: blue;"&gt;this uint &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; n == 0U ? &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeral&lt;/span&gt;&lt;span style="color: black;"&gt;.Zero : Church(n - 1U).Increase();

    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static uint &lt;/span&gt;&lt;span style="color: black;"&gt;Unchurch(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;n) =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;)n(x =&amp;gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;uint&lt;/span&gt;&lt;span style="color: black;"&gt;)x + 1U)(0U);&lt;/span&gt;&lt;span style="color: black;"&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Converting uint to Church numeral is recursive. When n is 0, Zero is returned directly. When n is positive, n is decreased and converted recursively. The recursion terminates when n is decreased to 0, then Increase is called for n times with Zero, and Church numeral n is calculated. And converting Church numeral n to uint just need to add 1U for n times to 0U.&lt;/p&gt;
&lt;p&gt;The following code demonstrate how the operators and conversions work:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchNumeralTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;IncreaseTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;numeral = 0U.Church();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U + 1U, (numeral = numeral.Increase()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U + 1U, (numeral = numeral.Increase()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U + 1U, (numeral = numeral.Increase()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U + 1U, (numeral = numeral.Increase()).Unchurch());
        numeral = 123U.Church();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(123U + 1U, numeral.Increase().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;AddTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U + 0U, 0U.Church().Add(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U + 1U, 0U.Church().Add(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U + 0U, 10U.Church().Add(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U + 10U, 0U.Church().Add(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U + 1U, 1U.Church().Add(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U + 1U, 10U.Church().Add(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U + 10U, 1U.Church().Add(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U + 5U, 3U.Church().Add(5U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(123U + 345U, 123U.Church().Add(345U.Church()).Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;DecreaseTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Numeral &lt;/span&gt;&lt;span style="color: black;"&gt;numeral = 3U.Church();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U - 1U, (numeral = numeral.Decrease()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U - 1U, (numeral = numeral.Decrease()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U - 1U, (numeral = numeral.Decrease()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, (numeral = numeral.Decrease()).Unchurch());
        numeral = 123U.Church();
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(123U - 1U, numeral.Decrease().Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;SubtractTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U - 0U, 0U.Church().Subtract(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, 0U.Church().Subtract(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U - 0U, 10U.Church().Subtract(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, 0U.Church().Subtract(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U - 1U, 1U.Church().Subtract(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U - 1U, 10U.Church().Subtract(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, 1U.Church().Subtract(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, 3U.Church().Subtract(5U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U, 123U.Church().Subtract(345U.Church()).Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;MultiplyTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U*0U, 0U.Church().Multiply(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U*1U, 0U.Church().Multiply(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U*0U, 10U.Church().Multiply(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(0U*10U, 0U.Church().Multiply(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U*1U, 1U.Church().Multiply(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U*1U, 10U.Church().Multiply(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U*10U, 1U.Church().Multiply(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U*5U, 3U.Church().Multiply(5U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(12U*23U, 12U.Church().Multiply(23U.Church()).Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;PowTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(0U, 1U), 0U.Church().Pow(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(10U, 0U), 10U.Church().Pow(0U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(0U, 10U), 0U.Church().Pow(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(1U, 1U), 1U.Church().Pow(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(10U, 1U), 10U.Church().Pow(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(1U, 10U), 1U.Church().Pow(10U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(3U, 5U), 3U.Church().Pow(5U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;&lt;span style="color: black;"&gt;.Pow(5U, 3U), 5U.Church().Pow(3U.Church()).Unchurch());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;DivideByRecursionTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U / 1U, 1U.Church().DivideBy(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(1U / 2U, 1U.Church().DivideBy(2U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U / 2U, 2U.Church().DivideBy(2U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(2U / 1U, 2U.Church().DivideBy(1U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(10U / 3U, 10U.Church().DivideBy(3U.Church()).Unchurch());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual(3U / 10U, 3U.Church().DivideBy(10U.Church()).Unchurch());
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Thu, 07 Nov 2024 13:05:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-csharp-3-numeral-arithmetic-and-predicate</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Church Encoding</category><category>Church Numeral</category></item><item><title>Lambda Calculus via C# (2) Church Encoding: Boolean and Logic</title><link>https://weblogs.asp.net:443/dixin/lambda-calculus-via-c-2-boolean-and-logic</link><description>&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/linq-via-csharp"&gt;FP &amp;amp; LINQ via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;h1&gt;[&lt;a href="https://weblogs.asp.net/dixin/Tags/Lambda%20Calculus" target="_blank"&gt;Lambda Calculus via C# series&lt;/a&gt;]&lt;/h1&gt;
&lt;p&gt;Lambda calculus is a formal system for function definition and function application, so in lambda calculus, the only primitive is anonymous function. Anonymous function is actually very powerful. With an approach called Church encoding. data and operation can be modeled by higher-order anonymous functions and their application. Church encoding is named after &lt;a href="http://en.wikipedia.org/wiki/Alonzo_Church"&gt;Alonzo Church&lt;/a&gt;, who first discovered this approach. This part discusses Church Boolean - modeling Boolean values and logic operators with functions.&lt;/p&gt;
&lt;h1&gt;Church Boolean&lt;/h1&gt;
&lt;p&gt;Boolean values True and False can be both represented by anonymous function with 2 parameters. True function simply output the first parameter, and False function output the second parameter:&lt;/p&gt;
&lt;pre class="code"&gt;True := &amp;lambda;tf.t
False := &amp;lambda;tf.f&lt;/pre&gt;
&lt;p&gt;As fore mentioned, &amp;lambda;tf.E is just the abbreviation of &amp;lambda;t.&amp;lambda;f.E, so these definitions actually are:&lt;/p&gt;
&lt;pre class="code"&gt;True := &amp;lambda;t.&amp;lambda;f.t
False := &amp;lambda;t.&amp;lambda;f.f&lt;/pre&gt;
&lt;p&gt;In this tutorial, for consistency and intuition, function definition with multiple variables is always represented in the latter curried form. In C#, they can be viewed as t =&amp;gt; f =&amp;gt; t and t =&amp;gt; f =&amp;gt; f, which are curried from (t, f) =&amp;gt; t and (t, f) =&amp;gt; f. Here t and f can be of any type, so leave their types as dynamic for convenience. In C#, at compile time dynamic is viewed as object and also supports any operation; at runtime if the operation is actually not supported, an exception is thrown. So, the function type of t =&amp;gt; f =&amp;gt; t and t =&amp;gt; f =&amp;gt; f is dynamic &amp;ndash;&amp;gt; dynamic &amp;ndash;&amp;gt; dynamic, which is represented as Func&amp;lt;dynamic, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt; in C#. For convenience, an alias Boolean can be defined for such function type:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Curried from (dynamic, dynamic) -&amp;gt; dynamic.
// Boolean is the alias of dynamic -&amp;gt; dynamic -&amp;gt; dynamic.
&lt;/span&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic &lt;/span&gt;&lt;span style="color: black;"&gt;@true);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;So that True and False can be defined with lambda expression:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean
        &lt;/span&gt;&lt;span style="color: black;"&gt;True = @true =&amp;gt; @false =&amp;gt; @true;
&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean
        &lt;/span&gt;&lt;span style="color: black;"&gt;False = @true =&amp;gt; @false =&amp;gt; @false;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;C# does not support defining function directly in the global scope, so True and False are defined as static filed member of a type.&amp;nbsp; In other functional languages like F#, functions can directly defined:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #f5f5f5; color: blue;"&gt;let &lt;/span&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;True t f = t
&lt;/span&gt;&lt;span style="background: #f5f5f5; color: blue;"&gt;let &lt;/span&gt;&lt;span style="background: #f5f5f5; color: black;"&gt;False t f = f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There is no noise and the function currying is default. Actually this F# code is compiled to CIL code similar to above C# structure (static member of a type).&lt;/p&gt;
&lt;h1&gt;Logical operators&lt;/h1&gt;
&lt;p&gt;After defining Boolean values True and False with functions, now the Boolean logics can be represented by functions too. And can be defined by the following function:&lt;/p&gt;
&lt;pre class="code"&gt;And := &amp;lambda;a.&amp;lambda;b.a b False&lt;/pre&gt;
&lt;p&gt;Applying function True with Boolean a and b:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When a is True, the application is beta reduced to True b False, which applies True function with b and False, and the first argument b is returned. In C#, this can be viewed that true &amp;amp;&amp;amp; b is the same as b.&lt;/li&gt;
&lt;li&gt;When a is False, the application is beta reduced to False b False, which applies False function with b and False, and the second argument False is returned. In C#, this can be viewed as false &amp;amp;&amp;amp; b is always false.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;And True b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a b False) True b
&amp;equiv; (&amp;lambda;b.True b False) b
&amp;equiv; True b False
&amp;equiv; b

  And False b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a b False) False b
&amp;equiv; (&amp;lambda;b.False b False) b
&amp;equiv; False b False
&amp;equiv; False&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In C#, And can be viewed as a =&amp;gt; b =&amp;gt; a(b)(False), it is of curried function type Boolean &amp;ndash;&amp;gt; Boolean -&amp;gt; Boolean:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        And = a =&amp;gt; b =&amp;gt; a(b)(False);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This demonstrates that the Boolean alias improves the readability. Without this alias, the type of And becomes (dynamic &amp;ndash;&amp;gt; dynamic &amp;ndash;&amp;gt; dynamic) &amp;ndash;&amp;gt; (dynamic &amp;ndash;&amp;gt; dynamic &amp;ndash;&amp;gt; dynamic) &amp;ndash;&amp;gt; (dynamic &amp;ndash;&amp;gt; dynamic &amp;ndash;&amp;gt; dynamic), which is Func&amp;lt;Func&amp;lt;dynamic, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt;, Func&amp;lt;Func&amp;lt;dynamic, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt;, Func&amp;lt;dynamic, Func&amp;lt;dynamic, dynamic&amp;gt;&amp;gt;&amp;gt;&amp;gt; in C#.&lt;/p&gt;
&lt;p&gt;This also demonstrates that dynamic type simplifies type conversion. If Boolean is defined as object &amp;ndash;&amp;gt; object -&amp;gt; object:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;object &lt;/span&gt;&lt;span style="color: black;"&gt;@true);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
        And = a =&amp;gt; b =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;)a(b)(False);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And must return Boolean, but a(b)(False) returns object, so a type conversion is required. Here a is either True or False, according to the definition of True and False, a(b)(False) returns either b or False. Since b and False are both of type Boolean, so here it is safe to convert a(b)(False) to Boolean. In contrast, when Boolean is defined as dynamic &amp;ndash;&amp;gt; dynamic -&amp;gt; dynamic, a(b)(False) returns dynamic, which is viewed as supporting any operation at compile time, including implicitly conversion to Boolean, so the explicit type conversion is not required. At run time, a(b)(False) always return Boolean, and converting Boolean to Boolean always succeeds, so And works smoothly without any exception.&lt;/p&gt;
&lt;p&gt;In the above lambda function and C# function, a function name False is referenced. Again, function is anonymous by default in lambda calculus. This tutorial uses function name only for for readability. By substituting function name, And can be defined as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;And := &amp;lambda;a.&amp;lambda;b.a b (&amp;lambda;t.&amp;lambda;f.f)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And the C# implementation becomes:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    And = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;a(b)(&lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;(@true =&amp;gt; @false =&amp;gt; @false));&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The function body is longer and less readable. Also, a is of type dynamic &amp;ndash;&amp;gt; dynamic -&amp;gt; dynamic, the second argument of a is expected to be object. When function reference False is given, False is a Boolean delegate instance, apparently it is an object and works there,&amp;nbsp; However, when an inline C# lambda expression is given. C# compiler cannot infer the the type of this lambda expression &amp;ndash; it could be anonymous function, or expression tree, and the type information of @true and @false cannot be inferred either. So here the constructor syntax is used to indicate this inline lambda expression is a function of type dynamic &amp;ndash;&amp;gt; dynamic -&amp;gt; dynamic.&lt;/p&gt;
&lt;p&gt;Again, C# does not support defining custom operators for functions, so a &amp;amp;&amp;amp; operator cannot be defined for Boolean type. However, extension method can be defined for Boolean type, also And can be implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;BooleanExtensions
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: green;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;And(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;.And(a)(b);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now And can be used fluently like an infix operator:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CallAnd()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result1 = True.And(True);

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;x = True;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;y = False;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result2 = x.And(y);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Once again, the function name And is only for readability, without refereeing to the function name., the function application (And x y) has to be written as (&amp;lambda;a.&amp;lambda;b.a b (&amp;lambda;t.&amp;lambda;f.f)) x y, and in C#, calling And anonymously works but is also less readable:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CallAnonymousAnd()
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result1 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(a =&amp;gt; b =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;)a(b)(False))(True)(True);

    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;x = True;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;y = False;
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result2 = &lt;/span&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;(a =&amp;gt; b =&amp;gt; (&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;)a(b)(False))(x)(y);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Or is defined as:&lt;/p&gt;
&lt;pre class="code"&gt;Or :=&amp;nbsp; &amp;lambda;a.&amp;lambda;b.a True b&lt;/pre&gt;
&lt;p&gt;When a is True, True True b returns the first argument True; When a is False, False True b returns the second argument b. In C#, this can be viewed as true || b is always true, and false || b is the same as b.&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Or True b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a True b) True b
&amp;equiv; (&amp;lambda;b.True True b) b
&amp;equiv; True True b
&amp;equiv; True
 
  Or False b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a True b) False b
&amp;equiv; (&amp;lambda;b.False True b) b
&amp;equiv; False True b
&amp;equiv; b&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Not is defined as:&lt;/p&gt;
&lt;pre class="code"&gt;Not := &amp;lambda;a.a False True&lt;/pre&gt;
&lt;p&gt;When a is True, True False True returns the first argument False; when a is False, False False True returns the second argument True:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Not True
&amp;equiv; (&amp;lambda;a.a False True) True
&amp;equiv; True False True
&amp;equiv; False
 
  Not False
&amp;equiv; (&amp;lambda;a.a False True) False
&amp;equiv; False False True
&amp;equiv; True&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Xor is defined as:&lt;/p&gt;
&lt;pre class="code"&gt;Xor := &amp;lambda;a.&amp;lambda;b.a (Not b) b&lt;/pre&gt;
&lt;p&gt;When a is True, True (Not b) b returns the first argument Not b; when a is False, True (Not b) b returns the second argument b:&lt;/p&gt;
&lt;pre class="code"&gt;  &lt;span style="color: black;"&gt;Xor True b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a (Not b) b) True b
&amp;equiv; (&amp;lambda;b.True (Not b) b) b
&amp;equiv; True (Not b) b
&amp;equiv; Not b
 
  Xor False b
&amp;equiv; (&amp;lambda;a.&amp;lambda;b.a (Not b) b) True b
&amp;equiv; (&amp;lambda;b.False (Not b) b) b
&amp;equiv; False (Not b) b
&amp;equiv; b&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;These 3 operators can be simply implemented as:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; 
    Or = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;a(True)(b);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; 
    Not = boolean =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;boolean(False)(True);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;
    Xor = a =&amp;gt; b =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;a(Not(b))(b);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Again, they can be wrapped as extension methods too:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;Or(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;.Or(a)(b);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;Not(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;.Not(a);

&lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;Xor(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b) =&amp;gt; &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBoolean&lt;/span&gt;&lt;span style="color: black;"&gt;.Xor(a)(b);&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;Conversion between Church Boolean and System.Boolean&lt;/h1&gt;
&lt;p&gt;It could be intuitive if the Church Boolean function can be directly compared with .NET bool value. The following methods can be defined to convert between them:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchEncoding
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    &lt;/span&gt;&lt;span style="color: green;"&gt;// System.Boolean structure to Boolean function.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;Church(&lt;/span&gt;&lt;span style="color: blue;"&gt;this bool &lt;/span&gt;&lt;span style="color: black;"&gt;boolean) =&amp;gt; boolean ? True : False;

    &lt;/span&gt;&lt;span style="color: green;"&gt;// Boolean function to System.Boolean structure.
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;&lt;span style="color: black;"&gt;Unchurch(&lt;/span&gt;&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;boolean) =&amp;gt; &lt;/span&gt;&lt;span style="color: black;"&gt;boolean(&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)(&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;With the help of conversion, the following code demonstrate how to use the logical operators:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;[&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestClass&lt;/span&gt;&lt;span style="color: black;"&gt;]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;ChurchBooleanTests
&lt;/span&gt;&lt;span style="color: black;"&gt;{
    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;NotTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((!&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.Not());
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((!&lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.Not());
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;AndTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.And(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.And(False));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.And(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;amp;&amp;amp; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.And(False));
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;OrTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;|| &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.Or(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;|| &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.Or(False));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;|| &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.Or(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;|| &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.Or(False));
    }

    [&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;TestMethod&lt;/span&gt;&lt;span style="color: black;"&gt;]
    &lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;&lt;span style="color: black;"&gt;XorTest()
    {
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;^ &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.Xor(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;true &lt;/span&gt;&lt;span style="color: black;"&gt;^ &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), True.Xor(False));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;^ &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.Xor(True));
        &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="color: black;"&gt;.AreEqual((&lt;/span&gt;&lt;span style="color: blue;"&gt;false &lt;/span&gt;&lt;span style="color: black;"&gt;^ &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;).Church(), False.Xor(False));
    }
}&lt;/span&gt;&lt;/pre&gt;
&lt;h1&gt;If&lt;/h1&gt;
&lt;p&gt;The if logic is already built in Church Booleans. Church Booleans is a function that can be applied with 2 argument. If this Church Boolean function is True, the first argument is returned, else the second argument is returned. So naturedly, the following is the If function, which is just a wrapper of Church Boolean function application:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;If := &amp;lambda;b.&amp;lambda;t.&amp;lambda;f.b t f&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The first argument b is a Church Boolean. when b is True, If returns second argument t. When b is False, If returns third argument f. In C#:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// EagerIf = condition =&amp;gt; then =&amp;gt; @else =&amp;gt; condition(then)(@else)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
    EagerIf = condition =&amp;gt; then =&amp;gt; @else =&amp;gt;
        condition    &lt;/span&gt;&lt;span style="color: green;"&gt;// if (condition)
            &lt;/span&gt;&lt;span style="color: black;"&gt;(then)   &lt;/span&gt;&lt;span style="color: green;"&gt;// then { ... }
            &lt;/span&gt;&lt;span style="color: black;"&gt;(@else); &lt;/span&gt;&lt;span style="color: green;"&gt;// else { ... }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There is one issue with this C# implementation. As fore mentioned, C#&amp;rsquo;s reduction strategy is applicative order, when C# function is called, arguments are evaluated, then function is called:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CallEagerIf(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;condition, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result = Eager&lt;/span&gt;&lt;span style="color: black;"&gt;If(condition)
        (a.And(b)) &lt;/span&gt;&lt;span style="color: green;"&gt;// then branch.
        &lt;/span&gt;&lt;span style="color: black;"&gt;(a.Or(b)); &lt;/span&gt;&lt;span style="color: green;"&gt;// else branch.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In this example, disregarding condition is True or False, the then branch a.And(b) and else branch a.Or(b) are both executed. If would be better if one branch is executed for a certain condition. The solution is to make If&amp;rsquo;s second and third arguments of type T to a factory of type Unit&amp;lt;T&amp;gt; &amp;ndash;&amp;gt; T:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// If = condition =&amp;gt; thenFactory =&amp;gt; elseFactory =&amp;gt; condition(thenFactory, elseFactory)(Id)
&lt;/span&gt;&lt;span style="color: blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Unit&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt;&amp;gt;
    If = condition =&amp;gt; thenFactory =&amp;gt; elseFactory =&amp;gt;
        condition
            (thenFactory)
            (elseFactory)(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Functions&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;dynamic&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.Id);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In lambda calculus this is equivalent to:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: black;"&gt;If := &amp;lambda;b.&amp;lambda;t.&amp;lambda;f.b t f Id&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now calling If becomes:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CallLazyIf(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;condition, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result = &lt;/span&gt;&lt;span style="color: black;"&gt;If(condition)
        (id =&amp;gt; a.And(b)) &lt;/span&gt;&lt;span style="color: green;"&gt;// then.
        &lt;/span&gt;&lt;span style="color: black;"&gt;(id =&amp;gt; a.Or(b)); &lt;/span&gt;&lt;span style="color: green;"&gt;// else.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;When condition is True, only a.And(b) is executed. When condition is False, only a.Or(b) is executed. Now the then and else branches are represented by factory functions id =&amp;gt; a.And(b) and id =&amp;gt; a.Or(b), where the id argument is the Id function. This argument usually is not used by the function body, it can be named as _ to indicate &amp;ldquo;don&amp;rsquo;t care&amp;rdquo;:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;internal static void &lt;/span&gt;&lt;span style="color: black;"&gt;CallLazyIf(&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;condition, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;a, &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;b)
{
    &lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Boolean &lt;/span&gt;&lt;span style="color: black;"&gt;result = &lt;/span&gt;&lt;span style="color: black;"&gt;If(condition)
        (_ =&amp;gt; a.And(b)) &lt;/span&gt;&lt;span style="color: green;"&gt;// then.
        &lt;/span&gt;&lt;span style="color: black;"&gt;(_ =&amp;gt; a.Or(b)); &lt;/span&gt;&lt;span style="color: green;"&gt;// else.
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;
&lt;p&gt;&lt;audio style="display: none;" controls="controls"&gt;&lt;/audio&gt;&lt;/p&gt;</description><pubDate>Mon, 04 Nov 2024 13:05:00 GMT</pubDate><guid isPermaLink="true">https://weblogs.asp.net:443/dixin/lambda-calculus-via-c-2-boolean-and-logic</guid><category>LINQ via C#</category><category>C#</category><category>.NET</category><category>Lambda Calculus</category><category>Functional Programming</category><category>Church Encoding</category><category>Church Booleans</category></item></channel></rss>