11

If you have for instance next line of Prolog declaration:

move(state(middle, onbox, middle, hasnot),
     grasp,
     state(middle, onbox, middle, has)).

Are both move and state functors?

I'm kind off confused by facts, functors, terms, ...

Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244

3 Answers3

9

In Prolog functors are syntactic elements we use to build structures (compound terms) from simpler ones.

Think of a hierarchy of bound Prolog terms, with the base containing the simplest "atomic" cases, i.e. atoms and numbers. Add to these Prolog variables, which may be bound or not depending on context. The rules for Prolog functor names (identifiers) are the same as for Prolog atoms

Functors are syntactic units that have a finite number of arguments ("arity"), and if a functor is supplied with terms for those arguments, then we get a compound term. In your example there is a principal functor move with three arguments, so its arity is 3. The functor name and arity are often combined, since technically Prolog treats the same functor name with two distinct arities as different functors, and so we might refer to move/3 as the outer functor of your compound term.

Note that the first and third arguments in your example of a term are themselves compound terms, built using functor state/4:

move(state(middle, onbox, middle, hasnot),
     grasp,
     state(middle, onbox, middle, has))

Here I removed the period from the end of your example. Likely in its original context this was indeed a Prolog "fact", although it might also have been a query. Periods in Prolog can serve terminate input of a term.

The key point here is that Prolog predicates are special cases of functors. If move/3 is a predicate (taking three arguments), then your example could be a fact (if it is asserted someplace in your application) or a query (e.g. if posed as a top-level goal). In accordance with what we said above about a functor name with two distinct arities, Prolog treats a predicate name with different arities as different predicates!

Prolog's use of functors to express facts (and rules) makes it a fairly slick and powerful metaprogramming environment. Prolog has a special built-in predicate (operator =..) called univ that unpacks a compound term into a list whose head is the principal functor's name and whose remaining items are the arguments given to that functor in the specific compound term. For example:

?- X = state(middle, onbox, middle, hasnot), X =.. List.

X = state(middle, onbox, middle, hasnot)
List = [state, middle, onbox, middle, hasnot]

yes

This allows us to convert between Prolog lists and compound terms in both directions, and to build Prolog goals "dynamically" if need be (using a predicate name for the principal functor of a term).

Community
  • 1
  • 1
hardmath
  • 8,753
  • 2
  • 37
  • 65
  • It's getting clearer and clearer. Need to make a mind shift and that can take some days. I'm beginning to get the point. Is a variable also a term, whether it is bound or not? – Lieven Cardoen Oct 01 '13 at 18:20
  • 1
    Yes, a variable is a Prolog term also (as are lists). – hardmath Oct 02 '13 at 03:52
  • Might be interested in Haskell Curry's book *Foundations of Mathematical Logic* where he says *...a functor is a means of combining phrases to form other phrases...* – 147pm Jan 20 '22 at 17:23
8

Yes, move and state are functors. A functor is F in F(Term1, ...). But they aren't facts: in your case, there is only one fact, which is the complete line.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
4

Functors describe a term, but are not the term itself (like method signatures in imperative languages) and consist of the structure's or predicate's name and arity.

In you example, the functors are move/3 and state/4.

Notice that foo(a) and foo(a,b) have different functors, foo/1 and foo/2.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70