logic programming prolog program = sequence of fopc sentences all sentences are horn clauses p1 p2 ...
TRANSCRIPT
Logic Programming Prolog
Program = sequence of FOPC sentences All sentences are Horn clauses
p1 p2 … pn q All variables are universally quantified Variables in different sentences are distinct
Built-in inferencing mechanism Backward-chaining, DFS
Negation as failure ~P is True if it fails to prove P
Syntax FOPC sentences
: :- : , : ; ~ : not Predicates/constants: lowercase variables: uppercase
Operators +, -, *, / <, >, <=, >=, =:=, =\= T1 == T2, T1 \== T2 (T1 is identical to T2) X = Y, X \= Y (X unifies with Y)
Syntax Example
like(john, tom) like(john,tom). X like(X, tom) like(X,tom). X dog(X) animal(X) animal(X) :- dog(X). X brother(X, Z) parent(Z, Y) uncle(X, Y)
uncle(X,Y) :- brother(X,Z),parent(Z,Y).
X,I income(X, I) (I > 5000) rich(X) rich(X) :- income(X,I),(I>5000).
Running a Program 1. Write a program
myprog.dec q(X):-p(X).
c(X):-a(X),b(X).
...
2. Run the interpreter
?-
3. Read the program ?- consult(myprog).
Running a Program 4. Ask questions
Give a clause prolog interpreter tries to prove it Question with constants yes(T) or no(F) Question with variable subst.(T) or no (F)
hate(john, X) :- know(john, X) know(john, tom)
?- know(john, tom) yes.?- know(john, jane) no.?- know(john, X) X=tom?- hate(john, X) X=tom
Inference Program flow
Backward chaining, DFS Left-to-right Sentence order
gp(X,Y):-p(X,Z),p(Z,Y).p(a,b). p(b,c).
?-gp(X,c){c/Y}: subgoal = p(X,Z),p(Z,c).
{a/X,b/Z}: subgoal = p(b,c).yes
X = a
Inference
like(john, dog).
like(john, game).
like(tom, game).
friend(X,Y) :- like(X,Z),like(Y,Z).
?- friend(john, tom)
{john/X, tom/Y}: subgoal = like(john,Z),like(tom,Z).
{dog/Z}: subgoal = like(tom,dog).
no.
{game/Z}: subgoal = like(tom,game).
yes.
Inference Inference = search (DFS)
f(X):- g(X),h(X).
g(a).
g(b).
g(c).
h(b).
f(X)?
g(X),h(X)?
h(a)? h(b)?
X=a
yes.
X=b
Predefined predicates assert(p), retract(p) : add/delete facts
assert(parent(john,tom)). assert((parent(X,Y):-child(Y,X))).
consult(file) : read file reconsult(file) : delete all and read file again listing, listing(p) : display current KB trace, notrace : monitor procedure spy(p), nospy(p) : trace predicate p halt : exit read(X) : unify X with input write(X) : display X
Predefined predicates
run :- write(‘what is your name?’), read(X), assert(man(X)), write(‘Hi: ’), write(X), nl.
?- run.what is your name? kim.Hi, kim?- man(kim).yes
Lists and recursion [ ]
Ordered set of elements Used for recursive search Ex> [1, 2, 3], [john, tom], [ ]
Unification with lists First element is separated by ‘|’group([a,b,c])
?- group(X).
?- group([X,Y,Z]). ?- group([X|Y]).
Lists and recursion Anonymous variables
_ : Don’t care?- group([a,c,b]). no
?- group([a,_,_]). yes
Example: define a ‘member’ predicate Member(E, L): T if E is in L Ex>
member(a,[a,b,c]) yes
member(b,[a,b,c]) yes
member(d,[a,b,c]) no
Lists and recursion ‘member’ program
member( , ).
member( , ):-member( , ).
?- member(a,[a,b])
{a/X}: yes
?- member(b,[a,b])
{b/X,[b]/T}: subgoal=member(b,[b])
{b/X}: yes
Lists and recursion ‘writelist’ program
writelist( ).
writelist( ):-write( ),nl,writelist( ).
?- writelist([a,b])
{a/H, [b]/T}: write(a), subgoal=writelist([b])
{b/H,[]/T}: write(b), subgoal=writelist([])
{}: yes