900 likes | 1.13k Views
Spring 2014 Program Analysis and Verification Lecture 6: Axiomatic Semantics III. Roman Manevich Ben-Gurion University. Syllabus. Previously. Hoare logic Inference system Annotated programs Soundness and completeness Weakest precondition calculus. Axiomatic semantics for While.
E N D
Spring 2014Program Analysis and Verification Lecture 6: Axiomatic Semantics III Roman Manevich Ben-Gurion University
Previously • Hoare logic • Inference system • Annotated programs • Soundness and completeness • Weakest precondition calculus
Axiomatic semantics for While { P[a/x] } x:= a { P } [assp] [skipp] { P } skip { P } { P } S1 { Q }, { Q } S2 { R } { P } S1; S2 { R } [compp] { b P} S1 { Q}, { b P} S2 { Q} { P} if bthenS1elseS2 { Q} { b P } S { P } { P } while bdoS {b P } { P’ } S { Q’ } { P } S { Q } [ifp] [whilep] [consp] if PP’ and Q’Q
Weakest liberal precondition A backward-going predicate transformer The weakest liberal precondition for Q is wlp(C, Q)if and only if for all states ’if C, ’ then ’ Q Propositions: p { wlp(C, Q) } C { Q } If p { P } C { Q } then P wlp(C, Q)
Weakest liberal precondition Q wlp(C, Q) C(wlp(C, Q)) P C C(P) A backward-going predicate transformer The weakest liberal precondition for Q is wlp(C, Q)if and only if for all states ’if C, ’ then ’ Q
Strongest postcondition A forward-going predicate transformer The strongest postcondition for P is’ sp(P, C)if and only if there exists such that P and C, ’ Propositions: p { P } C { sp(P, C) } If p { P } C { Q } then sp(P, C) Q
CalculatingWeakestpreconditions By VadimPlessky (http://svgicons.sourceforge.net/) [see page for license], via Wikimedia Commons
Calculating wlp wlp(skip, Q) = Q wlp(x := a, Q) = Q[a/x] wlp(S1; S2, Q) = wlp(S1, wlp(S2, Q)) wlp(if bthenS1elseS2, Q) = (b wlp(S1, Q)) (b wlp(S2, Q)) wlp(while bdoS, Q) = … ? hard to capture
Calculating the wlp of a loop • Idea: we know the following statements are semantically equivalent • while bdoS • if bdo (S; while bdoS) else skip • Let’s try to substitute and calculate on wlp(while bdoS, Q) = wlp(if bdo (S; while bdoS) else skip, Q) = (b wlp(S; while bdoS, Q)) (b wlp(skip, Q)) = (b wlp(S, wlp(while bdoS, Q))) (b Q) LoopInv = (b wlp(S, LoopInv)) (b Q)
Another variant for WP of loops Parametric in the loop invariant wlp(while bdo{}S, Q) = where {b } S{}and b Q
Variable swap program – specify { ? } t := xx := yy := t{ ? }
Prove using weakest precondition { y=b x=a } t := x{ ? }x := y{ ? } y := t{ x=b y=a }
Prove using weakest precondition { y=b x=a } t := x{ y=b t=a }x := y{ x=b t=a } y := t{ x=b y=a }
Absolute value program if x<0 then x := -xelse skip if b then Sis syntactic sugar forif b then S else skip The latter form is easier to reason about
Absolute value program – specify { ? }if x<0 then x := -xelse skip{ ? }
Absolute value program – specify { x=v }if x<0 then x := -xelse skip{ x=|v| }
Prove using weakest precondition { x=v }{ } if x<0 then{ } x := -x{ }else{ } skip{ }{x=|v| }
Prove using weakest precondition { x=v }{ (-x=|v| x<0) (x=|v| x0) } if x<0 then{ -x=|v| } x := -x{ x=|v| }else{ x=|v| } skip{ x=|v| }{ x=|v| }
Conjunction rule { P } S { Q } { P’ } S { Q’ } { P P’ } S {Q Q’ } [conjp] Allows breaking up proofs into smaller, easier to manage, sub-proofs
More useful rules Breaks if C is non-deterministic { P } C { Q } { P’ } C { Q’ } { P P’ } C {Q Q’ } [disjp] { P } C { Q } { v. P } C { v.Q } [existp] vFV(C) { P } C { Q } {v. P } C {v. Q } vFV(C) [univp] [Invp] { F } C { F } Mod(C) FV(F)={} Mod(C) = set of variables assigned to in sub-statements of C FV(F) = free variables of F
Invariance + Conjunction = Constancy { P } C { Q } { F P } C { F Q } [constancyp] Mod(C) FV(F)={} Mod(C) = set of variables assigned to in sub-statements of C FV(F) = free variables of F
Today Strongest postcondition Extension for memory Proving termination
Strongestpostconditioncalculus By VadimPlessky (http://svgicons.sourceforge.net/) [see page for license], via Wikimedia Commons
Floyd’s strongest postcondition rule { P } x:= a { v. x=a[v/x] P[v/x] } where v is a fresh variable [assFloyd] The value of x in the pre-state Example{ z=x } x:=x+1 { ? } This rule is often considered problematic because it introduces a quantifier – needs to be eliminated further on We will now see a variant of this rule
Floyd’s strongest postcondition rule { P } x:= a { v. x=a[v/x] P[v/x] } where v is a fresh variable [assFloyd] meaning: {x=z+1} Example{ z=x } x:=x+1 { v. x=v+1 z=v } This rule is often considered problematic because it introduces a quantifier – needs to be eliminated further on We will now see a variant of this rule
“Small” assignment axiom First evaluate ain the precondition state(as a may access x) Create an explicit Skolem variable in precondition Then assign the resulting value to x { x=v } x:=a { x=a[v/x]}where vFV(a) [assfloyd] Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}
“Small” assignment axiom { x=v } x:=a { x=a[v/x]}where vFV(a) [assfloyd] Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}
“Small” assignment axiom { x=v } x:=a { x=a[v/x]}where vFV(a) [assfloyd] Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}
“Small” assignment axiom { x=v } x:=a { x=a[v/x]}where vFV(a) [assfloyd] Examples:{x=n} x:=5*y {x=5*y}{x=n} x:=x+1 {x=n+1}{x=n} x:=y+1 {x=y+1}[existp] {n. x=n} x:=y+1 {n. x=y+1} therefore {true} x:=y+1 {x=y+1} [constancyp] {z=9} x:=y+1 {z=9 x=y+1}
Calculating sp sp(skip, P) = P sp(x:=a, P) = v. x=a[v/x] P[v/x] sp(S1;S2, P) = sp(S2, sp(S1, P)) sp(ifbthenS1elseS2, P) =sp(S1, b P) sp(S2, b P) sp(whilebdo {} S, P) = bwhere {b } S {}and P b
Prove using strongest postcondition { x=a y=b }t := xx := yy := t{ x=b y=a }
Prove using strongest postcondition { x=a y=b }t := x{ x=a y=b t=a } x := yy := t{ x=b y=a }
Prove using strongest postcondition { x=a y=b }t := x{ x=a y=b t=a } x := y{ x=b y=b t=a }y := t{ x=b y=a }
Prove using strongest postcondition { x=a y=b }t := x{ x=a y=b t=a } x := y{ x=b y=b t=a }y := t{ x=b y=a t=a }{ x=b y=a } // cons
Prove using strongest postcondition { x=v }if x<0 then{ x=v x<0 } x := -x{ x=-v x>0 }else{ x=v x0 } skip{ x=v x0 }{ v<0 x=-v v0 x=v }{ x=|v| }
Prove using strongest postcondition { x=v }if x<0 then{ x=v x<0 } x := -x{ x=-v x>0 }else{ x=v x0 } skip{ x=v x0 }{ v<0 x=-v v0 x=v }{ x=|v| }
Sum program – specify { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { ? }x := 0res := 0while (x<y) do res := res+x x := x+1 { ? } Background axiom Define Sum(0, n) = 0+1+…+n
Sum program – specify { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0res := 0while (x<y) do res := res+x x := x+1 { res = Sum(0, y) } Background axiom Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0res := 0Inv = while (x<y) do res := res+x x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0Inv = while (x<y) do res := res+x x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = while (x<y) do res := res+x x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do res := res+x x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1 { res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1 { y0 res=m+x x=n+1 m=Sum(0, n) n<y } { y0 res=Sum(0, x) x=n+1 n<y} // sum axiom { y0 res=Sum(0, x) xy } // cons{ res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Sum program – prove { x=Sum(0, n) } { y=n+1 } { x+y=Sum(0, n+1) } { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) xy }while (x<y) do { y0 res=m x=n ny m=Sum(0, n) x<y } { y0 res=m x=n m=Sum(0, n) n<y } res := res+x { y0 res=m+x x=n m=Sum(0, n) n<y } x := x+1 { y0 res=m+x x=n+1 m=Sum(0, n) n<y } { y0 res=Sum(0, x) x=n+1 n<y} // sum axiom { y0 res=Sum(0, x) xy } // cons{ y0 res=Sum(0, x) xy xy}{ y0 res=Sum(0, y) x=y}{ res = Sum(0, y)} Define Sum(0, n) = 0+1+…+n
Buggy sum program { y0 }x := 0{ y0 x=0 }res := 0{ y0 x=0 res=0 }Inv = { y0 res=Sum(0, x) } = { y0 res=m x=n m=Sum(0, n)} while (xy) do { y0 res=m x=n m=Sum(0, n) xy ny } x := x+1 { y0 res=m x=n+1 m=Sum(0, n) ny} res := res+x { y0 res=m+x x=n+1 m=Sum(0, n) ny} { y0 res-x=Sum(0, x-1) ny} { y0 res=Sum(0, x) }{ y0 res=Sum(0, x) x>y } {res = Sum(0, y)}