Docsity

Hoare Semantics Syntax, Lecture notes of Computer Science

Partial preview of the text

Download Hoare Semantics Syntax and more Lecture notes Computer Science in PDF only on Docsity!

Hoare Semantics

Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

Objectives

You should be able to ...

I (^) Explain the syntax of Hoare triples and relate them to small step semantics. I (^) Use a Hoare triple to show the correctness of a simple program. I (^) Explain the properties of the weakest precondition.

Definition of →

Skip and Assignment

< skip , σ >→< E, σ > < u := t, σ >→< E, σ[u := σ(t)] > < S 1 , σ >→< S 2 , τ > < S 1 ; S, σ >→< S 2 ; S, τ >

E; S ≡ S

< if B then S 1 else S 2 fi , σ >→< S 1 , σ > where σ |= B < if B then S 1 else S 2 fi , σ >→< S 2 , σ > where σ |= ¬B < while B do S 1 od , σ >→< S 1 ; while B do S 1 od , σ > where σ |= B < while B do S 1 od , σ >→< E, σ > where σ |= ¬B

Hoare Triples

I (^) The → semantics gives us exact transformations, but sometimes we want something more general. I (^) Define {p}S{q}, where p and q are assertions, and S is a program: I (^) |= {p}S{q} – if p is true before the program runs, q will be true afterwards; if the program terminates. “Partial Correctness” I (^) |=tot {p}S{q} – if p is true before the program runs, q will be true afterwards. Termination guaranteed. “Total Correctness” I (^) These are sometimes called correctness formulas.

Axiom 1: Skip

{p} skip {p}

Axiom 2: Assignment

{p[u := t]}u := t{p}

I (^) Is this what you expected?

{y > 10 }x := y{x > 10 }

Rule 4: Conditional

{p ∧ B}S 1 {q}, {p ∧ ¬B}S 2 {q} {p} if B then S 1 else S 2 fi {q}

I (^) See Dijkstra’s paper EWD 264.

Rule 5: Loop

{p ∧ B}S{p} {p} while B do S od {p ∧ ¬B}

Skip, Assignment, and Sequence

{p} skip {p} {p[u := t]}u := t{p}

{p}S∗ 1 {r}, {r}S∗ 2 {q} {p}S∗ 1 ; {r}S∗ 2 {q}

I (^) Example

{y = 20, x = 10} t := x; {y = 20, t = 10} x := y; {x = 20, t = 10} y := t {x = 20, y = 10}

If, Consequence

{p ∧ B}S∗ 1 {q}, {p ∧ ¬B}S∗ 2 {q} {p} if B then {p ∧ B}S∗ 1 {q} else {p ∧ ¬B}S∗ 2 {q} fi {q}

p → p 1 , {p 1 }S∗{q 1 }, q 1 → q {p}{p 1 }S∗{q 1 }{q}

The Verification

{true} if x > y then m := x fi ; if x < y then m := y fi {m = max(x, y)}

The Verification

{true} if x > y then m := x fi ; if x < y then {y = max(x, y)}m := y{m = max(x, y)} else {m = max(x, y)} skip {m = max(x, y)} fi {m = max(x, y)}

The Verification

{true} if x > y then {P[x = max(x, y)]}m := x{P} else {P} skip {P} fi ; {P ≡ x < y ∧ y = max(x, y) ∨ x > y ∧ m = max(x, y)} if x < y then {y = max(x, y)}m := y{m = max(x, y)} else {m = max(x, y)} skip {m = max(x, y)} fi {m = max(x, y)}

The Verification

{true} {x > y ∧ x = max(x, y) ∨ x < y ∧ y = max(x, y) ∨ x = y ∧ m = max(x, y)} if x > y then {P[x = max(x, y)]}m := x{P} else {P} skip {P} fi ; {P ≡ x < y ∧ y = max(x, y) ∨ x ≥ y ∧ m = max(x, y)} if x < y then {y = max(x, y)}m := y{m = max(x, y)} else {m = max(x, y)} skip {m = max(x, y)} fi {m = max(x, y)}