Prolog Programming in Logic

advertisement
Prolog
Programming in Logic
2
SWI-Prolog




SWI-Prolog is a good, standard Prolog for Windows
and Linux
Can be installed on Macintosh with a little more effort
(requires X11 and Mac developer tools)
It's licensed under GPL, therefore free
Downloadable from:
http://www.swi-prolog.org/
3
Syllogisms



“Prolog” is all about programming in logic.
Aristotle described syllogisms 2300 years ago
Sample syllogism:



Socrates is a man.
All men are mortal.
Therefore, Socrates is mortal.

Syllogisms are a form of logic. Can Prolog do them?

Note: If a word or term is in red, you should learn and remember its
meaning
4
Forward and backward reasoning


A syllogism gives two premises and a conclusion
Forward reasoning: Given some premises, ask “What can we
conclude?”


Backward reasoning: Given some premises and a conjectured
conclusion, try to derive the conclusion from the premises


Forward reasoning is inefficient when you are trying to get a particular
conclusion
You start from the conclusion and try to work backward to prove it
You use Prolog by asking it specific questions

This is backward reasoning -- from (potential) conclusions to facts
5
Syllogisms in Prolog
Syllogism
Prolog
Socrates is a man.
man(socrates).
All men are mortal.
mortal(X) :- man(X).
Is Socrates mortal?
?- mortal(socrates).
6
Facts, rules, and queries







Fact: Socrates is a man.
man(socrates).
Rule: All men are mortal.
mortal(X) :- man(X).
Query: Is Socrates mortal?
mortal(socrates).
Queries have the same form as facts
7
Variables and atoms




Variables begin with a capital letter:
X, Socrates, _result
A variable can have a value
An atom is a value; it just stands for itself
Atoms do not begin with a capital letter:
x, socrates
8
Running Prolog I


Create your “database” (program) in any editor
Save it as text only, with a .pl extension

If you have Perl installed, you may have to use the .pro
extension instead


Google “swi prolog file extension” for instructions
Here's the complete program:
man(socrates).
mortal(X) :- man(X).
9
Running Prolog II



SWI-Prolog is interpreted and completely interactive
You may be able to run your program by double-clicking
your .pl file
Here are two ways you can run the interpreter:



Double-click on the swipl file, or
If your PATH is set correctly, enter swipl at the command line
At the ?- prompt in the interpreter, enter:


Then, ask your question at the prompt:


?- consult('Complete path to your .pl file').
?- mortal(socrates).
Prolog responds:

true.
10
Prolog is a theorem prover


Prolog’s true. means “I can prove it”
Prolog’s false. really means “I can’t prove it”




It does not mean “I can prove it is untrue.”
?- mortal(plato).
false.
This is the closed world assumption: the Prolog
program knows everything it needs to know
Prolog supplies values for variables when it can

?- mortal(X).
X = socrates
11
Structures



A structure consists of a name and zero or more
arguments.
Omit the parentheses if there are no arguments
Example structures:



sunshine
man(socrates)
path(garden, south, sundial)
12
Base Clauses



A base clause is just a structure, terminated with a
period.
A base clause represents a simple fact.
Example base clauses:



debug_on.
loves(john, mary).
loves(mary, bill).
13
Nonbase Clauses


A nonbase clause is a structure, a turnstile
(meaning “if”), and a list of structures.
Example nonbase clauses:





:-
mortal(X) :- man(X).
mortal(X) :- woman(X).
happy(X) :- healthy(X), wealthy(X), wise(X).
The comma between structures means “and”
“X is happy if X is healthy, wealthy, and wise.”
14
Predicates



A predicate is a collection of clauses with the same
functor (name) and arity (number of arguments).
loves(john, mary).
loves(mary, bill).
loves(chuck, X) :- female(X), rich(X).
The scope of a variable (such as X) is the single
clause in which it occurs.
15
Programs



In Prolog, a program is just a collection of predicates.
Predicates can be in any order.
Clauses within a predicate are used in the order in
which they occur.
16
Atoms

You can make an atom containing any characters at all by
enclosing it in single quotes:


'C:\\My Documents\\examples.pl'
If you use double quotes, you will get a list of ASCII values



In a quoted atom, a single quote must be doubled or backslashed:


?- X = "Hello".
X = [72, 101, 108, 108, 111].
You probably don’t want this!
'Can''t, or won\'t?'
Backslashes in file names must also be doubled:


'C:\\My Documents\\examples.pl'
Better yet, use forward slashes in paths; every OS, including Windows,
understands this
17
Common problems




Capitalization is meaningful!
No space is allowed between a functor and its
argument list:
man(socrates), not man (socrates).
Double quotes indicate a list of ASCII character
values, not a string
Don’t forget the period! (But if you do, you can put it
on the next line.)
18
Backtracking





loves(chuck, X) :- female(X), rich(X).
female(jane).
female(mary).
rich(mary).
---------- Suppose we ask: loves(chuck, X).
 female(X) = female(jane), X = jane.
 rich(jane) fails.
 female(X) = female(mary), X = mary.
 rich(mary) succeeds.
19
Backtracking and Beads

Each Prolog call is like a “bead” in a string of beads:
call
fail


exit
redo
Each structure has four ports: call, exit, redo, fail
Exit ports connect to call ports;
fail ports connect to redo ports
20
Calls as nested beads
loves(chuck, X) :- female(X), rich(X).
loves(chuck, X)
call
fail
female(X)
rich(X)
exit
redo
21
Additional answers





female(jane).
female(mary).
female(susan).
?- female(X).
X = jane ;
X = mary
Yes
female(X)
female(jane)
female(mary)
female(susan)
22
Negation

Since Prolog is programming in logic, you would expect it to
support negation (the “not” operator)


It does (the operator is \+), but negation isn’t as useful as you might
think
Consider \+loves(chuck, X)


You might think that this would instantiate X with a value that chuck
doesn’t love
Here’s what really happens:




Prolog tries to find a solution (an X) for loves(chuck, X)
If loves(chuck, X) succeeds, \+loves(chuck, X) fails, so the
instantiation for X is discarded
If loves(chuck, X) fails, there is no instantiation for X, but
\+loves(chuck, X) succeeds
Hence, whether the test succeeds or fails, X doesn’t get a value
23
Readings




A clause can be read declaratively (as a statement of
fact) or procedurally (as a list of things to try to do)
loves(chuck, X) :- female(X), rich(X).
Declarative reading: Chuck loves X if X is female and
rich.
Approximate procedural reading:
To find an X that Chuck loves:



First try to find a female X (fail and backtrack if you can’t)
Given a particular value for X, try to show that X is rich (fail
and backtrack if you can’t)
Declarative readings are almost always preferred.
24
The End
that that is is that that is not is not is not that so
That that is, is; that that is not, is not.
Is not that so?
25
Download