KEMBAR78
Prolog Viva | PDF | Mathematical Logic | Computer Programming
0% found this document useful (0 votes)
8 views4 pages

Prolog Viva

Prolog is a high-level logic programming language used primarily in AI and computational linguistics, based on formal logic. Key features include the use of facts, rules, and queries, with constructs like predicates and lists for problem representation. The document also discusses various problems solvable by Prolog, such as the Travelling Salesman Problem and the 4-Queens Problem, as well as recursion and conditional statements.

Uploaded by

Barnali Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Prolog Viva

Prolog is a high-level logic programming language used primarily in AI and computational linguistics, based on formal logic. Key features include the use of facts, rules, and queries, with constructs like predicates and lists for problem representation. The document also discusses various problems solvable by Prolog, such as the Travelling Salesman Problem and the 4-Queens Problem, as well as recursion and conditional statements.

Uploaded by

Barnali Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

Prolog viva
1. What is prolog?

Prolog stands for Programming in Logic.

It is a high-level logic programming language associated primarily with artificial


intelligence and computational linguistics. Prolog is based on formal logic and allows
expressing facts and rules about problems within a system using a form of symbolic
reasoning.

2. Name one language which can replace Prolog in AI applications.


LISP

3. Which version of Prolog have you used?


GNU Prolog is a free Prolog compiler with constraint solving over finite domains
developed by Daniel Diaz.
4. Write features of prolog

a) Prolog is a logic programming language based on first-order predicate logic.


b) It uses facts, rules, and queries to represent and solve problems.
c) Backtracking and pattern matching are core mechanisms for execution.
d) It is well-suited for applications in AI, natural language processing, and expert
systems.
e) Prolog supports recursion and non-deterministic programming naturally.

5. What are the main constructs in Prolog?

Main Constructs in Prolog (point-wise):

1. Facts – Declare known information (e.g., parent(john, mary).).


2. Rules – Define relationships using conditions (e.g., grandparent(X, Y) :-
parent(X, Z), parent(Z, Y).).
3. Queries – Ask questions to the system (e.g., ?- parent(john, mary).).
4. Variables – Represent unknowns, written with capital letters (e.g., X, Y).
5. Predicates – Functions that define relationships between arguments.

6. Give one example of predicate.

a. father(john, mary). – john is the father of mary


b. likes(alice, pizza). – alice likes pizza
c. sibling(X, Y) :- parent(Z, X), parent(Z, Y). – X and Y are siblings if they share a
parent Z
d. greater_than(X, Y) :- X > Y. – X is greater than Y
e. is_even(N) :- 0 is N mod 2. – N is even

1
2

7. What is list in Prolog. Give Example.


a. A list is an ordered collection of elements.
b. It is written using square brackets, e.g., [a, b, c].
c. Elements can be atoms, numbers, variables, or even other lists.
d. The head is the first element; the tail is the rest of the list.
e. Example: [apple, banana, mango] or [H|T] for head-tail pattern matching.
[apple, banana, mango] = [H|T].
Result:
 H = apple (Head)
 T = [banana, mango] (Tail)

8. what are the list operations can be performed in prolog. Give examples
a. Concatenation – Join two lists.
Example: append([1,2], [3,4], X). % X = [1,2,3,4]
b. Membership – Check if an element is in a list.
Example: member(3, [1,2,3,4]). % true
c. Length – Find the number of elements.
Example: length([a,b,c], L). % L = 3
d. Head and Tail – Extract first element and rest.
Example: [H|T] = [x,y,z]. % H = x, T = [y,z]
e. Delete – Remove an element from list.
Example: select(b, [a,b,c], X). % X = [a,c]

9. State the problem statement of the Travelling Salesman Problem (TSP).


Find the shortest possible route that visits each city once and returns to the starting
city.
Example: Given cities A, B, C, D with distances, find the minimum-cost tour
covering all.

10. State the Water Jug Problem.


Measure a specific quantity of water using two jugs of different capacities.
Example: Using a 4-liter and a 3-liter jug, measure exactly 2 liters.

11. State the Monkey and Banana Problem.


A monkey wants to reach bananas hanging from the ceiling using tools in the room.
Example: Monkey must move a box under bananas and climb to grab them.

12. State the 4-Queens Problem.


A: Place 4 queens on a 4×4 chessboard so that no two queens attack each other.
Example: Ensure no two queens share the same row, column, or diagonal.

13. How recursion is implemented in Prolog? Give one example.

a) Prolog uses recursion to repeat operations, especially in list processing and


mathematical problems.
b) A base case stops recursion.
c) A recursive rule calls itself with a smaller or simpler input.

2
3

Query:
?- factorial(3, F). → F = 6

14. Do we have loops in Prolog? What can be useful in that case? write in crisp and
point-wise
a. Prolog does not have traditional loops like for or while.
b. Instead, recursion is used to perform repeated actions.

15. What are the conditional statements are available in Prolog? Give one example.
a) If-Then (->) – Executes the "then" part if the condition is true.
Example: X > 0 -> write('Positive').
b) If-Then-Else (-> ;) – Adds an "else" part.
Example: X > 0 -> write('Positive') ; write('Non-positive').
c) Cut (!) with conditions – used to control backtracking in Prolog.

Types of Cuts:

1. Green Cut
o Used for efficiency only; doesn’t change logical meaning.
o Just prevents unnecessary backtracking.
o ✅ Safe to use.

Example
max(X, Y, X) :- X >= Y, !. % Green cut
max(_, Y, Y).

2. Red Cut

 Changes the logical meaning of the program.


 Used to force a decision and prune alternatives.
 ❌ Can lead to incorrect logic if used carelessly.

Example

grade(Marks, pass) :- Marks >= 40, !.


grade(_, fail). % Red cut forces decision at 40

3
4

You might also like