Prolog - A Logic Programming Language
Prolog - A Logic Programming Language
Programming Language
The name of PROLOG is from
PROgramming in LOGic
Background - Logic
Logic:
In logical formulas
constants
=>
loves(mary, tom)
variables
X Y (loves(X,Y)
mother_of(X,Y))
---------------------------------- ----------------------------------In Prolog, everything is
written as a logical formula
PROLOG
A programming language for symbolic, non-numeric
computation
Well suited for solving problems that involve object
and relations between object
Using predicate logic
It only consists of three types of statements:
An Example Program:
defining family relations
The family tree can be
defined by Prolog like this:
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(liz, pat).
parent(bob, pat).
parent(pat, jim).
7 facts about who is whos parent
tom
pam
liz
bob
ann
pat
jim
A full stop
?- parent(bob, pat).
Having found this is an unconditional true fact,
Prolog will answer
yes
?- parent(ann,pat).
no
?- parent(X, liz).
X = tom
.
we can ask an even broader question!
Family Tree Example
cont
.
composed query
Family Tree Example
cont
parent
grand
parent
parent
, means and
This is actually asking who jims grandparent is
.
Extending the program by adding rules
Family Tree Example
cont
a :- b , c.
It read as
if b and c are true then a is true,
a is defined as b and c,
in order to solve a, solve b then solve c
.
more examples of writing/using rules
Family Tree Example
cont
case letter
Anything beginning with an upper-case letter
is a variable (e.g. Tom)
Always use a full stop to finish a statement
, means and
; means or
:- means only-if
(a :- b. means b is true only if a is true)
0-9
s2
start_state(s1).
halt_state(s2).
start_state(s1).
halt_state(s2).
trans(s1,0, s2).
trans(s1,1, s2).
......
trans(s1,9,s2).
trans(s2,0, s2).
trans(s2,1, s2).
......
trans(s2,9,s2).
or
both works
If X is 0,1,2,3,4,5,6,7,8,or 9,
then X is a digit
is_digit(X):- X=0; X=1; X=2;X=3; X=4; X=5;
X=6;X=7; X=8; X=9.
?- is_digit(3).
Yes
?- is_digit(X).
X=0;
X=1;
Things to Remember
prolog
| ?- [filename].
| ?- fa.
| ?- pda.
Control-D
my
answers
correct
answers
/5]
comments