Terms
Prolog programs are constructed from terms. A term is either a constant, a structure or a variable.
Constants
Constants represent a specific object or relationship. There are two kinds of constants - atoms and numbers.
An alpha-numeric atom consists of a lower case letter followed by zero or more alphabetic characters, digits or underscores (_
). A quoted atom is any sequence of characters enclosed in single quotes ('
). Examples of valid Prolog syntax for defining atoms are: a
, a_b
, 'a b'
, '1'
and '@=<'
.
Examples of valid Prolog syntax for defining numbers are: 0
, 143650
, -1
, 785.00234
, -2.4e89
and 657E89
.
Structures
Structures, also known as compound terms, consist of a functor (name) and a number of arguments (other terms). The number of arguments a structure has is known as its arity. A structure is defined in Prolog by specifying its functor followed by its arguments separated by commas and enclosed in round brackets. Examples of valid Prolog syntax for defining structures are: p()
, p(a)
and p(X, a, q(1,2))
.
A special type of structure is the list.
Variables
Variables represent a term we do not yet know. When the term the variable represents is unknown the variable is uninstantiated. When the term the variable represents is known the variable is instantiated. Variables have names beginning with a capital letter. Examples of valid Prolog syntax for defining variables are: X
, ABC
, Name
and Date_of_Birth
.
A special type of variable is the anonymous variable. An anonymous variable is useful when a variable is required but its name will never be used. An anonymous variable starts with an underscore. Examples of valid Prolog syntax for specifying an anonymous variables are: _
and _zYz123
.
Clauses
Clauses are the structural elements of a program. A Prolog programmer develops a program by writing a collection of clauses in a text file. The programmer then uses the consult
command, specifying the name of the text file, to load the clauses into the Prolog environment.
The are two types of clauses - facts and rules.
Facts
A fact is an atom or structure followed by a full stop. Examples of valid Prolog syntax for defining facts are: cold.
, male(homer).
and father(homer,bart).
.
Rules
A rule consists of a head and a body. The head and body are separated by a :-
and followed by full stop. If the body of a clause is true then the head of the clause is true. Examples of valid Prolog syntax for defining rules are: bigger(X,Y) :- X > Y.
and parents(F,M,C) :- father(F,C), mother(M,C).
Queries
Queries allow us to ask questions of the Prolog environment. The Prolog environment will try to answer the query by using the facts and rules that have been loaded into it. Prolog queries start with a ?-
and end with a full stop. When using the console there is no need to type in the ?-
as it is automatically added for you. Examples of valid Prolog syntax for specifying a query are: ?- 1 < 2.
and ?- father(F,C).
Examples
Facts.
father(homer,bart).
father(homer,lisa).
father(homer,maggie).
mother(marge,bart).
mother(marge,lisa).
mother(marge,maggie).
father(ned,rod).
father(ned,todd).
male(homer).
male(bart).
male(ned).
male(rod).
male(todd).
Rules.
parents(F,M,C) :- father(F,C), mother(M,C).
siblings(A,B) :- parents(F,M,A), parents(F,M,B).
brother(A,B) :- siblings(A,B), male(B).
Simple questions - true if the fact exists in the knowledge base, else false.
?- father(homer,bart).
yes
?- father(homer,lisa).
yes
?- father(homer,maggie).
yes
?- father(homer,rod).
no
?- father(homer,ralph).
no
Question using a variable.
?- father(homer,C).
C = bart
yes;
C = lisa
yes;
C = maggie
yes
?- father(ned,C).
C = rod
yes;
C = todd
yes
Question using two variables - evaluation of the query uses backtracking to find multiple solutions.
?- father(F,C).
C = bart
F = homer
yes;
C = lisa
F = homer
yes;
C = maggie
F = homer
yes;
C = rod
F = ned
yes;
C = todd
F = ned
yes
?- parents(homer,marge,C).
C = bart
yes;
C = lisa
yes;
C = maggie
yes
?- brother(lisa,X).
X = bart
yes;
no
?- brother(bart,X).
X = bart
yes;
no