1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
ISBN Chapter 3 Describing Syntax and Semantics.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 2: Algorithm Discovery and Design
מבוא מורחב 1 Lecture 3 Material in the textbook Sections to
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Describing Syntax and Semantics
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
analysis, plug ‘n’ chug, & induction
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Lecture 9. Arithmetic and geometric series and mathematical induction
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
MATH 224 – Discrete Mathematics
CSE 311: Foundations of Computing Fall 2013 Lecture 8: More Proofs.
Chapter 4: Induction and Recursion
מבוא מורחב 1 Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
Mathematical Induction I Lecture 4: Sep 16. This Lecture Last time we have discussed different proof techniques. This time we will focus on probably the.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion.
Principles Of Programming Languages Lecture 2 Outline Design-By-Contract Iteration vs. Recursion Scope and binding High-order procedures.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
INDUCTION Slides of Ken Birman, Cornell University.
Quiz 7 The way I expected it.. How to do it! 1. Let P(n) be the statement that n 2 = n(n + 1)(2n + 1)/6 for the positive integer n. Be.
Mathematical Induction I Lecture 5: Sep 20 (chapter of the textbook and chapter of the course notes)
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
INDUCTION Lecture 22 CS2110 – Fall 2009 A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described.
CS314 – Section 5 Recitation 9
Chapter 4: Induction and Recursion
Operational Semantics of Scheme
CS314 – Section 5 Recitation 10
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Induction and Recursion
Mathematical Induction Recursion
CS21b: Structure and Interpretation
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Functional Programming
This Lecture Substitution model
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Applied Discrete Mathematics Week 9: Integer Properties
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Material in the textbook Sections to 1.2.1
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Recursion.
Lecture 6 - Recursion.
Presentation transcript:

1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that our code works

2/32 A way to figure out what happens during evaluation Not really what happens in the computer Rules of substitution model: 1.If self-evaluating (e.g. number, string, #t / #f), just return value 2.If name, replace it with value associated with that name 3.If lambda, create a procedure 4.If special form (e.g. if), follow the special form’s rules for evaluating 5.If combination (e 0 e 1 e 2 … e n ): Evaluate subexpressions e i in any order to produce values (v 0 v 1 v 2 … v n ) If v 0 is primitive procedure (e.g. +), just apply it to v 1 … v n If v 0 is compound procedure (created by lambda): –Substitute v 1 … v n for corresponding parameters in body of procedure, then repeat on body Substitution model

3/32 (define average (lambda (x y)(/ (+ x y) 2))) (average (+ 3 4) 3) (5) Micro Quiz Rules of substitution model 1.If self-evaluating (e.g. number, string, #t / #f), just return value 2.If name, replace it with value associated with that name 3.If lambda, create a procedure 4.If special form (e.g. if), follow the special form’s rules for evaluating 5.If combination (e 0 e 1 e 2 … e n ): Evaluate subexpressions e i in any order to produce values (v 0 v 1 v 2 … v n ) If v 0 is primitive procedure (e.g. +), just apply it to v 1 … v n If v 0 is compound procedure (created by lambda): –Substitute v 1 … v n for corresponding parameters in body of procedure, then repeat on body

4/32 Substitution model – a simple example (define square (lambda (x) (* x x))) (square 4) square  [procedure (x) (* x x)] 4  4 (* 4 4) 16 (define average (lambda (x y) (/ (+ x y) 2))) (average 5 (square 3)) (average 5 (* 3 3)) (average 5 9) (/ (+ 5 9) 2) (/ 14 2) 7

5/32 A less trivial example: factorial Compute n factorial, defined as n! = n(n-1)(n-2)(n-3)...1 How can we capture this in a procedure, using the idea of finding a common pattern?

6/32 How to design recursive algorithms Follow the general approach: 1. Wishful thinking 2. Decompose the problem 3. Identify non-decomposable (smallest) problems 1. Wishful thinking Assume the desired procedure exists. Want to implement fact? OK, assume it exists. BUT, it only solves a smaller version of the problem. –This is just like finding a common pattern: but here, solving the bigger problem involves the same pattern in a smaller problem

7/32 2. Decompose the problem Solve a problem by 1. solve a smaller instance (using wishful thinking) 2. convert that solution to the desired solution Step 2 requires creativity! Must design the strategy before writing Scheme code. n! = n(n-1)(n-2)... = n[(n-1)(n-2)...] = n * (n-1)! solve the smaller instance, multiply it by n to get solution (define fact (lambda (n) (* n (fact (- n 1)))))

8/32 Minor Difficulty (define fact (lambda (n) (* n (fact (- n 1))))) (fact 2) (* 2 (fact 1)) (* 2 (* 1 (fact 0))) (* 2 (* 1 (* 0 (fact -1)))) … d’oh!

9/32 3. Identify non-decomposable problems Decomposing is not enough by itself Must identify the "smallest" problems and solve directly Define 1! = 1 (or alternatively define 0! = 1) (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)))))

10/32 General form of recursive algorithms test, base case, recursive case (define fact (lambda (n) (if (= n 1) ; test for base case 1 ; base case (* n (fact (- n 1))))) ; recursive case base case: smallest (non-decomposable) problem recursive case: larger (decomposable) problem more complex algorithms may have multiple base cases or multiple recursive cases (requiring more than one test)

11/32 Summary of recursive processes Design a recursive algorithm by 1. wishful thinking 2. decompose the problem 3. identify non-decomposable (smallest) problems Recursive algorithms have 1. test 2. base case 3. recursive case

12/32 (fact 3) (if (= 3 1) 1 (* 3 (fact (- 3 1)))) (if #f 1 (* 3 (fact (- 3 1)))) (* 3 (fact (- 3 1))) (* 3 (fact 2)) (* 3 (if (= 2 1) 1 (* 2 (fact (- 2 1))))) (* 3 (if #f 1 (* 2 (fact (- 2 1))))) (* 3 (* 2 (fact (- 2 1)))) (* 3 (* 2 (fact 1))) (* 3 (* 2 (if (= 1 1) 1 (* 1 (fact (- 1 1)))))) (* 3 (* 2 (if #t 1 (* 1 (fact (- 1 1)))))) (* 3 (* 2 1)) (* 3 2) 6 (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))

13/32 (fact 3) (* 3 (fact 2)) (* 3 (* 2 (fact 1))) (* 3 (* 2 1)) (* 3 2) 6 Note the “shape” of this process (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))

14/32 The fact procedure uses a recursive algorithm For a recursive algorithm: In the substitution model, the expression keeps growing (fact 3) (* 3 (fact 2)) (* 3 (* 2 (fact 1)))

15/32 Recursive algorithms use increasing space In a recursive algorithm, bigger operands consume more space (fact 4) (* 4 (fact 3)) (* 4 (* 3 (fact 2))) (* 4 (* 3 (* 2 (fact 1)))) (* 4 (* 3 (* 2 1))) (fact 8) (* 8 (fact 7)) (* 8 (* 7 (fact 6))) (* 8 (* 7 (* 6 (fact 5))))... (* 8 (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 (fact 1)))))))) (* 8 (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 1)))))) (* 8 (* 7 (* 6 (* 5 (* 4 (* 3 2)))))

16/32 A Problem With Recursive Algorithms Try computing 101! 101 * 100 * 99 * 98 * 97 * 96 * … * 2 * 1 How much space do we consume with pending operations? Better idea: start with 1, remember that 2 is next compute 1 * 2, remember that 3 is next compute 2 * 3, remember that 4 is next compute 6 * 4, remember that 5 is next … compute , and stop This is an iterative algorithm – it uses constant space

17/32 Iterative algorithm to compute 4! as a table In this table: One column for each piece of information used One row for each step product i product * i i +1 4 n4444n The answer is in the product column of the last row answer The last row is the one where i > n Next to computeCurrent value first row handles 0! cleanly

18/32 Iterative factorial in scheme (define ifact (lambda (n) (ifact-helper 1 1 n))) (define ifact-helper (lambda (product i n) (if (> i n) product (ifact-helper (* product i) (+ i 1) n)) ) ) initial row of table compute next row of table answer is in product column of last row at last row when i > n

19/32 Partial trace for (ifact 4) (define ifact-helper (lambda (product i n) (if (> i n) product (ifact-helper (* product i) (+ i 1) n)))) (if (> 2 4) 1 (ifact-helper (* 1 2) (+ 2 1) 4)) (ifact-helper 2 3 4) (if (> 3 4) 2 (ifact-helper (* 2 3) (+ 3 1) 4)) (ifact-helper 6 4 4) (if (> 4 4) 6 (ifact-helper (* 6 4) (+ 4 1) 4)) (ifact-helper ) (if (> 5 4) 24 (ifact-helper (* 24 5) (+ 5 1) 4)) 24 (ifact-helper 1 2 4) (if (> 1 4) 1 (ifact-helper (* 1 1) (+ 1 1) 4)) (ifact 4) (ifact-helper 1 1 4)

20/32 Partial trace for (ifact 4) (define ifact-helper (lambda (product i n) (if (> i n) product (ifact-helper (* product i) (+ i 1) n)))) (ifact-helper 2 3 4) (ifact-helper 6 4 4) (ifact-helper ) 24 (ifact-helper 1 2 4) (ifact 4) (ifact-helper 1 1 4) Note the “shape” of this process

21/32 Recursive process = pending operations when procedure calls itself Recursive factorial: (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1)) ) ))) (fact 4) (* 4 (fact 3)) (* 4 (* 3 (fact 2))) (* 4 (* 3 (* 2 (fact 1)))) Pending operations make the expression grow continuously pending operation

22/32 Iterative process = no pending operations Iterative factorial: (define ifact-helper (lambda (product i n) (if (> count n) product (ifact-helper (* product i) (+ i 1) n)))) (ifact-helper 1 1 4) (ifact-helper 1 2 4) (ifact-helper 2 3 4) (ifact-helper 6 4 4) (ifact-helper ) Fixed space because no pending operations no pending operations

23/32 Summary of iterative processes Iterative algorithms use constant space How to develop an iterative algorithm 1. Figure out a way to accumulate partial answers 2. Write out a table to analyze precisely: –initialization of first row –update rules for other rows –how to know when to stop 3. Translate rules into Scheme code Iterative algorithms have no pending operations when the procedure calls itself

24/32 Why is our code correct? How do we know that our code will always work? Proof by authority – someone with whom we dare not disagree says it is right! For example Proof by statistics – we try enough examples to convince ourselves that it will always work! E.g. keep trying, but bring sandwiches and a cot Proof by faith – we really, really, really believe that we always write correct code! E.g. the Pset is due in 5 minutes and I don’t have time Formal proof – we break down and use mathematical logic to determine that code is correct.

25/32 Proof by induction Proof by induction is a very powerful tool in predicate logic Informally, if you can: 1.Show that some proposition P is true for n=0 2.Show that whenever P is true for some legal value of n, then it follows that P is true for n+1 …then you can conclude that P is true for all legal values of n

26/32 A simple example 1 = = = = 15 …

27/32 An example of proof by induction Base case: Inductive step:

28/32 Steps in proof by induction 1.Define the predicate P(n) (induction hypothesis) Decide what the variable n denotes Decide the universe over which n applies 2.Prove that P(0) is true (base case) 3.Prove that P(n) implies P(n+1) for all n (inductive step) Do this by assuming that P(n) is true, then trying to prove that P(n+1) is true 4.Conclude that P(n) is true for all n by the principle of induction.

29/32 Back to factorial Induction hypothesis P(n): “our recursive procedure for fact correctly computes n! for all integer values of n, starting at 1” (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))

30/32 Proof by induction that fact works Base case: does this work when n=1? Note that this is P(1), not P(0) – we need to adjust the base case because our universe of legal values for n includes only the positive integers Yes – the IF statement guarantees that in this case we only evaluate the consequent expression: thus we return 1, which is 1! (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))

31/32 Proof by induction that fact works Inductive step: We assume it works for some legal value of n > 0… so (fact n) computes n! correctly … and show that it works correctly for n+1 What does (fact n+1) compute? Use the substitution model: (fact n+1) (if (= n+1 1) 1 (* n+1 (fact (- n+1 1)))) (if #f 1 (* n+1 (fact (- n+1 1)))) (* n+1 (fact (- n+1 1))) (* n+1 (fact n)) (* n+1 n!) (n+1)! By induction, fact will always compute what we expected, provided the input is in the right range (n > 0)

32/32 Lessons learned Induction provides the basis for supporting recursive procedure definitions In designing procedures, we should rely on the same thought process Find the base case, and create solution Determine how to reduce to a simpler version of same problem, plus some additional operations Assume code will work for simpler problem, and design solution to extended problem