Professional Documents
Culture Documents
Sheet
Sheet
Sheet
.
!
"
#$! %&.469 "*!+,-+
./0
%!!1
%&!
%2!1
3,1%
,
"!1
,*
3
%4
4
!"# $%&
'!()* +*%"
!,-#% !" )
." $ Knowledge Base /0
$ 0
% '!()*
12." ."$3 %
$4 %$"#
$2."
! &
# &
%
##& % 3*3! $ " +4
! 1 2." !"50 (3
676 #&& -%$"
." %" !"#
89-
& 167
1 67%$" 50("
!#
- !
1 3" !
+(*+!% !9(
%"
1$
!
2."&!$# 50(& !"
." !"&
%"
*
% 5*
-*!" :
0 +*%"-"'$&5# %
+ 8+9
2."&!
*
$ "
Introduction 1
Prolog Constants 3
Rules 3
Prolog’s Search Strategy 6
Unification & Recursion 7
Lists 8
Box Model of Execution 10
Programming Techniques 12
List Processing 14
Control and Negation 15
Parsing in Prolog 16
Special Control Predicate 18
Input/Output 20
Prolog Example 21
Outline
Introduction to Prolog
Knowledge representation
Logic Programming Prolog’s search strategy
Sukree Sinthupinyo
1 2
3 4
Logic Programming 1
Knowledge Representation Knowledge Representation (cont’)
5 6
7 8
Logic Programming 2
Examples of statement in
Prolog Constants Prolog form
Constant is a string of characters bill likes ice-cream.
starting with a lower case letter. bill is tall
loves(jane,jim).
jane hits jimmy with the cricket bat
jane and jim are constant.
john travels to london by train
No predicate may be variable
X(jane,jim).
9 10
11 12
Logic Programming 3
Rules (continue) Rules (continue)
13 14
15 16
Logic Programming 4
The Logical Variable (continue) Rules and Conjunction
The logical variable cannot be A man is happy if he is rich and famous
overwritten with a new value. might translate to:
For example, in Pascal: happy(Person):-
X:= 1; X:= 2;
man(Person),
In Prolog:
rich(Person),
X=1 ∧ X=2.
cannot be true as X cannot be both ‘2’ and ‘1’ famous(Person).
simultaneously. An attempt to make a logical
variable take a new value will fail.
17 18
19 20
Logic Programming 5
Prolog’s Search Strategy Queries and Disjunctions
Prolog use depth-first search. A query is a goal which is submitted to
Prolog enables the programmer to Prolog in order to determine whether
implement other search methods quite this goal is true or false.
easily. Prolog normally expects queries it prints
Prolog is an interactive system. the prompt:
?-
21 22
23 24
Logic Programming 6
A Simple Conjunction (continue) A Simple Conjunction (continue)
25 26
Unification Recursion
Unification is a two way matching Prolog depends heavily upon it
process Examples
book(waverley,X) and book(Y,scott)
X/scott, Y/waverley One of my ancestors is one of my parents
or one of their ancestors.
Examples
X=fred. succeeds A string of characters is a single character
jane=fred fail or a single character followed by a string of
Y=fred,X=Y succeeds characters
X=happy(jim) succeeds
27 28
Logic Programming 7
Recursion (continue) Lists
Examples Lists can be regarded as special Prolog
ancestor(X,Y) :- parent(X,Y). structures.
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
Lists can be used to represent an
Program Database ordered sequence of Prolog terms.
ancestor(X,Y) :- parent(X,Y). Examples
ancestor(X,Y) :- parent(X,Z),
ancestor(Z,Y).
[ice_cream,coffee,chocolate]
parent(bob,bill). parent(bill,jane). [a,b,c,c,d,e]
parent(jane,jim). parent(bob,babe).
29 30
31 32
Logic Programming 8
Lists (continue) Lists (continue)
33 34
35 36
Logic Programming 9
The Box Model of Execution
The Box Model of Execution (continue)
We can think in term of procedure The control flows into the box through
rather than predicate. the Call port.
Then, Prolog seek a clause with a head
that unify with the goal.
Seek solutions to all the subgoals in the
body of the successful clause.
If the unification fails for all clauses then
control would pass out of the Fail port.
37 38
39 40
Logic Programming 10
The Flow of Control (continue)
The Flow of Control (continue)
41 42
43 44
Logic Programming 11
Practical Matter (continue) Practical Matter (continue)
45 46
47 48
Logic Programming 12
Programming Techniques and Programming Techniques and
Lists Processing (continue) Lists Processing (continue)
Evaluation in Prolog successor(X,Y):- Y is X + 1.
?- successor(3,X).
Pascal: Y:=2+1;
?- successor(X,4).
Prolog: Y=2+1.
?- successor(X,Y).
This command only binds the variable Y with
?- successor(3,5).
the clause 2+1 and gives the value of Y as 2+1
Prolog: Y is 2+1.
After the above command, the value of Y is 3.
49 50
51 52
Logic Programming 13
Lists Processing Lists Processing (continue)
53 54
Example Example
all_digits([]). everything_after_a([Head|Tail],Result):-
all_digits([Head|Tail]):- Head = a,
member(Head,[0,1,2,3,4,5,6,7,8,9]), Result = Tail.
all_digits(Tail). everything_after_a([Head|Tail],Ans):-
everything_after_a(Tail,Ans).
Return a Result – Having processed one element
return_after_event(Info,[H|T],Result):- Return a Result – Having processed all elements
property(Info,H), process_all(Info,[],[]).
result(Info,H,T,Result). process_all(Info,[H1|T1],[H2|T2]):-
return_after_event(Info,[H|T],Ans):- process_one(Info,H1,H2),
return_after_event(Info,T,Ans). process_all(Info,T1,T2).
55 56
Logic Programming 14
Lists Processing (continue) Control and Negation
Example Some useful predicate for control
triple([],[]). true/0
triple([H1|T1],[H2|T2]):-
father(jim,fred).
H2 is 3*H1,
triple(T1,T2). father(jim,fred) :- true.
fail/0
lives_forever(X) :- fail.
repeat/0
test :- repeat, write(hello), fail.
57 58
59 60
Logic Programming 15
Generate – Test Test – Process
integer_with_two_digit_square(X) :- int(X), test_process(Info,X,Y):-
test square(X).
test(Info,X),
test square(X) :- Y is X*X, Y >= 10, Y < 100.
process(Info,X,Y).
Finite generator
int(1). int(2). int(3). int(4). int(5). Example
parity(X,Y) :- odd(X), Y=odd.
Infinite Generator parity(X,Y) :- \+ (odd(X)),Y=even.
int(1).
int(N) :- int(N1), N is N1+1.
61 62
63 64
Logic Programming 16
Simple English Syntax First Attempt at Parsing
The man ate the cake. sentence(S):-append(NP,VP,S),
noun_phrase(NP),verb_phrase(VP).
noun_phrase(NP):-append(Det,Noun,NP),
determiner(Det),noun(Noun).
verb_phrase(VP):-append(Verb,NP,VP),
verb(Verb),noun_phrase(NP).
determiner([a]). determiner([the]).
noun([man]). noun([cake]).
verb([ate]).
65 66
67 68
Logic Programming 17
Prolog Grammar Rules Prolog Grammar Rules
Prolog enables you to write in: How to Extract a Parse Tree
sentence --> noun phrase, verb phrase. sentence([[np,NP],[vp,VP]]) --> noun_phrase(NP),
verb_phrase(VP).
and Prolog turns this into: noun_phrase([[det,Det],[noun,Noun]]) -->
sentence(S,S0):-noun_phrase(S,S1), determiner(Det), noun(Noun).
verb_phrase(S1,S0). determiner(the) --> [the].
69 70
71 72
Logic Programming 18
Special Control Predicate (continue) Special Control Predicate (continue)
73 74
75 76
Logic Programming 19
Input/Output Input/Output and File
read/1 see/1: Take input from the named file
test:-read(X),double(X,Y), seen/0: Close the current input stream
write(Y),nl.
and take input from user
?- test.
|: 2. seeing/1: Returns name of current
4 input stream
yes
77 78
79 80
Logic Programming 20
Input/Output and File (continue) Prolog Examples
test:-read(X),\+(X = -1), move(state(middle,onbox,middle,hasnot),grasp,
state(middle,onbox,middle,has)).
double(X,Y),write(Y),nl,test. move(state(P,onfloor,P,H),climb,state(P,onbox,
go:-tell(out),see(in),test,seen, P,H)).
move(state(P1,onfloor,P1,H),push(P1,P2),
told. state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),walk(P1,P2),
state(P2,onfloor,B,H)).
81 82
findsuccessor(OldState,NewState,Operator) :-
move(OldState,Operator,NewState).
83
Logic Programming 21