1
INTRODUCTION TO PROLOG
Prolog, an abbreviation for "Programming in Logic," stands as a high-level programming
language extensively employed in artificial intelligence and computational linguistics. Its genesis
in the early 1970s marks a departure from conventional programming paradigms, introducing
logic programming as its hallmark approach.
“Prolog is a logic programming language. It has an important role in artificial intelligence. Unlike
many other programming languages, Prolog is intended primarily as a declarative programming
language”.(Geeks For Geeks, 2024)
Logic programming, the cornerstone of Prolog, eschews procedural steps in favor of logical
relations and rules. Prolog programs articulate relationships and constraints among entities
through facts and rules, culminating in the derivation of solutions from queries.
A distinctive feature of Prolog lies in its utilization of Horn clauses, logical statements adopting
an implication format. These clauses orchestrate relationships between variables, enabling
Prolog to engage in logical inference and deduction.
Prolog finds its forte in domains necessitating symbolic computation, such as natural language
processing, expert systems, and symbolic mathematics. Its prowess in representing intricate
relationships and executing automated reasoning renders it indispensable in diverse artificial
intelligence endeavors.
PROLOG SYNTAX AND STRUCTURE
Prolog's syntax and structure diverge significantly from traditional imperative languages like
Ruby, characterized by:
a. Declarative Syntax: Programs embody sets of facts and rules delineating relationships
and properties, expressed through logical statements termed predicates.
Example of Declarative Syntax
%Facts
parent(john, mary).
parent(john, peter).
parent(paul, john).
parent(lisa, mary).
%Rules
ancestor(X, Y) :- parent(X, Y).
2
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
In this example:
Facts: We establish relationships between individuals. For instance, parent(john, mary)
indicates that John is the parent of Mary.
Rules: We define logical rules that can be used to infer relationships. In this case, the
ancestor rule recursively defines an ancestor relationship. An individual X is considered
an ancestor of Y if X is a parent of Y or if there exists an individual Z such that X is a
parent of Z and Z is an ancestor of Y.
b. Clauses, Predicates, and Goals: Clauses, representing facts or rules, consist of a head
specifying the predicate and a body articulating conditions or goals for predicate veracity.
c. Variables and Constants: Variables, denoted by strings commencing with an
uppercase letter or underscore, signify unknown values, while constants, also known as
atoms, symbolize fixed values or objects.
Examples of Variables and Constants Prolog Syntax
% Facts
consonant(a).
consonant(b).
consonant(c).
consonant(d).
consonant(e).
% Rule to check if a character is a consonant
is_consonant(X) :-
consonant(X).
% Query examples
% Query: is_consonant(a).
% Output: false (since 'a' is not a consonant)
% Query: is_consonant(b).
% Output: true (since 'b' is a consonant)
In Prolog, variables begin with an uppercase letter or an underscore (_), while atoms
(which represent constants) typically begin with a lowercase letter. In this example, 'X' is
3
a variable, and 'a', 'b', 'c', 'd', and 'e' are constants representing consonants. The
is_consonant/1 predicate checks whether its argument is a consonant or not.
d. Logical Operators: Logical operators such as conjunction (,), disjunction (;), and
negation (not or +) amalgamate predicates, delineating logical relationships.
1. Logical AND (``,): The comma ‘,` is used to combine multiple goals in a query. It
succeeds only if all the goals are satisfied.
For example:
likes(john, apples).
likes(john, oranges).
% Query: Does John like both apples and oranges?
?- likes(john, apples), likes(john, oranges).
2. Logical OR (;): The semicolon ; is used to provide alternatives in a query. It succeeds
if at least one of the alternatives is satisfied.
For example:
likes(john, apples).
likes(john, oranges).
% Query: Does John like either apples or oranges?
?- likes(john, apples); likes(john, oranges).
3. Logical NOT (\+): The backslash followed by a plus sign \+ is used to express
negation. It succeeds if the goal cannot be proven true.
For example:
likes(john, apples).
likes(john, oranges).
% Query: Does John dislike bananas?
?- \+ likes(john, bananas).
These logical operators are essential for constructing queries with more complex
conditions in Prolog programs.
4
e. Querying and Backtracking: Queries, executed to unearth solutions to logical queries
or goals, entail a backtracking mechanism where Prolog explores alternative paths
systematically.
VARIABLES IN PROLOG
“Variable is a string. The string can be a combination of lowercase or upper case
letters''.(Javatpoint, 2024). Variables, placeholders within Prolog, epitomize unknown values or
patterns, facilitating pattern matching and unification during execution. Key aspects include:
● Variable Naming: Variables commence with an uppercase letter or underscore (_)
followed by a combination of letters, digits, and underscores.
● Unification: Unification entails finding substitutions for variables rendering two terms
identical during execution.
● Usage in Predicates: Variables serve to represent unknowns within predicates, aiding
Prolog in resolving logical queries by binding them to suitable values.
● Backtracking with Variables: Backtracking, a core Prolog mechanism, enables
exploration of multiple predicate solutions by revisiting and reassessing variable
bindings.
Here is an Example of Naming Variables in PROLOG:
likes(john, pizza).
likes(mary, sushi).
likes(tom, pizza).
likes_food(Person, Food) :- likes(Person, Food).
Explanation
Person, Food: These are general variable names, representing any person or any food.
john, mary, tom: These are specific atoms representing individuals.
pizza, sushi: These are specific atoms representing types of food.
COMMENTS ON PROLOG
5
Comments in Prolog elucidate code for comprehension by programmers and future readers,
commencing with the percent symbol (%) and extending to the line's end. Commentary spans
single-line, multi-line, commenting out code, and documentation comments.
Educba, 2024 stated that there are two comments in PROLOG
1. The percentage “%” symbol is necessary for the single-line comment.
2. The “/*” symbol is started the comment block to write.
The “*/” symbol ends the comment block.
NUMERICS AND STRINGS IN PROLOG
Prolog supports numeric data types like integers and floating-point numbers for arithmetic
operations and comparisons, alongside strings represented as sequences of characters
enclosed in single quotes. Operations include concatenation, substring extraction, and
comparison, showcasing Prolog's text processing capabilities.
FUNCTIONS IN PROLOG
Prolog functions, embodied as predicates, deliver results based on logical rules and conditions,
with examples encompassing factorial computation, Fibonacci sequence generation, and list
manipulation.
In Prolog, functions are called predicates. Predicates represent relationships between objects or
properties of objects in the world.
Here are some key points about predicates in Prolog:
Predicate Definition: Predicates are defined using facts and rules. Facts are statements about
relationships that are always true, while rules define relationships based on conditions.
% Fact
likes(john, apples).
% Rule
likes(john, X) :- fruit(X).
Recursion: Prolog predicates can be recursive, meaning they can call themselves.
Example
% Define a predicate to calculate factorial
6
factorial(0, 1).
factorial(N, Result) :-
N > 0,
N1 is N - 1,
factorial(N1, Result1),
Result is N * Result1.
Built-in Predicates: Prolog comes with a set of built-in predicates for common tasks like
arithmetic operations, list manipulation, input/output, etc.
Example
?- X is 5 + 3.
% X will be unified with the result of the addition, 8.
?- append([1, 2], [3, 4], Result).
% Result will be unified with the list [1, 2, 3, 4].
Cut Operator: Prolog has a cut operator (!) which is used to control backtracking. It's used to
commit to a choice and prevent Prolog from looking for alternative solutions.
Example
% Define a predicate to determine if a number is positive
positive(X) :- X > 0, !.
positive(0) :- !, fail. % fail if it's 0, to prevent backtracking
positive(_) :- fail. % fail for anything else
CASE STATEMENTS ON PROLOG
While Prolog lacks explicit case or switch statements, pattern matching through multiple
predicate clauses enables analogous functionality. Examples include grade calculation, weather
forecast prediction, and employee benefits determination.
In Prolog, you can achieve similar functionality to case statements in other programming
languages using pattern matching with multiple clauses. Here's an example:
SYNTAX
% Define some cases for a predicate called "action"
action_case(1) :-
% Case 1: Perform action A
write('Performing action A'), nl.
7
action_case(2) :-
% Case 2: Perform action B
write('Performing action B'), nl.
action_case(3) :-
% Case 3: Perform action C
write('Performing action C'), nl.
action_case(_) :-
% Default case: No matching case found
write('No matching action'), nl.
You can then call action_case with different arguments to execute different actions:
SYNTAX
?- action_case(1).
Performing action A
true.
?- action_case(2).
Performing action B
true.
?- action_case(3).
Performing action C
true.
?- action_case(4).
No matching action
true.
This approach allows you to define different behaviors for different cases based on the input
arguments.
CONCLUSION
Prolog emerges as a pioneer in logic programming, distinguished by its declarative nature and
powerful inference engine. The language's versatility in handling diverse computational tasks,
from rule-based inference to complex constraint satisfaction problems, underscores its
relevance in modern artificial intelligence and expert system domains. As Prolog continues to
evolve, its integration with contemporary technologies promises innovative solutions to intricate
computational challenges, solidifying its status as a cornerstone in the realm of logic-based
programming paradigms.
8
REFERENCES:
Geeks For Geeks, 2024. Prolog|An
Introduction.geeksforgeeks.https://www.google.com/amp/s/www.geeksforgeeks.org/prolog-an-in
troduction/amp/
Javatpoint, 2024. Prolog syntax.Javatpoint.https://www.javatpoint.com/prolog-syntax
Educba, 2024.Prolog Comments.Educbahttps://www.educba.com/prolog-comments/