PDF Program Proofs 1st Edition K. Rustan M. Leino download
PDF Program Proofs 1st Edition K. Rustan M. Leino download
com
https://ebookmeta.com/product/program-proofs-1st-edition-k-
rustan-m-leino/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/introduction-to-dependent-types-with-
idris-encoding-program-proofs-in-types-1st-edition-boro-sitnikovski-2/
ebookmeta.com
https://ebookmeta.com/product/introduction-to-dependent-types-with-
idris-encoding-program-proofs-in-types-1st-edition-boro-sitnikovski/
ebookmeta.com
https://ebookmeta.com/product/greedy-girl-7-deadly-sins-1st-edition-m-
k-moore-moore-m-k/
ebookmeta.com
ebookmeta.com
Dirty Daddies Pride 2023 1st Edition Adaline Raine Aster
Rae Cara Dee Chara Croft Chloe Lynn Ellis Colette Davison
J M Dabney Sammi Cee M A Innes Maggie Ryan P D Carter
Reese Morrison Siobhan Smile T L Travis
https://ebookmeta.com/product/dirty-daddies-pride-2023-1st-edition-
adaline-raine-aster-rae-cara-dee-chara-croft-chloe-lynn-ellis-colette-
davison-j-m-dabney-sammi-cee-m-a-innes-maggie-ryan-p-d-carter-reese-
morrison-siobhan-smile-2/
ebookmeta.com
https://ebookmeta.com/product/organic-reactions-and-their-
mechanisms-2nd-edition-v-k-ahluwalia/
ebookmeta.com
https://ebookmeta.com/product/on-the-history-and-transmission-of-
lacanian-psychoanalysis-1st-edition-chris-vanderwees/
ebookmeta.com
https://ebookmeta.com/product/social-workers-as-game-changers-
confronting-complex-social-issues-through-cases-first-edition-lewis-
laura/
ebookmeta.com
https://ebookmeta.com/product/breath-ghost-mountain-wolf-
shifters-5-1st-edition-audrey-faye-faye-audrey/
ebookmeta.com
The Gnostic Scriptures 2nd Edition Bentley Layton David
Brakke
https://ebookmeta.com/product/the-gnostic-scriptures-2nd-edition-
bentley-layton-david-brakke/
ebookmeta.com
Program Proofs
Program Proofs
K. Rustan M. Leino
Illustrated by Kaleb 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 Gyre Pagella, Bera Mono, and Noto Emoji by the
author.
ISBN: 978-0-262-54623-2
10 9 8 7 6 5 4 3 2 1
d_r0
Contents
Preface
Notes for Teachers
0. Introduction
Reference Material
A. Dafny Syntax Cheat Sheet
B. Boolean Algebra
C. Answers to Select Exercises
References
Index
Preface
Welcome to Program Proofs!
I've designed this book to teach a practical understanding of what
it means to write specifications for code and what it means for code
to satisfy the specifications. In this preface, I want to tell you about
the book itself and how to use it.
Material
I have written this book to support the level of a second-year
university course in computer science. It can also be used as a
comprehensive introduction for industrial software 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 consequence) 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) functional programs and Part 2 on
imperative programs. Other than occasional references between
these parts, Parts 1 and 2 are independent of each other.
What the Book Is Not
Here are some things this book is not:
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 imperative language. This seems like a
reasonable assumption for a second-year university 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) program-reasoning techniques that are not
covered. There are also many useful programming paradigms
that are not covered. The mathematics or motivations behind
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.
Dafny
All specifications, programs, and program proofs in the book use the
Dafny programming 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 invariants, 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 calculations.
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
abstraction, 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 verifier 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, 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, Cesare Tinelli, and especially Graeme Smith, who used
earlier drafts of this book in teaching 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 Automated 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.
Notes for Teachers
Much thought goes into the selection and order of material in a book.
Here, I describe the purpose of and motivation for chapters in
greater detail. If you're a learner and just want to get started with the
book, skip ahead to Chapter 0. If you're a teacher and want to plan a
course outline, this is for you.
Part 0
The river here is a wide, handsome stream, and the walls extend
quite to the banks, and have two water-gates; the character is the
same as nearer its embouchure. I passed one of these water-gates
at sunset, and was much struck by the beauty of the landscape, with
the fishing canoes just returning towards Loggun: the stream sweeps
off to the south-south-west, and then to the south. Loggun was said
to be thirty miles distant by the river. Here my poor friend declared it
impossible to remain, and we moved on towards Loggun the next
morning. We could advance, however, but a few miles. Mr. Toole’s
sufferings were most acute; he twice fainted, and we lifted him on
and off his horse like an infant, so helpless had he become. What
added also to our distress was, that from this time until the evening
of the 16th, the Shouaa Arabs, who occupy the frontier of the
Loggun country, refused to allow us to pass until the sultan had been
consulted, and a number of his questions answered as to the
purpose of our visit. We were now close to the river, and
notwithstanding the heat, the only means we had of defending either
ourselves or our animals from the torture of the millions of insects
that beset us, was by lighting fires at the entrance of our tent, and
constantly supplying them with weeds and wet straw: the thick
suffocating smoke arising from this description of fire afforded us
temporary relief. I rode down to the river, which here flows with great
beauty and majesty past the high walls of this capital of Loggun; it
comes direct from the south-west, with a rapid current. We entered
the town by the western gate, which leads to the principal street: it is
as wide as Pall Mall, and has large dwellings on each side, built with
great uniformity, each having a court-yard in front, surrounded by
walls, and a handsome entrance, with a strong door hasped with
iron: a number of the inhabitants were seated at their doors for the
purpose of seeing us enter, with their slaves ranged behind them. At
first they took but little notice of us: indeed, our appearance could not
have been very imposing: one of our party was laid on a camel, and
another supported on his horse by two persons, who walked on each
side of him, while he raved most incoherently from the violence of
the fever by which he was consuming. At length, however, a person
of apparent consequence advanced towards my horse, bending
nearly double, and joining his hands (the first salutation of the kind
that I had seen), followed by his slaves stooping still lower than
himself. After explaining that he was deputed by the sultan to
welcome kab n’jaffy (the white man), and repeating frequently that
he was kaffama (my friend), he preceded our party; and, as we
moved on, each assembly that we passed rose from the ground,
advanced towards us, and saluted us in the same manner as I have
already described. We were at length conducted to our habitation,
which consisted of four separate huts, well built, within an outer wall,
with a large entrance hall for our servants: in the most retired and
quiet spot I spread the mat and pillow of my patient, who was in a
sad state of exhaustion and irritation.
The next morning I was sent for to appear before the sultan: ten
immense negroes, of high birth, most of them gray-bearded, bare-
headed, and carrying large clubs, preceded me through the streets,
and I was received with considerable ceremony. After passing
through several dark rooms, I was conducted to a large square court,
where some hundred persons were assembled, and all seated on
the ground: in the middle was a vacant space, to which they led me,
and I was desired to sit down also. Two slaves, in striped cotton
tobes, who were fanning the air through a lattice-work of cane,
pointed out the retirement of the sultan. On a signal, this shade was
removed, and something alive was discovered on a carpet, wrapped
up in silk tobes, with the head enveloped in shawls, and nothing but
the eyes visible: the whole court prostrated themselves, and poured
sand on their heads, while eight frumfrums and as many horns blew
a loud and very harsh-sounding salute.
My present, a red bornouse, a striped cotton caftan, a turban, two
knives, two pairs of scissors, and a pair of red trowsers, was laid
before him: he again whispered a welcome, for it is considered so
extremely ill-bred in a Loggun gentleman to speak out, that it is with
difficulty you can catch the sound of their voices.
He examined me very minutely, when the shade was again
drawn. I begged for permission to embark on the Shary, and was told
he would consider of it. He particularly inquired if I wished to
purchase b’lowy, or handsome female slaves, which I assured him I
did not; “because,” said he, “if you do, go no farther: I have some
hundreds, and will sell them to you as cheap as any one.”
Loggun, the capital of which country (Kernuk) is on the banks of
the Shary, and in 11° 7′ north latitude, is a very populous country.
Kernuk has fifteen thousand inhabitants at least. They speak a
language nearly Begharmi. The Shouaas are all round them, and to
them they are indebted for the plentiful supply of bullocks, milk, and
fat, with which the market abounds: these necessaries are paid for
by tobes, and blue cotton in stripes, which the Loggun people make
and dye of a very beautiful colour. They have, also, a metal currency
in Loggun, the first I had seen in Negroland: it consists of thin plates
of iron, something in the shape of the tip with which they shoe race-