Desarrollo de Software con Verificadores Deductivos ...

61
Software Development with Automatic Deductive Verifiers Desarrollo de Software con Verificadores Deductivos Autom ´ aticos Paqui Lucio Dpto de Lenguajes y Sistemas Inform´ aticos. Valencia, 15 de Diciembre de 2016 Paqui Lucio Software Development with Automatic Deductive Veri

Transcript of Desarrollo de Software con Verificadores Deductivos ...

Page 1: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 1/ 35

Desarrollo de Software conVerificadores Deductivos Automaticos

Paqui Lucio

Dpto de Lenguajes y Sistemas Informaticos.

Valencia, 15 de Diciembre de 2016

Paqui Lucio Software Development with Automatic Deductive Verifiers 1/ 35

Page 2: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 2/ 35

From Turing to Hoare Logic

Turing 1949: Checking a large routinePrograms can be mathematically described and analyzed.

Naur 1966: Proof of algorithms by general snapshotsThe need for a proof arises because we want to convinceourselves that the algorithm we have written is correct.

Floyd 1967: Assigning meaning to programsA “formally verified” program is one whose correctness can beproved mathematically.

Hoare 1969: An axiomatic basis for computer programmingA “correctness proof” is made w.r.t. a mathematicaldescription (or specification) of what the program is intendedto do (for all possible values of its input).

Paqui Lucio Software Development with Automatic Deductive Verifiers 2/ 35

Page 3: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 2/ 35

From Turing to Hoare Logic

Turing 1949: Checking a large routinePrograms can be mathematically described and analyzed.

Naur 1966: Proof of algorithms by general snapshotsThe need for a proof arises because we want to convinceourselves that the algorithm we have written is correct.

Floyd 1967: Assigning meaning to programsA “formally verified” program is one whose correctness can beproved mathematically.

Hoare 1969: An axiomatic basis for computer programmingA “correctness proof” is made w.r.t. a mathematicaldescription (or specification) of what the program is intendedto do (for all possible values of its input).

Paqui Lucio Software Development with Automatic Deductive Verifiers 2/ 35

Page 4: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 2/ 35

From Turing to Hoare Logic

Turing 1949: Checking a large routinePrograms can be mathematically described and analyzed.

Naur 1966: Proof of algorithms by general snapshotsThe need for a proof arises because we want to convinceourselves that the algorithm we have written is correct.

Floyd 1967: Assigning meaning to programsA “formally verified” program is one whose correctness can beproved mathematically.

Hoare 1969: An axiomatic basis for computer programmingA “correctness proof” is made w.r.t. a mathematicaldescription (or specification) of what the program is intendedto do (for all possible values of its input).

Paqui Lucio Software Development with Automatic Deductive Verifiers 2/ 35

Page 5: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 3/ 35

Is program P correct? ≡ Does P satisfy its specification S?

P is written in a programming language PL

S is written in a specification language SL

Axiomatic semantics of PL: Set of rules for establishing that Psatisfies S

Paqui Lucio Software Development with Automatic Deductive Verifiers 3/ 35

Page 6: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 4/ 35

Is program P correct? ≡ Does P satisfy its specification S?

P is written in a programming language PL

While-Programs

S is written in a specification language SL

First-Order Formulas

Axiomatic semantics of PL: Set of rules for establishing that Psatisfies S

Hoare Formal System (a.k.a.Hoare Logic)

Paqui Lucio Software Development with Automatic Deductive Verifiers 4/ 35

Page 7: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 5/ 35

While-Programs

Null: skip

(Simoultaneous) Assignment: x1, . . . , xn := t1, . . . , tn

Sequential Composition: P1;P2

Conditional: if b then P1 else P2

Iteration: while b do P

Example: r := 0;while (r+1)∗(r+1) ≤ x

do

r := r+1

Paqui Lucio Software Development with Automatic Deductive Verifiers 5/ 35

Page 8: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 6/ 35

First-Order Logic (FOL)

First-order syntax:

ϕ ::= p(t) | ¬ϕ | ϕ1 ∧ ϕ2 | ϕ1 ∨ ϕ2 | ϕ1 → ϕ2 | ∀x(ϕ) | ∃x(ϕ)

∀n( (n > 2 ∧ even(n))→∃x∃y(prime(x) ∧ prime(y) ∧ n = x+ y))

ϕ[t/x] stands for the formula that results by (simultaneously)replace every free occurrence of each xi in ϕ by ti.

∃z(x∗y = 2∗z)[y+1, x−1/x, y] = ∃z((y+1)∗(x−1) = 2∗z)

A formula is valid if it is true in any model (i.p. for any valueof its variables).

Paqui Lucio Software Development with Automatic Deductive Verifiers 6/ 35

Page 9: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 6/ 35

First-Order Logic (FOL)

First-order syntax:

ϕ ::= p(t) | ¬ϕ | ϕ1 ∧ ϕ2 | ϕ1 ∨ ϕ2 | ϕ1 → ϕ2 | ∀x(ϕ) | ∃x(ϕ)

∀n( (n > 2 ∧ even(n))→∃x∃y(prime(x) ∧ prime(y) ∧ n = x+ y))

ϕ[t/x] stands for the formula that results by (simultaneously)replace every free occurrence of each xi in ϕ by ti.

∃z(x∗y = 2∗z)[y+1, x−1/x, y] = ∃z((y+1)∗(x−1) = 2∗z)

A formula is valid if it is true in any model (i.p. for any valueof its variables).

Paqui Lucio Software Development with Automatic Deductive Verifiers 6/ 35

Page 10: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 6/ 35

First-Order Logic (FOL)

First-order syntax:

ϕ ::= p(t) | ¬ϕ | ϕ1 ∧ ϕ2 | ϕ1 ∨ ϕ2 | ϕ1 → ϕ2 | ∀x(ϕ) | ∃x(ϕ)

∀n( (n > 2 ∧ even(n))→∃x∃y(prime(x) ∧ prime(y) ∧ n = x+ y))

ϕ[t/x] stands for the formula that results by (simultaneously)replace every free occurrence of each xi in ϕ by ti.

∃z(x∗y = 2∗z)[y+1, x−1/x, y] = ∃z((y+1)∗(x−1) = 2∗z)

A formula is valid if it is true in any model (i.p. for any valueof its variables).

Paqui Lucio Software Development with Automatic Deductive Verifiers 6/ 35

Page 11: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 7/ 35

Axiomatic Semantics

Hoare triple {ϕ}P{ψ} says that if program P is started in (astate satisfying) ϕ, then P terminates (if it does) in a statethat satisfies ψ.

{m ≤ n} j := (m+ n)/2 {m ≤ j ≤ n}

{False} skip {∀n((n > 2 ∧ even(n))→∃x∃y(prime(x) ∧ prime(y) ∧ n = x+ y))}

Paqui Lucio Software Development with Automatic Deductive Verifiers 7/ 35

Page 12: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 8/ 35

Hoare Formal System

{ψ[t/x]} x := t {ψ}ϕ→ ψ

{ϕ}skip{ψ}ϕ→ ϕ1 {ϕ1}P{ψ}

{ϕ}P{ψ}

{ϕ}P1{α} {α}P2{ψ}{ϕ}P1;P2{ψ}

{ϕ ∧ b}P1{ψ} {ϕ ∧ ¬b}P2{ψ}{ϕ}if b then P1 else P2 {ψ}

{Inv ∧ b}P{Inv} (Inv ∧ ¬b)→ ψ

{Inv}while b do P{ψ}

Paqui Lucio Software Development with Automatic Deductive Verifiers 8/ 35

Page 13: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 9/ 35

{x ≥ 0}r := 0;while (r+1)∗(r+1) ≤ x

//invariant ???do

r := r+1

{r∗r ≤ x < (r+ 1) ∗ (r+ 1)}

r ≤√x < r+ 1

x r (r + 1) ∗ (r + 1) ≤ x

13 0 1 ∗ 1 ≤ 1313 1 2 ∗ 2 ≤ 1313 2 3 ∗ 3 ≤ 1313 3 4 ∗ 4 > 13

invariant r ∗ r ≤ x

Paqui Lucio Software Development with Automatic Deductive Verifiers 9/ 35

Page 14: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 9/ 35

{x ≥ 0}r := 0;while (r+1)∗(r+1) ≤ x

//invariant ???do

r := r+1

{r∗r ≤ x < (r+ 1) ∗ (r+ 1)}r ≤√x < r+ 1

x r (r + 1) ∗ (r + 1) ≤ x

13 0 1 ∗ 1 ≤ 1313 1 2 ∗ 2 ≤ 1313 2 3 ∗ 3 ≤ 1313 3 4 ∗ 4 > 13

invariant r ∗ r ≤ x

Paqui Lucio Software Development with Automatic Deductive Verifiers 9/ 35

Page 15: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 9/ 35

{x ≥ 0}r := 0;while (r+1)∗(r+1) ≤ x

//invariant ???do

r := r+1

{r∗r ≤ x < (r+ 1) ∗ (r+ 1)}r ≤√x < r+ 1

x r (r + 1) ∗ (r + 1) ≤ x

13 0 1 ∗ 1 ≤ 1313 1 2 ∗ 2 ≤ 1313 2 3 ∗ 3 ≤ 1313 3 4 ∗ 4 > 13

invariant r ∗ r ≤ x

Paqui Lucio Software Development with Automatic Deductive Verifiers 9/ 35

Page 16: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 9/ 35

{x ≥ 0}r := 0;while (r+1)∗(r+1) ≤ x

//invariant ???do

r := r+1

{r∗r ≤ x < (r+ 1) ∗ (r+ 1)}r ≤√x < r+ 1

x r (r + 1) ∗ (r + 1) ≤ x

13 0 1 ∗ 1 ≤ 1313 1 2 ∗ 2 ≤ 1313 2 3 ∗ 3 ≤ 1313 3 4 ∗ 4 > 13

invariant r ∗ r ≤ x

Paqui Lucio Software Development with Automatic Deductive Verifiers 9/ 35

Page 17: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 10/ 35

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 10/ 35

Page 18: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 11/ 35

do

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 11/ 35

Page 19: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 12/ 35

do

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 12/ 35

Page 20: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 13/ 35

do

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 13/ 35

Page 21: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 14/ 35

do

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 14/ 35

Page 22: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 15/ 35

do

do

Paqui Lucio Software Development with Automatic Deductive Verifiers 15/ 35

Page 23: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 16/ 35

From Hoare Logic to VCG

“Thus the practice of proving programs would seem to lead to

solution of three of the most pressing problems in software and

programming, namely, reliability, documentation, and

compatibility. However, program proving, certainly at present,

will be difficult even for programmers of high caliber; and may

be applicable only to quite simple program designs.”

C. Antony R. HoareAn axiomatic basis for computer programming

1969

Paqui Lucio Software Development with Automatic Deductive Verifiers 16/ 35

Page 24: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 17/ 35

Dijkstra Weakest Precondition

The idea firstly appears in the paper Guarded commands,nondeterminacy and formal derivation of programs, E.W. Dijkstra, 1975.

Hoare Logic:Given a precondition ϕ, a code fragment P and a postcondition ψ,is {ϕ}P{ψ} true?

Dijkstra Weakest Precondition:Given a code fragment P and postcondition ψ, find the uniqueformula wp(P,ψ) which is the weakest precondition for P and ψ.

weakest: ϕ→ wp(P,ψ) is valid for any ϕ such that{ϕ}P{ψ} is true.

Each experienced mathematician knows that achievements dependcritically on the availability of suitable notations.

E.W. Dijkstra, My hopes of computing science, 1979

Paqui Lucio Software Development with Automatic Deductive Verifiers 17/ 35

Page 25: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 17/ 35

Dijkstra Weakest Precondition

The idea firstly appears in the paper Guarded commands,nondeterminacy and formal derivation of programs, E.W. Dijkstra, 1975.

Hoare Logic:Given a precondition ϕ, a code fragment P and a postcondition ψ,is {ϕ}P{ψ} true?

Dijkstra Weakest Precondition:Given a code fragment P and postcondition ψ, find the uniqueformula wp(P,ψ) which is the weakest precondition for P and ψ.

weakest: ϕ→ wp(P,ψ) is valid for any ϕ such that{ϕ}P{ψ} is true.

Each experienced mathematician knows that achievements dependcritically on the availability of suitable notations.

E.W. Dijkstra, My hopes of computing science, 1979

Paqui Lucio Software Development with Automatic Deductive Verifiers 17/ 35

Page 26: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 18/ 35

wp(x := t, ψ) = ψ[t/x]

For example:

wp(x, y := y+ 1, x− 1, x ∗ y = 0)= (y + 1) ∗ (x− 1) = 0= (y = −1) ∨ (x = 1) is weaker than x = 1

is weaker than (y = −1) ∧ (x = 1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 18/ 35

Page 27: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 18/ 35

wp(x := t, ψ) = ψ[t/x]

For example:

wp(x, y := y+ 1, x− 1, x ∗ y = 0)= (y + 1) ∗ (x− 1) = 0

= (y = −1) ∨ (x = 1) is weaker than x = 1is weaker than (y = −1) ∧ (x = 1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 18/ 35

Page 28: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 18/ 35

wp(x := t, ψ) = ψ[t/x]

For example:

wp(x, y := y+ 1, x− 1, x ∗ y = 0)= (y + 1) ∗ (x− 1) = 0= (y = −1) ∨ (x = 1)

is weaker than x = 1is weaker than (y = −1) ∧ (x = 1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 18/ 35

Page 29: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 18/ 35

wp(x := t, ψ) = ψ[t/x]

For example:

wp(x, y := y+ 1, x− 1, x ∗ y = 0)= (y + 1) ∗ (x− 1) = 0= (y = −1) ∨ (x = 1) is weaker than x = 1

is weaker than (y = −1) ∧ (x = 1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 18/ 35

Page 30: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 18/ 35

wp(x := t, ψ) = ψ[t/x]

For example:

wp(x, y := y+ 1, x− 1, x ∗ y = 0)= (y + 1) ∗ (x− 1) = 0= (y = −1) ∨ (x = 1) is weaker than x = 1

is weaker than (y = −1) ∧ (x = 1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 18/ 35

Page 31: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 19/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

For example:

wp(x:=y+1; y:=x-1, x ∗ y = 0)

= wp(x:=y+1, wp(y:=x-1, x ∗ y = 0))= wp(x:=y+1, x ∗ (x− 1) = 0)= (y + 1) ∗ y = 0= (y = −1) ∨ (y = 0)

Paqui Lucio Software Development with Automatic Deductive Verifiers 19/ 35

Page 32: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 19/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

For example:

wp(x:=y+1; y:=x-1, x ∗ y = 0)= wp(x:=y+1, wp(y:=x-1, x ∗ y = 0))

= wp(x:=y+1, x ∗ (x− 1) = 0)= (y + 1) ∗ y = 0= (y = −1) ∨ (y = 0)

Paqui Lucio Software Development with Automatic Deductive Verifiers 19/ 35

Page 33: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 19/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

For example:

wp(x:=y+1; y:=x-1, x ∗ y = 0)= wp(x:=y+1, wp(y:=x-1, x ∗ y = 0))= wp(x:=y+1, x ∗ (x− 1) = 0)

= (y + 1) ∗ y = 0= (y = −1) ∨ (y = 0)

Paqui Lucio Software Development with Automatic Deductive Verifiers 19/ 35

Page 34: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 19/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

For example:

wp(x:=y+1; y:=x-1, x ∗ y = 0)= wp(x:=y+1, wp(y:=x-1, x ∗ y = 0))= wp(x:=y+1, x ∗ (x− 1) = 0)= (y + 1) ∗ y = 0

= (y = −1) ∨ (y = 0)

Paqui Lucio Software Development with Automatic Deductive Verifiers 19/ 35

Page 35: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 19/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

For example:

wp(x:=y+1; y:=x-1, x ∗ y = 0)= wp(x:=y+1, wp(y:=x-1, x ∗ y = 0))= wp(x:=y+1, x ∗ (x− 1) = 0)= (y + 1) ∗ y = 0= (y = −1) ∨ (y = 0)

Paqui Lucio Software Development with Automatic Deductive Verifiers 19/ 35

Page 36: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 20/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

For example:

wp(if x ≥ y then z:=x else z:=y, z = max(x, y))

= (x ≥ y → wp(z:=x, z = max(x, y))) ∧(¬(x ≥ y)→ wp(z:=y, z = max(x, y)))

= (x ≥ y → x = max(x, y)) ∧ (¬(x ≥ y)→ y = max(x, y))

Paqui Lucio Software Development with Automatic Deductive Verifiers 20/ 35

Page 37: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 20/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

For example:

wp(if x ≥ y then z:=x else z:=y, z = max(x, y))= (x ≥ y → wp(z:=x, z = max(x, y))) ∧

(¬(x ≥ y)→ wp(z:=y, z = max(x, y)))

= (x ≥ y → x = max(x, y)) ∧ (¬(x ≥ y)→ y = max(x, y))

Paqui Lucio Software Development with Automatic Deductive Verifiers 20/ 35

Page 38: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 20/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

For example:

wp(if x ≥ y then z:=x else z:=y, z = max(x, y))= (x ≥ y → wp(z:=x, z = max(x, y))) ∧

(¬(x ≥ y)→ wp(z:=y, z = max(x, y)))= (x ≥ y → x = max(x, y)) ∧ (¬(x ≥ y)→ y = max(x, y))

Paqui Lucio Software Development with Automatic Deductive Verifiers 20/ 35

Page 39: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 21/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

wp(while (r+1)∗(r+1) ≤ x do r := r+1,r ∗ r ≤ x < (r+1)∗(r+1)) = r∗r ≤ x provided that

(r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → wp(r:=r+1, r∗r ≤ x)

( r∗r ≤ x ∧¬( (r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 21/ 35

Page 40: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 21/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

wp(while (r+1)∗(r+1) ≤ x do r := r+1,r ∗ r ≤ x < (r+1)∗(r+1)) = r∗r ≤ x provided that

(r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → wp(r:=r+1, r∗r ≤ x)

( r∗r ≤ x ∧¬( (r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 21/ 35

Page 41: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 21/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

wp(while (r+1)∗(r+1) ≤ x do r := r+1,r ∗ r ≤ x < (r+1)∗(r+1)) = r∗r ≤ x provided that

(r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → wp(r:=r+1, r∗r ≤ x)

( r∗r ≤ x ∧¬( (r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 21/ 35

Page 42: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 21/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

wp(while (r+1)∗(r+1) ≤ x do r := r+1,r ∗ r ≤ x < (r+1)∗(r+1)) = r∗r ≤ x provided that

(r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → wp(r:=r+1, r∗r ≤ x)

( r∗r ≤ x ∧¬( (r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 21/ 35

Page 43: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 22/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

wp(while (r+1)∗(r+1) ≤ x do r := r+1,r ∗ r ≤ x < (r+1)∗(r+1)) = r∗r ≤ x provided that

(r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → (r+1)∗(r+1) ≤ x

( r∗r ≤ x ∧¬( (r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1)

Paqui Lucio Software Development with Automatic Deductive Verifiers 22/ 35

Page 44: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 23/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

{ϕ}P{ψ} iff

ϕ→ wp(P,ψ) and

all the provisos for calculating wp(P,ψ)

are valid (implications) formulas.

Paqui Lucio Software Development with Automatic Deductive Verifiers 23/ 35

Page 45: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 23/ 35

wp(x := t, ψ) = ψ[t/x]

wp(P1; P2, ψ) = wp(P1, wp(P2, ψ))

wp(if b then P1 else P2, ψ) = (b→ wp(P1, ψ)) ∧(¬b→ wp(P2, ψ))

wp(skip, ψ) = ψ

wp(while b do P , ψ) = α provided that

(α ∧ b)→ wp(P,α)(α ∧ ¬b)→ ψ

{ϕ}P{ψ} iff

ϕ→ wp(P,ψ) and

all the provisos for calculating wp(P,ψ)

are valid (implications) formulas.

Paqui Lucio Software Development with Automatic Deductive Verifiers 23/ 35

Page 46: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 24/ 35

Verification Condition Generation

VCG({ϕ}P{ψ}) = { ϕ→ wp(P ,ψ) } ∪ vc(P,ψ)

where

vc(x := t, ψ) = vc(skip, ψ) = ∅vc(P1; P2, ψ) = vc(P1, wp(P2, ψ)) ∪ vc(P2, ψ)

vc(if b then P1 else P2, ψ) = vc(P1, ψ) ∪ vc(P2, ψ)

vc(while b do P, ψ) ={ (Inv ∧ b)→ wp(P,Inv),

(Inv ∧ ¬b)→ ψ }∪ vc(P ,Inv)

Inv is the (inferred/user-defined) invariant of the iterationwhile b do P

Paqui Lucio Software Development with Automatic Deductive Verifiers 24/ 35

Page 47: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 25/ 35

VCG computes the set of all the FOL-implications of a Hoareproof-tree.

VCG ( { x ≥ 0 }r:= 0;while (r+1)∗(r+1) ≤ x do r := r+1{ r ∗ r ≤ x < (r+1)∗(r+1) } )

=

{ (r∗r ≤ x ∧ (r+1)∗(r+1) ≤ x) → (r+1)∗(r+1) ≤ x ,

(r∗r ≤ x ∧¬((r+1)∗(r+1) ≤ x)) → r ∗ r ≤ x < (r+1)∗(r+1) }

RootApprox.dfy

Paqui Lucio Software Development with Automatic Deductive Verifiers 25/ 35

Page 48: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 26/ 35

From VCG to Current Deductive Verification Tools

Hoare’s Verification Grand Challenge:

This contribution ... revives an old challenge: the constructionand application of a verifying compiler that guaranteescorrectness of a program before running it.Correctness of computer programs is the fundamental concernof the theory of programming and of its application inlarge-scale software engineering.

Tony HoareThe Verifying Compiler: A Grand Challenge for Computing Research

2003

Paqui Lucio Software Development with Automatic Deductive Verifiers 26/ 35

Page 49: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 27/ 35

Deductive Verification:

Logical reasoning (deduction) is used to prove properties.Expressive (at least first-order) logic.Contract-based specifications (standard approach)

Arquitectures in deductive verification:

1 On top of interactive proof assistants

Isabelle/HOL, Coq, HOL Ligth, PVS, ...

2 Automatic Program Verifiers

1 Program logics for a specific target language

ACL2, KeY, KIV, VeriFun, ...

2 VCG + Automatic theorem provers (SMT-solver)

Spark, Verifast, Dafny, Why, Frama-C, ...

Paqui Lucio Software Development with Automatic Deductive Verifiers 27/ 35

Page 50: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 27/ 35

Deductive Verification:

Logical reasoning (deduction) is used to prove properties.Expressive (at least first-order) logic.Contract-based specifications (standard approach)

Arquitectures in deductive verification:

1 On top of interactive proof assistants

Isabelle/HOL, Coq, HOL Ligth, PVS, ...

2 Automatic Program Verifiers

1 Program logics for a specific target language

ACL2, KeY, KIV, VeriFun, ...

2 VCG + Automatic theorem provers (SMT-solver)

Spark, Verifast, Dafny, Why, Frama-C, ...

Paqui Lucio Software Development with Automatic Deductive Verifiers 27/ 35

Page 51: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 27/ 35

Deductive Verification:

Logical reasoning (deduction) is used to prove properties.Expressive (at least first-order) logic.Contract-based specifications (standard approach)

Arquitectures in deductive verification:

1 On top of interactive proof assistants

Isabelle/HOL, Coq, HOL Ligth, PVS, ...

2 Automatic Program Verifiers

1 Program logics for a specific target language

ACL2, KeY, KIV, VeriFun, ...

2 VCG + Automatic theorem provers (SMT-solver)

Spark, Verifast, Dafny, Why, Frama-C, ...

Paqui Lucio Software Development with Automatic Deductive Verifiers 27/ 35

Page 52: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 28/ 35

(Annotated) Program

Compiler

Syntactically Correct Program

(r*r <= x && (r+1)*(r+1) <= x) (r+1)*(r+1) <= x (r*r <= x && !((r+1)*(r+1) <= x)) r*r <= x < (r+1)*(r+1)

VCG

Automated Theorem Prover

Correct Program

Syntax-error report

Semantic-error report

Paqui Lucio Software Development with Automatic Deductive Verifiers 28/ 35

Page 53: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 29/ 35

Example: A more interesting invariant

Binary Search

Paqui Lucio Software Development with Automatic Deductive Verifiers 29/ 35

Page 54: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 30/ 35

“The future looks bright for the collaboration of verification

and reasoning. Recent advances in both fields and increasingly

tight interaction have already given rise to industrially relevant

verification tools. We predict that this is only the beginning,

and that within a decade tools based on verification

technology will be as useful and widespread for software

development as they are today in the hardware domain.”

Bernhard Beckert & Reiner HahnleReasoning and Verification: State of the Art and Current Trends1

IEEE Intelligent System

2014

1This paper provides a representative selection of 27 verification systems.Paqui Lucio Software Development with Automatic Deductive Verifiers 30/ 35

Page 55: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 31/ 35

Timsort is broken by S. de Gouw, F. de Boer, and J. Rot (2015)

Paqui Lucio Software Development with Automatic Deductive Verifiers 31/ 35

Page 56: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 32/ 35

Dafny is an automatic program verifier (VCG + SMT-solver).

Dafny is being developed by Microsoft Research.

Dafny 1.9.8 (August 31, 2016) is the 14th stable release,

since Oct 30, 2012.

Dafny encourages using best-practice programming styles, inparticular the design by contract approach.

Dafny provides

Design-time feedbackFluid interaction

for accessible integrated verification.

Dafny generates executable (.NET) code, omittingspecification (ghost) constructs.

Paqui Lucio Software Development with Automatic Deductive Verifiers 32/ 35

Page 57: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 33/ 35

Using assert and assume in software development

assert ϕDafny first tries to prove ϕ, and if successful, then ϕ can beused in the rest of proof.Provides a non-instantiable lemma ϕ:A property that is previously and separately proved and helpsto prove other properties.

assume ϕDafny assumes that ϕ is true: without proving ϕ it is enabledto use ϕ in the current proof.Dafny does not consider verified any file with one assume.

In verified software development:assume ϕ for checking if ϕ is the required property;If OK then change from assume to assert;If ”assertion violation” then ϕ must be proved in a lemma orsome previous assert(s) must be inserted.

Paqui Lucio Software Development with Automatic Deductive Verifiers 33/ 35

Page 58: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 33/ 35

Using assert and assume in software development

assert ϕDafny first tries to prove ϕ, and if successful, then ϕ can beused in the rest of proof.Provides a non-instantiable lemma ϕ:A property that is previously and separately proved and helpsto prove other properties.

assume ϕDafny assumes that ϕ is true: without proving ϕ it is enabledto use ϕ in the current proof.Dafny does not consider verified any file with one assume.

In verified software development:assume ϕ for checking if ϕ is the required property;If OK then change from assume to assert;If ”assertion violation” then ϕ must be proved in a lemma orsome previous assert(s) must be inserted.

Paqui Lucio Software Development with Automatic Deductive Verifiers 33/ 35

Page 59: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 33/ 35

Using assert and assume in software development

assert ϕDafny first tries to prove ϕ, and if successful, then ϕ can beused in the rest of proof.Provides a non-instantiable lemma ϕ:A property that is previously and separately proved and helpsto prove other properties.

assume ϕDafny assumes that ϕ is true: without proving ϕ it is enabledto use ϕ in the current proof.Dafny does not consider verified any file with one assume.

In verified software development:assume ϕ for checking if ϕ is the required property;If OK then change from assume to assert;If ”assertion violation” then ϕ must be proved in a lemma orsome previous assert(s) must be inserted.

Paqui Lucio Software Development with Automatic Deductive Verifiers 33/ 35

Page 60: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 34/ 35

Example: The development of a verified quicksort

QuickSort

Paqui Lucio Software Development with Automatic Deductive Verifiers 34/ 35

Page 61: Desarrollo de Software con Verificadores Deductivos ...

Software Development with Automatic Deductive Verifiers 35/ 35

The beauty of a theorem from mathematics,the preciseness of an inference rule in logic,the intrigue of a puzzle,and the challenge of a game – all are presentin the field of automated reasoning.

(Larry Wos, 1988)

Paqui Lucio Software Development with Automatic Deductive Verifiers 35/ 35