Prolog Programming - A First Course - Paul Brna.pdf

(635 KB) Pobierz
257798103 UNPDF
PrologProgramming
AFirstCourse
PaulBrna
March5,2001
Abstract
Thecourseforwhichthesenotesaredesignedisintendedforundergraduate
studentswhohavesomeprogrammingexperienceandmayevenhavewritten
afewprogramsinProlog.Theyarenotassumedtohavehadanyformal
courseineitherpropositionalorpredicatelogic.
Attheendofthecourse,thestudentsshouldhaveenoughfamiliaritywith
Prologtobeabletopursueanyundergraduatecoursewhichmakesuseof
Prolog.
Thisisaratherambitiousundertakingforacourseofonlytwelvelectures
sothelecturesaresupplementedwithexercisesandsmallpracticalprojects
whereverpossible.
ThePrologimplementationusedisSICStusPrologwhichiscloselymod-
elledonQuintusProlog(SICSistheSwedishInstituteofComputerScience).
Thereferencemanualshouldalsobeavailableforconsultation[SICStus,1988].
c ° PaulBrna1988
Contents
1Introduction 1
1.1DeclarativevsProceduralProgramming............ 1
1.2WhatKindofLogic?....................... 1
1.3AWarning............................ 2
1.4ARequest............................. 2
2KnowledgeRepresentation 3
2.1PropositionalCalculus...................... 3
2.2FirstOrderPredicateCalculus................. 4
2.3WeTurntoProlog....................... 5
2.4PrologConstants........................ 7
2.5GoalsandClauses........................ 8
2.6MultipleClauses......................... 8
2.7Rules................................ 9
2.8Semantics.............................10
2.9TheLogicalVariable.......................11
2.10RulesandConjunctions.....................12
2.11RulesandDisjunctions......................13
2.12BothDisjunctionsandConjunctions..............14
2.13WhatYouShouldBeAbleToDo................15
3Prolog’sSearchStrategy 16
3.1QueriesandDisjunctions ....................16
3.2ASimpleConjunction......................19
3.3ConjunctionsandDisjunctions.................21
3.4WhatYouShouldBeAbleToDo................23
4Unification,RecursionandLists 26
4.1Unification............................26
4.2Recursion.............................27
4.3Lists................................29
4.4WhatYouShouldBeAbleToDo................32
i
ii
5TheBoxModelofExecution 34
5.1TheBoxModel..........................34
5.2TheFlowofControl.......................35
5.3AnExampleusingtheByrdBoxModel ............36
5.4AnExampleusinganAND/ORProofTree..........38
5.5WhatYouShouldBeAbleToDo................38
6ProgrammingTechniquesandListProcessing 53
6.1The‘Reversibility’ofPrologPrograms............53
6.1.1EvaluationinProlog..................54
6.2CallingPatterns.........................55
6.3ListProcessing..........................56
6.3.1ProgramPatterns....................56
6.3.2ReconstructingLists...................60
6.4ProofTrees............................62
6.5WhatYouShouldBeAbleToDo................63
7ControlandNegation 66
7.1SomeUsefulPredicatesforControl...............66
7.2TheProblemofNegation....................67
7.2.1NegationasFailure....................68
7.2.2UsingNegationinCaseSelection............69
7.3SomeGeneralProgramSchemata................70
7.4WhatYouShouldBeAbleToDo................77
8ParsinginProlog 78
8.1SimpleEnglishSyntax......................78
8.2TheParseTree..........................79
8.3FirstAttemptatParsing....................80
8.4ASecondApproach.......................81
8.5PrologGrammarRules.....................82
8.6ToUsetheGrammarRules...................83
8.7HowtoExtractaParseTree..................83
8.8AddingArbitraryPrologGoals ................84
8.9WhatYouShouldBeAbleToDo................84
9ModifyingtheSearchSpace 86
9.1ASpecialControlPredicate...................86
9.1.1Commit..........................86
9.1.2MakeDeterminate....................89
9.1.3FailGoalNow......................90
iii
9.2ChangingtheProgram......................91
9.2.1DoNotDoIt!.......................92
9.2.2SometimesYouhaveTo!.................93
9.3WhatYouShouldBeAbleToDo................94
10PrologSyntax 97
10.1Constants.............................97
10.2Variables .............................98
10.3CompoundTerms.........................98
10.4(Compound)TermsasTrees...................99
10.5CompoundTermsandUnification...............99
10.6TheOccursCheck........................100
10.7ListsAreTermsToo.......................101
10.8HowToGlueTwoListsTogether................102
10.9RulesasTerms..........................104
10.10WhatYouShouldBeAbleToDo................105
11Operators 112
11.1TheThreeForms.........................112
11.1.1Infix............................112
11.1.2Prefix...........................113
11.1.3Postfix...........................113
11.2Precedence............................113
11.3AssociativityNotation......................116
11.3.1InfixOperators......................116
11.3.2ThePrefixCase.....................117
11.3.3PrefixOperators.....................117
11.3.4PostfixOperators.....................117
11.4HowtoFindOperatorDefinitions ...............117
11.5HowtoChangeOperatorDefinitions..............118
11.6AMoreComplexExample....................119
11.7WhatYouShouldBeAbleToDo................120
12AdvancedFeatures 122
12.1PowerfulFeatures.........................122
12.1.1PowerfulFeatures—Typing...............122
12.1.2PowerfulFeatures—SplittingUpClauses.......123
12.1.3PowerfulFeatures—ComparisonsofTerms......128
12.1.4PowerfulFeatures—FindingAllSolutions.......128
12.1.5PowerfulFeatures—FindOutaboutKnownTerms .130
Zgłoś jeśli naruszono regulamin