Chaps 1 3 Ai Prolog
Chaps 1 3 Ai Prolog
Prolog Language
Chapter 1 – Introduction to Prolog
1.1 Defining relation by facts
1.2 Defining relations by rules
1.3 Recursive rules
1.4 How Prolog answers questions
1.5 Declarative and Procedural Meaning of
Programs
1.1 Defining Relations by facts
Prolog (programming in logic) is a programming
language for symbolic, non-numeric
computation
Specially suite for solving problems that involve
objects and relations between objects.
When we tried to say tom is a parent of bob
tom and bob are objects and parent is a relation between object tom and bob
ann pat
jim
How to ask Prolog?
• ?- parent(bob,pat). yes
• ?-parent(liz,pat). no
• Using Variables – defined as Capital Letter
?-parent(X,liz).
X=tom
?-parent(bob,X).
X=ann if more than one answer, press ; to get others or press enter to stop
X = pat
?-parent(X,Y).
Using , to make conjunction (and)
Who grandparent of jim?
?- parent(Y,jim), parent(X,Y).
Using ; to make disjunction (or)
?-parent(Y,jim);parent(Y,pat).
Summary
• Use Prolog to define a relation
• User can ask back the relation defined in Prolog
program
• Prolog consists of Clauses.
• Each clause terminates with a full stop.
• There are concrete object or constants (such as
tom, ann) and are called atom
• General objects (such as X, Y –starting with
capitals) called variable.
• Questions to the system consists of one or more
goals.
1.2 Defining Relations by rules
Prolog clauses are three types: facts, rules and
questions
Facts declares things that are always
unconditionally true e.g male(bob).
Rules declare things that are true depending on
a give condition
e.g grandparent(X,Z):- parent(X,Y),parent(Y,Z).
Right-hand side is called a condition part or body
Left-hand side is called a conclusion or head
Questions – The user can ask the question what
things are true.
1.3 – Recursive rules
• Sometimes, we need to write X
• Predecessor case
– predecessor(X,Z):-parent(X,Z).
– predecessor(X,Z):-
parent(X,Y), predecessor(Y,Z).
………..
Putting Comment: Z
/* */ => between those /* and */ are comment
% => starting from % to end of line is comment
How prolog answer questions
Informal explanations
• Prolog seeks for the goals provided by the
user as questions
• Progol searches the successful path and
if it reaches unsuccessful branch, it
backtracks to previous one and tries to
apply alternative clauses
• That why, there is some important clues to
write program to run faster in later section.
Declarative and Procedural
Meaning of Programs
• Declarative Meaning – is concerned only with
how the relations is defined by the program or
what will be the output of the program
• Procedural Meaning – is concerned with how
the relations are evaluated by the prolog system
or how this output is obtained
Suggestion: Write program in declaration way and don’t worry about how does it
compute to obtain the goals. It would be Prolog program development
Summary
• Prolog programming consists of defining
relations and querying about relations
• A program consists of clauses, and there are
three types: facts, rules and questions.
• A relation can be specified by facts
• A procedure is a set of clauses about the same
relations.
• Two types of prolog meanings: declarative and
procedural meaning
Chapter 2- Syntax and Meaning of
Prolog Program
• Data Objects – is composed of simple
objects, structures, constants, variables, Data objects
atoms and numbers.
– Atoms and number
• Atoms can create in three ways:
(1) String of letters, digits and the underscore Simple objects structures
character, ‘_’, starting with a lower case letter
(2) String of special characters, e.g <--->
(3) String of characters enclosed in a single
quotes, like ‘Tom’
constants variables
– Variables – can create with string of letter,
digits and the underscore character, but
starting with upper case character or
underscore characters. atoms numbers
• E.g X, _x
• Anonymous variables, used as underscore,
eg. _
– ?-parent(X,_).
– Lexical Scope – all variables are scoped in
one clauses and all atoms are scoped to
the whole program
Structures
date
• Are objects that have several
components
1 feb 2006
• The components themselves can
be structure.
– e.g date(1,feb, 2006). or
date(Day,feb,2006).
a b b c