Вы находитесь на странице: 1из 41

PROLOG

Andrian Rakhmatsyah
Institut Teknologi Telkom

PROLOG is a programming system in which


logic is used as a programming language
as well as a framework for program
interpretation
Generally, a PROLOG program is desciption
of a world (that is a collection of object
that are related to each other in some
way), written in the language of PROLOG.

PROLOG can be used either as a


Software Tool for development of intelligent
systems (such as expert systems and robot
control systems) or

Simply as a general-purpose programming


language with a powerful mechanism for
problem solving.

Basically, the system works as follows :


Users submits a description of a problem
written in PROLOG language.

The PROLOG interpreter then applies logical


reasoning to find answers for the problem

Conventional Programming
The programmer instruct the machine HOW to solve a problem
by performing a given sequence of actions
The machine carries out the instructions in a specified order

Logic Programming

The programmer defines WHAT the problem is, using the


language of logic;
The systems applies logical deduction to find answers for the
problem

PROLOG PROGRAMS

In a PROLOG program:
A FACT represents a unit of information that is
assumed to be true;
A RULE represents a conditional assertion
A VARIABLE represents an unspesified element of a
world
A CONSTANT represents a spesific elements of that
world
A PREDICATE represents a relatioship between
element or a property of a class of elements.
A PROCEDURE is a group of clauses having the
same head predicate.

In general, PROLOG have the following syntax rule (for


most PROLOG versions) :
User-defined symbols begin with a letter which may be
followed by zero or more (lower- or uppercase) letters,
digits, or underscores

Variables, and only variables, begin with a capital letter


or an underscore. Anonymous variables are allowed
and are represented by single underscores; each sole
occurrence of the underscore symbol represents a
different variable (even in same clause). Clauses are
special formulas and composed of a nmber of
elementary formulas that are called atomic formulas

likes(sue,husband(ann)),

husband is not a constant or a variable, it is a


function symbol.
A function symbols represent a way of reffering to an
element indirectly, via other elements.
husband(ann) refers to an element, not by its
name, but by its relation to (represented by ) ann.
Syntactically, a term never stands alone, but always within
some atomic formula.

A Term is either a variable or a constant or an


expression of the form f (T1,T2,,Tn) where f is a
function symbol and T1, T2, , Tn (N > 0) are terms.
The symbol f is called the functor, n the arity, and are
T1, T2, , Tn arguments of the terms.

An atomic formula is an expression of the form p(T1,


T2, , Tn), where p is predicate symbol and T1, T2,
, Tn are terms. If n = 0 that is, no argument exist,
then the parentheses are omitted

A PROLOG program is a finite set of clauses of the


form :
A :- B1, , Bn
Where n 0 and A and the Bis are atomic formulas. The
above clause is read
A if B1 and and Bn.
The atomic formula A is called the head of the clause
and (B1, Bn) is called the body of the clause. If n =
0, then the clause, with the symbol :- omitted, is
called a unit clause

Example 1. The world of Ann & Sue is described


below
Ann likes every toy she plays with.
Doll is a toy.
Snoopy is a toy.
Ann plays with Snoopy.
Sue likes everything Ann likes

The above description is translated into


PROLOG program.
likes(ann,X) :- toy(X),
plays(ann, X).
toy(doll).
toy(snoopy).
plays(ann,snoopy).
likes(sue,Y):- likes(ann, Y).

Queries

Given the program in example 1, one may seek


information by asking questions such as
What does Sue like ?
In PROLOG, the above query is written as follows :
?- likes(sue,X).
PROLOG, applying logical deduction to find an
answer for the query. PROLOGs answer will be :
X = snoopy

A query has a the form ?- A1,,An, where each Ai


is an atomic formula. In This query, (A1,,An)
is called a goal, and each Ai is subgoal.
If n = 1, then the goal is called a simple goal.
If n = 0, we have an empty goal,which is denoted
by .

WORKING WITH AMZI


PROLOG

Amzi! Listener (ALIS)


a Prolog interpreter
The listener directly executes source
code, which is stored in a plain text file
(PRO file), or typed in directly at the ?prompt
Amzi! Standalone Executable Runtime
(ARUN)
The Amzi! runtime executes a Prolog
load module (XPL file)

Enter and edit your source code (using File/New to


create a new file)
Consult your source code file into the listener (using
Listener/Consult)
Issue Prolog queries to test your code and use the
listener debugger, as needed (using Listener/Debug
on)
Respond to runtime errors by either continuing or
failing
Modify your source code to fix the problems
Reconsult your source code file into the listener (using
Listener/Reconsult, which will automatically save all
modified source files/windows)
Goto step 3 until your module works

DEBUGGER
The Amzi! debugger is used to find and identify
errors.
It works on interpreted source code.
The debugger can only be invoked from, and
used within, the listener. In order to step
through code, you need to consult the source
code form of the module

INTERMIXING INTERPRETED AND COMPILED CODE

The Amzi! listener differs from other Prolog listeners because,


can consult both source code files (*.PRO) and compiled object
code (*.PLM) files. This allows, to keep code under
development in source form, and debugged code in compiled
form.
The Listener is the heart of the Amzi! development tools. It is
where you develop, test and debug your Prolog code. The text
file ENV.PRO is consulted automatically for you when the
listener starts up, which initializes various flags and options.
ENV.PRO in turn loads UTIL.PLM which contains a number
of useful predicates like member and append (see UTIL.PRO
for a complete list).

Amzi! Compiler (ACMP)


Compile source files
(*.PRO) into object
files (*.PLM),
Compiled code runs at
least 10 times faster
than interpreted code

Amzi! Linker (ALNK)


Once an application has been debugged, the
object files (PLM) are linked into a single
executable load module (XPL file) that can be
embedded in a C/C++, Visual Basic or other
program, or run as a standalone program
The linker automatically links in a copy of the
standard Amzi! library AMZILIB.PLM into
each XPL file

PREDEF. FUNC. &


PREDICATES

PROLOG allows arithmetic terms and formulas


to be written in infix order, instead of prefix
order.
Example.
+(1,*(2,3)) is equivalent to 1 + 2 * 3
is(X,+(Y,1)is equivalent to X is Y + 1

?- 3 = 1 + 2
no
Comment : 3 is not the same terms as 1 + 2
?- 3 is 1 + 2
yes
Comment : 3 is the value of 1 + 2.
The Predicate symbols = does not represent the
normal equality but it represent a special
relation in PROLOG called unifiability

Two Term T1 and T2 are unifiable (written T1 = T2) if they


can be made identical by a substitution of their
variables with appropriate terms
Ex.
?- X + 2 = 1 + Y
X = 1
Y = 2

The Predicate = unifies its


two arguments with no
arithmetic computation involved
The predicate is unifies its
first argument with the
(computed) value of its second
argument

?- 4-1
no
?- 4-1
yes
?- X+2
X = 1
?- X +
1+2
no

is 1+2
=:= 1+2
= 1+2
2 =:=

Problem VS Solution

Translate the following sentences into a PROLOG program :


Everyone who teaches a computing unit is smart.
John teaches the unit MA1
Johns wife teaches the unit SA1
MA1 is a mathematics unit
SA1 is a computing unit

From the above PROLOG program, identify the FACT,


RULES, TERMs, and ATOMIC FORMULA. Also list VARIABLEs,
CONSTANT, FUNCTIONS, and PREDICATEs.
Load the program to a PROLOG system and enter a query to
ask if ANYONE IS SMART. What is the logical meaning of the
answer

smart(X) :teaches(X,Y),computing(Y).
teaches(john,ma1).
teaches(wife(john),sa1).
mathematics(ma1).
computing(sa1).

FACTS
teaches(john,ma1).
teaches(wife(john),sa1).
mathematics(ma1).
computing(sa1).
RULE
smart(X) :- teaches(X,Y),computing(Y).

ATOMIC FORMULAs
smart(X), teaches(X,Y), computing(Y).
TERM
Variabels : X, Y
Constants : john, ma1, sa1
Function : wife(john)
function symbol :
wife
PREDICATEs: smart, teaches, computing

To ask if there is anyone smart ?

?- smart(X)
X = wife(john).
no

Consider the following English version

Every mother likes her child if her child is good


Every mother is woman
Ann is woman
Anns husband is good.

Translate the above sentences into two different PROLOG


programs : one contains function symbols and the other does
not.
Load the program to a PROLOG system and enter a query to
ask if there is any woman who likes someones husband.
What is the logical meaning of the answer

Program 1
likes(mother(X),X) :- good(X).
woman(mother(X)).
woman(ann).
good(husband(ann)).
Program 2
likes(X,Y) :- mother(X,Y), good(Y).
woman(Y) :- mother(X,Y).
woman(ann).
good(X) :- husband(X,ann)

the program 1 is more expensive, because its


functions allow reference to a large number of
entities such as Anns husband, Anns mother, and
Anns mother-in-law, whereas in program 2, the
only entity that can be displayed is ann .
Is there any woman who likes someones husband ?
?- woman(X), likes(X,husband(Y)).
X = mother(husband(ann)).
X = ann

?- woman(X), husband(Y,Z), likes(X,Y).


no
because the system is unable to find anyones husband
to do this we must introduce new constant, say ann_husband,
& ann_mother_in_law
husband(ann_husband,ann).
mother(ann_mother_in_law,ann_husband)
?- woman(X), husband(Y,Z), likes(X,Y).
X = ann_mother_in_law
Y = ann_husband
Z = ann

The unification process of PROLOG assumes every function is


one-to-one, that is f(x) = f(y) only if x = y. Consider the
following pairs of expressions and determine if they are
unifiable. If so, Find appropriate variable substitution !
a.
b.
c.
d.
e.

loves(X,husband(Y));
loves(X,husband(X));
mother(john);
min(log(X),Y);
X + 1;

loves(mother(Z),Z);
loves(mother(Z),Z);
mother(sue);
min(U,exp(V)).
Y + 2;

Вам также может понравиться