Download presentation
Presentation is loading. Please wait.
1
Knowledge Based Information System
Prolog Programming.
2
Prolog Prolog is a declarative language
A program is a collection of axioms from which theorems can be proven. Rather than describing how to compute a solution, a program consists of a data base of facts and logical relationships (rules). Rather then running a program to obtain a solution, the user asks a question. When asked a question, the run time system searches through the data base of facts and rules to determine (by logical deduction) the answer.
3
Logic Programming Axiom are written in a standard form known as Horn clauses A Horn clause consists of a consequent (head H) and a body (terms Bi) H B1, B2,…, Bn Semantics: when all Bi are true, we can deduce that H is true
4
Application Areas Prolog has been a very important tool in
artificial intelligence applications expert systems Updated versions of Prolog are now being used for natural language interfaces smart information management systems
5
Prolog PROgramming in LOGic
It is the most widely used logic programming language Its development started in 1970 and it was result of the collaboration between researchers from Marseille, France, and Edinburgh, Scotland Main applications: Artificial intelligence: expert systems, natural language processing, … Databases: query languages, data mining,… Mathematics: theorem proving, symbolic packages,…
6
Declarative Language This means that The programmer
declares facts defines rules for reasoning with the facts Prolog uses deductive reasoning to determine new facts from old decide whether a proposed fact (goal) can be logically derived from known facts (such a decision is called a conclusion)
7
Fact and rule Comes from Horn clause
HB1,B2,…Bn Which means if all the Bs are true, then H is also true In Prolog, we write fact in the form predicate(atom1,….) Predicate is a name that we give to a relation An atom is a constant value, usually written in lower case Fact is the H part of horn clause Rule is in the form predicate(Var1,…):- predicate1(…), predicate2(…), … Where Var1 is a variable, usually begins with upper case Yes, it’s just a rewriting of Fact is a rule that does not have the right hand side. Means “and”
8
A Prolog Program The program, sometimes called Database is a text file (*.pl) that contain the facts and rules. It contains all the relations needed to define the problem. When you launch a program you are in query mode represented by the ? – prompt. In query mode you ask questions about the relations described in the program. When Prolog is launched the ?- should appear meaning you are in query mode. In SWI-Prolog you can load a program by typing the command [file]. when the file containing your program is file.pl. When you have done this you can use all the facts and rules that are contained in the program.
9
SWI-Prolog We will use SWI-Prolog for the Prolog programming assignment After the installation, try the example program ?- [likes]. % likes compiled 0.00 sec, 2,148 bytes Yes ?- likes(Mother,baby). No ?- likes(sam, X). X = Steve ; X = Paul ; X = Samuel ; Load example likes.pl This goal cannot be proved, so it assumed to be false (This is the so called Close World Assumption) Asks the interpreter to find more solutions
10
Example location(desk, office). location(apple, kitchen).
Program location(desk, office). location(apple, kitchen). location(flashlight, desk). location('washing machine', cellar). location(nani, 'washing machine'). location(broccoli, kitchen). location(crackers, kitchen). location(computer, office). Query ?- location(apple, kitchen). yes
11
Prolog Programming Model
A program is a database of (Horn) clauses that are assumed to be true. The clauses in a Prolog database are either facts or rules. Each ends with a period. A fact is a clause without a right-hand side. rainy(asheville). A rule has a right-hand side. snowy(X) :- rainy(X), cold(X). The token :- is the implication symbol. The comma (,) indicates “and”
12
Prolog Clauses A query or goal is a clause with an empty left-hand side. Queries are given to the prolog interpreter to initiate execution of a program. Each clauses is composed of terms: Constants (atoms, that are identifier starting with a lowercase letter, numbers, punctuation, and quoted strings) E.g. baby, 4.5, +, ‘Hi, Mom’ Variables (identifiers starting with an uppercase letter) E.g. sugar Structures (predicates or data structures) E.g. africaan(sugar), date(Year,Month,Day)
13
Structures Structures consist of an atom called the functor and a list of arguments E.g. date(Year,Month,Day) E.g. T = tree(3, tree(2,nil,nil), tree(5,nil,nil)) 3 2 5
14
Prolog reasoning If we have this fact and rule rainy(Nairobi).
rainy(mombasa). dull(X):- rainy(X). We can ask (or query) prolog on its command prompt ?- dull(C). (is there a C that makes this predicate true?) It will automatically try to substitute atoms in its fact into its rule such that our question gives the answer true in this example, we begin with dull(X), so the program first chooses an atom for X, that is Nairobi(our first atom in this example) The program looks to see if there is rainy(Nairobi). There is! So the substitution gives the result “true” The Prolog will answer C= Nairobi To find an alternative answer, type “;” and “Enter” It’ll give C= Mombasa If it cannot find any more answer, it will answer “no”
15
How to ask question First, write a prolog program in a .pl file.
Then load the file, using a prolog interpreter. Or use the consult command: ?- consult(‘file.pl’). If you want to load the same program again, use reconsult. -> prevent two copies in memory. A backslash in the file name may have to be written twice, such as c:\\firstprog.pl Do not forget this.
16
How to ask question Cont’n
Then you can ask question. To exit, use command: halt.
17
Example 2 . /* Clause 1 */ located_in(Dagoretti,Nairobi).
/* Clause 2 */ located_in(Thika,Kiambu). /* Clause 3 */ located_in(kongowea,Mombasa). /* Clause 4 */ located_in(kondele,Kisumu). /* Clause 5 */ located_in(X,Kenya) :- located_in(X,Nairobi). /* Clause 6 */ located_in(X,Kenya) :- located_in(X,Kiambu). /* Clause 7 */ located_in(X,Kenya) :- located_in(X,Mombasa). /* Clause 8 */ located_in(X,Coastal_Kenya) :- located_in(X,Mombasa). /* Clause 9 */ located_in(X,Western-Kenya) :- located_in(X,Kisumu). /* Clause 10 */ located_in(Ruiru,Kiambu). .
18
Question in Prolog To ask whether Thika is in Kiambu:
?- located_in(Thika,Kiambu). This query matches clause 1. So prolog replies “yes”. ?- located_in(kongowea,Coastal_Kenya). This query can be solve by calling clause 8, and then clause 3. So prolog replies “yes”.
19
-located_in(Thika,Nairobi)
?-located_in(Thika,Nairobi). this query gets “no” as its answer because this fact cannot be deduced from the knowledge base. The query succeeds if it gets a “yes” and fails if it gets a “no”.
20
Prolog can fill in the variables
?- located_in(X, Kiambu). This is a query for prolog to find X that make the above query true. This query can have multiple solutions: both Thika and Ruiru are in Kiambu. What prolog does is: find one solution and asks you whether to look for another. ->
21
Cannot find any more solution
Questioning prolog The process will look like X = Thika More (y/n)? y X = Ruiru no Some implementations let you type semicolon without asking any question. Cannot find any more solution
22
Sometimes it won’t let you ask for alternatives
This is because: Your query prints output, so prolog assumes you do not want any more solutions. For example: ?- located_in(X,Kiambu), write(X). will print only one answer. Your query contains no variable. Prolog will only print “yes” once. Print out
23
What about printing all solutions
To get all the towns in kiambu: ?-located_in(X,Kiambu), write(X), nl, fail. New line Rejects the current solution. Forcing prolog to go back and substitutes other alternatives for X.
24
located_in is said to be nondeterministic
Because there can be more than one answer.
25
Any of the arguments can be queried
?- located_in(Thika,X). ?- located_in(X, Nairobi). ?- located_in(X,Y). Gets the names of regions that contain Thika. Gets the names of the cities that are in Nairobi. Gets all the pairs that of located_in that it can find or deduce.
26
Questioning prolog ?-located_in(X,X).
Forces the two arguments to have the same value, which will result in a fail.
27
Principle of Resolution
Prolog execution is based on the principle of resolution If C1 and C2 are Horn clauses and the head of C1 matches one of the terms in the body of C2, then we can replace the term in C2 with the body of C1 For example, C1: likes(sam,Sugar) :- africaan(sugar), sweet(sugar). C2: africaan(honey). C3: sweet(honey). We can replace the first and the second terms in C1 by C2 and C3 using the principle of resolution (after instantiating variable sugar to honey) Therefore, likes(sam, honey) can be proved
28
Another Example takes(john, ics201). takes(john, ics245).
takes(Samuel, ics302). Takes(Samuel, ics254). classmates(X, Y) :- takes(X, Z), takes(Y, Z). via resolution, yields the new rule: classmates(john, Y) :- takes(Y, ics254).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.