More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.6 1 TexPoint fonts used in EMF. Read the TexPoint manual before.

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
Binary Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Stateful Objects and Stable Identities CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.2 © Mitchell Wand, This work is licensed under a.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Converting from Immutable to Mutable Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.4 © Mitchell Wand, This work is licensed under.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
Contracts, Purpose Statements, Examples and Tests
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
The Iterative Design Recipe
Rewriting your function using map and foldr
Examining Two Pieces of Data
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License

Lesson Introduction In this lesson, we’ll show an example of how invariants can be used to improve a linear search. The transformation we’ll use is called “reduction in strength”, and is a well-known algorithm that compilers use to improve the code in a loop. 2

Another example: Integer Square Root int-sqrt : Nat -> Nat GIVEN: n, RETURNS: z such that z² ≤ n < (z+1)² examples: (int-sqrt 25) = 5 (int-sqrt 26) = 5... (int-sqrt 35) = 5 (int-sqrt 36) = 6 3 This is one of my favorite examples.

Video Demonstration Watch the video demonstration at vUApE vUApE Note: the video is a little out of date: – it talks about accumulators instead of context arguments – the purpose statements are not always up to our current coding standards – sorry about that. Below are the slides from the video, slightly updated. 4

int-sqrt.v0 ;; STRATEGY: Call more general function (define (int-sqrt.v0 n) (linear-search 0 n (lambda (z) (< n (sqr (+ z 1)))))) 5

int-sqrt.v1 (define (int-sqrt.v1 n) (local ((define (inner-loop z) ;; PURPOSE: Returns int-sqrt(n) ;; WHERE z² ≤ n ;; HALTING MEASURE (- n z) (cond [(< n (sqr (+ z 1))) z] [else (inner-loop (+ z 1))]))) (inner-loop 0))) 6 invariant guarantees that the halting measure is non- negative we just checked that (z+1)² ≤ n, so calling inner-loop with z+1 satisfies the invariant.

A picture of this invariant 7 0Nz INVARIANT: z² ≤ n all these numbers also have squares that are ≤ n

What happens at the recursive call? 8 0Nz INVARIANT: z² ≤ n all these numbers also have squares that are ≤ n CONDITION: (z+1)² ≤ n 0Nz_new = z+1 0Nz INVARIANT z² ≤ n: true again for the new value of z.

Improving this code Don't like to do sqr at every step, so let's keep the value of (sqr (+ z 1)) in a context argument, which we'll call u. Compute new value of u as follows: z' = (z+1) u' = (z'+1)*(z'+1) = ((z+1)+1)*((z+1)+1) = (z+1)² + 2(z+1) + 1 = u + 2z + 3 9

Function Definition (define (int-sqrt.v2 n) (local ((define (inner-loop z u) ;; PURPOSE: Returns int-sqrt(n) ;; WHERE z² ≤ n ;; AND u = (z+1)² ;; HALTING MEASURE (- n z) (cond [(< n u) z] [else (inner-loop (+ 1 z) (+ u (* 2 z) 3))]))) (inner-loop 0 1))) 10 the inner loop finds the answer for the whole function update context argument to maintain the invariant initialize context argument to make the invariant true u = (z+1)²

Let's do it one more time Add invariant: v = 2*z+3 z' = z+1 v' = 2*z'+ 3 = 2*(z+1) + 3 = 2*z = (2*z + 3) + 2 = v

Function Definition (define (int-sqrt.v3 n) (local ((define (inner-loop z u v) ;; PURPOSE: Returns int-sqrt(n) ;; WHERE z^2 ≤ n ;; AND u = (z+1)^2 ;; AND v = 2*z+3 ;; HALTING MEASURE (- n z) (cond [(< n u) z] [else (inner-loop (+ 1 z) (+ u v) (+ v 2))]))) (inner-loop 0 1 3))) 12 v = 2z+3 u = (z+1)² You could never understand this program if I hadn't written down the invariants!

Lesson Summary We’ve seen how invariants can be used to improve the code of a linear search or loop. We’ve seen how invariants can be used to explain the “reduction-in-strength” optimization. We’ve seen how invariants can be used to explain an otherwise-obscure piece of code. 13

Next Steps Study the file 08-8-square-roots.rkt in the Examples folder. Do the Guided Practices If you have questions about this lesson, ask them on the Discussion Board Go on to the next lesson 14