PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2 General purpose logic programming language ...

Post on 27-Dec-2015

222 views 3 download

Tags:

Transcript of PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2 General purpose logic programming language ...

1

PROLOGKAIST 20061013 Gunwoo Park

2

What’s Prolog?

General purpose logic programming language Declarative, which means that the program logic

is written in terms of relations (A → B) The concept is developed by a group around Alan

Colmerauer in Marseille, France, in the early 1970s First implementation is launched at 1972 Initially aimed at natural language processing, but

now its usage is stretched far into other areas Even you can make Prolog program in GUI by using

modern Prolog environments

What than How

3

Schematic View of Prolog Programming

A program is written by many clauses ( .pl ) A clause is a fact or rule

Fact Example: cindy is beautiful Rule Example: If someone is beautiful, I love him/her

Then, how to use the program? It’s done by send-ing query to the virtual machine Query Example: Do I love cindy?

Then the inference engine(virtual machine) ana-lyzes the clauses to make an answer to the query Program Answers: Yes

4

Schematic View of Prolog Program-ming (cont’d)

Program (Clauses)

Inference En-gine

(Prolog Envi-ronment)

Program User

< Scenario >1. User asks query “Do I love cindy?”2. Inference engine gets query, and sees clauses.3. Inference engine checks that cindy is beautiful, and I love any beautiful person.4. So, inference engine concludes I love cindy, and answer “Yes”

5

Environment of Prolog

SWI-Prolog is a popular development environ-ment for Prolog (http://www.swi-prolog.org)

There are many versions of SWI to support vari-ous operating systems like Windows, Mac, and Linux

6

Data Type

Before seeing real examples, data type should be understood

Prolog’s single data type is the term. Terms are either atoms, numbers, vari-ables or compound terms.

The first letter of data name is very im-portant in Prolog because it determines the type Type of cindy => Atom Type of Cindy => Variable

7

Data Types (cont’d)

The types of the term Atom : General-purpose name with no inherent meaning

Starts with lower-case: x, blue, gunwoo With single quote: ‘Taco’, ‘some atom’

Number : Floats or integers (13, 14.23) Variable : String consisting of letters and special charac-

ters, which is used as placeholder of arbitrary terms (X, Y, Z, Someone)

Compound terms: Composed of an atom called functor and a number of arguments, which are again terms trunk_year(‘Mazda’, 1986) Person_Friend(zelda, [tom, jim])

8

Rules and Facts

Program describes relations, defined by means of clauses.

A set of clauses is contained in .pl file Two types of clauses – Facts and Rules A rule is of the form of Head :- Body,

which means that Head is true if Body is true

Clauses with empty bodies are called facts. An example of fact is cat(tom), means that tom is a cat.

9

Rules and Facts - Example

As mentioned, clauses can be either facts or rules.

female(jessica) .

female(cindy) .

child(jessica, cindy) .

daughter(X, Y) :- female(X), child(X, Y) .

Semantics: jessica and cindy are female. jessica is cindy’s daughter. But there is a rule. If X is female and child of Y, then X is daughter of Y.

Then let’s ask question to the set of clauses !!!

Facts

Rule

The comma is AND operation

The dot is end of a clause

10

Query

Let’s ask a query (question) to the vir-tual machine, then the machine give an-swer by backtracking declared facts and rules.

?- daughter(jessica, cindy) .

yes

?- professor(jessica, mack) .

no

11

Boolean Operation

Negation failure(X) :- \+ success(X) .

AND (Conjunction) mom(X) :- female(X) , parent(X)

OR (Disjunction) good(X) :- money(X); power(X)

12

Knowledge Base Example 1

[kb1.pl]woman(mia) .woman(jody) .woman(yolanda) .playsAirGuitar(jody) .

[Usage Scenario]?- woman(mia) .?- playsAirGuitar(mia) .?- playsAirGuitar(yolanda) .?- tatooed(jody) .

13

Knowledge Base Example 2

[kb2.pl]listenToMusic(mia) .happy(yolanda) .playsAirGuitar(mia) :- listensToMusic(mia) .playsAirGuitar(yolanda) :- listensToMusic(yolanda) .listensToMusic(yolanda) :- happy(mia) .

[Usage Scenario]?- happy(yolanda) .?- playsAirGuitar(mia) .?- playsAirGuitar(yolanda) .?- listensToMusic(yolanda) .

14

Knowledge Base Example 3

[kb3.pl]happy(vincent) .listensToMusic(butch) .playsAirGuitar(vincent) :-

listensToMusic(vincent),happy(vincent) .

playsAirGuitar(butch) :-happy(butch) .

playsAirGuitar(butch) :-listensToMusic(butch) .

[Usage Scenario]?- playsAirGuitar(vincent) .?- playsAirGuitar(butch) .

15

Using Variables in Query

We can use variable in a query After executing a query, value is bound

to the variable as a result

?- daughter(X, cindy) . => X : jessica

?- female(X) . => X : jessica, cindy

?- daughter(jessica, _) . => yes

※ ’_’ means “there is anything which satisfies the question”. (Existential Quantification)

16

Knowledge Base Example 4

[kb4.pl]woman(mia) .woman(jody) .woman(yolanda) .loves(vincent, mia) .loves(marcellus, mia) .loves(pumpkin, honey_bunny) .loves(honey_bunny, pumpkin) .

[Usage Scenario]?- woman(X) .?- loves(marcellus, X), woman(X) .

17

Knowledge Base Example 5

[kb5.pl]loves(vincent, mia) .loves(marcellus, mia) .loves(pumpkin, honey_bunny) .loves(honey_bunny, pumpkin) .jealous(X, Y) :- loves(X, Z), loves(Y, Z) .

[Usage Scenario]?- jealous(marcellus, W) .

18

Other features

Built-in predicates I/O, like printing a character in the screen, is almost

impossible to be defined by user-defined clauses. So, Prolog provides built-in predicates for system-

atic operation. By using the built-in predicates we can do

Using graphics Communication with operating system

Loop and recursion Prolog provides the means of recursive predicates Loop can be made by those recursive predicates

19

Example Program 1

[ example.pl ]

son(jack, george) .

father(B, A) :- son(A, B) .

After loading example.pl to SWI, execute a simple query…

20

Example Program 2

Print “Hello World” in the screen using built-in predicate

21

Demonstration

readLine.pl

22

Applications of Prolog

Natural language processing (Relation) Intelligent systems (Inference) Complicated knowledge databases

(Knowledge) Logic data analysis (Inference)

23

Extensions of Prolog

Constraint logic programming HiLog and λProlog F-logic OW Prolog Logtalk Prolog-MPI Oblog

Þ Although logic programming is lack of some aspects like efficiency or flexibility, its programming paradigm is used considerably

Þ Prolog can be considered as the father of logic pro-gramming

24

Thank you