Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Prolog, is a fact the same as a functor?

Tags:

prolog

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, ...

like image 228
Lieven Cardoen Avatar asked Oct 01 '13 12:10

Lieven Cardoen


3 Answers

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).

like image 73
hardmath Avatar answered Nov 02 '22 00:11

hardmath


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.

like image 28
Alexey Romanov Avatar answered Nov 02 '22 00:11

Alexey Romanov


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.

like image 38
Cephalopod Avatar answered Nov 02 '22 01:11

Cephalopod