create account

#3 Functional Programming: Induction over list by besick

View this thread on: hive.blogpeakd.comecency.com
· @besick · (edited)
$0.02
#3 Functional Programming: Induction over list
<html>
<p>&nbsp;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.&nbsp;</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) &nbsp;&nbsp;&nbsp;)</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) -&gt; P(x : xs). Let x :: Int and xs :: [Int] be arbitray.
    <ul>
      <li><strong>Induction hypothesis:<br>
</strong>sumList (double xs) = 2 * sumList xs&nbsp;</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))&nbsp;</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. &nbsp;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> &nbsp;</p>
<p><img src="https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/U5dsXTGKW2WzmUXHpwEhXN96haMv1bN_1680x8400" width="128" height="128"/></p>
</html>
👍  , ,
properties (23)
authorbesick
permlinkfunctional-programming-induction-over-list
categorytechnology
json_metadata{"tags":["technology","programming","science","education","teaching"],"links":["https://steemit.com/technology/@besick/1-introduction-to-functional-programming-and-formal-methods","https://steemit.com/technology/@besick/2-functional-programming-and-formal-methods-lists-part-1"],"app":"steemit/0.1","format":"html","image":["https://steemit-production-imageproxy-thumbnail.s3.amazonaws.com/U5dsXTGKW2WzmUXHpwEhXN96haMv1bN_1680x8400"]}
created2017-12-11 23:14:45
last_update2017-12-12 22:40:48
depth0
children1
last_payout2017-12-18 23:14:45
cashout_time1969-12-31 23:59:59
total_payout_value0.022 HBD
curator_payout_value0.002 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,516
author_reputation475,406,238,551
root_title"#3 Functional Programming: Induction over list"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id23,169,484
net_rshares5,766,918,705
author_curate_reward""
vote details (3)
@woz.software ·
Can't wait for your attempt to explain Monads

Not a burrito! :)
👍  
properties (23)
authorwoz.software
permlinkre-besick-functional-programming-induction-over-list-20171212t055937478z
categorytechnology
json_metadata{"tags":["technology"],"app":"steemit/0.1"}
created2017-12-12 05:59:39
last_update2017-12-12 05:59:39
depth1
children0
last_payout2017-12-19 05:59:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation2,321,910,395,519
root_title"#3 Functional Programming: Induction over list"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,200,266
net_rshares1,147,241,915
author_curate_reward""
vote details (1)