ebook img

Logic programming with Prolog PDF

257 Pages·2013·1.17 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Logic programming with Prolog

Max Bramer Logic Programming with Prolog Second Edition Logic Programming with Prolog Max Bramer Logic Programming with Prolog Second Edition 123 MaxBramer SchoolofComputing UniversityofPortsmouth Portsmouth,UK ISBN978-1-4471-5486-0 ISBN978-1-4471-5487-7(eBook) DOI10.1007/978-1-4471-5487-7 SpringerLondonHeidelbergNewYorkDordrecht ©Springer-VerlagLondon2005,2013 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartof thematerialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation, broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionorinformation storageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilarmethodology nowknownorhereafterdeveloped.Exemptedfromthislegalreservationarebriefexcerptsinconnection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’slocation,initscurrentversion,andpermissionforusemustalwaysbeobtainedfromSpringer. PermissionsforusemaybeobtainedthroughRightsLinkattheCopyrightClearanceCenter.Violations areliabletoprosecutionundertherespectiveCopyrightLaw. Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant protectivelawsandregulationsandthereforefreeforgeneraluse. While the advice and information in this book are believed to be true and accurate at the date of publication,neithertheauthorsnortheeditorsnorthepublishercanacceptanylegalresponsibilityfor anyerrorsoromissionsthatmaybemade.Thepublishermakesnowarranty,expressorimplied,with respecttothematerialcontainedherein. Printedonacid-freepaper SpringerispartofSpringerScience+BusinessMedia(www.springer.com) Contents Introduction ...................................................................... ix 1 GettingStarted.............................................................. 1 1.1 StartingProlog ....................................................... 1 1.2 PrologPrograms...................................................... 3 1.3 DataObjectsinProlog:PrologTerms............................... 8 PracticalExercise1.......................................................... 11 2 ClausesandPredicates..................................................... 13 2.1 Clauses................................................................ 13 2.2 Predicates............................................................. 15 2.3 LoadingClauses...................................................... 18 2.4 Variables.............................................................. 22 PracticalExercise2.......................................................... 27 3 SatisfyingGoals............................................................. 29 3.1 Introduction........................................................... 29 3.2 Unification............................................................ 31 3.2.1 UnifyingCallTerms........................................ 31 3.3 EvaluatingGoals ..................................................... 35 3.4 Backtracking.......................................................... 39 3.5 SatisfyingGoals:ASummary....................................... 48 3.6 RemovingCommonVariables....................................... 50 3.7 ANoteonDeclarativeProgramming................................ 51 3.8 ImportantNoteonUser-ControlledBacktracking.................. 52 PracticalExercise3.......................................................... 53 4 OperatorsandArithmetic................................................. 55 4.1 Operators ............................................................. 55 4.2 Arithmetic ............................................................ 58 4.3 EqualityOperators ................................................... 61 v vi Contents 4.4 LogicalOperators .................................................... 64 4.5 MoreAboutOperatorPrecedence................................... 65 PracticalExercise4.......................................................... 68 5 InputandOutput........................................................... 69 5.1 Introduction........................................................... 69 5.2 OutputtingTerms..................................................... 69 5.3 InputtingTerms....................................................... 71 5.4 InputandOutputUsingCharacters.................................. 72 5.5 OutputtingCharacters................................................ 72 5.6 InputtingCharacters.................................................. 73 5.7 UsingCharacters:Examples......................................... 74 5.8 InputandOutputUsingFiles........................................ 76 5.9 FileOutput:ChangingtheCurrentOutputStream ................. 77 5.10 FileInput:ChangingtheCurrentInputStream..................... 77 5.10.1 ReadingfromFiles:EndofFile ........................... 78 5.10.2 ReadingfromFiles:EndofRecord........................ 78 5.11 UsingFiles:Examples ............................................... 79 PracticalExercise5.......................................................... 81 6 Loops......................................................................... 85 6.1 Introduction........................................................... 85 6.2 LoopingaFixedNumberofTimes.................................. 85 6.3 LoopingUntilaConditionIsSatisfied.............................. 89 6.3.1 Recursion ................................................... 89 6.3.2 Usingthe'repeat'Predicate................................. 91 6.4 BacktrackingwithFailure ........................................... 94 6.4.1 SearchingthePrologDatabase............................. 94 6.4.2 FindingMultipleSolutions................................. 96 PracticalExercise6.......................................................... 97 7 PreventingBacktracking .................................................. 99 7.1 Introduction........................................................... 99 7.2 TheCutPredicate .................................................... 99 7.3 CutwithFailure ...................................................... 105 PracticalExercise7.......................................................... 107 8 ChangingthePrologDatabase............................................ 109 8.1 ChangingtheDatabase:AddingandDeletingClauses............. 109 8.2 AddingClauses....................................................... 110 8.3 DeletingClauses...................................................... 111 8.4 ChangingtheDatabase:Example ................................... 112 8.5 MaintainingaDatabaseofFacts..................................... 114 PracticalExercise8.......................................................... 117 9 ListProcessing.............................................................. 119 9.1 RepresentingDataasLists........................................... 119 9.2 NotationforLists..................................................... 120 Contents vii 9.3 DecomposingaList.................................................. 122 9.4 Built-inPredicate:member .......................................... 124 9.5 Built-inPredicate:length ............................................ 125 9.6 Built-inPredicate:reverse ........................................... 126 9.7 Built-inPredicate:append ........................................... 127 9.8 ListProcessing:Examples........................................... 128 9.9 Usingfindall/3toCreateaList...................................... 132 PracticalExercise9.......................................................... 134 10 StringProcessing ........................................................... 137 10.1 ConvertingStringsofCharactersToandFromLists............... 137 10.2 JoiningTwoStrings.................................................. 138 10.3 TrimmingaString.................................................... 139 10.4 InputtingaStringofCharacters ..................................... 141 10.5 SearchingaString.................................................... 142 10.6 DividingaStringintoItsComponentParts......................... 144 PracticalExercise10......................................................... 146 11 MoreAdvancedFeatures.................................................. 147 11.1 Introduction........................................................... 147 11.2 ExtendingProlog:Arithmetic ....................................... 147 11.3 ExtendingProlog:OperationsonStrings ........................... 153 11.4 ExtendingProlog:Sets............................................... 155 11.5 ProcessingTerms..................................................... 157 PracticalExercise11......................................................... 163 12 UsingGrammarRulestoAnalyseEnglishSentences .................. 165 12.1 Introduction........................................................... 165 12.2 ParsingEnglishSentences........................................... 165 12.3 ConvertingSentencestoListForm.................................. 181 PracticalExercise12......................................................... 186 13 ProloginAction............................................................. 187 13.1 ImplementinganArtificialLanguage ............................... 187 13.2 DevelopinganExpertSystemShell................................. 200 PracticalExercise13......................................................... 210 Appendix1 Built-inPredicates............................................... 211 Appendix2 Built-inOperators ............................................... 217 Appendix3 SpecimenSolutionstoPracticalExercises.................... 221 Appendix4 Glossary........................................................... 243 Index............................................................................... 251 Introduction LogicProgrammingisthenamegiventoadistinctivestyleofprogramming,very differentfromthatofconventionalprogramminglanguagessuchasCCCandJava. Fans of Logic Programming would say that 'different' means clearer, simpler and generallybetter! AlthoughthereareotherLogicProgramminglanguages,byfarthemostwidely usedisProlog.ThenamestandsforProgramminginLogic.Thisbookteachesthe techniques of Logic Programming through the Prolog language. Prolog is based on research by computer scientists in Europe in the 1960s and 1970s, notably at the Universities of Marseilles, London and Edinburgh. The first implementation was at the University of Marseilles in the early 1970s. Further development at the University of Edinburgh led to a de facto standard version, now known as Edin- burgh Prolog. Prolog has been widely used for developing complex applications, especially in the field of Artificial Intelligence. Although it is a general-purpose language,itsmainstrengthsareforsymbolicratherthanfornumericalcomputation. Thedevelopers ofthelanguagewereresearchersworkingonautomatingmath- ematicaltheoremproving.Thisfieldisoftenknownascomputationallogic.Butif youarenotaComputerScientist,alogicianoramathematiciandonotletthisdeter you! This book is aimed at the 99.9 % of the population who are none of these. Thosewhoare,alreadyhaveanumberofexcellenttextbooksfromwhichtochoose. Theideathatthemethodsdevelopedbycomputationallogicianscouldbeusedas thebasisforapowerfulgeneralpurposeprogramminglanguagewasrevolutionary 30 years ago. Unfortunately most other programming languages have not yet caughtup. The most striking feature of Prolog for the newcomer is how much simpler the programs look than in other languages. Many language designers started out withgoodintentionsbutcouldnotresist,orperhapscouldnotavoid,makingtheir creationsoverelaborate.Insomelanguagesevenwritingthecustomarytestprogram to print out the words Hello World! to the user's screen is hard work. All the user hastodoinPrologistoenterwrite('HelloWorld!'). Traditional programming languages have one feature in common. They all containaseriesofinstructionsthataretobeperformed('executed')oneafteranother. ix

Description:
Logic Programming is the name given to a distinctive style of programming, very different from that of conventional programming languages such as C++ and Java. By far the most widely used Logic Programming language is Prolog. Prolog is a good choice for developing complex applications, especially in
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.