l7
l7
Introduction to Prolog
Prolog – PROgramming in LOGic
• Declarative and logical programming language
• Built-in inference mechanism based on
resolution
SWI-Prolog
• Download from www.swi-prolog.org
– Log in to lts1
• You can get to this machine from EECS or VPNd in
• If off campus, ssh to ssh-server.eecs.wsu.edu, then connect to lts1
– Type pl to start.
– Type halt. to exit.
pl
Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13)
Copyright (c) 1990-2003 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
?- likes(mary,food).
Yes
?- likes(X,food).
X = mary /* press ; to get next answer, press return to finish */
Yes
Interactive Definitions (soc.pl)
To enter facts and/or rules directly, type:
| ?- consult(user).
|
You get a new prompt as above (|). At this point you would enter your facts and rules. For example,
| man(socrates).
| mortal(X) :- man(X).
| end_of_file.
| ?-
When done entering facts and rules, just type 'end_of_file.', and you will return to the original
prompt.
Now we can ask queries of our facts and rules. For example,
| ?- man(socrates).
yes
| ?- mortal(X).
no
Logical Operators
a :- b. /* a if b */
a :- b,c. /* a if b and c. */
a :- b;c. /* a if b or c. */
a :- not b. /* a if b fails */
a :- b -> c;d. /* a if (if b then c else d) */
Compound Query (dog.pl)
likes(X,Y), food(Y).
parent(charles1, george1).
Queries
• Who was the parent of Charles I?
parent(charles1, Parent).
Queries
• Who were the children of Charles I?
parent(Child, charles1).
Create Rules (rules.pl, neg1.pl, neg2.pl)
• M is the mother of P if she is a parent of P and
is female.
• F is the father of P if he is a parent of P and is
male
• X is a sibling of Y if they both have the same
parent.
• sister, brother, aunt, uncle, grandparent,
cousin
Prolog Data Objects
• Atoms
– String of letters and digits
– Start with lower case letter
– yes, parent, female, tom, tom_jones
– String of characters enclosed in single quotes ('Tom Jones')
• Numbers [-][0-9]*[.][0-9]*
– 2.71, -100.5, 0.5 (but not .5)
• Variables
– String of letters and digits
– Start with upper case letter (or _, anonymous variable)
– Universally quantified
• Structures
– functor(arg1, arg2, )
– date(2, october, 1964)
– Arguments can be constants, variables, or other structures
Arithmetic Operators
Operators +, -, *, /, sqrt, exp, cos, and so forth are
available. However, such expressions will not
“match” a variable.
prime(2).
prime(3).
prime(5).
...
prime(1+1) will fail, because cannot be unified with
any entry in the KB.
Comparison Operators
X = Y % X and Y are unifiable
T1 == T2 % True if T1 and T2 are identical (names of variables are the same)
T1 \== T2 % True if T1 and T2 are not identical
E1 =:= E2 % True if values of expressions E1 and E2 are equal
E1 =\= E2 % True if values of expressions E1 and E2 are not equal
E1 < E2 % True if numeric value of E1 is < numeric value of E2
Similar for operators =<, >, and >=
positive(N) :- N>0.
?- N is 1+1.
?- N is 1+1, P is N*2, Q is P+P.
?- N is X+1.
?- I is I+1.
?- I is 6, I is I+1. /* Creates an error */
?- I is 6, J is I+1.
Example Program (factorial.pl)
?- [factorial]. /* Could also type pl -s factorial.pl at start */
% factorial.pl compiled 0.00 sec, 628 bytes
Yes
?- listing(factorial/2).
factorial(0, 1).
factorial(A, B) :-
A>0,
C is A-1,
factorial(C, D),
B is A*D.
Yes
?- factorial(10,What).
Yes
?- halt.
Arithmetic Examples (arithmetic.pl)
• The result of adding 1 to a number
• The function signum(x) which is x-1 if x > 0,
and 0 otherwise.
• The maximum of two numbers
• The maximum of three numbers
• The absolute value of a number
Negation
How can Prolog handle verification
that a goal is not entailed by Then
the knowledge base? ?- bachelor(henry).
The not predicate. yes
?- bachelor(tom).
Consider the program: no
bachelor(P) :- male(P), ?- bachelor(Who).
not(married(P)). Who= henry ;
male(henry). no
male(tom). ?- not(married(Who)).
married(tom). no.
Negation Examples
cube(C,N) :- C is N * N * N.
For interactive input:
cube :- read(X), calc(X).
calc(stop) :- !.
calc(X) :- C is X * X * X, write(C), cube.
Now will read until see “stop”.
Files
browse(File) :-
seeing(Old), /* save for later */
see(File), /* open this file */
repeat,
read(Data), /* read from File */
process(Data),
seen, /* close File */
see(Old), /* previous read source */
!. /* stop now */
process(end-of-file) :- !.
process(Data) :- write(Data), nl, fail.
Interactive Version
browse(File) :-
seeing(Old), /* save for later */
see(user),
write(‘Enter name of file to browse: ‘),
read(File),
see(File), /* open this file */
repeat,
read(Data), /* read from File */
process(Data),
seen, /* close File */
see(Old), /* previous read source */
Green’s Trick Revisited
1. studyingwith(Sally,Morton)
2. at(Morton,Cub)
3. -studyingwith(x3,y3) v -at(y3,z3) v at(x3,z3)
4. -at(x4,y4) v reach(x4,phone(y4))
Where can Sally be reached?
Exists x reach(Sally, x)
Negate and clausify: -reach(Sally,x5)
Form disjunct with opposite and add to DB:
5. –reach(Sally, x5) v reach(Sally, x5)
Green’s Trick Revisited
1. studyingwith(Sally,Morton)
2. at(Morton,CUB)
3. -studyingwith(x3,y3) v -at(y3,z3) v at(x3,z3)
4. -at(x4,y4) v reach(x4,phone(y4))
5. –reach(Sally, x5) v reach(Sally, x5)
6. [4,5 x4/Sally x5/phone(y4)] –
at(Sally, y6) v reach(Sally, phone(y6))
7. [3,6 x3/Sally z3/y6] –
studyingwith(Sally, y7) v –at(y7,a7) v reach(Sally, phone(a7)
8. [1,7 y7/Morton] –at(Morton,a8) v reach(Sally, phone(a8))
9. [2,8 a8/CUB] reach(Sally, phone(CUB))
Green’s Trick in Prolog (s.pl)
studyingwith(sally,morton).
at(morton,cub).
at(X,Z) :- studyingwith(X,Y), at(Y,Z).
reach(X,phone(Y)) :- at(X,Y).
• assert(Term)
– Add fact or clause to database.
• retract(Term)
– Remove all facts or clauses in the database that
unify with Term.
• Can see results in listing.
Lists in Prolog
• Elements of lists are any valid Prolog data object
• Elements are terms separated by commas, do not have to be
same type
• Vertical bar separates head (first) from tail (rest)
• The head of [john, mary, pat] is john
• The tail of [john, mary, pat] is [mary, pat]
• The representation of the combination is Head | Tail (in our
example, [john | [mary, pat]] or [john | [mary | [pat | []]]])
• Member function
member(X, L) :- L = [X|_].
member(X, L) :- L = [A|B], member(X,B).
Prolog list manipulation [sum.pl, temp.pl]
first([X|Y],X).
rest([X|Y],Y).
addfirst(X,R,[X|R]).
means
The head (first) of [X|Y] is X.
The tail (rest) of [X|Y] is Y.
Putting X at the head and Y as the tail constructs
(addfirst) the list [X|R].