logic programming prolog program = sequence of fopc sentences all sentences are horn clauses p1 p2 ...

14
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 distin ct Built-in inferencing mechanism Backward-chaining, DFS Negation as failure ~P is True if it fails to prove P

Upload: benedict-skinner

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 2: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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)

Page 3: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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).

Page 4: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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).

Page 5: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 6: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 7: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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.

Page 8: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 9: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 10: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 11: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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]).

Page 12: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 13: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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

Page 14: Logic Programming Prolog Program = sequence of FOPC sentences All sentences are Horn clauses p1  p2  …  pn  q All variables are universally quantified

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