Coinductive logic programming [12, 14, 13, 5] extends standard logic programming with the ability of reasoning about infinite objects and their properties. Whereas syntax of logic programs remains the same, semantics is different. To illustrate this, let us consider the following logic program which defines some predicates on lists of integers, constructed with the standard function symbols  of arity 0 for the empty list and [|] of arity 2 for the list consisting of a head element and a tail. For simplicity, we will consider built-in integers, as they are in Prolog.
The expected meaning is that all_pos() holds if all the elements of are positive, member(,) if is an element of , max(,) if is the greatest element of list . As will be illustrated in detail in Sect. 2, in standard logic programming terms are inductively defined, that is, are finite, and predicates are inductively defined as well. In the example program, only finite lists are considered, such as, e.g., [1|[2|]], and the three predicates are correctly defined on such lists.
In coinductive logic programming, instead, terms are coinductively defined, that is, can be infinite, and predicates are coinductively defined as well. In the example program, also infinite lists such as [1|[2|[3|[4|...]]]], are considered, and the coinductive interpretation of the predicate all_pos gives the expected meaning on such lists. However, this is not the case for the other two predicates: for member the correct interpretation is the inductive one, whereas for max neither the inductive nor the coinductive interpretation are correct: with the former the predicate is always false on infinite lists, with the latter max(,) is true whenever is greater than all the elements of .
The last example shows that the coinductive interpretation of predicates is sometimes not precise enough, in the sense that also wrong facts are included. This problem can be found, and has been studied, also in other programming paradigms, and some solutions have been proposed which allow the programmer to interpret corecursive definitions not in the standard coinductive way [9, 10, 3, 6, 7].
In this paper, we solve the problem in a more foundational way, by applying to the case of logic programs a notion recently introduced in the more general framework of inference systems  (indeed, a logic program can be seen as an inference system where judgments are atoms). That is, programs, consisting of facts and rules with the usual syntax, can be enriched by co-facts (corresponding to coaxioms in ), which syntactically resemble facts but have a special meaning: intuitively, they can only be applied “at infinite depth” in a proof tree, as will be formally defined in the following. By adding co-facts, the intended meaning (declarative semantics) of a program can be a fixed point which is not necessarily the least, nor the greatest one.
In this way, it is possible to express predicates on non well-founded structures, such as infinite lists and graphs, for which the coinductive interpretation would be not precise enough. Moreover, this paradigm nicely subsumes standard (inductive) and coinductive logic programming, since both can be expressed by a particular choice of co-facts, hence inductive and coinductive predicates can coexist in the same program.
For what concerns operational semantics, in coinductive logic programming standard SLD resolution is replaced by co-SLD resolution [14, 5], which, roughly speaking, keeps trace of the already encountered goals, called (dynamic) coinductive hypotheses, so that, when a goal is encountered the second time, it is considered successful. In this paper, we define an operational semantics of logic programs which is similar to co-SLD resolution, but takes co-facts into account, and prove its correctness with respect to the proposed declarative semantics. The proof is interesting since it is a non-trivial application of the bounded coinduction principle introduced in .
The operational semantics we define is in big-step style, as also proposed for co-SLD resolution , and, hence, is amenable for directly deriving an implementation; indeed, we have implemented in SWI-Prolog a prototype meta-interpreter, whose clauses are driven by our operational semantics, and with which we have been able to successfully test all examples shown in this paper, and many others.
The rest of the paper is organized as follows: in Sect. 2 we introduce logic programs with co-facts, their declarative semantics, and the bounded coinduction principle to reason on such programs, illustrating the notions with some examples. In Sect. 3 we formally define operational semantics, show a derivation example, and prove soundness with respect to declarative semantics. In Sect. 4 we describe the prototype meta-interpreter. In Sect. 5 we summarize our contribution, survey related work, and discuss further work.
Assume a signature consisting of sets of predicate symbols , function symbols , and variable symbols , each one with an associated arity , being for variable symbols. A function of arity is called a constant.
Terms are (possibly infinite) trees where nodes are labeled with function and variable symbols and the number of children of a node corresponds to the symbol arity (for a more formal definition based on paths see, e.g., ). Atoms are (possibly infinite) trees where the root is labeled with a predicate symbol and other nodes are labeled with function and variable symbols, also accordingly with the arity. Terms and atoms are ground if they do not contain variable symbols, finite (or syntactic) if they are finite trees.
A logic program is a set of (definite) clauses of shape , where are finite atoms. A clause where is called a fact, otherwise it is called a rule.
A substitution is a mapping from a finite subset of variables into terms. We write for the application of a substitution to a term . We call an instance of . These notions can be analogously defined on atoms and clauses. A substitution is ground (or grounding) if it maps variables into ground terms, it is syntactic if it maps variables into finite (syntactic) terms. The declarative semantics of a logic program describes its meaning in an abstract way, as the set of ground atoms which are defined to be true by the program, in a sense to be made precise depending on the kind of declarative semantics we choose. In the following paragraphs, we briefly recall the standard declarative semantics of logic programs, then their coinductive declarative semantics, and finally we define the declarative semantics generated by co-facts and discuss its advantages.
Standard declarative semantics
In the standard declarative semantics of logic programs, only finite terms and atoms are considered. The Herbrand universe HU is defined as the set of finite ground terms, and the Herbrand base HB as the set of finite ground atoms. Sets are called interpretations. Given a logic program , the (one step) inference operator is defined as follows:
where is the set of instances of clauses in obtained by a ground syntactic substitution.
An interpretation is a model of a program (is closed with respect to ) if . The standard declarative semantics of is the least interpretation which is a model taking as order set inclusion, that is, the intersection of all closed interpretations. Defining a proof tree for a ground atom as a tree where the root is , nodes are ground instances of rules, and leaves are ground instances of facts, the standard declarative semantics can be equivalently characterized as the set of finite ground atoms which have a finite proof tree.
It is easy to see that, with this definition, the predicates of the example program introduced in Sect. 1 have the expected meaning on finite lists. For instance, for the predicate max we obtain all atoms max(,) where is a (ground term representing a) finite list and is the greatest element of .
Co-inductive declarative semantics
A limit of the standard declarative semantics described above is that we cannot define predicates on non-well-founded structures, such as infinite lists or graphs. Considering our running example, we would like to define the predicates all_pos, member, and max on infinite lists as well. To obtain this, first of all infinite terms and atoms should be included. The complete Herbrand Universe co-HU  is the set of (finite and infinite) ground terms. The complete Herbrand base co-HB is the set of (finite and infinite) ground atoms. Sets are called co-interpretations.
We can define the (one step) inference operator analogously to that above:
where is the set of instances of clauses in obtained by a ground substitution.111An instance of a clause in obtained by mapping some variables into infinite ground terms belongs to , but does not belong to .
Again as above, a co-interpretation is a model of a program (is closed with respect to ) if , and we can consider the the least co-interpretation which is a model, that is, the intersection of all closed co-interpretations. This co-interpretation, called in the following inductive (declarative) semantics of and denoted , generalizes the standard declarative semantics of predicates to infinite terms. However, in order to prove predicates on such infinite terms, we would like to allow infinite proof trees as well, and to this end a different declarative semantics can be adopted, explained below.
A co-interpretation is a co-model of (is consistent with respect to ) if and only if . The coinductive (declarative) semantics of , denoted , is the greatest co-interpretation which is a co-model taking as order set inclusion, that is, the union of all consistent co-interpretations. Equivalently, can be characterized as the set of ground atoms which have a (finite or infinite) proof tree.
Let us analyze what happens in the running example. Consider first the predicate all_pos. It is easy to see that, with the inductive semantics , this predicate has the expected meaning only on finite lists (as in the standard semantics) and on infinite lists which have a non positive element. However, for term representing an infinite list of positives, e.g., [1|[2|[3|[4|...]]]], the atom all_pos() cannot be proved by a finite proof tree, hence the predicate turns out to be false. With the coinductive semantics, instead, we can prove all_pos() by the infinite proof tree shown below:
Hence, this predicate is a typical example where coinductive semantics is necessary (when including infinite terms), and provides the expected meaning. However, this is not the case for the other two predicates. More precisely, for (term representing an) infinite list:
member(,) always holds. In this case, the coinductive semantics seems not to be the right choice, and the desired semantics is obtained by taking the inductive semantics (least model) on infinite lists as well, that is, . For this reason, in coinductive logic programming the programmer can specify by a special notation that some predicates should be interpreted inductively rather than coinductively.
max(,) holds whenever is greater than all the elements of . In this case, the coinductive semantics includes the desired semantics (and it is necessary for this, since max(,) would never hold in the inductive semantics), but it is not precise enough. Indeed, for an infinite (regular) list [1|[2|[1|[2|...]]]], we can prove max(,2) as expected, but we can also prove, e.g., max(,4), as shown by the infinite proof trees (T1) and (T2), respectively, shown below:
Declarative semantics generated by co-facts
In order to define this semantics, first of all the syntax is slightly generalized allowing, besides facts and rules, co-facts, written .A. Co-facts are finite atoms, hence syntactically resemble facts, but have a special meaning. Below is the version equipped with co-facts of the running example.
In the following, the metavariable denotes a set of co-facts (finite atoms).
The (declarative) semantics of a program generated by co-facts , denoted , is defined as follows.
First, we consider the program obtained by enriching by the co-facts in considered as facts, and we take its inductive semantics .
Then, we take the largest co-model which is included in . In other words, we take the coinductive interpretation of where, however, clauses are instantiated only on elements of .
Note that is different from . For instance, let be the program
and be the singleton set consisting of the co-fact
Then, , and . Hence the intersection is , whereas .
As we have shown in  in the more general framework of inference systems, corresponds to a fixed-point of the operator which is neither the greatest, nor the least one.
In terms of proof trees, is the set of ground atoms which have a (finite or infinite) proof tree in whose nodes all have a finite proof tree in . Taking this semantics, all the predicates in the running example get the expected meaning. Indeed, for (term representing an) infinite list:
all_pos() holds for infinite list of positives, since the atom all_pos() has the previously shown infinite proof tree, and all its nodes have a (trivial) finite proof tree in , consisting in an instantiation of the co-fact.
member(,) only holds if belongs to . Otherwise, there would exist an infinite proof tree in , but its nodes have no finite proof tree in , since there are no co-facts, and the only fact is not applicable.
max(,) only holds when is the greatest element of . In this case, as shown at page 2, there exists an infinite proof tree in whenever is greater than all the elements of . However, a finite proof tree in for each node only exists when belongs to the list, hence is actually the greatest element. For instance, the two nodes of the infinite proof tree (T1) for max([1|[2|[1|[2|...]]]],2) have the finite proof trees (FT1) and (FT2) shown below:
whereas there is no finite proof tree for the nodes of the infinite proof tree (T2). In other words, co-facts allow the programmer to “filter out” atoms which should not be true, making the semantics precise.
Note that the condition to have a finite proof tree in trivially holds for nodes which are roots of a finite subtree (a finite proof tree in is a finite proof tree in as well), hence is only significant for nodes which occur in an infinite path. Moreover, if the infinite tree is rational, that is, has a finite number of different subtrees, then an infinite path always consists of (possibly) a finite prefix and a period. Hence, if the condition holds for the first node of the period, then it holds for all the other nodes. In the example of (T1) above, another finite proof tree for the second node max([2|[1|[2|...]]],2) can be obtained from (FT1) for the first node:
Correspondingly, in the operational semantics which will be provided in Sect. 3, standard SLD resolution in is triggered when an atom is encountered the second time.
Note also that, as the examples above clearly show, the inductive and coinductive semantics can be obtained as special cases of the semantics generated by co-facts of a program, notably:
the inductive semantics when the set of co-facts is empty;
the coinductive semantics when the set of (ground instances of) co-facts is co-HB.
That is, co-facts allow to mix together, without any need of a special notation, predicates for which the appropriate interpretation is either inductive or coinductive, and to express predicates for which the appropriate interpretation is neither of the two, as the max example shows.
Let be a set of ground atoms, corresponding to the intended meaning of some predicates.
In order to prove that the the atoms in are defined to be true by a program enriched by co-facts , that is, , we can use the following bounded coinduction principle, which is a generalization of the standard coinduction principle. This and other proof techniques are illustrated in the more general framework of inference systems in .
Theorem 2.1 (Bounded coinduction principle).
If the following two conditions hold:
, that is, each atom in has a finite proof tree in
, that is, for each atom ,
The two conditions corresponds to require that is a co-model of (is consistent with respect to ) which is bounded by (included in) , and is defined as the largest such co-model. ∎
The standard coinduction principle can be obtained as a specific instance of the principle above, when (ground instances of) coincide with co-HB; for this particular case the first condition trivially holds.
We illustrate the proof technique by formally proving that, in the running example, we can derive all atoms in the set .
To prove boundedness, we have to show that, for each max(,) such that is the greatest element of , max(,) has a finite proof tree in . This can be easily shown. Indeed, if is the greatest element of , then with , for . Hence, max(,) has a finite proof tree in which consists in nodes which are instances of the rule and a leaf which is an instance of the co-fact (a concrete example for max([2|[1|[2|1|...]]],2) has been shown before).
To prove consistency, we have to show that, for each max(,) such that greatest element of , max(,) is the consequence of (an instance of) a clause with all atoms of the body in . This can be easily shown, indeed, if is the greatest element of , then with and greatest element of . Hence, max(,) is the consequence of the following instance of the rule: max([|],) :- max(,), is max(,) where the atom max(,) belongs to .
The same proof technique will be used in the next section to show that the operational semantics is sound with respect to the semantics generated by co-facts.
3 Big-step operational semantics
In this section we define an operational counterpart of the semantics generated by co-facts introduced in Sect. 2, and prove its correctness.
This operational semantics is a generalization of SLD [11, 8] and co-SLD [12, 14, 13] resolution. However, it is presented, rather than in the traditional small-step style, in big-step style, as introduced in . This style turns out to be simpler since coinductive hypotheses (see below) can be kept local. Moreover, it naturally leads to an interpreter, and the proof of soundness with respect to declarative semantics is more direct since we compare two inference systems. For a proof of equivalence of big-step and small-step co-SLD resolution see .
We introduce some notations. First of all, in this section the metavariable denotes finite (syntactic) atoms and the metavariables denote finite (syntactic) terms. An equation has shape where and are finite terms. We write for a set of equations. Equations allow a finite (syntactic) representation also of rational terms and atoms, that is, having a finite number of different subterms, see, e.g., [2, 5] for the details. Finally, a goal is a sequence of atoms, and the metavariable denotes a syntactic goal, that is, a sequence of finite atoms. The empty sequence is denoted by .
The operational semantics is defined by a judgment , meaning that, given a program and a set of co-facts , resolution of the (syntactic) goal succeeds and produces a set of equations that describes a solution of the goal. For instance, assuming and from the previous section, which define, among others, the predicate max computing the greatest element of a list, it would be possible to derive the following judgment
meaning that a solution of the goal max(L,2) is the infinite term l = [1|[2|[1|2| ... ]]].
This judgment is defined following a schema which is similar to co-SLD resolution, in the sense that resolution keeps track of the already encountered atoms, which are called (dynamic) coinductive hypotheses . However, when the same atom is encountered the second time, rather than just considering successful as it happens in co-SLD resolution, standard SLD resolution of is triggered in the program obtained by enriching by the co-facts in .
Formally, two auxiliary judgments are introduced:
, meaning that, given a program and a set of co-facts , resolution of the goal represented by and , under the coinductive hypotheses , succeeds producing a set of equations which describes a solution of the goal.
meaning that, given a program , standard SLD resolution of the goal represented by and succeeds producing a set of equations which describes a solution of the goal.
Rules (inductively) defining these judgments are shown in Fig. 1.
The first two rules define the judgment corresponding to standard SLD resolution. In rule (empty), resolution of the empty goal succeeds. In rule (step), an atom is selected from the goal to be resolved, and a clause of the program is chosen such that the selected atom unifies with the head of the clause, as expressed by the fact that adding the equations to the current set we get a solvable set of equations. Then, resolution of the original goal succeeds if resolution of both the body of the clause and the remaining goal succeed.
The following three rules define the judgment corresponding to an intermediate step of resolution. Rules (co-empty) and (co-step) are analogous to rules (empty) and (step). The only difference is that, in rule (co-step), in the resolution of the body of the clause, the selected atom is added to the current set of coinductive hypotheses. In this way, rule (co-hyp) can handle the case when resolution encounters the same atom for the second time, that is, more formally, the selected atom unifies with a coinductive hypothesis, as expressed by the fact that adding the equations to the current set we get a solvable set of equations. In this case, standard SLD resolution of such atom is triggered in the program obtained by enriching by the co-facts in , and resolution of the original goal succeeds if both such standard SLD resolution of the selected atom and resolution of the remaining goal succeed.
Finally, rule (main) defines the main judgment corresponding to an initial step of resolution, where the goal is syntactic, that is, no equation has been produced yet.
In the following, to avoid confusion with proof trees considered in Sect. 2, trees obtained by instantiating the rules of big-step operational semantics are called derivations.
A derivation example
Assuming and from the previous section, which define, among others, the predicate max computing the greatest element of a list, we show a derivation for the judgment
, with222We use the abbreviated Prolog syntax for lists in this example. , . For keeping the derivation simpler, in rules (co-step) and (step) we do not manifest some substitutions, but, instead, we implicitly apply them in the resulting goals. Furthermore, we consider the predicate _ is max(_,_) to be predefined, as it is the case in real Prolog systems; for it we have introduced a special semantic rule (labeled with p, for predefined) that deals with standard inductive predefined predicates (see Sect. 4). Finally, for space reasons we have abbreviated the names of the applied rules.
with , , , , and where , , and are the following derivations:
In the whole derivation the co-fact for max is used just once in rule (step) marked with (s*) in derivation; the co-fact could be employed also in rule (step) in derivation , but without success, since the substitution cannot satisfy the goal M1 is max(2,M2),M is max(1,M1).
The big-step operational semantics is sound with respect to the declarative semantics defined in Sect. 2. That is, if resolution of the goal succeeds producing a set of equations which describe a solution of the goal, then (any ground instance of) this solution is a set of atoms which are true in the declarative semantics. Formally, set the set of the ground solutions of , necessarily defined on the set of the variables occurring in . In the following, when we pick a substitution from , we will implicitly assume that this substitution has as domain. Note that the set of ground solutions is antitone with respect to set inclusion, that is, if then . Soundness can be stated as follows.
Theorem 3.1 (Soundness).
If holds then, for each , .
The proof of soundness is an application of the bounded coinduction principle (Theorem 2.1) introduced in Sect. 2. For sake of clarity, we provide the proof in a top-down manner, that is, we first give the proof schema, and then state and prove the three needed subtheorems.
Set the set of atoms which are true in the operational semantics, that is, . We write if and agree on the common domain, that is, for each , . Note that, if , then is a well-defined substitution; analogously, we write if each ground solution of agrees with a ground solution of on the common domain, that is, for each there exists such that .
Proof of Theorem 3.1.
Thanks to Lemma 3.2(4), the statement can be equivalently formulated as
. By bounded coinduction, we have to show that:
, that is, each has a finite proof tree in .
, that is, for all ,
there are such that .
The two conditions can be proved as follows.
Now we detail the proof, by stating and proving the three theorems and the lemma used above. Some proofs are given in Appendix A. We first introduce the following notations. If , and , then we write for the set of equations that represents the unification of with , that is, .
The boundedness condition is obtained by Theorem 3.2 and Theorem 3.3. The former states that resolution under coinductive hypotheses implies standard SLD resolution in the program enriched by co-facts, the latter states that standard SLD resolution is sound with respect to the inductive semantics.
We start with an auxiliary lemma.
If holds, then
if then holds, with
if is solvable then holds
Straightforward induction on inference rules. ∎
If holds and is solvable then holds, with .
The following theorem states that if resolution under coinductive hypotheses of a goal succeeds, then standard SLD resolution of the goal in the program enriched by co-facts succeeds as well.
If holds, then holds with .
By induction on the inference rules which define .
We trivially conclude by rule (empty).
By inductive hypothesis we get that and hold with , and a fresh renaming of a clause in . By Lemma 3.1(1) we get that holds, with , thus by rule (step) we get the thesis.
The following theorem states that if standard SLD resolution of a goal succeeds, producing a set of equations which describes a solution of the goal, then this solution is a set of atoms which are true in the inductive semantics. In other words, this theorem states the standard soundness property of SLD resolution. The theorem could be derived by showing equivalence of the judgment with the traditional small-step definition of SLD resolution (as done in  for co-SLD resolution), and relying on the well-know soundness of the latter. A direct proof can be done by induction on the rules which define , as stated below
Theorem 3.3 (Soundness of standard SLD resolution).
If then, for each , .
We state now some lemmas needed to prove Theorem 3.4.
The following lemma states some properties of the judgment . In particular, points 1 and 2 state a form of monotonicity with respect to the set of coinductive hypotheses and the set of input equations: that is, we can freely add coinductive hypotheses and remove input equations preserving the results of the evaluation. Point 3 states another monotonicity property: we can add input equations provided that this addition does not break the solvability of the output system of equations. Finally, point 4 states a decomposition property: the evaluation of a single atom in a goal produces a subset of the equations produced by the entire goal.
If holds, then:
if then holds
if then holds for some
if is solvable then holds
if then holds for some
Straightforward induction on inference rules. ∎
As a consequence we get the following result.
If holds and is solvable, then holds, with .
From point 2 and 3 of Lemma 3.2. ∎
If holds, and is solvable, then holds and .
The proof can be found in the Appendix.
If , , and , then .
Theorem 3.4 (Consistency).
If holds and , then there exist atoms such that and holds with .
Since by hypothesis holds, we have necessarily applied rule (main), hence
holds. We have also necessarily applied rule (co-step), hence holds,
for some fresh renamed clause in such that is solvable.
Therefore for all we have , and since , that implies , we get .
Now we have to show that holds for some such that . We prove this statement in two steps.
First we prove that for some with
Then we remove the equations in .
The first part can be proved by showing that for each judgement in the derivation of , also the judgement holds for some such that . This statement can be proved by induction on the derivation as follows.
The rule has the following shape:
By inductive hypothesis we have and , with and . Consider , since , we get , therefore that is, ; moreover there exist and such that and , because and . Since all introduced variables are fresh, we have that , that is,