Prajan AI
Prajan AI
Faculty of Management
National College of Computer Studies
Paknajol, Kathmandu
Prolog (short for PROgramming in LOGic) was developed in the early 1970s by Alain
Colmerauer and Philippe Roussel. It is widely used in artificial intelligence applications due
to its powerful pattern-matching capabilities and declarative nature, allowing developers to
describe what they want to achieve rather than explicitly detailing how to do it. It is a logic
programming language that is particularly well-suited for tasks that involve symbolic
reasoning, natural language processing, and problem-solving. It is fundamentally different
from imperative programming languages like C, Java, or Python, as it is based on formal
logic rather than sequential instructions.
Here, the rules are written in the form of logical clauses, where head and body are present.
For example, H is head and B1, B2, B3 are the elements of the body. Now if we state that “H
is true, when B1, B2, B3 all are true”, this is a rule. On the other hand, facts are like the rules,
but without any body. So, an example of fact is “H is true”.
Here are some of the core concepts and constructs that define Prolog programming:
Facts:
Facts represent basic assertions about the world or domain of interest. They are used to define
relationships and properties.
Syntax:
predicate(arguments).
Example:
cat(tom).
likes(tom, milk).
Here, cat(tom) asserts that Tom is a cat, and likes(tom, milk) asserts that Tom likes milk.
Rules:
Rules define relationships between facts using logical implications. They have a head
(conclusion) and a body (condition).
Syntax:
head :- body.
Example:
animal(X) :- cat(X).
animal(X) :- dog(X).
Here, the rule animal(X) :- cat(X) implies that if X is a cat, then X is an animal.
Queries:
Queries are used to ask questions about the information represented in the program. The Prolog
interpreter attempts to satisfy these queries based on available facts and rules.
Syntax:
?- query.
Example:
?- likes(tom, milk).
This query asks if Tom likes milk, and the interpreter responds with true or false.
Variables:
Variables in Prolog are placeholders for terms and begin with an uppercase letter or an
underscore. They can be unified with other variables, constants, or structures during execution.
Example:
Unification:
Unification is the process of making two terms equal, a fundamental mechanism in Prolog.
Prolog uses unification to match goals with facts and rules, solving queries by finding
substitutions that satisfy the logical expressions.
Backtracking:
Backtracking is a systematic method for finding all possible solutions to a query. If a query
fails, Prolog backtracks to try alternative solutions, exploring different paths in the search
space.
Lists:
Lists are a fundamental data structure in Prolog, used to represent collections of elements.
Syntax:
[Head|Tail]
Example:
list_length([], 0).
list_length([_|Tail], Length) :-
list_length(Tail,
TailLength), Length is
TailLength + 1.
Hence, Prolog is a powerful language for declarative programming and symbolic reasoning.
Its unique features like unification, backtracking, and logical inference make it ideal for
applications requiring complex data relationships and problem-solving.
SWIProlog Setup:
Setting up SWI-Prolog is a straightforward process and can be done on various platforms,
including Windows, macOS, and Linux. Here's a step-by-step guide for installing on Windows:
Solution,
male(ram).
male(shyam).
male(laxman).
female(radha).
female(geeta).
female(rita).
Output:
Q.NO:3 Write a program in Prolog to find max and min of two numbers:
a) Find maximum between 100 and 200
b) Find minimum between 300 and 500
Solution,
find_max(X,Y,X):- X >= Y.
find_max(X,Y,Y):- X < Y.
find_min(X,Y,X):- X =< Y.
find_min(X,Y,Y):- X > Y.
Output:
Q.NO:4 Write a Prolog program to find the owner of the car:
a) What car does Ram owns?
b) Does Ram owns my car?
c) List all, who owns Nissan car.
d) Does Sita owns sedan car?
e) Does sita owns Nissan truck?
Solution,
owns(ram,car(bmw)).
owns(shyam,car(nissan)).
owns(hari,car(ferrari)).
owns(sita,car(nissan)).
sedan(car(bmw)).
sedan(car(ferrari)).
truck(car(nissan)).
Output:
Q.NO:5 Find the factorial number using recursion in Prolog:
a) Find factorial of 5.
Solution,
fact(0,1).
fact(N,F):-
(
N1 is N-1,
fact(N1,F1),
F is N*F1
).
Output:
Q.NO:6 Find the Fibonacci of number using recursion in Prolog:
a) Find Fibonacci number at n=7.
Solution,
fib(0, 1).
fib(1, 1).
fib(N, Result) :- N1 is N - 1,
N2 is N -2, fib(N1, Result1),
fib(N2, Result2),
Result is Result1 + Result2.
Output:
Q.NO:7 Write a prolog program for Tower of Hanoi:
Solution,
move(1,X,Y,_):-
write('Move top disk from '),write(X),write(' to
'),write(Y),nl. move(N,X,Y,Z):-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Output:
Q.NO:8 Write a prolog program for Monkey Banana Problem:
Solution,
move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
push(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(State1) :-
move(State1,_,State2),
canget(State2).
Output:
Q.NO:9 Write a prolog program for Family Relations Problems:
Solution,
female(pam).
female(liz).
female(pat).
female(ann).
male(jim).
male(bob).
male(peter).
parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
parent(bob, peter).
parent(peter, jim).
mother(X,Y):- parent(X,Y), female(X).
father(X,Y):-
parent(X,Y),male(X).
haschild(X):- parent(X,_).
sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.
brother(X,Y):- parent(Z,X),parent(Z,Y),male(X),X\==Y.
Output:
Q.NO: 10 Write a java program for BFS Search:
Solution,
import java.io.*;
import java.util.*;
public class BFS
{
private int node;
private LinkedList<Integer> adj[];
private Queue<Integer> que;
BFS(int v)
{
node = v;
adj = new LinkedList[node];
for (int i=0; i<v; i++)
{
adj[i] = new LinkedList<>();
}
que = new LinkedList<Integer>();
}
void insertEdge(int v,int w)
{
adj[v].add(w);
}
void BFS(int n)
{
boolean nodes[] = new boolean[node];
int a = 0;
nodes[n]=true;
que.add(n);
while (que.size() != 0)
{
n = que.poll();
System.out.print(n+" ");
for (int i = 0; i < adj[n].size(); i++)
{
a = adj[n].get(i);
if (!nodes[a])
{
nodes[a] = true;
que.add(a);
}
}
}
}
public static void main(String args[])
{
BFS graph = new BFS(6);
graph.insertEdge(0, 1);
graph.insertEdge(0, 3);
graph.insertEdge(0, 4);
graph.insertEdge(4, 5);
graph.insertEdge(3, 5);
graph.insertEdge(1, 2);
graph.insertEdge(1, 0);
graph.insertEdge(2, 1);
graph.insertEdge(4, 1);
graph.insertEdge(3, 1);
graph.insertEdge(5, 4);
graph.insertEdge(5, 3);
System.out.println("Breadth First Search is:");
graph.BFS(0);
}
}
Output:
Q.NO: 11 Write a java program for DFS Search:
Solution,
import java.util.*;
class DFS {
private LinkedList<Integer> adj[];
private boolean visited[];
DFS(int V)
{
adj = new LinkedList[V];
visited = new boolean[V];
for (int i = 0; i < V; i++)
adj[i] = new LinkedList<Integer>();
}
void insertEdge(int src, int dest) {
adj[src].add(dest);
}
void DFS(int vertex)
{ visited[vertex] = true;
System.out.print(vertex + " ");
Iterator<Integer> it = adj[vertex].listIterator();
while (it.hasNext()) {
int n = it.next();
if (!visited[n])
DFS(n);
}
}