Download presentation
Presentation is loading. Please wait.
Published byGodfrey Higgins Modified over 9 years ago
1
PROLOG KAIST 20061013 Gunwoo Park 1
2
What’s Prolog? 2 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 3 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 sending query to the virtual machine Query Example: Do I love cindy? Then the inference engine(virtual machine) analyzes the clauses to make an answer to the query Program Answers: Yes
4
Schematic View of Prolog Programming (cont’d) 4 Program (Clauses) Inference Engine (Prolog Environment) Program User 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 5 SWI-Prolog is a popular development environment for Prolog (http://www.swi-prolog.org)http://www.swi-prolog.org There are many versions of SWI to support various operating systems like Windows, Mac, and Linux
6
Data Type 6 Before seeing real examples, data type should be understood Prolog’s single data type is the term. Terms are either atoms, numbers, variables or compound terms. The first letter of data name is very important in Prolog because it determines the type Type of cindy => Atom Type of Cindy => Variable
7
Data Types (cont’d) 7 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 characters, 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. 8
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 9
10
Query Let’s ask a query (question) to the virtual machine, then the machine give answer by backtracking declared facts and rules. ?- daughter(jessica, cindy). yes ?- professor(jessica, mack). no 10
11
Boolean Operation 11 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 12 [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 13 [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 14 [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) 15
16
Knowledge Base Example 4 16 [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 17 [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 systematic 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 18
19
Example Program 1 19 [ 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 20
21
Demonstration 21 readLine.pl
22
Applications of Prolog Natural language processing (Relation) Intelligent systems (Inference) Complicated knowledge databases (Knowledge) Logic data analysis (Inference) 22
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 programming 23
24
Thank you 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.