T4 Pro 1
T4 Pro 1
1
How does Prolog answer questions?
⚫ Propositional Logic
– A question to Prolog is always a sequence of one or
more goals.
– To answer a question, Prolog tries to satisfy all the
goals.
– To satisfy a goal means to demonstrate that the goal
logically follows the facts and rules in the program.
⚫ eg. (P Q) R
⚫ If sentences P and Q are true, R is true.
2
How does Prolog answer questions?(cont’d)
⚫ First Order Logic
– If the question contains variables, and the variables are
instantiated, Prolog will try to match the objects
assigned into the variables (in place of variables) in
other to satisfy goals.
4
Recall the example in previous tutorial
5
Prolog’s Inference through example
Query by users (Goal for the inference)
?- predecessor (tom, pat)
This is the first goal but cannot find in the facts.
Prolog first tries that clause which appears first in the program:
predecessor(X, Z) :- parent(X, Z).
6
Prolog’s Inference through example(cond’t)
parent(tom, pat) no clause satisfying, therefore this goal
fails.
7
Prolog’s Inference through example(cond’t)
i. parent(tom, Y) it matches one of the facts in the program:
parent(tom, bob).
Y = bob.
8
Prolog’s Inference through example(cond’t)
1 2
9
Programming Prolog
⚫ Programmers should concentrate mainly on the
declarative meaning and, whenever possible,
avoid being distracted by the execution details.
10
Programming Prolog (cond’t)
⚫ Prolog's basic mechanism of resolution
– The way Prolog finds answers is called SLD resolution by
building the SLD Tree.
⚫ Different Logic programming systems build
different SLD-trees, because of
– Computational rule: choose which goal given a query, and
– Search strategy: how to traverse the SLD-tree
⚫ Pure Prolog is a particular logic programming
system with
– Computational rule: select the leftmost goal first.
– Search strategy: depth-first search and left to right.
11
Logical operations
⚫ Comparison
– X > Y X >= Y X< Y X =< Y X == Y X \== Y X =:= Y X
=\= Y
– ?- 1+2 == 2+1.
– no.
12
Arithmetic operators
13
Negation
14
Negation an example
⚫ student(pat).
⚫ passed(joe).
⚫ failed_student(X) :- not(passed(X)), student(X).
– ?- failed_student(pat).
– yes.
– ?- failed_student(X).
– no. ----> cannot find nobody.
15
Lists
16
Lists
⚫ The vertical bar can be used to separate the head and the tail.
⚫ [a, b, c] = [a | [b,c]] = [a, b | [c]] = [a, b, c | []].
17
Operations on Lists
⚫ Membership
– member(X,[X|Tail]).
– member(X,[Head|Tail]) :- member(X,Tail).
⚫ Deleting an Item
– del(X,[X,Tail],Tail).
– del(X,[Y,Tail],[Y|Tail1]) :- del(X,Tail,Tail1).
⚫ S is a sublist of L
– sublist(S,L) :- conc(L1,L2,L), conc(S,L3,L2).
18
Input and Output of prolog
19
Advanced topic of prolog (Controlling
Backtracking)
⚫ Prolog will automatically backtrack if necessary when
satisfying a goal.
20
Advanced topic of prolog (Controlling
Backtracking 2)
⚫ For example
– ?- max(X,Y,X) :- X >= Y.
– ?- max(X,Y,Y) :- X < Y.
– ?- max(X,Y,X) :- X >= Y, !.
– ?- max(X,Y,Y) :- X < Y.
21
Advanced topic of prolog(Data Manipulation)
22
Advanced topic of prolog(Data Manipulation 2 )
⚫ ?- crisis.
⚫ no.
⚫ ?- assert(crisis).
⚫ yes.
⚫ ?- crisis.
⚫ yes.
⚫ retract(crisis).
⚫ yes.
⚫ ?- crisis.
⚫ no.
23
Advanced topic of prolog(Data Manipulation 3 )
⚫ assert(C) : a clause C is asserted in a arbitrary position in the
database.
⚫ asserta(C) : a clause C is asserted at the beginning of the
database.
⚫ assertz(C) :a clause C is asserted at the end of the database.
24
End
25