Upload
shahnimit82
View
119
Download
1
Tags:
Embed Size (px)
Citation preview
12 March'09 Turbo Programming 1
Seminar onPROLOG
Subject: Intelligent Systems
Prepared by:Nirali RathodME-II (ACR)Roll No.:173
12 March'09 Turbo Programming 2
Contents
Introduction to Prolog How to use Turbo Prolog Facts, Objects and Predicates Prolog variables Using rules Simple input and output
12 March'09 Turbo Programming 3
What is prolog
“PROgramming in LOGic” Developed by Alain Colmerauer and P. Roussel
in 1972 at the university of Marseilles in France. Supports formal symbolic reasoning
12 March'09 Turbo Programming 4
Features of Prolog
1. Can compile stand alone programs.2. A full complement of standard predicates is
available.3. A functional interface to other languages is
provided.4. Declared variables are used to provide more
secure development control.
12 March'09 Turbo Programming 5
Conti…
5. Both integer and floating point arithmetic supported.
6. Program development, compilation and debugging is very easy because of integrated editor.
12 March'09 Turbo Programming 6
How Prolog is different
Turbo prolog, like other implementations of Prolog,is an object oriented language.
Prolog uses only data about objects and their relationships.
User defines the goal,and the computer must find both the procedure and solution.
Prolog program is a collection of data or facts and their relationships among these facts.-program is database.
12 March'09 Turbo Programming 7
How Prolog is different
What is known about the problem is stored as data.
The user defines a goal, or hypothesis, and the program, using formal reasoning, attempts to prove or disprove the goal based on the known data.
12 March'09 Turbo Programming 8
Comparison
Procedural languages
Use previously defined procedures to solve problems
Most efficient at numerical processing
Systems created and maintained by programmers
Use structured programming
Object-oriented languages
Use heuristics to solve problems
Most efficient at formal reasoning
Systems developed and maintained by knowledge engineers
Interactive and cyclic development
12 March'09 Turbo Programming 9
Starting Turbo Prolog
12 March'09 Turbo Programming 10
Fundamental of Prolog
Declaring some facts Defining some rules Asking question
12 March'09 Turbo Programming 11
Starting with Prolog
Editor window: It is used to create program. Dialog window: when the program is executing,
output will be in the Dialog window. Message window: It keeps you up to date on
processing activity. Trace window: It is useful for finding problems in
the programs you create.
12 March'09 Turbo Programming 12
Editor: to create or edit program
12 March'09 Turbo Programming 13
Dialog: output
12 March'09 Turbo Programming 14
Message: processing activity
12 March'09 Turbo Programming 15
trace: finding problems in the program
12 March'09 Turbo Programming 16
Six options
Edit Files Compile Run Options Setup
12 March'09 Turbo Programming 17
Creating a sample program
Select the edit mode and enter the program. Enter the text or make corrections as necessary. Save the program to disk using the file save
option. Compile the program. Execute the program.
12 March'09 Turbo Programming 18
12 March'09 Turbo Programming 19
12 March'09 Turbo Programming 20
12 March'09 Turbo Programming 21
12 March'09 Turbo Programming 22
12 March'09 Turbo Programming 23
Expressing facts
Main part of prolog program consists of collection of knowledge about specific subject.
12 March'09 Turbo Programming 24
Expressing facts
Main part of prolog program consists of collection of knowledge about specific subject.
database
Rules
facts
12 March'09 Turbo Programming 25
Expressing facts
Prolog permits to describe facts in terms of symbolic relationships.
E.g.. Right speaker is not emitting sound: English : The right speaker is dead. Prolog : is(right_speaker,dead)
12 March'09 Turbo Programming 26
Expressing facts
Prolog permits to describe facts in terms of symbolic relationships.
E.g.. Right speaker is not emitting sound: English : The right speaker is dead. Prolog : is(right_speaker,dead).
Relation: Defines the way in which a collection of Objects belong together
12 March'09 Turbo Programming 27
Expressing facts
Prolog permits to describe facts in terms of symbolic relationships.
E.g.. Right speaker is not emitting sound: English : The right speaker is dead. Prolog : is(right_speaker,dead).
Objects: Name of an element of certain typeRepresents entity or property of entity(Elements within parentheses are arguments of Predicate)
12 March'09 Turbo Programming 28
Expressing facts
Prolog permits to describe facts in terms of symbolic relationships.
E.g.. Right speaker is not emitting sound: English : The right speaker is dead. Prolog : is(right_speaker,dead).
Predicate :function with a value true or false.Express property of a relationship
12 March'09 Turbo Programming 29
Expressing facts
Prolog permits to describe facts in terms of symbolic relationships.
E.g.. Right speaker is not emitting sound: English : The right speaker is dead. Prolog : is(right_speaker,dead).
Predicate + period = complete clause
12 March'09 Turbo Programming 30
clauses
facts Rules
predicates Periods(.)
relations
arguments
objects
variables
Relationship of clauses, predicates, relations and objects
12 March'09 Turbo Programming 31
Turbo prolog data types
variables Non variables
objects other
symbol
string
integer
real
char
file
list
Compound object
Turbo prolog data types
12 March'09 Turbo Programming 32
Prolog Variables
Variable can be used in Turbo Prolog clause or goal to specify an unknown quantity.
A variable name must be begin with a capital letter.
For example,
symptom(Disease,runny_nose)
variable is Disease.
12 March'09 Turbo Programming 33
Turbo prolog data types
variables Non variables
objects other
symbol
string
integer
real
char
file
list
Compound object
Turbo prolog data types
Must begin with an upper case letterEg. Age Patient
12 March'09 Turbo Programming 34
12 March'09 Turbo Programming 35
Variables:
No global variable. All variables are local to the clause of which they
are a part.
12 March'09 Turbo Programming 36
Unification
The process by which prolog tries to match a term against the facts or the heads of other rules in an effort to prove goal is called unification.
Predicates unify each other if : They have the same relation name. They have the same number of
arguments. All argument pairs unify with each other.
12 March'09 Turbo Programming 37
Unification
The process by which prolog tries to match a term against the facts or the heads of other rules in an effort to prove goal is called unification
Predicates unify each other if : They have the same relation name. They have the same number of
arguments. All argument pairs unify with each other.
Similar to parameter passing in programming
12 March'09 Turbo Programming 38
Compound goals
Goal : symptom(Diesease,runny_nose)and
symptom(Diesease,mild_body_ache)
Disease = cold
1 solution
Goal :
12 March'09 Turbo Programming 39
12 March'09 Turbo Programming 40
Symptom(Diesease,runny_nose)
Clause 1
Clause 2
Clause 3
Clause 4
Clause 5
Symptom(cold,runny_nose)
Clause 1
Clause 2
Clause 3
Symptom(cold,mild_body_ache)
Goal succeeds
Clause 1
Clause 2
12 March'09 Turbo Programming 41
Backtracking
If any condition in the chain fails, Prolog backtracks to previous condition, moves relentlessly forward and backward
through the conditions, trying every available binding in an attempt to
get the goal to succeed as many ways as
possible. It find its own way.
12 March'09 Turbo Programming 42
12 March'09 Turbo Programming 43
12 March'09 Turbo Programming 44
Using Rules
A rule is an expression that indicates the truth of a particular fact depending upon one or more facts
X is sister of Y If X is female And parents of X are M and F And parents of Y are M and F
12 March'09 Turbo Programming 45
Using Rules
The process of using rules to solve problems is called formal reasoning
It gives prolog the ability of decision making.
12 March'09 Turbo Programming 46
Arithmetic Operators
+ addition - subtraction * multiplication / real division // integer division ** power Goal : X is 3*4.
X = 12yes
12 March'09 Turbo Programming 47
Logical Operators
a :- b. /* a if b */ a :- b,c. /* a if b and c.*/ a :- b;c. /* a if b or c.*/ a :- \++ b. /* a if b is not provable*/ a :- not b. /* a if b fails*/ a :- b -> c;d. /* a if (if b then c else d)*/
12 March'09 Turbo Programming 48
Execution Control
Execution
Top to bottom
Left to right
12 March'09 Turbo Programming 49
Write predicate
test:-
write (“This is an example”),
write(“of multiple write statements.”).
Displays:
This is an example of multiple write statements.
12 March'09 Turbo Programming 50
Output predicates
Three output predicates: 1. write
2. writedevice
3. writef
12 March'09 Turbo Programming 51
The write predicate
go :-
hypothesis(Patient,Disease),
write(Patient,” probably has “,Disease,”.”) ,nl.
New lineAnother method \n
Go will unify with the
Conclusion of new rule
Unify with conclusion of your new rule
12 March'09 Turbo Programming 52
Writef Predicate
Used to force alignment of numbers or text Format:
writef(format,E1,E2,…,En)
ex. writef(“%-10#5.0$%3.2\n”,fan,23,3.1)
displays
fan #23$3.10
12 March'09 Turbo Programming 53
Input predicates
readln : string or symbol readchar : character readint : integer readreal : real
12 March'09 Turbo Programming 54
readchar Predicate Symptom(patient,feve
r):-
write(“Does the “,patient,” have a fever(y/n)?”),
readchar(Reply),nl
Reply=‘y’.
Symptom(patient,fever):-
write(“Does the “,patient,” have a fever(yes/no)?”),
readln(Reply)
Reply=“yes”.
ReadlnPredicate
12 March'09 Turbo Programming 55
readintPredicate Write(“what
is”,patient,” ’s age?”).
readint(Age),
Age>=12,
Write(Patient,”cannot be evaluated with”),nl
Write(“this system.”).
Write(“what is the real price of ”,Item,”?”),
readreal(Price).
readrealPredicate
12 March'09 Turbo Programming 56
Applications of Prolog
Prolog is used in applications that requires formal reasoning.
Basic Applications: Expert System Natural Language Processing Robotics Gaming and Simulation
12 March'09 Turbo Programming 57
Natural Language Processing Using Prolog, knowledge about human language
is expressed in formal rules and facts. The computer can then begin a dialog with the
user, asking questions about the problem and interpreting the user’s answer.
12 March'09 Turbo Programming 58
Robotics
Robotics is a branch of artificial intelligence concerned with enabling computers to see and manipulate objects in their environment.
Prolog facilitates the development of robotic control programs that can use input data from sensors to control manipulators.
A robot can then apply formal reasoning to decisions, much as a human being does.
12 March'09 Turbo Programming 59
Gaming and Simulation
Prolog is ideal for games and simulations (cognitive modeling) involving formal reasoning.
Most games and simulation employ a set of logical rules that control the play or action which is very adaptable to Prolog programming.
Classical game such as
N Queen Problem is written in Prolog.
12 March'09 Turbo Programming 60
Expert system
Expert system are programs that use inference techniques that involves formal reasoning normally performed by a human expert to solve problems in specific area of knowledge.
Each expert system can advise, diagnose, analyze and categorize using a previously defined knowledge base.
The knowledge base is a collection of rule and facts which can be written in Prolog.
12 March'09 Turbo Programming 61
Prolog shines in two major areas: search Pattern Matching
12 March'09 Turbo Programming 62
Building an Expert System
Defining the goal and domain Getting the facts Charting the facts Clustering Writing the program Testing the program
12 March'09 Turbo Programming 63
Disadvantages
Very inefficient If trying to use it for numerical or string processing involving known procedures.
Does not support virtual memory
12 March'09 Turbo Programming 64