- 1456 Views
- Uploaded on
- Presentation posted in: General

Prolog for Dummies

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Prolog for Dummies

Ulf Nilsson

Dept of Computer and Information Science

Linköping University

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.

- Adam is a parent of Bill
- Paris is the capital of France
- 5 is greater than 2 plus 2
- X times 1 is equal to X
- X is a subset of Y
- 5 is the maximumof 2 and 5
- There is an edge from a to b

- Adam is a parent
- Adam is male
- X plus 1 is non-zero
- Paris is a capital
- Grass is green
- The music was loud

- Who is the father of Bill?
- Is there an edge from a to b?
- Which town is a capital?
- Who is male?

- Constantsadam, paris, 5, 3.14, [], ´Adam’, ...
- VariablesX, Y, List, _12, _, ...
- Function symbolsplus/2, +/2, f/1, ...
- Predicate symbolscapital/2, greater/2, non_zero/1, >/2, ...

Terms represent individuals

- Constants
- Variables
- Compound terms
- E.g. paris, X, plus(2,3), plus(2,plus(3,4))
- Infix notation: 2+3

Atomic formulas describe relations:

- If p is a predicate letter of arity n and t1,...,tn are terms then p(t1,...,tn) is an atomic formula.
- E.g. capital(paris,france) greater(X,2)
- Infix notation: X > 2

- A logic program is a set of clauses:
- facts
- rules

- The program is used to answer queries.

- A fact is an expression of the form:
A.

where A is an atomic formula.

- Examples:
edge(a, X).

parent(adam, bill).

- Consider a fact A.
- Declarative (logical) reading:
For all variables, A is true.

- Procedural (operational) reading:
A is solved.

- A rule is an expression of the form:
A0 :- A1, ... , An.

where each Ai is an atomic formula.

- Examples:
path(X,Y) :- edge(X,Y).

father(X,Y) :- parent(X,Y), male(X).

- Consider a rule A0 :- A1, ... , An.
- Declarative (logical) reading:
For all variables, A0 if A1 and...and An.

- Procedural (operational) reading:
To solve A0, first solve A1, then A2 etc.

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

- A query is an expression of the form:
?- A1, ..., An.

where n=0,1,2,... and A1, ..., An are atomic formulas.

- Examples:
?- father(X, bill).

?- parent(X, bill), male(X).

- Consider a query ?- A1, ... , An.
- Declarative (logical) reading:
Are there variables such that A1 and...and An?

- Procedural (operational) reading:
First solve A1, then A2 etc

?- B1,...,Bm,A2,...,An.

?- A1,A2,...,An.

A1 :- B1,...,Bm.

where

A1 :- B1,...,Bm is an instantiated program clause.

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

?- parent(adam,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).

parent(X,Y) :-

father(X,Y).

parent(X,Y) :-

mother(X,Y).

father(adam,bill).

mother(anne,bill).

?- parent(anne,bill)

B0 :- B1,...,Bm.

?- A1= B0, B1,...,Bm,A2,...,An.

?- (B1,...,Bm,A2,...,An)q.

where:

- B0 :- B1,...,Bm is a renamed program clause.

- q is a solution to the equation A1 = B0.

?- A1,A2,...,An.

?- X=X1, bill=Y1, father(X1,Y1).

?- father(X,bill).

?- X=adam, bill=bill.

?- true.

Answer: X=adam

?- parent(X,bill).

parent(X1,Y1) :- father(X1,Y1).

father(adam,bill).

X=adam

?- p(bill,Y).

?- f(X,Z1), p(Z1,Y).

?- X=X2, Z1=Y2, f(X2,Y2), p(Z1,Y).

?- bill=X3, Y=Y3, f(X3,Y3).

?- true.

?- X=X1, Y=Y1, p(X1,Z1), p(Z1,Y1).

?- f(bill,Y).

?- bill=bill, Y=carl.

?- X=adam,Z1=bill, p(Z1,Y).

?- p(X,Z1), p(Z1,Y).

Y=carl

?- gp(X,Y).

f(bill,carl).

p(X3,Y3) :- f(X3,Y3).

p(X2,Y2) :- f(X2,Y2).

gp(X1,Y1) :- p(X1,Z1),p(Z1,Y1).

f(adam,bill).

X=bill

?- p(carl,Y).

?- fail.

?- f(X,Z1), p(Z1,Y).

?- X=X2, Z1=Y2, f(X2,Y2), p(Z1,Y).

?- X=bill,Z1=carl, p(Z1,Y).

?- p(X,Z1), p(Z1,Y).

?- X=X1, Y=Y1, p(X1,Z1), p(Z1,Y1).

?- f(carl,Y).

?- carl=X3, Y=Y3, f(X3,Y3).

?- gp(X,Y).

p(X3,Y3) :- f(X3,Y3).

f(bill,carl).

p(X2,Y2) :- f(X2,Y2).

gp(X1,Y1) :- p(X1,Z1),p(Z1,Y1).

FAILURE!!!

?- p(X,Z),p(Z,Y).

X=anne

X=adam

?- f(X,Z),p(Z,Y).

?- m(X,Z),p(Z,Y).

?- p(bill,Y).

?- p(bill,Y).

?- p(carl,Y).

?- f(carl,Y).

?- m(carl,Y).

?- f(bill,Y).

?- m(bill,Y).

?- f(bill,Y).

?- m(bill,Y).

?- fail.

?- fail.

?- true.

Y=carl

?- fail.

?- true.

Y=carl

?- fail.

?- gp(X,Y).

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

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

...

b

d

f

a

c

e

g

edge(a,b).

edge(a,c).

edge(b,d).

edge(c,d).

edge(d,e).

edge(f,g).

path(Node,Node).

path(Node1,Node3) :-

edge(Node1,Node2),

path(Node2,Node3).

.

.

a

a

.

b

b

c

[]

c

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

- The empty list: []
- A non-empty list: .(X,Y) or [X|Y]
Syntactic Sugar:

- [b] instead of [b|[]] and .(b, [])
- [a,b] instead of [a|[b]] and [a|[b|[]]]
- [a,b|X] instead of [a|[b|X]]

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

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

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

- setof(X, p(X), S)
~ S is the set of all X such that p(X)

- bagof(X, p(X), B)
~ B is the sequence of all X such that p(X)

- findall(X, p(X), B)
B is the sequence of all X such that p(X)

- Prolog contains a weak form of negation
called “negation as failure”.

- Written: \+ p(a)
- A query ?- \+ p(a) succeeds if the query ?- p(a) fails finitely.
- Robust only when the goal contains no variables. (Use only as a test!)

a

b

on_top(X) :- \+ blocked(X).

blocked(X) :- on(Y, X).

on(a, b).

%----------------------------

?- on_top(a).

yes