0% found this document useful (0 votes)
90 views16 pages

Prolog - A Logic Programming Language

PROLOG is a logic programming language based on first-order predicate logic. It uses logical formulas to represent facts and rules about objects and the relationships between them. PROLOG programs consist of facts, rules, and queries. Facts are unconditional statements, rules are conditional statements, and queries are goals that need to be proved. PROLOG is well-suited for solving problems involving objects and relations between objects using predicate logic. Examples show how family tree relations can be defined and queried using PROLOG facts, rules, and queries.

Uploaded by

nov30111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views16 pages

Prolog - A Logic Programming Language

PROLOG is a logic programming language based on first-order predicate logic. It uses logical formulas to represent facts and rules about objects and the relationships between them. PROLOG programs consist of facts, rules, and queries. Facts are unconditional statements, rules are conditional statements, and queries are goals that need to be proved. PROLOG is well-suited for solving problems involving objects and relations between objects using predicate logic. Examples show how family tree relations can be defined and queried using PROLOG facts, rules, and queries.

Uploaded by

nov30111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

PROLOG - A Logic

Programming Language
The name of PROLOG is from
PROgramming in LOGic

Background - Logic
Logic:

a tool for reasoning.


Reasoning:
a process of drawing a conclusion from a set of facts
and observations.
Logical formulas:

a formal way to describe things (assigned a truth


value)
Logical Operators: and
or
not imply

Logical Formulas - examples


In English

In logical formulas
constants

Mary loves Tom

=>

Every mother loves


her children
=>

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:

Facts - unconditional statement e.g loves(mary, tom)


Rules - conditional statement e.g.
loves(X,Y)mother_of(X,Y)
Queries - something need to be proved (or goal needs
to be solved), e.g.
?- loves(X, tom).

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

Family Tree Example cont.


We now can make queries about anything regarding the
parent relation

?- parent(bob, pat).
Having found this is an unconditional true fact,
Prolog will answer

yes
?- parent(ann,pat).
no
?- parent(X, liz).
X = tom

Who is Lizs parent?

.
we can ask an even broader question!
Family Tree Example

cont

Find X and Y such that


X is a parent of Y
?- parent(X,Y).
There are more than one answer, Prolog will
display one at the time
X = pam Y = bob ;
X = tom Y = bob ;
......

.
composed query
Family Tree Example

cont

Find X and Y such that


X is a parent of Y and Y is jims parent
?- parent(X,Y), parent(Y,jim).
X=bob Y=pat

parent
grand
parent
parent

, means and
This is actually asking who jims grandparent is

.
Extending the program by adding rules
Family Tree Example

cont

We know that a person's parents parent is the


person's grandparent
In Prolog, this can be written as
grandparent (X,Z) :- parent(X,Y), parent(Y,Z).

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

/* define the sibling relation


X and Y are siblings as long as they share a
same parent
*/
sibling(X,Y) :- parent(Z,X), parent(Z,Y),
haschild(X) :- parent(X,Y).
Try ?- sibling(X,Y). To check what you get

A Summary on Prologs Syntax


Program name must be started with a lower-

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)

Worksheet 4 using Prolog to define your finite


automata and pushdown automata. Here is where you start.
0-9
s1

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

Why ; is used, not ,?

or
both works

trans(s1,X, s2):- is_digit(X).


trans(s2,X, s2):- is_digit(X).
is_digit(X):- X=0; X=1; X=2;
X=3; X=4; X=5; X=6;
X=7; X=8; X=9.

I have provided two simulators, fa and pda


which runs any given fa and pda.

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

to enter Prolog system


to load a file into Prolog
Don't type suffix .pl
to start FA simulator
Don't forget the full stop
to start PDA simulator
to quit from Prolog, FA, or PDA

The Provided Files


engine.pl - a prolog source file contains FA/PDA
simulator
integers.pl - an FA to recognise integers
brackets.pl - a PDA to recognise strings of matched
brackets
Rest of files are examples from the handout:
p20q3.pl p31q2.pl p32q1.pl p20q1.pl p20q4.pl
p31q4.pl p20q2.pl p31q3.pl palindrome_npda.pl

Memo for In-class test 8 [


questions
1
2
3
4
5

my
answers

correct
answers

/5]

comments

You might also like