SlideShare a Scribd company logo
1 of 36
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.com
0
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Program verification attempts to ensure that a computer program is
correct. And a program is correct if it behaves in accordance with its
specifications.
 This does not necessarily mean that the program solves the problem
that it was intended to solve; the program’s specifications may be at
odds with or not address all aspects of a client’s requirements.
 Program validation attempts to ensure that the program indeed meets
the client’s original requirements.
 Program testing seeks to show that particular input values produce
acceptable output values.
 Proof of correctness uses the techniques of a formal logic system to
prove that if the input variables satisfy certain specified predicates or
properties, the output variables produced by executing the program
satisfy other specified properties.
1
Preparedby:SharifOmarSalem–ssalemg@gmail.com
What is assertions in computer programming
 an assertion is a predicate (for example a true–false statement) placed in a
program to indicate that the developer thinks that the predicate is always true at
that place.
 For example, the following code contains two assertions:
 Programmers can use assertions to help specify programs and to reason about
program correctness.
 For example, a precondition — an assertion placed at the beginning of a section
of code — determines the set of states under which the programmer expects
the code to execute. A postcondition — placed at the end — describes the
expected state at the end of execution.
2
x > 0 and x > 1, and they are indeed true at
the indicated points during execution.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 The previous example uses the notation for including assertions used
by C.A.R. Hoare in his 1969 paper.
 That notation cannot be used in existing mainstream programming
languages. However, programmers can include unchecked assertions
using the comment feature of their programming language. For
example, in
3
 C Language
 Java Language
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 The central feature of Hoare logic is the Hoare triple. A triple
describes how the execution of a piece of code changes the state of
the computation. A Hoare triple is of the form

 where Q and R are assertions and P is a P command. Q is named
the precondition and R the postcondition: when the precondition is
met, the command establishes the postcondition. Assertions are
formulas in predicate logic.
 Hoare logic provides axioms and inference rules for all the
constructs of a simple imperative programming language.
 In addition to the rules for the simple language in Hoare's original
paper, rules for other language constructs have been developed
since then by Hoare and many other researchers. There are rules
for concurrency, procedures, jumps, and pointers.
4
{Q} P {R}  {Pre-condition} Program {Post-condition}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
5
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Let us denote (suppose) by
 X  an arbitrary collection of input values.
 P  the program or program segment.
 Y  an arbitrary collection of output values.
 Y = P(X)  Y is the result of applying the program P using the inputs
X (the notation suggests that the Y values depend on the X values
through the actions of program P).
 Q(X)  a predicate describes conditions that the input values are
supposed to satisfy.
Q is the pre-condition.
 R(X,Y)  R[X, P(X)]  A predicate R describes conditions that the
output values are supposed to satisfy. These conditions will often
involve the input values.
R is the post-condition.
6
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 For example, if a program is supposed to find the square root of a
positive number.
 X  Positive numbers (single input is x).
 Q(X)  x > 0
 Y  Output result (single output is y)  P(X)=
 R(X,Y)  x=y2
 Program P is correct if the following implication is valid:
 ("X)(Q(X)  R[X, P(X)])
 For the square root case, it is:
 ("x)(x > 0  x=[P(x)]2 )
7
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 A program or program segment is broken down into individual statements si,
with predicates (Conditions) inserted between statements as well as at the
beginning and end.
 These predicates are also called assertions because they assert what is
supposed to be true about the program variables at that point in the program.
 {Q}
s0
 {R1}
s1
 {R2}
sn1
.
 {R}
 Where Q, R1, R2, ... , Rn = R are assertions. The intermediate assertions are often
obtained by working backward from the output assertion R.
8
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 P is provably correct if each of the following implications holds:
 {Q}s0{Rl}
 {Rl}sl{R2}
 {R2}s2{R3}
.
.
.
 {Rn1}sn1{R}
 A proof of correctness for P consists of producing this sequence of valid
implications.
 Some new rules of inference can be used, based on the nature of the program
statement si.
9
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 The goal of Hoare logic is to provide a compositional method for proving
the validity of Hoare triples. That is, the structure of a program's
correctness proof should mirror the structure of the program itself.
 Hoare logic is a methodology to assert the correctness of a program by
defining a precondition and postcondition predicates to define the status
of the variable before and after executing the program.
 Many rules could be used during applying hoare logic.
 Empty statement axiom schema
 Assignment axiom schema
 Rule of composition
 Conditional rule
 Consequence rule
 Loop rule
10
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Empty statement axiom schema
The empty statement rule asserts that the skip statement
does not change the state of the program, thus whatever
holds true before skip also holds true afterwards.
11
{Q} Skip {Q}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Assignment axiom schema
The program/command part here is an assignment related to the
variable under assertion.
 The assignment axiom states that after the assignment any
predicate holds for the variable that was previously true, are also
true for the right-hand side of the assignment:
12
x:=2
x:=x+1
y:=x+1
{Q} Assignment {R}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 this axiom is “backwards” - it allows the precondition to be inferred
automatically from the statement and the postcondition .
 For example, the Hoare triple:
 {x  1 > 0} x = x  1 {x > 0}
 is valid by the assignment rule.
 The post-condition is:
 x > 0
 Substituting x  1 for x throughout the post-condition results in:
 x – 1 > 0 or x > 1
 which is the pre-condition.
13
Hoare Logic rules
{R(x/y)} y:=x {R}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Examples:
{ ? } x := 3 { x+y > 0 }
 What is the weakest precondition ?
{ ? } x=y+7 {x>42}
 What is the weakest precondition ?
{ ? } x := 3+y + z { x + y - z > 0 }
 What is the weakest precondition ?
{ ? } x := 3*y + z { x * y - z > 0 }
 What is the weakest precondition ?
14
y > -3
y > 35
y > -1.5
3*y2 + z*y - z > 0
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Sequence/Composition Rule:
 Hoare's rule of composition applies to sequentially-executed
programs S and T, where S executes prior to T and is written S;T.
15
{Q} S {Z} , {Z} T {R} ⊢ {Q} S;T {R}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Example:
{ ? } x := x + 1; y := x + y { y > 5 }
What is the weakest precondition ?
The solution method begin from backward by finding the weakest
precondition for the second part of the sequence
{ ? } y := x + y { y > 5 }
Then continue by finding the weakest precondition for the First part of
the sequence
{ ? } x := x + 1 { x > 5-y }
16
x+y>4
x+y>5
x+y>4
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Consequence rule
17
{Q} P {R} , Q1 → Q ⊢ {Q1} P {R}
{Q} P {R} , R1 → R ⊢ {Q} P {R1}
{Q} P {R} , Q1 → Q , R1 → R ⊢ {Q1} P {R1}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Condition Rule
 A conditional statement is a program statement of the form:
 The Hoare triple is inferred from two other Hoare triples:
This simply says that each branch of the conditional statement must
be proved correct.
18
if condition B then
P1
else
P2
end if
{Q ∧ B } P1 {R} if B is true
{Q ∧ B } P2 {R} if B is false
Hoare Logic rules
{Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Example:
{ ? } if x > 0 then y := x else y := -x { y > 5 }
What is the weakest precondition ?
 Conditional statement 1 “then: {Q1} y :=x { y > 5}”
 Q1 = x>5
 Condicional statement 2 “else: {Q2} y :=-x { y > 5}”
 Q2 = -x > 5
 Q = |x| > 5
19
|x| > 5
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Example:
{a = T ^ b = 6 ^ c = 10} x := b {(x = 6 ^ a = T) ∨ (x = 10 ^ a = F)} and
{a = F ^ b = 6 ^ c = 10} x := c {(x = 6 ^ a = T) ∨ (x = 10 ^ a = F)}
 After applying the condition rule:
 {(b = 6 ^ c = 10)} If (a = T) then x := b; else x := c {(x = 6 ^ a = T) ∨ (x
= 10 ^ a = F)}
20
{Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Rule for Iteration
 Suppose that si is a loop statement in the form:
 B is a condition of the while loop and P is a program segment/command.
 The Loop Rule of inference states that we can infer the following rule
{Q Λ B} P {Q} ⊢ {Q} si {Q Λ B }
 The precondition Q holds before the loop is entered and after it terminates.
 Q represents a predicate, or relation, among the values of the program
variables unaffected by the action of the loop iteration which is The loop
invariant.
21
while condition B do
P
end while
Preparedby:SharifOmarSalem–ssalemg@gmail.com
22
Rule for Iteration
• Q is the loop invariant - this is where the main difficulty is!
• This rule can be extended to handle total correctness where
we use termination condition test at post-condition predicate.
• A loop invariant is a relation among program variables that
is true when control enters a loop, remains true each time
the program executes the body of the loop, and is still true
when control exits the loop. Understanding loop invariants
can help us analyze programs, check for errors, and derive
programs from specifications.
{Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬B}
{Inv ∧ Condition} P {Inv} ⊢ {Inv} while (Condition) [P] {Inv ∧¬Condition}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Example:
while (x < 10) x = x+1
 Start with this:
 x <= 10 is a useful loop invariant.
 {x <= 10} while (x < 10) x = x+1 {??}
 Move inside the test: {Inv ∧ Condition} P {Inv}
 {x <= 10 ^ x < 10} x = x+1 {x <= 10}
 Backing out: {Inv} while (Condition) [P] {Inv ∧¬Condition}
 { x <= 10} while (x<10) x=x+1 {¬(x < 10) ^ x <= 10}
23
Preparedby:SharifOmarSalem–ssalemg@gmail.com
24
Hoare Logic rules
Preparedby:SharifOmarSalem–ssalemg@gmail.com
• Empty statement rule.
• Assignment rule.
• Composition rule.
• Consequence rule.
• Conditional rule.
• Loop rule.
25
{Q} Skip {Q}
{Q} Assignment {R}
{Q} S {Z} , {Z} T {R} ⊢ {Q} S;T {R}
{Q} P {R} , Q1 → Q , R1 → R ⊢ {Q1} P {R1}
{Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R}
{Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬E}
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Here are a number of valid Hoare Triples for the same command
segment
 {x = 5} x := x * 2 { true }
 {x = 5} x := x * 2 { x > 0 }
 {x = 5} x := x * 2 { (x = 10) ^ (x = 5) }
 {x = 5} x := x * 2 { x = 10 }
 All are true, but the most useful one is the one with se (it is the most specific
condition )
x=10 is the strongest postcondition ………… Why????????????
 check: x = 10 ⇒ true
 check: x = 10 ⇒ x > 0
 check: x = 10 ⇒ x = 10 || x = 5
 check: x = 10 ⇒ x = 10
26
If {Q} P {R} and for all R* such that {Q} P {R*}, R ⇒ R*, then R is the
strongest postcondition [ sp(P,Q) ] of P with respect to Q.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Here are a number of valid Hoare Triples for the same command
segment
 {(x = 5) Λ (y = 10)} z := x / y { z < 1 }
 {(x < y) Λ (y > 0)} z := x / y { z < 1 }
 {(y ≠ 0) Λ (x / y < 1)} z := x / y { z < 1 }
 All are true, but the most useful one is the one with the most general condition
(y ≠ 0) Λ (x / y < 1) is the weakest precondition ……….. Why??????
 check: (x = 5) Λ (y = 10) ⇒ (y ≠ 0) Λ (x / y < 1)
 check: (x < y) Λ (y > 0) ⇒ (y ≠ 0) Λ (x / y < 1)
 check: (y ≠ 0) Λ (x / y < 1) ⇒ (y ≠ 0) Λ (x / y < 1)
27
If {Q} P {R} and for all Q* such that {Q*} P {R}, Q* ⇒ Q, then Q is the
weakest precondition [ wp(P,R) ] of P with respect to R.
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 {Q} P {R} holds if and only if Q ⇒ wp(P,R)
 In other words, a Hoare Triple is still valid if the precondition is
stronger than necessary, but not if it is too weak
 {Q} P {R} holds if and only if sp(P,Q) ⇒ R
 A Hoare Triple is still valid if the postcondition is weak
enough, but not if it is too strong.
In other words, both conditions must be strong enough
to hold the best general condition as the precondition
and the best specific condition as the postcondition.
28
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Assignment rule conditions
{ Q } x := 3 { x+y > 0 }
 What is the weakest precondition Q?
 If {Q} P {R} then the weakestprecondition [wp(P,R) ]
 Assignment rule
 wp(X:= E, R) = (X:=E), R
 = (x:=3), (x + y > 0)
 = (3) + y > 0
 = y > -3
29
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Assignment
{ Q } x := 3*y + z { x * y - z > 0 }
 What is the weakest precondition Q?
 If {Q} P {R} then the weakestprecondition [wp(P,R) ]
 Assignment rule
 wp(X:= E, R) = (X:=E), R
 = (x:=3*y+z), (x * y – z > 0)
 = (3*y+z) * y - z > 0
 = 3*y2 + z*y - z > 0
30
Preparedby:SharifOmarSalem–ssalemg@gmail.com
 Sequence
{ Q } x := x + 1; y := x + y { y > 5 }
 What is the weakest precondition Q?
 Sequence rule
 wp(S;T, R) = wp(S, wp(T, R))
 wp(x:=x+1; y:=x+y, y>5)
 = wp(x:=x+1, wp(y:=x+y, y>5))
 = wp(x:=x+1, x+y>5)
 = x+1+y>5
 = x+y>4
31
wp(y:=x+y, y>5)
= (y:= x+y ), ( y>5)
= x+y > 5
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Conditional
{ Q } if x > 0 then y := x else y := -x { y > 5 }
 What is the weakest precondition Q?
answer:
 Case 1 then: {Q1} y :=x { y > 5}
Q1 = x>5
 Case 2 else: {Q2} y :=-x { y > 5}
Q2 = -x > 5
 Q = (x > 5) ∧ ( -x > 5) = |x| > 5
32
wp(y:=x, y>5)
= (y:= x ), ( y>5)
= x > 5
wp(y:= -x, y>5)
= (y:= -x ), ( y>5)
= -x > 5
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Conditional
{ Q } if x > 0 then y := x else y := -x { y > 5 }
 What is the weakest precondition Q?
 Conditional rule
wp(if B then P1 else P2, R)
= B ⇒ wp(P1,R) ∧ B’ ⇒ wp(P2,R)
 wp(if x>0 then y:=x else y:=-x, y>5)
= x>0 ⇒ wp(y:=x, y>5) ∧ x≤0 ⇒ wp(y:=-x, y>5)
= x>0 ⇒ x>5 ∧ x≤0 ⇒ -x>5
 = |x| > 5
33
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.com
34
Preparedby:SharifOmarSalem–ssalemg@gmail.com
Prepared by: Sharif Omar Salem – ssalemg@gmail.com
35

More Related Content

What's hot

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
SRS(software requirement specification)
SRS(software requirement specification)SRS(software requirement specification)
SRS(software requirement specification)Akash Kumar Dhameja
 
How To Write A Test Case In Software Testing | Edureka
How To Write A Test Case In Software Testing | EdurekaHow To Write A Test Case In Software Testing | Edureka
How To Write A Test Case In Software Testing | EdurekaEdureka!
 
Software Requirements
 Software Requirements Software Requirements
Software RequirementsZaman Khan
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dagsindhu mathi
 
Software project management
Software project managementSoftware project management
Software project managementR A Akerkar
 
Introduction To Mycin Expert System
Introduction To Mycin Expert SystemIntroduction To Mycin Expert System
Introduction To Mycin Expert SystemNipun Jaswal
 
software design principles
software design principlessoftware design principles
software design principlesCristal Ngo
 
Constructive Cost Model - II (COCOMO-II)
Constructive Cost Model - II (COCOMO-II)Constructive Cost Model - II (COCOMO-II)
Constructive Cost Model - II (COCOMO-II)AmanSharma1172
 
Translating English to Propositional Logic
Translating English to Propositional LogicTranslating English to Propositional Logic
Translating English to Propositional LogicJanet Stemwedel
 
Software Engineering (Introduction to Software Engineering)
Software Engineering (Introduction to Software Engineering)Software Engineering (Introduction to Software Engineering)
Software Engineering (Introduction to Software Engineering)ShudipPal
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategiesSHREEHARI WADAWADAGI
 
New software testing-techniques
New software testing-techniquesNew software testing-techniques
New software testing-techniquesFincy V.J
 
Software review
Software reviewSoftware review
Software reviewamjad_09
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introductionwahab khan
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
Advanced topics in software engineering
Advanced topics in software engineeringAdvanced topics in software engineering
Advanced topics in software engineeringRupesh Vaishnav
 
Software testing & Quality Assurance
Software testing & Quality Assurance Software testing & Quality Assurance
Software testing & Quality Assurance Webtech Learning
 

What's hot (20)

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
SRS(software requirement specification)
SRS(software requirement specification)SRS(software requirement specification)
SRS(software requirement specification)
 
Software metrics
Software metricsSoftware metrics
Software metrics
 
How To Write A Test Case In Software Testing | Edureka
How To Write A Test Case In Software Testing | EdurekaHow To Write A Test Case In Software Testing | Edureka
How To Write A Test Case In Software Testing | Edureka
 
Software Requirements
 Software Requirements Software Requirements
Software Requirements
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Software project management
Software project managementSoftware project management
Software project management
 
Introduction To Mycin Expert System
Introduction To Mycin Expert SystemIntroduction To Mycin Expert System
Introduction To Mycin Expert System
 
software design principles
software design principlessoftware design principles
software design principles
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
Constructive Cost Model - II (COCOMO-II)
Constructive Cost Model - II (COCOMO-II)Constructive Cost Model - II (COCOMO-II)
Constructive Cost Model - II (COCOMO-II)
 
Translating English to Propositional Logic
Translating English to Propositional LogicTranslating English to Propositional Logic
Translating English to Propositional Logic
 
Software Engineering (Introduction to Software Engineering)
Software Engineering (Introduction to Software Engineering)Software Engineering (Introduction to Software Engineering)
Software Engineering (Introduction to Software Engineering)
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategies
 
New software testing-techniques
New software testing-techniquesNew software testing-techniques
New software testing-techniques
 
Software review
Software reviewSoftware review
Software review
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introduction
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Advanced topics in software engineering
Advanced topics in software engineeringAdvanced topics in software engineering
Advanced topics in software engineering
 
Software testing & Quality Assurance
Software testing & Quality Assurance Software testing & Quality Assurance
Software testing & Quality Assurance
 

Similar to Program verification and correctness using Hoare logic

#6 formal methods – loop proof using induction method
#6 formal methods – loop proof using induction method#6 formal methods – loop proof using induction method
#6 formal methods – loop proof using induction methodSharif Omar Salem
 
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemTMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemIosif Itkin
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic sliderainoftime
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In RRsquared Academy
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in rmanikanta361
 
Computer programming chapter ( 4 )
Computer programming chapter ( 4 ) Computer programming chapter ( 4 )
Computer programming chapter ( 4 ) Ibrahim Elewah
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming DharmaKumariBhandari
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5patcha535
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial ProgrammingSakthi Dasans
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If conditionyarkhosh
 

Similar to Program verification and correctness using Hoare logic (20)

#6 formal methods – loop proof using induction method
#6 formal methods – loop proof using induction method#6 formal methods – loop proof using induction method
#6 formal methods – loop proof using induction method
 
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light SystemTMPA-2015: Implementing the MetaVCG Approach in the C-light System
TMPA-2015: Implementing the MetaVCG Approach in the C-light System
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic slide
 
R Programming: Comparing Objects In R
R Programming: Comparing Objects In RR Programming: Comparing Objects In R
R Programming: Comparing Objects In R
 
Static Analysis and Verification of C Programs
Static Analysis and Verification of C ProgramsStatic Analysis and Verification of C Programs
Static Analysis and Verification of C Programs
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
 
Computer programming chapter ( 4 )
Computer programming chapter ( 4 ) Computer programming chapter ( 4 )
Computer programming chapter ( 4 )
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial Programming
 
Bsit1
Bsit1Bsit1
Bsit1
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
APSEC2020 Keynote
APSEC2020 KeynoteAPSEC2020 Keynote
APSEC2020 Keynote
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 

Program verification and correctness using Hoare logic

  • 2. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Program verification attempts to ensure that a computer program is correct. And a program is correct if it behaves in accordance with its specifications.  This does not necessarily mean that the program solves the problem that it was intended to solve; the program’s specifications may be at odds with or not address all aspects of a client’s requirements.  Program validation attempts to ensure that the program indeed meets the client’s original requirements.  Program testing seeks to show that particular input values produce acceptable output values.  Proof of correctness uses the techniques of a formal logic system to prove that if the input variables satisfy certain specified predicates or properties, the output variables produced by executing the program satisfy other specified properties. 1
  • 3. Preparedby:SharifOmarSalem–ssalemg@gmail.com What is assertions in computer programming  an assertion is a predicate (for example a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place.  For example, the following code contains two assertions:  Programmers can use assertions to help specify programs and to reason about program correctness.  For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the programmer expects the code to execute. A postcondition — placed at the end — describes the expected state at the end of execution. 2 x > 0 and x > 1, and they are indeed true at the indicated points during execution.
  • 4. Preparedby:SharifOmarSalem–ssalemg@gmail.com  The previous example uses the notation for including assertions used by C.A.R. Hoare in his 1969 paper.  That notation cannot be used in existing mainstream programming languages. However, programmers can include unchecked assertions using the comment feature of their programming language. For example, in 3  C Language  Java Language
  • 5. Preparedby:SharifOmarSalem–ssalemg@gmail.com  The central feature of Hoare logic is the Hoare triple. A triple describes how the execution of a piece of code changes the state of the computation. A Hoare triple is of the form   where Q and R are assertions and P is a P command. Q is named the precondition and R the postcondition: when the precondition is met, the command establishes the postcondition. Assertions are formulas in predicate logic.  Hoare logic provides axioms and inference rules for all the constructs of a simple imperative programming language.  In addition to the rules for the simple language in Hoare's original paper, rules for other language constructs have been developed since then by Hoare and many other researchers. There are rules for concurrency, procedures, jumps, and pointers. 4 {Q} P {R}  {Pre-condition} Program {Post-condition}
  • 7. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Let us denote (suppose) by  X  an arbitrary collection of input values.  P  the program or program segment.  Y  an arbitrary collection of output values.  Y = P(X)  Y is the result of applying the program P using the inputs X (the notation suggests that the Y values depend on the X values through the actions of program P).  Q(X)  a predicate describes conditions that the input values are supposed to satisfy. Q is the pre-condition.  R(X,Y)  R[X, P(X)]  A predicate R describes conditions that the output values are supposed to satisfy. These conditions will often involve the input values. R is the post-condition. 6
  • 8. Preparedby:SharifOmarSalem–ssalemg@gmail.com  For example, if a program is supposed to find the square root of a positive number.  X  Positive numbers (single input is x).  Q(X)  x > 0  Y  Output result (single output is y)  P(X)=  R(X,Y)  x=y2  Program P is correct if the following implication is valid:  ("X)(Q(X)  R[X, P(X)])  For the square root case, it is:  ("x)(x > 0  x=[P(x)]2 ) 7
  • 9. Preparedby:SharifOmarSalem–ssalemg@gmail.com  A program or program segment is broken down into individual statements si, with predicates (Conditions) inserted between statements as well as at the beginning and end.  These predicates are also called assertions because they assert what is supposed to be true about the program variables at that point in the program.  {Q} s0  {R1} s1  {R2} sn1 .  {R}  Where Q, R1, R2, ... , Rn = R are assertions. The intermediate assertions are often obtained by working backward from the output assertion R. 8
  • 10. Preparedby:SharifOmarSalem–ssalemg@gmail.com  P is provably correct if each of the following implications holds:  {Q}s0{Rl}  {Rl}sl{R2}  {R2}s2{R3} . . .  {Rn1}sn1{R}  A proof of correctness for P consists of producing this sequence of valid implications.  Some new rules of inference can be used, based on the nature of the program statement si. 9
  • 11. Preparedby:SharifOmarSalem–ssalemg@gmail.com  The goal of Hoare logic is to provide a compositional method for proving the validity of Hoare triples. That is, the structure of a program's correctness proof should mirror the structure of the program itself.  Hoare logic is a methodology to assert the correctness of a program by defining a precondition and postcondition predicates to define the status of the variable before and after executing the program.  Many rules could be used during applying hoare logic.  Empty statement axiom schema  Assignment axiom schema  Rule of composition  Conditional rule  Consequence rule  Loop rule 10
  • 12. Preparedby:SharifOmarSalem–ssalemg@gmail.com Empty statement axiom schema The empty statement rule asserts that the skip statement does not change the state of the program, thus whatever holds true before skip also holds true afterwards. 11 {Q} Skip {Q}
  • 13. Preparedby:SharifOmarSalem–ssalemg@gmail.com Assignment axiom schema The program/command part here is an assignment related to the variable under assertion.  The assignment axiom states that after the assignment any predicate holds for the variable that was previously true, are also true for the right-hand side of the assignment: 12 x:=2 x:=x+1 y:=x+1 {Q} Assignment {R}
  • 14. Preparedby:SharifOmarSalem–ssalemg@gmail.com  this axiom is “backwards” - it allows the precondition to be inferred automatically from the statement and the postcondition .  For example, the Hoare triple:  {x  1 > 0} x = x  1 {x > 0}  is valid by the assignment rule.  The post-condition is:  x > 0  Substituting x  1 for x throughout the post-condition results in:  x – 1 > 0 or x > 1  which is the pre-condition. 13 Hoare Logic rules {R(x/y)} y:=x {R}
  • 15. Preparedby:SharifOmarSalem–ssalemg@gmail.com Examples: { ? } x := 3 { x+y > 0 }  What is the weakest precondition ? { ? } x=y+7 {x>42}  What is the weakest precondition ? { ? } x := 3+y + z { x + y - z > 0 }  What is the weakest precondition ? { ? } x := 3*y + z { x * y - z > 0 }  What is the weakest precondition ? 14 y > -3 y > 35 y > -1.5 3*y2 + z*y - z > 0
  • 16. Preparedby:SharifOmarSalem–ssalemg@gmail.com Sequence/Composition Rule:  Hoare's rule of composition applies to sequentially-executed programs S and T, where S executes prior to T and is written S;T. 15 {Q} S {Z} , {Z} T {R} ⊢ {Q} S;T {R}
  • 17. Preparedby:SharifOmarSalem–ssalemg@gmail.com Example: { ? } x := x + 1; y := x + y { y > 5 } What is the weakest precondition ? The solution method begin from backward by finding the weakest precondition for the second part of the sequence { ? } y := x + y { y > 5 } Then continue by finding the weakest precondition for the First part of the sequence { ? } x := x + 1 { x > 5-y } 16 x+y>4 x+y>5 x+y>4
  • 18. Preparedby:SharifOmarSalem–ssalemg@gmail.com Consequence rule 17 {Q} P {R} , Q1 → Q ⊢ {Q1} P {R} {Q} P {R} , R1 → R ⊢ {Q} P {R1} {Q} P {R} , Q1 → Q , R1 → R ⊢ {Q1} P {R1}
  • 19. Preparedby:SharifOmarSalem–ssalemg@gmail.com Condition Rule  A conditional statement is a program statement of the form:  The Hoare triple is inferred from two other Hoare triples: This simply says that each branch of the conditional statement must be proved correct. 18 if condition B then P1 else P2 end if {Q ∧ B } P1 {R} if B is true {Q ∧ B } P2 {R} if B is false Hoare Logic rules {Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R}
  • 20. Preparedby:SharifOmarSalem–ssalemg@gmail.com Example: { ? } if x > 0 then y := x else y := -x { y > 5 } What is the weakest precondition ?  Conditional statement 1 “then: {Q1} y :=x { y > 5}”  Q1 = x>5  Condicional statement 2 “else: {Q2} y :=-x { y > 5}”  Q2 = -x > 5  Q = |x| > 5 19 |x| > 5
  • 21. Preparedby:SharifOmarSalem–ssalemg@gmail.com Example: {a = T ^ b = 6 ^ c = 10} x := b {(x = 6 ^ a = T) ∨ (x = 10 ^ a = F)} and {a = F ^ b = 6 ^ c = 10} x := c {(x = 6 ^ a = T) ∨ (x = 10 ^ a = F)}  After applying the condition rule:  {(b = 6 ^ c = 10)} If (a = T) then x := b; else x := c {(x = 6 ^ a = T) ∨ (x = 10 ^ a = F)} 20 {Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R}
  • 22. Preparedby:SharifOmarSalem–ssalemg@gmail.com Rule for Iteration  Suppose that si is a loop statement in the form:  B is a condition of the while loop and P is a program segment/command.  The Loop Rule of inference states that we can infer the following rule {Q Λ B} P {Q} ⊢ {Q} si {Q Λ B }  The precondition Q holds before the loop is entered and after it terminates.  Q represents a predicate, or relation, among the values of the program variables unaffected by the action of the loop iteration which is The loop invariant. 21 while condition B do P end while
  • 23. Preparedby:SharifOmarSalem–ssalemg@gmail.com 22 Rule for Iteration • Q is the loop invariant - this is where the main difficulty is! • This rule can be extended to handle total correctness where we use termination condition test at post-condition predicate. • A loop invariant is a relation among program variables that is true when control enters a loop, remains true each time the program executes the body of the loop, and is still true when control exits the loop. Understanding loop invariants can help us analyze programs, check for errors, and derive programs from specifications. {Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬B} {Inv ∧ Condition} P {Inv} ⊢ {Inv} while (Condition) [P] {Inv ∧¬Condition}
  • 24. Preparedby:SharifOmarSalem–ssalemg@gmail.com Example: while (x < 10) x = x+1  Start with this:  x <= 10 is a useful loop invariant.  {x <= 10} while (x < 10) x = x+1 {??}  Move inside the test: {Inv ∧ Condition} P {Inv}  {x <= 10 ^ x < 10} x = x+1 {x <= 10}  Backing out: {Inv} while (Condition) [P] {Inv ∧¬Condition}  { x <= 10} while (x<10) x=x+1 {¬(x < 10) ^ x <= 10} 23
  • 26. Preparedby:SharifOmarSalem–ssalemg@gmail.com • Empty statement rule. • Assignment rule. • Composition rule. • Consequence rule. • Conditional rule. • Loop rule. 25 {Q} Skip {Q} {Q} Assignment {R} {Q} S {Z} , {Z} T {R} ⊢ {Q} S;T {R} {Q} P {R} , Q1 → Q , R1 → R ⊢ {Q1} P {R1} {Q ∧ B } P1 {R} , {Q ∧ B } P2 {R} ⊢ {Q} if B then P1 else P2 endif {R} {Q ∧ B} P {Q} ⊢ {Q} while (B) [P] {Q∧¬E}
  • 27. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Here are a number of valid Hoare Triples for the same command segment  {x = 5} x := x * 2 { true }  {x = 5} x := x * 2 { x > 0 }  {x = 5} x := x * 2 { (x = 10) ^ (x = 5) }  {x = 5} x := x * 2 { x = 10 }  All are true, but the most useful one is the one with se (it is the most specific condition ) x=10 is the strongest postcondition ………… Why????????????  check: x = 10 ⇒ true  check: x = 10 ⇒ x > 0  check: x = 10 ⇒ x = 10 || x = 5  check: x = 10 ⇒ x = 10 26 If {Q} P {R} and for all R* such that {Q} P {R*}, R ⇒ R*, then R is the strongest postcondition [ sp(P,Q) ] of P with respect to Q.
  • 28. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Here are a number of valid Hoare Triples for the same command segment  {(x = 5) Λ (y = 10)} z := x / y { z < 1 }  {(x < y) Λ (y > 0)} z := x / y { z < 1 }  {(y ≠ 0) Λ (x / y < 1)} z := x / y { z < 1 }  All are true, but the most useful one is the one with the most general condition (y ≠ 0) Λ (x / y < 1) is the weakest precondition ……….. Why??????  check: (x = 5) Λ (y = 10) ⇒ (y ≠ 0) Λ (x / y < 1)  check: (x < y) Λ (y > 0) ⇒ (y ≠ 0) Λ (x / y < 1)  check: (y ≠ 0) Λ (x / y < 1) ⇒ (y ≠ 0) Λ (x / y < 1) 27 If {Q} P {R} and for all Q* such that {Q*} P {R}, Q* ⇒ Q, then Q is the weakest precondition [ wp(P,R) ] of P with respect to R.
  • 29. Preparedby:SharifOmarSalem–ssalemg@gmail.com  {Q} P {R} holds if and only if Q ⇒ wp(P,R)  In other words, a Hoare Triple is still valid if the precondition is stronger than necessary, but not if it is too weak  {Q} P {R} holds if and only if sp(P,Q) ⇒ R  A Hoare Triple is still valid if the postcondition is weak enough, but not if it is too strong. In other words, both conditions must be strong enough to hold the best general condition as the precondition and the best specific condition as the postcondition. 28
  • 30. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Assignment rule conditions { Q } x := 3 { x+y > 0 }  What is the weakest precondition Q?  If {Q} P {R} then the weakestprecondition [wp(P,R) ]  Assignment rule  wp(X:= E, R) = (X:=E), R  = (x:=3), (x + y > 0)  = (3) + y > 0  = y > -3 29
  • 31. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Assignment { Q } x := 3*y + z { x * y - z > 0 }  What is the weakest precondition Q?  If {Q} P {R} then the weakestprecondition [wp(P,R) ]  Assignment rule  wp(X:= E, R) = (X:=E), R  = (x:=3*y+z), (x * y – z > 0)  = (3*y+z) * y - z > 0  = 3*y2 + z*y - z > 0 30
  • 32. Preparedby:SharifOmarSalem–ssalemg@gmail.com  Sequence { Q } x := x + 1; y := x + y { y > 5 }  What is the weakest precondition Q?  Sequence rule  wp(S;T, R) = wp(S, wp(T, R))  wp(x:=x+1; y:=x+y, y>5)  = wp(x:=x+1, wp(y:=x+y, y>5))  = wp(x:=x+1, x+y>5)  = x+1+y>5  = x+y>4 31 wp(y:=x+y, y>5) = (y:= x+y ), ( y>5) = x+y > 5
  • 33. Preparedby:SharifOmarSalem–ssalemg@gmail.com Conditional { Q } if x > 0 then y := x else y := -x { y > 5 }  What is the weakest precondition Q? answer:  Case 1 then: {Q1} y :=x { y > 5} Q1 = x>5  Case 2 else: {Q2} y :=-x { y > 5} Q2 = -x > 5  Q = (x > 5) ∧ ( -x > 5) = |x| > 5 32 wp(y:=x, y>5) = (y:= x ), ( y>5) = x > 5 wp(y:= -x, y>5) = (y:= -x ), ( y>5) = -x > 5
  • 34. Preparedby:SharifOmarSalem–ssalemg@gmail.com Conditional { Q } if x > 0 then y := x else y := -x { y > 5 }  What is the weakest precondition Q?  Conditional rule wp(if B then P1 else P2, R) = B ⇒ wp(P1,R) ∧ B’ ⇒ wp(P2,R)  wp(if x>0 then y:=x else y:=-x, y>5) = x>0 ⇒ wp(y:=x, y>5) ∧ x≤0 ⇒ wp(y:=-x, y>5) = x>0 ⇒ x>5 ∧ x≤0 ⇒ -x>5  = |x| > 5 33