What's the difference between functor, a fact, a predicate and a rule in Prolog?
To address your given example:
brother(john, jack).
^ ^ ^
functor | |
argument |
argument
\________ ________/
\/
fact/predicate
brother/2
is a predicate AND a fact as well (see 3.72 fact in @GuyCoder's quote from the standard) since you can write it as a clause with a body that consists of the single goal true
:
brother(john, jack) :- % <- head of the clause
true. % <- body of the clause
The predicate brother
in your example has two arguments, therefore the arity 2 and is referred to as brother/2 (see 3.129 predicate in @GuyCoder's post). The name or identifier of the predicate brother/2 is also called the functor of the predicate (see 3.77 functor; note that 3.77 and 3.129 use the same definition). You can think of a fact as a special kind of predicate that you can define without rules.
If you had some facts parent_of/2
and male/2
and defined a predicate brother_of/2
based on those, e.g...
brother_of(X,Y) :- % X is brother of Y if
dif(X,Y), % X and Y are different AND
male(X), % X is male AND
parent_of(P,X), % X has a parent P AND
parent_of(P,Y). % Y has the same parent P
... then the above definition constitutes a rule because the body of the clause is not the goal true
(see 3.154 rule). The rule above consists of the following parts:
brother_of(X,Y) :- % head of the rule
dif(X,Y), % goal \
male(X), % goal \ body of
parent_of(P,X), % goal / the rule
parent_of(P,Y). % goal /
The head of the rule is followed by :-
which is an implication arrow pointing towards the head of the rule and the goals are separated by ,
which is a logical AND (conjunction). So the body of a rule consists of a single goal or a conjunction of goals and the body of a rule implies the head of the rule. Hence you can read the above definition of brother_of/2 as a logic formula:
brother_of(X,Y)
← dif(X,Y)
∧ male(X)
∧ parent_of(P,X)
∧ parent_of(P,Y)
If you come from mathematical logic you might find it helpful to recall that a predicate is defined as a boolean-valued function, that is, a function that maps its arguments to true or false. So a predicate is the characteristic function of a relation (see Predicate (mathematical logic). Since you can query facts and get true
/false
as an answer, they constitute predicates. You can observe this when you query your definition of brother/2:
?- brother(john,jack).
true. % <- maps to true
?- brother(john,jason).
false. % <- maps to false
If you issue queries with variables, you get substitutions for said variables that make the predicate true instead of the answer true
, e.g.:
?- brother(X,Y).
X = john, % these substitutions for X and Y
Y = jack. % map to true
A final remark: When talking about Prolog the terms predicate and relation are often used interchangeably and it is quite helpful to think about writing predicates as describing relations (see the comments in the definition of brother_of/2 above). Hence, for the above queries it is also appropriate to say: The relation brother(john,jack)
holds. The relation brother(john,jason)
does not hold. The relation brother(X,Y)
holds for the substitutions X=john
and Y=jack
.
From
ISO/IEC 13211-1 First edition 1995-06-01
Information technology - Programming languages - Prolog -
Part 1:
General Core
3.9 arity: The number of arguments of a compound term. Syntactically, a non-negative integer associated with a functor or predicate.
3.19 body: A goal, distinguished by its context as part of a rule (see 3.154).
3.32 clause: A fact or a rule. It has two parts: a head, and a body.
3.37 compound term: A functor of arity N, N positive, together with a sequence of N arguments.
3.72 fact: A clause whose body is the goal true.
NOTE - A fact can be represented in Prolog text by a term
whose principal functor is neither (:-)/1
nor (:-)/2
.
3.77 functor: An identifier together with an arity.
3.81 goal: A predication which is to be executed (see body, query, and 7.7.3).
3.84 head (of a rule): A predication, distinguished by its context.
3.88 identifier: A basic unstructured object used to denote an atom, functor name or predicate name.
3.129 predicate: An identifier together with an arity.
3.133 predication: A predicate with arity N and a sequence of N arguments.
3.143 query: A goal given as interactive input to the top level.
3.154 rule: A clause whose body is not the goal true.
During execution, if the body is true for some substitution,
then the head is also true for that substitution. A rule
is represented in Prolog text by a term whose principal
functor is (:-)/2
where the first argument is converted
to the head, and the second argument is converted to the
body.
So brother(john, jack).
is a fact.
The difference between a term and a predicate (or goal) can be subtle. They look the same and are distinguished by context. For example:
foo(1).
foo(2).
foo_results(Results) :- setof(X, foo(X), Results).
?- foo_results(Results).
Results = [1, 2].
The predicate foo/1
has two clauses. The predicate foo_results/1
calls this, but indirectly ... the setof/3
meta-predicate takes a term as its second argument, which it interprets as a predicate. In other words, a functor (name/arity) is the skeleton of a kind of term; some meta-predicates (e.g., call/1
, setof/3
, etc.) can interpret a term (functor/arity) as a predicate.