Manipulating Terms

This section includes predicates for identifying, comparing and manipulating terms. The general categories are:

  • Term Classification
  • Comparison of Terms
  • Equality of Terms
  • Dissecting Terms
  • Term Classification

    The following predicates may be used to classify terms in Prolog:

    atom(X)

    atom succeeds if X is currently instantiated to an atom; else it fails.

    atomic(X)

    atomic succeeds if X is currently instantiated to an atom or an integer; else it fails.

    defined(ExtendedPredicate)

    defined succeeds if an extended predicate is defined . This is useful for testing whether an LSX, for example, is available for use.

    float(X)

    float succeeds if X is currently instantiated to a float. Can also be used in arithmetic expressions to convert a value to a float.

    integer(X)

    integer succeeds if X is currently instantiated to an integer. Can also be used in arithmetic expressions to convert a value to an integer.

    list(X)

    list succeeds if X is currently instantiated to a list; else it fails. If X is the empty list, [ ], list fails.

    islist(X)

    islist succeeds if X is instantiated to a list OR the empty list, [].

    long(X)

    succeeds if X is a long integer

    nonvar(X)

    nonvar succeeds if X is not an unbound variable; else it fails.

    number(X)

    number succeeds if X is currently instantiated to an integer or float.

    short(X)

    succeeds if X is a short integer

    string(X)

    string succeeds if X is currently instantiated to a string; else it fails.

    structure(X)

    structure succeeds if X is currently instantiated to a structure; else it fails.

    var(X)

    var succeeds if X is an unbound variable; else it fails.

    Comparison of Terms

    Two terms may be compared via the standard ordering.

    X @< Y

    X @< Y succeeds if X is less than Y in the standard order

    X @> Y

    X @> Y succeeds if X is greater than Y in the standard order

    X @=< Y

    X @=< Y succeeds if X is less or equal to Y in the standard order

    X @>= Y

    X @>= Y succeeds if X is greater or equal to Y in the standard order

    compare(Result, Term1, Term2)

    compare compares terms Term1 and Term2 using the standard ordering. Unifies Result with ==, < or > depending on whether the terms are equal, or Term1 @< Term2 or Term1 @> Term2.

    Equality of Terms

    Equality is determined with the following predicates:

    X = Y

    X = Y succeeds if X can be unified with Y.

    X \= Y

    X \= Y succeeds if X cannot be unified with Y.

    X == Y

    X == Y succeeds if X and Y are identical, i.e., they unify with no variable bindings occurring.

    X \== Y

    X \== Y succeeds if X and Y are not identical.

    Dissecting Terms

    These predicates are useful for breaking apart and constructing generic terms when you don't know the specifics of their structure. A good example of this is a pretty printer.

    arg(N, Term, Argument)

    N must be instantiated to a positive integer less than or equal to the number of arguments in the compound term Term. Argument is then instantiated to the Nth argument of Term. If all these conditions are not met, the goal fails. For example:

    functor(Term, Functor, N)

    There are two cases to consider:

    For example:

    gensym(Root, Sym)

    gensym/2 is used to create atom names on the fly. The names are formed by adding successive integers to the root. gensym is designed to create new symbols each time it is called; it fails on backtracking. For example

    numbervars(Term, Start, End)

    numbervars converts the non-anonymous variables into numbered atoms. Start must be instantiated to a non-negative integer. Any non-anonymous variables (i.e. those appearing only once) occurring in Term are bound to an atom of the form _I. End is instantiated to Start plus the number of distinct variables in Term.

    I is determined by Start and the index of the variable.

    For example:

    Term =.. List (univ)

    The operator "=.." is called "univ." "Term =.. List" converts between compound terms Term and the list List. If Term is instantiated to a compound term, then List is unified with the list whose first element is the principal functor of Term, and whose successive elements are the arguments of Term. An atom is treated as a compound term of arity 0.

    If Term is a variable, List must be instantiated to a list of definite length whose first element is an atom. Then Term is unified with the structure whose principal functor is the head of List and whose arguments are the elements in the tail of List.

    univ fails if the arguments do not make legal structures.

    varlist(List)

    varlist/1 returns a list of the variable names passed to the current term. Each entry in the list is a list itself. For example:

    varsof(Term, List)

    varsof/2 succeeds if List can be unified with a list of all uninstantiated (and non-anonymous) variables in Term. Each variable occurs only once in List even though it may have more than one occurrence in Term.

    For example:

    Copyright ©1987-2000 Amzi! inc. All Rights Reserved.