1 / 17

Inverting a program (Chapter 21 of Science of Programming)

You have the output of your program. You lost the input. You want to recover the input. What do you do?. Inverting a program (Chapter 21 of Science of Programming). Run the program backwards. Given program P, find its inverse P -1 . Then execute P - 1.

fey
Télécharger la présentation

Inverting a program (Chapter 21 of Science of Programming)

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. You have the output of your program. You lost the input. You want to recover the input. What do you do? Inverting a program(Chapter 21 of Science of Programming) Run the program backwards. Given program P, find its inverse P-1. Then execute P-1 You know what the inverse f-1 of a function f is. Some functions have inverses; others don’t! f(x) = x+1. f-1(x) = x-1 g(x) = x2 doesn’t have an inverse because g(1) = g(-1) = 1

  2. Given program P, find its inverse P-1 Inverting a program(Chapter 21 of Science of Programming) Some statements are easy to invert (x:= x+1) -1 = x:= x-1 We can’t invert x:= 1 But: inverse of {x = 3} x:= 1 is {x = 1} x:= 3

  3. (y:= x – y)-1 = <rewrite> (y:= – (y – x))-1 = <rewrite> (y:= y – x; y:= – y )-1 = <Def of inverse> (y:= – y)-1; (y:= y – x)-1 = <Def of inverse> y:= – y; y:= y + x = <Rewrite> y:= x – y Inverse of x, y:= y, x ? Read it backward! x, y:= y, x Inverting two simple programs Inverse of x:= x + y; y:= x – y; x:= x – y Do them backward, inverting each one (x:= x – y)-1 ; (y:= x – y)-1 ; (x:= x + y)-1 = <definition of inverse and previous calculation> x:= x + y; y:= x – y; x:= x – y It’s its own inverse! What does this sequence do?

  4. Inverse ofskip is itself Inverse of S1; S2; …; Snis Sn-1; …; S2-1; S1-1 Inversion of general commands Let c1 and c2 be constants, x a new variable. This block assigns a constant c1 to new variable x and terminate with x = c2: x:= c1; S {x = c2} Inverse of x:= c1; S {x = c2} is x:= c2; S-1 {x = c1}

  5. To invert, have to know whether R1 or R2 is true in final state, so require R1  R2 = false. For symmetry, require B1  B2 = false. {B1  B2} if B1 S1 {R1} B2 S2 {R2} fi {R1  R2} Inversion of IF To get the inverse, read backwards. {R2  R1} R2 S2-1 {B2} R1 S1-1 {B1} if fi {B2  B1}

  6. We annotate this more to prepare for inversion. It needs a precondition, which will become the postcondition when DO is inverted. For symmetry, we use precondition !C Also, since B occurs twice, C must also: do B S od {!B} {!C} do B S {C} od {!B} {!B} do C S-1 {B} od {!C} Inversion of a simple DO The inverse is then:

  7. ii+n j j+n ii+n j j+n ii+ki+n j j+kj+n unswapped swapped swapped unswappedunswapped pre: b post: b inv: b Given n ≥ 0. Segments b[i..i+n-1] and b[j..j+n-1] don’t overlap. Swap them. Inverting a program that swaps segments of equal length Informal pre-postconditions unswapped swapped swapped

  8. ii+n j j+n ii+n j j+n ii+ki+n j j+kj+n unswapped swapped swapped unswappedunswapped pre: b post: b inv: b Inverting a program that swaps segments of equal length unswapped swapped swapped k:= 0; do k ≠ n b[i+k], b[j+k]:= b[j+k], b[i+k]; k:= k+1 od Note: this is its own inverse. But we can calculate another inverse.

  9. Inverting a program that swaps segments of equal length Introduces fresh variable k; in order to invert, needs to end with k having a value k = n. We annotate this program also in preparation for inverting the loop k:= 0; do k ≠ n b[i+k], b[j+k]:= b[j+k], b[i+k]; k:= k+1 od k:= 0; loop: {k = 0} do k ≠ n b[i+k], b[j+k]:= b[j+k], b[i+k]; k:= k+1 {k ≠ 0} od {k = n} {k = n}

  10. Inverting a program that swaps segments of equal length Using definition of a block, the inverse is: {k = n} loop-1 {k = 0} k:= 0; loop: {k = 0} do k ≠ n b[i+k], b[j+k]:= b[j+k], b[i+k]; k:= k+1 {k ≠ 0} od {k = n} {k = n}

  11. Inverting a program that swaps segments of equal length Using inverse of the loop and its body, we get: pool: {k = n} do k ≠ 0 k:= k-1; b[k+i], b[k+j]:= b[k+j], b[k+i] {k ≠ n} od {k = 0} loop: {k = 0} do k ≠ n b[i+k], b[j+k]:= b[j+k], b[i+k]; k:= k+1 {k ≠ 0} od {k = n} Put together with inverse of the block, remove annotations: k:= n; do k ≠ 0 k:= k-1; b[k+i], b[k+j]:= b[k+j], b[k+i] od Original swaps left to right; inverse does it right to left

  12. // Sort b[0..n-1] k:= 0; //invariant: b[0..k-1] is sorted and b[0..k-1] ≤ b[k..n-1] do k ≠ n b[k], b[t]:= b[t], b[k], where t = index of min of b[k..n-1]; k:= k+1; od Inverting selection sort Problem in inverting it: Can’t invert the swap because value to use for t is unknown. Suppose all value in b are different. Then the algorithm changes all equally likely initial arrays into sorted order. Suppose in the inverse, we choose a random value for t in b[k..n-1]. Then the inverse will change the sorted array into one possible initial array. Since a random t is chosen each time swap is executed, the inversion will produce some array, with all possible arrays equally. Therefore, the inversion will shuffle the array.

  13. // Sort b[0..n-1] k;= 0; //invariant: b[0..k-1] is sorted and b[0..k-1] ≤ b[k..n-1] do k ≠ n b[k], b[t]:= b[t], b[k], where t = index of min of b[k..n-1]; k:= k+1; od Inverting selection sort k:= 0; do k ≠ n body od body: b[k], b[t]:= b[t], b[k], where t = index of min of b[k..n-1]; k:= k+1;

  14. Inverting selection sort k:= 0; loop: {k = 0} do k ≠ n body {k ≠ 0} od {k = n} {k = n} Its inverse is: k:= n; pool: {k = n} do k ≠ 0 body-1{k ≠ n} od {k = 0} {k = 0} k:= 0; do k ≠ n body od

  15. body: b[k], b[t]:= b[t], b[k], where t = index of min of b[k..n-1]; k:= k + 1 We can’t invert the swap, because we don’t know what value to use for t. We choose a random one in b[k..n-1]. Inverse: k:= k – 1; b[k], b[t]:= b[t], b[k], where t = random int in k..n-1;

  16. Its inverse is: k:= n; do k ≠ 0 k:= k – 1; b[k], b[t]:= b[t], b[k], where t = random int in k..n-1; od

  17. The inverse of selection sort is therefore: k:= n; do k ≠ 0 k:= k+1; b[k], b[t]:= b[t], b[k], where t = random int in k..n-1; od In the 1960’s/1970’s, Communications of the ACM had a section on algorithms. People submitted short algorithms for various tasks. One time, the above algorithm appeared, saying that it shuffled the array. There was no explanation at all, of correctness, of anything. I didn’t understand it. When we began playing with inversions, I inverted selection sort to get this.

More Related