Skip to main content
main-content

Über dieses Buch

We have added new material to Chapter 3 to give an account of up-to-date programming techniques using accumulators and difference structures. Chapter 8 contains some new information on syntax errors. Operator precedences are now compatible with the most widely-used implementations. We have made further reorganisations and improvements in presentation, and have corrected a number of minor errors. We thank the many people who brought typographical errors in the previous edition to our attention, and we thank A.R.C. for careful proofreading. Cambridge, England W.F.C. January,1987 C.S.M. PREFACE TO THE SECOND EDITION (1984) Since the first publishing of Programming in Prolog in 1981, Prolog has continued to attract an unexpectedly great deal of interest in the computer science community and is now seen as a potential basis for an important new generation of programming languages and systems. We hope that Programming in Prolog has partially satisfied the increasing need for an easy, yet comprehensive introduction to the language as a tool for practical programming. In this second edition we have taken the opportunity to improve the presentation and to correct various minor errors in the original. We thank the many people who have given us suggestions for corrections and improvement. Cambridge, England W.F.C.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Tutorial Introduction

Abstract
Prolog is a computer programming language that is used for solving problems that involve objects and the relationships between objects. In this chapter we shall show the essential elements of the language in real programs, but without becoming diverted by details, formal rules, and exceptions. At this point, we are not trying to be complete or precise. We want to bring you quickly to the point where you can write useful programs, so to do that we must concentrate on the basics: facts, questions, variables, conjunctions, and rules. Other features of Prolog, such as lists and recursion, will be treated in later chapters.
William F. Clocksin, Christopher S. Mellish

Chapter 2. A Closer Look

Abstract
In this chapter we provide a more complete discussion of the parts of Prolog that were introduced in the previous chapter. Prolog provides ways to structure data as well as ways to structure the order in which attempts are made to satisfy goals. Structuring data involves knowing the syntax by which we can denote data. Structuring the order in which goals are solved involves knowing about backtracking.
William F. Clocksin, Christopher S. Mellish

Chapter 3. Using Data Structures

Abstract
The Oxford English Dictionary defines the word “recursion” in the following way: RECURSION. [Now rare or obs. 1626]. A backward movement, return. This definition is cryptic and perhaps outdated. Recursion is now a very popular and powerful technique in the world of non-numerical programming. It is used in two ways. It can be used to describe structures that have other structures as components. It can also be used to describe programs that need to satisfy a copy of themselves before they themselves can succeed. Sometimes, beginners view recursion with some suspicion, because, how is it possible for some relationship to be defined in terms of itself? In Prolog, recursion is the normal and natural way of viewing data structures and programs. We hope that the theme of this chapter, recursion, will be made explicit in a comfortable and unobtrusive way.
William F. Clocksin, Christopher S. Mellish

Chapter 4. Backtracking and the “Cut”

Abstract
Let us summarise what we learned in Chapters 1 and 2 about what can happen to a goal:
1.
An attempt can be made to satisfy a goal. When we satisfy a goal, we search the database from the top. Two things can happen:
(a)
A matching fact (or rule head) can be found. In this case, we say the goal has been matched. We mark the place in the database, and instantiate any previously uninstantiated variables that have matched. If we matched against a rule, we shall first have to attempt to satisfy the subgoals introduced by the rule. If the goal succeeds, we then attempt to satisfy the next goal. In our diagrams, this is the goal in the next box below the arrow. If the original goal appears in a conjunction, this will be the goal to its right in the program.
 
(b)
No matching fact (or rule head) can be found. In this case, we say the goal has failed. We then attempt to re-satisfy the goal in the box above the arrowhead. If the original goal appears in a conjunction, then this will be the goal on its left in the program.
 
 
William F. Clocksin, Christopher S. Mellish

Chapter 5. Input and Output

Abstract
Thus far, the only means we have seen of providing information to a Prolog program has been by asking questions of the Prolog system. Also, the only method of finding out what a variable stands for at some point in the satisfaction of a goal has been by asking a question in such a way that Prolog will print out the answer in the “X = answer” form. Much of the time, such direct interaction with questions is all that is required to ensure that a program is working properly. However, for many occasions it is useful to write a Prolog program that initiates a conversation with you by itself.
William F. Clocksin, Christopher S. Mellish

Chapter 6. Built-in Predicates

Abstract
In this chapter we introduce some of the built-in predicates that a Prolog system might provide. What do we mean when we say that a predicate is built-in? We mean that the predicate’s definition is provided in advance by the Prolog system, instead of by your own clauses. Built-in predicates may provide facilities that cannot be obtained by definitions in pure Prolog. Or they may provide convenient facilities just to save each programmer from having to define them himself. We have already encountered some built-in predicates: the predicates for reading and writing discussed in Chapter 5. Also, the “cut” could be regarded as a built-in predicate.
William F. Clocksin, Christopher S. Mellish

Chapter 7. More Example Programs

Abstract
Each section of this chapter deals with a particular application of Prolog programming. We suggest that you read all of the sections in this chapter. Do not be concerned if you do not understand the purpose of a program because you are not acquainted with the particular application. For example, only those readers who have been introduced to Calculus will appreciate the value of symbolic differentiation. Read it anyway, because the program for finding symbolic derivatives demonstrates how to use pattern matching to transform one kind of structure (an arithmetic expression) into another one. What is important is to gain an understanding of programming techniques available to the Prolog programmer, regardless of the particular application.
William F. Clocksin, Christopher S. Mellish

Chapter 8. Debugging Prolog Programs

Abstract
By this point you will have used and modified many of the example programs described earlier, and you will have written programs of your own. It is now relevant to consider what to do when your program does not behave as intended. Such problems with programs are known as “bugs”, and the process of removing bugs from programs is known as “debugging”. We believe that a convenient approach to programming is what could be described as “preventative programming”. To paraphrase an old proverb, “an ounce of careful programming is worth a pound of debugging”. In this chapter we shall attempt to describe some techniques for debugging, but we shall start with a discussion of how to try to prevent bugs from infesting your programs. We realise that such a problem is unsolved in general, but we simply wish to convey some informal techniques that have helped other Prolog programmers.
William F. Clocksin, Christopher S. Mellish

Chapter 9. Using Prolog Grammar Rules

Abstract
Sentences in a language such as English are much more than just arbitrary sequences of words. We cannot string together any set of words and make a reasonable sentence. At the very least, the result must conform to what we consider to be grammatical.
William F. Clocksin, Christopher S. Mellish

Chapter 10. The Relation of Prolog to Logic

Abstract
The programming language Prolog was invented by Alain Colmerauer and his associates around 1970. It was a first attempt at the design of a practical programming language that would enable a programmer to specify his tasks in logic, instead of in terms of conventional programming constructs about what the machine should do when. This motivation explains the name of the programming language, for “Prolog” stands for Programming in Logic.
William F. Clocksin, Christopher S. Mellish

Chapter 11. Projects in Prolog

Abstract
This chapter contains a list of projects that you may wish to undertake in order to exercise your programming ability. Some of the projects are easy, but some may be appropriate as “term projects” as a part of a course in Prolog. The easier projects should be used to supplement the exercises in the previous chapters. The projects are in no particular order, although those in Section 11.2 are more open ended and ambitious, and will require some knowledge or background reading in various areas of artificial intelligence and computer science. A few of the projects assume knowledge about some particular field of study, so if you are not a mathematical physicist, do not feel discouraged if you cannot write a program to differentiate three dimensional vector fields.
William F. Clocksin, Christopher S. Mellish

Backmatter

Weitere Informationen