1 / 13

A simple strategy for finding invariants

Learn a general strategy for developing correct programs by construction using loop invariants from a recurrence relation.

bfitzgerald
Download Presentation

A simple strategy for finding invariants

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A simple strategy forfinding invariants Maximum product subarray Octobre 2018, Pierre-Edouard Portier http://peportier.me/teaching_2018_2019/

  2. denotes all adjacent elements in array with . It is called a segment (or subarray) of the array. When , is an empty segment.

  3. denotes a quantifier. To each quantifier is associated an associative and commutative binary operator . For example, the quantifiers for binary operators are . is a bounded variable with its range, and a function. For example, .

  4. Here are a few useful rules that can be used with quantifiers: • Cartesian product: • Range split: • Singleton: • Range disjunction, if is idempotent, we have: • Associativity and commutativity

  5. A very general strategy, proposed by XueJinyun[1], for developing • programs correct by construction: • Formulate as precisely as possible what the algorithm should do. • Partition the problem into sub-problems with the same structure as • the original one. • Formulate the algorithm as a recurrence relation. • Develop loop invariants from the recurrence relation to transform • the algorithm into a program. [1] http://dblp.uni-trier.de/pers/hd/x/Xue:Jinyun

  6. Given an array of integers, compute . NB. For , (the identity element of )

  7. We look for a function such that:

  8. Can be a function of ?

  9. Can be a function of .

  10. Now, we wish to express as a function of .

  11. We introduced 3 computations: , and . By associating variables to each of them, we obtain a straightforward invariant: can be trivially satisfied with the initialization: i,p,x,y := 1,1,1,1

  12. The recurrence relation tells us what to do in the loop to maintain . We obtain the following program: i,p,x,y := 1,1,1,1; do in  if a(i-1) >0 x,y =: x*a(i-1), min(y*a(i-1), 1) |a(i-1)==0 x,y =: 1, 0 |a(i-1) <0 x,y =: max(y*a(i-1), 1), x*a(i-1) fi p =: max(p,x); i =: i+1; od

  13. By a change of variable, we can rewrite this program as: i,p,x,y := 0,1,1,1; do in  if a(i)>0 x,y =: x*a(i), min(y*a(i), 1) |a(i)==0 x,y =: 1, 0 |a(i)<0 x,y =: max(y*a(i), 1), x*a(i) fi p =: max(p,x); i =: i+1; od

More Related