AI Lab 12-1
AI Lab 12-1
EXPERIMENT NO. 12
FAMILIARIZATION WITH PROLOG LANGUAGE
OBJECTIVE
Familiarization with Prolog language.
THEORY
Prolog is a programming language particularly well suited to logic and artificial intelligence
programming. "Prolog" in fact stands for "Programming in Logic." In this brief introduction we
will try to give you a little taste of Prolog without bogging you down with a great deal of
technical jargon. By the end of this section you should be able to use Prolog and write some little
programs that give you a feel for the language. Feel free to consult the books and Web sites on
Prolog mentioned later should you want to go further.
A person uses a computer programming language to direct a computer to perform desired tasks.
You might be familiar already with the names of some common programming languages, such as
C, Basic, Pascal, or Cobol.
Not all programming languages work the same way. In languages you might be familar with
already, such as C, the programmer commonly tells the computer that some letters or words are
to be variables, for example that the letter "X" is to be considered a variable that will stand for an
integer number (such as 1, 2, etc.). The C program might then involve a loop mechanism (a
repeating set of commands) in which the computer assigns different integer values to X every
time it goes through the loop. In fact a large part of the program could consist of variable
declarations, "iterative" loops, calculations of values, and assignments of values to the variables.
The programmer tells the programmer not only what to do, but how to do it.
A Prolog program is likely to look a little different than a typical C program. The programmer
tells the computer less the "how" than the "what." For example, in Prolog the programmer might
begin by telling the computer a bunch of "facts." The facts can be about characteristics of people
or things in the world ("Spot is frisky"), about relations of such things ("John is the father of
Susan"), and about "rules" pertaining to such facts ("Scott is the grandfather of Susan" is true if
"Scott is father the John" is true and "John is the father of Susan" is true).
The Prolog program could then be used to ask the computer about the facts already given and the
computer would be able to provide answers. Given the facts in the previous paragraph, if the
computer is asked "Is John the father of Susan?" it would reply "Yes." If asked, "Is John the
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
father of James" it would reply "No" because it was not given that fact or other facts from which
it could be deduced.
Prolog, like any other common programming language, will not be able to process ordinary
English sentences such as those given above; rather it requires the programmer to write
statements in a particular way . The programmer must know how to phrase Prolog sentences
properly. In a moment we will show you how to write some simple Prolog statements, so that
you can directly use the Prolog facility (PT-Prolog) within PT-Thinker.
But let's point out that you don't have to know Prolog in order to use its logical abilities - that is
where PT-Thinker can help you. PT-Thinker is a Prolog program that can take ordinary English
sentences and translate them into a form suitable for processing by Prolog. So you can see the
kind of logical inferences PT-Thinker can make by telling PT-Thinker facts and then seeing how
it deduces conclusions from those facts. As already mentioned, should you decide to do so you
can also use the Prolog facility within PT-Thinker to give the computer such facts and questions
phrased in the Prolog language.
How to Write Prolog Statements Let's start off with some examples of Prolog statements. Note
that Prolog is case sensitive, that is, capital letters are considered different than lower-case
letters, so don't just substitute an "A" for an "a" and assume it will mean the same thing.
Let's tell the computer some facts about a family we know. We'll put the Prolog statement after
the English statement.
Now let's talk about these statements. We can define a fact in the Prolog language by placing the
verb or adjective first and the relevant related nouns in parentheses. The period defines the end
of the statement. (Notice that we aren't using capital letters to start the names; capital letters or
terms starting with them we reserve for variables.)
We would type the above facts and then load them into Prolog. The exact command here may
vary with the specific Prolog program/version with which one is working. (In PT-Thinker
Prolog, one first types "assert" to enter the mode for fact loading, types in the statements, and
then enters F10 to load the facts.) Think of this loading process as teaching Prolog the above
facts. Prolog then responds with a "prompt," (such as "?-"), that is, the opportunity for the user to
ask questions.
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
Let's continue with examples, but this time with questions you can ask Prolog. Below are sample
English questions, their Prolog equivalent, and the answer from Prolog. Assume we have loaded
the above facts into Prolog already.
When we ask Prolog above about John being the father of Susan, we do not have to change the
form of the Prolog statement from that which we used to tell Prolog that John is the father of
Susan. This same statement may be used to tell the computer the fact initially or inquire about it
later. The computer knows it is an inquiry in the second circumstance because we have typed it
in response to the Prolog prompt, rather than loading it in as a fact.
Note that we use the variable letter "X" to ask the computer who is tired ("tired(X).") and who
Martha's husband is ("husband(X,martha)."). Another way to translate these Prolog statements is
as "Give me all those who are tired." and "Give me all those who are a husband of Martha." We
use the same strategy when asking about who eats pizza ("eats(X,pizza).". In such cases, Prolog's
response ends by asking us in effect "Do you want me to look for another?" In PT-Prolog, we
signal yes by hitting our space bar, and Prolog attempts to find another. When there are no more
to find, it responds "no." (In other Prolog facilities, one may press the "return" key rather than
the spacebar, and a semi-colon may end the line rather than a question asking us if we want to
find another.)
At this point you can go to the PT-Prolog facility within PT-Thinker, enter some facts, and ask
some questions. For a start you might put in facts about countries and capitals
("capital(london,england") and ask Prolog to give them back to you ("capital(X,france)") or tell
you the truth of statements ("capital(london,germany)"). Or type in some facts about particular
people and the foods they like or the colors they prefer.
How Prolog satisfies goals:
When we ask the computer a question, it looks back through the facts it has been given and tries
to find a match. Prolog will search the list of facts you gave it (entered in an earlier step) from
the top with the goal of "satisfying" the question. If a question such as "eats(john,pizza)" is
satisfied by finding it as a fact, Prolog will report "yes" or "true" (depending on the particular
Prolog system). If a sentence such as "eats(X,pizza)." is satisfied by finding a facts about
someone who eats pizza, Prolog responds with the name of the X who satisfies it. Note that the
computer only knows what you've told it (or loaded into it). If you ask it whether Cincinnati is a
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
city in Ohio, it looks to see if it has been given that information or other information from which
it can deduce this. If it cannot find any such information, it reports "no." It will report this even
though Cincinnati really is a city in Ohio. The computer has no way of knowing Cincinnati is an
Ohio city apart from having been given that fact earlier. So "no" here from the computer should
be interpreted more as "was not able to verify" or "did not find a match for this claim" rather than
"this statement is false in the real world."
Consider the following:
This statement, in Prolog, asks for Prolog to find an X such that it true both that X eats pizza and
that X eats oranges. The comma acts as an "and" here. Prolog starts at the top of the list of facts it
has been given and tries to satisfy the first part, and it finds that John eats pizza. It then tries to
satisfy the second part with john for X (X is instantiated to john), starting at the top of the list,
since this is a new goal. But it fails to find that John eats oranges. (Recall that we told it that John
eats pizza but not that John eats oranges.) So it backtracks to where it previously found that John
eats pizza and tries to resatisfy the statement with a different X. It finds that Susan eats pizza (X
is instantiated to susan now), and so it then tries to satisfy "susan eats oranges" by starting at the
top of the list. It finds this as a fact, so it then gives the response of "susan."
Due to the way Prolog satisfies goals, sometimes Prolog will engage in a search that starts at the
top of the list (if it takes it to be a new goal) and other times backtrack to where it last satisfied
the goal (if it is trying to resatisfy a goal). This needs to be taken into account when designing a
complex program; sometimes Prolog can engage in too much backtracking in attempting to
resatisfy goals, and in this case a "cut" ("!") can be introduced into the statement to prevent
backtracking beyond the point of the cut. To find out more about backtracking and the cut please
consult a Prolog text or tutorial.
Rules:
Now that we understand the structure of simple Prolog statements and queries, we can
investigate some more complicated statements. We can give Prolog some facts about
conditionals, that is, what happens if something else is the case.
A good way to look at Prolog's handling of such statements is the following. To Prolog, the first
statement is interpreted as a rule to the effect that "If "husband (john, martha)" succeeds, then
"wife (martha, john)" succeeds. The second statement is "converses (john, X)" succeeds if
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
"fancies (X, baseball)" succeeds and "smart (X)" succeeds. The ":-" symbols tell Prolog how to
read the rule.
As we've said, in Prolog we give the computer facts and rules about facts and then we can ask it
questions. All these facts and rules we give it are stored in an internal database, and we can save
this database in the form of a file that we can use in a later session. If we don't save it, the
computer will not "remember" it if we turn off the computer and run Prolog later.
Prolog elements:
We've already talked about variables, which are letters or phrases that can assume any value.
Conventions vary, but traditionally variables are capital letters, words starting with capital
letters, words starting with an underscore (_), or an underscore itself (called the "anonymous"
variable).
Prolog recognizes ordinary words (beginning with a lower case letter, with no spaces), or words
surrounded by single quotation marks (these can include spaces and start with capital letters). All
these are taken as constants, not variables.
Prolog can recognize integers (such as -244, 0, 3, etc.), and some versions can recognize floating
point numbers (such as 3.45, -22.005, etc.).
Another type of data recognized by Prolog are structures, such as "state(Illinois)" and
"shirt(brand(izod))." We've already seen these. And Prolog can handle lists. You give Prolog a
list of elements by enclosing it in brackets, as in
[baseball,football,basketball,soccer].
and
[a,b,c].
Prolog can handle a list by treating it as having a head and a tail. The head is the first element in
the list, and the tail is everything after the head. So in the above example of the list [a,b,c], the
head is "a" and the tail is the remaining list "b,c." The division between head and tail can be
indicated by using "|" as in the case of a list with a head of "a" and a tail of the rest of the list:
[a|b,c,d,e]. Prolog also allows one to signify the head and the tail of a list using variables and the
symbol "|" as in "[X|Y]." Here the X denotes the head of the list and the Y denotes the tail of the
list. Or consider the use of "[a|T]" to indicate all of the lists with "a" as the head and anything as
the tail; "[H|b]" indicates all of the lists with anything as the head and "b" as the tail.
In this way we can ask the computer to search for lists that match a particular pattern, and it will
find lists that instantiate that pattern. Here are some examples:
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
The predefined predicate "append" can be used in Prolog to join lists together. For example,
"append([1,2,3],[4,5,6],X)." will cause Prolog to reply "X = [1,2,3,4,5,6]".
Arithmetic:
Prolog can handle arithmetic in a pretty normal fashion. Note the following Prolog symbols:
= indicates "equals"
\= indicates "is not equal"
< indicates "is less than"
> indicates "is greater than"
=< indicates "is less than or equal to"
>= indicates "is greater than or equal to"
+ indicates "plus"
- indicates "minus"
/ indicates "divided by"
* indicates "multiplied by"
To do arithmetic, Prolog accepts notation in a variety of orders, the easiest to follow being the
traditional "infix" manner such as appears in the following:
5*6
10/2
2+3
7-5
If you leave out clarifying parentheses in complex expressions, Prolog solves them the way we
learned in elementary school, with multiplication evaluated before addition, etc. Note that to get
Prolog to actually evaluate each of the above expressions to an answer, you have to use "is."
A is 5*6
B is 10/2
C is 2+3
D is 7-5
Department of Information and Communication Engineering
BS Cyber Security and Digital Forensics
Introduction to Artificial Intelligence
Lab Exercise: