64
12 March'09 Turbo Programming 1 Seminar on PROLOG Subject: Intelligent Systems Prepared by: Nirali Rathod ME-II (ACR) Roll No.:173

Turbo Prolog 173 ACR

Embed Size (px)

Citation preview

Page 1: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 1

Seminar onPROLOG

Subject: Intelligent Systems

Prepared by:Nirali RathodME-II (ACR)Roll No.:173

Page 2: Turbo Prolog 173 ACR

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

Page 3: Turbo Prolog 173 ACR

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

Page 4: Turbo Prolog 173 ACR

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.

Page 5: Turbo Prolog 173 ACR

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.

Page 6: Turbo Prolog 173 ACR

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.

Page 7: Turbo Prolog 173 ACR

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.

Page 8: Turbo Prolog 173 ACR

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

Page 9: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 9

Starting Turbo Prolog

Page 10: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 10

Fundamental of Prolog

Declaring some facts Defining some rules Asking question

Page 11: Turbo Prolog 173 ACR

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.

Page 12: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 12

Editor: to create or edit program

Page 13: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 13

Dialog: output

Page 14: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 14

Message: processing activity

Page 15: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 15

trace: finding problems in the program

Page 16: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 16

Six options

Edit Files Compile Run Options Setup

Page 17: Turbo Prolog 173 ACR

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.

Page 18: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 18

Page 19: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 19

Page 20: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 20

Page 21: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 21

Page 22: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 22

Page 23: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 23

Expressing facts

Main part of prolog program consists of collection of knowledge about specific subject.

Page 24: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 24

Expressing facts

Main part of prolog program consists of collection of knowledge about specific subject.

database

Rules

facts

Page 25: Turbo Prolog 173 ACR

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)

Page 26: Turbo Prolog 173 ACR

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

Page 27: Turbo Prolog 173 ACR

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)

Page 28: Turbo Prolog 173 ACR

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

Page 29: Turbo Prolog 173 ACR

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

Page 30: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 30

clauses

facts Rules

predicates Periods(.)

relations

arguments

objects

variables

Relationship of clauses, predicates, relations and objects

Page 31: Turbo Prolog 173 ACR

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

Page 32: Turbo Prolog 173 ACR

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.

Page 33: Turbo Prolog 173 ACR

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

Page 34: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 34

Page 35: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 35

Variables:

No global variable. All variables are local to the clause of which they

are a part.

Page 36: Turbo Prolog 173 ACR

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.

Page 37: Turbo Prolog 173 ACR

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

Page 38: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 38

Compound goals

Goal : symptom(Diesease,runny_nose)and

symptom(Diesease,mild_body_ache)

Disease = cold

1 solution

Goal :

Page 39: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 39

Page 40: Turbo Prolog 173 ACR

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

Page 41: Turbo Prolog 173 ACR

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.

Page 42: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 42

Page 43: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 43

Page 44: Turbo Prolog 173 ACR

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

Page 45: Turbo Prolog 173 ACR

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.

Page 46: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 46

Arithmetic Operators

+ addition - subtraction * multiplication / real division // integer division ** power Goal : X is 3*4.

X = 12yes

Page 47: Turbo Prolog 173 ACR

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)*/

Page 48: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 48

Execution Control

Execution

Top to bottom

Left to right

Page 49: Turbo Prolog 173 ACR

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.

Page 50: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 50

Output predicates

Three output predicates: 1. write

2. writedevice

3. writef

Page 51: Turbo Prolog 173 ACR

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

Page 52: Turbo Prolog 173 ACR

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

Page 53: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 53

Input predicates

readln : string or symbol readchar : character readint : integer readreal : real

Page 54: Turbo Prolog 173 ACR

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

Page 55: Turbo Prolog 173 ACR

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

Page 56: Turbo Prolog 173 ACR

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

Page 57: Turbo Prolog 173 ACR

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.

Page 58: Turbo Prolog 173 ACR

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.

Page 59: Turbo Prolog 173 ACR

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.

Page 60: Turbo Prolog 173 ACR

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.

Page 61: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 61

Prolog shines in two major areas: search Pattern Matching

Page 62: Turbo Prolog 173 ACR

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

Page 63: Turbo Prolog 173 ACR

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

Page 64: Turbo Prolog 173 ACR

12 March'09 Turbo Programming 64