# functors - PROLOG

This is a discussion on functors - PROLOG ; Is the concept of functors in Prolog similar to functors in Javascript? I noticed that in the solutions Markus provided, he seems to prefer to construct X-Y-Z instead of retaining the original terms in the results. (Apologies if I used ...

1. ## functors

Is the concept of functors in Prolog similar to functors in
Javascript? I noticed that in the solutions Markus provided, he seems
to prefer to construct X-Y-Z instead of retaining the original terms
in the results. (Apologies if I used the wrong words, please correct
me).

i.e.

height(john, 160).
height(mary, 125).
height(joe, 180).

% ------ case 1 --
% preferred,
findall(Name-Height, height(Name, Height), Bag).

% ------ case 2 --
% ... rather than
findall(height(Name, Height), height(Name, Height), Bag).

if there was a term like this:

height(anybody, _).

In case 2, is there a new "height(Name, Height)" term being created,
or it should not matter?

Thanks. Sorry for the confused tone of the post.

2. ## Re: functors

Chui Tey <teyc@cognoware.com> writes:

> to prefer to construct X-Y-Z instead of retaining the original terms

X-Y-Z is rarely a good choice; it's infix syntax for the term
-(-(X,Y), Z) (that is: a term with functor "-" and arity 2, whose
first argument is a term with functor "-" and arity 2 [whose first
argument is X and second is Y] and second is Z), and that is seldom
useful. It's clearer and cheaper to use a term like triple(X, Y, Z).

> findall(Name-Height, height(Name, Height), Bag).

It's common to use the functor -/2 for pairs, and several Prolog
built-in predicates (like keysort/2) work on such structures.

> if there was a term like this:
>
> height(anybody, _).

height/2 is a pretty bad name for a relation; better call it
object_height/2 or person_height/2 etc. The above fact contains two
terms: The atom 'anybody', and an anonymous variable. There is NO term
with functor "height" in it, as would be the case in a fact like:

factum(height(anybody,_)).

> In case 2, is there a new "height(Name, Height)" term being created,
> or it should not matter?

In case 2, you get a term height(X,Y) for each solution. In case 1,
you get a term -(X, Y) for each solution. Whether you should use
"height" or "-" as functor depends on what you want. One advantage of
using "-" is that you can use said built-in predicates straight away.

--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/

3. ## Re: functors

1) If it isn't a fact, how come I get this...

?- height(X, Y).

X = martha,
Y = 165

X = ben,
Y = 165 ;

X = mark,
Y = 170 ;

X = sue,
Y = 170 ;

X = britt,
Y = 175 ;

X = herb,
Y = 175 ;

X = anybody ;

>
> factum(height(anybody,_)).
>

2) SWI-Prolog gave me this:

?- factum(height(anybody, Y)).
ERROR: Undefined procedure: factum/1

4. ## Re: functors

On Sep 27, 5:24 am, Chui Tey <t...@cognoware.com> wrote:
>
> 1) If it isn't a fact, how come I get this...
>
> ?- height(X, Y).
>
> X = martha,
> Y = 165

[etc., snip]

Markus said there was no "term" height(...), not that
you have no "fact" of this kind.

There is a somewhat confusing, even ambiguous distinction
in Prolog between the functors which give rise to nonatomic
terms and the predicates (relations) which appear in

Think of terms as nouns and predicates as verbs. Together
they form clauses, which are like complete sentences.

In the fact:

height(martha, 165).

we have a predicate height/2 applied to two terms:
martha (an atom), and 165 (an integer). Both martha
and 165 are called atomic terms, in the sense that neither
has any constituent parts.

Now a functor is way of constructing terms that have
parts. Most Prolog implementations do not require an
explicit declaration that an identifier is a functor
(nor for that matter that one is a predicate). It is
left to the compiler to deduce which is which.

So you might want a functor that joins two terms, say
martha and 165, together and makes a compound term
(or structure) with those parts:

person_cm(martha,165)

The identifiers for functors and predicates satisfy
pretty much the same requirements, which partly why
I said that the distinction between them is ambiguous.
But there is a deeper ambiguity, in that the list to
term "assignment" operator =.. works equally well
with functors and predicates. If you use a predicate
in place of functor as the head of the list, then it
will construct a clause-like term that is suitable
for passing to call, once, findall, etc. as a goal
to be satified. For example:

?- X =.. [person_cm, martha, 165]

X = person_cm(martha,165)
yes

?- X =.. [height, martha, 165]

X = height(martha,165)
yes

So it should perhaps be acknowledged that Prolog
implementations by and large treat predicate
identifiers as a special kind of functor identifier,
and that predicates differ from ordinary functors
mainly in that they give rise to clause-like terms
that can be invoked as goals.

regards, chip