100% found this document useful (2 votes)
53 views

Program Construction Calculating Implementations from Specifications 1st Edition Roland Backhouse - The ebook is ready for instant download and access

The document promotes various ebooks available for download at ebookname.com, including titles on programming, construction, and psychology. It highlights the first edition of 'Program Construction: Calculating Implementations from Specifications' by Roland Backhouse, along with other educational resources. The document also includes copyright information and a detailed table of contents for the featured book.

Uploaded by

atbasmunni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
53 views

Program Construction Calculating Implementations from Specifications 1st Edition Roland Backhouse - The ebook is ready for instant download and access

The document promotes various ebooks available for download at ebookname.com, including titles on programming, construction, and psychology. It highlights the first edition of 'Program Construction: Calculating Implementations from Specifications' by Roland Backhouse, along with other educational resources. The document also includes copyright information and a detailed table of contents for the featured book.

Uploaded by

atbasmunni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Endless Ebook, One Click Away – Start Downloading at ebookname.

com

Program Construction Calculating Implementations


from Specifications 1st Edition Roland Backhouse

https://ebookname.com/product/program-construction-
calculating-implementations-from-specifications-1st-edition-
roland-backhouse/

OR CLICK HERE

DOWLOAD EBOOK

Browse and Get More Ebook Downloads Instantly at https://ebookname.com


Click here to visit ebookname.com and download ebook now
Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Guide Specifications for Highway Construction 9th


Edition Aashto

https://ebookname.com/product/guide-specifications-for-highway-
construction-9th-edition-aashto/

The Kiln Book Materials Specifications Construction 3°


Edition Frederick L. Olsen

https://ebookname.com/product/the-kiln-book-materials-
specifications-construction-3-edition-frederick-l-olsen/

Calculating promises 1st Edition Roy Kreitner

https://ebookname.com/product/calculating-promises-1st-edition-
roy-kreitner/

Titans Fox vs Pitt Dick Leonard

https://ebookname.com/product/titans-fox-vs-pitt-dick-leonard/
Biochemistry and Genetics 2007 p 514 3rd Edition Golder
Wilson

https://ebookname.com/product/biochemistry-and-
genetics-2007-p-514-3rd-edition-golder-wilson/

MIMO OFDM for LTE WiFi and WiMAX Coherent versus Non
coherent and Cooperative Turbo Transceivers Wiley IEEE
1st Edition Lajos L Hanzo

https://ebookname.com/product/mimo-ofdm-for-lte-wifi-and-wimax-
coherent-versus-non-coherent-and-cooperative-turbo-transceivers-
wiley-ieee-1st-edition-lajos-l-hanzo/

Work Psychology Understanding Human Behaviour in the


Workplace 4th Edition Joanne Silvester

https://ebookname.com/product/work-psychology-understanding-
human-behaviour-in-the-workplace-4th-edition-joanne-silvester/

Mountains 1st Edition Peter Aleshire

https://ebookname.com/product/mountains-1st-edition-peter-
aleshire/

Handbook of Stress Trauma and the Family Routledge


Psychosocial Stress Series 1st Edition Don. R.
Catherall

https://ebookname.com/product/handbook-of-stress-trauma-and-the-
family-routledge-psychosocial-stress-series-1st-edition-don-r-
catherall/
Reverberation Chambers Theory and Applications to EMC
and Antenna Measurements 1st Edition Stephen J. Boyes

https://ebookname.com/product/reverberation-chambers-theory-and-
applications-to-emc-and-antenna-measurements-1st-edition-stephen-
j-boyes/
Program Construction
This page intentionally left blank
Calculating Implementations
from Specifications

Roland Backhouse
The University of Nottingham, UK

WILEY
Copyright © 2003 John Wiley & Sons, Ltd
The Atrium, Southern Gate, Chichester,
West Sussex PO19 8SQ, England
National 01243 779777
International (+44) 1243 779777
Email (for orders and customer service enquiries): cs-books@wiley.co.uk
Visit our Home Page on http://www.wileyeurope.com or http://www.wiley.com
All Rights Reserved. No part of this publication may be reproduced, stored in a retrieval
system or transmitted in any form or by any means, electronic, mechanical, photocopying,
recording, scanning or otherwise, except under the terms of the Copyright, Designs and
Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency
Ltd, 90 Tottenham Court Road, London WIT 4LP, UK, without the permission in writing of
the Publisher, with the exception of any material supplied specifically for the purpose of
being entered and executed on a computer system, for exclusive use by the purchaser of the
publication. Requests to the Publisher should be addressed to the Permissions Department,
John Wiley & Sons, Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ,
England, or emailed to permreq@wiley.co.uk, or faxed to (+44) 1243 770620.
Neither the authors nor John Wiley & Sons, Ltd accept any responsibility or liability for
loss or damage occasioned to any person or property through using the material, instruc-
tions, methods or ideas contained herein, or acting or refraining from acting as a result of
such use. The authors and publisher expressly disclaim all implied warranties, including
merchantability or fitness for any particular purpose. There will be no duty on the authors
or publisher to correct any errors or defects in the software.
Designations used by companies to distinguish their products are often claimed as trade-
marks. In all instances where John Wiley & Sons, Ltd is aware of a claim, the product names
appear in capital or all capital letters. Readers, however, should contact the appropriate
companies for more complete information regarding trademarks and registration.
This publication is designed to provide accurate and authoritative information in regard
to the subject matter covered. It is sold on the understanding that the Publisher is not
engaged in rendering professional services. If professional advice or other expert assis-
tance is required, the services of a competent professional should be sought.
Wiley also publishes its books in a variety of electronic formats. Some content that appears
in print may not be available in electronic books.

Library of Congress Cataloging-in-Publication Data


(to follow)

British Library Cataloguing in Publication Data


A catalogue record for this book is available from the British Library
0 470 84882 0

Typeset in 10/12.5pt Lucida Bright by T&T Productions Ltd, London.


Printed and bound in Great Britain by Biddies Ltd, Guildford and Kings Lynn.
This book is printed on acid-free paper responsibly manufactured from sustainable
forestry in which at least two trees are planted for each one used for paper production.
Contents

Preface ix

1 A Science of Computing 1
1.1 Debugging 2
1.2 Testing a Correct Program 3
1.3 Testing an Incorrect Program 5
1.4 Correct by Construction 6

2 A Searching Problem and Its Solution 9


2.1 Problem Statement 9
2.2 Problem Solution 11
2.3 Proof of Correctness 12
2.4 What, Why and How 14
2.5 Exercises 15
2.6 Summary 21

3 Calculational Proof 23
3.1 The Nature of Proof 23
3.2 Construction versus Verification 26
3.3 Formatting Calculations 31
3.3.1 Basic Structure 31
3.3.2 Hints 32
3.3.3 Relations between Steps 34
3.3.4 'If and 'Only If 36
3.4 A Classic Example 37
3.5 Summary 39

4 Implementation Issues 41
4.1 Binary Search 41
4.1.1 Implementation 44
4.2 Verifying Correctness—A Taster 45
4.3 Summary 52

5 Calculational Logic: Part 1 53


5.1 Logical Connectives 54
5.2 Boolean Equality 56
5.3 Examples of the Associativity of Equivalence 59
5.4 Continued Equivalences 61
5.5 The Island of Knights and Knaves 63
vi Contents

5.6 Negation 65
5.7 Summary 68

6 Number Conversion 71
6.1 The Floor Function 71
6.2 Properties of Floor 73
6.3 Indirect Equality 75
6.4 Rounding Off 77
6.5 Summary 80

7 Calculational Logic: Part 2 83


7.1 Disjunction 83
7.2 Conjunction 85
7.3 Implication 88
7.3.1 Definitions and Basic Properties 89
7.3.2 Replacement Rules 90
7.4 Exercises: Logic Puzzles 93
7.5 Summary 96

8 Maximum and Minimum 97


8.1 Definition of Maximum 97
8.2 Using Indirect Equality 98
8.3 Exercises 101
8.4 Summary 103

9 The Assignment Statement 105


9.1 Hoare Triples 105
9.2 Ghost Variables 107
9.3 Hoare Triples as Program Specifications 109
9.4 Assignment Statements 112
9.5 The Assignment Axiom 113
9.6 Calculating Assignments 115
9.7 Complications 118
9.8 Summary 119

10 Sequential Composition and Conditional Statements 121


10.1 Sequential Composition 121
10.2 The skip Statement 123
10.3 Conditional Statements 124
10.4 Reasoning about Conditional Statements 126
10.5 Constructing Conditional Statements 130
10.6 Combining the Rules 132
10.7 Summary 136

11 Quantifiers 137
11.1 DotDotDot and Sigmas 137
11.2 Introducing Quantifier Notation 141
11.2.1 Summation 141
11.2.2 Free and Bound Variables 143
11.2.3 Properties of Summation 146
11.2.4 The Gauss Legend 151
11.2.5 Warning 152
Contents vii

1 1.3 Universal and Existential Quantification 153


11.3.1 Universal Quantification 154
11.3.2 Existential Quantification 155
11.3.3 De Morgan's Rules 156
1 1 .4 Quantifier Rules 156
11.4.1 The Notation 157
11.4.2 Free and Bound Variables 158
11.4.3 Dummies 158
11.4.4 Range Part 158
11.4.5 Trading 159
11.4.6 Term Part 159
1 1 .4. 7 DistributMty Properties 159
11.5 Summary 163

12 Inductive Proofs and Constructions 165


12.1 Patterns and Invariants 166
12.2 Mathematical Induction 170
12.3 Strong Induction 175
12.4 From Verification to Construction 179
12.5 Summary 182

13 Iteration 183
13.1 The do-od Statement 183
13.2 Constructing Loops 184
13.3 Basic Arithmetic Operations 187
13.3.1 Summing the Elements of an Array 187
13.3.2 Evaluating a Polynomial 188
13.3.3 Evaluation of Powers 191
13.4 Summary 195

14 Sorting and Searching Algorithms 197


14.1 The Dutch National Flag 197
14.1.1 Problem Statement 197
14.1.2 The Solution 199
14.1.3 Verifying the Solution 201
14.2 Finding the K Smallest Values 205
14.2.1 The Specification 206
14.2.2 The Algorithm 208
14.3 Summary 212

15 Remainder Computation 215


15.1 Formal Specification 215
15.2 Elementary Algorithm 217
15.3 The mod and div Functions 219
15.3.1 Basic Properties 221
15.3.2 Separating mod from 4- 223
15.3.3 Separating + from mod 224
15.3.4 Modular Arithmetic 224
15.4 Long Division 228
15.4.1 Implementing Long Division 229
15.4.2 Discarding Auxiliary Variables 233
15.5 On-line Remainder Computation 234
1 5.6 Casting Out Nines 238
15.7 Summary 239
viii Contents

16 Cyclic Codes 241


16.1 Codes and Codewords 241
16.2 Boolean Polynomials 243
16.3 Data and Generator Polynomials 246
16.4 Long Division 247
16.5 Hardware Implementations 249
16.6 Summary 253

Appendix 255

Solutions to Exercises 263

References 331

Glossary of Symbols 333

Index 335
Preface

Programming is a highly skilled activity, and good programmers are few and far
between. In few other professions is the 90-10 rule (90% of the effort goes into
the last 10% of the work) so vitally important. Many programmers are able to
write programs that 'work' in most circumstances; few programmers know the
basic principles of program specification, let alone how to construct programs
that guarantee to meet their specifications in all circumstances.
It is no wonder. Many texts have been written that explain how to encode compu-
tational processes in some specific programming language (C, Java, Visual Basic,
or whatever), but few tackle the much harder problem of presenting the problem-
solving skills that are needed to formulate programming problems precisely and
concisely, and to convert those formulations into elegant implementations.
This book is about programming per se. It is about the most elementary princi-
ples of program construction—problem decomposition, invariant properties, and
guarantees of progress. It is intended to appeal to both novice programmers, who
wish to start on the right track, and to experienced programmers who wish to
properly master their craft.
Although the subject matter of the book is 'elementary', in the sense of foun-
dational, it is not 'easy'. Programming is challenging, and it is wrong to skirt the
issues or to wrap it up in a way that makes it seem otherwise. I have lectured
on this material for many years, mostly to undergraduates on computing science
degrees, and, occasionally, to professional programmers. Inevitably, it is the expe-
rienced programmers who appreciate its value the most. Novice programmers
have the additional hurdle of learning how to write code—too often in a highly
complex programming language. For them, the problem is the programming lan-
guage, whereas, of course, the programming language should not be a problem,
but part of the solution.
In order to present the real challenges of programming without obfuscation,
the book uses a very simple programming language, with just four programming
constructs—assignment, sequential composition, conditionals and loops. I have
omitted variable declarations, so that the focus of the book remains clear. Experts
will recognize the language as the Guarded Command Language, a simple, elegant
language designed by Dijkstra specifically for this purpose.
Preface

The book is a major revision of my earlier book Program Construction and Ver-
ification, published in 1986. Some sections remain the same, but there is much
that is different. The main difference is reflected in the omission of 'verification'
in the title. The primary goal of the book is to show how programs are constructed
to meet their specifications, by means of simple, mathematical calculations. The
emphasis on construction is crucial; the fact that the calculations can be formally
verified is also important, but much less so. Unfortunately, however, the empha-
sis in many related texts is the reverse; the fundamental principles of program
construction are introduced as a mechanism for performing a post hoc validation
of the program's correctness, and their integral role in the activity of developing
programs is neglected. Even worse, automatic verification is often given as the pri-
mary justification for their use. I have no doubt that this misplaced emphasis on
verification rather than construction has, for long, stood in the way of the accep-
tance and active use of the principles by practising programmers. Quite rightly,
professional programmers will strive to ensure that their programs are correct by
construction; it is this endeavour that this text aims to support.
Another aspect of the text that has undergone major revision is the discus-
sion of logical reasoning. I now realize just how inadequate my own education
in logic has been. The traditional style of reasoning in mathematics is to verify
a conjecture by means of a sequence of true statements, each statement being a
self-evident truth or implied by earlier ones. Calculational logic, as presented in
this book, places equality of propositions at the forefront. Mathematical theorems
are derived (not verified) by a process of algebraic calculation, just as in school
algebra and in differential and integral calculus.
The basis for calculational logic was formulated by Dijkstra and Scholten, in
their 1990 book Predicate Calculus and Program Calculus. In my view, their work
has initiated a major revolution in the art of effective reasoning. As yet, however,
it is largely unknown in mathematical circles. Hopefully, this book will help to
foster the widespread acceptance and application of their ideas.

Intended Readership
The book has been written primarily for self-study. Many exercises have been sup-
plied, with complete solutions, interspersed throughout the text. Only by making a
serious attempt at the exercises can the material be properly mastered. My hope is
that readers will do the exercises as they read, rather than postponing them until
the chapter has been completed. Many of the exercises are quite challenging; the
solutions are there to be consulted, so that the reader can build up confidence
and experience in an effective, calculational style of reasoning.
I anticipate that all readers will have an elementary, operational understanding
of at least one programming language. (By this, I mean that they will know about
the store ('memory') of a computer, and how an assignment statement updates the
store. They will also know how conditional statements, sequences of statements,
Preface xi

and loops are executed.) Most benefit will be gained by studying the book simulta-
neously with, or shortly after, learning how to code in a conventional (preferably
simple!) programming language. In this way, it is possible to appreciate just how
easy it is to get things wrong, and to gain the satisfaction of being confident that
you are right.
The assumed mathematical knowledge is minimal. Only simple properties of
arithmetic (including inequalities between integers and reals) are required. How-
ever, my experience of where students have difficulty (apart from the intrinsic
difficulty of the subject matter) is primarily in the mathematical skill of alge-
braic manipulation. Allowing sufficient time in the curriculum for the students to
develop this skill, rather than trying to cram it into an already overfull syllabus,
is the best remedy.
Lots more examples and exercises could have been included in the book. I have
deliberately refrained from doing so in order to keep the book relatively short.
My plan is to publish additional supporting material from time to time, access to
which can be gained via the URL http://www.wiley.com/go/backhouse.

Acknowledgments
It is a pleasure to record my gratitude to some of the people who have helped in
the realization of this book, however indirect their help may have been.
First and foremost, I am indebted to my own teachers, without whom my own
work would not have been possible. It will soon become very evident to the reader
of the 'Bibliographic Remarks' who has influenced me the most. I am particularly
indebted to the late Edsger W. Dijkstra, whose writings have been a major inspi-
ration for many years. I have also learnt much from David Giles's books; David
also reviewed an early draft of this book, in exceptionally comprehensive and
helpful detail, for which I am very grateful. Thanks, too, to Tony Hoare, for his
support and encouragement; I am also grateful to him and the Queen's University
of Belfast for kind permission to reproduce a major part of his inaugural lecture.
I learnt a very great deal during the 13 years that I spent in the Netherlands,
about mathematical method, about teamwork, and about academic and scientific
values. Many thanks go to Henk Doornbos, Paul Hoogendijk, Ed Voermans and
Jaap van der Woude, whose loyalty and enthusiasm are unforgettable. Thanks,
also, to Doaitse Swierstra for his support and friendship in difficult times. Special
thanks must go to Netty van Gasteren, who tragically died only recently, from
whom I learnt more than I really appreciated at the time.
A number of people have helped directly by reading drafts of the text, pointing
out errors and making suggestions for improvements. Robert L. Baber and Tony
Seda both gave me extensive and very thoughtful reviews of an earlier draft. I hope
that I have done justice to their criticisms. Diethard Michaelis has also sent me
many invaluable comments and corrections.
xii Preface

The book was prepared using Math/pad, a system that aims to integrate the
process of doing mathematics and writing about it. Its use, in combination with
the TjX and KTgX systems, has been of immeasurable benefit to me. I am grateful
to Eindhoven University of Technology and the Dutch government for their gen-
erous support of the development and implementation of Math/pad. I am greatly
indebted to Richard Verhoeven, who not only implemented the system, working
many long hours in doing so, but who also has always been, and continues to be,
extremely helpful, willing and generous, giving of his time far beyond the call of
duty.
Thanks are also due to the staff of John Wiley & Sons, Ltd, for their friendly
and professional help in producing this book. Thanks also to Sam Clark, of
T&T Productions Ltd, for the cheerful and competent way he has assisted in trying
to make sure that everything is exactly right.
As always, my biggest thanks go to my wife, Hilary.
Roland Backhouse
January 2003
1
A Science of
Computing

The hallmark of a science is the avoidance of error.


J. Robert Oppenheimer

The electronic, digital computer is a marvel of modern technology. Within a life-


time* it has developed from nothing to a tool whose use is so widespread that we
are often unaware that it is there. The first computers, built in the 1940s, were
monstrous. As recently as the 1960s and 1970s, it was common for new employ-
ees in a company to be shown 'the computer'—housed in a special-purpose, air-
conditioned room, to which admission was restricted to a select few. Nowadays,
computers are used in many household appliances, they are also used in cars,
trains and aircraft, and we even carry them around with us—laptops, palm-tops,
what would we do without them?
The developments in computer hardware have been truly phenomenal. But
developments in computer software have not kept pace. The programming lan-
guages that are in use today have changed little from the programming languages
that were developed in the 1950s, and programming remains a highly skilled activ-
ity. It is the nature of the task that is the problem. The hardware designer must
build a dumb machine, whose sole purpose is to slavishly execute the instructions
given to it by the programmer; the programmer has to design general-purpose sys-
tems and programs, at a level of detail at which they can be faithfully executed,
without error, by even the dumbest of machines.
Chapter 1: A Science of Computing

1.1 Debugging
There is a story about the current state of computer software that is widely
reported on the Internet. The chief executive of a multi-billion dollar software
company compared the computer industry with the car industry.
'If the automobile industry had kept up with technology like the com-
puter industry has,'
he is reported to have said,
'we would all be driving $25 cars that get 1000 to the gallon'.
'That may be true,'

was the swift response of the president of one automobile company,


'but who would want to drive a car that crashes twice a day?'
This story is, most likely, just a joke1. But, like all good jokes, it succeeds because
it reflects profoundly on modern-day reality.
At the time of writing (December 2002), it is still the case that computer soft-
ware is liable to spontaneously 'crash', due to simple programming errors (or
'bugs' as they are called in the software industry). In contrast, the car indus-
try is often held up as an exemplar of excellence in standards of safety and
reliability.
Memories are short. Cars may be (relatively) safe and reliable today but, in the
1950s, as highlighted by Ralph Nader in his acclaimed book Unsafe At Any Speed,
cars were knowingly made and sold that were liable to spontaneous crashes, and
car manufacturers were guilty of deliberately undermining efforts to invest in
improved standards. ('Safety and sales strategies do not mix' was the argument
used at the time.)
The computer industry of today is very much like the car industry of the 1950s.
Computers are still relatively new, and the novelty factor has not worn off, so that
guarantees of reliability and fitness-for-purpose are, for many, a low priority. Few
programmers are trained in scientific methods of constructing programs and,
consequently, they waste substantial amounts of effort 'debugging' their pro-
grams, rarely with complete success. ('Debugging' is the process of repeatedly
testing, followed by patching, the program, in an attempt to remove discovered
errors.)
The need for alternative, mathematically rigorous, program construction tech-
niques was recognized in the late 1960s when the large computer manufactur-
ers first began to realize that the costs of producing computer software were
1
At least, I think it is. It may be true, but it is difficult to verify the authenticity of material on the
Internet. For this reason, names have been omitted in this account.
Another Random Scribd Document
with Unrelated Content
PEASANTS OF THE VILLAGE OF BERMIGO DE SAYAGO
PROVINCE OF ZAMORA
Plate 214

GENERAL VIEW OF AVILA


Plate 215

GENERAL VIEW OF AVILA


Plate 216

VIEW OF AVILA
Plate 217

GATE OF THE ALCAZAR


AVILA
Plate 218

GATE OF SAN VICENTE


AVILA
Plate 219
GATE OF SAN VICENTE
AVILA
Plate 220

GATE OF SAN VICENTE


AVILA
Plate 221

GATE OF SAN VICENTE


AVILA
Plate 222
A STREET IN AVILA
Plate 223

VIEW OF THE CATHEDRAL


AVILA
Plate 224

EXTERIOR OF THE CATHEDRAL


Plate 225
ENTRANCE TO THE CATHEDRAL
AVILA
Plate 226

PLAN OF CATHEDRAL
Plate 227
AVILA CATHEDRAL
Plate 228
SIDE DOOR OF THE CATHEDRAL
AVILA
Plate 229
CATHEDRAL. PULPIT OF REPOUSSÉ IRON WORK
AVILA
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like