4.21 Analysing and Constructing Terms
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Analysing and Constructing Terms
          • functor/3
          • functor/4
          • arg/3
          • =../2
          • compound_name_arity/3
          • compound_name_arguments/3
          • numbervars/3
          • numbervars/4
          • var_number/2
          • term_variables/2
          • nonground/2
          • term_variables/3
          • term_singletons/2
          • is_most_general_term/1
          • copy_term/2
          • copy_term/4
          • copy_term_nat/4
          • term_factorized/3
          • term_factorized/4
          • term_minimal/2
          • term_automaton/2
          • automaton_minimal/2
          • automaton_minimal/3
          • Non-logical operations on terms
    • Packages
Availability:built-in
[det]term_automaton(?Term, ?Automaton)
True when Automaton is the term graph of Term written out as an explicit automaton, and Term is the term its start state denotes. Which way the predicate runs is decided by Automaton: it answers Automaton if that is unbound, and Term if it is not.

Automaton is a compound whose argument I is the colour of state I, and state 1 is the start state. An argument that is a compound gives the state one successor per argument of that compound, and the argument is the number of the state the successor goes to. An argument that is not a compound makes the state a sink holding that value. So a leaf is a state rather than an argument, which is why nothing in an argument position is ever anything but a state number:

?- term_automaton(f(g(a),g(a)), A).
A = automaton(f(2, 3), g(4), g(4), a).

The translation is faithful in both directions: one state per physically distinct compound cell of Term and one per distinct leaf, sharing what Term already shares and collapsing nothing. Term may be cyclic, and so may Automaton:

?- X = f(X), term_automaton(X, A).
A = automaton(f(1)).

The functor name of Automaton is not looked at when it is read; like arg/3, only the arity matters. The name used when it is written is always automaton. Note that the value of a sink can never be a compound, so a state label that is a compound has to be taken apart into states of its own.