UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING...

22
UNIVERSITI TENAGA NASIO NAL CMPB454 CMPB454 ARTIFICIAL INTELLIGENCE ARTIFICIAL INTELLIGENCE (AI) (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor: Alicia Tang Y. C.

Transcript of UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING...

Page 1: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

UNIVERSITI TENAGA NASIONAL

CMPB454CMPB454ARTIFICIAL INTELLIGENCEARTIFICIAL INTELLIGENCE

(AI)(AI)

CHAPTER 6LOGIC PROGRAMMING

USING PROLOG

CHAPTER 6LOGIC PROGRAMMING

USING PROLOG

Instructor: Alicia Tang Y. C.

Page 2: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

2

PROLOG STANDS FOR PROGRAMMING IN LOGIC Features:

SYMBOLIC LANGUAGE CLEAN SYNTAXBACKTRACKINGPATTERN MATCHINGBUILT-IN SEARCH ENGINERECURSIONUNIFICATION

Look at “List”

See last example

Refer diagram

Page 3: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

3

Prolog as a Language for AI Problem Prolog as a Language for AI Problem SolvingSolving

Prolog as a Language for AI Problem Prolog as a Language for AI Problem SolvingSolving

Introduction to PROLOG– PROLOG is a language, an environment, and as a powerful problem solving tool.

– As a representational formalism, PROLOG offers an interpreter for predicate calculus specifications.

Page 4: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

4

PROLOG It is based on Predicate Logic (Horn

clause, a special type of clausal form). It makes use of resolution to proof goals. Search is done depth first with

backtracking. The primitive form is a predicate:

– father(kasim, hani). Kasim is the father of Hani– age(kasim, 60). Kasim is 60 years old– retired(kasim). Kasim has retired– intake(bit, june, 2004). BIT intake is in June’04– pi(3.145). Pi = 3.145

Page 5: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

5

FACTS (KNOWLEDGE)

parent(john,bob).parent(mary,alice).parent(mary,bob).parent(john,alice).parent(lee, june).sex(john,male).sex(mary,female).sex(bob, male).sex(alice,female).sex(june,female).sex(lee,male).

First Prolog Program

Page 6: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

6

mother(Mother,Child):- parent(Mother,Child). sex(Mother, female).

mother_1(Mother,Child):- sex(Mother, female), parent(Mother, Child).

sibling(Child1, Child2):- parent(M,Child1), sex(M, female), parent(F, Child1), sex(F, male), parent(M, Child2), parent(F, Child2).

female is a fact or “constant”

In English, the statements read as:

“Siblings are those who share the same parents”.

You will get funny answer. Try it out in Lab.

Page 7: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

7

Simpler is better. Define extra predicates.

mother(M, Child):- parent(M,Child), sex(M, female).

father(F, Child):- parent(F,Child), sex(M, male).

sibling(Child1, Child2):- mother(M, Child1), father(F,Child1), mother(M, Child2), father(F, Child2), Child1 \= Child2.

Checks that generated answers are not the same, then return them

Page 8: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

8

Prolog QueriesQueries use the format as a predicate. Predicates must be terminated by a full stop.

?- parent(Who, unix).Who = thompson;Who = ritchie;no

Prolog variables are designated with a capitalised first letter in a word.

?- father(tom, pat).yes

(Prolog answers ‘yes’ indicating query success)

Page 9: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

9

Prolog Queries

?- father(tom,Who).Who = pat;Who = janeyes

(Prolog answers and the user types a semicolon; Prolog gives another solution)

Page 10: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

10

Prolog Queries

Note: Whenever Prolog gives an answer and the user responds with a semicolon (‘;’), Prolog backtracks and looks for alternative answers.

?- father(X,pat), age(X,Age).X = tom, Age = 42yes

Note: A multi-condition query can be specified using the commas (‘,’) to separate the conditions. This represents a conjunction (‘and’) of literals.

Page 11: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

11

Prolog “Rules”Prolog programs consists of statements called clauses which are similar to sentences in Natural Language. Prolog clauses consists of a head and a body and generally has the form:

head :- body.

The head of a clause consists of a single positive literal and the body consists of one or more literals.

A fact is a clause that has an empty body thus having the form:

P.

Page 12: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

12

Prolog Rules

A rule is a clause that has a head and a non-empty body. The simplest form of a rule is the following:

P :- Q.Examples of rules are:

parent(X,Y) :- child(Y,X). % X is a parent of Y if % Y is a child of X.

nice(DAY):- sunny(DAY), not windy(DAY),

neighbour(X,Y) :- address(X, Street, Num), address(Y, Street, Num1), not equal(Num, Num1).

Page 13: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

13

Prolog Symbols and Structures

p(X,Y) : predicate symbol p describes relationship between arguments X and Y.

, : Logical “and”.; : Logical “or”.not : Logical “not”.. : Terminator (same as ; in Pascal).:- : Implication symbol (PQ) is written as (Q if P).! : “Cut” symbol, used to control backtracking to make program more deterministic.[] : List symbol. Example: [1,2,3], [anne, arthur, ada].[Head|Tail] : A format used for list manipulation where Head is head of list and tail is tail of the list. E.g. [1,2,3]: Head is 1 and Tail is [2,3].

Page 14: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

14

Lists

A list is a Prolog structure used to represent an ordered sequence of items. For example:

[prolog, miranda, lisp, clips] [1,2,3,4,5,6,7,8,9,10] [ [a,b], [a], [c], [] ]

A list with no elements is an empty list and is denoted as []. The empty list is a Prolog atom. A non-empty list consists of a head and a tail. The head is the first element of the list and the tail is the list of all the other elements.

Page 15: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

15

List Examples List Examples

list head tail [1,2,3] 1 [2,3] [ [a,b], [a], [c]] [a,b] [[a],[c]] [mon,tue,wed] mon [tue,wed] [a] a []

An empty list has no head and no tail.

Page 16: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

16

sum_fr_zero_to_n(0,0).sum_fr_zero_to_n(N,S):- sum_fr_zero_to_n(N1,S1), N is N1+1, S is S1+N.

factorial(0,1).factorial(N,R):-

factorial(N1,R1),N is N1+1,

R is N*R1.

Page 17: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

17

rev_print([]).rev_print([H|R]):- rev_print(R), write(H), nl.

reverse([],[]).reverse([H|T], L):- reverse(T,R), append(R,[H],L).

append([], L, L).append([H|T], L, [H|T1]):- append(T,L,T1).

Page 18: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

18

del(X,[],[]).del(X,[X|Y],Y):-!.del(X,[Y|Z],[Y|W]):- del(X,Z,W).

member(X,[X|Tail]).member(X, [_Head|Tail]):- member(X,Tail).

count([],0).count([Head|Tail],Count):- count(Tail,Tailcount), Count is Tailcount + 1.

Page 19: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

19

Backtracking … very powerful

Suppose that we have developed a knowledge base of:

boss(dick,harry).boss(tom,dick).boss(ann,mary).boss(mary,harry).

and, query is made as:

?- boss(X,Y), boss(Y,Z).

Page 20: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

20

Let see how backtracking in Prolog works and get a feel of how “intelligent” Prolog as a logic language.

The first predicate expression in the query will match the first fact in the database, with X=dick and Y= harry. Moving to the second predicate expression in the query, the interpreter searches for a boss fact with harry as its first argument. There is no such fact in the database, so the second expression in the query fails. So the interpreter backtracks, returning to the first expression to make another choice (this happens every time a Prolog interpreter fails). It now has to go backward to a point where there exists a choice not explored yet. So now it uses the second fact and sets X=tom and Y=dick.

Page 21: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

21

The old match for the second expression was from the first fact, so mow it examines the second, third and the fourth facts in order. Unfortunately, none have dick as their first argument, so the expression fails.

Now the interpreter returns to the first predicate expression yet again. The first and the second facts have been tried, so it uses the third fact and sets X=ann and Y=mary.

It succeeds again and Z=harry again.

Page 22: UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:

22

The interpreter goes to the second predicate expression, and searches from the start of the database for a boss fact where dick is the first argument. And, yes there is such a fact (i.e. the first fact in the knowledge base).

So Z=harry, and since we are at the end of the query, the query succeeds.

Therefore tom is the super-boss. The interpreter displays X=tom, Y=dick, Z=harry.

If the user now types a semicolon, the interpreter forces a failure and start backtracking. The interpreter goes back to what it just finished, the second expression of the query, and try to find a different match.