1 / 6

Axiomatic semantics - II

Axiomatic semantics - II. We reviewed the axiomatic semantic rules for: assignment sequence conditional while loop We also mentioned: preconditions, postconditions precondition strengthening, postcondition weakening We finished off the while-loop example from last class. Loop invariant.

amma
Download Presentation

Axiomatic semantics - II

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. Axiomatic semantics - II • We reviewed the axiomatic semantic rules for: • assignment • sequence • conditional • while loop • We also mentioned: • preconditions, postconditions • precondition strengthening, postcondition weakening • We finished off the while-loop example from last class.

  2. Loop invariant • Recall the While rule: {B & I} S {I} _ {I} while B do S end {I and !B} • I is called the loop invariant: • "…if executing [the body] once preserves the truth of [the invariant], then executing [the body] any number of times also preserves the truth of [the invariant]." [Gordon, Programming Language Theory and its Implementation, paraphrased from page 24]

  3. Importance of loop invariants • Developing loop invariants are a powerful way to design and understand algorithms. • Consider selection sort: selectionSort(int[] array) { int min, temp, bar=0; while (bar < array.length - 1) { min = indexOfSmallest(array, bar); // find min temp = array[bar]; // swap array[bar] = array[min]; array[min] = temp; bar = bar + 1; // Loop invariant: region before bar is sorted // for all i,j<=bar, if i<j, array[i] <= array[j]} }

  4. Example on board • We worked through a few iterations of the selectionSort algorithm on the board to refresh out memory about how the algorithm works.

  5. Example [ 7 4 6 8 3 2 ] [ 24 6 8 3 7 ] [ 2 36 8 4 7 ] [ 2 3 48 6 7 ] [ 2 3 4 68 7 ] [ 2 3 4 6 7 8 ] region not known to be sorted region known to be sorted

  6. Next time: Variables

More Related