In universal algebra and mathematical logic, a **term algebra** is a freely generated algebraic structure over a given signature.^{[1]}^{[2]} For example, in a signature consisting of a single binary operation, the term algebra over a set *X* of variables is exactly the free magma generated by *X*. Other synonyms for the notion include **absolutely free algebra** and **anarchic algebra**.^{[3]}

From a category theory perspective, a term algebra is the initial object for the category of all *X*-generated algebras of the same signature, and this object, unique up to isomorphism, is called an initial algebra; it generates by homomorphic projection all algebras in the category.^{[4]}^{[5]}

A similar notion is that of a **Herbrand universe** in logic, usually used under this name in logic programming,^{[6]} which is (absolutely freely) defined starting from the set of constants and function symbols in a set of clauses. That is, the Herbrand universe consists of all ground terms: terms that have no variables in them.

An atomic formula or **atom** is commonly defined as a predicate applied to a tuple of terms; a ground atom is then a predicate in which only ground terms appear. The **Herbrand base** is the set of all ground atoms that can be formed from predicate symbols in the original set of clauses and terms in its Herbrand universe.^{[7]}^{[8]} These two concepts are named after Jacques Herbrand.

Term algebras also play a role in the semantics of abstract data types, where an abstract data type declaration provides the signature of a multi-sorted algebraic structure and the term algebra is a concrete model of the abstract declaration.

A **type** is a set of function symbols, with each having an associated arity (i.e. number of inputs). For any non-negative integer , let denote the function symbols in of arity . A constant is a function symbol of arity 0.

Let be a type, and let be a non-empty set of symbols, representing the variable symbols. (For simplicity, assume and are disjoint.) Then the **set of terms** of type over is the set of all well-formed strings that can be constructed using the variable symbols of and the constants and operations of . Formally, is the smallest set such that:

- — each variable symbol from is a term in , and so is each constant symbol from .
- For all and for all function symbols and terms , we have the string — given terms , the application of an -ary function symbol to them represents again a term.

The **term algebra** of type over is, in summary, the algebra of type that maps each expression to its string representation. Formally, is defined as follows:^{[9]}

- The domain of is .
- For each nullary function in , is defined as the string .
- For all and for each
*n*-ary function in and elements in the domain, is defined as the string .

A term algebra is called *absolutely free* because for any algebra of type , and for any function , extends to a unique homomorphism , which simply evaluates each term to its corresponding value . Formally, for each :

- If , then .
- If , then .
- If where and , then .

As an example type inspired from integer arithmetic can be defined by , , , and for each .

The best-known algebra of type has the natural numbers as its domain and interprets , , , and in the usual way; we refer to it as .

For the example variable set , we are going to investigate the term algebra of type over .

First, the set of terms of type over is considered. We use red color to flag its members, which otherwise may be hard to recognize due to their uncommon syntactic form. We have e.g.

- , since is a variable symbol;
- , since is a constant symbol; hence
- , since is a 2-ary function symbol; hence, in turn,
- since is a 2-ary function symbol.

More generally, each string in corresponds to a mathematical expression built from the admitted symbols and written in Polish prefix notation; for example, the term corresponds to the expression in usual infix notation. No parentheses are needed to avoid ambiguities in Polish notation; e.g. the infix expression corresponds to the term .

To give some counter-examples, we have e.g.

- , since is neither an admitted variable symbol nor an admitted constant symbol;
- , for the same reason,
- , since is a 2-ary function symbol, but is used here with only one argument term (viz. ).

Now that the term set is established, we consider the term algebra of type over . This algebra uses as its domain, on which addition and multiplication need to be defined. The addition function takes two terms and and returns the term ; similarly, the multiplication function maps given terms and to the term . For example, evaluates to the term .

As an example for unique extendability of a homomorphism consider defined by and . Informally, defines an assignment of values to variable symbols, and once this is done, every term from can be evaluated in a unique way in . For example,

In a similar way, one obtains .

Main articles: Herbrand interpretation and Herbrand structure |

The **signature** *σ* of a language is a triple <*O*, *F*, *P*> consisting of the alphabet of constants *O*, function symbols *F*, and predicates *P*. The **Herbrand base**^{[10]} of a signature σ consists of all ground atoms of *σ*: of all formulas of the form *R*(*t*_{1}, ..., *t*_{n}), where *t*_{1}, ..., *t*_{n} are terms containing no variables (i.e. elements of the Herbrand universe) and *R* is an *n*-ary relation symbol (*i.e.* predicate). In the case of logic with equality, it also contains all equations of the form *t*_{1} = *t*_{2}, where *t*_{1} and *t*_{2} contain no variables.

This article may be confusing or unclear to readers. Please help clarify the article. There might be a discussion about this on the talk page. (October 2018) (Learn how and when to remove this template message)

Term algebras can be shown decidable using quantifier elimination. The complexity of the decision problem is in NONELEMENTARY because binary constructors are injective and thus pairing functions.^{[11]}