Institut Supérieur de l’Aéronautique et de l’Espace
FITR304 - Software Validation
Deductive methods for proving imperative programs
Christophe Garion DMIA – ISAE
Christophe Garion IN324 Software Validation – deductive methods 1/ 149
FITR304 - Software Validation Deductive methods for proving - - PowerPoint PPT Presentation
Institut Suprieur de lAronautique et de lEspace FITR304 - Software Validation Deductive methods for proving imperative programs Christophe Garion DMIA ISAE Christophe Garion IN324 Software Validation deductive methods 1/
Christophe Garion DMIA – ISAE
Christophe Garion IN324 Software Validation – deductive methods 1/ 149
Christophe Garion IN324 Software Validation – deductive methods 2/ 149
Christophe Garion IN324 Software Validation – deductive methods 3/ 149
Christophe Garion IN324 Software Validation – deductive methods 4/ 149
1
2
3
4
Christophe Garion IN324 Software Validation – deductive methods 5/ 149
1
2
3
4
Christophe Garion IN324 Software Validation – deductive methods 6/ 149
Christophe Garion IN324 Software Validation – deductive methods 7/ 149
Christophe Garion IN324 Software Validation – deductive methods 8/ 149
Christophe Garion IN324 Software Validation – deductive methods 8/ 149
void sort (int* array, int n) { ... }
Christophe Garion IN324 Software Validation – deductive methods 9/ 149
Christophe Garion IN324 Software Validation – deductive methods 10/ 149
Christophe Garion IN324 Software Validation – deductive methods 12/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
bugzilla/)?
Christophe Garion IN324 Software Validation – deductive methods 13/ 149
1
2
3
4
Christophe Garion IN324 Software Validation – deductive methods 14/ 149
Christophe Garion IN324 Software Validation – deductive methods 15/ 149
Christophe Garion IN324 Software Validation – deductive methods 16/ 149
Christophe Garion IN324 Software Validation – deductive methods 17/ 149
(Cond.)
Christophe Garion IN324 Software Validation – deductive methods 18/ 149
1
2
3
4
Christophe Garion IN324 Software Validation – deductive methods 19/ 149
Christophe Garion IN324 Software Validation – deductive methods 20/ 149
Christophe Garion IN324 Software Validation – deductive methods 21/ 149
Christophe Garion IN324 Software Validation – deductive methods 22/ 149
r′ = 1 u′ = 1 v′ = u TEST r − n s′ = 1 u′ = u + v s′ = s + 1 TEST s − r r′ = r + 1
Christophe Garion IN324 Software Validation – deductive methods 23/ 149
Christophe Garion IN324 Software Validation – deductive methods 24/ 149
1
2
3
4
Christophe Garion IN324 Software Validation – deductive methods 25/ 149
1
2
3
Christophe Garion IN324 Software Validation – deductive methods 26/ 149
Christophe Garion IN324 Software Validation – deductive methods 27/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 28/ 149
Christophe Garion IN324 Software Validation – deductive methods 29/ 149
Christophe Garion IN324 Software Validation – deductive methods 29/ 149
Christophe Garion IN324 Software Validation – deductive methods 30/ 149
Christophe Garion IN324 Software Validation – deductive methods 30/ 149
Christophe Garion IN324 Software Validation – deductive methods 31/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 32/ 149
Christophe Garion IN324 Software Validation – deductive methods 33/ 149
Christophe Garion IN324 Software Validation – deductive methods 34/ 149
Christophe Garion IN324 Software Validation – deductive methods 35/ 149
Christophe Garion IN324 Software Validation – deductive methods 36/ 149
Christophe Garion IN324 Software Validation – deductive methods 37/ 149
Christophe Garion IN324 Software Validation – deductive methods 38/ 149
Christophe Garion IN324 Software Validation – deductive methods 39/ 149
Christophe Garion IN324 Software Validation – deductive methods 40/ 149
Use propositional language to model the following declarative sentences.
1
it is raining and it is cold.
2
if he eats too much, he will be sick.
3
it is sunny but it is cold.
4
if it is cold, I take my jacket.
5
I take either a jacket, either an umbrella.
6
it is not raining.
7
in autumn, if it is cold then I take a jacket.
8
in winter, I take a jacket only if it is cold.
9
if Peter does not forget to book tickets, we will go to theater.
10 if Peter does not forget to book tickets and if we fjnd a baby-sitter, we will
go to theater.
11 he went, although it was very hot, but he forgot his water bottle. 12 when I am nervous, I practise yoga or relaxation. Someone practising yoga
also practises relaxation. So when I do not practise relaxation, I am calm.
13 my sister wants a black and white cat.
Christophe Garion IN324 Software Validation – deductive methods 42/ 149
Christophe Garion IN324 Software Validation – deductive methods 42/ 149
(p → (p → p)) → ((p → ((p → p) → p)) → (p → p)) p → (p → p) (p → ((p → p) → p)) → (p → p) p → ((p → p) → p) p → p Christophe Garion IN324 Software Validation – deductive methods 43/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 44/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 45/ 149
Christophe Garion IN324 Software Validation – deductive methods 46/ 149
(I∧)
(E1
∧)
(E2
∧)
(I1
∨)
(I2
∨)
(E∨)
(I→)
(E→)
Christophe Garion IN324 Software Validation – deductive methods 47/ 149
(I→)
Christophe Garion IN324 Software Validation – deductive methods 48/ 149
Christophe Garion IN324 Software Validation – deductive methods 48/ 149
(I∧)
(I→)2
(I→)1
Christophe Garion IN324 Software Validation – deductive methods 49/ 149
(E⊥)
(EM)
(A)
Christophe Garion IN324 Software Validation – deductive methods 50/ 149
Christophe Garion IN324 Software Validation – deductive methods 51/ 149
http://www.irit.fr/panda/.
Christophe Garion IN324 Software Validation – deductive methods 52/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 53/ 149
Christophe Garion IN324 Software Validation – deductive methods 54/ 149
(Hyp)
(Aff )
(I∧)
(E1
∧)
(E2
∧)
(I1
∨)
(I2
∨)
(E∨)
(I→)
(E→)
(TE)
(E⊥)
(Hyp)
(Aff )
(Hyp)
(Aff )
(I∧)
(I→)
(I→)
Christophe Garion IN324 Software Validation – deductive methods 56/ 149
Christophe Garion IN324 Software Validation – deductive methods 57/ 149
http://www.spass-prover.org.
Christophe Garion IN324 Software Validation – deductive methods 58/ 149
begin_problem(pl_1). list_of_descriptions. name({*(A -> (B -> C)) -> ((A -> B) -> (A -> C))*}). author({*Christophe Garion*}). status(satisfiable). description({*Prove (A -> (B -> C)) -> ((A -> B) -> (A -> C))...*}). end_of_list. list_of_symbols. predicates[(A,0), (B,0), (C,0)]. end_of_list. list_of_formulae(conjectures). formula(implies(implies(A, implies(B, C)), implies(implies(A, B), implies(A, C)))). end_of_list. end_problem.
Christophe Garion IN324 Software Validation – deductive methods 58/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 59/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 60/ 149
Christophe Garion IN324 Software Validation – deductive methods 61/ 149
Christophe Garion IN324 Software Validation – deductive methods 62/ 149
Christophe Garion IN324 Software Validation – deductive methods 63/ 149
Christophe Garion IN324 Software Validation – deductive methods 65/ 149
Christophe Garion IN324 Software Validation – deductive methods 66/ 149
Christophe Garion IN324 Software Validation – deductive methods 67/ 149
Christophe Garion IN324 Software Validation – deductive methods 68/ 149
Christophe Garion IN324 Software Validation – deductive methods 68/ 149
Christophe Garion IN324 Software Validation – deductive methods 69/ 149
Christophe Garion IN324 Software Validation – deductive methods 70/ 149
Christophe Garion IN324 Software Validation – deductive methods 71/ 149
Christophe Garion IN324 Software Validation – deductive methods 72/ 149
5
6
7
Christophe Garion IN324 Software Validation – deductive methods 73/ 149
(I∀)
(E∀)
(I∃)
(E∃)
Christophe Garion IN324 Software Validation – deductive methods 74/ 149
(I∀)
(E∀)
(I∃)
(E∃)
Christophe Garion IN324 Software Validation – deductive methods 75/ 149
Christophe Garion IN324 Software Validation – deductive methods 76/ 149
Christophe Garion IN324 Software Validation – deductive methods 77/ 149
begin_problem(fol_1). list_of_descriptions. name({*(forall x Phi(x) /\ Psi(x)) -> (forall x Phi(x)) /\ (forall x Psi(x))*}). author({*Christophe Garion*}). status(satisfiable). description({*Prove (forall x Phi(x) /\ Psi(x)) -> (forall x Phi(x)) /\ (forall x Psi(x))...*}). end_of_list. list_of_symbols. predicates[(Phi,1), (Psi,1)]. end_of_list. list_of_formulae(conjectures). formula(implies(forall([X], and(Phi(X), Psi(X))), and(forall([X], Phi(X)), forall([X], Psi(X))))). end_of_list. end_problem.
Christophe Garion IN324 Software Validation – deductive methods 78/ 149
8
9
Christophe Garion IN324 Software Validation – deductive methods 79/ 149
8
9
Christophe Garion IN324 Software Validation – deductive methods 80/ 149
Y, etc.).
8
9
Christophe Garion IN324 Software Validation – deductive methods 82/ 149
P is a program as defjned previously
P
Christophe Garion IN324 Software Validation – deductive methods 83/ 149
Christophe Garion IN324 Software Validation – deductive methods 84/ 149
8
9
Christophe Garion IN324 Software Validation – deductive methods 85/ 149
(:=)
(:=)
(:=)
(:=)
(:=)
(:=)
Christophe Garion IN324 Software Validation – deductive methods 87/ 149
(Seq)
(:=) {(A + X ≥ 0)[A/0]}
A := 0 {A + X ≥ 0}
(:=) {(A + B ≥ 0)[B/X]}
B := X {A + B ≥ 0}
(Seq) {X ≥ 0}
A := 0; B := X {A + B ≥ 0}
Christophe Garion IN324 Software Validation – deductive methods 88/ 149
A := 1 B := X;
Christophe Garion IN324 Software Validation – deductive methods 89/ 149
(Cons)
(Str)
(Weak)
(Cons)
(Cond.)
Christophe Garion IN324 Software Validation – deductive methods 91/ 149
(It.)
Christophe Garion IN324 Software Validation – deductive methods 92/ 149
(It.)
Christophe Garion IN324 Software Validation – deductive methods 92/ 149
Christophe Garion IN324 Software Validation – deductive methods 93/ 149
8
9
Christophe Garion IN324 Software Validation – deductive methods 94/ 149
Christophe Garion IN324 Software Validation – deductive methods 95/ 149
(It.)
Christophe Garion IN324 Software Validation – deductive methods 96/ 149
K := 0 F := 1 while (K = N) do K := K + 1; F := F * K
Christophe Garion IN324 Software Validation – deductive methods 97/ 149
K := N; F := 1; while (K = 0) do F := F * K; K := K - 1
Christophe Garion IN324 Software Validation – deductive methods 98/ 149
Q := 0; R := X; while (Y ≤ R) do Q := Q + 1; R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 99/ 149
X := A; Y := B; while (X = Y) do if (X > Y) then X := X - Y else Y := Y - X fi
Christophe Garion IN324 Software Validation – deductive methods 100/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 101/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 102/ 149
Christophe Garion IN324 Software Validation – deductive methods 103/ 149
Christophe Garion IN324 Software Validation – deductive methods 104/ 149
Christophe Garion IN324 Software Validation – deductive methods 104/ 149
Christophe Garion IN324 Software Validation – deductive methods 104/ 149
Christophe Garion IN324 Software Validation – deductive methods 104/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 105/ 149
Christophe Garion IN324 Software Validation – deductive methods 106/ 149
Christophe Garion IN324 Software Validation – deductive methods 107/ 149
Christophe Garion IN324 Software Validation – deductive methods 108/ 149
Christophe Garion IN324 Software Validation – deductive methods 108/ 149
http://alt-ergo.lri.fr/.
Christophe Garion IN324 Software Validation – deductive methods 109/ 149
type E logic phi : E -> prop logic psi : E -> prop logic phi2 : E, E -> prop logic a: E goal Th_1 : (forall x : E. phi(x) and psi(x)) -> (forall x : E. phi(x)) and (forall x : E. psi(x)) goal Th_2 : (exists x : E. forall y : E. phi2(x, y)) -> (forall y : E. exists x : E. phi2(x, y)) goal Th_3 : (forall y : E. exists x : E. phi2(x, y)) -> (exists x : E. forall y : E. phi2(x, y)) goal Th_4 : (forall y : E. phi2(a, y)) -> (exists x : E. forall y : E. phi2(x, y))
Christophe Garion IN324 Software Validation – deductive methods 109/ 149
goal arith_1 : forall x, y : int. 2 * y - x <= 0 and -8 * y + x + 2 <= 0 and 2 * y + x - 3 <= 0
goal arith_2 : forall x, y : int. x * (x + 1) = y -> x * x = y - x goal arith_non_linear_1 : forall x, y : int. 2 <= x <= 6 and -3 <= y < 0 ->
goal arith_non_linear_2 : forall x, y : int. 2 <= x <= 6 and -3 <= y < 0 ->
Christophe Garion IN324 Software Validation – deductive methods 109/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 110/ 149
Christophe Garion IN324 Software Validation – deductive methods 111/ 149
Christophe Garion IN324 Software Validation – deductive methods 112/ 149
Q := 0; R := X;
while (Y ≤ R) do
Q := Q + 1; R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 113/ 149
Christophe Garion IN324 Software Validation – deductive methods 114/ 149
1
2
Christophe Garion IN324 Software Validation – deductive methods 115/ 149
1
2
1
2
3
Christophe Garion IN324 Software Validation – deductive methods 117/ 149
Q := 0;
R := X;
while (Y ≤ R) do
Q := Q + 1;
R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 118/ 149
Q := 0;
R := X;
while (Y ≤ R) do
Q := Q + 1;
R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 119/ 149
Q := 0;
R := X;
Christophe Garion IN324 Software Validation – deductive methods 120/ 149
while (Y ≤ R) do
Q := Q + 1;
R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 121/ 149
Q := Q + 1;
R := R - Y
Christophe Garion IN324 Software Validation – deductive methods 122/ 149
goal VC_1 : forall X, Y : int. (X >= 0) and (Y > 0) -> (X = X) and (X >= 0) and (0 = 0) goal VC_2 : forall X, Y, R, Q : int. (R = X) and (R >= 0) and (Q = 0) -> (X = R + (Q * Y)) and (R >= 0) and (Q >= 0) goal VC_3 : forall X, Y, R, Q : int. (X = R + (Q * Y)) and (R >= 0) and (Q >= 0) and not(Y <= R) -> (X = Q * Y + R) and (0 <= R < Y) and (0 <= Q) goal VC_4 : forall X, Y, R, Q : int. (X = R + (Q * Y)) and (R >= 0) and (Q >= 0) and (Y <= R) -> (X = (R-Y) + ((Q+1) * Y)) and ((R-Y) >= 0) and (Q+1 >= 0)
Christophe Garion IN324 Software Validation – deductive methods 123/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 124/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 125/ 149
http : / / frama
. com / download / acsl
http://frama-c.com.
http : / / www . frama - c . com / download / wp - manual - Neon - 20140301.pdf.
shell module load opam-softs init-opam
Christophe Garion IN324 Software Validation – deductive methods 127/ 149
max.c
int max(int i, int j) { if (i < j) { return j; } else { return i; } }
shell
frama-c -wp max.c
Christophe Garion IN324 Software Validation – deductive methods 128/ 149
max.c
int max(int i, int j) { if (i < j) { return j; } else { return i; } }
shell
frama-c-gui -wp max.c
Christophe Garion IN324 Software Validation – deductive methods 128/ 149
basic-annotated-max.c
//@ ensures \result == (i < j ? j : i); int max(int i, int j) { if (i < j) { return j; } else { return i; } }
Christophe Garion IN324 Software Validation – deductive methods 129/ 149
annotated-max.c
/*@ requires \valid(i) && \valid(j); @ requires r == \null || \valid(r); @ assigns *r; @ behavior zero: @ assumes r == \null; @ assigns \nothing; @ ensures \result == -1; @ behavior normal: @ assumes \valid(r); @ assigns *r; @ ensures *r == (*i < *j ? *j : *j); @ ensures \result == 0; @*/ int max(int *r, int* i, int* j) { if (!r) return -1; if (*i < *j) { *r = *j; return 0; } *r = *i; return 0; }
Christophe Garion IN324 Software Validation – deductive methods 131/ 149
Christophe Garion IN324 Software Validation – deductive methods 132/ 149
Christophe Garion IN324 Software Validation – deductive methods 133/ 149
10 Introduction on automated verifjcation 11 Automated theorem proving 12 Generating verifjcation conditions 13 Annotation language for C programs
Christophe Garion IN324 Software Validation – deductive methods 134/ 149
//@ ACSL annotation /*@ @ ACSL annotation @*/
Christophe Garion IN324 Software Validation – deductive methods 135/ 149
\cos, \sqrt etc.
&&
||
==>
<==>
\forall
\exists
Christophe Garion IN324 Software Validation – deductive methods 136/ 149
integer
real
boolean
Christophe Garion IN324 Software Validation – deductive methods 137/ 149
//@ assert logical_assertion;
Christophe Garion IN324 Software Validation – deductive methods 138/ 149
\old(e)
\result
//@ requires P; P is a precondition of the function //@ ensures Q; Q is a postcondition of the function
Christophe Garion IN324 Software Validation – deductive methods 139/ 149
//@ loop invariant P; P must hold before entering the
//@ loop variant E;
Christophe Garion IN324 Software Validation – deductive methods 140/ 149
assigns statement can (should!) be used to help provers. They precise
//@ assigns a;
//@ loop assigns a;
\nothing can be used with assigns clauses.
Christophe Garion IN324 Software Validation – deductive methods 141/ 149
assigns \everything by default.
Christophe Garion IN324 Software Validation – deductive methods 142/ 149
Christophe Garion IN324 Software Validation – deductive methods 143/ 149
//@ predicate predicate_name(par.) = definition
//@ predicate is_positive(integer x) = x > 0;
Christophe Garion IN324 Software Validation – deductive methods 144/ 149
//@ lemma lemma_name: wff //@ axiom axiom_name: wff
/*@ axiom div_mul_identity: @ \forall real x, real y: y != 0.0 ==> y*(x/y) == x; @*/
Christophe Garion IN324 Software Validation – deductive methods 145/ 149
Christophe Garion IN324 Software Validation – deductive methods 146/ 149
\valid(p)
\valid(p+(n..m))
\separated(p, q)
\separated(p+(n..m), q+(i..j))
Christophe Garion IN324 Software Validation – deductive methods 147/ 149
Christophe Garion IN324 Software Validation – deductive methods 148/ 149
\at(e,id) refers to the value of expression e at label id. id can be a regular C label or a label added with a ghost statement or
Pre
Here
Old
Post
LoopEntry
LoopCurrent