← Back to quizzesFree quiz

Fundamentals of Prolog Programming

Prolog is a declarative programming language that excels at representing and querying relational data. This course walks you through the core concepts tested in a typical Prolog quiz,…

11 questions~6 min
Fundamentals of Prolog Programming — Qwi
0 / 11
Score: 0%
1

Given the facts daughter(samantha,rebecca) and parent(rebecca,anna), which rule correctly derives that Anna is a parent of Samantha?

2

What will be the result of the query ?- max(4.2, 2.5, X). in the provided max/3 program?

3

In the factorial predicate f(N,F), what is the purpose of the guard N > 0 in the recursive clause?

4

When using the built‑in predicate =.. to convert a structure to a list, what is the result of the query ?- X =.. [names, may, smith].

5

Which of the following queries will succeed given the facts son(jack,michael) and parent(rebecca,anna)?

6

In the loop predicate example, why does the query ?- loop(2,1). fail?

7

What does the built‑in predicate not/1 represent in the rule mother(X,Y) :- parent(X,Y), not(male(X)).

8

Considering the list predicate memberL/2, which query will generate all members of the list [a,b,c]?

9

In the predicate convert/2 that capitalises a string, why is the constant 32 subtracted from each ASCII code?

10

What will be the output of the query ?- reverseL([a,b,c], X). given the definition using appendL/3?

11

Why does the query ?- father(michael, samantha). succeed in the familyRelation program?

Introduction to Prolog Fundamentals

Prolog is a declarative programming language that excels at representing and querying relational data. This course walks you through the core concepts tested in a typical Prolog quiz, including facts, rules, recursion, built‑in predicates, and list processing. By the end of the lesson you will be able to read, write, and debug Prolog programs with confidence.

1. Representing Knowledge with Facts and Rules

In Prolog, facts describe relationships that are unconditionally true, while rules define logical connections that can be inferred from existing facts.

  • Fact syntax: parent(rebecca, anna). means "Rebecca is a parent of Anna".
  • Rule syntax: parent(X,Y) :- daughter(Y,X). reads as "X is a parent of Y if Y is a daughter of X".

Consider the following example:

daughter(samantha, rebecca).
parent(rebecca, anna).
parent(X,Y) :- daughter(Y,X).

Using the rule above, Prolog can derive that Anna is a parent of Samantha because Samantha is a daughter of Rebecca, and Rebecca is a parent of Anna. This demonstrates how rules enable indirect reasoning.

2. Recursive Predicates and Guard Conditions

Recursion is a natural fit for Prolog. A classic example is the factorial/2 predicate, which computes the factorial of a number N and returns the result F.

factorial(0,1).                     % base case
factorial(N,F) :-
    N > 0,                         % guard condition
    N1 is N - 1,
    factorial(N1,F1),
    F is N * F1.

The guard N > 0 is crucial because it prevents infinite recursion by ensuring the recursive clause is only applied to positive integers. Without this guard, Prolog would keep calling the clause with decreasing values, eventually reaching negative numbers and never hitting the base case.

3. Built‑in Predicates for Arithmetic and Structure Manipulation

3.1 The max/3 Predicate

A simple arithmetic predicate can be written to find the maximum of two numbers:

max(A,B,Max) :-
    A >= B, !, Max = A.
max(_,B,B).

When you query ?‑ max(4.2, 2.5, X). Prolog evaluates the first clause, finds that 4.2 >= 2.5 succeeds, and binds X to 4.2. The result is:

X = 4.2

This demonstrates how the cut operator (!) can be used to commit to a choice once a condition is satisfied.

3.2 The =.. (Univ) Operator

The =.. predicate, also known as univ, converts between a structure and a list representation. For example:

?‑ X =.. [names, may, smith].

Prolog interprets the list as a functor names followed by its arguments. The query succeeds with:

X = names(may, smith)

Understanding =.. is essential for meta‑programming tasks such as dynamically constructing terms.

3.3 Negation as Failure with not/1

Prolog does not have classical logical negation. Instead, it uses negation as failure via the not/1 predicate. In the rule:

mother(X,Y) :- parent(X,Y), not(male(X)).

the goal not(male(X)) succeeds when Prolog cannot prove male(X). This is a powerful way to express concepts like "X is a mother if X is a parent and X is not male".

4. List Processing in Prolog

Lists are a fundamental data structure in Prolog. The predicate memberL/2 checks whether an element belongs to a list. A typical definition looks like:

memberL(X,[X|_]).
memberL(X,[_|Tail]) :- memberL(X,Tail).

To generate all members of the list [a,b,c], you would query:

?‑ memberL(X, [a,b,c]).

Prolog then backtracks, producing the solutions:

X = a ;
X = b ;
X = c.

This illustrates how Prolog can both test membership and enumerate list elements.

5. Understanding Query Success and Failure

5.1 Simple Fact Queries

Given the facts:

son(jack, michael).
parent(rebecca, anna).

The query ?‑ parent(rebecca, anna). succeeds because the fact directly matches the database. In contrast, queries like ?‑ parent(jack, michael). fail because no parent/2 fact for those arguments exists.

5.2 The loop/2 Example

Consider a predicate designed to iterate from an Index to an End:

loop(Index, End) :-
    Index = End, !.
loop(Index, End) :-
    Index < End,
    Next is Index + 1,
    loop(Next, End).

The query ?‑ loop(2,1). fails because the terminating condition Index = End is never satisfied and the guard Index < End is false from the start. Understanding these logical conditions helps you predict when a recursive predicate will succeed or terminate.

6. Putting It All Together – Sample Mini‑Project

To reinforce the concepts, build a small family‑tree program that includes:

  • Facts for parent/2, son/2, and daughter/2.
  • Rules for grandparent/2, ancestor/2, and mother/2 using not/1.
  • A recursive predicate descendant/2 that demonstrates guard usage.
  • List utilities such as memberL/2 to retrieve all children of a person.

Example snippet:

parent(rebecca, anna).
parent(anna, sam).
son(sam, michael).

grandparent(X,Z) :- parent(X,Y), parent(Y,Z).
ancestor(X,Z) :- parent(X,Z).
ancestor(X,Z) :- parent(X,Y), ancestor(Y,Z).

mother(X,Y) :- parent(X,Y), not(male(X)).

Querying ?‑ ancestor(rebecca, sam). will succeed, confirming that Rebecca is an ancestor of Sam.

7. SEO‑Friendly Recap and Key Takeaways

Mastering Prolog fundamentals—facts, rules, recursion, built‑in predicates, and list handling—provides a solid foundation for logic programming and artificial intelligence applications. Remember these core ideas:

  • Use :- to define rules that infer new relationships.
  • Guard conditions like N > 0 protect recursive predicates from infinite loops.
  • Built‑ins such as =.., not/1, and arithmetic comparisons enable powerful meta‑programming.
  • List predicates like memberL/2 showcase Prolog's natural backtracking capabilities.

By practicing the sample queries and extending the mini‑project, you will deepen your understanding and be ready to tackle more advanced Prolog challenges.