<html>
<p> Hi everyone! Welcome to the series where I'll be teaching you how to program in Haskell (functional programming part) and how to prove your programs. </p>
<p>Today I'll be talking again about Lists, since list are really important in Haskell.</p>
<p>Today we'll look at how to proove your program that uses recursion over list. Feel free to ask question, since this part is close to mathematics.</p>
<p>We want to proove some properties of our program.</p>
<h2>Induction</h2>
<p>What is induction exactly? We have for example proof by induction over the natural numbers for some statement P(n), where we prove first P(0) and then we can assume that P(n) holds. Next we have to show that P(n+1) holds. And then we can say that P(n) holds for every n.</p>
<p>Basically Induction over lists looks like this:</p>
<ul>
<li>Proof by induction: prove P(xs) for all xs in in [T]</li>
<li>Base case: prove P([]) (emptylist)</li>
<li>Step case: prove P(x:xs) under assumption P(xs) for an arbitrary xs::[T] and x::T
<ul>
<li>Induction hypothesis: xs :: [T], x::T and P(xs)</li>
<li>To prove P(x : xs)</li>
</ul>
</li>
</ul>
<p>Let's go straight to an example:</p>
<pre><code>sumList [] = 0<br>
sumList (x:xs) = x + (sumList xs)<br>
<br>
<br>
double [] = []<br>
double (x:xs) = (2*x) : (double xs)</code></pre>
<p>Proof by induction:<br>
P(xs) := ( (sumList (double xs) = 2 * (sumList xs) )</p>
<p>We want to prove that the sum of the list times two is the same as the every element times two and the summed. Above you see the definition with which we'll work. P(xs) is simply what we want to show. (the whole equation)</p>
<ul>
<li><strong>Base case</strong>: prove P([])<br>
sumList (double []) = sumList [] = 0<br>
2 * sumList [] = 2 * 0 = 0<br>
since both are 0, we can say that P([]) is true since 0=0<br>
</li>
<li><strong>Step case:</strong> P(xs) -> P(x : xs). Let x :: Int and xs :: [Int] be arbitray.
<ul>
<li><strong>Induction hypothesis:<br>
</strong>sumList (double xs) = 2 * sumList xs </li>
<li><strong>To prove:</strong><br>
sumList (double (x : xs)) = 2 * sumList (x : xs)<br>
<br>
sumList (double (x : xs)) = sumList ((2 · x) : double xs)<br>
= 2 · x + sumList (double xs) = 2 · x + 2 · sumList xs<br>
= 2 · (x + sumList xs) = 2 · (sumList (x : xs)) </li>
</ul>
</li>
</ul>
<p>The first we did is we used the definition of double. Then we used the definition of sumList. Then we finally could use our induction hypothesis so we could change the form. General Rule: At one point you'll have to use the induction hypothesis.</p>
<p><br></p>
<p><strong>I hope you enjoyed this quick tutorial on Inductions. Next time we'll dig deeper into types, higher-order functions and some awesome examples. Stay tuned.</strong></p>
<p><strong>If you enjoyed this, take a look at the other tutorials on Functional Programming!</strong></p>
<p><strong>Cheers!</strong></p>
<h2><strong>All Lessons</strong></h2>
<p><a href="https://steemit.com/technology/@besick/1-introduction-to-functional-programming-and-formal-methods">Lesson 1: Introduction to Functional Programming and Formal Methods</a></p>
<p><a href="https://steemit.com/technology/@besick/2-functional-programming-and-formal-methods-lists-part-1">Lesson 2: Haskell Lists Part 1</a> </p>
<p><img src="https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/U5dsXTGKW2WzmUXHpwEhXN96haMv1bN_1680x8400" width="128" height="128"/></p>
</html>