Algebraic Principles for Program Correctness Tools in Isabelle/HOL
Total Page:16
File Type:pdf, Size:1020Kb
Algebraic Principles for Program Correctness Tools in Isabelle/HOL Victor B. F. Gomes Supervisor: Georg Struth Department of Computer Science University of Sheffield A thesis submitted for the degree of Doctor of Philosophy February 2016 i Acknowledgements I gratefully acknowledge the funding received from the CNPq through the programme Science without Border (CsF) to undertake my PhD. Foremost, I would like to express my sincere gratitude to my supervi- sor Prof. Georg Struth for all the support and guidance during these three exciting years. Without his assistance and constant feedback, this thesis would not have been possible. In addition, I would like to thank the rest of my research panel: Prof. Marian Georghe and Dr. Joab Winkler for their insigthful comments and questions. I would also like to thank my collegue Alasdair Armstrong for his constant assistance while I was learning Isabelle. As well as to all the attendees of the verification reading group for all the in-depth and memorable discussions: Dr. Brijesh Dongol, Dr. James Cranch, Dr. Kirill Bogdanov and Dr. Michael Laurence. I wish to thank Prof. Jos´eNuno Oliveira, Prof. Peter Jipsen, Dr. Wal- ter Guttmann and Dr. Damien Pous for interesting and insightful conversations during RAMiCS'14. I also thank Prof. Jeremy Gib- bons, Dr. Stephan Van Staden, Prof. Roland Backhouse, Prof. Carrol Morgan and Prof. Lindsay Groves for their discussions, suggestions and guidance during MPC'15. Thanks to all my collegues in the department: Jorge, Jos´e,Raluca, Fernando and Umberto. Special thanks to the Brazilian gang: Carol, Tiago, L´ucia,T´eo,Yuri, Hegler and Gustavo. I am especially grateful for my Sheffield friends and flatmates, who I was lucky to spend time with during my PhD: Annemie, Fred, Natalie, Galyia, Jamie, Girija and Theo (thanks for the proofreading). Agrade¸coem especial minha fam´ıliapelo apoio incondicional, for¸cae incentivo. Minha amada m~ae,meu pai companheiro e meus queridos irm~aose irm~as:Camilla, Larissa, Andr´e,Samuel e Luana. Abstract This thesis puts forward a flexible and principled approach to the development of construction and verification tools for imperative pro- grams, in which the control flow and the data level are cleanly sep- arated. The approach is inspired by algebraic principles and bene- fits from an algebraic semantics layer. It is programmed in the Is- abelle/HOL interactive theorem prover and yields simple lightweight mathematical components as well as program construction and veri- fication tools that are themselves correct by construction. First, a simple tool is implemented using Kleeene algebra with tests (KAT) for the control flow of while-programs, which is the most com- pact verification formalism for imperative programs, and their stan- dard relational semantics for the data level. A reference formalisation of KAT in Isabelle/HOL is then presented, providing three different formalisations of tests. The structured comprehensive libraries for these algebras include an algebraic account of Hoare logic for par- tial correctness. Verification condition generation and program con- struction rules are based on equational reasoning and supported by powerful Isabelle tactics and automated theorem proving. Second, the tool is expanded to support different programming fea- tures and verification methods. A basic program construction tool is developed by adding an operation for the specification statement and one single axiom. To include recursive procedures, KATs are expanded further to quantales with tests, where iteration and the specification statement can be defined explicitly. Additionally, a nondeterministic extension supports the verification of simple concurrent programs. Finally, the approach is also applied to separation logic, where the control-flow is modelled by power series with convolution as sepa- rating conjunction. A generic construction lifts resource monoids to assertion and predicate transformer quantales. The data level is cap- tured by concrete store-heap models. These are linked to the algebra by soundness proofs. A number of examples shows the tools at work. I hold the opinion that the construction of computer programs is a mathematical activity like the solution of differential equations, that programs can be derived from their specifications through mathematical insight, calculation, and proof, using algebraic laws as simple and elegant as those of elementary arithmetic. C. A. R. Hoare iii Contents Contents iv List of Figures vii 1 Introduction1 1.1 Contributions . .3 1.2 Outline . .7 1.3 Publications . .9 2 Technical Background 10 2.1 The while Programming Language . 11 2.2 Hoare Logic . 14 2.3 Weakest Preconditions . 15 2.4 Separation Logic . 17 2.5 Refinement Calculus . 17 I Construction and Verification of while Programs 19 3 Algebras for Program Correctness 20 3.1 Kleene Algebras . 20 3.2 Kleene Algebras with Tests . 23 3.3 Isabelle/HOL Formalisation . 25 3.4 Propositional Hoare Logic . 30 3.5 Demonic Refinement Algebras . 32 3.6 Loop Transformation Example . 36 3.7 Conclusions . 40 4 A Basic Verification Tool 41 4.1 Principles of Tool Design . 41 4.2 Data Flow Integration . 43 4.3 Euclid's Algorithms . 47 iv CONTENTS 4.4 Arrays and for Loops . 49 4.5 Procedures, Local Variables and Function Calls . 51 4.6 Conclusions . 55 5 A Refinement Tool 56 5.1 The Specification Statement . 56 5.2 Data Flow Integration . 58 5.3 Sum of Even Fibonacci Numbers . 60 5.4 Insertion Sort . 65 5.5 Conclusions . 67 6 Recursive Programs 70 6.1 Verification of Recursive Programs . 70 6.2 Binary Search . 74 6.3 Quantales and Program Transformations . 77 6.4 Conclusions . 79 7 Predicate Transformers Semantics 80 7.1 Alternative Semantics . 80 7.2 Backward Predicate Transformers . 81 7.3 Forward Predicate Transformers . 82 7.4 Relating Semantics . 83 7.5 Modal Kleene Algebras . 85 7.6 Domain Quantale . 88 7.7 Implementation in Isabelle/HOL . 91 7.8 Conclusions . 93 8 Nondeterministic Programs 94 8.1 Guarded Command Language (GCL)................ 94 8.2 Propositional Hoare Logic for GCL ................. 96 8.3 GCL Examples . 98 8.4 Parallelism by Nondeterminism . 99 8.5 Isabelle Implementation . 102 8.6 Examples . 103 8.7 Conclusions . 104 II Verification of while programs with Pointers 108 9 Separation Logic 109 9.1 Design Approach . 109 9.2 Partial Algebras . 111 v CONTENTS 9.3 Power Series and Convolution . 112 9.4 Assertion Quantale . 114 9.5 Predicate Transformer Quantales . 118 9.6 Verification Conditions . 119 9.7 Refinement Laws . 120 9.8 Relational Fault Model . 121 9.9 Modal Separation Algebras . 124 9.10 Conclusions . 127 10 Separation Logic Assertions 128 10.1 Assertions over Heaplets . 128 10.2 Tactics for Separation Logic . 131 10.3 Conclusions . 134 11 Programs with Pointers 135 11.1 Program State Integration . 135 11.2 Linked Lists . 138 11.3 Examples . 139 11.4 Constructing a Linked List Reversal Algorithm. 140 11.5 Conclusions . 143 12 Conclusion 144 12.1 Summary . 145 12.2 Future Work . 145 A Algebraic Structures 147 B Introduction to Isabelle/HOL 151 B.1 Proving in Isabelle . 151 B.2 Isabelle Axiomatic Type Class . 153 B.3 Isabelle Main Library . 154 B.4 Eisbach . 155 C Source Code 156 C.1 Kleene Algebras . 157 C.2 Kozen's Loop Transformation Theorem . 159 C.3 Refinement Kleene Algebra . 163 C.4 Correctness Tools . 165 References 177 vi List of Figures 1.1 Algebraic approach for programm correctness tools. .7 2.1 Big-step operational semantics of the while language . 12 2.2 Relational semantics of the while language . 13 2.3 Rules of Hoare logic . 15 2.4 The weakest liberal precondition operator for a while language . 16 2.5 Morgan's refinement laws . 18 3.1 Algebraic semantics for a while language. 24 3.2 Example of proof in Isar, co-simulation law for Kleene star. 27 3.3 Back's refinement atomicity theorem. 35 4.1 Principles of tool design . 42 4.2 The hoare tactic. 47 4.3 Euclid's algorithm. 48 4.4 Integer division algorithm. 48 4.5 Power algorithm . 50 4.6 Linear search algorithm . 52 4.7 Maximum function of two natural numbers . 54 5.1 Construction of the sum of even Fibonacci numbers program . 62 5.2 Sum of even Fibonacci numbers . 63 5.3 Verification of the sum of even Fibonacci numbers program . 64 5.4 Construction of insertion sort algorithm (excerpts) . 66 5.5 Insertion sort algorithm . 67 5.6 Verification of insertion sort algorithm (proof steps) . 68 6.1 Verification of binary search (annotated algorithm) . 76 7.1 Euclid's algorithm by weakest liberal precondition . 92 8.1 Verification of Euclid's greatest common divisor algorithm in GCL 99 8.2 Verification of maximum algorithm in GCL ............. 99 vii LIST OF FIGURES 8.3 The initialisation protocol example . 105 8.4 Mutual inclusion problem example . 106 10.1 Tactics for separation logic . 132 11.1 Deallocation of the first element in a list segment . 140 11.2 Linked list deallocation algorithm . 141 11.3 In situ linked list reversal algorithm by refinement . 142 11.4 In situ linked list reversal algorithm . 143 A.1 Algebraic structures . 149 B.1 Example of proof in Isar, co-simulation law for Kleene star. ..