chapter 3 programming with recursion
play

Chapter 3 Programming with Recursion 1. Examples . . . . . . . . . . - PDF document

Ch.3: Programming with Recursion Plan Chapter 3 Programming with Recursion 1. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 2. Correctness . . . . . . . . . . . . . . . . . . . .


  1. Ch.3: Programming with Recursion Plan Chapter 3 Programming with Recursion 1. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 2. Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 3. Construction methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.13 4. Forms of recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.15 3.1 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  2. Ch.3: Programming with Recursion 3.1. Examples 3.1. Examples Factorial (revisited from Section 2.13) Program (fact.sml) fun fact n = if n < 0 then error "fact: negative argument" else if n = 0 then 1 else n ∗ fact (n − 1) Useless test of the error case at each recursive call! Hence we introduce an auxiliary function, and can then use pattern matching in its declaration: fun factAux 0 = 1 | factAux n = n ∗ factAux (n − 1) fun fact1 n = if n < 0 then error "fact1: negative argument" else factAux n In fact1 : pre-condition verification ( defensive programming ) In factAux : no pre-condition verification 3.2 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  3. Ch.3: Programming with Recursion 3.1. Examples Exponentiation Specification function expo x n TYPE: real → int → real PRE: n ≥ 0 POST: x n Construction Error case: n < 0 : produce an error message Base case: n = 0 : return 1 General case: n > 0 : return x n = x ∗ x n − 1 = x ∗ expo x (n − 1) Program (expo.sml) fun expoAux x 0 = 1.0 | expoAux x n = x ∗ expoAux x (n − 1) fun expo x n = if n < 0 then error "expo: negative argument" else expoAux x n 3.3 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  4. Ch.3: Programming with Recursion 3.1. Examples Triangle Specification function triangle a b TYPE: int → int → int PRE: (none) � POST: i a ≤ i ≤ b Construction Base case: a > b : return 0 General case: a ≤ b : � � return i = a + i = a + triangle (a+1) b a ≤ i ≤ b a +1 ≤ i ≤ b Program (triangle.sml) fun triangle a b = if a > b then 0 else a + triangle (a+1) b 3.4 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  5. Ch.3: Programming with Recursion 3.2. Correctness 3.2. Correctness How do we know that a recursive program computes what we want? Lets try a very simple program. fun f n = if n = 0 then 0 else 1 + f(n-1) What does f compute? 3.5 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  6. Ch.3: Programming with Recursion 3.2. Correctness Correctness (cont) What does this function compute? fun f n = if n = 0 then 0 else 1 + f(n-1) Answer: f n = n, if n>=0 Seems reasonable, but how do we prove it? 3.6 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  7. Ch.3: Programming with Recursion 3.2. Correctness Proof by induction For all n > = 0, f ( n ) = n . Base case: n = 0. f ( n ) = 0 = n follows immediately. Induction step: Hypothesis: f ( k ) = k , for all k such that 0 < = k < n We have f ( n ) = 1 + f ( n − 1), by definition 1 + f ( n − 1) = 1 + ( n − 1), by induction hypothesis 1 + ( n − 1) = n , by algebraic transformations Thus f ( n ) = n It follows that f ( n ) = n , for all n > = 0. Question: What can we say about f ( − 1)? 3.7 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  8. Ch.3: Programming with Recursion 3.2. Correctness Another example, slightly harder What does this function compute? fun g(0) = 0 | g(n) = n + g(n) 3.8 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  9. Ch.3: Programming with Recursion 3.2. Correctness Another example (cont) What does this function compute? fun g(0) = 0 | g(n) = n + g(n) Answer: g ( n ) = ( n ∗ n + n ) / 2 Proof by induction. For all n > = 0, g ( n ) = ( n ∗ n + n ) / 2 Base case: n = 0. g ( n ) = ( n ∗ n + n ) / 2 by definition ( n ∗ n + n ) / 2 = (0 ∗ 0 + 0) / 2 = 0 Induction step Hypothesis: g ( k ) = ( k ∗ k + k ) / 2, for all k such that 0 < = k < n We have g ( n ) = n + g ( n − 1), by definition = n + (( n − 1) ∗ ( n − 1) + ( n − 1)) / 2, by induction hypothesis = n + ( n ∗ n − 2 ∗ n + 1 + n − 1) / 2, by algebraic transformations = n + ( n ∗ n − n ) / 2 − n , simplification = ( n ∗ n + n ) / 2 Thus g ( n ) = ( n ∗ n + n ) / 2 It follows that g ( n ) = ( n ∗ n + n ) / 2, for all n > = 0. 3.9 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  10. Ch.3: Programming with Recursion 3.2. Correctness If you think these examples where too simple Exercises: • Prove that the factorial given earlier produces the intended results. • Prove the same for the gcd function. What do the functions below compute? Make an educated guess (try on an SML system if you like) and show by induction that your guess was correct. fun h(n) = if n = 0 then 0 else h(n-1)+2*n-1; fun m(a,b) = if a = 0 then 0 else b+m(a-1,b); 3.10 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  11. Ch.3: Programming with Recursion 3.2. Correctness Correctness of functional programs Suppose we have a recursive function fun f(x) = ... f(..) ... and we want to show that it satisfies some property P ( x, f ( x )) Solution: • Show that the functions terminates (for the values of x we are interested in) • Assume that all recursive calls satisfy the property P ( . . . , f ( . . . )). Show P ( x, f ( x )) . Then we have shown that P ( x, f ( x )) holds for all values of x we are interested in. (This is just an induction proof in disguise.) (By the way, showing that if an answer is returned it will be correct is also known as partial correctness .) 3.11 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  12. Ch.3: Programming with Recursion 3.2. Correctness Example (factorial): fun fac(x) = if x = 0 then 1 else x*fac(x-1) Assume that fac(x) terminates for all x > = 0. Show that P ( x, fac ( x )) < = > fac ( x ) = 1 ∗ 2 ∗ . . . ∗ x holds. Assuming property holds for recursive calls... By the definition of the function fac we have fac ( x ) = if x = 1 then 0 else x ∗ (1 ∗ 2 ∗ . . . ∗ ( x − 1)) For x = 0 the property is obvious. For x <> 0 we prove the property by algebraic manipulations. 3.12 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  13. Ch.3: Programming with Recursion 3.3. Construction methodology 3.3. Construction methodology Objective Construction of an SML program computing the function: f ( x ) : D → R given its specification S Methodology 1. Choice of a variant A case analysis is done on a numeric variant expression: let an expression a of type integer be the chosen variant, and let an integer b be the lower bound of a . 2. Handling of the error cases What if a < b ? Defensive programming: raise an exception Otherwise: assume the caller established the pre-condition 3.13 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  14. Ch.3: Programming with Recursion 3.3. Construction methodology 3. Handling of the base cases For all the minimal values of a , directly (without recursion) express the result in terms of x 4. Handling of the general case When a has a non-minimal value, investigate how the results of one or more recursive calls can be combined with the argument so as to obtain the desired overall result, such that: 1. Recursive calls are on a ′ , of type A , such that a ′ < a 2. Recursive calls satisfy the pre-condition of S State all this via an expression computing the result Correctness If a program is constructed using this methodology, then it is correct with respect to its specification (as long as the cases are correctly expressed) This methodology makes the following hypotheses: • The general case can be expressed using recursion • The resolution of the problem does not involve unspecified and/or unimplemented auxiliary problems • The possible values of a have a lower bound 3.14 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  15. Ch.3: Programming with Recursion 3.4. Forms of recursion 3.4. Forms of recursion Up to now: • One recursive call • Some variant is decremented by one That is: simple recursion (construction process by simple induction) Forms of recursion • Simple recursion • Complete recursion • Multiple recursion • Mutual recursion • Nested recursion • Recursion on a generalised problem 3.15 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  16. Ch.3: Programming with Recursion 3.4. Forms of recursion Complete recursion Example 1: integer division (quotient and remainder) Specification function intDiv a b TYPE: int → int → (int ∗ int) PRE: a ≥ 0 and b > 0 POST: (q,r) such that a = q ∗ b + r and 0 ≤ r < b Construction Variant: a Error case: a < 0 or b ≤ 0 : produce an error message Base case: a < b : since a = 0 ∗ b + a , return (0,a) This covers more than the minimal value of a (namely 0)! General case: a ≥ b : since a = q ∗ b + r iff (a − b) = (q − 1) ∗ b + r , the call intDiv (a − b) b will give q − 1 and r 3.16 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

  17. Ch.3: Programming with Recursion 3.4. Forms of recursion Program (intDiv.sml) fun intDivAux a b = if a < b then (0,a) else let val (q1,r1) = intDivAux (a − b) b in (q1+1,r1) end fun intDiv a b = if a < 0 orelse b <= 0 then error "intDiv: invalid argument" else intDivAux a b Necessity of the induction hypothesis not only for a − 1 , but actually for all values less than a : complete induction! 3.17 Sven-Olof Nystr¨ om/IT Dept/Uppsala University FP

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend