Making Classes Provable Through Contracts, Models and Frames
Total Page:16
File Type:pdf, Size:1020Kb
View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by CiteSeerX DISS. ETH NO. 17610 Making classes provable through contracts, models and frames A dissertation submitted to the SWISS FEDERAL INSTITUTE OF TECHNOLOGY ZURICH (ETH Zurich)¨ for the degree of Doctor of Sciences presented by Bernd Schoeller Diplom-Informatiker, TU Berlin born April 5th, 1974 citizen of Federal Republic of Germany accepted on the recommendation of Prof. Dr. Bertrand Meyer, examiner Prof. Dr. Martin Odersky, co-examiner Prof. Dr. Jonathan S. Ostroff, co-examiner 2007 ABSTRACT Software correctness is a relation between code and a specification of the expected behavior of the software component. Without proper specifica- tions, correct software cannot be defined. The Design by Contract methodology is a way to tightly integrate spec- ifications into software development. It has proved to be a light-weight and at the same time powerful description technique that is accepted by software developers. In its more than 20 years of existence, it has demon- strated many uses: documentation, understanding object-oriented inheri- tance, runtime assertion checking, or fully automated testing. This thesis approaches the formal verification of contracted code. It conducts an analysis of Eiffel and how contracts are expressed in the lan- guage as it is now. It formalizes the programming language providing an operational semantics and a formal list of correctness conditions in terms of this operational semantics. It introduces the concept of axiomatic classes and provides a full library of axiomatic classes, called the mathematical model library to overcome prob- lems of contracts on unbounded data structures. This thesis argues that modular verification is essential for the reuse of trusted object-oriented code. Modular verification introduces problems with hidden interference of components, known as the frame problem. This thesis introduces the concept of dynamic frame contracts and shows how such contracts can overcome the frame problem, at the same time retaining full information hiding and being faithful to the inheritance relation. The thesis includes an experimental implementation of a fully auto- mated verifier called Ballet. This verifier transforms Eiffel into proof obli- gations that are handed over to a fully automated theorem prover. i ii ZUSAMMENFASSUNG Korrekte Software beschreibt eine Relation zwischen Programmtext und ein- er Spezifikation des zu erwartenden Verhaltens einer Software-Kompo- nente. Ohne eine geeignete Spezifikation ist der Begriff korrekter Software bedeutungslos. Design by Contract ist eine Methode die Spezifikationen in den Softwa- reentwicklungsprozess einbindet. Sie hat sich bewahrt¨ als eine einfache und gleichermaßen machtige¨ Technologie und wird von Entwicklern an- genommen. In den 20 Jahren seit ihrer Entstehung haben sich viele An- wendungsfelder fur¨ Design by Contract ergeben: Dokumentation, Ver- standnis¨ der objektorientierten Vererbung, Uberpr¨ ufung¨ zur Laufzeit, oder vollautomatisches Testen. Diese Dissertation beschaftigt¨ sich mit der formalen Verifikation von contracted Code. Sie analysiert die Programmiersprache Eiffel und wie in dieser Programmiersprache Vertrage¨ (Contracts) benutzt werden konnen.¨ Eine operationelle Semantik formalisiert die Programmiersprache. Die Be- weisverpflichtungen zur Uberpr¨ ufung¨ der Korrektheit werde definiert. Sie entwickelt den Begriff der axiomatischen Klasse und entwickelt eine Bibliothek solcher Klassen, die Mathematical Model Library, um Probleme mit Vertragen¨ uber¨ unbeschrankten¨ Datenstrukturen zu beheben. Diese Dissertation argumentiert, dass modulares Beweisen essentiell fur¨ die Wiederverwendungen von vertrauenswurdigen,¨ objektorientierten Komponenten ist. Eine modulare Beweisfuhrung¨ scheitert an versteckten Interferenzen verschiedener Komponenten. Dieses Problem ist unter dem Namen Frame Problem bekannt. Diese Dissertation erweitert Design by Contracts um Dynamic Frame Contracts, um diese Probleme zu beheben. Dynamic Frame Contracts bewahren die Kapselung von Komponenten und sind vertraglich¨ mit der Vererbungsbeziehung der objektorientierten Entwicklung. iii Die Dissertation enthalt¨ die experimentelle Implementierung eines voll- automatischen Beweiswerkzeugs mit dem Namen Ballet. Dieses Werkzeug wandelt Eiffel Programmtext in Beweisverpflichtungen fur¨ einen automa- tischen Beweiser um. iv ACKNOWLEDGEMENTS First and foremost, I would like to thank my supervisor Prof. Bertrand Meyer. Working as part of his research group and his constant personal input and feedback formed the basis for my understanding of software engineering, language semantics and verification of object-oriented pro- grams. I would like to thank Prof. Jonathan Ostroff. The possibility to stay with his research group in Toronto had a great influence on my understanding of models and frames. I also want to include the other members of his research group, Faraz Torshizi, Hai Feng Huang, David Makalsky and Chen- Wei Wang. My gratitudes goes many people at the “Chair of Software Engineer- ing” at ETH Zurich.¨ This includes Prof. Peter M¨uller, who was always available for questions on Spec# and JML, and the other members: Manuel Oriol, Till Bay, Ilinca Ciupa, Adam Darvas, Werner Dietl, Hermann Lehner, An- dreas Leitner, Yann M¨uller, Martin Nordio, Michela Pedroni, Marco Piccioni, Joseph Ruskiewicz and Jason Wei, and the former members Vijay d’Silva, Ar- naud Bailly, Karine Arnout, Susanne Cech Previtali and Piotr Nienaltowski. Each one of them, through smaller or more extensive discussions, con- tributed to the overall result of this thesis. If would like to thank Prof. Martin Odersky, for volunteering on short notice to become second co-examiner, for giving me the opportunity to present my work at his research group at EPFL and for the feedback given. Finally, I would like to thank all members of my family (the number in- creased significantly during my studies) and my girlfriend, Jana Steinbr¨uck, for providing all the needed personal support required to endure the ups and downs of my scientific work. v vi CONTENTS 1 Introduction 1 1.1 Towards trusted components . 1 1.2 The grand challenge of the verifying compiler . 2 1.3 Summary and outlook . 3 2 Essential Results 5 2.1 Overview . 5 2.2 Language semantics . 7 2.3 Models and model contracts . 9 2.4 A library of models . 13 2.5 Composability and the frame problem . 18 2.6 The frame problem in Eiffel . 19 2.7 Dynamic frame contracts . 21 2.8 Ballet verifier . 23 3 Specifications in Eiffel 27 3.1 Type safety . 28 3.2 Contracts . 29 3.3 Side-effects of contracts . 31 3.4 Partial functions in contracts . 32 3.5 Pure object-orientation . 32 3.6 Axiomatic classes . 33 3.7 Language semantics . 34 3.8 Contract-based reasoning . 35 3.9 Recursive contracts . 37 3.10 Agent-based contracts . 39 3.11 Specification features . 39 4 Modular reasoning and verification 41 4.1 Modular boundary . 41 vii 4.2 Modularity in inheritance . 42 4.3 Modularity of suppliers . 43 4.4 The imperative of modular verification . 43 4.4.1 Non-modular verification is not easier . 44 4.4.2 Cluster development model . 44 4.4.3 Software reuse . 44 4.4.4 Modular continuity . 45 4.4.5 Complete functional specifications . 45 4.4.6 Subtyping and polymorphism . 45 4.5 Summary . 46 5 Language Semantics 47 5.1 Related Work . 48 5.2 Mathematical notation . 49 5.3 Static model . 50 5.3.1 Classes and Types . 50 5.3.2 Features . 51 5.3.3 Program text . 52 5.3.4 Language simpliciations . 54 5.3.5 Contracts . 56 5.3.6 Implementation . 57 5.4 State model . 57 5.4.1 Heap . 58 5.4.2 Environment . 59 5.4.3 Global State . 60 5.4.4 Monomorphic state relation . 61 5.5 Execution model . 62 5.5.1 Evaluating expressions . 63 5.5.2 Side effects in expressions . 64 5.5.3 Executing instructions . 66 5.6 Monotonic properties of the state . 67 5.6.1 Object allocation . 67 5.6.2 Once setting . 68 5.7 Weak purity . 68 5.8 Correctness . 70 6 Model-based contracts 73 6.1 Related work . 74 6.2 Mathematical language . 75 6.3 Finite and typed models . 75 6.4 MML: a mathematical model library . 76 viii 6.5 Library design . 77 6.6 Comparison of model libraries . 79 6.7 Applying models . 80 6.8 Models and inheritance . 82 6.9 Composite models . 83 6.10 Classic and model contracts . 85 6.11 Default implementation . 87 6.12 Implementing the abstraction function . 88 6.13 Applying models to EiffelBase . 89 6.13.1 Analyzing existing EiffeBase . 90 6.13.2 Designing a new EiffeBase . 93 6.14 Summary . 98 7 Dynamic Frame Contracts 103 7.1 Closed world reasoning . 104 7.2 Frame problem in software development . 105 7.3 Related work . 107 7.4 Frames in the object-oriented programs . 108 7.4.1 Read vs write effects . 109 7.5 Dynamic Frames . 110 7.5.1 Object-orientation . 111 7.5.2 Frame variables . 112 7.5.3 Modular reasoning . 114 7.5.4 Object creation . 115 7.5.5 Summary . 115 7.6 Frames as contracts . 116 7.7 Extending dynamic frames . 119 7.8 Granularity of Frames . 120 7.9 Confining change . 120 7.10 Formalization . 121 7.10.1 Use sets . 121 7.10.2 Modify sets . 124 7.10.3 Correctness . 125 7.11 Reasoning with frames . 126 7.11.1 Soundness . 126 7.12 Frames and Inheritance . 142 7.13 Applying frames . 142 7.13.1 Contracts for frames . 143 7.14 Patterns of frames . 144 7.14.1 Encapsulation . 144 7.14.2 Friend . 145 ix 7.14.3 View . 146 7.14.4 Proxy . 146 7.14.5 Wrapper . 147 7.15.