Bab 4 Prolog

41
PROLOG Andrian Rakhmatsyah Institut T eknologi T elkom

Transcript of Bab 4 Prolog

Page 1: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 1/41

PROLOG

Andrian RakhmatsyahInstitut Teknologi Telkom

Page 2: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 2/41

PROLOG is a programming system in whichlogic is used as a programming languageas well as a framework for programinterpretation

Generally, a PROLOG program is desciption

of a world (that is a collection of objectthat are related to each other in some

way), written in the language of PROLOG.

Page 3: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 3/41

PROLOG can be used either as a

Software Tool for development of intelligent

systems (such as expert systems and robotcontrol systems) or

Simply as a general-purpose programming

language with a powerful mechanism for

problem solving.

Page 4: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 4/41

Basically, the system works as follows : Users submits a description of a problem

 written in PROLOG language.

 The PROLOG interpreter then applies logicalreasoning to find answers for the problem

Page 5: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 5/41

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 thelanguage of logic;

•  The systems applies logical deduction to find answers for theproblem

Page 6: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 6/41

PROLOG PROGRAMS

Page 7: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 7/41

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.

Page 8: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 8/41

In general, PROLOG have the following syntax rule (formost 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 letteror an underscore. Anonymous variables are allowed

and are represented by single underscores; each soleoccurrence of the underscore symbol represents adifferent variable (even in same clause). Clauses arespecial formulas and composed of a nmber ofelementary formulas that are called atomic formulas

Page 9: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 9/41

l i kes( 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 .

Page 10: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 10/41

 A Term is either a variable or a constant or anexpression of the form f (T1,T2,…,Tn) where f is afunction 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

Page 11: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 11/41

 A PROLOG program is a finite set of clauses of theform :

A : - B1, …, Bn

 Where n ≥ 0 and A and the Bi’s are atomic formulas. Theabove clause is read

“ A if B1 and … and Bn“.

 The atomic formula A is called the head of the clauseand (B1,… Bn) is called the body of the clause. If n =0, then the clause, with the symbol “:-“ omitted, iscalled a unit clause

Page 12: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 12/41

Example 1. The world of Ann & Sue is describedbelow 

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

Page 13: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 13/41

 The above description is translated intoPROLOG program.

l i kes( ann, X) : - t oy( X) ,pl ays( ann, X) .

t oy( dol l ) .

t oy( snoopy) .pl ays( ann, snoopy) .l i kes( sue, Y) : - l i kes( ann, Y) .

Page 14: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 14/41

Queries

Page 15: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 15/41

Given the program in example 1, one may seekinformation by asking questions such as

“What does Sue like ?”

In PROLOG, the above query is written as follows :?- l i kes( sue, X) .

PROLOG, applying logical deduction to find ananswer for the query. PROLOG’s answer will be :

X = snoopy

Page 16: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 16/41

 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 □.

Page 17: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 17/41

WORKING WITH AMZIPROLOG

Page 18: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 18/41

 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)

Page 19: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 19/41

Enter and edit your source code (using File/New tocreate a new file)

Consult your source code file into the listener (usingListener/Consult)

Issue Prolog queries to test your code and use thelistener debugger, as needed (using Listener/Debugon)

Respond to runtime errors by either continuing orfailing

Modify your source code to fix the problems Reconsult your source code file into the listener (using

Listener/Reconsult, which will automatically save allmodified source files/windows)

Goto step 3 until your module works

Page 20: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 20/41

DEBUGGER 

 The Amzi! debugger is used to find and identifyerrors.

It works on interpreted source code.  The debugger can only be invoked from, and

used within, the listener. In order to stepthrough code, you need to consult the sourcecode form of the module

Page 21: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 21/41

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 underdevelopment in source form, and debugged code in compiledform.

 The Listener is the heart of the Amzi! development tools. It is where you develop, test and debug your Prolog code. The textfile ENV.PRO is consulted automatically for you when thelistener starts up, which initializes various flags and options.

ENV.PRO in turn loads UTIL.PLM which contains a numberof useful predicates like member and append (see UTIL.PROfor a complete list).

Page 22: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 22/41

 Amzi! Compiler (ACMP)

Compile source files

(*.PRO) into objectfiles (*.PLM),

Compiled code runs at

least 10 times fasterthan interpreted code

Page 23: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 23/41

 Amzi! Linker (ALNK) Once an application has been debugged, the

object files (PLM) are linked into a singleexecutable load module (XPL file) that can beembedded in a C/C++, Visual Basic or otherprogram, or run as a standalone program

 The linker automatically links in a copy of thestandard Amzi! library AMZILIB.PLM intoeach XPL file

Page 24: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 24/41

Page 25: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 25/41

PREDEF. FUNC. &

PREDICATES

Page 26: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 26/41

PROLOG allows arithmetic terms and formulasto be written in infix order, instead of prefix

order.

Example.+( 1, *( 2, 3) ) is equivalent to 1 + 2 * 3is( X, +( Y, 1) is equivalent to X is Y + 1

Page 27: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 27/41

?- 3 = 1 + 2noComment : 3 is not the same terms as 1 + 2

?- 3 i s 1 + 2yesComment : 3 is the value of 1 + 2.

 The Predicate symbols “=“ does not represent thenormal “equality” but it represent a specialrelation in PROLOG called “unifiability”

Page 28: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 28/41

 Two Term T1 and T2 are unifiable (written T1 = T2 ) if theycan be made identical by a substitution of their

 variables with appropriate terms

Ex.

?- X + 2 = 1 + Y

X = 1

 Y = 2

Page 29: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 29/41

 The Predicate “ = “ unifies itstwo arguments with noarithmetic computation involved

 The predicate “ is “ unifies itsfirst argument with the(computed) value of its secondargument

?- 4- 1 i s 1+2no?- 4- 1 =: = 1+2yes?- X+2 = 1+2X = 1?- X + 2 =: =1+2

no

Page 30: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 30/41

Problem VS Solution

Page 31: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 31/41

Translate the following sentences into a PROLOG program :Everyone who teaches a computing unit is smart.John teaches the unit MA1John’s wife teaches the unit SA1MA1 is a mathematics unitSA1 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 toask if ANYONE IS SMART. What is the logical meaning of theanswer

Page 32: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 32/41

smar t ( X) : -t eaches( X, Y) , comput i ng( Y) .

t eaches( j ohn, ma1) .t eaches( wi f e( j ohn) , sa1) .mat hemat i cs( ma1) .comput i ng( sa1) .

Page 33: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 33/41

FACTS teaches(john,ma1). teaches(wife(john),sa1). mathematics(ma1). computing(sa1).

RULE smart(X) :- teaches(X,Y),computing(Y).

Page 34: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 34/41

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

Page 35: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 35/41

To ask if there is anyone smart ?

?- smar t ( X)X = wi f e( j ohn) .

no

Page 36: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 36/41

Consider the following English version

Every mother likes her child if her child is goodEvery mother is woman

Ann is womanAnn’s husband is good.

Translate the above sentences into two different PROLOGprograms : one contains function symbols and the other doesnot.

Load the program to a PROLOG system and enter a query toask if there is any woman who likes someone’s husband.What is the logical meaning of the answer

Page 37: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 37/41

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)

Page 38: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 38/41

the program 1 is more expensive, because itsfunctions allow reference to a large number ofentities such as Ann’s husband, Ann’s mother, and

Ann’s mother-in-law, whereas in program 2, theonly entity that can be displayed is ann .

Is there any woman who likes someone’s husband ?

?- woman(X), likes(X,husband(Y)).X = mother(husband(ann)).

X = ann

Page 39: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 39/41

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

because the system is unable to find anyone’s husband

to do this we must introduce new constant, say ann_husband,& ann_mother_in_law

husband( ann_husband, ann) .mot her ( ann_mot her _i n_l aw, ann_husband)

?- woman(X), husband(Y,Z), likes(X,Y).X = ann_mot her _i n_l aw

 Y = ann_husbandZ = ann

Page 40: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 40/41

The unification process of PROLOG assumes every function isone-to-one, that is f(x) = f(y) only if x = y. Consider thefollowing pairs of expressions and determine if they are

unifiable. If so, Find appropriate variable substitution !

a. loves(X,husband(Y)); loves(mother(Z),Z);

 b. loves(X,husband(X)); loves(mother(Z),Z);

c. mother(john); mother(sue);

d. min(log(X),Y); min(U,exp(V)).

e. X + 1; Y + 2;

Page 41: Bab 4 Prolog

7/23/2019 Bab 4 Prolog

http://slidepdf.com/reader/full/bab-4-prolog 41/41