Prolog Programming a First Course
Total Page:16
File Type:pdf, Size:1020Kb
Prolog Programming A First Course Paul Brna March 5, 2001 Abstract The course for which these notes are designed is intended for undergraduate students who have some programming experience and may even have written a few programs in Prolog. They are not assumed to have had any formal course in either propositional or predicate logic. At the end of the course, the students should have enough familiarity with Prolog to be able to pursue any undergraduate course which makes use of Prolog. This is a rather ambitious undertaking for a course of only twelve lectures so the lectures are supplemented with exercises and small practical projects wherever possible. The Prolog implementation used is SICStus Prolog which is closely mod- elled on Quintus Prolog (SICS is the Swedish Institute of Computer Science). The reference manual should also be available for consultation [SICStus, 1988]. °c Paul Brna 1988 Contents 1 Introduction 1 1.1 Declarative vs Procedural Programming . 1 1.2 What Kind of Logic? . 1 1.3 A Warning . 2 1.4 A Request . 2 2 Knowledge Representation 3 2.1 Propositional Calculus . 3 2.2 First Order Predicate Calculus . 4 2.3 We Turn to Prolog ....................... 5 2.4 Prolog Constants . 7 2.5 Goals and Clauses . 8 2.6 Multiple Clauses . 8 2.7 Rules . 9 2.8 Semantics . 10 2.9 The Logical Variable . 11 2.10 Rules and Conjunctions . 12 2.11 Rules and Disjunctions . 13 2.12 Both Disjunctions and Conjunctions . 14 2.13 What You Should Be Able To Do . 15 3 Prolog’s Search Strategy 16 3.1 Queries and Disjunctions . 16 3.2 A Simple Conjunction . 19 3.3 Conjunctions and Disjunctions . 21 3.4 What You Should Be Able To Do . 23 4 Unification, Recursion and Lists 26 4.1 Unification . 26 4.2 Recursion . 27 4.3 Lists . 29 4.4 What You Should Be Able To Do . 32 i ii 5 The Box Model of Execution 34 5.1 The Box Model . 34 5.2 The Flow of Control . 35 5.3 An Example using the Byrd Box Model . 36 5.4 An Example using an AND/OR Proof Tree . 38 5.5 What You Should Be Able To Do . 38 6 Programming Techniques and List Processing 53 6.1 The ‘Reversibility’ of Prolog Programs . 53 6.1.1 Evaluation in Prolog . 54 6.2 Calling Patterns . 55 6.3 List Processing . 56 6.3.1 Program Patterns . 56 6.3.2 Reconstructing Lists . 60 6.4 Proof Trees . 62 6.5 What You Should Be Able To Do . 63 7 Control and Negation 66 7.1 Some Useful Predicates for Control . 66 7.2 The Problem of Negation . 67 7.2.1 Negation as Failure . 68 7.2.2 Using Negation in Case Selection . 69 7.3 Some General Program Schemata . 70 7.4 What You Should Be Able To Do . 77 8 Parsing in Prolog 78 8.1 Simple English Syntax . 78 8.2 The Parse Tree . 79 8.3 First Attempt at Parsing . 80 8.4 A Second Approach . 81 8.5 Prolog Grammar Rules . 82 8.6 To Use the Grammar Rules . 83 8.7 How to Extract a Parse Tree . 83 8.8 Adding Arbitrary Prolog Goals . 84 8.9 What You Should Be Able To Do . 84 9 Modifying the Search Space 86 9.1 A Special Control Predicate . 86 9.1.1 Commit . 86 9.1.2 Make Determinate . 89 9.1.3 Fail Goal Now . 90 iii 9.2 Changing the Program . 91 9.2.1 Do Not Do It! . 92 9.2.2 Sometimes You have To! . 93 9.3 What You Should Be Able To Do . 94 10 Prolog Syntax 97 10.1 Constants . 97 10.2 Variables . 98 10.3 Compound Terms . 98 10.4 (Compound) Terms as Trees . 99 10.5 Compound Terms and Unification . 99 10.6 The Occurs Check . 100 10.7 Lists Are Terms Too . 101 10.8 How To Glue Two Lists Together . 102 10.9 Rules as Terms . 104 10.10What You Should Be Able To Do . 105 11 Operators 112 11.1 The Three Forms . 112 11.1.1 Infix . 112 11.1.2 Prefix . 113 11.1.3 Postfix . 113 11.2 Precedence . 113 11.3 Associativity Notation . 116 11.3.1 Infix Operators . 116 11.3.2 The Prefix Case . 117 11.3.3 Prefix Operators . 117 11.3.4 Postfix Operators . 117 11.4 How to Find Operator Definitions . 117 11.5 How to Change Operator Definitions . 118 11.6 A More Complex Example . 119 11.7 What You Should Be Able To Do . 120 12 Advanced Features 122 12.1 Powerful Features . 122 12.1.1 Powerful Features —Typing . 122 12.1.2 Powerful Features —Splitting Up Clauses . 123 12.1.3 Powerful Features —Comparisons of Terms . 128 12.1.4 Powerful Features —Finding All Solutions . 128 12.1.5 Powerful Features —Find Out about Known Terms . 130 iv 12.2 Open Lists and Difference Lists . 131 12.3 Prolog Layout . 136 12.3.1 Comments . 136 12.4 Prolog Style . 138 12.4.1 Side Effect Programming . 138 12.5 Prolog and Logic Programming . 140 12.5.1 Prolog and Resolution . 140 12.5.2 Prolog and Parallelism . 140 12.5.3 Prolog and Execution Strategies . 141 12.5.4 Prolog and Functional Programming . 141 12.5.5 Other Logic Programming Languages . 141 12.6 What You Should Be Able To Do . 141 A A Short Prolog Bibliography 142 B Details of the SICStus Prolog Tracer 145 C Solutions and Comments on Exercises for Chapter ?? 148 C.1 Exercise 2.1 . 148 C.2 Execise 2.2 . 149 C.3 Exercise 2.3 . 150 C.4 Exercise 2.4 . 150 C.5 Exercise 2.5 . 151 C.6 Exercise 2.6 . 152 C.7 Exercise 2.7 . 152 D Solutions and Comments on Exercises for Chapter ?? 155 D.1 Exercise 3.1 . 155 D.2 Exercise 3.2 . 156 E Solutions and Comments on Exercises for Chapter ?? 160 E.1 Exercise 4.1 . 160 E.2 Exercise 4.2 . 160 E.3 Exercise 4.3 . 162 F Solutions and Comments on Exercises for Chapter ?? 165 F.1 Exercise 6.1 . 165 G Solutions and Comments on Exercises for Chapter ?? 175 G.1 Exercise 8.1 . 175 H Solutions and Comments on Exercises for Chapter ?? 178 H.1 Exercise 9.1 . 178 I Solutions and Comments on Exercises for Chapter ?? 183 I.1 Exercise 11.1 . 183 J Solutions and Comments on Exercises for Chapter ?? 184 J.1 Exercise 12.1 . 184 List of Figures 3.1 A Failed Match . 18 3.2 A Successful Match . 20 5.1 The Byrd Box Model Illustrated . 34 5.2 Illustrating Simple Flow of Control . 36 5.3 Program Example with Byrd Box Representation . 37 5.4 The AND/OR Tree for the Goal a(X,Y) . 38 5.5 The Development of.