Prolog 1, Programming in Logic

Download Report

Transcript Prolog 1, Programming in Logic

Prolog
Programming in Logic
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/
2
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
3
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
4
Syllogisms in Prolog
Syllogism
Prolog
Socrates is a man.
man(socrates).
All men are mortal.
mortal(X) :- man(X).
Is Socrates mortal?
?- mortal(socrates).
5
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
6
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
7
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).
8
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.
9
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
10
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)
11
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).
12
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.”
13
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.
14
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.
15
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
16
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.)
17
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.
18
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
19
Calls as nested beads
loves(chuck, X) :- female(X), rich(X).
loves(chuck, X)
call
fail
female(X)
rich(X)
exit
redo
20
Additional answers





female(jane).
female(mary).
female(susan).
?- female(X).
X = jane ;
X = mary
Yes
female(X)
female(jane)
female(mary)
female(susan)
21
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.
22
The End
23