SlideShare uma empresa Scribd logo
1 de 49
Baixar para ler offline
Program
verification
and testing
          www.tudorgirba.com
1
       ne 5 fl ight 50
Aria
-25 ac cidents
Therac
g
   tium F DIV bu
Pen
Testing                        Verification
run the program                formally prove that
with a set of inputs and       the program
check the output for defects   has no defects
:
       E xample mbers
                    u
           atural n
max of 2 n
if (x ≥ y)
	 max := x
else
	 max := y




                              :
                    E xample mbers
                                 u
                        atural n
             max of 2 n
x = 2
y = 3


if (x ≥ y)
	 max := x
else
	 max := y




                              :
                    E xample mbers
                                 u
                        atural n
             max of 2 n
x = 2
y = 3


if (x ≥ y)
	 max := x
else
	 max := y




max = 3


                              :
                    E xample mbers
                                 u
                        atural n
             max of 2 n
:
       E xample mbers
                    u
           atural n
max of 2 n
if (x ≥ y)
	 max := x
else
	 max := y




                              :
                    E xample mbers
                                 u
                        atural n
             max of 2 n
(x ≥ 0 ∧ y ≥ 0)



if (x ≥ y)
	 max := x
else
	 max := y




                                   :
                         E xample mbers
                                      u
                             atural n
                  max of 2 n
(x ≥ 0 ∧ y ≥ 0)



if (x ≥ y)
	 max := x
else
	 max := y



(max ≥ x) ∧
(max ≥ y) ∧
(max = x ∨ max = y)
                                       :
                             E xample mbers
                                          u
                                 atural n
                      max of 2 n
computation



information                 information
              computer
program
                  S



    {P}                      {Q}

precondition             postcondition
Partial correctness

  {P}   S   {Q}




  [P]   S   [Q]

Total correctness
Skip
                {Q} Skip {Q}



Abort
             {P} Abort {False}



Assignment
             {Q[x/E]} x := E {Q}
P: (x > 1)




S: x := x + 1




                      le
                Examp
P: (x > 1)




S: x := x + 1




Q: (x > 2)


                      le
                Examp
S: x := x + 2




Q: (x = y)


                      le
                Examp
P: (x = y - 2)




S: x := x + 2




Q: (x = y)


                       le
                 Examp
{P} S1 {Q} , {Q} S2 {R}
Sequence
                     {P} S1;S2 {R}




              {P∧B} S1 {Q} , {P∧¬B} S2 {Q}
Conditional
              {P} if B then S1 else S2 {Q}
P   I ∧ ({I∧B} S {I}) , (I ∧ ¬B   Q)
While loop
                   {P} while B do S end {Q}
P     I ∧ ({I∧B} S {I}) , (I ∧ ¬B   Q)
While loop
                                        {P} while B do S end {Q}




Loop invariant I

I = property which stays true before and after every loop

0. initial condition: P   I;
1. iterative (inductive) condition: {I ∧ B} s {I};
2. final condition: I ∧ ¬B Q
P: (x ≥ 0) ∧ (y > 0)


S: quo := 0;
   rem := x;
   while (y ≤ rem) do
    rem = rem − y;
    quo = quo + 1
   end


Q: (quo ∗ y + rem = x) ∧
   (0 ≤ rem < y)                              :
                                  E xample inder
                                        n d rema s
                           Qu otient a 2 integer
                                       ng
                            o f dividi
while (lo < hi) {

 m = (lo + hi) / 2;

 if (n > m) 	 	
             	

   lo = m + 1;		

  else

   hi = m;	 	 	

}	 	 	 	 	 	 	

n = lo;	 	 	 	
        	
        	 	 	
                                               ch
                                 : bina ry sear
                      E xample
I: lo <= n ∧ n <= hi

while (lo < hi) {	 lo <= n ∧ n <= hi*/
                  /*I:

 m = (lo + hi) / 2;

 if (n > m) 	 	 /* 
 in both cases: lo <= n ∧ n <= hi */
             	

   lo = m + 1;		 /* 	 n > m => n >= m+1 => n >= lo */

  else

   hi = m;	 	 	 /* 	 !(n < m) => n <= m => n <= hi */

}	 	 	 	 	 	 	 /* 	 I stays true */

n = lo;	 	 	 	 /* 
 lo<=n ∧ n<=hi ∧
        	
        	 	 	   	 !(lo<hi) => lo==n ∧ n==hi */
                                                               ch
                                                 : bina ry sear
                                      E xample
Weakest Precondition wp(S, Q)

∀ {P} S {Q} :: P        wp(S,Q)
Verification of {P} S {Q}

1. Compute wp(S, Q)

2. Prove P         wp(S, Q)
Assignment
wp(x:=A, Q) = Qx←A




Array Assignment
wp(a[x]:=A, Q) = Qa←a′
Assignment
wp(x:=A, Q) = Qx←A

wp(x:=5,x+y=6)   = 5+y = 6
wp(x:=x+1,x+y=6) = x+1+y = 6




Array Assignment
wp(a[x]:=A, Q) = Qa←a′
Assignment
wp(x:=A, Q) = Qx←A

wp(x:=5,x+y=6)      = 5+y = 6
wp(x:=x+1,x+y=6) = x+1+y = 6




Array Assignment
wp(a[x]:=A, Q) = Qa←a′

wp(a[1]:=x+1, a[1]=a[2]) = a′[1]=a′[2] where a′[1] = x +1, a′[i] = a[i], ∀ i ≠ 1
                              = x+1=a[2]
Sequencing
wp(S1; S2, Q)	 wp(S1, wp(S2, Q))
             =
Sequencing
wp(S1; S2, Q)	 wp(S1, wp(S2, Q))
             =	


wp(x:=x+1;y:=y+x,y>10)
Sequencing
wp(S1; S2, Q)	 wp(S1, wp(S2, Q))
             =	


wp(x:=x+1;y:=y+x,y>10)
	 	 	 	 	 	 	 = wp(x:=x+1,wp(y:=y+x,y>10))
                   	 wp(x:=x+1, y+x>10)
                   =
                  	 = y+x+1>10
Conditional
wp(if (B) then S1 else S2, Q)	 =
              (B    wp(S1, Q)) ∧ (¬B   wp(S2, Q))
Conditional
wp(if (B) then S1 else S2, Q)	 =
                  (B      wp(S1, Q)) ∧ (¬B       wp(S2, Q))



Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)
Conditional
wp(if (B) then S1 else S2, Q)	 =
                  (B      wp(S1, Q)) ∧ (¬B       wp(S2, Q))



Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)


(x≥y   wp(max:=x, Q))∧(x<y    wp(max:=y, Q) =
Conditional
wp(if (B) then S1 else S2, Q)	 =
                     (B     wp(S1, Q)) ∧ (¬B      wp(S2, Q))



Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)


(x≥y     wp(max:=x, Q))∧(x<y    wp(max:=y, Q) =

  (x≥y     Qmax←x) ∧ (x<y   Qmax←y) =
Conditional
wp(if (B) then S1 else S2, Q)	 =
                        (B     wp(S1, Q)) ∧ (¬B      wp(S2, Q))



Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)


(x≥y      wp(max:=x, Q))∧(x<y      wp(max:=y, Q) =

  (x≥y        Qmax←x) ∧ (x<y   Qmax←y) =

       (x≥y     ((x≥x) ∧ (x≥y) ∧ (x=x ∨ x=y)) ∧
Conditional
wp(if (B) then S1 else S2, Q)	 =
                        (B     wp(S1, Q)) ∧ (¬B      wp(S2, Q))



Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)


(x≥y      wp(max:=x, Q))∧(x<y      wp(max:=y, Q) =

  (x≥y        Qmax←x) ∧ (x<y   Qmax←y) =

       (x≥y     ((x≥x) ∧ (x≥y) ∧ (x=x ∨ x=y)) ∧

   ((x<y        ((y≥x) ∧ (y≥y) ∧ (y=x ∨ y=y))
While loop
L = while (B) do S end
wp(L,Q)	 I ∧
        =
             ∀y, ((B ∧ I)    wp(S, I ∧   x < y))

             ∀y, ((¬B ∧ I)    Q)
While loop
L = while (B) do S end
wp(L,Q)	 I ∧
        =
                      ∀y, ((B ∧ I)          wp(S, I ∧       x < y))

                      ∀y, ((¬B ∧ I)           Q)



Loop verification

I = property which stays true before and after every loop

0. P     I;
1. I∧B        wp(s, I);
2. I∧¬B        Q.
P: (x≥0) ∧ (y>0)
S: quo := 0;
   rem := x;

   while (y ≤ rem) do
    rem = rem − y;
    quo = quo + 1
   end

Q: (quo∗y+rem=x) ∧ (0≤rem<y)

                                                  :
                                      E xample inder
                                            n d rema s
                               Qu otient a 2 integer
                                           ng
                                o f dividi
P: (x≥0) ∧ (y>0)
S: quo := 0;
   rem := x;
I: (quo∗y+rem=x) ∧ (rem≥0) ∧ (y>0) ∧ (x≥0)
   while (y ≤ rem) do
    rem = rem − y;
    quo = quo + 1
   end

Q: (quo∗y+rem=x) ∧ (0≤rem<y)

                                                  :
                                      E xample inder
                                            n d rema s
                               Qu otient a 2 integer
                                           ng
                                o f dividi
P: (x≥0) ∧ (y>0)
I: (quo∗y+rem=x) ∧ (rem≥0) ∧ (y>0) ∧ (x≥0)
Q: (quo∗y+rem=x) ∧ (0≤rem<y)


(x ≥ 0) ∧ (y > 0)
  (x = x) ∧ (x ≥ 0) ∧ (x ≥ 0) ∧ (y > 0)

(x=rem+y∗quo) ∧ (x≥0) ∧ (rem≥0) ∧ (y>0) ∧ (y≤rem)
  (x = (rem − y) + y ∗ (quo + 1)) ∧
  x ≥ 0 ∧ rem − y ≥ 0 ∧ y > 0

(x=rem+y∗quo) ∧ (x≥0) ∧ (rem≥0) ∧ (y>0) ∧ (y>rem)
  (x = rem + y ∗ quo) ∧ (0 ≤ rem < y)
                                                   :
                                          E xample tions
                                               n condi
                                 ve   rificatio
program
                  S



    {P}                      {Q}

precondition             postcondition
Tudor Gîrba
        www.tudorgirba.com




creativecommons.org/licenses/by/3.0/

Mais conteúdo relacionado

Mais procurados

Integration by Parts, Part 2
Integration by Parts, Part 2Integration by Parts, Part 2
Integration by Parts, Part 2
Pablo Antuna
 
Calculus 08 techniques_of_integration
Calculus 08 techniques_of_integrationCalculus 08 techniques_of_integration
Calculus 08 techniques_of_integration
tutulk
 
Implicit Differentiation, Part 1
Implicit Differentiation, Part 1Implicit Differentiation, Part 1
Implicit Differentiation, Part 1
Pablo Antuna
 
2.7 chain rule short cuts
2.7 chain rule short cuts2.7 chain rule short cuts
2.7 chain rule short cuts
math265
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
Mark Brandao
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
Taketo Sano
 

Mais procurados (20)

Complex analysis notes
Complex analysis notesComplex analysis notes
Complex analysis notes
 
Integration by Parts, Part 2
Integration by Parts, Part 2Integration by Parts, Part 2
Integration by Parts, Part 2
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Calculus 08 techniques_of_integration
Calculus 08 techniques_of_integrationCalculus 08 techniques_of_integration
Calculus 08 techniques_of_integration
 
Resumen de Integrales (Cálculo Diferencial e Integral UNAB)
Resumen de Integrales (Cálculo Diferencial e Integral UNAB)Resumen de Integrales (Cálculo Diferencial e Integral UNAB)
Resumen de Integrales (Cálculo Diferencial e Integral UNAB)
 
Derivatives
DerivativesDerivatives
Derivatives
 
Succesive differntiation
Succesive differntiationSuccesive differntiation
Succesive differntiation
 
Lesson 8: Derivatives of Polynomials and Exponential functions
Lesson 8: Derivatives of Polynomials and Exponential functionsLesson 8: Derivatives of Polynomials and Exponential functions
Lesson 8: Derivatives of Polynomials and Exponential functions
 
Implicit Differentiation, Part 1
Implicit Differentiation, Part 1Implicit Differentiation, Part 1
Implicit Differentiation, Part 1
 
2.7 chain rule short cuts
2.7 chain rule short cuts2.7 chain rule short cuts
2.7 chain rule short cuts
 
Functions
FunctionsFunctions
Functions
 
Derivatives of Trigonometric Functions, Part 2
Derivatives of Trigonometric Functions, Part 2Derivatives of Trigonometric Functions, Part 2
Derivatives of Trigonometric Functions, Part 2
 
Mba admission in india
Mba admission in indiaMba admission in india
Mba admission in india
 
Taylor problem
Taylor problemTaylor problem
Taylor problem
 
11365.integral 2
11365.integral 211365.integral 2
11365.integral 2
 
C3 Transformations
C3 TransformationsC3 Transformations
C3 Transformations
 
Natural and Clamped Cubic Splines
Natural and Clamped Cubic SplinesNatural and Clamped Cubic Splines
Natural and Clamped Cubic Splines
 
Complex function
Complex functionComplex function
Complex function
 
Unit1
Unit1Unit1
Unit1
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
 

Destaque

20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison
Computer Science Club
 
Formal Verification Techniques
Formal Verification TechniquesFormal Verification Techniques
Formal Verification Techniques
DVClub
 

Destaque (10)

Program verification
Program verificationProgram verification
Program verification
 
Program Verification / Automated Theorem Proving
Program Verification / Automated Theorem ProvingProgram Verification / Automated Theorem Proving
Program Verification / Automated Theorem Proving
 
Desafío cs sociales 1
Desafío cs sociales 1Desafío cs sociales 1
Desafío cs sociales 1
 
Theorem-proving Verification of Multi-clock Synchronous Circuits on Multimoda...
Theorem-proving Verification of Multi-clock Synchronous Circuits on Multimoda...Theorem-proving Verification of Multi-clock Synchronous Circuits on Multimoda...
Theorem-proving Verification of Multi-clock Synchronous Circuits on Multimoda...
 
A Survey of functional verification techniques
A Survey of functional verification techniquesA Survey of functional verification techniques
A Survey of functional verification techniques
 
Automated theorem proving for special functions: the next phase
Automated theorem proving for special functions: the next phaseAutomated theorem proving for special functions: the next phase
Automated theorem proving for special functions: the next phase
 
20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison20130928 automated theorem_proving_harrison
20130928 automated theorem_proving_harrison
 
Functional verification techniques EW16 session
Functional verification techniques  EW16 sessionFunctional verification techniques  EW16 session
Functional verification techniques EW16 session
 
Formal Verification Techniques
Formal Verification TechniquesFormal Verification Techniques
Formal Verification Techniques
 
Formal verification
Formal verificationFormal verification
Formal verification
 

Semelhante a 09 - Program verification

Newton Raphson method for load flow analysis
Newton Raphson method for load flow analysisNewton Raphson method for load flow analysis
Newton Raphson method for load flow analysis
divyanshuprakashrock
 

Semelhante a 09 - Program verification (20)

Algebra Presentation on Topic Modulus Function and Polynomials
Algebra Presentation on Topic Modulus Function and PolynomialsAlgebra Presentation on Topic Modulus Function and Polynomials
Algebra Presentation on Topic Modulus Function and Polynomials
 
Application of derivatives 2 maxima and minima
Application of derivatives 2  maxima and minimaApplication of derivatives 2  maxima and minima
Application of derivatives 2 maxima and minima
 
AJMS_389_22.pdf
AJMS_389_22.pdfAJMS_389_22.pdf
AJMS_389_22.pdf
 
lec-10-perceptron-upload.pdf
lec-10-perceptron-upload.pdflec-10-perceptron-upload.pdf
lec-10-perceptron-upload.pdf
 
Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential Solution to schrodinger equation with dirac comb potential
Solution to schrodinger equation with dirac comb potential
 
Microeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptxMicroeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptx
 
NCERT Class 9 Maths Polynomials
NCERT Class 9 Maths  PolynomialsNCERT Class 9 Maths  Polynomials
NCERT Class 9 Maths Polynomials
 
Gr 11 equations
Gr 11   equationsGr 11   equations
Gr 11 equations
 
Appendex
AppendexAppendex
Appendex
 
Improper integral
Improper integralImproper integral
Improper integral
 
Group No 05, calculus.pptx
Group No 05, calculus.pptxGroup No 05, calculus.pptx
Group No 05, calculus.pptx
 
Newton Raphson method for load flow analysis
Newton Raphson method for load flow analysisNewton Raphson method for load flow analysis
Newton Raphson method for load flow analysis
 
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
 
Sbma 4603 numerical methods Assignment
Sbma 4603 numerical methods AssignmentSbma 4603 numerical methods Assignment
Sbma 4603 numerical methods Assignment
 
Imc2016 day2-solutions
Imc2016 day2-solutionsImc2016 day2-solutions
Imc2016 day2-solutions
 
Bessel 1 div_3
Bessel 1 div_3Bessel 1 div_3
Bessel 1 div_3
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theorem
 
Top School in india
Top School in indiaTop School in india
Top School in india
 
Physical Chemistry Assignment Help
Physical Chemistry Assignment HelpPhysical Chemistry Assignment Help
Physical Chemistry Assignment Help
 
Quadratic equation.pptx
Quadratic equation.pptxQuadratic equation.pptx
Quadratic equation.pptx
 

Mais de Tudor Girba

Mais de Tudor Girba (20)

Beyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalismBeyond software evolution: Software environmentalism
Beyond software evolution: Software environmentalism
 
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
Software craftsmanship meetup (Zurich 2015) on solving real problems without ...
 
GT Spotter
GT SpotterGT Spotter
GT Spotter
 
Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)Don't demo facts. Demo stories! (handouts)
Don't demo facts. Demo stories! (handouts)
 
Don't demo facts. Demo stories!
Don't demo facts. Demo stories!Don't demo facts. Demo stories!
Don't demo facts. Demo stories!
 
Humane assessment on cards
Humane assessment on cardsHumane assessment on cards
Humane assessment on cards
 
Underneath Scrum: Reflective Thinking
Underneath Scrum: Reflective ThinkingUnderneath Scrum: Reflective Thinking
Underneath Scrum: Reflective Thinking
 
1800+ TED talks later
1800+ TED talks later1800+ TED talks later
1800+ TED talks later
 
Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)Software assessment by example (lecture at the University of Bern)
Software assessment by example (lecture at the University of Bern)
 
Humane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development roomHumane assessment: Taming the elephant from the development room
Humane assessment: Taming the elephant from the development room
 
Moose: how to solve real problems without reading code
Moose: how to solve real problems without reading codeMoose: how to solve real problems without reading code
Moose: how to solve real problems without reading code
 
Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)Software Environmentalism (ECOOP 2014 Keynote)
Software Environmentalism (ECOOP 2014 Keynote)
 
The emergent nature of software systems
The emergent nature of software systemsThe emergent nature of software systems
The emergent nature of software systems
 
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)Presenting is storytelling at Uni Zurich - slides (2014-03-05)
Presenting is storytelling at Uni Zurich - slides (2014-03-05)
 
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
Presenting is storytelling at Uni Zurich - handouts (2014-03-05)
 
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
Underneath Scrum: Reflective Thinking (talk at Scrum Breakfast Bern, 2013)
 
Demo-driven innovation teaser
Demo-driven innovation teaserDemo-driven innovation teaser
Demo-driven innovation teaser
 
Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)Software assessment essentials (lecture at the University of Bern 2013)
Software assessment essentials (lecture at the University of Bern 2013)
 
Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)Demo-driven innovation (University of Zurich, June 2013)
Demo-driven innovation (University of Zurich, June 2013)
 
Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011Humane assessment with Moose at GOTO Aarhus 2011
Humane assessment with Moose at GOTO Aarhus 2011
 

09 - Program verification

  • 2. 1 ne 5 fl ight 50 Aria
  • 4. g tium F DIV bu Pen
  • 5.
  • 6. Testing Verification run the program formally prove that with a set of inputs and the program check the output for defects has no defects
  • 7. : E xample mbers u atural n max of 2 n
  • 8. if (x ≥ y) max := x else max := y : E xample mbers u atural n max of 2 n
  • 9. x = 2 y = 3 if (x ≥ y) max := x else max := y : E xample mbers u atural n max of 2 n
  • 10. x = 2 y = 3 if (x ≥ y) max := x else max := y max = 3 : E xample mbers u atural n max of 2 n
  • 11. : E xample mbers u atural n max of 2 n
  • 12. if (x ≥ y) max := x else max := y : E xample mbers u atural n max of 2 n
  • 13. (x ≥ 0 ∧ y ≥ 0) if (x ≥ y) max := x else max := y : E xample mbers u atural n max of 2 n
  • 14. (x ≥ 0 ∧ y ≥ 0) if (x ≥ y) max := x else max := y (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y) : E xample mbers u atural n max of 2 n
  • 15. computation information information computer
  • 16. program S {P} {Q} precondition postcondition
  • 17. Partial correctness {P} S {Q} [P] S [Q] Total correctness
  • 18. Skip {Q} Skip {Q} Abort {P} Abort {False} Assignment {Q[x/E]} x := E {Q}
  • 19. P: (x > 1) S: x := x + 1 le Examp
  • 20. P: (x > 1) S: x := x + 1 Q: (x > 2) le Examp
  • 21. S: x := x + 2 Q: (x = y) le Examp
  • 22. P: (x = y - 2) S: x := x + 2 Q: (x = y) le Examp
  • 23. {P} S1 {Q} , {Q} S2 {R} Sequence {P} S1;S2 {R} {P∧B} S1 {Q} , {P∧¬B} S2 {Q} Conditional {P} if B then S1 else S2 {Q}
  • 24. P I ∧ ({I∧B} S {I}) , (I ∧ ¬B Q) While loop {P} while B do S end {Q}
  • 25. P I ∧ ({I∧B} S {I}) , (I ∧ ¬B Q) While loop {P} while B do S end {Q} Loop invariant I I = property which stays true before and after every loop 0. initial condition: P I; 1. iterative (inductive) condition: {I ∧ B} s {I}; 2. final condition: I ∧ ¬B Q
  • 26. P: (x ≥ 0) ∧ (y > 0) S: quo := 0; rem := x; while (y ≤ rem) do rem = rem − y; quo = quo + 1 end Q: (quo ∗ y + rem = x) ∧ (0 ≤ rem < y) : E xample inder n d rema s Qu otient a 2 integer ng o f dividi
  • 27. while (lo < hi) { m = (lo + hi) / 2; if (n > m) lo = m + 1; else hi = m; } n = lo; ch : bina ry sear E xample
  • 28. I: lo <= n ∧ n <= hi while (lo < hi) { lo <= n ∧ n <= hi*/ /*I: m = (lo + hi) / 2; if (n > m) /* in both cases: lo <= n ∧ n <= hi */ lo = m + 1; /* n > m => n >= m+1 => n >= lo */ else hi = m; /* !(n < m) => n <= m => n <= hi */ } /* I stays true */ n = lo; /* lo<=n ∧ n<=hi ∧ !(lo<hi) => lo==n ∧ n==hi */ ch : bina ry sear E xample
  • 29. Weakest Precondition wp(S, Q) ∀ {P} S {Q} :: P wp(S,Q)
  • 30. Verification of {P} S {Q} 1. Compute wp(S, Q) 2. Prove P wp(S, Q)
  • 31. Assignment wp(x:=A, Q) = Qx←A Array Assignment wp(a[x]:=A, Q) = Qa←a′
  • 32. Assignment wp(x:=A, Q) = Qx←A wp(x:=5,x+y=6) = 5+y = 6 wp(x:=x+1,x+y=6) = x+1+y = 6 Array Assignment wp(a[x]:=A, Q) = Qa←a′
  • 33. Assignment wp(x:=A, Q) = Qx←A wp(x:=5,x+y=6) = 5+y = 6 wp(x:=x+1,x+y=6) = x+1+y = 6 Array Assignment wp(a[x]:=A, Q) = Qa←a′ wp(a[1]:=x+1, a[1]=a[2]) = a′[1]=a′[2] where a′[1] = x +1, a′[i] = a[i], ∀ i ≠ 1 = x+1=a[2]
  • 34. Sequencing wp(S1; S2, Q) wp(S1, wp(S2, Q)) =
  • 35. Sequencing wp(S1; S2, Q) wp(S1, wp(S2, Q)) = wp(x:=x+1;y:=y+x,y>10)
  • 36. Sequencing wp(S1; S2, Q) wp(S1, wp(S2, Q)) = wp(x:=x+1;y:=y+x,y>10) = wp(x:=x+1,wp(y:=y+x,y>10)) wp(x:=x+1, y+x>10) = = y+x+1>10
  • 37. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q))
  • 38. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q)) Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y)
  • 39. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q)) Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y) (x≥y wp(max:=x, Q))∧(x<y wp(max:=y, Q) =
  • 40. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q)) Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y) (x≥y wp(max:=x, Q))∧(x<y wp(max:=y, Q) = (x≥y Qmax←x) ∧ (x<y Qmax←y) =
  • 41. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q)) Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y) (x≥y wp(max:=x, Q))∧(x<y wp(max:=y, Q) = (x≥y Qmax←x) ∧ (x<y Qmax←y) = (x≥y ((x≥x) ∧ (x≥y) ∧ (x=x ∨ x=y)) ∧
  • 42. Conditional wp(if (B) then S1 else S2, Q) = (B wp(S1, Q)) ∧ (¬B wp(S2, Q)) Q: (max ≥ x) ∧ (max ≥ y) ∧ (max = x ∨ max = y) (x≥y wp(max:=x, Q))∧(x<y wp(max:=y, Q) = (x≥y Qmax←x) ∧ (x<y Qmax←y) = (x≥y ((x≥x) ∧ (x≥y) ∧ (x=x ∨ x=y)) ∧ ((x<y ((y≥x) ∧ (y≥y) ∧ (y=x ∨ y=y))
  • 43. While loop L = while (B) do S end wp(L,Q) I ∧ = ∀y, ((B ∧ I) wp(S, I ∧ x < y)) ∀y, ((¬B ∧ I) Q)
  • 44. While loop L = while (B) do S end wp(L,Q) I ∧ = ∀y, ((B ∧ I) wp(S, I ∧ x < y)) ∀y, ((¬B ∧ I) Q) Loop verification I = property which stays true before and after every loop 0. P I; 1. I∧B wp(s, I); 2. I∧¬B Q.
  • 45. P: (x≥0) ∧ (y>0) S: quo := 0; rem := x; while (y ≤ rem) do rem = rem − y; quo = quo + 1 end Q: (quo∗y+rem=x) ∧ (0≤rem<y) : E xample inder n d rema s Qu otient a 2 integer ng o f dividi
  • 46. P: (x≥0) ∧ (y>0) S: quo := 0; rem := x; I: (quo∗y+rem=x) ∧ (rem≥0) ∧ (y>0) ∧ (x≥0) while (y ≤ rem) do rem = rem − y; quo = quo + 1 end Q: (quo∗y+rem=x) ∧ (0≤rem<y) : E xample inder n d rema s Qu otient a 2 integer ng o f dividi
  • 47. P: (x≥0) ∧ (y>0) I: (quo∗y+rem=x) ∧ (rem≥0) ∧ (y>0) ∧ (x≥0) Q: (quo∗y+rem=x) ∧ (0≤rem<y) (x ≥ 0) ∧ (y > 0) (x = x) ∧ (x ≥ 0) ∧ (x ≥ 0) ∧ (y > 0) (x=rem+y∗quo) ∧ (x≥0) ∧ (rem≥0) ∧ (y>0) ∧ (y≤rem) (x = (rem − y) + y ∗ (quo + 1)) ∧ x ≥ 0 ∧ rem − y ≥ 0 ∧ y > 0 (x=rem+y∗quo) ∧ (x≥0) ∧ (rem≥0) ∧ (y>0) ∧ (y>rem) (x = rem + y ∗ quo) ∧ (0 ≤ rem < y) : E xample tions n condi ve rificatio
  • 48. program S {P} {Q} precondition postcondition
  • 49. Tudor Gîrba www.tudorgirba.com creativecommons.org/licenses/by/3.0/