Prolog[1]

28
TOPICS PAGE NO. I. Introduction 3-7 II. Program s 8-32 1. Program to find the last element of a list 8 2. Program to delete an element form a list. 9 3. Program to demonstrate the example of Green Cut. 11 4. Program to calculate integer division. 13 5. Program to check the number of parents. 14 6. Program to add two numbers. 15 7. Program to subtract one number from another 1

description

practical file contents for prolog

Transcript of Prolog[1]

Page 1: Prolog[1]

TOPICS PAGE NO.

I. Introduction 3-7

II. Programs 8-32

1. Program to find the last element of a list 8

2. Program to delete an element form a list. 9

3. Program to demonstrate the example of Green Cut. 11

4. Program to calculate integer division. 13

5. Program to check the number of parents. 14

6. Program to add two numbers. 15

7. Program to subtract one number from another number. 16

8. Program to multiply two numbers. 17

9. Program to find the quotient of a number divided by another number. 18

1

Page 2: Prolog[1]

TOPICS PAGE NO.

10.Program to check which number is greater. 20

11.Program to calculate the factorial of a number. 22

12.Program to find prime numbers up to 20 23-24

13.Program to generate Fibonacci number. 25

14.Program to calculate the area of a Triangle. 29-30

15.Program to display the head of a list. 31

16.Program to display the tail of a list 32

Prolog

Prolog is a logic programming language. Prolog (PROgramming LOGic) rose within the realm of Artificial Intelligence (AI). It is a general

2

Page 3: Prolog[1]

purpose language often associated with artificial intelligence and computational linguistics. It has a purely logical subset, called "pure Prolog", as well as a number of extra logical features. It originally became popular with AI researchers, who know more about "what" and "how" intelligent behavior is achieved. The philosophy behind it deals with the logical and declarative aspects. Prolog represents a fundamentally new approach to computing and became a serious competitor to LISP

Significant Language Features

Prolog is a rich collection of data structures in the language and human reasoning, and a powerful notation for encoding end-user applications. It has its logical and declarative aspects, interpretive nature, compactness, and inherent modularity.

Intelligent Systems - programs which perform useful tasks by utilizing artificaial intelligence techniques.

Expert Systems - intelligent systems which reproduce decision-making at the level of a human expert.

Natural Language Systems - which can analys and respond to statements made in ordinary language as opposed to approved keywords or menu selections.

Relational Database Systems

Areas of Application Prolog is the highest level general-purpose language widely used today. It is taught with a strong declarative emphasis on thinking of the logical relations between objects or entities relevant to a given problem, rather than on procedural steps necessary to solve it. The system decides the way to solve the problem, including the sequences of instructions that the computer must go through to solve it. It is easier to say what we want done and leave it to the computer to do it for us. Since a major criterion in the

3

Page 4: Prolog[1]

commercial world today is speed of performance, Prolog is an ideal prototyping language. Its concept makes use of parallel architectures. It solves problems by searching a knowledge base (or more correctly a database) which would be greatly improved if several processors are made to search different parts of the database

The Prolog system recognizes the type of a term object by its syntax. A variable always starts with an uppercase character and all other data types start with a lowercase character. Below there is a picture showing the hierarchy of types in Prolog:

AtomsAtoms are strings that consist of:

Alphanumeric characters including the underscore character ( _ ) and that start with a lowercase character (A..Z, a..z, _).

Atoms enclosed in single or double quotes, for instance 'Papa bear' or "Papa bear". By using quotes an atom can start with a capital and can contain whitespace characters. Inserting a single double quote in a string that starts with a double quote is done by placing two double quotes inside the string, for instance, "This string ""contains"" 2 extra double quotes". The two double quotes inside the string are interpreted by the compiler as a single double quote. This is the same for inserting a single quote inside a string that is enclosed by single quotes.There is however a difference in using the single quote character and the double quote character, single quotes are removed from the atom and double quotes not. For example;

4

Page 5: Prolog[1]

the goal 'a' = a. succeeds because the single quotes are removed from the atom 'a'. If the goal is "a"=a. then the result will be no because the double quotes are not removed. There is also the Prolog double_quotes, this Prolog flag determines how the compiler processes atoms enclosed by double quotes.

An atom may also consist of the following special characters # $ % & * + - . / : < = > @ \ ^ ` ~

NumbersThese are integer numbers like, 33, -33, 0, 23534 and floating point numbers like, 23.3e+34, -0.3532e-03.

VariablesVariables start with an uppercase character and then consist of alphanumeric characters or the underscore character. Below there are some examples of variables:

    Var    X    Hello_this_is_a_variable

A variable that just consists of a single underscore characters is called an anonymous variable.

Structures

Structures are data objects that consist of one or more other components, these can in turn also be structures. These components are bounded by a functor. A functor is an atom that binds the components. Below there are several examples of structures:

    mother(clair, jil).    point(12, 2).    functor_1( g(h, i), other_compound_term(A, b) ).

The arity of a structure is the number of components, the arity of point(12, 2) is 2 and the arity of triangle( 12, 2, 15) is 3.

5

Page 6: Prolog[1]

Turbo Prolog is a quite old Prolog system that only works with MS-DOS. This implies serious memory limitations. Also, it uses a special Prolog dialect with typed variables and some other restrictions that does not comply with the Edinborough Standard . However, it is very fast, and contains a convenient visual debugger. I once learned Prolog by observing this debugger.

PROGRAM 1 - Program to find the last element of a list.

Code

Program to find the last element of a list.

last(X, [X]).last(X, [_|Y]):- last(X, Y).?- last(X, [i, love, my, country]), write(X).

6

Page 7: Prolog[1]

Output

PROGRAM 2 - Program to delete an element form a list

Code

/* Program to delete an element form a list.*/

efface(A, [A|L], L).efface(A, [B|L], [B|M]):- efface(A, L, M).?- efface(c, [a, b, c, d, e], R), write(R).

7

Page 8: Prolog[1]

Output

PROGRAM 4 - Program to demonstrate the example of Green Cut

Code/*Program to demonstrate the example of Green Cut.*/ uniform(X, blue):- class(X, Y), Y < 6.uniform(X, red):- class(X, Y), Y > 5, Y < 10.uniform(X, white):- class(X, Y), Y > 9.happy(Child):- uniform(Child, Colour),!,likes(Child, Colour).likes(ramesh, red).class(ramesh, 4).

8

Page 9: Prolog[1]

?- happy(ramesh).

Output

PROGRAM 6 - Program to calculate integer division

Code

9

Page 10: Prolog[1]

/*Program to calculate integer division.*/

is_integer(0).is_integer(X):- is_integer(Y), X is Y + 1.divide(Nl, N2, Result) :- is_integer(Result),Product1 is Result*N2,Product2 is (Result+l)*N2, Product1 =< Nl,Product2 > Nl,! .?- divide(27, 6, X), write(X).

Output

PROGRAM 7 - Program to check the number of parents

Code/* Program to check the number of parents.*/

10

Page 11: Prolog[1]

number_of_parents(adam,N) :- !, N = 0. number_of_parents(eve,N) :- !, N = 0. number_of_parents(X,2). ?- number_of_parents(eve,X), write(X).

Output

PROGRAM 8 - Program to add two numbers

Code/* Program to add two numbers. */

add(X, Y, Z):- Z is X + Y.

11

Page 12: Prolog[1]

?- add(1, 2, S), write(S).

Output

PROGRAM 9 - Program to subtract one number from another number.

Code/* Program to subtract one number from another number.*/

12

Page 13: Prolog[1]

sub(X, Y, Z):- Z is X - Y.

?- sub(2, 1, S), write(S).

Output

PROGRAM 10 - Program to multiply two numbers.

Code/* Program to multiply two numbers.*/

prod(X, Y, Z):- Z is X * Y.

13

Page 14: Prolog[1]

?- prod(3, 2, S), write(S).

Output

PROGRAM 11 - Program to find the quotient of a number divided by another number

Code/* Program to find the quotient of a number divided by another number.*/

14

Page 15: Prolog[1]

quo(X, Y, Z):- Z is X / Y.

?- quo(6, 2, S), write(S).

Output

PROGRAM 13 - Program to check which number is greater.

Code/* Program to check which number is greater. */

greater(X, Y) :- X > Y, write(X), !.greater(X, Y) :- X < Y, write(Y).

?- greater(3,4).

15

Page 16: Prolog[1]

Output

PROGRAM 15 - Program to calculate the factorial of a number.

Code/* Program to calculate the factorial of a number.*/

fact(1,1).fact(N,F):- N1 is N - 1, fact(N1,F1), F is N * F1.?-fact(5,R),write(R).

16

Page 17: Prolog[1]

Output

PROGRAM 16 - Program to find prime numbers up to 20

Code

/* Program to find prime numbers up to 20.*/

primes(Limit,Ps):- integers(2,Limit,Is),sift(Is,Ps).

17

Page 18: Prolog[1]

integers(Low,High,[Low|Rest]):- Low =< High,!,M is Low + 1,integers(M,High,Rest).

integers(_,_,[]).

sift([],[]).sift([I|Is],[I|Ps]):- remove(I,Is,New),

sift(New,Ps).

remove(P,[],[]).remove(P,[I|Is],[I|Nis]):- not(0 is I mod P),

!,remove(P,Is,Nis).

remove(P,[I|Is],Nis):- 0 is I mod P, !,remove(P,Is,Nis).

?-primes(20,Ps),write(Ps).

Output

18

Page 19: Prolog[1]

PROGRAM 17 - Program to generate Fibonacci number

Code/* Program to generate Fibonacci number.*/

19

Page 20: Prolog[1]

fibo(2,1) :- !.fibo(1,1) :- !.fibo(N,F) :- N1 is N - 1, N2 is N - 2, fibo(N1,F1), fibo(N2,F2), F is F1 + F2.?-fibo(7,F),write(F).

Output

20

Page 21: Prolog[1]

PROGRAM 21 - Program to display the head of a list

Code

/* Program to display the head of a list.*/

head([X|Y], X).

?-head([abhishek, 2, maiti,1985],H), write(H).

Output

21

Page 22: Prolog[1]

PROGRAM 22 - Program to display the tail of a list

Code/* Program to display the tail of a list.*/

tail([X|Y], Y).

?-tail([abhishek, 2, maiti,1985],T), write(T).

Output

22

Page 23: Prolog[1]

23