The Haskell Road to Logic, Math and Programming KeesDoetsandJanvanEijck March4, 2004 Contents Preface v 1 GettingStarted 1 1.1 StartinguptheHaskellInterpreter . . . . . . . . . . . . . . . . . 2 1.2 ImplementingaPrimeNumberTest . . . . . . . . . . . . . . . . 3 1.3 HaskellTypeDeclarations . . . . . . . . . . . . . . . . . . . . . 8 1.4 IdentifiersinHaskell . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5 PlayingtheHaskellGame . . . . . . . . . . . . . . . . . . . . . 12 1.6 HaskellTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.7 ThePrimeFactorizationAlgorithm . . . . . . . . . . . . . . . . . 19 1.8 ThemapandfilterFunctions. . . . . . . . . . . . . . . . . . . 20 1.9 HaskellEquationsandEquationalReasoning . . . . . . . . . . . 24 1.10 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2 TalkingaboutMathematicalObjects 27 2.1 LogicalConnectivesandtheirMeanings . . . . . . . . . . . . . . 28 2.2 LogicalValidityandRelatedNotions. . . . . . . . . . . . . . . . 38 2.3 MakingSymbolicFormExplicit . . . . . . . . . . . . . . . . . . 50 2.4 LambdaAbstraction. . . . . . . . . . . . . . . . . . . . . . . . . 58 2.5 DefinitionsandImplementations . . . . . . . . . . . . . . . . . . 60 2.6 AbstractFormulasandConcreteStructures . . . . . . . . . . . . 61 2.7 LogicalHandlingoftheQuantifiers . . . . . . . . . . . . . . . . 64 2.8 QuantifiersasProcedures . . . . . . . . . . . . . . . . . . . . . . 68 2.9 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 3 TheUseofLogic: Proof 71 3.1 ProofStyle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 3.2 ProofRecipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.3 RulesfortheConnectives . . . . . . . . . . . . . . . . . . . . . . 78 3.4 RulesfortheQuantifiers . . . . . . . . . . . . . . . . . . . . . . 90 i ii CONTENTS 3.5 SummaryoftheProofRecipes . . . . . . . . . . . . . . . . . . . 96 3.6 SomeStrategicGuidelines . . . . . . . . . . . . . . . . . . . . . 99 3.7 ReasoningandComputationwithPrimes. . . . . . . . . . . . . . 103 3.8 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 4 Sets,TypesandLists 113 4.1 Let’sTalkAboutSets . . . . . . . . . . . . . . . . . . . . . . . . 114 4.2 Paradoxes,TypesandTypeClasses . . . . . . . . . . . . . . . . . 121 4.3 SpecialSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 4.4 AlgebraofSets . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 4.5 PairsandProducts. . . . . . . . . . . . . . . . . . . . . . . . . . 136 4.6 ListsandListOperations . . . . . . . . . . . . . . . . . . . . . . 139 4.7 ListComprehensionandDatabaseQuery . . . . . . . . . . . . . 145 4.8 UsingListstoRepresentSets . . . . . . . . . . . . . . . . . . . . 149 4.9 ADataTypeforSets . . . . . . . . . . . . . . . . . . . . . . . . 153 4.10 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 5 Relations 161 5.1 TheNotionofaRelation . . . . . . . . . . . . . . . . . . . . . . 162 5.2 PropertiesofRelations . . . . . . . . . . . . . . . . . . . . . . . 166 5.3 ImplementingRelationsasSetsofPairs . . . . . . . . . . . . . . 175 5.4 ImplementingRelationsasCharacteristicFunctions . . . . . . . . 182 5.5 EquivalenceRelations. . . . . . . . . . . . . . . . . . . . . . . . 188 5.6 EquivalenceClassesandPartitions . . . . . . . . . . . . . . . . . 192 5.7 IntegerPartitions . . . . . . . . . . . . . . . . . . . . . . . . . . 202 5.8 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 6 Functions 205 6.1 BasicNotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 6.2 Surjections,Injections,Bijections . . . . . . . . . . . . . . . . . 218 6.3 FunctionComposition . . . . . . . . . . . . . . . . . . . . . . . 222 6.4 InverseFunction. . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.5 PartialFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . 229 6.6 FunctionsasPartitions . . . . . . . . . . . . . . . . . . . . . . . 232 6.7 Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 6.8 Congruences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 6.9 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 7 InductionandRecursion 239 7.1 MathematicalInduction . . . . . . . . . . . . . . . . . . . . . . . 239 7.2 RecursionovertheNaturalNumbers . . . . . . . . . . . . . . . . 246 7.3 TheNatureofRecursiveDefinitions . . . . . . . . . . . . . . . . 251 CONTENTS iii 7.4 InductionandRecursionoverTrees . . . . . . . . . . . . . . . . 255 7.5 InductionandRecursionoverLists . . . . . . . . . . . . . . . . . 265 7.6 SomeVariationsontheTowerofHanoi . . . . . . . . . . . . . . 273 7.7 InductionandRecursionoverOtherDataStructures . . . . . . . . 281 7.8 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 8 WorkingwithNumbers 285 8.1 AModuleforNaturalNumbers. . . . . . . . . . . . . . . . . . . 286 8.2 GCDandtheFundamentalTheoremofArithmetic . . . . . . . . 289 8.3 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 8.4 ImplementingIntegerArithmetic . . . . . . . . . . . . . . . . . . 297 8.5 RationalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . 299 8.6 ImplementingRationalArithmetic . . . . . . . . . . . . . . . . . 305 8.7 IrrationalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . 309 8.8 TheMechanic’sRule . . . . . . . . . . . . . . . . . . . . . . . . 313 8.9 ReasoningaboutReals . . . . . . . . . . . . . . . . . . . . . . . 315 8.10 ComplexNumbers . . . . . . . . . . . . . . . . . . . . . . . . . 319 8.11 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 9 Polynomials 331 9.1 DifferenceAnalysisofPolynomialSequences . . . . . . . . . . . 332 9.2 GaussianElimination . . . . . . . . . . . . . . . . . . . . . . . . 337 9.3 PolynomialsandtheBinomialTheorem . . . . . . . . . . . . . . 344 9.4 PolynomialsforCombinatorialReasoning . . . . . . . . . . . . . 352 9.5 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 10 Corecursion 361 10.1 CorecursiveDefinitions . . . . . . . . . . . . . . . . . . . . . . . 362 10.2 ProcessesandLabeledTransitionSystems . . . . . . . . . . . . . 365 10.3 ProofbyApproximation . . . . . . . . . . . . . . . . . . . . . . 373 10.4 ProofbyCoinduction . . . . . . . . . . . . . . . . . . . . . . . . 379 10.5 PowerSeriesandGeneratingFunctions . . . . . . . . . . . . . . 385 10.6 ExponentialGeneratingFunctions . . . . . . . . . . . . . . . . . 396 10.7 FurtherReading . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 11 FiniteandInfiniteSets 399 11.1 MoreonMathematicalInduction . . . . . . . . . . . . . . . . . . 399 11.2 Equipollence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 11.3 InfiniteSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 11.4 Cantor’sWorldImplemented . . . . . . . . . . . . . . . . . . . . 418 11.5 CardinalNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . 420 iv CONTENTS TheGreekAlphabet 423 References 424 Index 428 Preface Purpose Long ago, when Alexander the Great asked the mathematician Menaechmus for a crash course in geometry, he got the famous reply “There is no royal road to mathematics.” Where there was no shortcut for Alexander, there is no shortcut forus. Still,thefactthatwehaveaccesstocomputersandmatureprogramming languages means that there are avenues for us that were denied to the kings and emperorsofyore. Thepurposeofthisbookistoteachlogicandmathematicalreasoninginpractice, andtoconnectlogicalreasoningwithcomputerprogramming. Theprogramming language that will be our tool for this is Haskell, a member of the Lisp family. Haskellemergedinthelastdecadeasastandardforlazyfunctionalprogramming, aprogrammingstylewhereargumentsareevaluatedonlywhenthevalueisactu- allyneeded. Functionalprogrammingisaformofdescriptiveprogramming,very different from the style of programming that you find in prescriptive languages likeCorJava. Haskellisbasedonalogicaltheoryofcomputablefunctionscalled thelambdacalculus. Lambdacalculusisaformallanguagecapableofexpressingarbitrary computablefunctions. Incombinationwithtypesitformsacompact waytodenoteontheonehandfunctionalprogramsandontheother handmathematicalproofs.[Bar84] Haskellcanbeviewedasaparticularlyelegantimplementationofthelambdacal- culus. It is a marvelousdemonstration tool for logic and math because its func- tionalcharacterallowsimplementationstoremainveryclosetotheconceptsthat getimplemented,whilethelazinesspermitssmoothhandlingofinfinitedatastruc- tures. v vi Haskellsyntax is easy to learn, andHaskell programsare constructedand tested in a modular fashion. This makes the language well suited for fast prototyping. Programmers find to their surprise that implementation of a well-understood al- gorithm in Haskell usually takes far less time than implementation of the same algorithminotherprogramminglanguages. Gettingfamiliarwithnewalgorithms throughHaskellisalsoquiteeasy. LearningtoprograminHaskellislearningan extremelyusefulskill. Throughout the text, abstract concepts are linked to concrete representations in Haskell. Haskellcomeswithaneasytouseinterpreter,Hugs. Haskellcompilers, interpretersanddocumentationarefreelyavailablefromtheInternet[HT]. Every- thingonehastoknowaboutprogramminginHaskelltounderstandtheprograms inthe bookis explainedaswegoalong, butwe donotcovereveryaspectofthe language.ForafurtherintroductiontoHaskellwereferthereaderto[HFP96]. Logic in Practice The subject of this book is the use of logic in practice, more in particular the useoflogicinreasoningaboutprogrammingtasks. Logicisnottaughthereasa mathematicaldisciplineperse,butasanaidintheunderstandingandconstruction of proofs, and as a tool for reasoning about formal objects like numbers, lists, trees, formulas, and so on. As we go along, we will introduce the concepts and tools that form the set-theoretic basis of mathematics, and demonstrate the role of these concepts and tools in implementations. These implementations can be thoughtofasrepresentationsofthemathematicalconcepts. Althoughitmaybearguedthatthelogicthatisneededforaproperunderstanding ofreasoninginreasonedprogrammingwillgetacquiredmoreorlessautomatically in the process of learning (applied) mathematics and/or programming, students nowadaysenteruniversitywithoutanyexperiencewhatsoeverwithmathematical proof,thecentralnotionofmathematics. TherulesofChapter3representadetailedaccountofthestructureofaproof.The purposeofthisaccountistogetthestudentacquaintedwithproofsbyputtingem- phasisonlogicalstructure. Thestudentisencouragedtowrite“detailed”proofs, witheverylogicalmovespelledoutinfull. Thenextgoalistomoveontowriting “concise”proofs,in thecustomarymathematicalstyle, whilekeepingthe logical structureinmind. Oncethestudenthasarrivedatthisstage,mostofthelogicthat isexplainedinChapter3cansafelybeforgotten,orbetter,cansafelyfadeintothe subconsciousnessofthematuredmathematicalmind. PREFACE vii Pre- and Postconditions of Use Wedonotassumethatourreadershavepreviousexperiencewitheitherprogram- mingorconstructionofformalproofs. Wedoassumepreviousacquaintancewith mathematical notation, at the level of secondary school mathematics. Wherever necessary,wewillrecallrelevantfacts.Everythingoneneedstoknowaboutmath- ematical reasoning or programmingis explained as we go along. We do assume that our readers are able to retrievesoftware from the Internet and install it, and thattheyknowhowtouseaneditorforconstructingprogramtexts. After having workedthrough the materialin the book, i.e., after having digested the text and having carried out a substantial number of the exercises, the reader willbeabletowriteinterestingprograms,reasonabouttheircorrectness,anddoc- ument them in a clear fashion. The reader will also have learned how to set up mathematicalproofsinastructuredway,andhowtoreadanddigestmathematical proofswrittenbyothers. How to Use the Book Chapters 1–7 of the book are devoted to a gradual introduction of the concepts, toolsandmethodsofmathematicalreasoningandreasonedprogramming. Chapter 8 tells the story of how the various number systems (natural numbers, integers, rationals, reals, complex numbers) can be thought of as constructed in stagesfromthenaturalnumbers.Everythinggetslinkedtotheimplementationsof thevariousHaskelltypesfornumericalcomputation. Chapter 9 starts with the question of how to automate the task of finding closed formsforpolynomialsequences.Itisdemonstratedhowthistaskcanbeautomated withdifferenceanalysisplusGaussianelimination. Next,polynomialsareimple- mentedaslistsoftheircoefficients,withtheappropriatenumericaloperations,and itisshownhowthisrepresentationcanbeusedforsolvingcombinatorialproblems. Chapter10providesthefirstgeneraltextbooktreatment(asfarasweknow)ofthe importanttopicofcorecursion.Thechapterpresentstheproofmethodssuitablefor reasoningaboutcorecursivedatatypeslikestreamsandprocesses,andthengoes ontointroducepowerseriesasinfinitelistsofcoefficients,andtodemonstratethe usesofthisrepresentationforhandlingcombinatorialproblems. Thisgeneralizes theuseofpolynomialsforcombinatorics. Chapter 11 offers a guided tour through Cantor’s paradise of the infinite, while providingextrachallengesintheformofawiderangeofadditionalexercises. viii The book can be used as a course textbook, but since it comes with solutions to allexercises(electronicallyavailablefromtheauthorsuponrequest)itisalsowell suitedforprivatestudy. CoursesbasedonthebookcouldstartwithChapters1–7, andthenmakeachoicefromtheremainingChapters. Herearesomeexamples: RoadtoNumericalComputation Chapters1–7,followedby8and9. RoadtoStreamsandCorecursion Chapters1–7,followedby9and10. RoadtoCantor’sParadise Chapters1–7,followedby11. Study of the remaining parts of the book can then be set as individual tasks for studentsreadyforanextrachallenge. Theguidelinesforsettingupformalproofs in Chapter 3 should be recalled from time to time while studying the book, for properdigestion. Exercises Partsofthetextandexercisesmarkedbya*aresomewhatharderthantherestof thebook. All exercises are solved in the electronically avaible solutions volume. Before turningtothesesolutions,oneshouldreadtheImportantAdvicetotheReaderthat thisvolumestartswith. Book Website and Contact TheprogramsinthisbookhaveallbeentestedwithHugs98,theversionofHugs thatimplementstheHaskell98standard. Thefullsourcecodeofallprogramsis integrated in the book; in fact, each chapter can be viewed as a literate program [Knu92] in Haskell. The source code of all programs discussed in the text can be foundon the website devotedto thisbook, ataddress http://www.cwi.nl/ ~jve/HR.Hereyoucanalsofindalistoferrata,andfurtherrelevantmaterial. Readerswhowanttosharetheircommentswiththeauthorsareencouragedtoget [email protected]. PREFACE ix Acknowledgments Remarksfromthepeoplelistedbelowhavesparkedoffnumerousimprovements. ThankstoJohanvanBenthem,JanBergstra,JacobBrunekreef,ThierryCoquand (whofoundthelecturenotesontheinternetandsentushiscomments),Timvan Erven, Wan Fokkink, Evan Goris, Robbert de Haan, Sandor Heman, Eva Hoog- land, Rosalie Iemhoff, Dick de Jongh, Anne Kaldewaij, Breannda´n O´ Nualla´in, Alban Ponse, Vincent van Oostrom, Piet Rodenburg, Jan Rutten, Marco Swaen, JanTerlouw,JohnTromp,YdeVenema,AlbertVisser andStephanieWehnerfor suggestionsand criticisms. The beautifulimplementation of the sieve of Eratos- thenesinSection3.7wassuggestedtousbyFer-JandeVries. The course on which this book is based was developedat ILLC (the Institute of Logic, Language and Computation of the University of Amsterdam) with finan- cial support from the Spinoza Logic in Action initiative of Johan van Benthem, whichisherewithgratefullyacknowledged.WealsowishtothankILLCandCWI (Centrum voor Wiskunde en Informatica, or Centre for Mathematics and Com- puter Science, also in Amsterdam), the home institute of the second author, for providing us with a supportive working environment. CWI has kindly granted permissiontoreusematerialfrom[Doe96]. ItwasKrzysztofAptwho,perceivingtheneedofadeadline,spurredusontoget intouchwithapublisherandputourselvesundercontract.