Program Proofs 1st Edition K. Rustan M. Leino
Program Proofs 1st Edition K. Rustan M. Leino
com
https://ebookname.com/product/program-proofs-1st-edition-k-
rustan-m-leino/
OR CLICK HERE
DOWLOAD NOW
https://ebookname.com/product/program-evaluation-3rd-edition-john-m-
owen/
ebookname.com
https://ebookname.com/product/maasailand-ecology-k-m-homewood/
ebookname.com
https://ebookname.com/product/philosophical-devices-proofs-
probabilities-possibilities-and-sets-papineau/
ebookname.com
https://ebookname.com/product/asia-america-and-the-transformation-of-
geopolitics-1st-edition-william-h-overholt/
ebookname.com
https://ebookname.com/product/judith-butler-and-political-theory-
troubling-politics-1st-edition-samuel-chambers/
ebookname.com
https://ebookname.com/product/the-architecture-reference-
specification-book-2nd-edition-julia-mcmorrough/
ebookname.com
https://ebookname.com/product/the-ardennes-offensive-us-iii-xii-corps-
southern-sector-bruce-quarrie/
ebookname.com
https://ebookname.com/product/forensic-pathology-reviews-5-1st-
edition-michael-tsokos/
ebookname.com
Coaching Mentoring For Dummies Second Edition Macleod
https://ebookname.com/product/coaching-mentoring-for-dummies-second-
edition-macleod/
ebookname.com
Program Proofs
Program Proofs
K. Rustan M. Leino
All rights reserved. No part of this book may be reproduced in any form by any
electronic or mechanical means (including photocopying, recording, or information
storage and retrieval) without permission in writing from the publisher.
The MIT Press would like to thank the anonymous peer reviewers who provided
comments on drafts of this book. The generous work of academic experts is essential
for establishing the authority and quality of our publications. We acknowledge with
gratitude the contributions of these otherwise uncredited readers.
This book was set in TEX Gyre Pagella, Bera Mono, and Noto Emoji by the author.
Printed and bound in the United States of America.
Illustrated by Kaleb Leino.
ISBN: 978-0-262-54623-2
10 9 8 7 6 5 4 3 2 1
Contents
Preface ix
Notes for Teachers xv
0. Introduction 1
0.0. Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
0.1. Outline of Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.2. Dafny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
0.3. Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Material
I have written this book to support the level of a second-year university course in com-
puter science. It can also be used as a comprehensive introduction for industrial soft-
ware engineers who are new to specification and verification and want to apply such
techniques in their work.
The book assumes basic knowledge of programs and programming. The style of
this prior programming (functional, imperative) and the particular prior language
used are not so important, but it is helpful if the prior programming has not completely
ignored the concept of types.
The book also assumes some basics of logic. The “and”, “or”, and “not” operators
from programming will go a long way, but some fluency with implication (logical con-
sequence) is also important. For example, a reader is expected to feel comfortable with
the meaning of a formula like
2 <= x ==> 10 <= 4 * (x + 1)
The book’s Appendix B reviews some useful logic rules, but is hardly suitable as a first
introduction to logic. For that, I would recommend a semester course in logic.
Beyond the basics of logic, concepts like mathematical induction and well-founded
orderings play a role in program proofs. The book explains these concepts as needed.
The book is divided into three parts. Part 0 covers some foundations, leading up
to writing proofs. After that, Part 1 focuses on (specifications and proofs of) func-
tional programs and Part 2 on imperative programs. Other than occasional references
between these parts, Parts 1 and 2 are independent of each other.
xi
• It is not a beginner’s guide to programming. The book assumes the reader has
written (and compiled and run) basic programs in either a functional or impera-
tive language. This seems like a reasonable assumption for a second-year univer-
sity course in computer science.
• It is not a beginner’s guide to logic, but see Appendix B for a review of some
useful logic rules and some exercises.
• It is not a Dafny language guide or reference manual. The focus is on teaching
program proofs. The book explains the Dafny constructs in the way they are used
to support this learning, and Appendix A provides a cheat sheet for the language.
• It is not a research survey. There are many (mature or under-development) pro-
gram-reasoning techniques that are not covered. There are also many useful pro-
gramming paradigms that are not covered. The mathematics or motivations be-
hind those advanced techniques are outside the scope of this book. Instead, this
book focuses on teaching basic concepts and includes best practices for doing so.
• The book does not teach how to build a program verifier. Indeed, throughout
this book, I treat the verifier as a black box. A recurring theme is the process of
building proofs manually, which is good practice for interacting with any verifier.
Sections 13.7, 15.0, and 16.1 depend on Chapter 4, but the rest of their enclosing chapters
do not. The dotted lines show recommended dependencies—it would be beneficial, but
not absolutely required, to study Chapter 7 before Chapters 8 and 12, and likewise to
study Chapter 6 before Chapter 12.
Dafny
All specifications, programs, and program proofs in the book use the Dafny program-
ming language and can be checked in the Dafny verification system. Broadly speaking,
the constructs of the Dafny language support four kinds of activities.
• There are constructs for imperative programming, such as assignment statements,
loops, arrays, and dynamically allocated objects. The simpler of these are the
bread and butter of many classic treatments of program proofs.
• There are constructs for functional programming, such as recursive functions and
algebraic datatypes. In Dafny, these behave like in mathematics; for example,
functions are deterministic and cannot change the program state.
• There are constructs for writing specifications, such as preconditions, loop invari-
ants, and termination metrics. The way these are integrated into the language
has been influenced by the pioneering Eiffel language and the Java Modeling
Language (JML). Specifications can use any of the functional-language features,
which makes them quite expressive.
• Lastly, there are constructs for proof authoring, such as lemmas and proof calcu-
lations.
These various features blend together. For example, all the constructs use the same
expression language; these expressions include chaining expressions (like 0 <= x < y
< 100), implication (==>), quantifications (forall, exists), and sets (like {2, 3, 5}),
which are often found in specifications and math, but can also be used in programs;
methods, functions, and proofs bind values to local variables in the same way; in a
method, an if statement divides up control flow, and in a lemma, it divides up proof
obligations; variables can be marked as ghost, which makes them suitable for abstrac-
tion, but otherwise behave as ordinary compiled variables; and induction is achieved
simply by calling a lemma recursively, where termination is specified and checked in
the same way as for methods and functions.
Not only is the Dafny language versatile, but so are its uses. The Dafny development
tools are quick to install and are available on Windows, MacOS, and Linux. The ver-
ifier runs automatically in the VS Code integrated development environment. Dafny
programs compile to executable code for several language platforms, including .NET,
Java, JavaScript, and Go. The toolset itself is available as open source at
github.com/dafny-lang/dafny
Even before this book, Dafny has been used in teaching for over a decade. It has also
been used in several impressive research projects (for example, at Microsoft Research,
xiii
VMware Research, ConsenSys R&D, CMU, U. Michigan, and MIT) and is currently in
industrial use (for example, at Amazon Web Services).
Online Information
Some additional information about this book is available online at
www.program-proofs.com
Acknowledgments
I have many to thank for helping make this book possible.
I extend my deep gratitude to Rajeev Joshi, Rosemary Monahan, Bryan Parno, Ce-
sare Tinelli, and especially Graeme Smith, who used earlier drafts of this book in teach-
ing their university courses. The book has greatly benefited from their feedback, and
from feedback of their students.
The detailed comments from Rajeev Joshi, Yannick Moy, Jean-Christophe Filliâtre,
Peter Müller, and Ran Ettinger were much beyond the call of duty and were really
helpful! I’ve also received good feedback from Nada Amin, Nathan Chong, David
Cok, Josh Cowper, Mikaël Mayer, Gaurav Parthasarathy, and Robin Salkeld.
I’m grateful for the encouragement of Byron Cook and Reto Kramer in the Auto-
mated Reasoning Group where I work at Amazon Web Services.
The term “program proofs” as a rubric for the kind of science and engineering that
this book is about was suggested by Nik Swamy.
To write and typeset this book, I used the Madoko system, and I thank Daan Leijen
for creating Madoko and for helping me with customizations.
A big shout-out to Kaleb, who drew the cheerful chapter illustrations.
Lastly, thank you, Gwen, for your loving support and the countless weekends we
spent at coffee shops while I was writing.
Thank you all!
K.R.M.L.
Exploring the Variety of Random
Documents with Different Content
1.E.5. Do not copy, display, perform, distribute or redistribute
this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com