2
votes

I am beginning to learn Prolog. I have various books and I even bought the standard. I like things like standards because they define things precisely. But I can't understand some concepts. In particular the difference between functors and predicates, and what is a goal exactly ? Typically the books use a terminology which is varying and sometimes not precise enough adding to the confusion.

In the standard a predication is defined as "a predicate with arity N and a sequence of N arguments" and a predicate as "an identifier together with an arity". Thus when they write something like father(Father, Child) in The Art of Prolog 2nd, they call father/2 a predicate.

OK but when I check the syntax, we talk about compound terms (not predicates or predications) which are formed of a functor name and arguments. So I checked the definitions in the Standard and saw that a functor is "an identifier together with an arity", the same definition as predicate, and a compound term as "a functor of arity N, N positive, together with a sequence of N arguments".

Thus what is the relation between functors and predicates ? Syntactically they look the same. Why father(Father, Child) couldn't be called a compound term (I guess it is indeed, syntactically) with functor father/2 instead of predicate father/2 ? When do we use one terminology or the other ?

And to put the cherry on top, a goal is "a predication which is to be executed". Do we talk about goals inside the Prolog program or only in the query ? In The Art of Prolog, it is written that "goals are atoms or compound terms". Thus could we talk about goals everywhere in the Prolog program ? My readings make me feel that it is used in the query only. But I'm not sure.

In a query, I guess mother(X, Y) is a goal and mother/2 a predicate but is mother(X, Y), male(Y) a goal ? Or composed of two goals ? It is said that a goal can be an atom or a compound term. But if we look at , in functional notation then we have a compound term and then a single goal composed of two sub-goals ? I don't understand.

To put it briefly, I don't know when to call things predicate (predication), functor (compound term) or goal.

EDIT

Having read the comments, the answer and other questions (28972038 and 15807506), I came to the conclusion that we have a situation similar to the one in Lisp-like languages : the same notation is used for "executable" code and data. Thus everything is written as terms and a lot of them are compound terms which involves functors and arguments. This is the syntax of data. And the same syntax is used for clauses (facts and rules) which compose a Prolog program. We talk about functors when we focus on the data side of things and predicates when we focus on the "executable" (or interpretable ) side of things, the meaning of the logic program. The focus in question usually depends on the context in which the constructs are used.

1
One of the most interesting and useful things about Prolog is that clauses (= rules and facts) have a natural representation as Prolog terms. For example, a Prolog rule is a Prolog term, with functor :-/2 and two arguments: head and body. Both head and body are again Prolog terms. In addition, I would like to add that father/2 is a pretty ambiguous name for a predicate with two arguments: father_child/2 would be much better, because it makes clear what the arguments actually mean.mat
Thanks for the comment, it helped too.Ludovic Kuty
Thanks. I didn't find it when looking for similar questionsLudovic Kuty
One thing still wrong in your description is the role of functors: A functor is always part of a term. If you are ever unsure about the exact form of your terms, use write_canonical/1 in ISO-compliant Prolog systems to see the canonical syntax of a term. For example: ?- write_canonical(my_rule(X, Y) :- father_child(X, Y))., yielding: :-(my_rule(A,B),father_child(A,B)).mat
Atoms are not compound terms, but both atoms and compound terms are terms! Therefore, you can simplifiy some of your statements above, and in some cases "compound term" is simply too limiting: For example, a goal need not have any arguments at all.mat

1 Answers

0
votes

I try to explain this to my students in the following way: What is father(X,Y) in the following cases (independent from the missing context...)?

?- isA(father(X,Y)).

and

?- father(X,Y).

In the first case, it's the father functor that constructs a binary term, in the second case, it's a binary predicate.

The confusion (that they look the same) is also used actively when you go beyond plain Prolog and to things like:

?- X = father, X(tim,john).

or using father/2 in a findall query.