Mechanized Verifjcationof the Correctness and Asymptotic Complexity - - PowerPoint PPT Presentation

mechanized verifjcationof the correctness and asymptotic
SMART_READER_LITE
LIVE PREVIEW

Mechanized Verifjcationof the Correctness and Asymptotic Complexity - - PowerPoint PPT Presentation

Mechanized Verifjcationof the Correctness and Asymptotic Complexity of Programs Armal Guneau under the supervision of Arthur Charguraud and Franois Pottier Computerprograms: cooking recipes,but forcomputers? Momseasy apple pie 3/4T


slide-1
SLIDE 1

Mechanized Verifjcationof the Correctness and Asymptotic Complexity of Programs

Armaël Guéneau under the supervision of Arthur Charguéraud and François Pottier

slide-2
SLIDE 2

Computerprograms: cooking recipes,but forcomputers?

Mom’seasy apple pie

  • Slice 6 apples
  • Mix with 3/4C sugar, 2T flour,

3/4T cinnamon, 1T lemon juice

  • Transfer between two pie crusts
  • Bake 40 min at 425°F

Computing the lengths of two lists

let length_sum l1 l2 = let x = length l1 in let y = length l2 in x + y 1/40

slide-3
SLIDE 3

Computerprograms: cooking recipes,but forcomputers?

Mom’seasy apple pie

  • Slice 6 apples
  • Mix with 3/4C sugar, 2T flour,

3/4T cinnamon, 1T lemon juice

  • Transfer between two pie crusts
  • Bake 40 min at 425°F

Computing the lengths of two lists

let length_sum l1 l2 = let x = length l1 in let y = length l2 in x + y 1/40

slide-4
SLIDE 4

Computer: cooking recipes,but forcomputers? (2)

Real-world programs are usually very large. Can one trust the execution of that code to “do the right thing”? What does it mean to do the right thing? “The right thing”: a specifjcation, written in a formal language.

2/40

slide-5
SLIDE 5

Computer: cooking recipes,but forcomputers? (2)

Real-world programs are usually very large. Can one trust the execution of that code to “do the right thing”? What does it mean to do the right thing? “The right thing”: a specifjcation, written in a formal language.

2/40

slide-6
SLIDE 6

Computer: cooking recipes,but forcomputers? (2)

Real-world programs are usually very large. Can one trust the execution of that code to “do the right thing”? What does it mean to do the right thing? “The right thing”: a specifjcation, written in a formal language.

2/40

slide-7
SLIDE 7

Computer: cooking recipes,but forcomputers? (2)

Real-world programs are usually very large. Can one trust the execution of that code to “do the right thing”? What does it mean to do the right thing? “The right thing”: a specifjcation, written in a formal language.

2/40

slide-8
SLIDE 8

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-9
SLIDE 9

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-10
SLIDE 10

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-11
SLIDE 11

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-12
SLIDE 12

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-13
SLIDE 13

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-14
SLIDE 14

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-15
SLIDE 15

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-16
SLIDE 16

less confidence more confidence

Whatdo we expect froma program?

Safety (does not crash) Partial correctness (returns a correct result; might not terminate) Total correctness (always returns a correct result) Complexity bound (runs in a predictable amount of time) Real-time bound (runs within a precise time budget) Security (e.g. timing side channel) Fault tolerant (resists to hardware faults)

in this work

3/40

slide-17
SLIDE 17

Anillustrative example: Binary Search

Consider a sorted array of integers: Question: is 27 in the array? If so, at which index?

4/40

slide-18
SLIDE 18

Anillustrative example: Binary Search(2)

At each step, reduce by half the segment to search by comparing 27 with the middle element.

5/40

slide-19
SLIDE 19

Atentative binary searchimplementation

(* search in array a for x, in the range [i, j) *) (* returns the index of x, or -1 if not found *) let rec bsearch (a: int array) x i j = if j <= i then -1 else let k = i + (j - i) / 2 in if x = a.(k) then k else if x < a.(k) then bsearch a x i k else bsearch a x (i+1) j

  • We can test this program on example input data
  • We can formally prove its (total) functional correctness
  • Yet, something is wrong...

6/40

slide-20
SLIDE 20

Atentative binary searchimplementation

(* search in array a for x, in the range [i, j) *) (* returns the index of x, or -1 if not found *) let rec bsearch (a: int array) x i j = if j <= i then -1 else let k = i + (j - i) / 2 in if x = a.(k) then k else if x < a.(k) then bsearch a x i k else bsearch a x (i+1) j

  • We can test this program on example input data
  • We can formally prove its (total) functional correctness
  • Yet, something is wrong...

6/40

slide-21
SLIDE 21

Atentative binary searchimplementation(2)

On an array containaing 1 billion elements:

  • A correct binary search should do at most 30 recursive calls

(230 » 1 billion)

  • On some inputs, the code shown performs 1 billion recursive calls

7/40

slide-22
SLIDE 22

Atentative binary searchimplementation(3)

(* search in array a for x, in the range [i, j) *) (* returns the index of x, or -1 if not found *) let rec bsearch (a: int array) x i j = if j <= i then -1 else let k = i + (j - i) / 2 in if x = a.(k) then k else if x < a.(k) then bsearch a x i k else bsearch a x (i+1) j

buggy, should be k+1

8/40

slide-23
SLIDE 23

Atentative binary searchimplementation(4)

In summary, on an array of size n:

  • We expect Oplog nq recursive calls;
  • But our program does up to n recursive calls.

9/40

slide-24
SLIDE 24

Atentative binary searchimplementation(4)

In summary, on an array of size n:

  • We expect Oplog nq recursive calls;
  • But our program does up to n recursive calls.

9/40

slide-25
SLIDE 25

Formal verifjcation of correctnessandcomplexityof a program

Step1 State a programspecifjcation that characterizes the intended behavior: functional correctness and runtime complexity Step2 Prove a theorem relating concrete code to the specifjcation Two kinds of possible human mistakes:

  • in math results used in the analysis; or
  • when relating the concrete code to the abstract algorithm

Use a proofassistant (Coq) to mechanically check every step of the proof

10/40

slide-26
SLIDE 26

Formal verifjcation of correctnessandcomplexityof a program

Step1 State a programspecifjcation that characterizes the intended behavior: functional correctness and runtime complexity Step2 Prove a theorem relating concrete code to the specifjcation Two kinds of possible human mistakes:

  • in math results used in the analysis; or
  • when relating the concrete code to the abstract algorithm

Use a proofassistant (Coq) to mechanically check every step of the proof

10/40

slide-27
SLIDE 27

Formal verifjcation of correctnessandcomplexityof a program

Step1 State a programspecifjcation that characterizes the intended behavior: functional correctness and runtime complexity Step2 Prove a theorem relating concrete code to the specifjcation Two kinds of possible human mistakes:

  • in math results used in the analysis; or
  • when relating the concrete code to the abstract algorithm

Use a proofassistant (Coq) to mechanically check every step of the proof

10/40

slide-28
SLIDE 28

Howdo we specify a program’srunningtime?

Option 1: as an upper bound on the wall-clock time. Useful for embedded systems, but not realistic for commodity hardware. Option 2: as a number of cycles for an idealized machine model. Knuth: “Merge sort runs in . [This bound] can be re- duced to at the expense of a somewhat longer program.” Option 3: as a number of function calls in a high-level language. More abstract, but still has modularity issues.

11/40

slide-29
SLIDE 29

Howdo we specify a program’srunningtime?

Option 1: as an upper bound on the wall-clock time. Useful for embedded systems, but not realistic for commodity hardware. Option 2: as a number of cycles for an idealized machine model. Knuth: “Merge sort runs in 10N log N ` 4.92N. [This bound] can be re- duced to 9N log N at the expense of a somewhat longer program.” Option 3: as a number of function calls in a high-level language. More abstract, but still has modularity issues.

11/40

slide-30
SLIDE 30

Howdo we specify a program’srunningtime?

Option 1: as an upper bound on the wall-clock time. Useful for embedded systems, but not realistic for commodity hardware. Option 2: as a number of cycles for an idealized machine model. Knuth: “Merge sort runs in 10N log N ` 4.92N. [This bound] can be re- duced to 9N log N at the expense of a somewhat longer program.” Option 3: as a number of function calls in a high-level language. More abstract, but still has modularity issues.

11/40

slide-31
SLIDE 31

Howdo we specify a program’srunningtime?

Option 4: specify the running time using asymptotic complexity. Describe the “order of growth” of the running time as inputs grow large e.g. Oplog nq, Opnq, Opn log nq, Opn2q, …. Less precise, but informative enough in many cases.

11/40

slide-32
SLIDE 32

Advantagesof asymptotic complexityspecifjcations

Specifjcations capturing asymptotic costs:

  • have been widely applied to a large class of programs and

algorithms;

  • are independent of the machine, runtime system and the details of

the implementation;

  • allow modular reasoning. Abstract over implementation details.

12/40

slide-33
SLIDE 33

Inthis thesis

Goal: specify and prove that programs compute a correct result with a bounded asymptotic runtime. Proofs should be:

  • static;
  • machine-checked;
  • hardware- and runtime- independent;
  • modular.

Contribution: A step forward for the verifjcation of the correctnessandcomplexity of imperative,higher-order programs with subtle invariantsandanalysis, at a reasonable cost.

13/40

slide-34
SLIDE 34

Inthis thesis

Goal: specify and prove that programs compute a correct result with a bounded asymptotic runtime. Proofs should be:

  • static;
  • machine-checked;
  • hardware- and runtime- independent;
  • modular.

Contribution: A step forward for the verifjcation of the correctnessand complexity of imperative,higher-order programs with subtle invariantsand analysis, at a reasonable cost.

13/40

slide-35
SLIDE 35

Details of the contribution

  • 1. A formal account of O()

Existing: single-variate O (math, programs), multi-variate O on paper Contributed: Coq library for single and multi-variate O, with lemmas useful for program analysis

14/40

slide-36
SLIDE 36

Contributions

  • 2. A methodology for complexity proofs

Existing:

  • manual verifjcation without Opq abstraction
  • automated analysis restricted to polynomial bounds

Contributed:

  • general asymptotic bounds
  • with semi-automated cost inference
  • implemented as an extension of CFML

(Separation Logic framework in Coq)

15/40

slide-37
SLIDE 37

Contributions

  • 3. Case studies

Existing: polynomial or logarithmic bounds, simple algorithms (quicksort), or interactive verifjcation without O Contributed: several algorithms, including a state-of-the-art graph algorithm with nontrivial correctness and complexity

16/40

slide-38
SLIDE 38

Outline of the rest of the talk

Reasoning with abstract cost functions Semi-automatic inference of cost functions Separation Logic with Time Credits Case study—an Incremental Cycle Detection Algorithm

17/40

slide-39
SLIDE 39

Reasoningwith abstractcost functions

slide-40
SLIDE 40

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on :

  • :

.

  • :

. …but which statement are we proving?

18/40

slide-41
SLIDE 41

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on j ´ i:

  • :

.

  • :

. …but which statement are we proving?

18/40

slide-42
SLIDE 42

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on j ´ i:

  • j ´ i ď 0:

Op1q.

  • :

. …but which statement are we proving?

18/40

slide-43
SLIDE 43

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on j ´ i:

  • j ´ i ď 0:

Op1q.

  • j ´ i ą 0:

Op1q ` Op1q ` Op1q “ Op1q. …but which statement are we proving?

18/40

slide-44
SLIDE 44

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on j ´ i:

  • j ´ i ď 0:

Op1q.

  • j ´ i ą 0:

Op1q ` Op1q ` Op1q “ Op1q. Where is the catch? …but which statement are we proving?

18/40

slide-45
SLIDE 45

Informal reasoningprinciplesonO canbe abused

1

let rec bsearch a x i j =

2

if j <= i then -1 else

3

let k = i + (j - i) / 2 in

4

if x = a.(k) then k

5

else if x < a.(k) then

6

bsearch a x i k

7

else

8

bsearch a x (k+1) j

Claim:

bsearch a x i j costs Op1q.

Proof: By induction on j ´ i:

  • j ´ i ď 0:

Op1q.

  • j ´ i ą 0:

Op1q ` Op1q ` Op1q “ Op1q. …but which statement are we proving?

18/40

slide-46
SLIDE 46

MeaningofOp1q

What we just proved: @i j , D c , “bsearch a x i j” performs at most c function calls What “ ” means:

bsearch a x i j” performs at most function calls

19/40

slide-47
SLIDE 47

MeaningofOp1q

What we just proved: @i j , D c , “bsearch a x i j” performs at most c function calls What “Op1q” means: D c , @i j , “bsearch a x i j” performs at most c function calls

19/40

slide-48
SLIDE 48

MeaningofOplog nq

Informal specifjcation: “bsearch a x i j” runs in Oplogpj ´ iqq. Meaning: there exists a cost function such that,

  • for every a, x, i, j, “bsearch a x i j” performs at most

function calls

  • .

20/40

slide-49
SLIDE 49

MeaningofOplog nq

Informal specifjcation: “bsearch a x i j” runs in Oplogpj ´ iqq. Meaning: there exists a cost function f such that,

  • for every a, x, i, j, “bsearch a x i j” performs at most fpj ´ iq

function calls

  • f P Opλn. log nq.

20/40

slide-50
SLIDE 50

Construction of the cost function

Option 1: The user somehow guesses a suitable cost function. Here, “λn. 3 log n ` 4” works. Option 2: Semi-automatically construct the cost function as the proof progresses. Option 3: The cost function is automatically inferred by some clever algorithm... Restricted to specifjc classes of programs.

21/40

slide-51
SLIDE 51

Construction of the cost function

Option 1: The user somehow guesses a suitable cost function. Here, “λn. 3 log n ` 4” works. Option 2: Semi-automatically construct the cost function as the proof progresses. Option 3: The cost function is automatically inferred by some clever algorithm... Restricted to specifjc classes of programs.

21/40

slide-52
SLIDE 52

Construction of the cost function

Option 1: The user somehow guesses a suitable cost function. Here, “λn. 3 log n ` 4” works. Option 2: Semi-automatically construct the cost function as the proof progresses. Option 3: The cost function is automatically inferred by some clever algorithm... Restricted to specifjc classes of programs.

21/40

slide-53
SLIDE 53

Semi-automatic synthesis of cost functions

slide-54
SLIDE 54

Ourapproachto this problem

Part 1:

  • Synthesize a cost function with the same structure as the code
  • For recursive functions, recurrence equations are synthesized
  • Accounting details are automatically synthesized
  • User input is requested when some over-approximation is required

Part 2:

  • In a second step, prove a Opq bound for the inferred cost function

22/40

slide-55
SLIDE 55

Constraintinferredon the cost functionf

let rec bsearch a x i j = if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f n >= 1 + ( where n = j-i if n <= 0 then 0 else 0 + 1 + max 0 ( 1 + max (f (n/2)) (f (n - n/2 - 1)) ) ) 23/40

slide-56
SLIDE 56

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + …

a hole (“…”) is implemented as an evar in Coq

24/40

slide-57
SLIDE 57

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + (if j <= i then … else …) 24/40

slide-58
SLIDE 58

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + (if j <= i then … else …) 24/40

slide-59
SLIDE 59

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + (if (j-i) <= 0 then … else …) 24/40

slide-60
SLIDE 60

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + (if (j-i) <= 0 then 0 else …) 24/40

slide-61
SLIDE 61

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + … ) 24/40

slide-62
SLIDE 62

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + … ) 24/40

slide-63
SLIDE 63

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max … … ) 24/40

slide-64
SLIDE 64

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max 0 … ) 24/40

slide-65
SLIDE 65

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max 0 (1 + …) ) 24/40

slide-66
SLIDE 66

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max 0 (1 + max … …) ) 24/40

slide-67
SLIDE 67

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max 0 ( 1 + max (f ((j-i)/2)) … ) ) 24/40

slide-68
SLIDE 68

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f (j-i) >= 1 + ( if (j-i) <= 0 then 0 else 0 + 1 + max 0 ( 1 + max (f ((j-i)/2)) (f ((j-i) - (j-i)/2 - 1)) ) ) 24/40

slide-69
SLIDE 69

Interactive construction of the cost functionf

if j <= i then -1 else let k = i + (j - i) / 2 in if x = Array.get a k then k else if x < Array.get a k then bsearch a x i k else bsearch a x (k+1) j f n >= 1 + ( if n <= 0 then 0 else 0 + 1 + max 0 ( 1 + max (f (n/2)) (f (n - n/2 - 1)) ) ) 24/40

slide-70
SLIDE 70

Fromcost equation to asymptotic bound

For bsearch, there remains to fjnd a f P Opλn. log nq such that: @n. fpnq ě 1 ` # if n ď 0 1 ` maxp0, 1 ` maxpfpn

2 q, fpn ´ n 2 ´ 1qqq

  • Use the “Master Theorem”, when applicable

(available in Isabelle/HOL, not yet in Coq)

  • Substitution method: guess that there is a solution of the form

, inject it and resolve.

25/40

slide-71
SLIDE 71

Fromcost equation to asymptotic bound

For bsearch, there remains to fjnd a f P Opλn. log nq such that: @n. fpnq ě 1 ` # if n ď 0 1 ` maxp0, 1 ` maxpfpn

2 q, fpn ´ n 2 ´ 1qqq

  • Use the “Master Theorem”, when applicable

(available in Isabelle/HOL, not yet in Coq)

  • Substitution method: guess that there is a solution of the form

a log n ` b, inject it and resolve.

25/40

slide-72
SLIDE 72

The substitution method in action

Df : Z Ñ Z. @n. fpnq ě 1 ` # if n ď 0 1 ` maxp0, 1 ` maxpfpn

2 q, fpn ´ n 2 ´ 1qqq

^ f P Opλn. log nq Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-73
SLIDE 73

The substitution method in action

Df : Z Ñ Z. monotonic f ^ @n. fpnq ě 0 ^ @n. n ď 0 ù ñ fpnq ě 1 ^ @n. n ě 1 ù ñ fpnq ě fpn

2 q ` 3

^ f P Opλn. log nq Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-74
SLIDE 74

The substitution method in action

Da b : Z. fpnq “ a log n ` b ^ monotonic f ^ @n. fpnq ě 0 ^ @n. n ď 0 ù ñ fpnq ě 1 ^ @n. n ě 1 ù ñ fpnq ě fpn

2 q ` 3

^ f P Opλn. log nq Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-75
SLIDE 75

The substitution method in action

Da b : Z. fpnq “ a log n ` b (issue when n “ 0) ^ monotonic f ^ @n. fpnq ě 0 ^ @n. n ď 0 ù ñ fpnq ě 1 ^ @n. n ě 1 ù ñ fpnq ě fpn

2 q ` 3

^ f P Opλn. log nq Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-76
SLIDE 76

The substitution method in action

Da b c : Z. fpnq “ if n ą 0 then a log n ` b else c ^ monotonic f ^ @n. fpnq ě 0 ^ @n. n ď 0 ù ñ fpnq ě 1 ^ @n. n ě 1 ù ñ fpnq ě fpn

2 q ` 3

^ f P Opλn. log nq Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-77
SLIDE 77

The substitution method in action

Da b c : Z. fpnq “ if n ą 0 then a log n ` b else c ^ monotonic f ^ @n. fpnq ě 0 ^ @n. n ď 0 ù ñ fpnq ě 1 ^ @n. n ě 1 ù ñ fpnq ě fpn

2 q ` 3

^ True Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-78
SLIDE 78

The substitution method in action

Da b c : Z. fpnq “ if n ą 0 then a log n ` b else c ^ a ě 0 ^ b ě c ^ b ě 0 ^ c ě 0 ^ c ě 1 ^ b ě c ` 3 ^ a ě 3 ^ True Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-79
SLIDE 79

The substitution method in action

Da b c : Z. ^ a ě 0 ^ b ě c ^ b ě 0 ^ c ě 0 ^ c ě 1 ^ b ě c ` 3 ^ a ě 3 ^ True Can be solved automatically. The user does not have to manually provide values for , , and .

26/40

slide-80
SLIDE 80

The substitution method in action

Da b c : Z. ^ a ě 0 ^ b ě c ^ b ě 0 ^ c ě 0 ^ c ě 1 ^ b ě c ` 3 ^ a ě 3 ^ True Can be solved automatically. The user does not have to manually provide values for a, b, and c.

26/40

slide-81
SLIDE 81

SeparationLogic with Time Credits

slide-82
SLIDE 82

Linking code to cost assertions

Program specifjcations using Separation Logic precondition program postcondition

tPu t tQu

time credits

27/40

slide-83
SLIDE 83

Linking code to cost assertions

Program specifjcations using Separation Logic with Time Credits precondition program postcondition

t$n ‹ Pu t tQu

time credits

27/40

slide-84
SLIDE 84

Linking code to cost assertions

Program specifjcations using Separation Logic with Time Credits precondition program postcondition

t$n ‹ Pu t tQu

time credits

27/40

slide-85
SLIDE 85

Time Credits: resourcesin separationlogic

$n

  • $n describes the right to perform n function calls or loop iterations
  • $pn ` mq “ $n ‹ $m
  • $0 “ emp
  • Credits are not duplicable:
  • Enable amortized complexity analysis

28/40

slide-86
SLIDE 86

Time Credits: resourcesin separationlogic

$n

  • $n describes the right to perform n function calls or loop iterations
  • $pn ` mq “ $n ‹ $m
  • $0 “ emp
  • Credits are not duplicable: $1 ù

ñ { $1 ‹ $1

  • Enable amortized complexity analysis

28/40

slide-87
SLIDE 87

Using time creditsin the specifjcationof bsearch

Specifjcation of the complexity of bsearch using time credits: Df : Z Ñ Z. # f P Opλn. log nq @a x i j. t$pfpj ´ iqq ‹ . . .u pbsearch a x i jq t...u

29/40

slide-88
SLIDE 88

Contribution: PossiblyNegative Time Credits

Separation Logic with Time Credits in N: $0 ” emp @m n P N. $pm ` nq ” $m ‹ $n @n P N. $n , emp My extension: Possibly Negative Time Credits in Z: $0 ” emp @m n P Z. $pm ` nq ” $m ‹ $n @n P Z. $n ‹ rn ě 0s , emp Corollary: $n ” $m ‹ $pn ´ mq

30/40

slide-89
SLIDE 89

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *) index_of

(too coarse)

index_of

(restrictive?)

index_of

(too complicated)

index_of

31/40

slide-90
SLIDE 90

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse)

index_of

(restrictive?)

index_of

(too complicated)

index_of

31/40

slide-91
SLIDE 91

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse)

index_of

(restrictive?)

index_of

(too complicated)

index_of

31/40

slide-92
SLIDE 92

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?)

index_of

(too complicated)

index_of

31/40

slide-93
SLIDE 93

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?)

index_of

(too complicated)

index_of

31/40

slide-94
SLIDE 94

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?) @a. let k :“ min ti | a.piq “ vu in t$pk ` 1qu index_of v a tλi. ri “ ksu (too complicated)

index_of

31/40

slide-95
SLIDE 95

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?) @a. let k :“ min ti | a.piq “ vu in t$pk ` 1qu index_of v a tλi. ri “ ksu (too complicated)

index_of

31/40

slide-96
SLIDE 96

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?) @a. let k :“ min ti | a.piq “ vu in t$pk ` 1qu index_of v a tλi. ri “ ksu (too complicated) @a. tempu index_of v a tλi. $p´i ´ 1qu

31/40

slide-97
SLIDE 97

Possibly Negative Time Creditsenable simpler specifjcations

let index_of (v: ’a) (a: ’a array): int = (* returns the index of the first occurrence of v in a *)

@a. t$p|a| ` 1qu index_of v a tλi. empu (too coarse) @a. t$p|a| ` 1qu index_of v a tλi. $p|a| ´ iqu (restrictive?) @a. let k :“ min ti | a.piq “ vu in t$pk ` 1qu index_of v a tλi. ri “ ksu (too complicated) @a. tempu index_of v a tλi. $p´i ´ 1qu

31/40

slide-98
SLIDE 98

Time Creditsin Z: benefjts

  • Simpler specifjcations

(when the cost depends on the result)

  • Signifjcant reduction of the number of intermediate side-conditions

(can accumulate debts and pay them off once at the end)

  • Simpler loop invariants

(no need to justify that a number of credits is positive at each step)

32/40

slide-99
SLIDE 99

Case Study: anIncremental Cycle DetectionAlgorithm

slide-100
SLIDE 100

Ourmain case study

Verifjcation of a state-of-the-art incremental cycle detection algorithm due to Bender, Fineman, Gilbert and Tarjan (2016). The problem: checking for acyclicity of a dynamically constructed graph

33/40

slide-101
SLIDE 101

Ourmain case study

Verifjcation of a state-of-the-art incremental cycle detection algorithm due to Bender, Fineman, Gilbert and Tarjan (2016). The problem: checking for acyclicity of a dynamically constructed graph

33/40

slide-102
SLIDE 102

Ourmain case study

Verifjcation of a state-of-the-art incremental cycle detection algorithm due to Bender, Fineman, Gilbert and Tarjan (2016). The problem: checking for acyclicity of a dynamically constructed graph

33/40

slide-103
SLIDE 103

Ourmain case study

Verifjcation of a state-of-the-art incremental cycle detection algorithm due to Bender, Fineman, Gilbert and Tarjan (2016). The problem: checking for acyclicity of a dynamically constructed graph

33/40

slide-104
SLIDE 104

Ourmain case study

Verifjcation of a state-of-the-art incremental cycle detection algorithm due to Bender, Fineman, Gilbert and Tarjan (2016). The problem: checking for acyclicity of a dynamically constructed graph

33/40

slide-105
SLIDE 105

Minimal OCaml interface

type add_edge_result = | EdgeAdded | EdgeCreatesCycle val add_edge_or_detect_cycle : graph -> vertex -> vertex -> add_edge_result

34/40

slide-106
SLIDE 106

Ourmain case study (2)

A state-of-the-art algorithm:

  • non-trivial implementation (200 lines of compact OCaml code)
  • subtle complexity analysis
  • used in Coq (universe constraints) and Dune (build dependencies)

35/40

slide-107
SLIDE 107

Incremental Cycle Detection: Complexity

Naive algorithm: Opmq traversal at each arc insertion. Inserting m arcs costs Opm2q. Using Bender et al.’s algorithm, inserting m arcs costs: Opm ¨ minp?m, n2{3qq Or:• Opm?mq for sparse graphs;

  • Opmn2{3q for dense graphs.

Specifjes the cost ofa sequence ofoperations. No closed formula for the amortized cost of a single operation.

36/40

slide-108
SLIDE 108

Incremental Cycle Detection: Complexity

Naive algorithm: Opmq traversal at each arc insertion. Inserting m arcs costs Opm2q. Using Bender et al.’s algorithm, inserting m arcs costs: Opm ¨ minp?m, n2{3qq Or:• Opm?mq for sparse graphs;

  • Opmn2{3q for dense graphs.

Specifjes the cost ofa sequence ofoperations. No closed formula for the amortized cost of a single operation.

36/40

slide-109
SLIDE 109

Toplevel specifjcation (functionalcorrectnessonly)

“IsDAG g G”: a Separation Logic predicate describing the algorithm’s data structure, at address g, representing the graph G.

add_edge_or_detect_cycle 37/40

slide-110
SLIDE 110

Toplevel specifjcation (functionalcorrectnessonly)

“IsDAG g G”: a Separation Logic predicate describing the algorithm’s data structure, at address g, representing the graph G.

@g G v w. let m :“ |edges G| in let n :“ |vertices G| in v, w P vertices G ^ pv, wq R edges G ù ñ ! IsDAG g G ) padd_edge_or_detect_cycle g v wq $ ’ & ’ % λ res. match res with | EdgeAdded ñ IsDAG g pG ` pv, wqq | EdgeCreatesCycle ñ rw Ý Ñ˚

G vsq

, / . /

  • 37/40
slide-111
SLIDE 111

Toplevel specifjcation (correctnessandcomplexity)

“IsDAG g G”: a Separation Logic predicate describing the algorithm’s data structure, at address g, representing the graph G.

Dψ. ψ P Opm ¨ minp?m, n2{3q ` nq ^ @g G v w. let m :“ |edges G| in let n :“ |vertices G| in v, w P vertices G ^ pv, wq R edges G ù ñ ! IsDAG g G ‹ $pψ pm ` 1, nq ´ ψ pm, nqq ) padd_edge_or_detect_cycle g v wq $ ’ & ’ % λ res. match res with | EdgeAdded ñ IsDAG g pG ` pv, wqq | EdgeCreatesCycle ñ rw Ý Ñ˚

G vsq

, / . /

  • 37/40
slide-112
SLIDE 112

Case Study: Summary

Final result

  • A formally verifjed OCaml library for incremental cycle detection
  • Succinct specifjcation
  • Robust proof (no hardcoded constants or manual accounting)
  • Code has been integrated in Dune, fjxing some complexity bugs

Contributions

  • State-of-the-art result on verifjed graph algorithms
  • A crucial improvement to the algorithm tomakeittrulyincremental

38/40

slide-113
SLIDE 113

Conclusion

slide-114
SLIDE 114

Summary

In this talk:

  • Motivation for the verifjcation of complexity using O
  • Cost functions and their inference
  • Possibly Negative Time Credits
  • A large case study

More in the manuscript:

  • Specifjc challenges related to multivariate
  • Summation lemmas for the analysis of for-loops
  • More case studies

39/40

slide-115
SLIDE 115

Summary

In this talk:

  • Motivation for the verifjcation of complexity using O
  • Cost functions and their inference
  • Possibly Negative Time Credits
  • A large case study

More in the manuscript:

  • Specifjc challenges related to multivariate O
  • Summation lemmas for the analysis of for-loops
  • More case studies

39/40

slide-116
SLIDE 116

Perspectives

Further automation

  • in Coq: high-level reasoning on synthesized cost expressions

(master theorem, simplifjcation procedures)

  • integration with automated complexity analysis tools
  • integration of the approach in more automated verifjcation tools

Implement support to allow extracting concrete complexity bounds Even more challenging applications:

  • space complexity
  • concurrent programs
  • cache-oblivious algorithms

40/40

slide-117
SLIDE 117

Perspectives

Further automation

  • in Coq: high-level reasoning on synthesized cost expressions

(master theorem, simplifjcation procedures)

  • integration with automated complexity analysis tools
  • integration of the approach in more automated verifjcation tools

Implement support to allow extracting concrete complexity bounds Even more challenging applications:

  • space complexity
  • concurrent programs
  • cache-oblivious algorithms

40/40

slide-118
SLIDE 118

Perspectives

Further automation

  • in Coq: high-level reasoning on synthesized cost expressions

(master theorem, simplifjcation procedures)

  • integration with automated complexity analysis tools
  • integration of the approach in more automated verifjcation tools

Implement support to allow extracting concrete complexity bounds Even more challenging applications:

  • space complexity
  • concurrent programs
  • cache-oblivious algorithms

40/40