
Algebra of Programnimg Prentice Hall International Series in Computer Science C.A.R. Hoare, Series Editor APT., K. R., From Logic to Prolog ARNOLD, A., Finite Transition Systems ARNOLD, A. and GUESSARIAN, I., Mathematicsfor Computer Science BARR, M. and WELLS, C, Category Theoryfor Computing Science (2nd edn) BEN-ARI, M., Principles ofConcurrentand Distributed Programming BEN-ARI, M., Mathematical Logicfor Computer Science BEST, E., Semantics ofSequential and Parallel Programs BIRD, R. and de MOOR, O., The Algebra ofProgramming BIRD, R. and WADLER, P., Introduction to Functional Programming BOVET, D.P. and CRESCENZI, P., Introduction to the Theory ofComplexity de BROCK, B., Foundations ofSemantic Databases BRODA, EISENBACH, KHOSHNEVISAN and VICKERS, Reasoned Programming BRUNS, G., Distributed Systems Analysis with CCS BURKE, E. and FOXLEY, E., Logic Programming BURKE, E. and FOXLEY, E., Logic and Its Applications CLEMENT, T., Programming Language Paradigms DAHL, O.-J., Verifiable Programming DUNCAN, E., Microprocessor Programming and Software Development ELDER, J., Compiler Construction ELLIOTT, R.J. and HOARE, C.A.R. (eds), Scientific Applications ofMultiprocessors FREEMAN,T.L. and PHILLIPS, R.C.,Fara//e//VMmericfl/Algorithms GOLDSCHLAGER, L. and LISTER, A., Computer Science: A modern introduction (2nd edn) GORDON, M.J.C., Programming Language Theory and Its Implementation GRAY, P.M.D., KULKARNI, K.G. and PATON, N.W., Object-oriented Databases HAYES, I. (ed.), Specification Case Studies (2nd edn) HEHNER,E.C.R., The Logic ofProgramming HINCHEY, M.G. andBOWEN, I.V., Applications ofFormal Methods HOARE, C.A.R., Communicating Sequential Processes HOARE, C.A.R. and GORDON, M.J.C. (eds), Mechanized Reasoning and Hardware Design HOARE, C.A.R. and JONES, C.B. (eds), Essays in Computing Science HUGHES, J.G., Database Technology: A software engineering approach HUGHES, J.G., Object-oriented Databases INMOS LTD, Occam 2 Reference Manual JONES, C.B., Systematic Software Development Using VDM (2nd edn) JONES, C.B. and SHAW, R.C.F. (eds), Case Studies in Systematic Software Development JONES, G. and GOLDSMITH, M., Programming in Occam2 JONES, N.D., GOMARD, C.K. and SESTOFT, P., Partial Evaluation and Automatic Program Generatio JOSEPH, M. (ed.), Real-time Systems: Specification, verification and analysis KALDEWAIJ, A., Programming: The derivation ofalgorithms KING, P.J.B., Computer and Communications Systems Performance Modelling LALEMENT, R., Computation as Logic McCABE, F.G., Logic and Objects McCABE, F.G., High-level Programmer's Guide to the 68000 MEYER, B., Introduction to the Theory ofProgramming Languages MEYER, B., Object-oriented Software Construction MILNER, R., Communication and Concurrency MITCHELL, R., Abstract Data Types and Modula 2 MORGAN, C., Programmingfrom Specifications (2nd edn) OMONDI, A.R., ComputerArithmetic Systems PATON, COOPER, WILLIAMS and TRINDER, Database Programming Languages PEYTON JONES, S.L., The Implementation ofFunctional Programming Languages Algebra of Programming Richard Bird and Oege de Moor University of Oxford An imprint of Pearson Education Harlow, England London New York - Reading, Massachusetts * San, Francisco Toronto Don Mills, Ontario Sydney Tokyo Singapore J-Hong Kong Seoul Taipei Cape Town Madrid Mexico City Amsterdam Munich Paris Milan Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: http://www.pearsoneduc .com First published by Prentice Hall © Prentice Hall Europe 1997 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 or otherwise, without prior permission, in writing, from the publisher. Printed and bound in Great Britain by MPG Books Ltd, Bodmin, Cornwall Library of Congress Cataloguing-in-Publication Data Available from the publisher British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library ISBN0-13-507245-X 10 98765432 04 03 02 01 00 Contents Foreword ix Preface xi Programs 1 1.1 Datatypes 1 1.2 Natural numbers 4 1.3 Lists t 7 1.4 Trees 14 1.5 Inverses 16 1.6 Polymorphic functions 18 1.7 Pointwise and point-free 19 Functions and Categories 25 2.1 Categories 25 2.2 Functors 30 2.3 Natural transformations 33 2.4 Constructing datatypes 36 2.5 Products and coproducts 38 2.6 Initial algebras 45 2.7 Type functors 49 Applications 55 3.1 Banana-split 55 3.2 Ruby triangles and Horner's rule 58 3.3 The TfiK problem - part one 62 3.4 Conditions and conditionals 66 3.5 Concatenation and currying 70 VI Relations and Allegories 81 4.1 Allegories 81 4.2 Special properties of arrows 86 4.3 Tabular allegories 91 4.4 Locally complete allegories 96 4.5 Boolean allegories 101 4.6 Power allegories 103 Datatypes in Allegories 111 5.1 Relators 111 5.2 Relational products 114 5.3 Relational coproducts 117 5.4 The power relator 119 5.5 Relational catamorphisms 121 5.6 Combinatorial functions 123 5.7 Lax natural transformations 132 Recursive Programs 137 6.1 Digits of a number 137 6.2 Least fixed points 140 6.3 Hylomorphisms 142 6.4 Fast exponentiation and modulus computation 144 6.5 Unique fixed points 146 6.6 Sorting by selection 151 6.7 Closure 157 Optimisation Problems 165 7.1 Minimum and maximum 166 7.2 Monotonic algebras 172 7.3 Planning a company party 175 7.4 Shortest paths on a cylinder 179 7.5 The security van problem 184 Thinning Algorithms 193 8.1 Thinning 193 8.2 Paths in a layered network 196 8.3 Implementing thin 199 8.4 The knapsack problem 205 8.5 The paragraph problem 207 8.6 Bitonic tours 212 Contents vn Dynamic Programming 219 9.1 Theory 220 9.2 The string edit problem 225 9.3 Optimal bracketing 230 9.4 Data compression 238 Greedy Algorithms 245 10.1 Theory 245 10.2 The detab-entab problem 246 10.3 The minimum tardiness problem 253 10.4 The T]eX problem - part two 259 Appendix 265 Bibliography 271 Index 291 Foreword It is a great pleasure and privilege to introduce this book on the Algebra of Programming as the hundredth book in the Prentice Hall International Series in Computing Science. It develops and consolidates one of the abiding and central themes of the series: it codifies the basic laws of algorithmics, and shows how they can be used to classify many ingenious and important programs into families related by the algebraic properties of their specifications. The formulae and equations that you will see here share the elegance of those which underlie physics or chemistry or any other branch of basic science; and like them, they inspire our interest, enlarge our understanding, and hold out promise of enduring benefits in application. Tony Hoare Preface Our purpose in this book is to show how to calculate programs. We describe an algebraic approach to programming, suitable both for the derivation of individual programs and for the study of programming principles in general. The programming principles we have in mind are those paradigms and strategies of program construction that form the core of the subject known as Algorithm Design. Examples of such principles include: dynamic programming, greedy algorithms, exhaustive search, and divide and conquer. The main ideas of the algebraic approach are illustrated by an extensive study of optimisation problems, conducted in Chapters 7-10. These are problems that involve finding a largest, smallest, cheapest, and so on, member of a set of possible solutions satisfying some given constraints. It is surprising how many computational problems can be specified in terms of optimising some suitable measure, even problems that do not at first sight fall into the traditional areas of combinatorial optimisation. However, the book is not primarily about optimisation problems, rather it is about one approach to solving programming problems in general. Our mathematical framework is a categorical calculus of relations. The calculus is categorical because we want to formulate algorithmic strategies without reference to specific datatypes, and relational because we need a degree of freedom in specification and proof that a calculus of functions alone would not provide. With the help of this calculus, the standard principles of algorithm design can be formulated as theorems about classes of problems whose specifications possess a particular structure. The problems are abstract in the sense that they are parameterised by one or more datatypes. These theorems say that, under appropriate conditions, a certain strategy works and leads to a particular form of abstract solution. Specific algorithms for specific problems are obtained by checking that the conditions hold and instantiating the results. The solution may take the form of a function, but more usually a relation, characterised as the solution to a certain recursive equation. The recursive equation is then refined to a recursive program that delivers a function, and the result is translated into a functional programming Xll Preface language. All the programs derived in Chapters 7-10 follow this pattern, and the popular language Gofer (Jones 1994) is used to implement the results. A categorical calculus provides not only a means for formulating algorithmic strategies abstractly, but also a smooth and integrated framework for conducting proofs. The style employed throughout the book is one of equational and inequational point-free reasoning with functions and relations. A point-free calculation is one in which the expressions under manipulation denote functions or relations, built using functional and relational composition as the basic combining form. In contrast, pointwise reasoning is reasoning conducted at the level of functional or relational application and expressed in a formalism such as the predicate calculus. The point-free style is intrinsic to a categorical approach, but is less common in proofs about programs.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages312 Page
-
File Size-