# Class 24: Prolog

Back to Logic Programming, Continued. On to Prolog, Concluded.

Held Friday, March 19

Summary

Contents

Handouts

Notes

• As you might guess, I was quite disappointed that so few of you had done the required reading. We will start today's class with a quiz to ensure that you did it for today.
• We all suffer if you don't do the reading: we can cover less material, because we have to go more slowly.
• Because of the problems with the MathLAN, the exam is due the Wednesday after break (Wednesday, April 7).
• Prolog can be found in /usr/local/bin/bp.
• We seem to have an old version.
• Instructions for a new version can be found at http://www.binnetcorp.com/BinProlog/user.html
• Rules need to be stored in a file with a .pro suffice.
• You load and compile the file with [filename].
• We'll spend some time playing with prolog today.

## An Introduction to Prolog

• Prolog was developed in the early 1970's to demonstrate that it is possible to ``program'' in predicate logic. The paper that introduced prolog was titled something like ``Logic + Control = Program''.
• When people speak of ``logic programming'' they often mean ``Prolog programming''.
• This can lead to some interesting misunderstandings. I have friends who do ``equational-logic programming'', programming based on the logic of equations. However, many people think that they do ``Prolog + equations''.
• Prolog includes some nonlogical components (we'll discuss some of them), some of which are there for the sake of efficiency.
• Many Prolog programmers take advantage of knowledge of these nonlogical components.
• An interaction with a Prolog system consists of
• Specifications of basic facts about objects and relationships (``The Velvet Underground was an influential band.'', ``Maureen Tucker was a member of the Velvet Underground.'')
• Specifications of rules about objects and relationships (``All members of influential bands are influential.'')
• Ask questions about objects and relationships (``Is Lou Reed influential?'' ``Is Barry Manilow influential?'' ``Are there any influential musicians?'')
• As this suggests, Prolog is particularly good at working with so-called atomic or symbolic values. That is, values whose meaning is determined by the program.

### The Syntax of Prolog

• Atoms, predicates, and functions begin with lowercase letters. For example, lou_reed, influential, ....
• variables begin with uppercase letters or underscore. For example, Musician.
• the arguments to a predicate are parenthesized and separated by commas. influencedBy(Musician,lou_reed).
• a period indicates the end of a fact or rule.
• lists are surrounded by brackets; items in a list are separated by commas; the vertical bar acts like cons (more or less).
• [] -- the empty list
• [a,b,c] -- a list of three elements
• [a|X] -- a list whose first element is a and whose remainder is the list X.
• [a,b|X] -- a list whose first two elements are a and b and whose remainder is the list X.
• Prolog predicate definitions have the form
P :- Q1,Q2,...,Qn.

where the P and Q's are all predicate applications.
• For example,
influential(X) :- influencedBy(X,Y).

• The meaning of the general form is ``for (all the variables in P), if there exist values for (all the other variables in the Q's) such that all the Q's hold, then P holds''.
• Basic facts (e.g., coolDude(louReed).) do not require the :-.
• A Prolog program is a collection of definitions. Input is a question, such as ``does this predicate hold on these values?'' or ``for what values does this predicate hold?''
• Prolog is often used for databases as the basic facts are effectively database entries and the predicate definitions are ways to reason with the basic entries.

### An Example: System Diagnosis

• As an example, let's consider a simple Prolog system for system diagnosis in the MathLAN.
• We can begin with some simple dependencies for when one system directly depends on another.
dependsOn(church,newton).
dependsOn(church,euler).
dependsOn(weyl,newton).
dependsOn(newton,scienceRouter).
dependsOn(scienceRouter,grinnellRouter).
dependsOn(euler,scienceRouter).
dependsOn(grinnellRouter,hunkOcable).
dependsOn(hunkOcable,iowaState).

• We can add a recursive dependency for "possible cause of failure". pcof(X,Y) holds if Y can cause X to fail.
pcof(X,Y) :- dependsOn(X,Y).
pcof(X,Y) :- dependsOn(X,Z), pcof(Z,Y).

?- [mathlan].

• Can Iowa State cause church to fail?
?- pcof(church,iowaState).
yes

• Can church cause Iowa State to fail?
?- pcof(iowaState,church).
no

• What can cause newton to fail? Note that you type a semicolon to see more answers.
?- pcof(newton,X).
X=scienceRouter;
X=grinnellRouter;
X=hunkOcable;
X=iowaState;
no

• What can the science router cause to fail? Note that we get some duplication. Can you tell why?
?- pcof(X,scienceRouter).
X=newton;
X=euler;
X=church;
X=church;
X=weyl;

### Implementation

• Let's return to an old question: how might we implement a langauge like Prolog? That is, how do we use predicate definitions to answer questions?
• One basic strategy:
Keep track of collections of intermediate "goals"
(predicates whose truth/falsity must be determined)
Repeatedly
Pick a goal
(usually the first in the collection)
Find a rule that might help solve that goal
(usually the first rule whose lhs matches the goal)
Determine replacements necessary to match the rule to the goal
(variables to be replaced by atoms)
Replace the goal in the set with the corresponding preconditions
(the stuff to the right of :-, after the substition is made).
If you run out of goals, the predicate holds.
If you can't solve a goal, "backtrack" to try another rule.

• You can draw this as a search tree. Normal prolog implementations traverse this tree in a left-to-right depth-first search.
• Note that when matching a goal that includes variables with a definition that uses constants, we instatiate the variables to the constants.
• Similarly, when the goal includes constants and the rule includes variables, instatiate the variables in the definition with the constants before replacing with the corresponding right hand side.
• Prolog stops when it finds the first proof (and set of instatiations).
• In most implementations, you can make it continue by typing a semicolon.
• There are also techniques for automatically finding all results.

### Working with Lists

• The code for this section can be found in lists.pro.
• Recall that Prolog uses the following syntax for lists: lists are surrounded by brackets; items in a list are separated by commas; the vertical bar acts like cons (more or less).
• [] -- the empty list
• [a,b,c] -- a list of three elements
• [a|X] -- a list whose first element is a and whose remainder is the list X.
• [a,b|X] -- a list whose first two elements are a and b and whose remainder is the list X.
• We can, of course, use Prolog to do things like "real computaton", such as manipulating lists.
• In fact, we can even use patterns to do it.
• The difficulty is that we have to do everything as a predicate.
• Consider head. Traditionally, we think of head as removing the first element of a list. However, in Prolog, we need to think of it as determining whether something is at the head of the list (and then use to proof techniques to extract the head of the list).
• Here's one definition

• You might read this as "X is the head of L if L's first element is Y and X is equal to Y."
• However, this is somewhat inelegant. It is much nicer to use some patterns.

• You might read this as "X is the head of the list beginning with Y if X equals Y."
• In fact, we can go even further.

• You might read this as "X is the head of the list that begins with X".
• Note that these are more sophisticated than the patterns of Haskell. In particular, we're able to reuse variables
• We can define tail similarly.
tail(XS,[X|XS]).

History

• Created Tuesday, January 19, 1999 as a blank outline.
• Filled in the details on Friday, March 19, 1999. Many were taken from a previous session of 302.
• Removed some sections (uncovered) on Monday, April 5, 1999. Moved those sections to the next outline.

Back to Logic Programming, Continued. On to Prolog, Concluded.

Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.