90 likes | 133 Views
Ch 23:Formal Semantics. A formal system:. Axioms and Rules,. for inferring valid specification. { m ≥1, n ≥1 }. assertion. pre-condition. x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x. program. specification. { x = gcd(m,n) }. assertion.
E N D
Ch 23:Formal Semantics A formal system: Axioms and Rules, for inferring valid specification { m≥1, n ≥1 } assertion pre-condition x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x program specification { x = gcd(m,n) } assertion post-condition IT 327
A formal system: Axioms and Rules, { P } skip { P } map { true } diverge { false } { [P] (i E) } i := E { P } assignment { P } C {Q} { P } ( C ) { Q } grouping { P } C0 {Q} & { Q } C1 { R } { P } C0 ; C1{ R } sequencing { P, B } C0 {Q} & { P, ¬B } C1 { Q } { P } if B then C0 else C1 { Q } if-statement { P, B } C{ P } { P } while B do C { P, ¬B } while-loop this P is called the loop invariant IT 327
(II) A formal system: Axioms and Rules, implies { P R} & { R } C { Q } { P } C{ Q } pre-condition strengthening { R Q} & { P } C { R } { P } C{ Q } post-condition weakening or { P1} C { Q } & { P2} C { Q } { P1 P2 } C{ Q } pre-condition disjunction and { P} C { Q1 } & { P} C { Q2 } { P} C{ Q1, Q2 } post-condition conjunction IT 327
A correct program { ( n+1) ≥0 } This (piece of) program is correct.. This is a correct specification n := n+1; { n ≥0 } Is what sense? It is correct because we can apply the following axiom to obtain the specification: { [P] (i E) } i := E { P } assignment IT 327
Another proof of the program correctness {b ≥0 } assertion pre-condition s := a; i := 0; while ¬(i=b) do ( s := s+1; i := i+1 ) program specification { s = a+b } assertion post-condition The most difficult part is to figure out a useful loop invariant { s = a+i } IT 327
{b ≥0 (b ≥0, a = a+0) } {b ≥0, a = a+0 } s := a; {b ≥0,s = a+0 } {b ≥0 } {(b ≥0,s = a+0) (b ≥0, s = a+0, 0=0) } s := a; i := 0; {b ≥0, s = a+0, 0=0 } {b ≥0, s = a+i } i := 0; {b ≥0, s = a+0, i = 0 } { (b ≥0,s = a+0 , i=0) (b ≥0, s = a+i) } IT 327
{ P, B } C{ P } { P } while B do C { P, ¬B } {b ≥0, s = a+i , ¬(i=b) } {b ≥0, s = a+i } s := s+1; i := i+1; while ¬(i=b) do ( s := s+1; i := i+1 ) {b ≥0, s = a+i } {b ≥0, s = a+i , ¬¬(i=b) } {(b ≥0, s = a+i , ¬¬(i=b)) (b ≥0, s = a+i , i=b) } {(b ≥0, s = a+i , i=b) s = a+b} IT 327
Examples of correctness proofs Easy introduction to loop invariants IT 327