1 / 14

Hoare Logic 2

Hoare Logic 2. slides by Chris Osborn. Hoare Triple. P { …code…} Q. P[e/x] { x := e } P. P { C 1 } R. R { C 2 } Q. P { C 1 ; C 2 } Q. P ∧ ¬ b { C 2 } Q. P ∧ b { C 1 } Q. P { if b then C 1 else C 2 } Q. While Rule. P ∧ b { C } P. P {While b C} P ∧ ¬ b.

sonja
Download Presentation

Hoare Logic 2

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. Hoare Logic 2 slides by Chris Osborn

  2. Hoare Triple P { …code…} Q

  3. P[e/x] { x := e } P P { C1 } R R { C2 } Q P { C1; C2 } Q P ∧¬ b { C2 } Q P ∧ b { C1 } Q P { if b then C1 else C2 } Q

  4. While Rule P ∧ b { C } P P {While b C} P ∧¬ b (P is a loop invariant)

  5. Rule of Consequence P  Pʹ Pʹ { C } Qʹ Qʹ Q P { C } Q

  6. Sample Proofs • sum of n • fibonacci • list append • list reverse • termination

  7. Sum of n x = 0 & y = 0 { While y < n y := y + 1; x := x + y } x = 1 + ... + n P ≡ x = 1 + ... + y ∧ y ≤ n

  8. x = 0 ∧ y = 0  x = 1 + ... + y ∧ y ≤ n ✔ ✔ x = 1 + ... + y ∧ y ≤ n ∧¬(y < n)  x = 1 + ... + n x = 1 + ... + y ∧ y ≤ n ∧ y < n  ? ✔ x + y + 1 = (1 + ... + (y + 1))∧ y + 1 ≤ n {y := y + 1} x + y = 1 + ... + y ∧ y ≤ n {x := x + y} x = 1 + ... + y ∧ y ≤ n ? {y := y + 1; x := x + y} x = 1 + ... + y ∧ y ≤ n x = 1 + ... + y ∧ y ≤ n ∧ y < n {y := y + 1; x := x + y} x = 1 + ... + y ∧ y ≤ n x = 1 + ... + y ∧ y ≤ n {While y < n ...} x = 1 + ... + y ∧ y ≤ n ∧¬(y < n) x = 0 ∧ y = 0 {While ...} x = 1 + ... + n

  9. Fibonacci x = 0 & y = 1 & z = 1 & 1 ≤ n { While z < n y := x + y; x := y – x; z := z + 1 } y = fib n P ≡ y = fib z ∧ x = fib (z-1) ∧ z ≤ n

  10. x = 0 ∧ y = 1 ∧ z = 0 ∧ 1 ≤ n y = fib z ∧ x = fib (z-1) ∧ z ≤ n ✔ y = fib z ∧ x = fib (z-1) ∧ z ≤ n ∧¬(z < n)  y = fib n y = fib z ∧ x = fib (z-1) ∧ z ≤ n ∧ z < n ? ✔ x+y = fib (z+1) ∧ x+y-x = fib (z+1-1) ∧ z + 1 ≤ n {y := x + y} y = fib (z+1) ∧ y-x = fib (z+1-1) ∧ z + 1 ≤ n {x := y - x} y = fib (z+1) ∧ x = fib (z+1-1) ∧ z + 1 ≤ n {z := z + 1} y = fib z ∧ x = fib (z-1) ∧ z ≤ n ? {y := x + y; x := y – x; z := z + 1} y = fib z ∧ x = fib (z-1) ∧ z ≤ n y = fib z ∧ x = fib (z-1) ∧ z ≤ n ∧ z < n{y := x + y; x := y – x; z := z + 1} y = fib z ∧ x = fib (z-1) ∧ z ≤ n y = fib z ∧ x = fib (z-1) ∧ z ≤ n{While z < n ...} y = fib z ∧ x = fib (z-1) ∧ z ≤ n ∧¬(z < n) x = 0 ∧ y = 1 ∧ z = 0 ∧ 1 ≤ n {While ...} y = fib n

  11. List length x = lst & y = 0 { While x ≠ [] x := tl x; y := y + 1 } y = len lst P ≡ len lst = y + len x

  12. x = lst ∧ y = 0  len lst = y + len x ✔ len lst = y + len x ∧¬(x ≠ [])  y = len lst len lst = y + len x ∧ x ≠ []  ? ✔ len lst = y + 1 + len(tl x) {x := tl x} len lst = y + 1 + len x {y := y + 1} len lst = y + len x ? {x := tl x; y := y + 1} len lst = y + len x len lst = y + len x ∧ x ≠ [] {x := tl x; y := y + 1} len lst = y + len x len lst = y + len x {While x ≠ [] ...} len lst = y + len x ∧¬(x ≠ []) x = lst ∧ y = 0 {While ...} y = len lst

  13. List reverse x = lst & y = [] { While x ≠ [] y := hd x :: y; x := tl x } y = rev lst P ≡ lst = rev y @ x

  14. x = lst ∧ y = [] lst = rev y @ x lst = rev y @ x ∧ ¬(x ≠ [])  y = rev lst ✔ lst = rev y @ x ∧ x ≠ []  ? ✔ lst = rev (hd x @ y) @ (tl x) {y := hd x @ y} lst = rev y @ (tl x) {x := tl x} lst = rev y @ x ? {y := hd x @ y; x := tl x} lst = rev y @ x lst = rev y @ x ∧ x ≠ [] {y := hd x @ y; x := tl x} lst = rev y @ x lst = rev y @ x {While x ≠ [] ...} lst = rev y @ x ∧¬(x ≠ []) x = lst ∧ y = [] {While ...} y = rev lst

More Related