Sei sulla pagina 1di 35

Introduction to AI

AAPP002-4-2 Ver 1.0

Prolog
Topic & Structure of The Lesson

• Facts
• Rules

AAPP002-4-2 Intro to AI Prolog Slide 2 of 35


Learning Outcomes

• At the end of this topic, You should be


able to
• Automated Systems
• Robotics
• CAD/CAM
• Simulations
• Intelligent robots and sensors

AAPP002-4-2 Intro to AI Prolog Slide 3 of 35


Key Terms You Must Be Able To
Use
• If you have mastered this topic, you should be able to use the
following terms correctly in your assignments and exams:

• Prolog
• Fact
• Rules

AAPP002-4-2 Intro to AI Prolog Slide 4 of 35


Recursion

• Definition:
• The recursion in any language is a
function that can call itself until the goal
has been succeed.

AAPP002-4-2 Intro to AI Prolog Slide 5 of 35


Recursion

• In Prolog:
• Recursion appears when a predicate
contain a goal that refers to itself.
• A program calls itself typically until some
final point is reached.

AAPP002-4-2 Intro to AI Prolog Slide 6 of 35


Parts of recursive definition

• A recursive definition always has at least


two parts.
– Fact that act like a stopping condition.
– A rule that call itself simplified

AAPP002-4-2 Intro to AI Prolog Slide 7 of 35


Examples of Recursion

• Example of the ancestors


• parent(john,paul).
• parent(paul,tom).
• parent(tom,mary).
• ancestor(X,Y) :- parent(X,Y).
• ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
?- ancestor(john,tom).

AAPP002-4-2 Intro to AI Prolog Slide 8 of 35


• CALL ancestor(john,tom).
• CALL parent(john,tom).
• FAIL parent(john,tom).
• CALL parent(john,Z).
• TRY Z=paul
• CALL ancestor(paul,tom).
• CALL parent(paul,tom).
• SUCCEEDS parent(paul,tom).
• SUCCEEDS ancestor(paul,tom).
• SUCCEEDS with Z=paul
• SUCCEEDS ancestor(john,tom).

AAPP002-4-2 Intro to AI Prolog Slide 9 of 35


Factorial problem
● Consider a classical example of computing factorial
using recursion.
● Recurrence relation or mathematical definition for
computing factorial of positive integer is:
1, if n = 0
FACTORIAL (n) =
n * FACTORIAL (n-1),
otherwise
● In Prolog, program for computing factorial of a
positive number is written using above definition.
● Factorial predicate contains two arguments; one as
input and another as output.

AAPP002-4-2 Intro to AI Prolog Slide 10 of 35


Recursion - Cont..
/*fact(N, R) – succeeds if R is unified with the factorial of
N */
factorial(0, 1). (1)
factorial(N, R) :- N1 is N–1, factorial(N1, R1), R is N * R1. (2)

● Rule (1) states that factorial of 0 is 1 which is a


terminating clause.
● Rule (2) is a recursive rule which states that factorial of
N is calculated by multiplying N with factorial of (N-1).

Goal: ?- factorial(3, R).

AAPP002-4-2 Intro to AI Prolog Slide 11 of 35


Execution of factorial query
Search tree:
?- factorial(3, R).
(2) {N = 3}
?- N1 is N-1, factorial(N1, R1), R is N * R1
(2) {N1 = 2}
?-N2 is N1-1,factorial(N2,R2),R1 is N1*R2,R is N*R1.
(2) {N2 = 1}
?-N3 is N2-1, factorial(N3, R3), R2 is N2 * R3….
(1) {N3 = 0, R3 = 1}
?- R2 is 1 * R3 , R1 is N1 * R2, R is N * R1.
Succeeds {R2 = 1, R1 = 2, R = 6}

AAPP002-4-2 Intro to AI Prolog Slide 12 of 35


Why use recursion?

• It allows us to define very clear and


elegant code.
– Why repeat code when you can reuse existing code.

• Relationships may be recursive


e.g. “X is my ancestor if X is my Ancestor’s ancestor.”

• Allows Prolog to perform complex search


of a problem space without any dedicated
algorithms.

AAPP002-4-2 Intro to AI Prolog Slide 13 of 35


Structures
• To create a single data element from a collection of related
terms we use a structure.
• A structure is constructed from a functor (a constant symbol)
and one of more components.

somerelationship(a,b,c,[1,2,3])

• The components can be of any type: atoms, integers,


variables, or structures.
• As functors are treated as data objects just like constants they
can be unified with variables

AAPP002-4-2 Intro to AI Prolog Slide 14 of 35


Structure unification
• 2 structures will unify if
– the functors are the same,
– they have the same number of components,
– and all the components unify.
| ?- person(Nm,london,Age) = person(bob,london,48).
Nm = bob,
Age = 48?
yes
| ?- person(Someone,_,45) = person(harry,dundee,45).
Someone = harry ?
yes

• (A plain underscore ‘_’ is not bound to any value. By using it you


are telling Prolog to ignore this argument and not report it.)
AAPP002-4-2 Intro to AI Prolog Slide 15 of 35
Structure unification (2)
A structure may also have another structure as a component.

|?-addr(flat(4),street(‘Home
Str.’),postcode(eh8_9lw))
= addr(flat(Z),Yy,postcode(Xxx)).
Z = 4,
Yy = street(‘Home Str.’),
Xxx = eh8_9lw ?
yes

• Unification of nested structures works recursively:


– first it unifies the entire structure,
– then it tries to unify the nested structures.

AAPP002-4-2 Intro to AI Prolog Slide 16 of 35


Structures = facts?
• The syntax of structures and facts is
identical but:
– Structures are not facts as they are not stored in the database
as being true (followed by a period ‘.’);
– Structures are generally just used to group data;
– Functors do not have to match predicate names.

By instantiating a variable with a structure which


is also a predicate you can pass commands.

AAPP002-4-2 Intro to AI Prolog Slide 17 of 35


Lists

• A collection of ordered data.


• Has zero or more elements enclosed by square brackets (‘[ ]’) and
separated by commas (‘,’).

[a]  a list with one element


[]  an empty list

[34,tom,[2,3]]  a list with 3 elements where the


3rd element is a list of 2 elements.

AAPP002-4-2 Intro to AI Prolog Slide 18 of 35


List Unification
• Two lists unify if they are the same
length and all their elements unify.
|?-[a,B,c,D]=[A,b,C,d].
|?-[(a+X),(Y+b)]=[(W+c),(d+b)].
A = a, W = a,
B = b, X = c,
C = c, Y = d?
D = d ? yes
yes

|?- [[X,a]]=[b,Y]. |?-[[a],[B,c],[]]=[X,[b,c],Y].


no B = b,
X = [a],
Y = [] ?
yes
AAPP002-4-2 Intro to AI Prolog Slide 19 of 35
Head and Tail
|?-[a,b,c,d]=[Head|Tail]. |?-[a,b,c,d]=[X|[Y|Z]].
Head = a, X = a,
Tail = [b,c,d]? Y = b,
yes Z = [c,d];
yes

|?-[a] = [H|T]. |?-[a,b,c]=[W|[X|[Y|Z]]].


H = a, W = a,
T = []; X = b,
yes Y = c,
Z = []? yes

|?-[] = [H|T]. |?-[a|[b|[c|[]]]]= List.


no List = [a,b,c]?
yes

AAPP002-4-2 Intro to AI Prolog Slide 20 of 35


Membership function

● An element is a member of a list if it matches with the


head or if it is contained in the tail of the list.
− The fact that it matches with the head of a list is written as:
mem_list(X, [X | _ ]).
− The rule that if an element does not match with the head, then it
may be in the tail is written as: mem_list(X, [ _ | Y]) :-
mem_list(X, Y).

mem_list(X, [X | _ ]). (1)


mem_list(X, [_ | Y]) :- mem_list(X, Y). (2)

− Underscore is used for non care entry.

AAPP002-4-2 Intro to AI Prolog Slide 21 of 35


Ground Query
Goal: ?- mem_list(d, [a, b, c, d, e, f]).
Proof tree: ?- mem_list(d, [a, b, c, d, e, f]).
(2) {X = d, Y = [b, c, d, e, f]}
?- mem_list(d, [b, c, d, e, f]).
(2) {X = d, Y1 = [c, d, e, f]}
?- mem_list(d, [c, d, e, f]).
(2) {X = d, Y2 = [d, e, f]}
?- mem_list(d, [d, e, f]).
(1)
succeeds Ans: Yes

AAPP002-4-2 Intro to AI Prolog Slide 22 of 35


Non ground Query
Goal: ?- mem_list(X, [a, b, c]).
Search tree:
?- mem_list(X, [a, b, c]).
(1) (2)
{X = a} {Y = [b, c]}
succeeds ?- mem_list(X, [b, c]).
(1) (2)
{X = b} {Y = [c]}
succeeds ?- mem_list(X, [c]).
(1) (2)
{X = c} {Y = []}
succeeds ?- mem_list(X, []).
Answer:X = a; X = b; X = c fails

AAPP002-4-2 Intro to AI Prolog Slide 23 of 35


Add or Delete an item

• To add item to a list, easiest way is to put item in front of the list.
• X is the new item Added to list L will be given by relation
[X | L] or add(X,L,[X|L] ) .

• Deleting an item X, from list L will be given by relation


del(X,L,L1)
where L1 is the list L with item X removed.

AAPP002-4-2 Intro to AI Prolog Slide 24 of 35


Append a list

• Another very useful list predicate builds lists from other


lists
or
alternatively splits lists into separate pieces.
• This predicate is usually called append.
• The second argument is appended to the first argument
to yield the third argument. For example
• ?- append([a,b,c],[d,e,f],X).
• X = [a,b,c,d,e,f]

AAPP002-4-2 Intro to AI Prolog Slide 25 of 35


Predefined functions

• Predefined basic arithmetic operators:


+ addition
- subtraction
* multiplication
/ division
** power
// integer division
mod modulo, the remainder of integer division

| ?- X = 1+2.
Operator ‘is’ is a built-in
X = 1+2
procedure.
yes
| ?- X is 1+2.
X=3
yes

AAPP002-4-2 Intro to AI Prolog Slide 26 of 35


Predefined functions
• Another example:
| ?- X is 5/2,
Y is 5//2,
Z is 5 mod 2.
X = 2.5
Y=2
Z=1
• Since X is 5-2-1 X is (5-2)-1, parentheses can be used to indicate
different associations. For example, X is 5-(2-1).
• Prolog implementations usually also provide standard functions such
as sin(X), cos(X), atan(X), log(X), exp(X), etc.
| ?- X is sin(3).
X = 0.14112000805986721
• Example:
| ?- 277*37 > 10000.
yes

AAPP002-4-2 Intro to AI Prolog Slide 27 of 35


Predefined functions

• Predefined comparison operators:


X>Y X is greater than Y
X<Y X is less than Y
X >= Y X is greater than or equal to Y
X =< Y X is less than or equal to Y
X =:= Y the values of X and Y are equal
X =\= Y the values of X and Y are not equal

| ?- 1+2 =:= 2+1.


yes
| ?- 1+2 = 2+1.
no
| ?- 1+A = B+2.
A=2
B=1
yes
AAPP002-4-2 Intro to AI Prolog Slide 28 of 35
Predefined functions

• GCD (greatest common divisor) problem:


– Given two positive integers, X and Y, their greatest common
divisor, D, can be found according to three cases:
(1) If X and Y are equal then D is equal to X.
(2) If X < Y then D is equal to the greatest common divisor of X
and the difference Y-X.
(3) If Y<X then do the same as in case (2) with X and Y
interchanged.
– The three rules are then expressed as three clauses:
gcd( X, X, X).
gcd( X, Y, D) :- X<Y, Y1 is Y-X, gcd( X, Y1, D).
gcd( X, Y, D) :- Y<X, gcd( Y, X, D).
?- gcd( 20, 25, D)
D=5.

AAPP002-4-2 Intro to AI Prolog Slide 29 of 35


Predefined functions

• Length counting problem: (Note: length is a build-in


procedure)
– Define procedure length( List, N) which will count the
elements in a list List and instantiate N to their number.
(1) If the list is empty then its length is 0.
(2) If the list is not empty then List = [Head|Tail]; then its length
is equal to 1 plus the length of the tail Tail.
– These two cases correspond to the following program:
length( [ ], 0).
length( [ _ | Tail], N) :- length( Tail, N1),
N is 1 + N1.
?- length( [a, b, [c, d], e], N)
N = 4.

AAPP002-4-2 Intro to AI Prolog Slide 30 of 35


Predefined functions

length1( [ ], 0).
length1( [ _ | Tail], N) :- length1( Tail, N1),
N = 1 + N1.

?- length( [a, b, [c, d], e], N)


N = 1+(1+(1+(1+0)))

length2( [ ], 0).
length2( [ _ | Tail], N) :- N = 1 + N1,
length2( Tail, N1).
length2( [ _ | Tail], 1 + N) :- length2( Tail, N).

| ?- length2([a,b,c],N), Length is N.
Length = 2
N = 1+(1+(1+0))

AAPP002-4-2 Intro to AI Prolog Slide 31 of 35


Quick Review Question

AAPP002-4-2 Intro to AI Prolog Slide 32 of 35


Summary of Main Teaching Points

• Prolog is the main alternative to LISP as an AI


programming language and exists in several
dialects, the Edinburgh (or DEC-10) syntax
being the most widespread.
• Programming in Prolog is very different to
programming in imperative languages such as
BASIC and C and emphasises problem
description over the low-level mechanics of how
to solve a particular problem. Prolog is
described as a declarative language.

AAPP002-4-2 Intro to AI Prolog Slide 33 of 35


Question and Answer Session

Q&A

AAPP002-4-2 Intro to AI Prolog Slide 34 of 35


What we will cover next

• Rete Algorithm

AAPP002-4-2 Intro to AI Prolog Slide 35 of 35

Potrebbero piacerti anche