6
TUTORIAL ON PROLOG (LAB) Text Book: Introduction to Turbo prolog -Carl Townsend Course: Artificial Intelligence and Expert System Md. Shamim Akhter Assistant Professor Department of Computer Science American International University-Bangladesh

prolog_tutorial

Embed Size (px)

Citation preview

Page 1: prolog_tutorial

TUTORIAL ON PROLOG (LAB)

Text Book: Introduction to Turbo prolog -Carl Townsend

Course: Artificial Intelligence and Expert System

Md. Shamim Akhter

Assistant Professor Department of Computer Science

American International University-Bangladesh

Page 2: prolog_tutorial

Chapter 1

What is Prolog?

Prolog was taken from the phrase “Programming in logic”. It was developed in 1972 by

Alain Colmerauer and P. Roussel at the University of Marseilles in France. Prolog, a

programming language centered on a small set of basic mechanisms, including pattern

matching, tree-based data structuring and automatic backtracking. Prolog is especially

well suited for problems that involve objects-in particular, structure objects- and relations

between them. For example, to express spatial relationships between objects, such as the

blue sphere is behind the green one. It is also easy to state a more general rule: if object X

is closer o the observer than object Y, and Y is closer than Z, than X must be closer than

Z.

A short history of Prolog

During the 1970s Prolog became popular in Europe for AI application. However, in the

United States, Prolog remained a relatively minor computer language. Researchers of

United States preferred the LISP language for AI applications. LISP was considered a

more powerful programming language for these applications, through it was more

difficult to learn and use than Prolog.

During those years Prolog and LISP both were very slow in execution and consumed

large amounts of memory. In addition, users needed considerable programming expertise

to use Prolog or LISP program. The picture suddenly changed in1981 at the First

International Conference on Fifth Generation Systems in Tokyo. The Japanese were

having great difficulty competing with the rapidly growing U.S computer market.

Forming a new and aggressive national technology plan, the Japanese announced they

would leapfrog over the current technology with new hardware and software by the

1990s. The computer language for the new systems would be Prolog.

As a result of this Japanese initiative, developers around the world took a new interest in

Prolog. Today’s versions of Prolog have features and power that far exceed those of a

few years ago. They often are easier to use, cost less than earlier versions and faster. On a

mainframe computer or micro computer, you will now find that Prolog often is the best

language for nay application that involves formal or symbolic reasoning.

Application for prolog

Prolog is useful for almost any application that requires formal reasoning. This includes

applications in expert systems, natural language processing, robotics and gaming &

simulations.

Chapter 2

Starting of Turbo Prolog

You have a compressed file named Turbo_prolog in web. Please download this file and

unzipped it. The name should be the same. So now you have a directory call

Turbo_Prolog. Open the directory and find the exe file named PROLOG. Click on any

one exe dos version or windows version and you will see the following screen in the

monitor.

Page 3: prolog_tutorial

Click F5 to get 4 windows in your screen.

If you did not find 4 windows (Editor, Dialog, Message and Trace) clearly then type

Alt+S, then the following screen will be seen. Thereafter, click on the Window size. Then

make changes the size of any window by moving the up-down, left-right key.

Page 4: prolog_tutorial

The Six Options:

Details of each Option will be given in Chapter 2, Page 14-28.

Chapter 3 How to run a Prolog program

1. F10 to get all windows in screen

2. Goto Edit in menubar and press Enter

3. Write the following codes into the Editor Window

/*Simple test Program */

predicates

likes(symbol,symbol)

clauses

likes(frank,sue).

likes(Harold,ruth).

4. Save this program (Alt+F to go to file Menu Option and then select “write to”

options to save in a specific file name (please give the file name “prog1.pro”).

5. Goto the Run Menubar Option and press Enter

6. (You are now in a Dialog window where you have Goal: ) Write your query there,

let you are going to type

likes (Harold, ruth)

7. After typing the query press enter

8. Your program will execute and give the following result

True

Goal:

Your Work, (Write query)

1. likes(frank, sue)

2. likes (frank, ruth)

Important topics need to understand

1. Facts

2. Domains, Domain types, Predicates, Clauses

3. Adding Comments

4. Using Trace Procedure

a. Open program prog1.pro

b. Change the program as following

/*Simple test Program */

domains

person1, person2=symbol

predicates

likes (person1,person2)

clauses

likes (frank, sue).

Page 5: prolog_tutorial

likes (Harold, ruth).

c. Save the program at any name and do the same activities that you have

done with prog1.pro (run will all goals)

d. After successfully running write the following commend any place above

domain section

Trace

e. Now again Save and Run this program with any one goal

f. Watch carefully, inside the Trace window and Editor Window …Where

the cursor in Editor Window is and what is written inside the Trace

Window..Try to understand…

g. To traverse the program in each step Press F10 to proceed step by step.

5. Variables

a. Bound and Free Variables

b. Anonymous Variables

After Trace procedure successfully executed, give the following

commends beside Goal:

1. likes(_,ruth)

2. likes (frank,_)

Your Work, (Write query)

1. Write a program that will answer the question about your family

Let your name is hasan

a. Goal: father(_,hasan)

b. Goal: mother(_,hasan)

c. Goal: age(hasan)

d. Goal: age(father(_,hasan))

e. Goal: age(X)

f. Goal: brother(_,_)

g. Goal: brother(X, hasan)

h. Goal: sister(X, hasan)

Hints: you need to specify the predicates name and define arguments

father, mother, age, brother, sister

Also specify the clauses as the same name of predicates.

Chapter 4

1. Definitions of Rules and working procedure

Page 58: program write and run

2. Compound facts

3. Unification and Rules of Unification (page 63)

4. not predicate and its uses and problems (page 65)

5. write, writef, writedevice predicates

6. readln, readchar, readint, readreal predicates

7. inkey(Char), keypressed predicates

8. Using Windows , “clearwindow” commend

Page 6: prolog_tutorial

9. Run program on (page 79)

10. fail predicate

11. Run program on (page 85, 87, 88), find out the difference and why this

differences are occurring.

Your Work:

Write program that will satisfy the following relations

1. For all X and Y, Y is an offspring of X if X is a parent of Y.

2. For all X and Y, X is a parent of Y if Y is an offspring of X.

3. For all X and Y, X is the mother of Y if X is a parent of Y and X is a female.

4. For all X, Y and Z, X is a grandparent of Z if X is a parent of Y and Y is a parent

of Z.

Chapter 5 1. The concept of Recursion

2. program practice and trace page 93 (both).

3. program practice and trace on page 95 (using repeat).

4. Find out the difference of program on page 93 and program on page 95 ( by

forcely stop you may need to use Ctrl+Break ).

5. Rules of Recursion

a. A program must include some method of terminating the recursion loop.

b. Variable binding in a rule or fact applies only to the current layer. When a

copy of the rule or fact in invoked, the variables in the copy are bound

through the process of unification, just as in any other invocation.

c. In most applications, the recursive procedure should do its work on the

way down.

6. Concept of Unwinding

a. Modify your previous program as program in page 98.Carefully watch the

difference.

b. Recursion involves two phases: “winding down” and “unwinding”.

Predicates before recursive call are invoked during winding down.

Predicates after the recursive call are invoked during unwinding.

7. Now modify the login program as like program on page 99, look the difference.

Exercise:

a. Page 100.

b. Write a program that will invoke an input (an integer) and find out the

factorial of that number recursively.

c. Write a program that will invoke two inputs and find out the first seven (7)

Fibonacci numbers from their Fibonacci series.