# Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.

## Presentation on theme: "Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University."— Presentation transcript:

Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University

Logic programs A logic program describes individuals and relations between individuals (or properties of individuals). The program is used to answer queries about the world described in the program.

Relations zAdam is a parent of Bill zParis is the capital of France z5 is greater than 2 plus 2 zX times 1 is equal to X zX is a subset of Y z5 is the maximum of 2 and 5 zThere is an edge from a to b

Properties zAdam is a parent zAdam is male zX plus 1 is non-zero zParis is a capital zGrass is green zThe music was loud

Queries zWho is the father of Bill? zIs there an edge from a to b? zWhich town is a capital? zWho is male?

Language primitives zConstants adam, paris, 5, 3.14, [], ´Adam’,... zVariables X, Y, List, _12, _,... zFunction symbols plus/2, +/2, f/1,... zPredicate symbols capital/2, greater/2, non_zero/1, >/2,...

Terms Terms represent individuals zConstants zVariables zCompound terms zE.g. paris, X, plus(2,3), plus(2,plus(3,4)) zInfix notation: 2+3

Atomic formulas Atomic formulas describe relations: zIf p is a predicate letter of arity n and t 1,...,t n are terms then p(t 1,...,t n ) is an atomic formula. zE.g. capital(paris,france) greater(X,2) zInfix notation: X > 2

Logic Programs zA logic program is a set of clauses: yfacts yrules zThe program is used to answer queries.

Facts zA fact is an expression of the form: A. where A is an atomic formula. zExamples: edge(a, X). parent(adam, bill).

Interpretation Facts zConsider a fact A. zDeclarative (logical) reading: For all variables, A is true. zProcedural (operational) reading: A is solved.

Rules zA rule is an expression of the form: A 0 :- A 1,..., A n. where each A i is an atomic formula. zExamples: path(X,Y) :- edge(X,Y). father(X,Y) :- parent(X,Y), male(X).

Interpretation Rules zConsider a rule A 0 :- A 1,..., A n. zDeclarative (logical) reading: For all variables, A 0 if A 1 and...and A n. zProcedural (operational) reading: To solve A 0, first solve A 1, then A 2 etc.

Example gp(X,Y) :- p(X,Z), p(Z,Y). p(X,Y) :- f(X,Y). p(X,Y) :- m(X,Y). f(adam,bill). f(bill,carl). m(anne,bill).

Queries zA query is an expression of the form: ?- A 1,..., A n. where n=0,1,2,... and A 1,..., A n are atomic formulas. zExamples: ?- father(X, bill). ?- parent(X, bill), male(X).

Interpretation Queries zConsider a query ?- A 1,..., A n. zDeclarative (logical) reading: Are there variables such that A 1 and...and A n ? zProcedural (operational) reading: First solve A 1, then A 2 etc

Ground SLD-Resolution ?- A 1,A 2,...,A n. ?- B 1,...,B m,A 2,...,A n. A 1 :- B 1,...,B m. where A 1 :- B 1,...,B m is an instantiated program clause.

parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill). A Derivation ?- parent(adam,bill) ?- father(adam,bill)?- true parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill).

parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill). Another Derivation ?- parent(anne,bill) ?- mother(anne,bill)?- true parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). father(adam,bill). mother(anne,bill).

Full SLD-Resolution ?- A 1,A 2,...,A n. ?- A 1 = B 0, B 1,...,B m,A 2,...,A n. B 0 :- B 1,...,B m. where: B 0 :- B 1,...,B m is a renamed program clause. ?- (B 1,...,B m,A 2,...,A n ) .  is a solution to the equation A 1 = B 0.

And Another One... ?- gp(X,Y). gp(X1,Y1) :- p(X1,Z1),p(Z1,Y1). ?- X=X1, Y=Y1, p(X1,Z1), p(Z1,Y1).?- p(X,Z1), p(Z1,Y). p(X2,Y2) :- f(X2,Y2). ?- X=X2, Z1=Y2, f(X2,Y2), p(Z1,Y).?- f(X,Z1), p(Z1,Y). f(adam,bill). ?- X=adam,Z1=bill, p(Z1,Y). ?- p(bill,Y). X=adam p(X3,Y3) :- f(X3,Y3). ?- bill=X3, Y=Y3, f(X3,Y3).?- f(bill,Y). f(bill,carl). ?- bill=bill, Y=carl. ?- true. Y=carl

And a Failed One... ?- gp(X,Y). gp(X1,Y1) :- p(X1,Z1),p(Z1,Y1). ?- X=X1, Y=Y1, p(X1,Z1), p(Z1,Y1).?- p(X,Z1), p(Z1,Y). p(X2,Y2) :- f(X2,Y2). ?- X=X2, Z1=Y2, f(X2,Y2), p(Z1,Y).?- f(X,Z1), p(Z1,Y). f(bill,carl). ?- X=bill,Z1=carl, p(Z1,Y). ?- p(carl,Y). X=bill p(X3,Y3) :- f(X3,Y3). ?- carl=X3, Y=Y3, f(X3,Y3).?- f(carl,Y).?- fail. FAILURE!!!

?- true. Y=carl ?- p(bill,Y). X=adam SLD-Tree ?- gp(X,Y). ?- f(bill,Y).?- m(bill,Y).?- fail. ?- p(X,Z),p(Z,Y). ?- f(X,Z),p(Z,Y). ?- p(carl,Y). ?- f(carl,Y).?- m(carl,Y).?- fail. ?- m(X,Z),p(Z,Y). ?- f(bill,Y).?- m(bill,Y). ?- true. Y=carl ?- fail. ?- p(bill,Y). X=anne

Example /* or(In1, In2, Out) */ or(0, 0, 0). or(0, 1, 1). or(1, 0, 1). or(1, 1, 1). /* nand(In1, In2, Out) */ nand(X, Y, Z) :- and(X, Y, Tmp), inv(Tmp, Z). /* inv(In, Out) */ inv(0, 1). inv(1, 0). /* and(In1, In2, Out) */ and(0, 0, 0). and(0, 1, 0). and(1, 0, 0). and(1, 1, 1).

Database lecturer(Lecturer,Course) :- course(Course,_,Lecturer,_). duration(Course,Length) :- course(Course,time(_,S,F),_,_), plus(S,Length,F). teaches(Lect,Day) :- course(_, time(Day,_,_), Lect, _). occupied(Room,Day,Time) :- course(_,time(Day,S,F),_,Room), S =< Time, Time =< F. % Database course(logic, time(monday, 8, 10), dave, a12)....

Recursion path(Node,Node). path(Node1,Node3) :- edge(Node1,Node2), path(Node2,Node3). edge(a,b). edge(a,c). edge(b,d). edge(c,d). edge(d,e). edge(f,g). a b c d e f g

a b c. [].. c b a List Notation.(a,.(b,.(c, [])))

More On List Notation zThe empty list: [] zA non-empty list:.(X,Y) or [X|Y] Syntactic Sugar: z[b] instead of [b|[]] and.(b, []) z[a,b] instead of [a|[b]] and [a|[b|[]]] z[a,b|X] instead of [a|[b|X]]

List manipulation list([ ]). list([X|Xs]) :- list(Xs). member(X,[X|Xs]). member(X,[Y|Ys]) :- member(X,Ys). append([ ],Ys,Ys). append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).

List Manipulation % reverse(A, B) % B is A in reverse order reverse([ ],[ ]). reverse([X|Xs],Zs) :- reverse(Xs,Ys), append(Ys,[X],Zs). % Alternative version reverse(Xs,Ys) :- reverse(Xs,[ ],Ys). reverse([ ],Ys,Ys). reverse([X|Xs],Acc,Ys) :- reverse(Xs,[X|Acc],Ys).

Insertion Sort % sort(A,B) % B is a sorted version of A sort([X|Xs],Ys) :- sort(Xs,Zs), insert(X,Zs,Ys). sort([ ],[ ]). % insert(A,B,C) % if B is a sorted list, then C is sorted % and contains all elements in B plus A insert(X,[ ],[X]). insert(X,[Y|Ys],[Y|Zs]) :- X > Y, insert(X,Ys,Zs). insert(X,[Y|Ys],[X,Y|Ys]) :- X =< Y.

Binary Trees % binary_tree(A) % A is a binary tree binary_tree(void). binary_tree(tree(Element,Left,Right)) :- binary_tree(Left), binary_tree(Right). % tree_member(A,B) % A is a node in the tree B tree_member(X,tree(X,_,_)). tree_member(X,tree(_,Left,_)) :- tree_member(X,Left). tree_member(X,tree(_,_,Right)) :- tree_member(X,Right).

Built In Predicates zsetof(X, p(X), S) ~ S is the set of all X such that p(X) zbagof(X, p(X), B) ~ B is the sequence of all X such that p(X) zfindall(X, p(X), B) B is the sequence of all X such that p(X)

Negation zProlog contains a weak form of negation called “negation as failure”. zWritten: \+ p(a) zA query ?- \+ p(a) succeeds if the query ?- p(a) fails finitely. zRobust only when the goal contains no variables. (Use only as a test!)

Example Negation on_top(X) :- \+ blocked(X). blocked(X) :- on(Y, X). on(a, b). %---------------------------- ?- on_top(a). yes b a