2. Syntax
1. Hello World
To write and run a “Hello, World!” program in Prolog on Ubuntu, follow these steps:
-
Install SWI-Prolog (if not already installed): Open a terminal and run:
Terminal window sudo apt-get updatesudo apt-get install swi-prolog -
Create a Prolog file: Create a file called
hello_world.pl
using your favorite text editor:Terminal window nano hello_world.pl -
Write the Prolog code: Add the following code to the file:
hello_world :- write('Hello, World!'), nl.Save and exit the editor (
CTRL + X
, thenY
to confirm). -
Run the Prolog file: Open a terminal and run SWI-Prolog with your Prolog file:
Terminal window swipl hello_world.pl -
Execute the
hello_world
predicate: Once inside the Prolog prompt, run the following:?- hello_world.You should see:
Hello, World!true.
2. Basics
The basics of Prolog revolve around logic programming, where you define relationships and rules, and Prolog uses them to derive conclusions. Here are the key concepts:
2.1 Facts
- Facts state things that are unconditionally true.
- Syntax:
fact_name(arguments).
- Example:
This declares that John is a parent of Mary.parent(john, mary).
2.2 Rules
- Rules define conditions under which something is true. They have a head (what you want to prove) and a body (conditions to be satisfied).
- Syntax:
head :- body.
- Example:
This means “X is the grandparent of Y if X is the parent of Z, and Z is the parent of Y.”grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
2.3 Queries
- Queries are questions asked to Prolog to determine if certain facts or rules hold.
- Syntax:
?- query.
- Example:
This checks if John is a parent of Mary. Prolog will respond with?- parent(john, mary).
true
orfalse
.
2.4 Variables
- Variables in Prolog start with an uppercase letter and can take any value during computation.
- Example:
This query asks, “Who is Mary’s parent?” and Prolog will return the possible values of?- parent(X, mary).
X
that satisfy the query.
2.5 Unification
- Unification is the process of matching terms. In Prolog, terms unify if they are the same or if variables can be instantiated to make them the same.
- Example:
Prolog tries to unify?- parent(john, Y).
parent(john, Y)
with the facts and returns the value ofY
that makes the fact true (e.g.,Y = mary
).
2.6 Backtracking
- Prolog uses backtracking to find all possible solutions to a query. If it finds a failure in satisfying a goal, it backtracks to the previous choice point and tries another path.
- Example:
If there are multiple children of John, Prolog will return one solution, and by typing?- parent(john, X).
;
after the first answer, Prolog will continue backtracking to find other solutions.
2.7 Lists
- Lists are an important data structure in Prolog, denoted by square brackets
[]
. - Example:
This query checks if?- member(X, [1, 2, 3]).
X
is a member of the list[1, 2, 3]
and will return values likeX = 1
,X = 2
, and so on.
2.8 Recursion
- Prolog heavily relies on recursion to process structures like lists or to perform repeated operations.
- Example:
This defines the length of a list: if it’s empty, the length is 0; otherwise, it’s 1 plus the length of the tail.length([], 0).length([_|T], N) :- length(T, N1), N is N1 + 1.
2.9 Negation
- Prolog uses negation as failure (
\+
), which means it assumes something is false if it cannot prove it to be true. - Example:
This checks if John is not the parent of Bob.?- \+ parent(john, bob).
2.10 The Cut Operator (!
)
- The cut (
!
) is used to control Prolog’s backtracking behavior, preventing Prolog from reconsidering alternative solutions once a decision is made. - Example:
This ensures that ifmax(X, Y, X) :- X >= Y, !.max(_, Y, Y).
X
is greater than or equal toY
, the cut prevents further evaluation of the second clause.
2. Control Structures in Prolog
Prolog’s control structures determine how the program flow proceeds, including logical flow, recursion, and decision-making. Some of the most important control structures are:
-
Conjunction (
,
)- The comma (
,
) is used to join conditions that must all be true for a rule to be satisfied. - Example:
Here, bothgrandparent(X, Y) :- parent(X, Z), parent(Z, Y).
parent(X, Z)
andparent(Z, Y)
must be true forgrandparent(X, Y)
to be true.
- The comma (
-
Disjunction (
;
)- The semicolon (
;
) represents logical OR. If one of the conditions is true, the whole statement is true. - Example:
This sayssibling(X, Y) :- parent(Z, X), parent(Z, Y).sibling(X, Y) :- step_sibling(X, Y).
X
andY
are siblings if they share a parent or if they are step-siblings.
- The semicolon (
-
Negation (
\+
)- Prolog uses negation as failure (
\+
), meaning that if Prolog fails to prove a statement true, it assumes it to be false. - Example:
This states thatnot_parent(X, Y) :- \+ parent(X, Y).
X
is not the parent ofY
if it cannot be proven thatX
is a parent ofY
.
- Prolog uses negation as failure (
-
If-Then (
->
)- The
->
operator allows for conditional execution. If the condition on the left is true, the right side will be executed. - Example:
Ifmax(X, Y, Z) :- X > Y -> Z = X ; Z = Y.
X > Y
is true,Z = X
; otherwise,Z = Y
.
- The
-
Cut (
!
)- The cut operator (
!
) is used to stop Prolog from backtracking beyond a certain point, committing to the current choice and pruning other alternatives. - Example:
Here, after confirmingmax(X, Y, X) :- X >= Y, !.max(_, Y, Y).
X >= Y
, Prolog will not consider the second clause.
- The cut operator (
-
Fail (
fail
)- The
fail
predicate always fails when executed, forcing backtracking. - Example:
This will write all parents by forcing backtracking after each result.find_any(X) :- parent(X, _), write(X), fail.find_any(_).
- The
-
Recursion
- Prolog programs often use recursion to iterate through data structures like lists.
- Example:
length([], 0).length([_|T], N) :- length(T, N1), N is N1 + 1.
3. Data Structures in Prolog
Prolog supports several types of data structures to represent different forms of knowledge:
-
Atoms
- Atoms are constants that represent simple entities. They can be any sequence of alphanumeric characters starting with a lowercase letter.
- Example:
john
,mary
,dog
.
-
Numbers
- Prolog supports both integers and floating-point numbers.
- Example:
3
,42.0
.
-
Variables
- Variables are placeholders for values that will be determined during execution. Variables start with an uppercase letter.
- Example:
X
,Person
.
-
Lists
- Lists are the primary data structure in Prolog for storing collections of items. Lists are written with square brackets and can contain any terms, including other lists.
- Example:
Lists are recursive, where the head (first element) and tail (remaining list) are often processed separately.[1, 2, 3][apple, orange, banana][X, Y, Z][Head | Tail].
-
Tuples (Compound Terms)
- Tuples are compound structures where multiple arguments are grouped under a functor.
- Example:
These are structures consisting of a functor (point(3, 4)date(2023, 09, 18)
point
ordate
) and the arguments (3, 4
or2023, 09, 18
).
-
Strings
- Prolog handles strings as a list of ASCII character codes. Alternatively, modern implementations allow string literals using double quotes (
" "
) as a shortcut. - Example:
"Hello"
,"world"
.
- Prolog handles strings as a list of ASCII character codes. Alternatively, modern implementations allow string literals using double quotes (
-
Functors
- A functor is the name of a compound term. It represents a function or relation, but in Prolog, it’s more of a structural building block.
- Example:
person(john, 30)
, whereperson
is the functor.
-
Facts, Rules, and Queries
- Facts: A basic data structure that defines relationships between entities.
parent(john, mary).
- Rules: A structure that defines a relationship based on conditions.
sibling(X, Y) :- parent(Z, X), parent(Z, Y).
- Queries: Structure used to ask questions of the Prolog system.
?- parent(john, mary).
- Facts: A basic data structure that defines relationships between entities.