<div class="gmail_quote">Roger,</div><div class="gmail_quote"><br><div class="im">&gt;  Scientists look for recurring patterns in nature. Once a pattern is discovered, its essential ingredients are identified and formalized into a law or mathematical equation.  <div>

<br></div></div><div>You could think of lambda calculus as a general language for formalizing and applying patterns. Squint enough and pinning down a pattern starts to look like lambda abstraction; using the pattern, function application.</div>

<div class="im"><div><br></div><div>&gt; Here is some intuition on why it is called the fold function: Imagine folding a towel.</div><div><br></div></div><div>Generally we don&#39;t look for insight in the layperson meaning of technical terminology. Sure, folding a list is intuitively like folding a long strip of paper. But folds also apply to trees and even gnarlier datatypes, and there intuition loses steam.</div>

<div><br></div><div>Sometimes it&#39;s better to internally alpha-rename to a fresh variable to avoid being misled. The meaning lies in the *usage* of the term, so in a way it&#39;s not really in English after all.</div>
<div class="im">
<div><br></div><div><span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">&gt; We saw that the fold function can be defined for the Nat data type. In fact, a suitable fold function can be defined for every recursive data type.</span><br style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">

</div><div><br></div></div><div>How would you define a fold for</div><div><br></div><div>data S a = S (a -&gt; Bool)</div><div><br></div><div>?</div><span class="HOEnZb"><font color="#888888"><div><br></div><div><br></div>

</font></span><div><span class="HOEnZb"><font color="#888888">-- Kim-Ee<br><br><br></font></span><div class="gmail_quote"><div class="im">On Sat, Sep 15, 2012 at 3:36 PM, Costello, Roger L. <span dir="ltr">&lt;<a href="mailto:costello@mitre.org" target="_blank">costello@mitre.org</a>&gt;</span> wrote:<br>

</div><div><div class="h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi Folks,<br><br>This is a story about discovery. It is a story about advancing the state-of-the-art by discovering patterns in functions.<br>

<br>Scientists look for recurring patterns in nature. Once a pattern is discovered, its essential ingredients are identified and formalized into a law or mathematical equation. Discovery of a pattern is important and those individuals who make such discoveries are rightfully acknowledged in our annals of science.<br>

<br>Scientists are not the only ones who look for recurring patterns, so do functional programmers. Once a pattern is discovered, its essential ingredients are identified and formalized into a function. That function may then become widely adopted by the programming community, thus elevating the programming community to a new level of capability.<br>

<br>The following is a fantastic example of discovering a pattern in multiple functions, discerning the essential ingredients of the pattern, and replacing the multiple functions with a single superior function. The example is from Richard Bird&#39;s book, Introduction to Functional Programming using Haskell.<br>

<br>Before looking at the example let&#39;s introduce an important concept in functional programming: partial function application.<br><br>A function that takes two arguments may be rewritten as a function that takes one argument and returns a function. The function returned also takes one argument. For example, consider the min function which returns the minimum of two integers:<br>

<br>min 2 3         -- returns 2<br><br>Notice that min is a function that takes two arguments.<br><br>Suppose min is given only one argument:<br><br>min 2<br><br>[Definition: When fewer arguments are given to a function than it can accept it is called partial application of the function.  That is, the function is partially applied.]<br>

<br>min 2 is a function. It takes one argument. It returns the minimum of the argument and 2. For example:<br><br>(min 2) 3       -- returns 2<br><br>To see this more starkly, let&#39;s assign g to be the function min 2:<br>

<br>let g = min 2<br><br>g is now a function that takes one argument and returns the minimum of the argument and 2:<br><br>g 3     -- returns 2<br><br>Let&#39;s take a second example: the addition operator (+) is a function and it takes two arguments. For example:<br>

<br>2 + 3   -- returns 5<br><br>To make it more obvious that (+) is a function, here is the same example in prefix notation:<br><br>(+) 2 3 -- returns 5<br><br>Suppose we provide (+) only one argument:<br><br>(+) 2<br><br>

That is a partial application of the (+) function.<br><br>(+) 2 is a function. It takes one argument. It returns the sum of the argument and 2. For example:<br><br>((+) 2) 3       -- returns 5<br><br>We can succinctly express (+) 2 as:<br>

<br>(+2)<br><br>Thus,<br><br>(+2) 3          -- returns 5<br><br>Okay, now we are ready to embark on our journey of discovery. We will examine three functions and find their common pattern.<br><br>The functions process values from this recursive data type:<br>

<br>data Nat = Zero | Succ Nat<br><br>Here are some examples of Nat values:<br><br>Zero, Succ Zero, Succ (Succ Zero), Succ (Succ (Succ Zero)), ...<br><br>The following functions perform addition, multiplication, and exponentiation on Nat values. Examine the three functions. Can you discern a pattern?<br>

<br>-- Addition of Nat values:<br>m + Zero = m<br>m + Succ n = Succ(m + n)<br><br>-- Multiplication of Nat values:<br>m * Zero = Zero<br>m * Succ n = (m * n) + m<br><br>-- Exponentiation of Nat values:<br>m ** Zero = Succ Zero<br>

m ** Succ n = (m ** n) * m<br><br>For each function the right-hand operand is either Zero or Succ n:<br><br>(+m) Zero<br>(+m) Succ n<br><br>(*m) Zero<br>(*m) Succ n<br><br>(**m) Zero<br>(**m) Succ n<br><br>So abstractly there is a function f that takes as argument either Zero or Succ n:<br>

<br>f Zero<br>f Succ n<br><br>Given Zero as the argument, each function immediately returns a value:<br><br>(+m)  Zero      = m<br>(*m)  Zero      = Zero<br>(**m) Zero      = Succ Zero<br><br>Let c denote the value.<br><br>

So, invoke f with a value for c and Zero:<br><br>f c Zero = c<br><br>For addition provide m as the value for c:<br><br>m + Zero = f m Zero<br><br>For multiplication provide Zero as the value for c:<br><br>m * Zero = f Zero Zero<br>

<br>For exponentiation provide (Succ Zero) as the value for c:<br><br>m ** Zero = f (Succ Zero) Zero<br><br>Next, consider how each of the functions (addition, multiplication, exponentiation) deals with Succ n.<br><br>Given the argument Succ n,  each function processes n and then applies a function to the result:<br>

<br>(+m)  Succ n    = (Succ)((+m) n)<br>(*m)  Succ n    = ((*m) n) (+m)<br>(**m) Succ n    = ((**m) n) (*m)<br><br>Let h denote the function that is applied to the result.<br><br>So, Succ n is processed by invoking f with a value for h and Succ n:<br>

<br>f h (Succ n) = h (f n)<br><br>For multiplication provide (+m) as the value for h:<br><br>m * Succ n = f (+m) (Succ n)<br><br>Recall that (+m) is a function: it takes one argument and returns the sum of the argument and m.<br>

<br>Thus one of the arguments of function f is a function.<br><br>f is said to be a higher order function.<br><br>[Definition: A higher order function is a function that takes an argument that is a function or it returns a function.]<br>

<br>Continuing on with the other values for h:<br><br>For addition provide (Succ) as the value for h:<br><br>m + Succ n = f (Succ) (Succ n)<br><br>For exponentiation provide (*m) as the value for h:<br><br>m ** Zero = f (*m) (Succ n)<br>

<br>Recap: to process Nat values use these equations:<br><br>f c Zero = c<br>f h (Succ n) = h (f n)<br><br>Actually we need to supply f with h, c, and the Nat value:<br><br>f h c Zero = c<br>f h c (Succ n) = h (f h c n)<br>

<br>f has this type signature: its arguments are a function, a value, and a Nat value; the result is a value. So this is f&#39;s type signature:<br><br>f :: function -&gt; value -&gt; Nat -&gt; value<br><br>That type signature is well-known in functional programming. It is the signature of a fold function. Here is some intuition on why it is called the fold function:<br>

<br>Imagine folding a towel. You make your first fold. The second fold builds on top of the first fold. Each fold builds on top of the previous folds. That is analogous to what the function f does. &quot;You make your first fold&quot; is analogous to immediately returning c:<br>

<br>f h c Zero = c<br><br>&quot;Each fold builds on top of the previous folds&quot; is analogous to processing Succ n by applying h to the result of processing the n&#39;th Nat value:<br><br>f h c (Succ n) = h (f h c n)<br>

<br>Recap: In analyzing the functions (addition, multiplication, exponentiation) on Nat we have discovered that they are all doing a fold operation.<br><br>That is a remarkable discovery.<br><br>Let&#39;s now see how to perform addition, multiplication, and exponentiation using f. However, since we now recognize that f is actually the fold function, let&#39;s rename it foldn (fold Nat). Here is its type signature and function definition:<br>

<br>foldn                   :: (a -&gt; a) -&gt; a -&gt; Nat -&gt; a<br>foldn h c Zero          =  c<br>foldn h c (Succ n)      =  h (foldn h c n)<br><br>The functions are implemented as:<br><br>m + n                   = foldn Succ m n<br>

m * n                   = foldn (+m) Zero n<br>m ** n                  = foldn (*m) (Succ Zero) n<br><br>Let&#39;s take an example and trace its execution:<br><br>Zero + Succ Zero<br>        = foldn Succ Zero (Succ Zero)<br>

        = Succ (foldn Succ Zero Zero)<br>        = Succ (Zero)<br><br>The holy trinity of functional programming is:<br><br>1.  User-defined recursive data types.<br>2.  Recursively defined functions over recursive data types.<br>

3.  Proof by induction: show that some property P(n) holds for each element of a recursive data type.<br><br>Nat is a user-defined recursive data type. The addition, multiplication, and exponentiation functions are recursively defined functions over Nat.<br>

<br>We saw that the fold function can be defined for the Nat data type. In fact, a suitable fold function can be defined for every recursive data type.<br><br>Wow!<br><br>There are two advantages of writing recursive definitions in terms of fold:<br>

<br>1.  The definitions are shorted; rather than having to write down two equations, we have only to write down one.<br>2.  It is possible to prove general properties of fold and use them to prove properties of specific instantiations. In other words, rather than having to write down many inductive proofs, we have only to write down one.<br>

<br>Recap: we have approached programming like a scientist; namely, we have examined a set of algorithms (the addition, multiplication, and exponentiation functions), discovered that they have a recurring pattern, and then evolved the algorithms to a higher order algorithm. That is advancing the field. Incremental advancements such as this is what takes Computer Science to new heights.<br>

<br>/Roger<br><br>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>

</blockquote></div></div></div><br></div></div><br class="Apple-interchange-newline">