T3 Pro 1
T3 Pro 1
⚫ Prepared by:
– Mr. Lawrence Cha
⚫ Modified by:
– John Tang, rm. 1005.
1
Goals of this tutorial
2
Two types of Logic
3
Prolog in general
⚫ What is prolog ?
– An AI Programming Language
– Prolog – Logic programming (first order logic).
– Philosophy of programming ››› Goal Oriented.
– Declarative language (Execution is not in the step-by-step
manner).
⚫ Characteristics of prolog?
– Programming prolog is same as writing down anything you know
that is true, in the form of rules and facts.
– A.I. problems are usually `searching' problems, prolog is
inherently a depth-first search engine.
4
Features of Prolog
⚫ Emphasize knowledge
5
Compared with other language
6
Use of Prolog
7
Elementary concepts in prolog (Clause)
⚫ Clause:
– A prolog program consists of clauses, and each clause
terminates with a full stop.
8
Elementary concepts in prolog (Clause)
⚫ Terms:
– All data objects in Prolog. Terms included:
⚫ Constant: Denotes a known entity/object/thing. Must begin with a
lowercase letter, e.g. john, book.
⚫ Variable: Representing a unknown object. Must begin with an
uppercase letter or an underscore, e.g. Name, _type.
– ‘_’ is the anonymous variable. It means ‘don’t care’.
⚫ Compound Terms: Representing a structure, which is treated as single
object in the program. And they can pictured as tree with the root of
the tree is the functor (name of the term) and offspring of the root as
the components.
9
Compound Term(an example)
Date
For example:
?- date(D,M,1999) = date(12,october,Y).
D = 12 12 October 1999
M = october
Y = 1999? ; Continue to find
no.
10
Writing prolog programs
⚫ Prolog clauses are of three types:
– Facts - declare things that are always, unconditionally true.
11
Writing facts
⚫ Fact
– The general syntax is a predicate followed by a number
of arguments.
– Facts do not contain variables.
⚫ For example:
Predicate 3 Arguments
12
Writing rules
⚫ Rules: expresses an if-then relationship.
– The general syntax of a rule : a(...) :- b(...), c(...), ..., z(...).
– Meaning : a(...) is true if b(...), c(...), ..., z(...) are all true.
– The left side of the :- sign is called a head (conclusion).
– The right side of the :- sign is the body(condition), which is a
sequence of goals .
– The commas in the body can be read as the logical `and'.
– When there are more than 2 rules with the same head, they
have the meaning of logical ‘or’.
⚫ A question is the goal for the prolog program. Prolog will try to satisfy
the goals using its databases (knowledge bases). Notes that prolog
answers “no” doesn’t mean that answer is definitely fault. It means
that the system cannot answer this questions with its databases.
13
Illustration of a prolog program
14
Querying Prolog System
User Queries Prolog's
Answers
X = tom
Y = liz;
...
15
Another relation defined : grandparent
⚫ Answer:
– X = bob
– Y = pat
16
Another relation defined : grandchildren
– X = bob
– Y = pat?;
17
Another relation defined : common parent
18
Recursive definitions in prolog
⚫ Predecessor relation:
– predecessor (X, Z) :- parent (X, Z).
– predecessor (X, Z) :- parent (X, Y), parent (Y, Z).
– predecessor (X, Z) :- parent (X, Y1), parent (Y1, Y2), parent (Y2, Z). etc.
⚫ Any problem?
19
Recursive definitions in prolog(cont’d)
A recursive definition:
predecessor(X,Z):-parent(X,Z).
predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).
⚫ ?- predecessor (pam, X).
– X = bob;
– X = ann;
– X = pat;
– X = jim
The prompt | ?- is the top level Prolog prompt, indicating that it is ready to accept queries.
Loading program
Prolog programs are text files with the .pl extension. Assume that you have a program called myprog.pl, you
can load it by using one of the following methods:
| ?- [myprog].
or
| ?- consult(myprog).
If the program is successfully loaded (i.e. your program has no error), you'll get a message like this:
{consulting /tmp/myprog.pl...}
{/tmp/myprog.pl consulted, 0 msec 1424 bytes}
yes
Listing programs
To show all facts and rules that have been loaded so far, type
| ?- listing.
or you can give the name of the predicate as the argument
22