1 Introduction
The superior expressive power of higher-order functional programing languages with respect to their first-order counterparts has been thoroughly demonstrated by Jones (Reference Jones2001). In logic programing, the first result of this type was established by Charalambidis et al. Reference Charalambidis, Nomikos and Rondogiannis(2019), where it was demonstrated that positive Higher-Order Datalog programs can capture broader complexity classes as their order increases. In particular, it was demonstrated that for all
$k\geq 1$
,
$(k+1)$
-Order Datalog captures
$k-\textsf {EXP}$
, under the assumption that the input database is ordered. The aforementioned result generalized a classical expressibility theorem which states that (first-order) Datalog captures
$\textsf {P}$
(Vardi Reference Vardi1982; Papadimitriou Reference Papadimitriou1985; Immerman Reference Immerman1986; Leivant Reference Leivant1989; Grädel Reference GrÄdel1992), again under the assumption that the input database is ordered. Notice that the ordering assumption underlying the above results, is actually a rather strong one because it allows (even weak) declarative query languages to simulate the ordering of the tape of a Turing machine.
Recently, Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024) defined the well-founded and the stable model semantics of Higher-Order Datalog with negation and illustrated its expressive power with non-trivial examples. Remarkably, one such example was the Generalized Geography two-player game, which is a well-known (Lichtenstein and Sipser Reference Lichtenstein and Sipser1980)
$\mathsf{PSPACE}$
-complete problem. The examples given in Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024) do not seem to require any ordering of the input and solve the corresponding problems declaratively. As noted by Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024), such examples indicate “that higher-order logic programming under the stable model semantics is a powerful and versatile formalism, which can potentially form the basis of novel ASP systems.” Such systems may be able to cope with demanding problems that arise in combinatorial optimization, game theory, machine learning theory, and so on (see, e.g.,, the discussion in Bogaerts et al. Reference Bogaerts, Janhunen and Tasharrofi(2016); Amendola et al. Reference Amendola, Ricca and Truszczynski(2019)). The results of Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024) trigger the natural question of the exact characterization of the expressive power of Higher-Order Datalog with negation and of whether we can obtain expressibility results that do not rely on the ordering assumption.
In this paper we undertake the formal study of the expressive power of Higher-Order Datalog with negation – which in the rest of the paper is denoted by Higher-Order
$Datalog^\neg$
. The results we obtain indeed demonstrate that negation is a very powerful construct of the language, justifying the increased expressiveness which was conjectured by Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024). Our results, which will be explained in detail in the rest of the paper, are presented in Table 1. All the results in the table are new, with the exception of those results concerning programs of order 1 (which are well-known, see e.g., Dantsin et al. Reference Dantsin, Eiter, Gottlob and Voronkov(2001); Niemelä (Reference NiemelÄ, Garcia de la Banda and Pontelli2008)). In particular, the main contributions of the paper can be summarized as follows:
Table 1.
Expressive power of Higher-Order
$Datalog^\neg$
(with no ordering assumption)

-
• We establish that
$(k+1)$ -Order
$Datalog^\neg$ captures
$k-\textsf {EXP}$ under the well-founded semantics. Notably, this result holds without requiring a predefined ordering of the input database: the use of existential predicate variables, facilitates the construction of such an ordering. Furthermore, we show that even in the fragment of the language without existential predicate variables (denoted by HO-Datalog
$^{\neg ,\not \exists }$ in Table 1), the same result can be achieved through partial applications and an enumeration procedure. Perhaps even more strikingly, the
$k-\textsf {EXP}$ expressibility result also holds for a stratified fragment of
$(k+1)$ -Order Datalog
$^{\neg ,\not \exists }$ . This last result is especially unexpected, considering that the stratified fragment of classical (first-order)
$Datalog^\neg$ exhibits strictly lower expressive power than
$Datalog^\neg$ under the well-founded semantics (Kolaitis Reference Kolaitis1991).
-
• We demonstrate that
$(k+1)$ -Order
$Datalog^\neg$ captures
$\textsf {co}-(k-\textsf {NEXP})$ under the stable model semantics using cautious reasoning and
$k-\textsf {NEXP}$ under brave reasoning. As before, these two results hold with or without the presence of existential predicate variables. Additionally, the two results hold within a fragment of
$(k+1)$ -Order
$Datalog^\neg$ that consists of programs that have a stratified part together with a simple unstratified part of a very specific form (choice rules). In other words, we prove that the expressive power, under the stable model semantics, of
$(k+1)$ -Order
$Datalog^\neg$ is equivalent to the power of the aforementioned restricted fragment.
-
• Since it is well-known that
$(k-1)-\textsf {EXP} \subseteq (k-1)-\textsf {NEXP} \subseteq k-\textsf {EXP}$ and
$(k-1)-\textsf {EXP} \subseteq \textsf {co}-((k-1)-\textsf {NEXP}) \subseteq k-\textsf {EXP}$ ,
$k$ -Order
$Datalog^\neg$ programs under the well-founded semantics are at most as powerful as
$k$ -Order
$Datalog^\neg$ programs under the stable model semantics, which, in turn, are at most as powerful as
$(k+1)$ -Order
$Datalog^\neg$ programs under the well-founded semantics (under both the brave and cautious reasoning schemes). This observation illustrates an interesting trade-off between order and non-determinism in the context of higher-order logic programing: by increasing the order of our programs while using well-founded semantics, we can surpass the expressive power provided by non-determinism in lower-order programs under the stable model semantics.
The rest of the paper is structured as follows: Section 2 introduces the language we will be studying. Section 3 derives the expressive power of Higher-Order
$Datalog^\neg$
under the well-founded semantics and Section 4 the power under the stable model semantics. Section 5 presents a semantics-preserving transformation that eliminates existential predicate variables from clause bodies; this transformation implies that our results hold even without the presence of existential predicate variables. Finally, Section 6 concludes the paper giving pointers for future work.
2 Higher-Order Datalog with negation: preliminaries
In this section we define the syntax of Higher-Order
$Datalog^\neg$
. The language uses two base types:
$o$
, the Boolean domain, and
$\iota$
, the domain of data objects. The composite types are partitioned into predicate ones (assigned to predicate symbols) and argument ones (assigned to parameters of predicates).
Definition 2.1. Types are either predicate or argument, denoted by
$\pi$
and
$\rho$
respectively, and defined as:

As usual, the binary operator
$\to$
is right-associative. It can be easily seen that every predicate type
$\pi$
can be written in the form
$\rho _1 \to \cdots \to \rho _n \rightarrow {o}$
,
$n\geq 0$
(for
$n=0$
we assume that
$\pi ={o}$
).
Definition 2.2. The alphabet of Higher-Order
$Datalog^\neg$
consists of: predicate variables of every predicate type
$\pi$
(denoted by capital letters such as
$\mathtt {P},\mathtt {Q},\ldots )$
; predicate constants of every predicate type
$\pi$
(denoted by lowercase letters such as
$\mathtt {p},\mathtt {q},\ldots$
); individual variables of type
$\iota$
(denoted by capital letters such as
$\mathtt {X},\mathtt {Y},\ldots$
); individual constants of type
$\iota$
(denoted by lowercase letters such as
$\mathtt {a},\mathtt {b},\ldots$
); the equality constant
$\approx$
of type
${\iota } \to {\iota } \to o$
; the conjunction constant
$\wedge$
of type
${o} \to {o} \to {o}$
; the inverse implication constant
$\leftarrow$
of type
${o} \to {o} \to {o}$
; and the negation constant
$\texttt { not}$
of type
${o} \to {o}$
.
Arbitrary variables (either predicate or individual ones) will be denoted by
$\mathsf{R}$
.
Definition 2.3. The expressions and literals of Higher-Order
$Datalog^\neg$
are defined as follows. Every predicate variable/constant and every individual variable/constant is an expression of the corresponding type; if
$\mathsf{E}_1$
is an expression of type
$\rho \to \pi$
and
$\mathsf{E}_2$
an expression of type
$\rho$
then
$(\mathsf{E}_1\ \mathsf{E}_2)$
is an expression of type
$\pi$
. Every expression of type
$o$
is called an atom. If
$\mathsf{E}$
is an atom, then
$\mathsf{E}$
and
$(\texttt { not}\, \mathsf{E})$
are literals of type
$o$
; if
$\mathsf{E}_1$
and
$\mathsf{E}_2$
are expressions of type
$\iota$
, then
$(\mathsf{E}_1\approx \mathsf{E}_2)$
and
$\texttt { not}\, (\mathsf{E}_1\approx \mathsf{E}_2)$
are literals of type
$o$
.
We will omit parentheses when no confusion arises.
Definition 2.4. A rule of Higher-Order
$Datalog^\neg$
is a formula
$\mathsf{p}\ \mathsf{R}_1 \cdots \mathsf{R}_n \leftarrow \mathsf{L}_1 \land \ldots \land \mathsf{L}_m$
, where
$\mathsf{p}$
is a predicate constant of type
$\rho _1 \to \cdots \to \rho _n \to {o}$
,
$\mathsf{R}_1,\ldots ,\mathsf{R}_n$
are distinct variables of types
$\rho _1,\ldots ,\rho _n$
respectively and the
$\mathsf{L}_i$
are literals. The literal
$\mathsf{p}\ \mathsf{R}_1 \cdots \mathsf{R}_n$
is the head of the rule and
$ \mathsf{L}_1 \land \ldots \land \mathsf{L}_m$
is the body of the rule. A program
$\mathsf{P}$
of Higher-Order
$Datalog^\neg$
is a finite set of rules.
We will follow the common logic programing practice and write
$\mathsf{L}_1,\ldots ,\mathsf{L}_m$
instead of
$\mathsf{L}_1 \wedge \cdots \wedge \mathsf{L}_m$
for the body of a rule. For brevity reasons, we will often denote a rule as
$\mathsf{p} \ \overline {\mathsf{R}} \leftarrow \mathsf{B}$
, where
$\overline {\mathsf{R}}$
is a shorthand for a sequence of variables
$\mathsf{R}_1 \cdots \mathsf{R}_n$
and
$\mathsf{B}$
represents the body of the rule. By abuse of notation, in the programs that we will write, we will avoid using currying as much as possible and will use tuples instead, a syntax that is more familiar to logic programmers. The tuple syntax can be directly transformed to the curried one by a simple preprocessing. So, for example, instead of succ
X Y we will write succ(
,X,Y), instead of the partial application succ
we will write succ(
), and so on. More generally, the partial application
$\mathsf{p}\ \mathsf{E}_1\ \cdots \ \mathsf{E}_n$
will be written as
$\mathsf{p}(\mathsf{E}_1,\ldots ,\mathsf{E}_n)$
.
The well-founded and the stable model semantics of Higher-Order
$Datalog^\neg$
, were defined in Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis(2024). The main idea of the semantics is to interpret higher-order user-defined predicate constants as three-valued relations over two-valued objects, that is as functions that take classical relations as arguments and return
$\mathit{true}$
,
$\mathit{false}$
, or
$\mathit{undef}$
. This interpretation of predicate constants, apart from giving a simple denotation of the various constructs of the language, also allows one to use Approximation Fixpoint Theory (Denecker et al. Reference Denecker, Marek, TruszczyŃski and Minker2000, Reference Denecker, Marek and Truszczynski2004), in order to define a variety of semantics for the language (such as well-founded, stable, Kripke-Kleene, and so on). For the reader of the main part of the present paper, a deep understanding of this semantics is not necessary: the programs that we give can be understood purely declaratively, without resorting to the help of the semantics (in the same way that a logic programmer does not need to master its model-theoretic semantics in order to write or understand a program). Programs of our language simply define extensional higher-order relations. For example, in the well-founded semantics a unary second-order predicate simply denotes a relation that takes a classical set as an argument and returns
$\mathit{true}$
,
$\mathit{false}$
, or
$\mathit{undef}$
as the result; actually, most of our programs will be stratified (see the forthcoming Definition2.7), which means that they actually denote classical higher-order relations (i.e., they never return
$\mathit{undef}$
as the result). Moreover, in the stable model semantics all the predicates denote two-valued relations. The only points where the reader will have to delve deeper into the semantics, is in order to understand the proofs of certain theorems provided in an appendix as supplementary material. For this reason (and also due to space restrictions) the full presentation of the semantics is also given as supplementary material.
The notion of order of a predicate, is formally defined as follows:
Definition 2.5. The order of a type is recursively defined as follows:

The order of a predicate constant (or variable) is the order of its type.
Definition 2.6. For all
$k\geq 1$
,
$k$
-Order
$Datalog^\neg$
is the fragment of Higher-Order
$Datalog^\neg$
in which all variables have order less than or equal to
$k-1$
and all predicate constants in the program have order less than or equal to
$k$
.
The following example showcases many aspects of the language and additionally introduces some useful predicates that will be needed in our subsequent simulations.
Example 1. We define the relation hamilton(X,Y) which is true if there exists a Hamilton path from vertex X to vertex Y in a graph represented by a binary predicate e which specifies the edges of the graph. The first rule in the definition of hamilton is the following:

The above rule states that there exists a Hamilton path from vertex X to vertex Y if there exists a relation that is a strict total ordering with first element X and last element Y and for every two consecutive elements in
the corresponding edge exists in e. Notice the use of
in the above rule: it is a predicate variable that does not appear in the head of the rule and therefore it is an existentially quantified variable of the body (i.e., the body can be read as “there exists a relation
such that …”). To be a strict total ordering,
must be irreflexive, transitive, and every two different elements must be related. This can be expressed with the following rules:

It is interesting to note above how we can implement universal quantification in the body of a rule: for example, to express the fact that is connected, that is that for all X, Y either X is related to Y or vice-versa, we just require that
is not disconnected, that is it is not the case that there exist X, Y that are not related. This is a common trick that we will use throughout the paper in order to represent universal quantification.
We now define the predicates first, last and succ. Predicate first(,X) is true for X being the individual constant that is the first element with respect to the ordering specified by
. Likewise, last(
,X) is true if X is the last element in
. The predicate succ(
,X,Y) is true for X and Y that are sequential in
.

Finally, we have the rules for subset:

The above two rules use again the trick for implementing universal quantification.
As in the case of first-order logic programs with negation, there exists a simple notion of stratification for higher-order logic programs with negation (Bogaerts et al. Reference Bogaerts, Charalambidis, Chatziagapis, Kostopoulos, Pollaci and Rondogiannis2024).
Definition 2.7. A program
$\mathsf{P}$
is called stratified if there exists a function
$S$
mapping predicate constants to natural numbers, such that for each rule
$\mathsf{p} \ \overline {\mathsf{R}} \leftarrow \mathsf{L}_1,\ldots ,\mathsf{L}_m$
and any
$i\in \{1,\ldots , m\}$
:
-
•
$S(\mathsf{q})\leq S(\mathsf{p})$ for every predicate constant
$\mathsf{q}$ occurring in
$\mathsf{L}_i$ .
-
• If
$\mathsf{L}_i$ is of the form
$(\texttt { not}\,\,\mathsf{E})$ , then
$S(\mathsf{q})\lt S(\mathsf{p})$ for each predicate constant
$\mathsf{q}$ occurring in
$\mathsf{E}$ .
-
• For any subexpression of
$\mathsf{L}_i$ of the form
$(\mathsf{E}_1\,\mathsf{E}_2)$ ,
$S(\mathsf{q})\lt S(\mathsf{p})$ for every predicate constant
$\mathsf{q}$ occurring in
$\mathsf{E}_2$ .
A possibly unexpected aspect of the above definition, is the last item, which says that the stratification function should not only increase because of negation, but also because of higher-order predicate application. The intuitive reason for this is that in Higher-Order
$Datalog^\neg$
one can define a higher-order predicate which is identical to negation, for example, by writing neg P
$\leftarrow$
not P. As a consequence, it is reasonable to assume that predicates occurring inside an application of neg should be treated similarly to predicates appearing inside the negation symbol.
One can easily verify that the Hamiltonian Path program of Example 1, is stratified. As we are going to see, the expressive power of stratified programs is the same as that of the non-stratified ones for orders
$k \geq 2$
under the well-founded semantics.
Languages such as Higher-Order
$Datalog^\neg$
, are usually referred as formal query languages. A program in our language can be considered to compute a query in the following sense: a first-order predicate, like e in Example 1, will be called an input predicate and its denotation (as a set of ground atoms) constitutes what is called the input database, usually denoted by
$D_{in}$
; a first-order predicate like Hamilton in Example 1, will be an output one and its denotation constitutes the output database, usually denoted by
$D_{out}$
.
More formally, a database schema
$\sigma$
is a finite set of first-order predicate symbols with associated arities. A database over a schema
$\sigma$
is a finite set of ground atoms whose predicate symbols belong to
$\sigma$
. A query is a mapping from databases over a schema
$\sigma _1$
to databases over a schema
$\sigma _2$
. A program
$\mathsf{P}$
can be seen as a query
$\mathcal{Q}_{\mathsf{P}}$
such that
$D_{out} = \mathcal{Q}_{\mathsf{P}}(D_{in})$
. We are interested in queries that are generic (Immerman Reference Immerman1986), that is queries that do not depend on the names of the individual constants in the input database. Given a fragment of our language, we are interested in the expressive power of the fragment under a given semantics, namely the set of queries that can be defined by programs of the fragment. In particular, we want to demonstrate that such a fragment captures a complexity class
$\mathcal{C}$
, that is it can express exactly all the queries whose evaluation complexity belongs to
$\mathcal{C}$
. By evaluation complexity we mean the complexity of checking whether a given atom belongs to the output database. Notice that different semantics of the fragments we study may lead to different evaluation complexities and therefore to capturing different complexity classes. Therefore, our results will be of the form “the fragment X of Higher-Order
$Datalog^\neg$
, under the Y semantics, captures the complexity class Z.”
In this work we consider the well-founded semantics, the stable model semantics with cautions reasoning and the stable model semantics with brave reasoning. In particular, under the well-founded semantics, a given atom belongs to the output database if and only if it is true in the well-founded model. Moreover, under the stable model semantics with cautious (resp. brave) reasoning, a given atom belongs to the output database if and only if it is true in all stable models (resp. in at least one stable model).
3 Expressive power under the well-founded semantics
The purpose of this section is to demonstrate that for all
$k\geq 1$
,
$(k+1)$
-Order
$Datalog^\neg$
, under the well-founded semantics, captures
$k-\textsf {EXP}$
. Proofs of such results usually consist of two parts; in our case these two parts can be intuitively described as follows:
-
• We show that every Turing machine that takes as input an encoding of an input database and computes a query over this relation that belongs to
$k-\textsf {EXP}$ , can be simulated by a
$(k+1)$ -Order
$Datalog^\neg$ program (whose meaning is understood under the well-founded semantics).
-
• We show that computing the well-founded semantics of every
$(k+1)$ -Order
$Datalog^\neg$ program over an input database, can be done in
$k$ -exponential time with respect to the number
$n$ of individual constants in the input database.
In the main part of the paper, we focus on the proof of the first result. The proof of the second result is given in an appendix as supplementary material. The proof of the first result has two important points that have to receive special attention, on which we briefly comment below.
Ordering of the input database: A Turing machine encodes an input database as a string on its tape. Such an encoding provides an implicit strict total ordering of the input. On the other hand, Higher-Order
$Datalog^\neg$
does not have a notion of tape, and therefore the input database is, at first sight, unordered. In less powerful languages, such as for example the language of Charalambidis et al. Reference Charalambidis, Nomikos and Rondogiannis(2019), this mismatch is solved by imposing an explicit strict total ordering on the individual constants of the input database. However, it turns out that in Higher-Order
$Datalog^\neg$
we do not need this ordering trick (usually referred in the literature as the ordering assumption): we can generate an ordering
of the constants of the input database and then use it to perform the Turing machine simulation.
Representing numbers: Since we want to simulate the operation of a Turing machine, we need to have a numbering scheme in our language in order to count the steps of the Turing machine and the positions on its tape. At first, we need some base-numbers; actually, it is sufficient to use the
$n$
individual constants in the input database. The trick here is to use these constants directly as numbers: we generate an ordering
on these constants and require that it is a strict total order. We then show how to simulate bigger numbers, namely numbers polynomially related to
$n$
. Finally, since we simulate the operation of a Turing machine that runs in
$k$
-exponential time, we must be able to represent bigger numbers. The trick here is to use higher-order relations: as the order of our programs increases, the more numbers we can represent. Actually, as we demonstrate,
$(k+1)$
-Order
$Datalog^\neg$
is sufficient to represent
$k$
-exponential numbers.
Regarding the ordering of the input database, we already have all the required machinery ready from Example 1. We can use the predicate ordering to generate a strict total order , over the constant symbols of the input database, which we will then use in all our simulations. In other words, the constants of our input database play, under this ordering, the role of base-numbers in our simulation (i.e., “small” numbers that can get up to
$n-1$
). Given an individual constant
$\mathsf{c}$
of the input database, we will say it represents the natural number
$m$
, or more formally
$num(\mathsf{c})=m$
, if and only if in the strict total order
the constant
$\mathsf{c}$
is the
$(m+1)$
-th element. In the following, we see how we can use these base-numbers to represent even bigger ones.
3.1 Representing numbers
Representing polynomially-big numbers: To represent natural numbers up to
$n^{d+1}-1$
, where
$d$
is any arbitrary but fixed natural number, we use tuples of individual constants with fixed length of size
$d+1$
. The following predicates define the “first” and “last” of such numbers (denoted by
$\texttt {first$_0$}$
and
$\texttt {last$_0$}$
) and the “less-than” and “successor” relations on them (denoted by
$\texttt {lt$_0$}$
and
$\texttt {succ$_0$}$
). Notice that the following definitions use the first and last predicates defined in Example 1.

In the tuple-representation of numbers, tuple
$({\texttt { X}}_0,\ldots ,{\texttt { X}}_d)$
of base-elements represents the number
$num{({\texttt { X}}_0,\ldots ,{\texttt { X}}_d)} = num({\texttt { X}}_0) + num({\texttt { X}}_1)\cdot n + \cdots + num({\texttt { X}}_d)\cdot n^{d}$
.
Representing exponentially-big numbers: We now demonstrate how we can represent “exponentially-big” numbers as higher-order relations. In the foregoing discussion we will need the following notation:
$\textrm {exp}_{0}(x) = x$
and
$\textrm {exp}_{n+1}(x) = 2^{\textrm {exp}_{n}(x)}$
. Let
$N_0 = n^{d+1} - 1$
be the largest number that can be represented by
$(d+1)$
-tuples of individual constants and for
$k \geq 1$
, let
$N_k$
be the largest number that can be represented by using
$k$
-order relations. We can exponentially increase the numbers up to the number
$N_{k+1} = \exp _1(N_k + 1)-1$
by using
$(k+1)$
-order relations. One can easily see that
$N_{k} = \exp _{k}(n^{d+1})-1$
.
If the
$k$
-order relations representing numbers up to
$N_k$
are of type
$\rho$
, then it suffices to use higher-order relations of type
$\rho \rightarrow o$
in order to represent numbers up to
$N_{k+1}$
. This is essentially a binary representation where the lower order numbers denote bit positions. Formally, let Z be a
$(k+1)$
-order element and
${\texttt {R}}_0,\ldots ,{\texttt {R}}_{N_k}$
be the ordering of the elements that represent numbers in the previous counting module. Let
$f$
be the function mapping
$\mathit{true}$
to
$1$
and
$\mathit{false}$
to
$0$
. Then we have
$num(\texttt {Z}) = f(\texttt {Z}(\texttt {R}_{0})) + f(\texttt {Z}(\texttt {R}_{1})) \cdot 2 + \cdots + f(\texttt {Z}(\texttt {R}_{N_k}))\cdot 2^{N_k}$
. We begin with predicates testing for the first and the last number.

The following definitions describe the “less than” relation between two elements that represent numbers. We examine if a number N is less than M by comparing the two numbers bit by bit in their binary representation. The successor of a number is defined with the use of less-than.

For
$k=0$
the above code is slightly different: variables X and Y should be replaced with
$\bar {\texttt {X}}$
and
$\bar {\texttt {Y}}$
.
3.2 Turing machine simulation
We now demonstrate how any query that belongs to
$k-\textsf {EXP}$
(
$k\geq 1$
), can be expressed by a
$(k+1)$
-Order
$Datalog^\neg$
program under the well-founded semantics. It suffices to assume that the output schema of the query consists of a single output predicate, since every query can be decomposed into multiple queries of this form. Since the query belongs to
$k-\textsf {EXP}$
, there exists a Turing machine that given on its tape an input database under some sensible encoding, decides whether a tuple belongs to the output relation of the query in at most
$\exp _{k}(n^d)$
steps, where
$n$
is the number of constant symbols in the input database and
$d$
is some constant. We simulate this Turing machine with a
$(k+1)$
-Order
$Datalog^\neg$
program.
Encoding the input: Before presenting the simulation of the Turing machine
$M$
, we mention certain simplifying assumptions, which do not affect the generality of the subsequent results.
-
• The input database consists of a single binary relation in and the output database is also a single binary relation out. In the following, the number of constants in the input database is denoted by
$n$ .
-
• The alphabet of
$M$ that will be simulated is
$\Sigma =\{0,1,\Box \}$ .
$M$ expects the input relation in as the standard binary encoding of a graph, which is based on the ordering of the individual constants, in the first
$n^2$ cells of its tape. For example, if the pair
$(x,y)$ belongs to in, then the tape of
$M$ contains a “1” at cell position
$num(x)+num(y) \cdot n$ , otherwise it contains “0.”
-
•
$M$ decides whether a tuple
$(a,b)$ belongs to the output relation out. The next
$n^2$ cells of its tape are used to encode
$(a,b)$ . All these cells contain the symbol “0,” except for the cell at position
$num(a)+num(b) \cdot n+n^2$ which contains “1.”
-
•
$M$ reaches its accepting state
$yes$ if and only if the tuple
$(a,b)$ belongs to the output relation out.
The following two predicates encode the binary input relation in and the tuple
$(a,b)$
as a binary string.

The predicate input
$_{1}$
(A,B,
,Z
$_0$
,…,Z
$_d$
) is true if the symbol 1 will be written during the initialization of
$M$
in the cell of the tape represented by the number (Z
$_0$
,…,Z
$_d$
). Similarly, input
$_{0}$
(A,B,
,Z
$_0$
,…,Z
$_d$
) is true if the symbol 0 will be written in that position.
Initial configuration of the T
uring M
achine: In order to represent the configurations of the Turing machine we use a higher-order predicate for each state and symbol, and a higher-order predicate for the cursor position. The predicate state
$_{s}$
(A,B,
,T) is true if at time T the Turing machine is in state
$s$
. The predicate symbol
$_{\sigma }$
(A,B,
,T,P), where
$\sigma \in \{0,1,\Box \}$
, is true if at time T the tape has symbol
$\sigma$
written in position P. The predicate cursor(A,B,
,T,P) is true if at time T the cursor is in position P.
We also need a higher-order predicate to lift the tuple representation of numbers to the
$k$
-order numbering notation. Predicate lift
$_{k}$
(
,
$\bar {\texttt {X}}$
,M) transforms the number represented by the tuple
$\bar {\texttt {X}}$
in the zero-order notation to the same number M in the
$k$
-order notation.

We proceed to describe the initial configuration of the Turing machine. At time 0, the machine is in its initial state
$s_0$
, the tape contains only the binary string of the encoded input and all the other positions are filled with the symbol
$\Box$
, and the cursor is in position 0. This is described by the following rules:

We now specify how the execution of the Turing machine is simulated.
Simulating transitions: To describe the transitions of the machine, we create rules which assert the next configuration of the machine, based on the current one and the transition function. Let
$(s,\sigma )\rightarrow (s',\sigma ',\text{right})$
be a transition, indicating that if the current state of the machine is
$s$
and its cursor reads the symbol
$\sigma$
, then the machine changes its state to
$s'$
, it writes
$\sigma '$
on the current cursor position and then moves the cursor to the right. In our simulation we use an auxiliary predicate current
$_{(s,\sigma )}$
(A,B,
,T,P) which is true if at the moment T the state of the machine is
$s$
, the cursor position is P and the cursor reads the symbol
$\sigma$
.

Now, the transition can be simulated by the following rules:

Other types of transitions can be expressed in a similar way. We also add what is commonly called “inertia rules.” They ensure that every position of the tape except for the position of the cursor, retains its content. For any symbol
$\sigma$
we include the following:

Finally, in order to produce the output relation, we use the following:

The out predicate is the one that “initiates the simulation”: it produces, using an existential predicate variable, the ordering that is used throughout the simulation and it verifies that there exists a value T that represents a time point (within the range of representable numbers) such that the machine reaches an accepting state. The above simulation leads to the following theorem:
Theorem 1. Every query in
$k-\textsf {EXP}$
(
$k \geq 1$
) can be expressed by a
$(k+1)$
-Order
$Datalog^\neg$
program.
The opposite direction of the above theorem also holds, as stated by the following theorem.
Theorem 2. Let
$\mathsf{P}$
be a
$(k+1)$
-Order
$Datalog^\neg$
program that defines a query
${\mathcal{Q}}_{\mathsf{P}}$
under the well-founded semantics. Then, there exists a deterministic Turing machine that takes as input an encoding of a database
$D$
that uses
$n$
individual constant symbols and a ground atom
$p(\bar {a})$
, where
$p$
is a predicate constant of
$\mathsf{P}$
and
$\bar {a}$
is a tuple of those individual constants, and decides whether
$p(\bar {a}) \in {\mathcal{Q}}_{\mathsf{P}}(D)$
, in at most
$exp_{k}(n^d)$
steps for some constant
$d$
.
By inspecting the simulation program, one easily sees that it is stratified. Therefore, we get the following result:
Corollary 3.1.
$(k+1)$
-Order
$Datalog^\neg$
and Stratified
$(k+1)$
-Order
$Datalog^\neg$
capture
$k-\textsf {EXP}$
under the well-founded semantics.
As a consequence, Higher-Order
$Datalog^\neg$
and Stratified Higher-Order
$Datalog^\neg$
both capture
$\mathsf{ELEMENTARY}$
(i.e., the union of
$k-\textsf {EXP}$
for all
$k$
).
4 Expressive power under the stable model semantics
In this section, we study the expressiveness of Higher-Order
$Datalog^\neg$
under the stable model semantics. We demonstrate how any query that belongs to
$\textsf {co}-(k-\textsf {NEXP})$
(
$k\geq 1$
), can be expressed by a
$(k+1)$
-Order
$Datalog^\neg$
program under the stable model semantics with cautious reasoning. Since the query belongs to
$\textsf {co}-(k-\textsf {NEXP})$
, there exists a non-deterministic Turing machine
$M$
that decides whether a tuple belongs to the complement of the output relation of the query in at most
$\exp _{k}(n^d)$
steps, where
$n$
is the number of constants in the input database and
$d$
is a constant. Without loss of generality, we assume that each computational path of
$M$
terminates after at most
$\exp _{k}(n^d)$
steps at a state in {yes, no}. We simulate
$M$
with a
$(k+1)$
-Order
$Datalog^\neg$
program.
For the most part, the simulation is the same as that of Section 3. It is intuitively helpful to consider each stable model of the following simulation as a possible computation path the machine could have taken. We will add some additional “choice” rules to simulate those non-deterministic transitions at any possible time step.
Let
$(\sigma ,s)$
be a pair of a symbol and a state of the Turing machine and assume there exist
$m$
possible transitions from this pair. Since the machine is non-deterministic,
$m$
can be greater than
$1$
. We add the following predicates and rules to our program.

This ensures that in every stable model and for every time point T, exactly one of b
$_{\sigma ,s,i}$
(T),
$i \in \{ 1,\ldots ,m \}$
, is true.
Let also the transition table be
$(\sigma ,s) \rightarrow (\sigma '_i,s'_i,\text{move}_i)$
for
$i = 1,\ldots ,m$
. Like in the deterministic case we create rules for each one such transition but we also add the previous branching predicates so that only exactly one rule can be “active” in a stable model.

Assuming, for example, that
$\text{move}_i$
is “right” (and likewise for “left” and “stay”):

If every computational path of the Turing machine reaches state “no,” then for every stable model there exists a time point T such that state
$_{no}$
(A,B,
,T) is true. Therefore, the following rule defines the output relation under cautious reasoning:

The above discussion leads to the following theorem:
Theorem 3. Every query in
$\textsf {co}-(k-\textsf {NEXP})$
can be expressed by a
$(k+1)$
-Order
$Datalog^\neg$
program under the stable model semantics and cautious reasoning.
The following theorem is the converse of the previous one and its proof can be found in the supplementary material.
Theorem 4. Let
$\mathsf{P}$
be a
$(k+1)$
-Order
$Datalog^\neg$
program that defines a query
${\mathcal{Q}}_{\mathsf{P}}$
under the stable model semantics and cautious reasoning. Then, there exists a non-deterministic Turing machine that takes as input an encoding of a database
$D$
that uses
$n$
individual constant symbols and a ground atom
$p(\bar {a})$
, where
$p$
is a predicate constant of
$\mathsf{P}$
and
$\bar {a}$
is a tuple of those individual constants, and decides whether
$p(\bar {a}) \not \in {\mathcal{Q}}_{\mathsf{P}}(D)$
, in at most
$exp_{k}(n^d)$
steps for some constant
$d$
.
Notice that our simulation consists of a stratified program together with the rules that define the b
$_{\sigma ,s,m}$
. We call this fragment of Higher-Order
$Datalog^\neg$
“Stratified+Choices Higher-Order
$Datalog^\neg$
.” We therefore have the following result.
Corollary 4.1.
$(k+1)$
-Order
$Datalog^\neg$
and Stratified+Choices
$(k+1)$
-Order
$Datalog^\neg$
capture
$\textsf {co}-(k-\textsf {NEXP})$
under the stable model semantics and cautious reasoning.
By a similar kind of analysis we can derive a result for the stable model semantics under brave reasoning. The arguments are very similar and omitted.
Corollary 4.2.
$(k+1)$
-Order
$Datalog^\neg$
and Stratified+Choices
$(k+1)$
-Order
$Datalog^\neg$
capture
$k-\textsf {NEXP}$
under the stable model semantics and brave reasoning.
Since
$(k-1)-\textsf {EXP} \subseteq (k-1)-\textsf {NEXP} \subseteq k-\textsf {EXP}$
and
$(k-1)-\textsf {EXP} \subseteq \textsf {co}-((k-1)-\textsf {NEXP}) \subseteq k-\textsf {EXP}$
it follows that Higher-order
$Datalog^\neg$
has the same expressive power (namely
$\mathsf{ELEMENTARY}$
) under both the well-founded and the stable model semantics, in both reasoning schemes.
5 Removing higher-order existential predicate variables
The Turing machine simulations discussed in the previous sections rely extensively on existential predicate variables. Actually, the simulations do not need the full fragment of Higher-Order
$Datalog^\neg$
. In this section, we explore whether the same expressive power can be achieved without the use of such variables but utilizing other powerful constructs of the language, namely partially applied predicates. To this end, we introduce a semantics-preserving transformation that converts every
$(k+1)$
-Order
$Datalog^\neg$
program containing existential predicate variables of order
$k\geq 1$
into an equivalent program of the same order that does not contain existential predicate variables. This result implies that
$(k+1)$
-Order
$Datalog^\neg$
without existential predicate variables has the same expressive power as the full language. Furthermore, the transformation we propose preserves stratification: if the original program is stratified, then the transformed program is stratified as well. In other words, stratified programs without existential predicate variables can be as expressive as unstratified programs with existential predicate variables. We first illustrate the proposed transformation with our Hamilton example.
Example 2. Consider our initial rule for the Hamilton query:

The key idea of the transformation is that instead of using an existential predicate variable for finding an appropriate relation that is a strict total order, we can use an iterative procedure that starts from the empty relation and successively adds to it pairs of individual constants until we get a relation that is indeed a strict total order. In other words, we construct ourselves, in a bottom-up way, the strict total order. The corresponding transformed program is the following:

Notice that in the last rule above, we add a new pair
$\texttt {(Z$_1$,Z$_2$)}$
to the
relation; the definition of add is quite simple and will be given later in the section. The variables
$\texttt {Z$_1$}$
and
$\texttt {Z$_2$}$
are existential but of lower order than the relation
. In other words, our transformation decreases by one the order of the existential predicate variables that the program contains. Thus, if we repeat this process successively, at the end we get a program that only contains existential variables of type
$\iota$
. In the Hamilton program, one step suffices to complete the transformation (the variables
$\texttt {Z$_1$}$
and
$\texttt {Z$_2$}$
are of type
$\iota$
).
We can now provide a more general description of the aforementioned transformation. Let
$\mathsf{P}$
be a program and assume it contains the following rule:

where
$\mathsf{B}[\bar {\texttt {X}},{\texttt {R}}]$
is an expression containing the variables
$\bar {\texttt {X}} = {\texttt {X}}_1,\ldots ,{\texttt {X}}_m$
that occur in the head of the rule and also a predicate variable
$\texttt {R}$
of order
$k \geq 1$
that does not occur in the head and thus is existentially quantified. Let
$\rho _{\texttt {R}}$
be the type of
$\texttt {R}$
. We replace the aforementioned rule with the following set of rules:

The predicate
${\texttt {empty}}_{\rho _{\texttt {R}}}$
defines the empty relation of objects of type
$\rho _{\texttt {R}}$
and the predicate
${\texttt {add}}_{\rho _{\texttt {R}}}$
adds an element to a relation of type
$\rho _{\texttt {R}}$
. The predicate
${\texttt {eq}}_{\rho _{\bar {\texttt {Z}}}}$
is a higher-order equality predicate.

Note that add
$_{\rho _{\texttt {R}}}$
(R,
$\bar {\texttt {Z}}$
) denotes a relation that contains every element
$\bar {\texttt {Y}}$
of
$\texttt {R}$
and also
$\bar {\texttt {Z}}$
. The process introduces only the existential variables
$\bar {\texttt { Z}}$
, which are of order at most
$k-1$
.
We have the following theorem, whose proof is given in the supplementary material.
Theorem 5. Let
$\mathsf{P}$
be a Higher-Order
$Datalog^\neg$
program that defines a query
$\mathcal{Q}$
under the well-founded semantics (resp. stable model semantics with cautious reasoning, stable model semantics with brave reasoning). Let
${\mathsf{P}}'$
be the program that results by applying the aforementioned transformation to some rule of
$\mathsf{P}$
. Then,
${\mathsf{P}}'$
defines the same query
$\mathcal{Q}$
under the well-founded semantics (resp. stable model semantics with cautious reasoning, stable model semantics with brave reasoning).
By applying the transformation described above to each rule and every existential variable of order
$k$
, we obtain a program without such variables. Repeating this process iteratively for variables of order
$k-1$
,
$k-2$
, and so on, we can eventually eliminate all existential predicate variables from the initial program.
If we denote with Higher-Order Datalog
$^{\neg ,\not \exists }$
the fragment of Higher-Order Datalog
$^{\neg }$
that does not contain existential predicate variables, then the following corollary is immediate:
Corollary 5.1.
$(k+1)$
-Order Datalog
$^{\neg ,\not \exists }$
and Stratified
$(k+1)$
-Order Datalog
$^{\neg ,\not \exists }$
capture
$k-\textsf {EXP}$
under the well-founded semantics.
$(k+1)$
-Order Datalog
$^{\neg ,\not \exists }$
and Stratified+Choices
$(k+1)$
-Order Datalog
$^{\neg ,\not \exists }$
under the stable model semantics capture
$\textsf {co}-(k-\textsf {NEXP})$
with cautious reasoning and
$k-\textsf {NEXP}$
with brave reasoning.
Obviously, Higher-Order Datalog
$^{\neg ,\not \exists }$
under any of the aforementioned semantics captures
$\mathsf{ELEMENTARY}$
.
6 Conclusions and future work
We have presented an exploration of the expressive power of Higher-Order
$Datalog^\neg$
under the well-founded and the stable model semantics. Our results identify fragments of the language that, despite being syntactically restricted, possess the same expressive power as the full language. Moreover, our results indicate that by increasing the order of programs under the well-founded semantics we can surpass the expressive power of lower-order programs under the stable model semantics.
There are several challenging directions for future work. First, although our results indicate that by increasing the order of the programs the well-founded semantics can match the power of stable model semantics, it is still unclear to us if there exists a formal transformation from a
$k$
-order program
$\mathsf{P}$
to a
$(k+1)$
-order program
$\mathsf{P}'$
such that the well-founded semantics of
$\mathsf{P}'$
captures, in some sense, the stable model semantics of
$\mathsf{P}$
. Another direction that would be very interesting and certainly quite challenging, would be the implementation of Higher-Order
$Datalog^\neg$
. In general, implementing efficiently non-monotonic extensions of Datalog is already non-trivial even at the first-order case. Probably, a promising direction would be to identify interesting subclasses of Higher-Order
$Datalog^\neg$
that lend themselves to efficient implementation while at the same time retaining some strong expressibility features of the language. For example, as observed by one of the reviewers, it would be interesting to investigate the notion of safety in Higher-Order
$Datalog^\neg$
and whether this notion affects the expressiveness and the potential for efficient implementation of the language.
Supplementary material
The supplementary material for this article can be found at http://dx.doi.org/10.1017/S1471068425100227.
Acknowledgments
This work was supported by a research project which is implemented in the framework of H.F.R.I call “Basic research Financing (Horizontal support of all Sciences)” under the National Recovery and Resilience Plan “Greece 2.0” funded by the European Union - NextGenerationEU (H.F.R.I. Project Number: 16116).