Discrete Mathematics Using a Computer John O’Donnell, Cordelia Hall and Rex Page Discrete Mathematics Using a Computer Second Edition John O’Donnell, PhD Cordelia Hall, PhD Computing Science Department, University of Glasgow, Glasgow G12 8QQ, UK Rex Page, PhD School of Computer Science, University of Oklahoma, Norman, Oklahoma, USA British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library of Congress Control Number: 2005935334 ISBN-10: 1-84628-241-1 ISBN-13: 978-1-84628-241-6 Printed on acid-free paper © Springer-Verlag London Limited 2006 Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Printed in the United States of America (HAM) 9 8 7 6 5 4 3 2 1 Springer Science+Business Media springer.com This book is dedicated to our parents. Preface to the Second Edition Computer science abounds with applications of discrete mathematics, yet stu- dents of computer science often study discrete mathematics in the context of purely mathematical applications. They have to figure out for themselves how to apply the ideas of discrete mathematics to computing problems. It is not easy. Most students fail to experience broad success in this enterprise, which is not surprising, since many of the most important advances in science and engineeringhavebeen,precisely,applicationsofmathematicstospecificscience and engineering problems. Tobesure,mostdiscretemathtextbooksincorporatesomeaspectsapplying discrete math to computing, but it usually takes the form of asking students to write programs to compute the number of three-ball combinations there are in a set of ten balls or, at best, to implement a graph algorithm. Few texts ask students to use mathematical logic to analyze properties of digital circuits or computer programs or to apply the set theoretic model of functions tounderstandhigher-orderoperations. Amajoraimofthistextistointegrate, tightly, the study of discrete mathematics with the study of central problems of computer science. Concepts in discrete mathematics are illustrated through the solution of problems that arise in software development, hardware design, and other fun- damental domains of computer science. The text introduces discrete math concepts and immediately applies them to computing problems. Applications of mathematical logic in design and analysis of hardware and software is an especiallystrongtheme. Thegoalinthispartofthematerialistopreparestu- dentsforaworldthatplacesahighvalueonthecorrectoperationofcomputing systems in safety-critical, security-sensitive, and embedded systems and recog- nizes that formal methods based in mathematical logic are the primary tools for ensuring that computing systems function properly in such environments. The emphasis, here, is on preparation. In commercial applications, mecha- nizedlogicenginesareessentialtotheenterpriseofapplyinglogictothedesign andimplementationofcomputinghardwareandsoftware. Thistextintroduces students to mechanized logic in the form of propositional proof checking, and, vii viii Preface throughnumerouspaper-and-pencilexercisesinapplyinglogictomathematical verification of hardware and software artifacts, gives students experience with thefundamentalnotionsusedbyengineerswhoapplymechanizedlogicengines to the design of commercial computing systems. We believe these skills will be of increasing value in computer and software engineering, and our experience suggests that such skills contribute positively, even in the short run, to the ability of students to successfully design and implement software. The text is organized in four parts: reasoning with equations, formal logic, set theory, and applications. The principle of induction is introduced early, for reasoning with equations, and applied to problems throughout the text. Reasoningwithequationscoversexamplesinseveraldomains,includingnatural numbers of course, but also including sequences and sets. The logic portion of the text discusses two frameworks for formal reasoning: the natural deduction formatofGentzenandanothersyntax-basedreasoningsystembasedinBoolean algebra. Propositional logic is introduced first, then predicate logic, both in a natural deduction and Boolean algebra setting. Set theory discusses the usual basics, and illustrates many of the concepts by applying induction to define the integers. The set theoretic definitions of relations and functions are discussed, along with the usual properties that categorize them and allow them to be combined and manipulated. The applications portion of the text covers two extended examples, one concerning the design of a circuit for n-bit, ripple-carryaddition, theotherontheimplementationofAVLtreeoperations. These augment the many, smaller examples that occur throughout the text and, together, help students understand how discrete mathematics contributes to the solution of difficult and important problems in computing. A website for the text contains a collection of tools for experimenting with most of the concepts introduced. Included among these is a proof-checking system for propositional calculus. Students can use this system to make sure their proofs are correct and, more importantly, to experience the notion that proofs can be entirely formal and, therefore, useful in verifying the correctness of software and digital circuits. Other tools allow experimentation with set operations, Boolean formulas, and the notions of predicate calculus. These tools are expressed in Haskell, and the various operations for experimentation, including proofs, are expressed using Haskell syntax. In addition, Haskell is usedtoexpressthesoftwareandhardwaredesignsthatillustratepracticaluses of logic and other aspects of discrete mathematics in computer science. We feel that Haskell is an ideal notational choice for these examples be- cause of its close affinity with customary algebraic notation. The compactness of software and hardware artifacts expressed in Haskell is another important advantage. Haskell serves both as a formal, mathematical notation, and as a practical and powerful programming language. This helps to strengthen the tight connection between mathematics and applications. Thus Haskell is used inthetextonanequalfootingwithothermathematicalnotations. Studentssee Haskellinitsroleasaprogramminglanguage,aswellasahardwaredescription Preface ix language, and the emphasis in this book is on reasoning about programs and circuits, not just programming. Wehopethatstudentswillfindtheexperienceoflearningaboutlogic,sets, mathematical induction, and other concepts of discrete mathematics and its applications to computing problems interesting and enjoyable, and that they will be able to use these ideas in subsequent studies and professional work in computer science. Software Tools for Discrete Mathematics Acentralpartofthisbookistheuseofthecomputertohelplearnthediscrete mathematics. The software (which is free; see below) provides many facilities that aid the student in learning the material: • Logic and set theory have many operators that are used to build mathe- matical expressions. The software allows the user to type in such expres- sions interactively and experiment with them. • Predicate logic expressions with quantifiers can be expanded into propo- sitional logic expressions, as long as the universe is finite and reasonably small. Thismakesthemeaningofthequantifiersmoreconcreteandhelps the development of intuition. • Students frequently misuse expressions in logic and set theory; a typical error that arises frequently is to write an expression that treats A ⊆ B as a set rather than a Boolean value. The software tools will immedi- ately flag such mistakes as type errors. Teaching experience shows that many students will have long-lasting misconceptions about basic nota- tions without immediate feedback. • A formal proof checker for natural deduction is provided. This allows students to find errors in their proofs before handing in exercises, and it also provides a quick and effective way for the instructor to check the validity of large numbers of proofs. Furthermore, the automated proof checkerunderscoresthenatureofformalproof;vagueorill-formedproofs are not acceptable. • Using a proof checker gives a deeper appreciation of the relationship be- tween discrete mathematics and computer science. The experience of debuggingaproofismuchlikedebuggingacomputerprogram; theproof checkerisitselfacomputerprogram(whichthestudentscanreadifthey wish to); proof checking software makes formal proof feasible for larger scale problems. • The techniques of recursion and induction are applied directly and for- mally to function definitions that the student can execute. x Preface The version of Haskell used in the book is Haskell98. This is a standard pure functional language with excellent support. Several implementations are freely available and they are supported on most major computers and oper- ating systems. Students can install the software on their own machines, and universities can, of course, install it on laboratory computers. The Software Tools for Discrete Mathematics package is a library of defini- tions that are loaded into Haskell. This package is available on the book web page (see Appendix B). Haskell is an ideal language for teaching discrete mathematics. It offers a powerful and concise expression language; many problems that would require writing a complete program of 10 to 100 lines of code in a language such as Pascal, C++, or Java can be written as a simple expression in Haskell, which is only a few lines long. This makes it possible to use Haskell interactively to experimentwiththemathematicalexpressionsofpropositionallogic,predicate logic, set theory, and relations. Such lightweight interactive exploration is infeasible in traditional imperative or object-oriented languages. Haskell is also well suited for complex applications, such as the proof checker used in Chapters 6 and 7, and the hardware description language used in Chapter 13. It is assumed that the reader of the book has no knowledge in advance about Haskell or functional programming; everything that is needed is covered here. Because it is self-contained, this book can be used in any curriculum, regardless of what programming languages happen to be in use. To the Student It’s best to read this book actively with pencil and paper at hand. As you read, try out the examples yourself. It is especially important to try some of theexercises,andsolutionstomanyofthemappearinAppendixC.Don’tjust readtheexerciseandthenthesolution—thebenefitcomesfromtryingtosolve an exercise yourself, even if you don’t get it right. When you find your own solution, or if you get stuck, then compare your solution with the one in the book. The web page for this book has additional information that will be useful as you study discrete mathematics: http://www.dcs.gla.ac.uk/ ˜jtod/discrete-mathematics/ Many of the exercises require the use of a computer with Haskell installed. Thesoftwareisfree,andit’sstraightforwardtodownloaditandinstallonyour ownmachine. Seethebookwebpageforinformationonobtainingthesoftware. Agoodwaytoimproveyourunderstandingofthematerialistoreadabout it at a more advanced level and also to learn about its application to real Preface xi problems. The Bibliography near the end of the book lists many good sources ofinformation,andeachchapterendswithsomesuggestionsforfurtherreading. We wish you success with your studies in mathematics and computer sci- ence! To the Instructor This book is primarily intended for students of computer science, and appli- cations of the mathematics to computing are stressed. No specific topics in computing are prerequisites, but some familiarity with elementary computer programming is assumed. The level is appropriate for courses in the first or second year of study. The contents of this book can be covered in a course of one semester. The Instructor’s Guide gives suggestions for organising the course, solu- tions to the exercises, additional problems with solutions and other teaching resources. It is available online: http://www.dcs.gla.ac.uk/˜jtod /discrete-mathematics/instructors-guide/ Because the four parts of the text are largely independent of one another, topics may be introduced in the order that best suits the needs of particular instructors and students. The only serious restriction on ordering is that Part I (reasoning with equations and induction), Part II (logic), and Part III (Sets) mustbecoveredbeforePartIV(applications). Reasoningwithequations,logic, andsettheorymaybecoveredinanyorder. Chapter1describesHaskell,which is used as a mathematical notation at many points in the text. Readers may need to refer to Chapter 1 as they read other portions of the text, but it is probably better to discuss that material on as as-needed basis instead of spendingablockoftimeonitinthebeginning. Thefollowinggraphshowsthe dependencies in more detail. Reasoning with equations Logic Sets Chapters 2−5 Chapters 6−7 Chapters 8−11 Applications Chapters 12−13 xii Preface A website accessible to instructors includes lesson plans, slides for lectures, homework problems, and exam questions for a course based on the text. Al- together, the website contains over 100 homework problems (with solutions), about 350 lecture slides, and more than 300 exam questions (with solutions). These materials are accessible on the web: http://www.dcs.gla.ac.uk/˜jtod /discrete-mathematics/instructors-guide/ Notation Standard mathematical notation is used in this book when discussing mathe- matics: A⊆B. Atypewriterfontisusedfornotationsthatareintendedtobe input to a computer: a ‘subset‘ b. For example, a general discussion in En- glish might say that a theorem is true; that theorem might make a statement about the proposition True, and a Haskell program would use the constant True. The end of a proof is marked by a square box (cid:1). Acknowledgements We would like to thank the following colleagues for their helpful feedback and encouragementduringtheprocessofwritingthisbook: TonyDavie, BillFind- lay, Joy Goodman, Mark Harman, Greg Michaelson, Genesio Gomes da Cruz Neto, Thomas Rauber, Richard Reid, Gudula Ru¨nger, and Noel Winstanley. We would also like to thank the students at the University of Glasgow and the University of Michigan, who gave both of us experience teaching with prelimi- naryversionsofthismaterial,andoureditors,KarenBarker,RosieKemp,and Catherine Brett, for their help in producing this book. Finally, we would like to thank the students and instructors who made use of the first edition of this text, especially those who took the time to let us know what they liked and disliked about it. We have benefitted from their comments and have tried to apply their ideas in this revision. All remaining errors are ours alone. John O’Donnell and Cordelia Hall Glasgow, Scotland Rex Page Norman, Oklahoma March 2006
Description: