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

24
PROLOG KAIST 20061013 Gunwoo Park 1

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

Page 1: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

1

PROLOGKAIST 20061013 Gunwoo Park

Page 2: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 3: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 4: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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”

Page 5: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 6: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 7: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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])

Page 8: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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.

Page 9: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 10: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 11: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

11

Boolean Operation

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

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

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

Page 12: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 13: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 14: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 15: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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)

Page 16: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 17: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 18: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 19: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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…

Page 20: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

20

Example Program 2

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

Page 21: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

21

Demonstration

readLine.pl

Page 22: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

22

Applications of Prolog

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

(Knowledge) Logic data analysis (Inference)

Page 23: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

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

Page 24: PROLOG KAIST 20061013 Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.

24

Thank you